aboutsummaryrefslogtreecommitdiff
path: root/src/gui/GlobalSettingsPage.cpp
blob: e3c09ffd5e2f52dae0a412fbc9affda1e2899c5f (plain)
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
#include "../../include/gui/GlobalSettingsPage.hpp"

#include "../../include/Overlay.hpp"
#include "../../include/GlobalHotkeys.hpp"
#include "../../include/Theme.hpp"
#include "../../include/Process.hpp"
#include "../../include/gui/GsrPage.hpp"
#include "../../include/gui/PageStack.hpp"
#include "../../include/gui/ScrollablePage.hpp"
#include "../../include/gui/Subsection.hpp"
#include "../../include/gui/List.hpp"
#include "../../include/gui/Label.hpp"
#include "../../include/gui/RadioButton.hpp"
#include "../../include/gui/LineSeparator.hpp"
#include "../../include/gui/CustomRendererWidget.hpp"

#include <assert.h>
#include <X11/Xlib.h>
extern "C" {
#include <mgl/mgl.h>
}
#include <mglpp/window/Window.hpp>
#include <mglpp/graphics/Rectangle.hpp>
#include <mglpp/graphics/Text.hpp>

#ifndef GSR_UI_VERSION
#define GSR_UI_VERSION "Unknown"
#endif

#ifndef GSR_FLATPAK_VERSION
#define GSR_FLATPAK_VERSION "Unknown"
#endif

namespace gsr {
    static const char* gpu_vendor_to_color_name(GpuVendor vendor) {
        switch(vendor) {
            case GpuVendor::UNKNOWN: return "amd";
            case GpuVendor::AMD:     return "amd";
            case GpuVendor::INTEL:   return "intel";
            case GpuVendor::NVIDIA:  return "nvidia";
        }
        return "amd";
    }

    static const char* gpu_vendor_to_string(GpuVendor vendor) {
        switch(vendor) {
            case GpuVendor::UNKNOWN: return "Unknown";
            case GpuVendor::AMD:     return "AMD";
            case GpuVendor::INTEL:   return "Intel";
            case GpuVendor::NVIDIA:  return "NVIDIA";
        }
        return "unknown";
    }

    static uint32_t mgl_modifier_to_hotkey_modifier(mgl::Keyboard::Key modifier_key) {
        switch(modifier_key) {
            case mgl::Keyboard::LControl:  return HOTKEY_MOD_LCTRL;
            case mgl::Keyboard::LShift:    return HOTKEY_MOD_LSHIFT;
            case mgl::Keyboard::LAlt:      return HOTKEY_MOD_LALT;
            case mgl::Keyboard::LSystem:   return HOTKEY_MOD_LSUPER;
            case mgl::Keyboard::RControl:  return HOTKEY_MOD_RCTRL;
            case mgl::Keyboard::RShift:    return HOTKEY_MOD_RSHIFT;
            case mgl::Keyboard::RAlt:      return HOTKEY_MOD_RALT;
            case mgl::Keyboard::RSystem:   return HOTKEY_MOD_RSUPER;
            default:                       return 0;
        }
        return 0;
    }

    static std::vector<mgl::Keyboard::Key> hotkey_modifiers_to_mgl_keys(uint32_t modifiers) {
        std::vector<mgl::Keyboard::Key> result;
        if(modifiers & HOTKEY_MOD_LCTRL)
            result.push_back(mgl::Keyboard::LControl);
        if(modifiers & HOTKEY_MOD_LSHIFT)
            result.push_back(mgl::Keyboard::LShift);
        if(modifiers & HOTKEY_MOD_LALT)
            result.push_back(mgl::Keyboard::LAlt);
        if(modifiers & HOTKEY_MOD_LSUPER)
            result.push_back(mgl::Keyboard::LSystem);
        if(modifiers & HOTKEY_MOD_RCTRL)
            result.push_back(mgl::Keyboard::RControl);
        if(modifiers & HOTKEY_MOD_RSHIFT)
            result.push_back(mgl::Keyboard::RShift);
        if(modifiers & HOTKEY_MOD_RALT)
            result.push_back(mgl::Keyboard::RAlt);
        if(modifiers & HOTKEY_MOD_RSUPER)
            result.push_back(mgl::Keyboard::RSystem);
        return result;
    }

