| 1 |
/* PreviewColors.vala |
| 2 |
* |
| 3 |
* Copyright (C) 2022-2025 Jerry Casiano |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. |
| 17 |
* |
| 18 |
* If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 19 |
*/ |
| 20 |
|
| 21 |
namespace FontManager { |
| 22 |
|
| 23 |
[GtkTemplate (ui = "/com/github/FontManager/FontManager/ui/font-manager-preview-colors.ui")] |
| 24 |
public class PreviewColors : Gtk.Box { |
| 25 |
|
| 26 |
public signal void style_updated (); |
| 27 |
|
| 28 |
public Gdk.RGBA foreground_color { get; set; } |
| 29 |
public Gdk.RGBA background_color { get; set; } |
| 30 |
public GLib.Settings settings { get; set; } |
| 31 |
|
| 32 |
[GtkChild] unowned Gtk.ColorDialogButton bg_color_button; |
| 33 |
[GtkChild] unowned Gtk.ColorDialogButton fg_color_button; |
| 34 |
|
| 35 |
Gtk.CssProvider css_provider; |
| 36 |
|
| 37 |
public void update_style () { |
| 38 |
css_provider.load_from_string(@".FontManagerFontPreviewArea {color: $foreground_color;background-color: $background_color;} .FontManagerFontPreviewArea text selection {color: $background_color;background-color: $foreground_color;}"); |
| 39 |
style_updated(); |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
void flatten_color_button (Gtk.ColorDialogButton button) { |
| 44 |
button.get_first_child().remove_css_class("color"); |
| 45 |
button.get_first_child().add_css_class("flat"); |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
void update_default_colors_if_needed () { |
| 50 |
if (settings == null) |
| 51 |
return; |
| 52 |
bool prefer_dark = settings.get_boolean("prefer-dark-theme"); |
| 53 |
var default_light_bg = Gdk.RGBA(); |
| 54 |
var default_light_fg = Gdk.RGBA(); |
| 55 |
var default_dark_bg = Gdk.RGBA(); |
| 56 |
var default_dark_fg = Gdk.RGBA(); |
| 57 |
default_light_bg.parse("white"); |
| 58 |
default_light_fg.parse("black"); |
| 59 |
default_dark_bg.parse("black"); |
| 60 |
default_dark_fg.parse("white"); |
| 61 |
if (prefer_dark) { |
| 62 |
if (foreground_color.equal(default_light_fg) && background_color.equal(default_light_bg)) { |
| 63 |
foreground_color = default_dark_fg; |
| 64 |
background_color = default_dark_bg; |
| 65 |
} |
| 66 |
} else { |
| 67 |
if (foreground_color.equal(default_dark_fg) && background_color.equal(default_dark_bg)) { |
| 68 |
foreground_color = default_light_fg; |
| 69 |
background_color = default_light_bg; |
| 70 |
} |
| 71 |
} |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
public void restore_state (GLib.Settings settings) { |
| 76 |
this.settings = settings; |
| 77 |
if (settings == null) |
| 78 |
return; |
| 79 |
SettingsBindFlags flags = SettingsBindFlags.DEFAULT; |
| 80 |
settings.bind_with_mapping("preview-foreground-color", |
| 81 |
fg_color_button, |
| 82 |
"rgba", |
| 83 |
flags, |
| 84 |
(GLib.SettingsBindGetMappingShared) PreviewColors.from_setting, |
| 85 |
(GLib.SettingsBindSetMappingShared) PreviewColors.to_setting, |
| 86 |
null, null); |
| 87 |
settings.bind_with_mapping("preview-background-color", |
| 88 |
bg_color_button, |
| 89 |
"rgba", |
| 90 |
flags, |
| 91 |
(GLib.SettingsBindGetMappingShared) PreviewColors.from_setting, |
| 92 |
(GLib.SettingsBindSetMappingShared) PreviewColors.to_setting, |
| 93 |
null, null); |
| 94 |
settings.changed.connect((key) => { |
| 95 |
if (key.contains("color")) |
| 96 |
update_style(); |
| 97 |
if (key.contains("dark")) { |
| 98 |
update_default_colors_if_needed(); |
| 99 |
update_style(); |
| 100 |
} |
| 101 |
}); |
| 102 |
update_default_colors_if_needed(); |
| 103 |
update_style(); |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
public override void constructed () { |
| 108 |
flatten_color_button(bg_color_button); |
| 109 |
flatten_color_button(fg_color_button); |
| 110 |
css_provider = new Gtk.CssProvider(); |
| 111 |
Gdk.Display display = Gdk.Display.get_default(); |
| 112 |
Gtk.StyleContext.add_provider_for_display(display, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER); |
| 113 |
BindingFlags flags = BindingFlags.BIDIRECTIONAL | BindingFlags.SYNC_CREATE; |
| 114 |
bg_color_button.bind_property("rgba", this, "background-color", flags); |
| 115 |
fg_color_button.bind_property("rgba", this, "foreground-color", flags); |
| 116 |
map.connect(() => { |
| 117 |
update_default_colors_if_needed(); |
| 118 |
update_style(); |
| 119 |
}); |
| 120 |
base.constructed(); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
// GSettingsBind*Mapping functions |
| 125 |
|
| 126 |
public static Variant to_setting (Value val, VariantType type) { |
| 127 |
return new Variant.string(((Gdk.RGBA) val).to_string()); |
| 128 |
} |
| 129 |
|
| 130 |
public static bool from_setting (Value val, Variant variant) { |
| 131 |
Gdk.RGBA rgba = Gdk.RGBA(); |
| 132 |
string rgba_string = variant.get_string(); |
| 133 |
bool result = rgba.parse(rgba_string); |
| 134 |
val.set_boxed(&rgba); |
| 135 |
return result; |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
} |