Repository navigation
Expand file tree
/
Copy pathsettingsactions.cpp
More file actions
136 lines (125 loc) · 5 KB
/
Copy pathsettingsactions.cpp
File metadata and controls
136 lines (125 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "settingsactions.hpp"
#include "eterm.hpp"
#include "settingspanel.hpp"
#include <eepp/ui/tools/uifontpickerdialog.hpp>
#include <eepp/ui/tools/uisettingspanel.hpp>
namespace eterm {
SettingsActions::SettingsActions( App* app ) : mApp( app ) {}
void SettingsActions::showSettings( const std::string& category ) {
if ( mSettingsWindow ) {
mSettingsWindow->show();
mSettingsWindow->toFront();
auto* panel = mSettingsWindow->find<Tools::UISettingsPanel>( "settings_panel_content" );
if ( !category.empty() )
panel->selectCategory( category );
panel->runOnMainThread( [panel] { panel->focusSearch(); } );
return;
}
mSettingsWindow = SettingsPanel::create( *mApp );
mSettingsWindow->on( Event::OnWindowClose,
[this]( const Event* ) { mSettingsWindow = nullptr; } );
if ( !category.empty() ) {
mSettingsWindow->find<Tools::UISettingsPanel>( "settings_panel_content" )
->selectCategory( category );
}
}
void SettingsActions::closeSettings() {
if ( mSettingsWindow )
mSettingsWindow->closeWindow();
}
void SettingsActions::openFontPicker( bool uiFont, bool fallbackFont ) {
const Uint32 flags = UIFontPickerDialog::ShowStyle |
( fallbackFont ? 0 : UIFontPickerDialog::ShowSize ) |
( !uiFont && !fallbackFont ? UIFontPickerDialog::MonospaceOnly : 0 );
auto* dialog = UIFontPickerDialog::New( flags );
dialog->setTitle( mApp->i18n( "select_font", "Select Font" ) );
dialog->setCloseShortcut( KEY_ESCAPE );
std::string currentPath = uiFont ? mApp->config->font.uiPath
: fallbackFont ? mApp->config->font.fallbackPath
: mApp->config->font.path;
// Not configured: the font in use (the bundled one, or the first explicit fallback).
if ( currentPath.empty() ) {
if ( uiFont ) {
currentPath =
FontTrueType::filePathOf( mApp->scene->getUIThemeManager()->getDefaultFont() );
} else if ( fallbackFont ) {
const auto& fallbacks =
mApp->scene->getResourceScope()->getFontService().getFallbackFonts();
if ( !fallbacks.empty() )
currentPath = FontTrueType::filePathOf( fallbacks.front().get() );
} else {
currentPath = FontTrueType::filePathOf( mApp->terminalFont );
}
}
if ( !currentPath.empty() )
dialog->setSelectedFont( currentPath );
if ( !fallbackFont ) {
auto selection = dialog->getSelection();
selection.size = static_cast<Uint32>(
!uiFont ? mApp->config->font.size
: mApp->config->font.uiSize > 0
? mApp->config->font.uiSize
: PixelDensity::pxToDp( mApp->scene->getUIThemeManager()->getDefaultFontSize() ) );
dialog->setSelection( selection );
}
dialog->setOnFontPicked( [this, uiFont, fallbackFont]( const UIFontSelection& selection ) {
if ( selection.font.path.empty() )
return;
auto& resourceScope = *mApp->scene->getResourceScope();
if ( uiFont ) {
auto font = FontTrueType::New( "eterm-ui-font", resourceScope );
if ( font->loadFromFile( selection.font.path ) ) {
mApp->config->font.uiPath = selection.font.path;
mApp->config->font.uiSize = selection.size;
applyUIFont( font.get(), mApp->config->font.uiSize );
}
} else if ( fallbackFont ) {
auto font = FontTrueType::New( "eterm-fallback-font", resourceScope );
if ( font->loadFromFile( selection.font.path ) ) {
mApp->config->font.fallbackPath = selection.font.path;
// It replaces the previous one (fallbacks are looked up in order).
auto& service = resourceScope.getFontService();
auto previous = service.getFallbackFonts();
for ( const auto& fallback : previous )
service.removeFallbackFont( fallback.get() );
service.addFallbackFont( std::move( font ) );
}
} else {
auto font = FontTrueType::New( "eterm-monospace", resourceScope );
if ( font->loadFromFile( selection.font.path ) ) {
mApp->config->font.path = selection.font.path;
mApp->config->font.size = selection.size;
mApp->terminalFont = font.get();
mApp->terminalFontSize = PixelDensity::dpToPx( mApp->config->font.size );
FontFamily::loadFromRegular( mApp->terminalFont );
mApp->forEachTerminal( [this]( UITerminal* terminal ) {
terminal->setFont( mApp->terminalFont );
terminal->setFontSize( mApp->terminalFontSize );
} );
}
}
mApp->savePreferences();
} );
dialog->center();
dialog->show();
}
void SettingsActions::setUIFontSize( Float size ) {
mApp->config->font.uiSize = size;
applyUIFont( nullptr, size );
mApp->savePreferences();
}
void SettingsActions::setTerminalFontSize( Float size ) {
mApp->config->font.size = size;
mApp->terminalFontSize = PixelDensity::dpToPx( size );
mApp->forEachTerminal(
[this]( UITerminal* terminal ) { terminal->setFontSize( mApp->terminalFontSize ); } );
mApp->savePreferences();
}
void SettingsActions::applyUIFont( Font* font, Float sizeDp ) {
mApp->scene->applyDefaultFont( font, PixelDensity::dpToPx( sizeDp ) );
}
void SettingsActions::applyFontRendering() {
mApp->scene->setFontRendering( mApp->config->font.hinting, mApp->config->font.antialiasing );
mApp->forEachTerminal( []( UITerminal* terminal ) { terminal->syncFontRenderingConfig(); } );
}
} // namespace eterm