    static std::string config_hotkey_to_string(ConfigHotkey config_hotkey) {
        std::string result;

        const std::vector<mgl::Keyboard::Key> modifier_keys = hotkey_modifiers_to_mgl_keys(config_hotkey.modifiers);
        for(const mgl::Keyboard::Key modifier_key : modifier_keys) {
            if(!result.empty())
                result += " + ";
            result += mgl::Keyboard::key_to_string(modifier_key);
        }

        if(config_hotkey.key != 0) {
            if(!result.empty())
                result += " + ";
            result += mgl::Keyboard::key_to_string((mgl::Keyboard::Key)config_hotkey.key);
        }

        return result;
    }

    GlobalSettingsPage::GlobalSettingsPage(Overlay *overlay, const GsrInfo *gsr_info, Config &config, PageStack *page_stack) :
        StaticPage(mgl::vec2f(get_theme().window_width, get_theme().window_height).floor()),
        overlay(overlay),
        config(config),
        gsr_info(gsr_info),
        page_stack(page_stack)
    {
        auto content_page = std::make_unique<GsrPage>();
        content_page->add_button("Back", "back", get_color_theme().page_bg_color);
        content_page->on_click = [page_stack](const std::string &id) {
            if(id == "back")
                page_stack->pop();
        };
        content_page_ptr = content_page.get();
        add_widget(std::move(content_page));

        add_widgets();
        load();

        auto hotkey_overlay = std::make_unique<CustomRendererWidget>(get_size());
        hotkey_overlay->draw_handler = [this](mgl::Window &window, mgl::vec2f, mgl::vec2f) {
            Button *configure_hotkey_button = configure_hotkey_get_button_by_active_type();
            if(!configure_hotkey_button)
                return;

            mgl::Text title_text("Press a key combination to use for the hotkey \"Start/stop recording\":", get_theme().title_font);
            mgl::Text hotkey_text(configure_hotkey_button->get_text(), get_theme().top_bar_font);
            mgl::Text description_text("The hotkey has to contain one or more of these keys: Alt, Ctrl, Shift and Super. Press Esc to cancel.", get_theme().body_font);
            const float text_max_width = std::max(title_text.get_bounds().size.x, std::max(hotkey_text.get_bounds().size.x, description_text.get_bounds().size.x));

            const float padding_horizontal = int(get_theme().window_height * 0.01f);
            const float padding_vertical = int(get_theme().window_height * 0.01f);

            const mgl::vec2f bg_size = mgl::vec2f(text_max_width + padding_horizontal*2.0f, get_theme().window_height * 0.1f).floor();
            mgl::Rectangle bg_rect(mgl::vec2f(get_theme().window_width*0.5f - bg_size.x*0.5f, get_theme().window_height*0.5f - bg_size.y*0.5f).floor(), bg_size);
            bg_rect.set_color(get_color_theme().page_bg_color);
            window.draw(bg_rect);

            const mgl::vec2f tint_size = mgl::vec2f(bg_size.x, 0.004f * get_theme().window_height).floor();
            mgl::Rectangle tint_rect(bg_rect.get_position() - mgl::vec2f(0.0f, tint_size.y), tint_size);
            tint_rect.set_color(get_color_theme().tint_color);
            window.draw(tint_rect);

            title_text.set_position(mgl::vec2f(bg_rect.get_position() + mgl::vec2f(bg_rect.get_size().x*0.5f - title_text.get_bounds().size.x*0.5f, padding_vertical)).floor());
            window.draw(title_text);

            //const float description_bottom = description_text.get_position().y + description_text.get_bounds().size.y;
            //const float remaining_height = (bg_rect.get_position().y + bg_rect.get_size().y) - description_bottom;
            hotkey_text.set_position(mgl::vec2f(bg_rect.get_position() + bg_rect.get_size()*0.5f - hotkey_text.get_bounds().size*0.5f).floor());
            window.draw(hotkey_text);

            description_text.set_position(mgl::vec2f(bg_rect.get_position() + mgl::vec2f(bg_rect.get_size().x*0.5f - description_text.get_bounds().size.x*0.5f, bg_rect.get_size().y - description_text.get_bounds().size.y - padding_vertical)).floor());
            window.draw(description_text);
        };
        hotkey_overlay->set_visible(false);
        hotkey_overlay_ptr = hotkey_overlay.get();
        add_widget(std::move(hotkey_overlay));
    }

