Add menu item for dark mode

This commit is contained in:
daleclack 2022-12-19 08:04:27 +08:00
parent d0cdfe02d7
commit fb16495ec7
5 changed files with 34 additions and 4 deletions

View File

@ -21,6 +21,9 @@
<attribute name='action'>win.back3</attribute>
</item>
</submenu>
<item>
<attribute name='custom'>check_dark</attribute>
</item>
<item>
<attribute name='label'>About</attribute>
<attribute name='action'>win.about</attribute>

View File

@ -181,6 +181,16 @@ void MyPrefs::set_parent_win(Gtk::Window *parent)
msg_dialog1.set_transient_for(*parent);
}
void MyPrefs::set_dark_mode(bool dark_mode_enabled){
// Put the config of dark mode to the class
dark_mode = dark_mode_enabled;
}
void MyPrefs::save_config_now(){
// Save config when the dark mode config is modified
btnok_clicked();
}
void MyPrefs::btnpath_clicked()
{
// Create a Dialog

View File

@ -32,6 +32,8 @@ public:
static MyPrefs *create();
void set_parent_win(Gtk::Window *parent);
void init_json_data(json &data1);
void save_config_now();
void set_dark_mode(bool dark_mode_enabled);
private:
Glib::RefPtr<Gtk::Builder> ref_Glade;

View File

@ -18,7 +18,7 @@ MyWin::MyWin()
btn_ver("Xe-Ver"),
cfg_box(Gtk::Orientation::VERTICAL, 5),
msg_dialog(*this),
dark_mode(true)
check_dark("Enable dark mode")
{
// Initalize window
set_icon_name("Xe-Release");
@ -58,7 +58,6 @@ MyWin::MyWin()
btn_box.append(btn_ver);
btn_box.set_opacity(0.7);
overlay.add_overlay(btn_box);
btn_ver.signal_clicked().connect(sigc::mem_fun(*this, &MyWin::main_releases));
// Show everything
set_child(back_overlay);
@ -79,7 +78,7 @@ MyWin::MyWin()
// Create Style for widgets
provider = Gtk::CssProvider::create();
if(dark_mode){
if(check_dark.get_active()){
provider->load_from_resource("/org/gtk/daleclack/style_dark.css");
}else{
provider->load_from_resource("/org/gtk/daleclack/style.css");
@ -92,6 +91,10 @@ MyWin::MyWin()
switcher.set_stack(stack1);
// show_all_children();
// Connect Signals
btn_ver.signal_clicked().connect(sigc::mem_fun(*this, &MyWin::main_releases));
check_dark.signal_toggled().connect(sigc::mem_fun(*this, &MyWin::check_toggled));
// Free Memory
pixbuf.reset();
sized.reset();
@ -114,6 +117,9 @@ void MyWin::titlebar_init()
auto object = menu_builder->get_object<Gio::MenuModel>("app-menu");
popover.set_menu_model(object);
// Add a check button for dark mode
popover.add_child(check_dark, "check_dark");
// Add Menu Actions
add_action("configs", sigc::mem_fun(*this, &MyWin::config_dialog));
add_action("back1", sigc::mem_fun(*this, &MyWin::background1));
@ -186,6 +192,7 @@ void MyWin::load_config(){
config_longterm = data["Longterm"];
config_stable = data["Stable"];
config_devel = data["Develop"];
check_dark.set_active(data["dark_mode"]);
}
else
{
@ -229,6 +236,12 @@ void MyWin::main_releases()
}
}
void MyWin::check_toggled(){
// Get the state of check button and set the config to json file
prefs->set_dark_mode(check_dark.get_active());
prefs->save_config_now();
}
void MyWin::about_dialog()
{
char *version, *copyright;

View File

@ -29,7 +29,8 @@ private:
void titlebar_init();
// Css Style
bool dark_mode;
// bool dark_mode;
Gtk::CheckButton check_dark;
Glib::RefPtr<Gtk::CssProvider> provider;
// Config Page
@ -55,4 +56,5 @@ private:
void on_window_hide(Gtk::Window *window);
void about_dialog();
void main_releases();
void check_toggled();
};