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

Init: set accent color from portal #704

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
17 changes: 17 additions & 0 deletions lib/Init.vala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Granite {
private static bool initialized = false;
private static Gtk.CssProvider? base_provider = null;
private static Gtk.CssProvider? dark_provider = null;
private static Gtk.CssProvider? accent_provider = null;
private static Gtk.CssProvider? app_provider = null;

/**
Expand Down Expand Up @@ -41,12 +42,28 @@ namespace Granite {
set_provider_for_display (display, gtk_settings.gtk_application_prefer_dark_theme);
});

var granite_settings = Granite.Settings.get_default ();
granite_settings.notify["accent-color"].connect (() => {
set_accent_for_display (display, granite_settings.accent_color);
});

set_provider_for_display (display, gtk_settings.gtk_application_prefer_dark_theme);
set_accent_for_display (display, granite_settings.accent_color);

var icon_theme = Gtk.IconTheme.get_for_display (display);
icon_theme.add_resource_path ("/io/elementary/granite");
}

private static void set_accent_for_display (Gdk.Display display, string accent_color) {
if (accent_provider == null) {
accent_provider = new Gtk.CssProvider ();
}

Gtk.StyleContext.remove_provider_for_display (display, accent_provider);
accent_provider.load_from_string ("@define-color accent_color %s;".printf (accent_color));
Gtk.StyleContext.add_provider_for_display (display, accent_provider, Gtk.STYLE_PROVIDER_PRIORITY_THEME);
}

private static void set_provider_for_display (Gdk.Display display, bool prefer_dark_style) {
if (app_provider == null) {
var base_path = Application.get_default ().resource_base_path;
Expand Down
1 change: 1 addition & 0 deletions lib/Styles/Index-dark.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ $warning-color: $BANANA_100;
$warning-icon-color: $BANANA_500;

// Common styles
@import '_exported.scss';
@import '_label.scss';
@import '_osd.scss';

Expand Down
1 change: 1 addition & 0 deletions lib/Styles/Index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ $warning_color: $BANANA_900;
$warning-icon-color: mix($BANANA_500, $BANANA_700, $weight: 50%);

// Common styles
@import '_exported.scss';
@import '_label.scss';
@import '_osd.scss';

Expand Down
8 changes: 8 additions & 0 deletions lib/Styles/_exported.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// These are defined as GtkCSS variables, so are accessible and overridable
// inside of apps. While internal sass variables are considered private API,
// these exported GtkCSS variables should be considered public API.

@define-color base_color #{"" + bg_color(1)};
@define-color bg_color #{"" + bg_color(2)};
@define-color fg_color #{"" + $fg-color};
@define-color highlight_color #{"" + $highlight_color};
8 changes: 8 additions & 0 deletions lib/Styles/_label.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
.accent {
color: #{'mix(@accent_color, @fg_color, 0.27)'};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will produce very good results for e.g. yellow. In libadwaita I had much better results with oklch colorspace (tho that would require you to update gtk once 4.16 is out)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, that or calculate it programmatically ofc, then you don't need to wait.


&:backdrop {
color: inherit;
}
}

.title-1 {
font-size: 24pt;
font-weight: 700;
Expand Down
51 changes: 51 additions & 0 deletions lib/Widgets/Settings.vala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,25 @@ namespace Granite {
LIGHT
}

private string? _accent_color = null;

/**
* The theme accent color chosen by the user
* @since 7.5.0
*/
[Version (since = "7.5.0")]
public string accent_color {
danirabbit marked this conversation as resolved.
Show resolved Hide resolved
get {
if (_accent_color == null) {
setup_accent_color ();
}
return (_accent_color);
}
private set {
_accent_color = value;
}
}

private ColorScheme? _prefers_color_scheme = null;

/**
Expand Down Expand Up @@ -96,6 +115,38 @@ namespace Granite {
}
}

private void setup_accent_color () {
try {
portal = Portal.Settings.get ();

var variant = portal.read (
"org.freedesktop.appearance",
"accent-color"
).get_variant ();

accent_color = parse_color (variant);

portal.setting_changed.connect ((scheme, key, value) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alice-mkh maybe it's because I'm on an older version of some library but I'm not getting anything at all here when I change the color in system settings so I can't really test further because it's broken for me all the way at the beginning

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh okay. It did work for me, but only until I open inspector due to the global provider issue I mentioned.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I can't even test that 😅

if (scheme == "org.freedesktop.appearance" && key == "accent-color") {
accent_color = parse_color (value);
}
});
} catch (Error e) {
warning (e.message);

// Set a default in case we can't get from system
accent_color = "#3689e6";
}
}

private string parse_color (GLib.Variant color) {
double red, green, blue;
color.get ("(ddd)", out red, out green, out blue);

Gdk.RGBA rgba = {(float) red, (float) green, (float) blue, 1};
return rgba.to_string ();
}

private void setup_prefers_color_scheme () {
try {
portal = Portal.Settings.get ();
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ libgranite_deps = [
gio_os_dep,
dependency('glib-2.0', version: '>=' + glib_min_version),
dependency('gobject-2.0', version: '>=' + glib_min_version),
dependency('gtk4', version: '>=4.4'),
dependency('gtk4', version: '>=4.12'),
]

icons_dir = join_paths(
Expand Down
Loading