Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dark mode support for Linux #4415

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/libslic3r/AppConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ void AppConfig::set_defaults()
//#endif
#endif // _WIN32

#ifdef __LINUX__
if (get("dark_color_mode").empty())
set("dark_color_mode", "0");
#endif // __LINUX__ dark mode

// BBS
/*if (get("3mf_include_gcode").empty())
set_bool("3mf_include_gcode", true);*/
Expand Down
61 changes: 56 additions & 5 deletions src/slic3r/GUI/GUI_App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2574,10 +2574,15 @@ bool GUI_App::on_init_inner()
#ifdef _MSW_DARK_MODE

#ifndef __WINDOWS__
wxSystemAppearance app = wxSystemSettings::GetAppearance();
GUI::wxGetApp().app_config->set("dark_color_mode", app.IsDark() ? "1" : "0");
GUI::wxGetApp().app_config->save();
#endif // __APPLE__
#if defined(__LINUX__) && (defined(__WXGTK20__) || defined(__WXGTK3__)) // __LINUX__ dark mode
bool is_dark = (wxGetApp().app_config->get("dark_color_mode") == "1" ? true : false);
g_object_set (gtk_settings_get_default (), "gtk-theme-name", (is_dark) ? "" : " ", NULL);
#else
wxSystemAppearance app = wxSystemSettings::GetAppearance();
GUI::wxGetApp().app_config->set("dark_color_mode", app.IsDark() ? "1" : "0");
GUI::wxGetApp().app_config->save();
#endif
#endif // __APPLE__ / __LINUX__


bool init_dark_color_mode = dark_mode();
Expand Down Expand Up @@ -3265,6 +3270,30 @@ static bool is_default(wxWindow* win)
}
#endif

// Checks if fg is +/- 10 rgb values from bg
bool GUI_App::is_similar(const wxColour& bg, const wxColour& fg){
int bg_r = bg.Red();
int bg_g = bg.Green();
int bg_b = bg.Blue();

int fg_r = fg.Red();
int fg_g = fg.Green();
int fg_b = fg.Blue();

return (bg_r+10 >= fg_r && fg_r >= bg_r-10
&& bg_g+10 >= fg_g && fg_g >= bg_g-10
&& bg_b+10 >= fg_b && fg_b >= bg_b-10);
}

const wxColour GUI_App::invert_color(const wxColour& color){
int color_r = 255-color.Red();
int color_g = 255-color.Green();
int color_b = 255-color.Blue();

return wxColour(color_r, color_g, color_b);
}


