Skip to content

Commit

Permalink
Set accent color based on wallpaper (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
meisenzahl committed May 4, 2021
1 parent 521e9a5 commit 83e7dbf
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/PantheonAccountsServicePlugin.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[DBus (name = "io.elementary.pantheon.AccountsService")]
private interface PantheonShell.Pantheon.AccountsService : Object {
public abstract int prefers_accent_color { get; set; }
public abstract int prefers_color_scheme { get; set; }
}

Expand Down
113 changes: 83 additions & 30 deletions src/Views/Appearance.vala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,47 @@ public class PantheonShell.Appearance : Gtk.Grid {

private Granite.Widgets.ModeButton text_size_modebutton;

private enum AccentColor {
NO_PREFERENCE,
RED,
ORANGE,
YELLOW,
GREEN,
MINT,
BLUE,
PURPLE,
PINK,
BROWN,
GRAY;

public string to_string () {
switch (this) {
case RED:
return "strawberry";
case ORANGE:
return "orange";
case YELLOW:
return "banana";
case GREEN:
return "lime";
case MINT:
return "mint";
case BLUE:
return "blueberry";
case PURPLE:
return "grape";
case PINK:
return "bubblegum";
case BROWN:
return "cocoa";
case GRAY:
return "slate";
}

return "auto";
}
}

construct {
column_spacing = 12;
halign = Gtk.Align.CENTER;
Expand Down Expand Up @@ -248,13 +289,15 @@ public class PantheonShell.Appearance : Gtk.Grid {

((GLib.DBusProxy) pantheon_act).g_properties_changed.connect ((changed, invalid) => {
var color_scheme = changed.lookup_value ("PrefersColorScheme", new VariantType ("i"));
switch ((Granite.Settings.ColorScheme) color_scheme.get_int32 ()) {
case Granite.Settings.ColorScheme.DARK:
prefer_dark_radio.active = true;
break;
default:
prefer_default_radio.active = true;
break;
if (color_scheme != null) {
switch ((Granite.Settings.ColorScheme) color_scheme.get_int32 ()) {
case Granite.Settings.ColorScheme.DARK:
prefer_dark_radio.active = true;
break;
default:
prefer_default_radio.active = true;
break;
}
}
});

Expand Down Expand Up @@ -311,36 +354,39 @@ public class PantheonShell.Appearance : Gtk.Grid {
var accent_label = new Gtk.Label (_("Accent:"));
accent_label.halign = Gtk.Align.END;

var blueberry_button = new ColorButton ("blueberry");
var blueberry_button = new PrefersAccentColorButton (pantheon_act, AccentColor.BLUE);
blueberry_button.tooltip_text = _("Blueberry");

var mint_button = new ColorButton ("mint", blueberry_button);
var mint_button = new PrefersAccentColorButton (pantheon_act, AccentColor.MINT, blueberry_button);
mint_button.tooltip_text = _("Mint");

var lime_button = new ColorButton ("lime", blueberry_button);
var lime_button = new PrefersAccentColorButton (pantheon_act, AccentColor.GREEN, blueberry_button);
lime_button.tooltip_text = _("Lime");

var banana_button = new ColorButton ("banana", blueberry_button);
var banana_button = new PrefersAccentColorButton (pantheon_act, AccentColor.YELLOW, blueberry_button);
banana_button.tooltip_text = _("Banana");

var orange_button = new ColorButton ("orange", blueberry_button);
var orange_button = new PrefersAccentColorButton (pantheon_act, AccentColor.ORANGE, blueberry_button);
orange_button.tooltip_text = _("Orange");

var strawberry_button = new ColorButton ("strawberry", blueberry_button);
var strawberry_button = new PrefersAccentColorButton (pantheon_act, AccentColor.RED, blueberry_button);
strawberry_button.tooltip_text = _("Strawberry");

var bubblegum_button = new ColorButton ("bubblegum", blueberry_button);
var bubblegum_button = new PrefersAccentColorButton (pantheon_act, AccentColor.PINK, blueberry_button);
bubblegum_button.tooltip_text = _("Bubblegum");

var grape_button = new ColorButton ("grape", blueberry_button);
var grape_button = new PrefersAccentColorButton (pantheon_act, AccentColor.PURPLE, blueberry_button);
grape_button.tooltip_text = _("Grape");

var cocoa_button = new ColorButton ("cocoa", blueberry_button);
var cocoa_button = new PrefersAccentColorButton (pantheon_act, AccentColor.BROWN, blueberry_button);
cocoa_button.tooltip_text = _("Cocoa");

var slate_button = new ColorButton ("slate", blueberry_button);
var slate_button = new PrefersAccentColorButton (pantheon_act, AccentColor.GRAY, blueberry_button);
slate_button.tooltip_text = _("Slate");

var auto_button = new PrefersAccentColorButton (pantheon_act, AccentColor.NO_PREFERENCE, blueberry_button);
auto_button.tooltip_text = _("Automatic based on wallpaper");

var accent_grid = new Gtk.Grid ();
accent_grid.column_spacing = 6;
accent_grid.add (blueberry_button);
Expand All @@ -353,6 +399,7 @@ public class PantheonShell.Appearance : Gtk.Grid {
accent_grid.add (grape_button);
accent_grid.add (cocoa_button);
accent_grid.add (slate_button);
accent_grid.add (auto_button);

var accent_info = new Gtk.Label (_("Used across the system by default. Apps can always use their own accent color.")) {
margin_bottom = 18,
Expand Down Expand Up @@ -382,15 +429,16 @@ public class PantheonShell.Appearance : Gtk.Grid {
});
}

private class ColorButton : Gtk.RadioButton {
public string color_name { get; construct; }
private class PrefersAccentColorButton : Gtk.RadioButton {
public AccentColor color { get; construct; }
public Pantheon.AccountsService? pantheon_act { get; construct; default = null; }

private static GLib.Settings interface_settings;
private static string current_accent;

public ColorButton (string _color_name, Gtk.RadioButton? group_member = null) {
public PrefersAccentColorButton (Pantheon.AccountsService? pantheon_act, AccentColor color, Gtk.RadioButton? group_member = null) {
Object (
color_name: _color_name,
pantheon_act: pantheon_act,
color: color,
group: group_member
);
}
Expand All @@ -399,22 +447,27 @@ public class PantheonShell.Appearance : Gtk.Grid {
interface_settings = new GLib.Settings (INTERFACE_SCHEMA);

var current_stylesheet = interface_settings.get_string (STYLESHEET_KEY);
current_accent = current_stylesheet.replace (STYLESHEET_PREFIX, "");
}

construct {
unowned Gtk.StyleContext context = get_style_context ();
context.add_class ("color-button");
context.add_class (color_name);
context.add_class (Granite.STYLE_CLASS_COLOR_BUTTON);
context.add_class (color.to_string ());

realize.connect (() => {
active = current_accent == color_name;
active = color == pantheon_act.prefers_accent_color;

toggled.connect (() => {
interface_settings.set_string (
STYLESHEET_KEY,
STYLESHEET_PREFIX + color_name
);
if (color != AccentColor.NO_PREFERENCE) {
interface_settings.set_string (
STYLESHEET_KEY,
STYLESHEET_PREFIX + color.to_string ()
);
}

if (((GLib.DBusProxy) pantheon_act).get_cached_property ("PrefersAccentColor") != null) {
pantheon_act.prefers_accent_color = color;
}
});
});
}
Expand Down

0 comments on commit 83e7dbf

Please sign in to comment.