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

Port 'Menu' demo to Vala #172

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/Menu/main.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#! /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 HashTable<string, Variant> (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 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<string, Variant> state) {
string attr_string = "";
GenericArray<string> attrs = new GenericArray<string> ();

Variant? bold_variant = state.lookup ("bold");
if (bold_variant != null && bold_variant.get_boolean ()) {
attrs.add (@"0 -1 weight bold");
}

Variant? italic_variant = state.lookup ("italic");
if (italic_variant != null && italic_variant.get_boolean ()) {
attrs.add (@"0 -1 style italic");
}

string color = state.lookup ("foreground").get_string ();
if (color != null) {
attrs.add (@"0 -1 foreground $color");
}

foreach (string arr_attrb in attrs) {
attr_string += arr_attrb + ", ";
}
return Pango.AttrList.from_string (attr_string);
}
Loading