Repository navigation
Expand file tree
/
Copy pathappconfig.cpp
More file actions
249 lines (236 loc) · 13.5 KB
/
Copy pathappconfig.cpp
File metadata and controls
249 lines (236 loc) · 13.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "appconfig.hpp"
#include <algorithm>
#include <eepp/system/filesystem.hpp>
#include <eepp/window/window.hpp>
#include <nlohmann/json.hpp>
using namespace EE::Graphics;
using namespace EE::System;
using namespace EE::UI;
using namespace eterm::Terminal;
using EE::eemax;
namespace eterm {
AppConfig::AppConfig( std::string configPath ) : mConfigPath( std::move( configPath ) ) {
FileSystem::dirAddSlashAtEnd( mConfigPath );
mIni.path( mConfigPath + "config.cfg" );
mState.path( mConfigPath + "state.cfg" );
}
void AppConfig::load() {
mIni.loadFromFile( mIni.path() );
mState.loadFromFile( mState.path() );
terminal.shell = mIni.getValue( "terminal", "shell", terminal.shell );
terminal.shellArguments =
mIni.getValue( "terminal", "shell_arguments", terminal.shellArguments );
// The legacy working_directory key also captured one-off command-line paths.
terminal.defaultWorkingDirectory =
mIni.getValue( "terminal", "default_working_directory", terminal.defaultWorkingDirectory );
terminal.executeInShell =
mIni.getValue( "terminal", "execute_in_shell", terminal.executeInShell );
terminal.historySize = mIni.getValueU( "terminal", "scrollback", terminal.historySize );
terminal.cursorStyle = TerminalCursorHelper::modeFromString( mIni.getValue(
"terminal", "cursor_style", TerminalCursorHelper::modeToString( terminal.cursorStyle ) ) );
terminal.useFrameBuffer = mIni.getValueB( "terminal", "framebuffer", terminal.useFrameBuffer );
terminal.closeOnExit = mIni.getValueB( "terminal", "close_on_exit", terminal.closeOnExit );
terminal.initialTabs = mIni.getValueU( "terminal", "initial_tabs", terminal.initialTabs );
terminal.exclusiveMode = mIni.getValueB( "terminal", "exclusive_mode", terminal.exclusiveMode );
terminal.inheritWorkingDirectory =
mIni.getValueB( "terminal", "inherit_working_directory", terminal.inheritWorkingDirectory );
terminal.allowOsc52ClipboardWrite = mIni.getValueB( "terminal", "allow_osc52_clipboard_write",
terminal.allowOsc52ClipboardWrite );
terminal.enableOsc8Hyperlinks =
mIni.getValueB( "terminal", "enable_osc8_hyperlinks", terminal.enableOsc8Hyperlinks );
terminal.graphemeClustering =
mIni.getValueB( "terminal", "grapheme_clustering", terminal.graphemeClustering );
terminal.detectLinks = mIni.getValueB( "terminal", "detect_links", terminal.detectLinks );
terminal.showProgramStatus =
mIni.getValueB( "terminal", "show_program_status", terminal.showProgramStatus );
terminal.programStatusNotifications = mIni.getValueB(
"terminal", "program_status_notifications", terminal.programStatusNotifications );
terminal.programStatusSounds =
mIni.getValueB( "terminal", "program_status_sounds", terminal.programStatusSounds );
terminal.programStatusAlertsWhenActive = mIni.getValueB(
"terminal", "program_status_alerts_when_active", terminal.programStatusAlertsWhenActive );
terminal.programStatusAlertsAllStates = mIni.getValueB(
"terminal", "program_status_alerts_all_states", terminal.programStatusAlertsAllStates );
terminal.programStatusSound =
mIni.getValue( "terminal", "program_status_sound", terminal.programStatusSound );
terminal.programStatusSoundVolume =
std::clamp( mIni.getValueI( "terminal", "program_status_sound_volume",
terminal.programStatusSoundVolume ),
0, 100 );
terminal.restoreLastSession =
mIni.getValueB( "terminal", "restore_last_session", terminal.restoreLastSession );
terminal.recoverHarnessSessions =
mIni.getValueB( "terminal", "recover_harness_sessions", terminal.recoverHarnessSessions );
ui.smoothScroll = mIni.getValueB(
"ui", "smooth_scroll", mIni.getValueB( "terminal", "smooth_scroll", ui.smoothScroll ) );
const auto newTerminalBehavior =
mIni.getValue( "terminal", "new_terminal_behavior", "current" );
terminal.newTerminalBehavior =
newTerminalBehavior == "vertical" ? NewTerminalBehavior::VerticalSplit
: newTerminalBehavior == "horizontal" ? NewTerminalBehavior::HorizontalSplit
: NewTerminalBehavior::CurrentTabBar;
terminal.scrollBarType = mIni.getValue( "terminal", "scrollbar_type", "overlay" ) == "outside"
? ScrollViewType::Outside
: ScrollViewType::Overlay;
const auto scrollBarMode = mIni.getValue( "terminal", "scrollbar_mode", "auto" );
terminal.scrollBarMode = scrollBarMode == "always_on" ? ScrollBarMode::AlwaysOn
: scrollBarMode == "always_off" ? ScrollBarMode::AlwaysOff
: ScrollBarMode::Auto;
font.path = mIni.getValue( "font", "path", font.path );
font.fallbackPath = mIni.getValue( "font", "fallback_path", font.fallbackPath );
font.size = mIni.getValueF( "font", "size", font.size );
font.hinting = Font::fontHintingFromString(
mIni.getValue( "font", "hinting", Font::fontHintingToString( font.hinting ) ) );
font.antialiasing = Font::fontAntialiasingFromString( mIni.getValue(
"font", "antialiasing", Font::fontAntialiasingToString( font.antialiasing ) ) );
font.uiPath = mIni.getValue( "font", "ui_path", font.uiPath );
font.uiSize = mIni.getValueF( "font", "ui_size", font.uiSize );
window.pixelDensity = mIni.getValueF( "window", "pixel_density", window.pixelDensity );
window.maxFPS = mIni.getValueU( "window", "frame_rate_limit", window.maxFPS );
window.vsync = mIni.getValueB( "window", "vsync", window.vsync );
window.exitOnClose = mIni.getValueB( "window", "exit_on_close", window.exitOnClose );
window.benchmarkMode = mIni.getValueB( "window", "benchmark_mode", window.benchmarkMode );
window.warnBeforeClose =
mIni.getValueB( "window", "warn_before_closing", window.warnBeforeClose );
window.alwaysShowTabBar =
mIni.getValueB( "window", "always_show_tab_bar", window.alwaysShowTabBar );
window.showTabBarWhenSplit =
mIni.getValueB( "window", "show_tab_bar_when_split", window.showTabBarWhenSplit );
window.tabPosition = UITabWidget::tabPositionFromString( mIni.getValue(
"window", "tab_position", UITabWidget::tabPositionToString( window.tabPosition ) ) );
window.rendererVersion = Renderer::glVersionFromString(
mIni.getValue( "window", "renderer_version",
Renderer::graphicsLibraryVersionToString( window.rendererVersion ) ) );
window.multisamples = mIni.getValueU( "window", "multisamples", window.multisamples );
theme.colorScheme = mIni.getValue( "theme", "color_scheme", theme.colorScheme );
theme.uiColorScheme = ColorSchemePreferences::fromStringExt( mIni.getValue(
"theme", "ui_color_scheme", ColorSchemePreferences::toString( theme.uiColorScheme ) ) );
windowState.size.setWidth( mState.getValueI( "window", "width", windowState.size.getWidth() ) );
windowState.size.setHeight(
mState.getValueI( "window", "height", windowState.size.getHeight() ) );
windowState.position.x = mState.getValueI( "window", "x", windowState.position.x );
windowState.position.y = mState.getValueI( "window", "y", windowState.position.y );
windowState.displayIndex =
mState.getValueI( "window", "display_index", windowState.displayIndex );
windowState.maximized = mState.getValueB( "window", "maximized", windowState.maximized );
windowState.tabBarWidth =
mState.getValueF( "window", "tab_bar_width", windowState.tabBarWidth );
}
bool AppConfig::save() {
if ( !FileSystem::isDirectory( mConfigPath ) && !FileSystem::makeDir( mConfigPath, true ) )
return false;
return savePreferences() && saveWindowState();
}
bool AppConfig::savePreferences() {
if ( !FileSystem::isDirectory( mConfigPath ) && !FileSystem::makeDir( mConfigPath, true ) )
return false;
mIni.deleteValue( "theme", "language" );
mIni.deleteValue( "window", "max_fps" );
mIni.setValue( "terminal", "shell", terminal.shell );
mIni.setValue( "terminal", "shell_arguments", terminal.shellArguments );
mIni.deleteValue( "terminal", "working_directory" );
mIni.setValue( "terminal", "default_working_directory", terminal.defaultWorkingDirectory );
mIni.setValue( "terminal", "execute_in_shell", terminal.executeInShell );
mIni.setValueU( "terminal", "scrollback", terminal.historySize );
mIni.setValue( "terminal", "cursor_style",
TerminalCursorHelper::modeToString( terminal.cursorStyle ) );
mIni.setValueB( "terminal", "framebuffer", terminal.useFrameBuffer );
mIni.setValueB( "terminal", "close_on_exit", terminal.closeOnExit );
mIni.setValueU( "terminal", "initial_tabs", terminal.initialTabs );
mIni.setValueB( "terminal", "exclusive_mode", terminal.exclusiveMode );
mIni.setValueB( "terminal", "inherit_working_directory", terminal.inheritWorkingDirectory );
mIni.setValueB( "terminal", "allow_osc52_clipboard_write", terminal.allowOsc52ClipboardWrite );
mIni.setValueB( "terminal", "enable_osc8_hyperlinks", terminal.enableOsc8Hyperlinks );
mIni.setValueB( "terminal", "grapheme_clustering", terminal.graphemeClustering );
mIni.setValueB( "terminal", "detect_links", terminal.detectLinks );
mIni.setValueB( "terminal", "show_program_status", terminal.showProgramStatus );
mIni.setValueB( "terminal", "program_status_notifications",
terminal.programStatusNotifications );
mIni.setValueB( "terminal", "program_status_sounds", terminal.programStatusSounds );
mIni.setValueB( "terminal", "program_status_alerts_when_active",
terminal.programStatusAlertsWhenActive );
mIni.setValueB( "terminal", "program_status_alerts_all_states",
terminal.programStatusAlertsAllStates );
mIni.setValue( "terminal", "program_status_sound", terminal.programStatusSound );
mIni.setValueI( "terminal", "program_status_sound_volume", terminal.programStatusSoundVolume );
mIni.setValueB( "terminal", "restore_last_session", terminal.restoreLastSession );
mIni.setValueB( "terminal", "recover_harness_sessions", terminal.recoverHarnessSessions );
mIni.setValueB( "ui", "smooth_scroll", ui.smoothScroll );
mIni.deleteValue( "terminal", "smooth_scroll" );
mIni.setValue( "terminal", "new_terminal_behavior",
terminal.newTerminalBehavior == NewTerminalBehavior::VerticalSplit ? "vertical"
: terminal.newTerminalBehavior == NewTerminalBehavior::HorizontalSplit
? "horizontal"
: "current" );
mIni.setValue( "terminal", "scrollbar_type",
terminal.scrollBarType == ScrollViewType::Overlay ? "overlay" : "outside" );
mIni.setValue( "terminal", "scrollbar_mode",
terminal.scrollBarMode == ScrollBarMode::AlwaysOn ? "always_on"
: terminal.scrollBarMode == ScrollBarMode::AlwaysOff ? "always_off"
: "auto" );
mIni.setValue( "font", "path", font.path );
mIni.setValue( "font", "fallback_path", font.fallbackPath );
mIni.setValueF( "font", "size", font.size );
mIni.setValue( "font", "hinting", Font::fontHintingToString( font.hinting ) );
mIni.setValue( "font", "antialiasing", Font::fontAntialiasingToString( font.antialiasing ) );
mIni.setValue( "font", "ui_path", font.uiPath );
mIni.setValueF( "font", "ui_size", font.uiSize );
mIni.setValueF( "window", "pixel_density", window.pixelDensity );
mIni.setValueU( "window", "frame_rate_limit", window.maxFPS );
mIni.setValueB( "window", "vsync", window.vsync );
mIni.setValueB( "window", "exit_on_close", window.exitOnClose );
mIni.setValueB( "window", "benchmark_mode", window.benchmarkMode );
mIni.setValueB( "window", "warn_before_closing", window.warnBeforeClose );
mIni.setValueB( "window", "always_show_tab_bar", window.alwaysShowTabBar );
mIni.setValue( "window", "tab_position",
UITabWidget::tabPositionToString( window.tabPosition ) );
mIni.setValueB( "window", "show_tab_bar_when_split", window.showTabBarWhenSplit );
mIni.setValue( "window", "renderer_version",
Renderer::graphicsLibraryVersionToString( window.rendererVersion ) );
mIni.setValueU( "window", "multisamples", window.multisamples );
mIni.setValue( "theme", "color_scheme", theme.colorScheme );
mIni.setValue( "theme", "ui_color_scheme",
ColorSchemePreferences::toString( theme.uiColorScheme ) );
return mIni.writeFile();
}
bool AppConfig::saveWindowState() {
if ( !FileSystem::isDirectory( mConfigPath ) && !FileSystem::makeDir( mConfigPath, true ) )
return false;
mState.setValueI( "window", "width", windowState.size.getWidth() );
mState.setValueI( "window", "height", windowState.size.getHeight() );
mState.setValueI( "window", "x", windowState.position.x );
mState.setValueI( "window", "y", windowState.position.y );
mState.setValueI( "window", "display_index", windowState.displayIndex );
mState.setValueB( "window", "maximized", windowState.maximized );
mState.setValueF( "window", "tab_bar_width", windowState.tabBarWidth );
return mState.writeFile();
}
void AppConfig::captureWindowState( EE::Window::Window* window ) {
if ( !window )
return;
windowState.size = Sys::getPlatformType() == Sys::PlatformType::macOS
? window->getSizeInScreenCoordinates()
: window->getLastWindowedSizeInScreenCoordinates();
windowState.position = window->getPosition();
windowState.position.x = eemax( 0, windowState.position.x );
windowState.position.y = eemax( 0, windowState.position.y );
windowState.displayIndex = window->getCurrentDisplayIndex();
windowState.maximized = window->isMaximized();
}
nlohmann::json AppConfig::takeSession() {
const std::string path = sessionPath();
std::string data;
if ( !FileSystem::fileGet( path, data ) )
return {};
FileSystem::fileRemove( path );
auto session = nlohmann::json::parse( data, nullptr, false );
return session.is_discarded() ? nlohmann::json{} : session;
}
bool AppConfig::saveSession( const nlohmann::json& session ) {
if ( !FileSystem::isDirectory( mConfigPath ) && !FileSystem::makeDir( mConfigPath, true ) )
return false;
// Titles can come from programs, so an invalid sequence must not fail the whole session.
return FileSystem::fileWrite(
sessionPath(), session.dump( -1, ' ', false, nlohmann::json::error_handler_t::replace ) );
}
} // namespace eterm