    std::unique_ptr<Subsection> GlobalSettingsPage::create_appearance_subsection(ScrollablePage *parent_page) {
        auto list = std::make_unique<List>(List::Orientation::VERTICAL);
        list->add_widget(std::make_unique<Label>(&get_theme().body_font, "Tint color", get_color_theme().text_color));
        auto tint_color_radio_button = std::make_unique<RadioButton>(&get_theme().body_font, RadioButton::Orientation::HORIZONTAL);
        tint_color_radio_button_ptr = tint_color_radio_button.get();
        tint_color_radio_button->add_item("Red", "amd");
        tint_color_radio_button->add_item("Green", "nvidia");
        tint_color_radio_button->add_item("blue", "intel");
        tint_color_radio_button->on_selection_changed = [](const std::string&, const std::string &id) {
            if(id == "amd")
                get_color_theme().tint_color = mgl::Color(221, 0, 49);
            else if(id == "nvidia")
                get_color_theme().tint_color = mgl::Color(118, 185, 0);
            else if(id == "intel")
                get_color_theme().tint_color = mgl::Color(8, 109, 183);
            return true;
        };
        list->add_widget(std::move(tint_color_radio_button));
        return std::make_unique<Subsection>("Appearance", std::move(list), mgl::vec2f(parent_page->get_inner_size().x, 0.0f));
    }

    std::unique_ptr<Subsection> GlobalSettingsPage::create_startup_subsection(ScrollablePage *parent_page) {
        auto list = std::make_unique<List>(List::Orientation::VERTICAL);
        list->add_widget(std::make_unique<Label>(&get_theme().body_font, "Start program on system startup?", get_color_theme().text_color));
        auto startup_radio_button = std::make_unique<RadioButton>(&get_theme().body_font, RadioButton::Orientation::HORIZONTAL);
        startup_radio_button_ptr = startup_radio_button.get();
        startup_radio_button->add_item("Yes", "start_on_system_startup");
        startup_radio_button->add_item("No", "dont_start_on_system_startup");
        startup_radio_button->on_selection_changed = [&](const std::string&, const std::string &id) {
            bool enable = false;
            if(id == "dont_start_on_system_startup")
                enable = false;
            else if(id == "start_on_system_startup")
                enable = true;
            else
                return false;

            const char *args[] = { "systemctl", enable ? "enable" : "disable", "--user", "gpu-screen-recorder-ui", nullptr };
            std::string stdout_str;
            const int exit_status = exec_program_on_host_get_stdout(args, stdout_str);
            if(on_startup_changed)
                on_startup_changed(enable, exit_status);
            return exit_status == 0;
        };
        list->add_widget(std::move(startup_radio_button));
        return std::make_unique<Subsection>("Startup", std::move(list), mgl::vec2f(parent_page->get_inner_size().x, 0.0f));
    }

    std::unique_ptr<RadioButton> GlobalSettingsPage::create_enable_keyboard_hotkeys_button() {
        auto enable_hotkeys_radio_button = std::make_unique<RadioButton>(&get_theme().body_font, RadioButton::Orientation::HORIZONTAL);
        enable_keyboard_hotkeys_radio_button_ptr = enable_hotkeys_radio_button.get();
        enable_hotkeys_radio_button->add_item("Yes", "enable_hotkeys");
        enable_hotkeys_radio_button->add_item("No", "disable_hotkeys");
        enable_hotkeys_radio_button->add_item("Only grab virtual devices (supports input remapping software)", "enable_hotkeys_virtual_devices");
        enable_hotkeys_radio_button->on_selection_changed = [&](const std::string&, const std::string &id) {
            if(on_keyboard_hotkey_changed)
                on_keyboard_hotkey_changed(id.c_str());
            return true;
        };
        return enable_hotkeys_radio_button;
    }

    std::unique_ptr<RadioButton> GlobalSettingsPage::create_enable_joystick_hotkeys_button() {
        auto enable_hotkeys_radio_button = std::make_unique<RadioButton>(&get_theme().body_font, RadioButton::Orientation::HORIZONTAL);
        enable_joystick_hotkeys_radio_button_ptr = enable_hotkeys_radio_button.get();
        enable_hotkeys_radio_button->add_item("Yes", "enable_hotkeys");
        enable_hotkeys_radio_button->add_item("No", "disable_hotkeys");
        enable_hotkeys_radio_button->on_selection_changed = [&](const std::string&, const std::string &id) {
            if(on_joystick_hotkey_changed)
                on_joystick_hotkey_changed(id.c_str());
            return true;
        };
        return enable_hotkeys_radio_button;
    }