void GUI_App::UpdateDarkUI(wxWindow* window, bool highlited/* = false*/, bool just_font/* = false*/)
{
if (wxButton *btn = dynamic_cast<wxButton*>(window)) {
Expand Down Expand Up @@ -3324,9 +3353,20 @@ void GUI_App::UpdateDarkUI(wxWindow* window, bool highlited/* = false*/, bool ju
original_col = window->GetForegroundColour();
auto fg_col = StateColor::darkModeColorFor(original_col);

#ifndef __LINUX__
if (fg_col != original_col) {
window->SetForegroundColour(fg_col);
}
#else
if (fg_col != original_col) {
if(bg_col != fg_col && !is_similar(bg_col, fg_col)){
window->SetForegroundColour(fg_col);
}
else{ // if bg and fg colors are the same, invert fg color
window->SetForegroundColour(invert_color(fg_col));
}
}
#endif
}
else {
auto original_col = window->GetBackgroundColour();
Expand All @@ -3339,9 +3379,20 @@ void GUI_App::UpdateDarkUI(wxWindow* window, bool highlited/* = false*/, bool ju
original_col = window->GetForegroundColour();
auto fg_col = StateColor::lightModeColorFor(original_col);

#ifndef __LINUX__
if (fg_col != original_col) {
window->SetForegroundColour(fg_col);
}
#else
if (fg_col != original_col) {
if(bg_col != fg_col && !is_similar(bg_col, fg_col)){
window->SetForegroundColour(fg_col);
}
else{ // if bg and fg colors are the same, invert fg color
window->SetForegroundColour(invert_color(fg_col));
}
}
#endif
}
}

Expand Down Expand Up @@ -3414,7 +3465,7 @@ void GUI_App::UpdateDVCDarkUI(wxDataViewCtrl* dvc, bool highlited/* = false*/)

void GUI_App::UpdateAllStaticTextDarkUI(wxWindow* parent)
{
#ifdef __WINDOWS__
#if defined(__WINDOWS__) || defined(__LINUX__)
wxGetApp().UpdateDarkUI(parent);

auto children = parent->GetChildren();
Expand Down
2 changes: 2 additions & 0 deletions src/slic3r/GUI/GUI_App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,8 @@ class GUI_App : public wxApp
void update_publish_status();
bool has_model_mall();
void update_label_colours();
bool is_similar(const wxColour& bg, const wxColour& fg);
const wxColour invert_color(const wxColour& color);
// update color mode for window
void UpdateDarkUI(wxWindow *window, bool highlited = false, bool just_font = false);
void UpdateDarkUIWin(wxWindow* win);
Expand Down
8 changes: 8 additions & 0 deletions src/slic3r/GUI/MainFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
#endif // _WIN32
#include <slic3r/GUI/CreatePresetsDialog.hpp>

#if defined(__WXGTK20__) || defined(__WXGTK3__)
#include <gtk/gtk.h>
#endif

namespace Slic3r {
namespace GUI {
Expand Down Expand Up @@ -676,6 +679,11 @@ DPIFrame(NULL, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, BORDERLESS_FRAME_
wxGetApp().UpdateDarkUIWin(this);
#endif // _MSW_DARK_MODE

#if defined(__LINUX__) && (defined(__WXGTK20__) || defined(__WXGTK3__))
bool is_dark = (wxGetApp().app_config->get("dark_color_mode") == "1" ? true : false);
g_object_set (gtk_settings_get_default (), "gtk-theme-name", (is_dark) ? "" : " ", NULL);
#endif // __LINUX__ dark mode

wxGetApp().persist_window_geometry(this, true);
wxGetApp().persist_window_geometry(&m_settings_dialog, true);
}
Expand Down
16 changes: 13 additions & 3 deletions src/slic3r/GUI/Preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#endif // _MSW_DARK_MODE
#endif //__WINDOWS__

#if defined(__WXGTK20__) || defined(__WXGTK3__)
#include <gtk/gtk.h>
#endif

namespace Slic3r { namespace GUI {

WX_DEFINE_LIST(RadioSelectorList);
Expand Down Expand Up @@ -599,6 +603,12 @@ wxBoxSizer* PreferencesDialog::create_item_darkmode_checkbox(wxString title, wxW
wxGetApp().update_ui_from_settings();
set_dark_mode();
#endif

#if defined(__LINUX__) && (defined(__WXGTK20__) || defined(__WXGTK3__))
bool is_dark = (wxGetApp().app_config->get("dark_color_mode") == "1" ? true : false);
g_object_set (gtk_settings_get_default (), "gtk-theme-name", (is_dark) ? "" : " ", NULL);
#endif // __LINUX__ dark mode

SimpleEvent evt = SimpleEvent(EVT_GLCANVAS_COLOR_MODE_CHANGED);
wxPostEvent(wxGetApp().plater(), evt);
e.Skip();
Expand Down Expand Up @@ -1081,7 +1091,7 @@ wxWindow* PreferencesDialog::create_general_page()
auto item_downloads = create_item_downloads(page,50,"download_path");

//dark mode
#ifdef _WIN32
#if defined(_WIN32) || (defined(__LINUX__) && (defined(__WXGTK20__) || defined(__WXGTK3__)))
auto title_darkmode = create_item_title(_L("Dark Mode"), page, _L("Dark Mode"));
auto item_darkmode = create_item_darkmode_checkbox(_L("Enable dark mode"), page,_L("Enable dark mode"), 50, "dark_color_mode");
#endif
Expand Down Expand Up @@ -1139,10 +1149,10 @@ wxWindow* PreferencesDialog::create_general_page()
sizer_page->Add(title_downloads, 0, wxTOP| wxEXPAND, FromDIP(20));
sizer_page->Add(item_downloads, 0, wxEXPAND, FromDIP(3));

#ifdef _WIN32
#if defined(_WIN32) || (defined(__LINUX__) && (defined(__WXGTK20__) || defined(__WXGTK3__)))
sizer_page->Add(title_darkmode, 0, wxTOP | wxEXPAND, FromDIP(20));
sizer_page->Add(item_darkmode, 0, wxEXPAND, FromDIP(3));
#endif
#endif // _WIN32 && __LINUX__

sizer_page->Add(title_user_experience, 0, wxTOP | wxEXPAND, FromDIP(20));
sizer_page->Add(item_priv_policy, 0, wxTOP, FromDIP(3));
Expand Down