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

SettingsPopover: use provider_for_screen for theme buttons #785

Merged
merged 5 commits into from
Sep 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 data/Application.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ vte-terminal {
padding: 0 9px;
}

.color-button radio {
padding: 10px;
-gtk-icon-shadow: none;
}

.color-custom radio {
-gtk-icon-source: -gtk-icontheme("list-add-symbolic");
}
Expand Down
28 changes: 21 additions & 7 deletions src/Widgets/SettingsPopover.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ public sealed class Terminal.SettingsPopover : Gtk.Popover {
}

private const string STYLE_CSS = """
.color-button radio {
.color-button.%s radio {
background-color: %s;
color: %s;
padding: 10px;
jeremypw marked this conversation as resolved.
Show resolved Hide resolved
-gtk-icon-shadow: none;
}
""";

Expand Down Expand Up @@ -167,11 +165,13 @@ public sealed class Terminal.SettingsPopover : Gtk.Popover {
halign = Gtk.Align.CENTER
};

button.get_style_context ().add_class (Granite.STYLE_CLASS_COLOR_BUTTON);
button.get_style_context ().add_class (theme_to_css_class (theme));

css_provider = new Gtk.CssProvider ();

unowned var style_context = button.get_style_context ();
style_context.add_class (Granite.STYLE_CLASS_COLOR_BUTTON);
style_context.add_provider (css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
Gtk.StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);

update_theme_provider (css_provider, theme);

button.toggled.connect ((b) => {
Expand Down Expand Up @@ -201,7 +201,7 @@ public sealed class Terminal.SettingsPopover : Gtk.Popover {
var foreground = theme_palette[Themes.PALETTE_SIZE - 2].to_string ();

try {
css_provider.load_from_data (STYLE_CSS.printf (background, foreground));
css_provider.load_from_data (STYLE_CSS.printf (theme_to_css_class (theme), background, foreground));
} catch (Error e) {
critical ("Unable to style color button: %s", e.message);
}
Expand All @@ -211,4 +211,18 @@ public sealed class Terminal.SettingsPopover : Gtk.Popover {
label.set_string ("%.0f%%".printf (font_scale.get_double () * 100));
return true;
}

// Valid css selectors must not start with a number or contain delimiters besides `-`
private static string theme_to_css_class (string theme) {
var theme_palette = Themes.get_rgba_palette (theme);
var background = theme_palette[Themes.PALETTE_SIZE - 3];
var foreground = theme_palette[Themes.PALETTE_SIZE - 2];

var css_class = "theme-%.0lf%.0lf%.0lf%.0lf-%.0lf%.0lf%.0lf%.0lf".printf (
danirabbit marked this conversation as resolved.
Show resolved Hide resolved
background.red * 128, background.green * 128, background.blue * 128, background.alpha * 128,
foreground.red * 128, foreground.green * 128, foreground.blue * 128, foreground.alpha * 128
);

return css_class;
}
}