    std::unique_ptr<List> GlobalSettingsPage::create_show_hide_hotkey_options() {
        auto list = std::make_unique<List>(List::Orientation::HORIZONTAL, List::Alignment::CENTER);

        list->add_widget(std::make_unique<Label>(&get_theme().body_font, "Show/hide UI:", get_color_theme().text_color));
        auto show_hide_button = std::make_unique<Button>(&get_theme().body_font, "", mgl::vec2f(0.0f, 0.0f), mgl::Color(0, 0, 0, 120));
        show_hide_button_ptr = show_hide_button.get();
        list->add_widget(std::move(show_hide_button));

        show_hide_button_ptr->on_click = [this] {
            configure_hotkey_start(ConfigureHotkeyType::SHOW_HIDE);
        };


        return list;
    }

    std::unique_ptr<List> GlobalSettingsPage::create_replay_hotkey_options() {
        auto list = std::make_unique<List>(List::Orientation::HORIZONTAL, List::Alignment::CENTER);

        list->add_widget(std::make_unique<Label>(&get_theme().body_font, "Turn replay on/off:", get_color_theme().text_color));
        auto turn_replay_on_off_button = std::make_unique<Button>(&get_theme().body_font, "", mgl::vec2f(0.0f, 0.0f), mgl::Color(0, 0, 0, 120));
        turn_replay_on_off_button_ptr = turn_replay_on_off_button.get();
        list->add_widget(std::move(turn_replay_on_off_button));

        list->add_widget(std::make_unique<Label>(&get_theme().body_font, "Save replay:", get_color_theme().text_color));
        auto save_replay_button = std::make_unique<Button>(&get_theme().body_font, "", mgl::vec2f(0.0f, 0.0f), mgl::Color(0, 0, 0, 120));
        save_replay_button_ptr = save_replay_button.get();
        list->add_widget(std::move(save_replay_button));

        turn_replay_on_off_button_ptr->on_click = [this] {
            configure_hotkey_start(ConfigureHotkeyType::REPLAY_START_STOP);
        };

        save_replay_button_ptr->on_click = [this] {
            configure_hotkey_start(ConfigureHotkeyType::REPLAY_SAVE);
        };

        return list;
    }

    std::unique_ptr<List> GlobalSettingsPage::create_record_hotkey_options() {
        auto list = std::make_unique<List>(List::Orientation::HORIZONTAL, List::Alignment::CENTER);

        list->add_widget(std::make_unique<Label>(&get_theme().body_font, "Start/stop recording:", get_color_theme().text_color));
        auto start_stop_recording_button = std::make_unique<Button>(&get_theme().body_font, "", mgl::vec2f(0.0f, 0.0f), mgl::Color(0, 0, 0, 120));
        start_stop_recording_button_ptr = start_stop_recording_button.get();
        list->add_widget(std::move(start_stop_recording_button));

        list->add_widget(std::make_unique<Label>(&get_theme().body_font, "Pause/unpause recording:", get_color_theme().text_color));
        auto pause_unpause_recording_button = std::make_unique<Button>(&get_theme().body_font, "", mgl::vec2f(0.0f, 0.0f), mgl::Color(0, 0, 0, 120));
        pause_unpause_recording_button_ptr = pause_unpause_recording_button.get();
        list->add_widget(std::move(pause_unpause_recording_button));

        start_stop_recording_button_ptr->on_click = [this] {
            configure_hotkey_start(ConfigureHotkeyType::RECORD_START_STOP);
        };

        pause_unpause_recording_button_ptr->on_click = [this] {
            configure_hotkey_start(ConfigureHotkeyType::RECORD_PAUSE_UNPAUSE);
        };

        return list;
    }

