-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings.cpp
More file actions
720 lines (638 loc) · 17.2 KB
/
Copy pathsettings.cpp
File metadata and controls
720 lines (638 loc) · 17.2 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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#include "settings.h"
#include "text_renderer.h"
#include "application.h"
#include "Verify.hpp"
SettingsFloat* Settings::makeFloat(std::string name, std::string key, float default_value) {
auto f = new SettingsFloat();
f->name = name;
f->key_name = key;
f->default_value = default_value;
f->value = App::settings->getValue(key, default_value);
f->set = (f->value != f->default_value);
return f;
}
SettingsInt* Settings::makeInt(std::string name, std::string key, int default_value) {
auto f = new SettingsInt();
f->name = name;
f->key_name = key;
f->default_value = default_value;
f->value = App::settings->getValue(key, default_value);
f->set = (f->value != f->default_value);
return f;
}
SettingsBool* Settings::makeBool(std::string name, std::string key, bool default_value) {
auto f = new SettingsBool();
f->name = name;
f->key_name = key;
f->default_value = default_value;
f->value = App::settings->getValue(key, default_value);
f->set = (f->value != f->default_value);
return f;
}
SettingsString* Settings::makeString(std::string name, std::string key, std::string default_value) {
auto f = new SettingsString();
f->name = name;
f->key_name = key;
f->default_value = default_value;
f->value = App::settings->getValue(key, default_value);
f->set = (f->value != f->default_value);
return f;
}
SettingsLabel* Settings::makeLabel(std::string name) {
auto f = new SettingsLabel();
f->name = name;
return f;
}
Settings::Settings(Widget* parent) : Widget(parent) {
id = icu::UnicodeString::fromUTF8("Settings");
tab_bar = new Tabs(this);
tab_bar->has_close_button = false;
tab_bar->can_add_new = false;
tab_bar->tab_clicked_callback = [&](TabInfo info){
if (info.id == 2) {
// project specifics
bool worked = App::settings->makeProjectSettings();
if (auto el = dynamic_cast<SettingsLabel*>(settings_menus[2][0])) {
if (worked) {
el->default_value = App::settings->getProjectSettingsPath();
}else{
el->default_value = "Failed to initialize project settings.";
}
}
}
handleChildren();
};
settings_menus[0] = {
makeBool(
"Dark Mode",
"dark_mode",
true
),
makeFloat(
"Font Size",
"font_size",
24.0f
),
makeBool(
"Transparency Effects",
"use_transparency",
false
),
makeFloat(
"Transparency Mode Opacity",
"opacity",
0.65f
),
makeString(
"Font Path",
"font_path",
getExecutableDir()+"/cascadia/CascadiaCode-Regular.ttf"
),
makeString(
"Tint Color",
"c_tint_color",
colorToString(App::theme.tint_color)
),
makeString(
"Strings Colors",
"c_strings_color",
colorToString(App::theme.syntax_colors[1])
),
makeString(
"Comments Color",
"c_comments_color",
colorToString(App::theme.syntax_colors[2])
),
makeString(
"Variables Color",
"c_vars_color",
colorToString(App::theme.syntax_colors[3])
),
makeString(
"Types Color",
"c_types_color",
colorToString(App::theme.syntax_colors[4])
),
makeString(
"Functions Color",
"c_functs_color",
colorToString(App::theme.syntax_colors[5])
),
makeString(
"Keywords Color",
"c_keywords_color",
colorToString(App::theme.syntax_colors[6])
),
makeString(
"Punctuation Color",
"c_punctuation_color",
colorToString(App::theme.syntax_colors[7])
),
makeString(
"Literals Color",
"c_literals_color",
colorToString(App::theme.syntax_colors[8])
),
};
settings_menus[1] = {
makeBool(
"Use Modal Editor",
"use_vim",
false
),
makeBool(
"Use Relative Line Numbers",
"use_rel_line_num",
false
),
makeBool(
"Use File Tabs",
"use_tabs",
true
),
makeBool(
"Invert Scroll Vertical",
"invert_scroll_v",
false
),
makeBool(
"Invert Scroll Horizontal",
"invert_scroll_h",
false
),
makeInt(
"Tab Width",
"tab_width",
4
),
makeString(
"Default Terminal",
"terminal_cmd",
#ifdef _WIN32
"cmd.exe"
#else
"/bin/bash"
#endif
),
makeBool(
"Show FPS",
"show_fps",
false
),
makeFloat(
"Animation Speed",
"anim_speed",
1.0f
),
makeFloat(
"Smooth Scroll Speed (1 is disabled, default 0.2)",
"smooth_scroll",
0.1f
),
makeInt(
"Max Files To Index",
"max_index_files",
2000
),
makeString(
"AI Model ID",
"lm_studio_model_id",
"qwen2.5-coder-1.5b-instruct@q4_k_m"
),
makeString(
"AI Model Provider",
"ai_model_url",
"http://localhost:1234/api/v0"
),
makeString(
"AI Model API Key",
"ai_model_api_key",
""
),
makeInt(
"Context Lines",
"lm_studio_context_lines",
30
),
makeInt(
"Max New Tokens (Only Non-Chat Completion)",
"lm_studio_max_tokens",
30
),
makeBool(
"Load AI Model On Start",
"lm_load_model_on_start",
false
),
makeBool(
"AI Provider Supports Non-Chat Completions",
"use_non_chat_completions",
true
),
makeBool(
"Auto Clean Up .TMP Files",
"use_auto_tmp_clean",
true
)
// makeBool(
// "Use Chauffeur",
// "use_chauffeur",
// false
// ),
// makeString(
// "Chauffeur Model Path (Restart CW)",
// "chauffeur_model_path",
// ""
// ),
// makeString(
// "Chauffeur Tokenizer Path (Restart CW)",
// "chauffeur_tokenizer_path",
// ""
// ),
// makeInt(
// "Chauffeur Prefix Lines",
// "chauffeur_prefix_lines",
// 10
// ),
// makeInt(
// "Chauffeur Suffix Lines",
// "chauffeur_suffix_lines",
// 7
// ),
// makeInt(
// "Chauffeur Max Continue",
// "chauffeur_max_continue",
// 6
// ),
// makeInt(
// "Chauffeur Wait Time (ms)",
// "chauffeur_time",
// 1000
// ),
// makeFloat(
// "Chauffeur Temperature (0-1)",
// "chauffeur_temp",
// 0.6f
// ),
// makeBool(
// "Chauffeur Use FIM (recommended)",
// "use_fim",
// true
// )
};
settings_menus[2] = {
makeLabel("Project Settings Located At (not editable):")
};
settings_menus[3] = {
makeString(
"AssistantV3 Term Mirror URL",
"ajm_asv3_tm_url",
""
),
makeBool(
"AssistantV3 Use HTTPS",
"ajm_asv3_tm_use_https",
false
),
makeString(
"AssistantV3 Password",
"ajm_asv3_password",
"devpassword"
)
};
std::vector<std::string> tabs = {
"Appearance",
"Editor",
"Project Specific",
"AJM"
};
int tabid = 0;
for (auto str : tabs) {
auto ti = TabInfo();
ti.title = icu::UnicodeString::fromUTF8(str);
ti.id = tabid;
tab_bar->addTab(ti);
tabid ++;
}
handleChildren();
}
void Settings::handleChildren() {
edits_to_element.clear();
elements_to_edit.clear();
App::activeLeafNode = nullptr;
auto copy = children;
for (auto c : copy) {
if (c && c != tab_bar) {
App::deleteWidget(c);
}
}
for (auto el : settings_menus[tab_bar->selected_id]) {
TextEdit* e = new TextEdit(this, [&](Widget* t){
auto el = edits_to_element[dynamic_cast<TextEdit*>(t)];
SettingsElement* before = nullptr;
for (auto te : settings_menus[tab_bar->selected_id]) {
if (te == el) {
break; // this loop finds the element directly prior to this one
}
before = te;
}
int y = 0; // we apply the scrolled to on the first element so that it will propegate down to the rest
if (before != nullptr) {
TextEdit* prior = elements_to_edit[before];
y = prior->t_y+prior->t_h; // all that just to get the y location... maybe there's a problem with my system. Oh well, it's too late now y'all
}else{
y = t_y+TextRenderer::get_text_height()+App::text_padding*4-scrolled_to;
}
t->t_y = y+TextRenderer::get_text_height()+App::text_padding;
t->t_h = TextRenderer::get_text_height()+App::text_padding*2;
t->t_x = t_x+App::text_padding*2;
t->t_w = TextRenderer::get_text_width(40);
max_scroll = fmax(0, (t->t_y+t->t_h+scrolled_to)-(t_h-tab_bar->t_h)-(t->t_h)*2);
});
e->rounded = true;
e->border = true;
setWithValue(e, el);
edits_to_element[e] = el;
elements_to_edit[el] = e;
}
}
void Settings::setWithValue(TextEdit* e, SettingsElement* el) {
if (auto i = dynamic_cast<SettingsInt*>(el)) {
e->setFullText(icu::UnicodeString::fromUTF8(std::to_string(i->value)));
}else if (auto i = dynamic_cast<SettingsFloat*>(el)) {
e->setFullText(icu::UnicodeString::fromUTF8(std::to_string(i->value)));
}else if (auto i = dynamic_cast<SettingsString*>(el)) {
e->setFullText(icu::UnicodeString::fromUTF8(i->value));
}else if (auto i = dynamic_cast<SettingsBool*>(el)) {
icu::UnicodeString vl = icu::UnicodeString::fromUTF8("false");
if (i->value) {
vl = icu::UnicodeString::fromUTF8("true");
}
e->setFullText(vl);
}else if (auto i = dynamic_cast<SettingsLabel*>(el)) {
icu::UnicodeString vl = icu::UnicodeString::fromUTF8(i->default_value);
e->setFullText(vl);
}
}
void Settings::render() {
App::DrawRect(t_x, t_y, t_w, t_h, App::theme.extras_background_color);
App::runWithSKIZ(tab_bar->t_x, tab_bar->t_y, tab_bar->t_w, tab_bar->t_h, [&](){
tab_bar->render();
});
App::runWithSKIZ(t_x, tab_bar->t_y+tab_bar->t_h, t_w, t_h-tab_bar->t_h, [&](){
for (auto it : elements_to_edit) {
int y = it.second->t_y-TextRenderer::get_text_height()-App::text_padding;
int x = t_x+App::text_padding*2;
icu::UnicodeString text = icu::UnicodeString::fromUTF8(it.first->name);
TextRenderer::draw_text(x, y, text, App::theme.main_text_color);
App::runWithSKIZ(it.second->t_x, it.second->t_y, it.second->t_w, it.second->t_h, [&](){
it.second->render();
});
}
});
if (rounded) {
App::DrawInverseRoundedRect(t_x, t_y+tab_bar->t_h, t_w, t_h-tab_bar->t_h, App::text_padding, App::theme.main_background_color);
App::DrawRoundBorder(t_x, t_y+tab_bar->t_h, t_w, t_h-tab_bar->t_h, App::theme.border, 5, App::text_padding);
}else{
App::DrawBorder(t_x, t_y+tab_bar->t_h, t_w, t_h-tab_bar->t_h, App::theme.border);
}
}
void Settings::handleChange(TextEdit* te, SettingsElement* se) {
icu::UnicodeString text = te->getFullText();
std::string str;
text.toUTF8String(str);
if (auto i = dynamic_cast<SettingsString*>(se)) {
if (str != "") {
std::string oldvalue = i->value;
i->value = str;
if (validate_input(i, oldvalue)) {
App::settings->setValue(se->key_name, str);
after_change(i);
}
}
}else if (auto i = dynamic_cast<SettingsBool*>(se)) {
if (str == "true") {
i->value = true;
}else if (str == "false") {
i->value = false;
}
if (validate_input(i)) {
App::settings->setValue(se->key_name, i->value);
after_change(i);
}
}else if (auto i = dynamic_cast<SettingsInt*>(se)) {
int result = -1;
try {
size_t pos;
result = std::stoi(str, &pos);
if (pos != str.length() || str.length() == 0) {
result = -1;
}
} catch (...) {
}
if (result >= 0 && i->value != result) {
i->value = result;
if (validate_input(i)) {
App::settings->setValue(se->key_name, result);
after_change(i);
}
}
}else if (auto i = dynamic_cast<SettingsFloat*>(se)) {
float result = -1;
try {
size_t pos;
result = std::stof(str, &pos);
if (pos != str.length() || result > 50 || str.length() == 0) {
result = -1;
}
} catch (...) {
}
if (result >= 0 && i->value != result) {
i->value = result;
if (validate_input(i)) {
App::settings->setValue(se->key_name, result);
after_change(i);
}
}
}
setWithValue(te, se);
App::rootelement->executeAction(WidgetActionType::SETTINGS_CHANGE);
}
bool Settings::on_key_event(int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ENTER && (action == GLFW_PRESS || action == GLFW_RELEASE)){
if (auto te = dynamic_cast<TextEdit*>(App::activeLeafNode)){
auto it = edits_to_element.find(te);
if (it != edits_to_element.end()) {
handleChange(it->first, it->second);
App::setActiveLeafNode(nullptr);
return true;
}
}
}
return Widget::on_key_event(key, scancode, action, mods);
}
bool Settings::on_scroll_event(double xchange, double ychange) {
if (Widget::on_scroll_event(xchange, ychange)) {
return true;
}
if (!cursor_in_this) {
return false;
}
scrolled_to += ychange*40;
if (scrolled_to < 0) {
scrolled_to = 0;
}else if (scrolled_to > max_scroll) {
scrolled_to = max_scroll;
}
return true;
}
bool Settings::on_mouse_button_event(int button, int action, int mods) {
if (!Widget::on_mouse_button_event(button, action, mods)) {
if (action == GLFW_RELEASE) {
return false;
}
if (auto te = dynamic_cast<TextEdit*>(App::activeLeafNode)){
auto it = edits_to_element.find(te);
if (it != edits_to_element.end()) {
handleChange(it->first, it->second);
App::setActiveLeafNode(nullptr);
return true;
}
}
}
return false;
}
bool Settings::validate_input(SettingsFloat* el) {
if (el->key_name == "font_size") {
if (el->value < 8 || el->value > 50) {
el->value = el->default_value;
return false;
}
TextRenderer::set_font_size(el->value);
std::string default_font_path = getExecutableDir()+"/cascadia/CascadiaCode-Regular.ttf";
std::string font_path = App::settings->getValue("font_path", default_font_path);
bool success = TextRenderer::init_font(font_path.c_str());
if (!success) {
TextRenderer::init_font(default_font_path.c_str());
}
}else if (el->key_name == "opacity") {
if (el->value < 0 || el->value > 1) {
el->value = el->default_value;
return false;
}
App::reclear = 2;
}
return true;
}
bool Settings::validate_input(SettingsInt* el) {
if (el->key_name == "max_index_files" || el->key_name == "chauffeur_prefix_lines" || el->key_name == "chauffeur_suffix_lines" || el->key_name == "lm_studio_max_tokens" || el->key_name == "lm_studio_context_lines") {
if (el->value < 1) {
el->value = el->default_value;
return false;
}
}else if (el->key_name == "tab_width") {
if (el->value < 1 || el->value > 25) {
el->value = el->default_value;
return false;
}
}
return true;
}
bool Settings::validate_input(SettingsBool* el) {
if (el->key_name == "dark_mode") {
App::darkmode = el->value;
App::updateFromTintColor(&App::theme);
}else if (el->key_name == "use_transparency") {
App::updateTransparency(el->value);
}
return true;
}
bool handleColor(bool syntax, SettingsString* el, int indx, std::string before) {
bool worked;
Color c = stringToColor(el->value, worked);
if (!worked) {
el->value = before; //el->default_value;
return false;
}
if (syntax) {
App::theme.syntax_colors[indx]->r = c.r;
App::theme.syntax_colors[indx]->g = c.g;
App::theme.syntax_colors[indx]->b = c.b;
}else if (indx == -1){ // handle non syntax-colors here
App::theme.tint_color->r = c.r;
App::theme.tint_color->g = c.g;
App::theme.tint_color->b = c.b;
}
return true;
}
bool Settings::validate_input(SettingsString* el, std::string before) {
if (el->key_name == "font_path") {
if (!std::filesystem::exists(el->value)) {
std::cout << "Path: " << el->value << "\ndoes not exist.\n";
el->value = el->default_value;
return false;
}
std::string default_font_path = el->default_value;
std::string font_path = el->value;
bool success = TextRenderer::init_font(font_path.c_str()); // Or whatever .ttf you have
if (!success) { // even with no sucess let's leave it be for now (we load the default font anyways so...).
TextRenderer::init_font(default_font_path.c_str());
}
return true;
}else if (el->key_name == "c_strings_color") {
return handleColor(true, el, 1, before);
}else if (el->key_name == "c_comments_color") {
return handleColor(true, el, 2, before);
}else if (el->key_name == "c_vars_color") {
return handleColor(true, el, 3, before);
}else if (el->key_name == "c_types_color") {
return handleColor(true, el, 4, before);
}else if (el->key_name == "c_functs_color") {
return handleColor(true, el, 5, before);
}else if (el->key_name == "c_keywords_color") {
return handleColor(true, el, 6, before);
}else if (el->key_name == "c_punctuation_color") {
return handleColor(true, el, 7, before);
}else if (el->key_name == "c_literals_color") {
return handleColor(true, el, 8, before);
}else if (el->key_name == "c_tint_color") {
bool worked = handleColor(false, el, -1, before);
App::updateFromTintColor(&App::theme);
return worked;
}
return true;
}
void Settings::executeAction(WidgetActionType typ) {
if (typ == WidgetActionType::SETTINGS_CHANGE) {
for (int i = 0; i < settings_menus.size(); i++) {
for (auto e : settings_menus[i]) {
if (auto se = dynamic_cast<SettingsBool*>(e)) {
se->value = App::settings->getValue(se->key_name, se->default_value);
}else if (auto se = dynamic_cast<SettingsInt*>(e)) {
se->value = App::settings->getValue(se->key_name, se->default_value);
}else if (auto se = dynamic_cast<SettingsString*>(e)) {
se->value = App::settings->getValue(se->key_name, se->default_value);
}else if (auto se = dynamic_cast<SettingsFloat*>(e)) {
se->value = App::settings->getValue(se->key_name, se->default_value);
}
}
}
tab_bar->tab_clicked_callback(tab_bar->tabs_list[tab_bar->selected_id]);
}
}
void Settings::after_change(SettingsString* el) {
if (el->key_name == "ajm_asv3_tm_url") {
App::rootelement->executeAction(AJM_SETTINGS_CHANGE);
}else if (el->key_name == "ajm_asv3_password") {
Verify::setup(el->value);
}
}
void Settings::after_change(SettingsInt* el) {
if (el->key_name == "tab_width") {
App::rootelement->executeAction(TAB_WIDTH_CHANGE);
}
}
void Settings::after_change(SettingsFloat* el) {
}
void Settings::after_change(SettingsBool* el) {
}