-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.cpp
More file actions
328 lines (296 loc) · 10.5 KB
/
Copy pathSettings.cpp
File metadata and controls
328 lines (296 loc) · 10.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
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
/* Settings.cpp – Coffee Toolkit */
#include "Settings.h"
#include "Constants.h"
#include <Application.h>
#include <File.h>
#include <InterfaceDefs.h>
#include <FindDirectory.h>
#include <Menu.h>
#include <MenuBar.h>
#include <MenuItem.h>
#include <Message.h>
#include <Path.h>
#include <Window.h>
#include <LocaleRoster.h>
#include <string.h>
static const char* kSettingsFile = "CoffeeToolkit_settings";
CoffeeSettings* CoffeeSettings::sInstance = nullptr;
CoffeeSettings::CoffeeSettings()
: fCelsius(true),
fMetric(true),
fRatio(15),
fTheme(0)
{
// Default language: detect system locale, fall back to "en"
strlcpy(fLanguage, "en", sizeof(fLanguage));
{
BLocaleRoster* roster = BLocaleRoster::Default();
BMessage preferredLangs;
if (roster != nullptr
&& roster->GetPreferredLanguages(&preferredLangs) == B_OK) {
const char* lang = nullptr;
if (preferredLangs.FindString("language", &lang) == B_OK
&& lang != nullptr && lang[0] != '\0' && lang[1] != '\0') {
char code[3] = { lang[0], lang[1], '\0' };
const char* supported[] = { "en", "es", "fr", "de", "ja" };
for (const char* s : supported) {
if (strcmp(code, s) == 0) {
strlcpy(fLanguage, code, sizeof(fLanguage));
break;
}
}
}
}
}
Load();
}
CoffeeSettings*
CoffeeSettings::Get()
{
if (!sInstance)
sInstance = new CoffeeSettings();
return sInstance;
}
void
CoffeeSettings::Load()
{
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return;
path.Append(kSettingsFile);
BFile file(path.Path(), B_READ_ONLY);
if (file.InitCheck() != B_OK)
return;
BMessage archive;
if (archive.Unflatten(&file) != B_OK)
return;
bool celsius;
if (archive.FindBool("celsius", &celsius) == B_OK)
fCelsius = celsius;
bool metric;
if (archive.FindBool("metric", &metric) == B_OK)
fMetric = metric;
int32 ratio;
if (archive.FindInt32("ratio", &ratio) == B_OK
&& ratio >= 15 && ratio <= 18)
fRatio = ratio;
int32 theme;
if (archive.FindInt32("theme", &theme) == B_OK
&& theme >= 0 && theme <= 2)
fTheme = theme;
const char* lang;
if (archive.FindString("language", &lang) == B_OK)
strlcpy(fLanguage, lang, sizeof(fLanguage));
}
void
CoffeeSettings::Save()
{
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return;
path.Append(kSettingsFile);
BFile file(path.Path(), B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
if (file.InitCheck() != B_OK)
return;
BMessage archive;
archive.AddBool("celsius", fCelsius);
archive.AddBool("metric", fMetric);
archive.AddInt32("ratio", fRatio);
archive.AddInt32("theme", fTheme);
archive.AddString("language", fLanguage);
archive.Flatten(&file);
}
rgb_color
CoffeeSettings::ThemePanelBg() const
{
switch (fTheme) {
case 1: return { 245, 245, 242, 255 };
case 2: return { 38, 38, 42, 255 };
default: return ui_color(B_PANEL_BACKGROUND_COLOR);
}
}
rgb_color
CoffeeSettings::ThemeTextColor() const
{
switch (fTheme) {
case 1: return { 20, 20, 20, 255 };
case 2: return { 210, 210, 210, 255 };
default: return ui_color(B_PANEL_TEXT_COLOR);
}
}
rgb_color
CoffeeSettings::ThemeDimTextColor() const
{
switch (fTheme) {
case 1: return { 90, 90, 90, 255 };
case 2: return { 150, 150, 150, 255 };
default: return { 120, 120, 120, 255 };
}
}
rgb_color
CoffeeSettings::ThemeOutlineColor() const
{
switch (fTheme) {
case 1: return { 100, 100, 100, 255 };
case 2: return { 80, 80, 80, 255 };
default: return { 70, 70, 70, 255 };
}
}
/* static */ void
CoffeeSettings::BuildSettingsMenu(BMenuBar* menuBar)
{
CoffeeSettings* s = Get();
BMenu* settingsMenu = new BMenu("Settings");
// Temperature submenu
BMenu* tempMenu = new BMenu("Temperature");
tempMenu->SetRadioMode(true);
BMenuItem* celsius = new BMenuItem("Celsius",
new BMessage(MSG_SET_TEMP_CELSIUS));
BMenuItem* fahrenheit = new BMenuItem("Fahrenheit",
new BMessage(MSG_SET_TEMP_FAHRENHEIT));
celsius->SetMarked(s->fCelsius);
fahrenheit->SetMarked(!s->fCelsius);
tempMenu->AddItem(celsius);
tempMenu->AddItem(fahrenheit);
settingsMenu->AddItem(tempMenu);
// Default Brew Ratio submenu
BMenu* ratioMenu = new BMenu("Default Brew Ratio");
ratioMenu->SetRadioMode(true);
struct { const char* label; uint32 msg; int32 val; } ratios[] = {
{ "1:15", MSG_SET_RATIO_15, 15 },
{ "1:16", MSG_SET_RATIO_16, 16 },
{ "1:17", MSG_SET_RATIO_17, 17 },
{ "1:18", MSG_SET_RATIO_18, 18 },
};
for (auto& r : ratios) {
BMenuItem* item = new BMenuItem(r.label, new BMessage(r.msg));
item->SetMarked(s->fRatio == r.val);
ratioMenu->AddItem(item);
}
settingsMenu->AddItem(ratioMenu);
// Theme submenu
BMenu* themeMenu = new BMenu("Theme");
themeMenu->SetRadioMode(true);
struct { const char* label; uint32 msg; int32 val; } themes[] = {
{ "System default", MSG_SET_THEME_SYSTEM, 0 },
{ "Light", MSG_SET_THEME_LIGHT, 1 },
{ "Dark", MSG_SET_THEME_DARK, 2 },
};
for (auto& t : themes) {
BMenuItem* item = new BMenuItem(t.label, new BMessage(t.msg));
item->SetMarked(s->fTheme == t.val);
themeMenu->AddItem(item);
}
settingsMenu->AddItem(themeMenu);
// Language submenu
BMenu* langMenu = new BMenu("Language");
langMenu->SetRadioMode(true);
struct { const char* label; uint32 msg; const char* code; } langs[] = {
{ "English", MSG_SET_LANG_EN, "en" },
{ "Espa\xc3\xb1ol", MSG_SET_LANG_ES, "es" },
{ "Fran\xc3\xa7" "ais", MSG_SET_LANG_FR, "fr" },
{ "Deutsch", MSG_SET_LANG_DE, "de" },
{ "\xe6\x97\xa5\xe6\x9c\xac\xe8\xaa\x9e", MSG_SET_LANG_JA, "ja" },
};
for (auto& l : langs) {
BMenuItem* item = new BMenuItem(l.label, new BMessage(l.msg));
item->SetMarked(strcmp(s->fLanguage, l.code) == 0);
langMenu->AddItem(item);
}
settingsMenu->AddItem(langMenu);
// Measurement Units submenu
BMenu* unitsMenu = new BMenu("Measurement Units");
unitsMenu->SetRadioMode(true);
BMenuItem* mm = new BMenuItem("Millimetres (mm)",
new BMessage(MSG_SET_UNIT_MM));
BMenuItem* inches = new BMenuItem("Inches (in)",
new BMessage(MSG_SET_UNIT_INCHES));
mm->SetMarked(s->fMetric);
inches->SetMarked(!s->fMetric);
unitsMenu->AddItem(mm);
unitsMenu->AddItem(inches);
settingsMenu->AddItem(unitsMenu);
menuBar->AddItem(settingsMenu, 0);
}
bool
CoffeeSettings::HandleSettingsMessage(BMessage* msg)
{
switch (msg->what) {
case B_ABOUT_REQUESTED:
be_app->PostMessage(B_ABOUT_REQUESTED);
return true;
case MSG_SET_UNIT_MM: fMetric = true; break;
case MSG_SET_UNIT_INCHES: fMetric = false; break;
case MSG_SET_TEMP_CELSIUS: fCelsius = true; break;
case MSG_SET_TEMP_FAHRENHEIT: fCelsius = false; break;
case MSG_SET_RATIO_15: fRatio = 15; break;
case MSG_SET_RATIO_16: fRatio = 16; break;
case MSG_SET_RATIO_17: fRatio = 17; break;
case MSG_SET_RATIO_18: fRatio = 18; break;
case MSG_SET_THEME_SYSTEM: fTheme = 0; break;
case MSG_SET_THEME_LIGHT: fTheme = 1; break;
case MSG_SET_THEME_DARK: fTheme = 2; break;
case MSG_SET_LANG_EN: strlcpy(fLanguage, "en", sizeof(fLanguage)); break;
case MSG_SET_LANG_ES: strlcpy(fLanguage, "es", sizeof(fLanguage)); break;
case MSG_SET_LANG_FR: strlcpy(fLanguage, "fr", sizeof(fLanguage)); break;
case MSG_SET_LANG_DE: strlcpy(fLanguage, "de", sizeof(fLanguage)); break;
case MSG_SET_LANG_JA: strlcpy(fLanguage, "ja", sizeof(fLanguage)); break;
default:
return false;
}
Save();
SyncAllWindows();
return true;
}
void
CoffeeSettings::SyncAllWindows()
{
for (int32 i = 0; i < be_app->CountWindows(); i++) {
BWindow* win = be_app->WindowAt(i);
if (!win->Lock())
continue;
BMenuBar* bar = dynamic_cast<BMenuBar*>(win->FindView("menubar"));
if (bar) {
BMenu* settingsMenu = bar->SubmenuAt(0);
if (settingsMenu
&& strcmp(settingsMenu->Name(), "Settings") == 0) {
// Temperature (submenu index 0)
BMenu* tempMenu = settingsMenu->SubmenuAt(0);
if (tempMenu) {
tempMenu->ItemAt(0)->SetMarked(fCelsius);
tempMenu->ItemAt(1)->SetMarked(!fCelsius);
}
// Ratio (submenu index 1)
BMenu* ratioMenu = settingsMenu->SubmenuAt(1);
if (ratioMenu) {
for (int32 r = 0; r < ratioMenu->CountItems(); r++)
ratioMenu->ItemAt(r)->SetMarked(fRatio == 15 + r);
}
// Theme (submenu index 2)
BMenu* themeMenu = settingsMenu->SubmenuAt(2);
if (themeMenu) {
for (int32 t = 0; t < themeMenu->CountItems(); t++)
themeMenu->ItemAt(t)->SetMarked(fTheme == t);
}
// Language (submenu index 3)
BMenu* langMenu = settingsMenu->SubmenuAt(3);
if (langMenu) {
const char* codes[] = { "en", "es", "fr", "de", "ja" };
for (int32 l = 0; l < langMenu->CountItems(); l++)
langMenu->ItemAt(l)->SetMarked(
strcmp(fLanguage, codes[l]) == 0);
}
// Units (submenu index 4)
BMenu* unitsMenu = settingsMenu->SubmenuAt(4);
if (unitsMenu) {
unitsMenu->ItemAt(0)->SetMarked(fMetric);
unitsMenu->ItemAt(1)->SetMarked(!fMetric);
}
}
}
win->Unlock();
}
// Notify all windows that the theme may have changed
for (int32 i = 0; i < be_app->CountWindows(); i++)
be_app->WindowAt(i)->PostMessage(MSG_THEME_CHANGED);
}