    std::unique_ptr<List> GlobalSettingsPage::create_stream_hotkey_options() {
        auto list = std::make_unique<List>(List::Orientation::HORIZONTAL, List::Alignment::CENTER);

        list->add_widget(std::make_unique<Label>(&get_theme().body_font, "Start/stop streaming:", get_color_theme().text_color));
        auto start_stop_streaming_button = std::make_unique<Button>(&get_theme().body_font, "", mgl::vec2f(0.0f, 0.0f), mgl::Color(0, 0, 0, 120));
        start_stop_streaming_button_ptr = start_stop_streaming_button.get();
        list->add_widget(std::move(start_stop_streaming_button));

        start_stop_streaming_button_ptr->on_click = [this] {
            configure_hotkey_start(ConfigureHotkeyType::STREAM_START_STOP);
        };

        return list;
    }

    std::unique_ptr<List> GlobalSettingsPage::create_hotkey_control_buttons() {
        auto list = std::make_unique<List>(List::Orientation::HORIZONTAL, List::Alignment::CENTER);

        // auto clear_hotkeys_button = std::make_unique<Button>(&get_theme().body_font, "Clear hotkeys", mgl::vec2f(0.0f, 0.0f), mgl::Color(0, 0, 0, 120));
        // clear_hotkeys_button->on_click = [this] {
        //     config.streaming_config.start_stop_hotkey = {mgl::Keyboard::Unknown, 0};
        //     config.record_config.start_stop_hotkey = {mgl::Keyboard::Unknown, 0};
        //     config.record_config.pause_unpause_hotkey = {mgl::Keyboard::Unknown, 0};
        //     config.replay_config.start_stop_hotkey = {mgl::Keyboard::Unknown, 0};
        //     config.replay_config.save_hotkey = {mgl::Keyboard::Unknown, 0};
        //     config.main_config.show_hide_hotkey = {mgl::Keyboard::Unknown, 0};
        //     load_hotkeys();
        //     overlay->rebind_all_keyboard_hotkeys();
        // };
        // list->add_widget(std::move(clear_hotkeys_button));

        auto reset_hotkeys_button = std::make_unique<Button>(&get_theme().body_font, "Reset hotkeys to default", mgl::vec2f(0.0f, 0.0f), mgl::Color(0, 0, 0, 120));
        reset_hotkeys_button->on_click = [this] {
            config.streaming_config.start_stop_hotkey = {mgl::Keyboard::F8, HOTKEY_MOD_LALT};
            config.record_config.start_stop_hotkey = {mgl::Keyboard::F9, HOTKEY_MOD_LALT};
            config.record_config.pause_unpause_hotkey = {mgl::Keyboard::F7, HOTKEY_MOD_LALT};
            config.replay_config.start_stop_hotkey = {mgl::Keyboard::F10, HOTKEY_MOD_LALT | HOTKEY_MOD_LSHIFT};
            config.replay_config.save_hotkey = {mgl::Keyboard::F10, HOTKEY_MOD_LALT};
            config.main_config.show_hide_hotkey = {mgl::Keyboard::Z, HOTKEY_MOD_LALT};
            load_hotkeys();
            overlay->rebind_all_keyboard_hotkeys();
        };
        list->add_widget(std::move(reset_hotkeys_button));

        return list;
    }

    std::unique_ptr<Subsection> GlobalSettingsPage::create_hotkey_subsection(ScrollablePage *parent_page) {
        auto list = std::make_unique<List>(List::Orientation::VERTICAL);
        List *list_ptr = list.get();
        auto subsection = std::make_unique<Subsection>("Hotkeys", std::move(list), mgl::vec2f(parent_page->get_inner_size().x, 0.0f));

        list_ptr->add_widget(std::make_unique<Label>(&get_theme().body_font, "Enable keyboard hotkeys?", get_color_theme().text_color));
        list_ptr->add_widget(create_enable_keyboard_hotkeys_button());
        list_ptr->add_widget(std::make_unique<Label>(&get_theme().body_font, "Enable controller hotkeys?", get_color_theme().text_color));
        list_ptr->add_widget(create_enable_joystick_hotkeys_button());
        list_ptr->add_widget(std::make_unique<LineSeparator>(LineSeparator::Orientation::HORIZONTAL, subsection->get_inner_size().x));
        list_ptr->add_widget(create_show_hide_hotkey_options());
        list_ptr->add_widget(create_replay_hotkey_options());
        list_ptr->add_widget(create_record_hotkey_options());
        list_ptr->add_widget(create_stream_hotkey_options());
        list_ptr->add_widget(std::make_unique<Label>(&get_theme().body_font, "Double-click the controller share button to save a replay", get_color_theme().text_color));
        list_ptr->add_widget(create_hotkey_control_buttons());
        return subsection;
    }

