Synchronize active GTK2 and GTK3 themes with .xth theme selection
This commit is contained in:
1 parent
21485612fb
commit
16fcaaaba6
16 files changed
+949
-16
No files matched your search
@@ -79,7 +79,7 @@ static void apply_selected(themes_state_t *st) {
|
||||
const char *argv[] = {"/usr/bin/geminux-apply-theme", st->items[st->sel].name, NULL};
|
||||
int rc = gemui_run_cmd_capture(argv, NULL, 0);
|
||||
gemui_theme_reload(st->app);
|
||||
gemui_show_toast(st->app, rc == 0 ? "Applied .Xresources + dunst theme" : "Theme apply failed", 2000);
|
||||
gemui_show_toast(st->app, rc == 0 ? "Applied .Xresources, GTK2/GTK3 & dunst theme" : "Theme apply failed", 2000);
|
||||
gemui_request_redraw(st->app);
|
||||
}
|
||||
|
||||
@@ -146,7 +146,7 @@ static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
|
||||
}
|
||||
|
||||
gemui_draw_button(app, cr, (gemui_rect_t){right_x, h - 94, right_w, 74},
|
||||
"Apply Across DWM, st, dmenu, dunst & Apps", HIT_APPLY, 0, true, false, false);
|
||||
"Apply Across DWM, st, dmenu, dunst, GTK2/3 & Apps", HIT_APPLY, 0, true, false, false);
|
||||
}
|
||||
|
||||
static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *utf8, void *ud) {
|
||||
|
||||
@@ -75,7 +75,6 @@ void configureWebkitRenderer(bool software) {
|
||||
::unsetenv("WEBKIT_DISABLE_COMPOSITING_MODE");
|
||||
::unsetenv("WEBKIT_SKIA_ENABLE_CPU_RENDERING");
|
||||
::setenv("GDK_BACKEND", "x11", 1);
|
||||
::setenv("GTK_THEME", "Adwaita:dark", 1);
|
||||
::setenv("NO_AT_BRIDGE", "1", 1);
|
||||
::setenv("GTK_A11Y", "none", 1);
|
||||
::setenv("WEBKIT_DISABLE_SANDBOX_THIS_IS_DANGEROUS", "1", 1);
|
||||
@@ -322,6 +321,7 @@ static const UaPreset kUaPresets[] = {
|
||||
};
|
||||
|
||||
struct XresTheme {
|
||||
std::string name = "GeminuxHelio";
|
||||
std::string fontFace = "Fira Code, Noto Sans";
|
||||
std::string bgMain = "#0f1419";
|
||||
std::string bgBar = "#0b0e14";
|
||||
@@ -370,6 +370,7 @@ XresTheme loadXresourcesTheme() {
|
||||
auto it = kv.find(k);
|
||||
if (it != kv.end() && !it->second.empty()) dst = it->second;
|
||||
};
|
||||
pick("theme.name", t.name);
|
||||
pick("theme.fontFace", t.fontFace);
|
||||
pick("theme.bgMain", t.bgMain);
|
||||
pick("theme.bgBar", t.bgBar);
|
||||
@@ -478,14 +479,54 @@ struct BrowserApp {
|
||||
bool syncingUiFlags = false;
|
||||
};
|
||||
|
||||
bool isDarkHexColor(const std::string& hex) {
|
||||
if (hex.size() != 7 || hex[0] != '#') return true;
|
||||
auto hexByte = [&](size_t pos, int& out) -> bool {
|
||||
auto nib = [](char c) -> int {
|
||||
if (c >= '0' && c <= '9') return c - '0';
|
||||
if (c >= 'a' && c <= 'f') return 10 + (c - 'a');
|
||||
if (c >= 'A' && c <= 'F') return 10 + (c - 'A');
|
||||
return -1;
|
||||
};
|
||||
int hi = nib(hex[pos]);
|
||||
int lo = nib(hex[pos + 1]);
|
||||
if (hi < 0 || lo < 0) return false;
|
||||
out = (hi << 4) | lo;
|
||||
return true;
|
||||
};
|
||||
int r = 0, g = 0, b = 0;
|
||||
if (!hexByte(1, r) || !hexByte(3, g) || !hexByte(5, b)) return true;
|
||||
double lum = 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
||||
return lum < 128.0;
|
||||
}
|
||||
|
||||
std::string sanitizeGtkThemeSuffix(const std::string& raw) {
|
||||
std::string out;
|
||||
for (unsigned char ch : raw) {
|
||||
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
|
||||
(ch >= '0' && ch <= '9') || ch == '.' || ch == '_' || ch == '-') {
|
||||
out.push_back(static_cast<char>(ch));
|
||||
}
|
||||
}
|
||||
return out.empty() ? "GeminuxHelio" : out;
|
||||
}
|
||||
|
||||
void applyGtkThemeCss(BrowserApp* app) {
|
||||
if (!app || !app->cssProvider) return;
|
||||
XresTheme t = loadXresourcesTheme();
|
||||
std::string gtkFont = t.fontFace + ", Noto Sans 9";
|
||||
bool preferDark = isDarkHexColor(t.bgMain);
|
||||
std::string candidateTheme = "Geminux-" + sanitizeGtkThemeSuffix(t.name);
|
||||
std::string homeThemeCss = getHomeDirPath() + "/.themes/" + candidateTheme + "/gtk-3.0/gtk.css";
|
||||
std::string sysThemeCss = "/usr/share/themes/" + candidateTheme + "/gtk-3.0/gtk.css";
|
||||
std::string gtkThemeName =
|
||||
(::access(homeThemeCss.c_str(), R_OK) == 0 || ::access(sysThemeCss.c_str(), R_OK) == 0)
|
||||
? candidateTheme
|
||||
: "Adwaita";
|
||||
if (GtkSettings* gs = gtk_settings_get_default()) {
|
||||
g_object_set(gs,
|
||||
"gtk-application-prefer-dark-theme", TRUE,
|
||||
"gtk-theme-name", "Adwaita:dark",
|
||||
"gtk-application-prefer-dark-theme", preferDark ? TRUE : FALSE,
|
||||
"gtk-theme-name", gtkThemeName.c_str(),
|
||||
"gtk-font-name", gtkFont.c_str(),
|
||||
nullptr);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user