From 4426d07903773f6bb1852756b5a2ec17a477986b Mon Sep 17 00:00:00 2001 From: Bharat Date: Tue, 11 Jun 2024 13:18:49 +0530 Subject: [PATCH 1/2] demos: Port 'Menu' demo to Vala Add Vala code for Menu --- src/Menu/main.vala | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/Menu/main.vala diff --git a/src/Menu/main.vala b/src/Menu/main.vala new file mode 100644 index 00000000..f7a3083e --- /dev/null +++ b/src/Menu/main.vala @@ -0,0 +1,59 @@ +#! /usr/bin/env -S vala workbench.vala --pkg libadwaita-1 --pkg pango --pkg glib-2.0 --pkg gio-2.0 + +public void main () { + + var label = (Gtk.Label) workbench.builder.get_object ("label"); + + var text_group = new SimpleActionGroup (); + label.insert_action_group ("text", text_group); + + var text_state = new GLib.HashTable (str_hash, str_equal); + text_state.insert ("italic", false); + text_state.insert ("bold", false); + text_state.insert ("foreground", "green"); + + var italic_action = new SimpleAction.stateful ("italic", null, new Variant.boolean (false)); + italic_action.notify["state"].connect (() => { + text_state.replace ("italic", italic_action.state.get_boolean ()); + label.attributes = (state_to_attr (text_state)); + }); + text_group.add_action (italic_action); + + var bold_action = new SimpleAction.stateful ("bold", null, new Variant.boolean (false)); + bold_action.notify["state"].connect (() => { + text_state.replace ("bold", bold_action.state.get_boolean ()); + label.attributes = (state_to_attr (text_state)); + }); + text_group.add_action (bold_action); + + var color_action = new SimpleAction.stateful ("color", new GLib.VariantType ("s"), new Variant.string ("green")); + color_action.notify["state"].connect (() => { + text_state.replace ("foreground", color_action.state.get_string ()); + label.attributes = (state_to_attr (text_state)); + }); + text_group.add_action (color_action); +} + +// Helper function to create a PangoAttrList from text_state +private Pango.AttrList state_to_attr (HashTable state) { + string attr_string = ""; + var attrs = new Gtk.StringList (null); + + Variant? bold_variant = state.lookup ("bold"); + if (bold_variant.get_boolean ()) { + attrs.append (@"0 -1 weight bold"); + } + + Variant? italic_variant = state.lookup ("italic"); + if (italic_variant.get_boolean ()) { + attrs.append (@"0 -1 style italic"); + } + + string color = state.lookup ("foreground").get_string (); + attrs.append (@"0 -1 foreground $color"); + + for (uint i = 0; i < attrs.n_items; i++) { + attr_string += attrs.get_string (i) + ", "; + } + return Pango.AttrList.from_string (attr_string); +} From f3643b92f6515816902eec2e0d3b6f8bcada9dd2 Mon Sep 17 00:00:00 2001 From: Bharat Date: Thu, 13 Jun 2024 13:06:09 +0530 Subject: [PATCH 2/2] Improvements Remove unnecessary package declarations Omit use of "GLib" Add checks for null (can be returned by state.lookup) Replace Gtk.StringList with GenericArray Use foreach to append attr_string Changes as suggested by @Diego-Ivan --- src/Menu/main.vala | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Menu/main.vala b/src/Menu/main.vala index f7a3083e..3b1fe741 100644 --- a/src/Menu/main.vala +++ b/src/Menu/main.vala @@ -1,13 +1,12 @@ -#! /usr/bin/env -S vala workbench.vala --pkg libadwaita-1 --pkg pango --pkg glib-2.0 --pkg gio-2.0 +#! /usr/bin/env -S vala workbench.vala --pkg libadwaita-1 public void main () { - var label = (Gtk.Label) workbench.builder.get_object ("label"); var text_group = new SimpleActionGroup (); label.insert_action_group ("text", text_group); - var text_state = new GLib.HashTable (str_hash, str_equal); + var text_state = new HashTable (str_hash, str_equal); text_state.insert ("italic", false); text_state.insert ("bold", false); text_state.insert ("foreground", "green"); @@ -26,7 +25,7 @@ public void main () { }); text_group.add_action (bold_action); - var color_action = new SimpleAction.stateful ("color", new GLib.VariantType ("s"), new Variant.string ("green")); + var color_action = new SimpleAction.stateful ("color", new VariantType ("s"), new Variant.string ("green")); color_action.notify["state"].connect (() => { text_state.replace ("foreground", color_action.state.get_string ()); label.attributes = (state_to_attr (text_state)); @@ -37,23 +36,25 @@ public void main () { // Helper function to create a PangoAttrList from text_state private Pango.AttrList state_to_attr (HashTable state) { string attr_string = ""; - var attrs = new Gtk.StringList (null); + GenericArray attrs = new GenericArray (); Variant? bold_variant = state.lookup ("bold"); - if (bold_variant.get_boolean ()) { - attrs.append (@"0 -1 weight bold"); + if (bold_variant != null && bold_variant.get_boolean ()) { + attrs.add (@"0 -1 weight bold"); } Variant? italic_variant = state.lookup ("italic"); - if (italic_variant.get_boolean ()) { - attrs.append (@"0 -1 style italic"); + if (italic_variant != null && italic_variant.get_boolean ()) { + attrs.add (@"0 -1 style italic"); } string color = state.lookup ("foreground").get_string (); - attrs.append (@"0 -1 foreground $color"); + if (color != null) { + attrs.add (@"0 -1 foreground $color"); + } - for (uint i = 0; i < attrs.n_items; i++) { - attr_string += attrs.get_string (i) + ", "; + foreach (string arr_attrb in attrs) { + attr_string += arr_attrb + ", "; } return Pango.AttrList.from_string (attr_string); }