    std::unique_ptr<Button> GlobalSettingsPage::create_exit_program_button() {
        auto exit_program_button = std::make_unique<Button>(&get_theme().body_font, "Exit program", mgl::vec2f(0.0f, 0.0f), mgl::Color(0, 0, 0, 120));
        exit_program_button->on_click = [&]() {
            if(on_click_exit_program_button)
                on_click_exit_program_button("exit");
        };
        return exit_program_button;
    }

    std::unique_ptr<Button> GlobalSettingsPage::create_go_back_to_old_ui_button() {
        auto exit_program_button = std::make_unique<Button>(&get_theme().body_font, "Go back to the old UI", mgl::vec2f(0.0f, 0.0f), mgl::Color(0, 0, 0, 120));
        exit_program_button->on_click = [&]() {
            if(on_click_exit_program_button)
                on_click_exit_program_button("back-to-old-ui");
        };
        return exit_program_button;
    }

    std::unique_ptr<Subsection> GlobalSettingsPage::create_application_options_subsection(ScrollablePage *parent_page) {
        const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
        auto list = std::make_unique<List>(List::Orientation::HORIZONTAL);
        list->add_widget(create_exit_program_button());
        if(inside_flatpak)
            list->add_widget(create_go_back_to_old_ui_button());
        return std::make_unique<Subsection>("Application options", std::move(list), mgl::vec2f(parent_page->get_inner_size().x, 0.0f));
    }

    std::unique_ptr<Subsection> GlobalSettingsPage::create_application_info_subsection(ScrollablePage *parent_page) {
        const bool inside_flatpak = getenv("FLATPAK_ID") != NULL;
        auto list = std::make_unique<List>(List::Orientation::VERTICAL);

        char str[128];
        const std::string gsr_version = gsr_info->system_info.gsr_version.to_string();
        snprintf(str, sizeof(str), "GSR version: %s", gsr_version.c_str());
        list->add_widget(std::make_unique<Label>(&get_theme().body_font, str, get_color_theme().text_color));

        snprintf(str, sizeof(str), "GSR-UI version: %s", GSR_UI_VERSION);
        list->add_widget(std::make_unique<Label>(&get_theme().body_font, str, get_color_theme().text_color));

        if(inside_flatpak) {
            snprintf(str, sizeof(str), "Flatpak version: %s", GSR_FLATPAK_VERSION);
            list->add_widget(std::make_unique<Label>(&get_theme().body_font, str, get_color_theme().text_color));
        }

        snprintf(str, sizeof(str), "GPU vendor: %s", gpu_vendor_to_string(gsr_info->gpu_info.vendor));
        list->add_widget(std::make_unique<Label>(&get_theme().body_font, str, get_color_theme().text_color));

        return std::make_unique<Subsection>("Application info", std::move(list), mgl::vec2f(parent_page->get_inner_size().x, 0.0f));
    }

    void GlobalSettingsPage::add_widgets() {
        auto scrollable_page = std::make_unique<ScrollablePage>(content_page_ptr->get_inner_size());

        auto settings_list = std::make_unique<List>(List::Orientation::VERTICAL);
        settings_list->set_spacing(0.018f);
        settings_list->add_widget(create_appearance_subsection(scrollable_page.get()));
        settings_list->add_widget(create_startup_subsection(scrollable_page.get()));
        settings_list->add_widget(create_hotkey_subsection(scrollable_page.get()));
        settings_list->add_widget(create_application_options_subsection(scrollable_page.get()));
        settings_list->add_widget(create_application_info_subsection(scrollable_page.get()));
        scrollable_page->add_widget(std::move(settings_list));

        content_page_ptr->add_widget(std::move(scrollable_page));
    }

    void GlobalSettingsPage::on_navigate_away_from_page() {
        save();
    }

