Skip to content

Commit

Permalink
Port AccelLabel to GTK4
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmhewitt committed Nov 27, 2021
1 parent 48dea41 commit 1d9d476
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
32 changes: 18 additions & 14 deletions lib/Widgets/AccelLabel.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019 elementary, Inc. (https://elementary.io)
* Copyright 2019-2021 elementary, Inc. (https://elementary.io)
* SPDX-License-Identifier: GPL-2.0-or-later
*/

Expand All @@ -19,7 +19,7 @@
* }}}
*
*/
public class Granite.AccelLabel : Gtk.Grid {
public class Granite.AccelLabel : Gtk.Box {
/**
* The name of the {@link GLib.Action} used to retrieve action accelerators
*/
Expand All @@ -35,6 +35,8 @@ public class Granite.AccelLabel : Gtk.Grid {
*/
public string label { get; construct set; }

private Gtk.Label label_widget;

/**
* Creates a new AccelLabel from a label and an accelerator string
*
Expand Down Expand Up @@ -66,26 +68,29 @@ public class Granite.AccelLabel : Gtk.Grid {
}

construct {
var label = new Gtk.Label (label);
label.hexpand = true;
label.margin_end = 6;
label.xalign = 0;
label_widget = new Gtk.Label (label) {
hexpand = true,
margin_end = 6,
xalign = 0
};

column_spacing = 3;
add (label);
spacing = 3;
append (label_widget);

update_accels ();

notify["accel-string"].connect (update_accels);
notify["action-name"].connect (update_accels);

bind_property ("label", label, "label");
bind_property ("label", label_widget, "label");
}

private void update_accels () {
GLib.List<unowned Gtk.Widget> list = get_children ();
for (int i = 0; i < list.length () - 1; i++) {
list.nth_data (i).destroy ();
weak Gtk.Widget child = label_widget.get_next_sibling ();
while (child != null) {
weak Gtk.Widget next_child = child.get_next_sibling ();
remove (child);
child = next_child;
}

string[] accels = {""};
Expand All @@ -105,9 +110,8 @@ public class Granite.AccelLabel : Gtk.Grid {
var accel_label_context = accel_label.get_style_context ();
accel_label_context.add_class (Granite.STYLE_CLASS_KEYCAP);

add (accel_label);
append (accel_label);
}
}
show_all ();
}
}
10 changes: 6 additions & 4 deletions lib/Widgets/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static string accel_to_string (string? accel) {
arr += _("Ctrl");
}

if (Gdk.ModifierType.MOD1_MASK in accel_mods) {
if (Gdk.ModifierType.ALT_MASK in accel_mods) {
arr += _("Alt");
}

Expand Down Expand Up @@ -267,8 +267,8 @@ private static double sanitize_color (double color) {
* @return a contrasting {@link Gdk.RGBA} foreground color, i.e. white ({ 1.0, 1.0, 1.0, 1.0}) or black ({ 0.0, 0.0, 0.0, 1.0}).
*/
public static Gdk.RGBA contrasting_foreground_color (Gdk.RGBA bg_color) {
Gdk.RGBA gdk_white = { 1.0, 1.0, 1.0, 1.0 };
Gdk.RGBA gdk_black = { 0.0, 0.0, 0.0, 1.0 };
Gdk.RGBA gdk_white = { 1.0f, 1.0f, 1.0f, 1.0f };
Gdk.RGBA gdk_black = { 0.0f, 0.0f, 0.0f, 1.0f };

var contrast_with_white = contrast_ratio (
bg_color,
Expand Down Expand Up @@ -319,6 +319,8 @@ namespace Granite.Widgets.Utils {
assert (window != null);

string hex = color.to_string ();
return set_theming_for_screen (window.get_screen (), @"@define-color color_primary $hex;@define-color colorPrimary $hex;", priority);
// TODO: Fix this
return null;
//return set_theming_for_screen (window.get_screen (), @"@define-color color_primary $hex;@define-color colorPrimary $hex;", priority);
}
}

0 comments on commit 1d9d476

Please sign in to comment.