diff options
author | dec05eba <dec05eba@protonmail.com> | 2024-12-31 21:43:10 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2024-12-31 21:43:10 +0100 |
commit | ae0fb952920d768dc35a746c6f4334116e3150da (patch) | |
tree | 982629a1fa0cf85fa5854a48491f3b9d574186a5 | |
parent | fb348d651daeb6268b8cfcd9a929e233f9abb8d2 (diff) |
Better global settings design, dont change startup setting in ui if systemctl command fails
-rw-r--r-- | include/gui/RadioButton.hpp | 3 | ||||
-rw-r--r-- | src/gui/GlobalSettingsPage.cpp | 11 | ||||
-rw-r--r-- | src/gui/RadioButton.cpp | 20 | ||||
-rw-r--r-- | src/gui/SettingsPage.cpp | 7 |
4 files changed, 26 insertions, 15 deletions
diff --git a/include/gui/RadioButton.hpp b/include/gui/RadioButton.hpp index a009eab..16d638e 100644 --- a/include/gui/RadioButton.hpp +++ b/include/gui/RadioButton.hpp @@ -27,7 +27,8 @@ namespace gsr { mgl::vec2f get_size() override; - std::function<void(const std::string &text, const std::string &id)> on_selection_changed; + // Return false to revert the change + std::function<bool(const std::string &text, const std::string &id)> on_selection_changed; private: void update_if_dirty(); private: diff --git a/src/gui/GlobalSettingsPage.cpp b/src/gui/GlobalSettingsPage.cpp index ca4c4ea..7c8b902 100644 --- a/src/gui/GlobalSettingsPage.cpp +++ b/src/gui/GlobalSettingsPage.cpp @@ -55,6 +55,7 @@ namespace gsr { 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)); @@ -62,10 +63,11 @@ namespace gsr { std::unique_ptr<Subsection> GlobalSettingsPage::create_startup_subsection(ScrollablePage *parent_page) { auto list = std::make_unique<List>(List::Orientation::VERTICAL); - auto startup_radio_button = std::make_unique<RadioButton>(&get_theme().body_font, RadioButton::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("Don't start this program on system startup", "dont_start_on_system_startup"); - startup_radio_button->add_item("Start this program on system startup", "start_on_system_startup"); + 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") @@ -73,13 +75,14 @@ namespace gsr { else if(id == "start_on_system_startup") enable = true; else - return; + 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)); diff --git a/src/gui/RadioButton.cpp b/src/gui/RadioButton.cpp index 061d811..a6ef96a 100644 --- a/src/gui/RadioButton.cpp +++ b/src/gui/RadioButton.cpp @@ -35,12 +35,12 @@ namespace gsr { const bool mouse_inside = mgl::FloatRect(draw_pos, item_size).contains(mgl::vec2f(event.mouse_button.x, event.mouse_button.y)); if(mouse_inside) { - const size_t prev_selected_item = selected_item; - selected_item = i; - - if(selected_item != prev_selected_item && on_selection_changed) - on_selection_changed(item.text.get_string(), item.id); + if(selected_item != i && on_selection_changed) { + if(!on_selection_changed(item.text.get_string(), item.id)) + return false; + } + selected_item = i; return false; } @@ -158,12 +158,12 @@ namespace gsr { for(size_t i = 0; i < items.size(); ++i) { auto &item = items[i]; if(item.id == id) { - const size_t prev_selected_item = selected_item; - selected_item = i; - - if(trigger_event && (trigger_event_even_if_selection_not_changed || selected_item != prev_selected_item) && on_selection_changed) - on_selection_changed(item.text.get_string(), item.id); + if(trigger_event && (trigger_event_even_if_selection_not_changed || selected_item != i) && on_selection_changed) { + if(!on_selection_changed(item.text.get_string(), item.id)) + break; + } + selected_item = i; break; } } diff --git a/src/gui/SettingsPage.cpp b/src/gui/SettingsPage.cpp index 2e6fa08..0474b91 100644 --- a/src/gui/SettingsPage.cpp +++ b/src/gui/SettingsPage.cpp @@ -516,6 +516,7 @@ namespace gsr { video_resolution_list_ptr->set_visible(!focused_selected && change_video_resolution_checkbox_ptr->is_checked()); change_video_resolution_checkbox_ptr->set_visible(!focused_selected); restore_portal_session_list_ptr->set_visible(portal_selected); + return true; }; change_video_resolution_checkbox_ptr->on_changed = [this](bool checked) { @@ -530,6 +531,8 @@ namespace gsr { if(estimated_file_size_ptr) estimated_file_size_ptr->set_visible(custom_selected); + + return true; }; video_quality_box_ptr->on_selection_changed("", video_quality_box_ptr->get_selected_id()); @@ -705,6 +708,7 @@ namespace gsr { framerate_mode_list_ptr->set_visible(advanced_view); notifications_subsection_ptr->set_visible(advanced_view); settings_scrollable_page_ptr->reset_scroll(); + return true; }; view_radio_button_ptr->on_selection_changed("Simple", "simple"); @@ -760,6 +764,7 @@ namespace gsr { framerate_mode_list_ptr->set_visible(advanced_view); notifications_subsection_ptr->set_visible(advanced_view); settings_scrollable_page_ptr->reset_scroll(); + return true; }; view_radio_button_ptr->on_selection_changed("Simple", "simple"); } @@ -860,6 +865,7 @@ namespace gsr { container_list_ptr->set_visible(custom_option); twitch_stream_key_entry_ptr->set_visible(twitch_option); youtube_stream_key_entry_ptr->set_visible(youtube_option); + return true; }; streaming_service_box_ptr->on_selection_changed("Twitch", "twitch"); @@ -872,6 +878,7 @@ namespace gsr { framerate_mode_list_ptr->set_visible(advanced_view); notifications_subsection_ptr->set_visible(advanced_view); settings_scrollable_page_ptr->reset_scroll(); + return true; }; view_radio_button_ptr->on_selection_changed("Simple", "simple"); } |