    void GlobalSettingsPage::load() {
        if(config.main_config.tint_color.empty())
            tint_color_radio_button_ptr->set_selected_item(gpu_vendor_to_color_name(gsr_info->gpu_info.vendor));
        else
            tint_color_radio_button_ptr->set_selected_item(config.main_config.tint_color);

        const char *args[] = { "systemctl", "is-enabled", "--quiet", "--user", "gpu-screen-recorder-ui", nullptr };
        std::string stdout_str;
        const int exit_status = exec_program_on_host_get_stdout(args, stdout_str);
        startup_radio_button_ptr->set_selected_item(exit_status == 0 ? "start_on_system_startup" : "dont_start_on_system_startup", false, false);

        enable_keyboard_hotkeys_radio_button_ptr->set_selected_item(config.main_config.hotkeys_enable_option, false, false);
        enable_joystick_hotkeys_radio_button_ptr->set_selected_item(config.main_config.joystick_hotkeys_enable_option, false, false);

        load_hotkeys();
    }

    void GlobalSettingsPage::load_hotkeys() {
        turn_replay_on_off_button_ptr->set_text(config_hotkey_to_string(config.replay_config.start_stop_hotkey));
        save_replay_button_ptr->set_text(config_hotkey_to_string(config.replay_config.save_hotkey));

        start_stop_recording_button_ptr->set_text(config_hotkey_to_string(config.record_config.start_stop_hotkey));
        pause_unpause_recording_button_ptr->set_text(config_hotkey_to_string(config.record_config.pause_unpause_hotkey));

        start_stop_streaming_button_ptr->set_text(config_hotkey_to_string(config.streaming_config.start_stop_hotkey));

        show_hide_button_ptr->set_text(config_hotkey_to_string(config.main_config.show_hide_hotkey));
    }

    void GlobalSettingsPage::save() {
        configure_hotkey_cancel();
        config.main_config.tint_color = tint_color_radio_button_ptr->get_selected_id();
        config.main_config.hotkeys_enable_option = enable_keyboard_hotkeys_radio_button_ptr->get_selected_id();
        config.main_config.joystick_hotkeys_enable_option = enable_joystick_hotkeys_radio_button_ptr->get_selected_id();
        save_config(config);
    }

    bool GlobalSettingsPage::on_event(mgl::Event &event, mgl::Window &window, mgl::vec2f offset) {
        if(!StaticPage::on_event(event, window, offset))
            return false;

        if(configure_hotkey_type == ConfigureHotkeyType::NONE)
            return true;

        Button *configure_hotkey_button = configure_hotkey_get_button_by_active_type();
        if(!configure_hotkey_button)
            return true;

        if(event.type == mgl::Event::KeyPressed) {
            if(event.key.code == mgl::Keyboard::Escape)
                return false;

            if(mgl::Keyboard::key_is_modifier(event.key.code)) {
                configure_config_hotkey.modifiers |= mgl_modifier_to_hotkey_modifier(event.key.code);
                configure_hotkey_button->set_text(config_hotkey_to_string(configure_config_hotkey));
            } else if(configure_config_hotkey.modifiers != 0) {
                configure_config_hotkey.key = event.key.code;
                configure_hotkey_button->set_text(config_hotkey_to_string(configure_config_hotkey));
                configure_hotkey_stop_and_save();
            }

            return false;
        } else if(event.type == mgl::Event::KeyReleased) {
            if(event.key.code == mgl::Keyboard::Escape) {
                configure_hotkey_cancel();
                return false;
            }

            if(mgl::Keyboard::key_is_modifier(event.key.code)) {
                configure_config_hotkey.modifiers &= ~mgl_modifier_to_hotkey_modifier(event.key.code);
                configure_hotkey_button->set_text(config_hotkey_to_string(configure_config_hotkey));
            }

            return false;
        }

        return true;
    }

    Button* GlobalSettingsPage::configure_hotkey_get_button_by_active_type() {
        switch(configure_hotkey_type) {
            case ConfigureHotkeyType::NONE:
                return nullptr;
            case ConfigureHotkeyType::REPLAY_START_STOP:
                return turn_replay_on_off_button_ptr;
            case ConfigureHotkeyType::REPLAY_SAVE:
                return save_replay_button_ptr;
            case ConfigureHotkeyType::RECORD_START_STOP:
                return start_stop_recording_button_ptr;
            case ConfigureHotkeyType::RECORD_PAUSE_UNPAUSE:
                return pause_unpause_recording_button_ptr;
            case ConfigureHotkeyType::STREAM_START_STOP:
                return start_stop_streaming_button_ptr;
            case ConfigureHotkeyType::SHOW_HIDE:
                return show_hide_button_ptr;
        }
        return nullptr;
    }

