-
-
Notifications
You must be signed in to change notification settings - Fork 268
Expand file tree
/
Copy pathconfig.hpp
More file actions
485 lines (389 loc) · 13.6 KB
/
Copy pathconfig.hpp
File metadata and controls
485 lines (389 loc) · 13.6 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#pragma once
#include <expected>
#include <filesystem>
#include "common/entrypoint.hpp"
#include "theme/theme-variant.hpp"
#include "vicinae.hpp"
#include <glaze/core/common.hpp>
#include <glaze/core/reflect.hpp>
#include <glaze/json.hpp>
#include <glaze/util/key_transformers.hpp>
#include <iostream>
#include <qdatetime.h>
#include <qfilesystemwatcher.h>
#include <qmargins.h>
#include <QTimer>
#include <string>
#include <string_view>
namespace config {
struct ProviderItemData {
std::optional<std::string> alias;
std::optional<bool> enabled;
std::optional<std::string> shortcut;
std::optional<glz::generic::object_t> preferences;
};
struct ProviderData {
std::optional<bool> enabled;
std::optional<glz::generic::object_t> preferences;
std::map<std::string, ProviderItemData> entrypoints;
};
template <typename T> struct Partial;
struct SystemThemeConfig {
std::string name;
std::string iconTheme;
};
struct ThemeConfig {
std::string appearance = "system";
SystemThemeConfig light;
SystemThemeConfig dark;
};
template <> struct Partial<SystemThemeConfig> {
std::optional<std::string> name;
std::optional<std::string> iconTheme;
};
template <> struct Partial<ThemeConfig> {
std::optional<std::string> appearance;
std::optional<Partial<SystemThemeConfig>> light;
std::optional<Partial<SystemThemeConfig>> dark;
};
template <> struct Partial<ProviderData> {
std::optional<bool> enabled;
std::optional<glz::generic::object_t> preferences;
std::optional<std::map<std::string, ProviderItemData>> entrypoints;
};
struct LayerShellConfig {
std::string keyboardInteractivity = "exclusive";
std::string layer = "top";
bool enabled = true;
};
template <> struct Partial<LayerShellConfig> {
std::optional<std::string> layer;
std::optional<std::string> keyboardInteractivity;
std::optional<bool> enabled;
};
struct BlurConfig {
bool enabled = true;
};
template <> struct Partial<BlurConfig> {
std::optional<bool> enabled;
};
struct Size {
int width = 770;
int height = 480;
};
template <> struct Partial<Size> {
std::optional<int> width;
std::optional<int> height;
};
struct WindowCSD {
bool enabled = true;
#ifdef Q_OS_MACOS
int rounding = 20;
#else
int rounding = 10;
#endif
int borderWidth = 3;
int shadowSize = 12;
};
template <> struct Partial<WindowCSD> {
std::optional<bool> enabled;
std::optional<int> rounding;
std::optional<int> borderWidth;
std::optional<int> shadowSize;
};
struct WindowCompactMode {
bool enabled;
};
template <> struct Partial<WindowCompactMode> {
std::optional<bool> enabled;
};
struct ClockConfig {
bool enabled = true;
unsigned interval = 60;
std::optional<std::string> format;
};
template <> struct Partial<ClockConfig> {
std::optional<std::string> format;
std::optional<unsigned> interval;
};
struct WindowConfig {
static constexpr float OPAQUE_OPACITY = 1.0F;
static constexpr float TRANSLUCENT_OPACITY = 0.6F;
static constexpr float ACRYLIC_OPACITY = 0.9F;
static constexpr float GLASS_POPUP_OPACITY = 0.2F;
static constexpr float SURFACE_OPACITY_LIFT = 0.65F;
std::optional<float> opacity;
std::optional<int> rounding;
WindowCSD clientSideDecorations;
Size size;
std::string screen;
BlurConfig blur;
WindowCompactMode compactMode;
LayerShellConfig layerShell;
ClockConfig clock;
std::string material = "auto";
// Corner radius is a window-level property, but historically lived under client_side_decorations.
// Fall back to that value when the flat key is unset to keep older configs working.
int effectiveRounding() const { return rounding.value_or(clientSideDecorations.rounding); }
std::string resolvedMaterial(bool liquidGlassAvailable, bool windowMaterialAvailable) const {
if (material != "auto") return material;
if (!blur.enabled) return "none";
if (liquidGlassAvailable) return "liquid_glass";
return windowMaterialAvailable ? "blur" : "none";
}
float resolvedOpacity(bool liquidGlassAvailable, bool windowMaterialAvailable) const {
if (opacity) return *opacity;
const std::string material = resolvedMaterial(liquidGlassAvailable, windowMaterialAvailable);
if (material == "liquid_glass") return TRANSLUCENT_OPACITY;
#ifdef Q_OS_WIN
if (material == "blur") return ACRYLIC_OPACITY;
#endif
return OPAQUE_OPACITY;
}
// Popups draw their own material layer; on liquid glass a fixed low tint keeps the
// glass legible regardless of the configured window opacity.
float resolvedPopupOpacity(bool liquidGlassAvailable, bool windowMaterialAvailable) const {
if (resolvedMaterial(liquidGlassAvailable, windowMaterialAvailable) == "liquid_glass") {
return GLASS_POPUP_OPACITY;
}
return resolvedOpacity(liquidGlassAvailable, windowMaterialAvailable);
}
// Fills that carry meaning (selection, hover, grid tiles) fade toward invisibility if they
// share the background's alpha, so they get a floor above the window opacity. The quadratic
// falloff concentrates the lift on very transparent surfaces and vanishes near opaque.
static constexpr float liftedOpacity(float base) {
return base + (1.0F - base) * (1.0F - base) * SURFACE_OPACITY_LIFT;
}
float resolvedSurfaceOpacity(bool liquidGlassAvailable, bool windowMaterialAvailable) const {
return liftedOpacity(resolvedOpacity(liquidGlassAvailable, windowMaterialAvailable));
}
float resolvedPopupSurfaceOpacity(bool liquidGlassAvailable, bool windowMaterialAvailable) const {
return liftedOpacity(resolvedPopupOpacity(liquidGlassAvailable, windowMaterialAvailable));
}
};
template <> struct Partial<WindowConfig> {
std::optional<int> rounding;
std::optional<float> opacity;
std::optional<Partial<WindowCSD>> clientSideDecorations;
std::optional<Partial<Size>> size;
std::optional<Partial<BlurConfig>> blur;
std::optional<Partial<WindowCompactMode>> compactMode;
std::optional<Partial<LayerShellConfig>> layerShell;
std::optional<std::string> material;
std::optional<ClockConfig> clock;
};
struct FontConfig {
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
std::string rendering = "native";
#else
std::string rendering = "qt";
#endif
struct FontSpec {
std::string family = "auto";
#ifdef Q_OS_MACOS
float size = 13;
#else
float size = 10.5;
#endif
} normal;
};
template <> struct Partial<FontConfig> {
std::optional<std::string> rendering;
struct FontSpec {
std::optional<std::string> family;
std::optional<float> size;
} normal;
};
struct Margin {
int left;
int top;
int right;
int bottom;
operator QMargins() const { return QMargins{left, top, right, bottom}; }
};
struct Header {
int height = 60;
Margin margins = {15, 5, 15, 5};
};
template <> struct Partial<Header> {
std::optional<Margin> margins;
std::optional<int> height;
};
struct Footer {
int height = 40;
};
template <> struct Partial<Footer> {
std::optional<int> height;
};
struct TelemetryConfig {
bool systemInfo = true;
};
template <> struct Partial<TelemetryConfig> {
std::optional<bool> systemInfo;
};
using KeybindMap = std::map<std::string, std::string>;
using ProviderMap = std::map<std::string, ProviderData>;
static constexpr const char *SCHEMA = "https://vicinae.com/schemas/config.json";
struct InputServer {
bool enabled = true;
};
template <> struct Partial<InputServer> {
std::optional<bool> enabled;
};
struct GlobalShortcuts {
#if defined(Q_OS_MACOS) || defined(Q_OS_WIN)
std::optional<std::string> toggle = "alt+space";
#else
std::optional<std::string> toggle = "super+control+space";
#endif
std::vector<std::string> inhibitApps;
};
template <> struct Partial<GlobalShortcuts> {
std::optional<std::string> toggle;
std::optional<std::vector<std::string>> inhibitApps;
};
struct ConfigValue {
std::string schema = SCHEMA;
std::vector<std::string> imports;
bool searchFilesInRoot = false;
bool closeOnFocusLoss = false;
bool considerPreedit = false;
bool popToRootOnClose = false;
bool popOnBackspace = true;
bool activateOnSingleClick = false;
bool wrapNavigation = false;
#if defined(Q_OS_LINUX) || defined(Q_OS_WIN)
bool encryptSensitiveData = false;
#else
bool encryptSensitiveData = true;
#endif
std::string escapeKeyBehavior;
std::string faviconService = "twenty";
std::string keybinding = "default";
std::optional<std::string> language;
int pixmapCacheMb = 50;
InputServer inputServer;
GlobalShortcuts globalShortcuts;
FontConfig font;
ThemeConfig theme;
TelemetryConfig telemetry;
WindowConfig launcherWindow;
Header header;
Footer footer;
// we use maps to keep serialized output predictable
KeybindMap keybinds;
std::vector<std::string> favorites;
std::vector<std::string> fallbacks;
ProviderMap providers;
std::optional<glz::generic::object_t> providerPreferences(std::string_view id) const {
if (auto it = providers.find(std::string{id}); it != providers.end()) { return it->second.preferences; }
return std::nullopt;
}
std::optional<glz::generic::object_t> preferences(const EntrypointId &id) const {
if (auto it = providers.find(id.provider); it != providers.end()) {
if (auto it2 = it->second.entrypoints.find(id.entrypoint); it2 != it->second.entrypoints.end()) {
if (auto preferences = it2->second.preferences) { return preferences; }
}
}
return std::nullopt;
}
bool followsSystemAppearance() const;
ThemeVariant activeAppearance() const;
const SystemThemeConfig &systemTheme() const;
};
using PartialValue = Partial<ConfigValue>;
template <> struct Partial<ConfigValue> {
std::string schema = SCHEMA;
std::optional<std::vector<std::string>> imports;
std::optional<bool> closeOnFocusLoss;
std::optional<bool> considerPreedit;
std::optional<bool> popToRootOnClose;
std::optional<bool> popOnBackspace;
std::optional<bool> activateOnSingleClick;
std::optional<bool> wrapNavigation;
std::optional<bool> encryptSensitiveData;
std::optional<std::string> escapeKeyBehavior;
std::optional<std::string> faviconService;
std::optional<std::string> keybinding;
std::optional<std::string> language;
std::optional<int> pixmapCacheMb;
std::optional<bool> searchFilesInRoot;
std::optional<Partial<InputServer>> inputServer;
std::optional<Partial<GlobalShortcuts>> globalShortcuts;
std::optional<Partial<FontConfig>> font;
std::optional<Partial<ThemeConfig>> theme;
std::optional<Partial<TelemetryConfig>> telemetry;
std::optional<Partial<WindowConfig>> launcherWindow;
std::optional<Partial<Header>> header;
std::optional<Partial<Footer>> footer;
std::optional<KeybindMap> keybinds;
std::optional<std::vector<std::string>> favorites;
std::optional<std::vector<std::string>> fallbacks;
std::optional<std::map<std::string, Partial<ProviderData>>> providers;
};
class Manager : public QObject {
Q_OBJECT
signals:
void configChanged(const ConfigValue &next, const ConfigValue &prev) const;
void configLoadingError(std::string_view message) const;
private:
struct LoadingOptions {
bool resolveImports;
std::unordered_set<std::filesystem::path> &visited;
};
using ConfigResult = std::expected<ConfigValue, std::string>;
using PartialConfigResult = std::expected<Partial<ConfigValue>, std::string>;
public:
Manager(std::filesystem::path path = Omnicast::configDir() / "settings.json");
ConfigValue defaultConfig() const;
const char *defaultConfigData() const;
bool mergeProviderWithUser(std::string_view id, const Partial<ProviderData> &data);
/**
* Update the current user configuration, instead of merging.
*/
bool updateUser(const std::function<void(Partial<ConfigValue> &value)> &updater);
bool mergeEntrypointWithUser(const EntrypointId &id, const ProviderItemData &data);
bool mergeWithUser(const Partial<ConfigValue> &patch);
bool mergeThemeConfig(const config::Partial<config::SystemThemeConfig> &cfg);
const SystemThemeConfig &theme() const;
std::filesystem::path path() const { return m_userPath; }
static void print(const ConfigValue &value);
const ConfigValue &value() const { return m_user; }
private:
static void prunePartial(Partial<ConfigValue> &user);
PartialConfigResult load(const std::filesystem::path &path, const LoadingOptions &opts);
static std::filesystem::path resolvePath(const std::filesystem::path &path,
const std::filesystem::path &cwd);
void reloadConfig();
ConfigResult loadUser(const LoadingOptions &opts);
bool writeUser(const Partial<ConfigValue> &cfg);
void initConfig();
QFileSystemWatcher m_watcher;
std::filesystem::path m_userPath;
ConfigValue m_user;
std::string m_defaultData;
ConfigValue m_defaultConfig;
QTimer m_fsDebounce;
std::vector<std::string> m_envOverrides;
};
}; // namespace config
#define SNAKE_CASIFY(T) \
template <> struct glz::meta<T> : glz::snake_case {}; \
template <> struct glz::meta<config::Partial<T>> : glz::snake_case {};
SNAKE_CASIFY(config::LayerShellConfig);
SNAKE_CASIFY(config::WindowConfig);
SNAKE_CASIFY(config::SystemThemeConfig);
SNAKE_CASIFY(config::ThemeConfig);
SNAKE_CASIFY(config::TelemetryConfig);
SNAKE_CASIFY(config::WindowCSD);
SNAKE_CASIFY(config::ClockConfig);
SNAKE_CASIFY(config::GlobalShortcuts);
#undef SNAKE_CASIFY
struct ConfigTransformer : glz::snake_case {
static constexpr std::string rename_key(const std::string_view key) {
if (key == "schema") return "$schema";
return glz::to_snake_case(key);
}
};
template <> struct glz::meta<config::ConfigValue> : ConfigTransformer {};
template <> struct glz::meta<config::Partial<config::ConfigValue>> : ConfigTransformer {};