    ConfigHotkey* GlobalSettingsPage::configure_hotkey_get_config_by_active_type() {
        switch(configure_hotkey_type) {
            case ConfigureHotkeyType::NONE:
                return nullptr;
            case ConfigureHotkeyType::REPLAY_START_STOP:
                return &config.replay_config.start_stop_hotkey;
            case ConfigureHotkeyType::REPLAY_SAVE:
                return &config.replay_config.save_hotkey;
            case ConfigureHotkeyType::RECORD_START_STOP:
                return &config.record_config.start_stop_hotkey;
            case ConfigureHotkeyType::RECORD_PAUSE_UNPAUSE:
                return &config.record_config.pause_unpause_hotkey;
            case ConfigureHotkeyType::STREAM_START_STOP:
                return &config.streaming_config.start_stop_hotkey;
            case ConfigureHotkeyType::SHOW_HIDE:
                return &config.main_config.show_hide_hotkey;
        }
        return nullptr;
    }

    void GlobalSettingsPage::for_each_config_hotkey(std::function<void(ConfigHotkey *config_hotkey)> callback) {
        ConfigHotkey *config_hotkeys[] = {
            &config.replay_config.start_stop_hotkey,
            &config.replay_config.save_hotkey,
            &config.record_config.start_stop_hotkey,
            &config.record_config.pause_unpause_hotkey,
            &config.streaming_config.start_stop_hotkey,
            &config.main_config.show_hide_hotkey
        };
        for(ConfigHotkey *config_hotkey : config_hotkeys) {
            callback(config_hotkey);
        }
    }

    void GlobalSettingsPage::configure_hotkey_start(ConfigureHotkeyType hotkey_type) {
        assert(hotkey_type != ConfigureHotkeyType::NONE);
        configure_config_hotkey = {0, 0};
        configure_hotkey_type = hotkey_type;

        content_page_ptr->set_visible(false);
        hotkey_overlay_ptr->set_visible(true);
        overlay->unbind_all_keyboard_hotkeys();
    }

    void GlobalSettingsPage::configure_hotkey_cancel() {
        Button *config_hotkey_button = configure_hotkey_get_button_by_active_type();
        ConfigHotkey *config_hotkey = configure_hotkey_get_config_by_active_type();
        if(config_hotkey_button && config_hotkey)
            config_hotkey_button->set_text(config_hotkey_to_string(*config_hotkey));

        configure_config_hotkey = {0, 0};
        configure_hotkey_type = ConfigureHotkeyType::NONE;
        content_page_ptr->set_visible(true);
        hotkey_overlay_ptr->set_visible(false);
        overlay->rebind_all_keyboard_hotkeys();
    }

    void GlobalSettingsPage::configure_hotkey_stop_and_save() {
        Button *config_hotkey_button = configure_hotkey_get_button_by_active_type();
        ConfigHotkey *config_hotkey = configure_hotkey_get_config_by_active_type();
        if(config_hotkey_button && config_hotkey) {
            bool hotkey_used_by_another_action = false;
            for_each_config_hotkey([&](ConfigHotkey *config_hotkey_item) {
                if(config_hotkey_item != config_hotkey && *config_hotkey_item == configure_config_hotkey)
                    hotkey_used_by_another_action = true;
            });

            if(hotkey_used_by_another_action) {
                const std::string error_msg = "The hotkey \"" + config_hotkey_to_string(configure_config_hotkey) + " is already used for something else";
                overlay->show_notification(error_msg.c_str(), 3.0, mgl::Color(255, 0, 0, 255), mgl::Color(255, 0, 0, 255), NotificationType::NONE);
                config_hotkey_button->set_text(config_hotkey_to_string(*config_hotkey));
                configure_config_hotkey = {0, 0};
                return;
            }

            *config_hotkey = configure_config_hotkey;
        }

        configure_config_hotkey = {0, 0};
        configure_hotkey_type = ConfigureHotkeyType::NONE;
        content_page_ptr->set_visible(true);
        hotkey_overlay_ptr->set_visible(false);
        overlay->rebind_all_keyboard_hotkeys();
    }
}