Skip to content

Commit

Permalink
Use stateful actions
Browse files Browse the repository at this point in the history
  • Loading branch information
leolost2605 committed Nov 9, 2023
1 parent dbbc6ab commit ec5c575
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 24 deletions.
28 changes: 4 additions & 24 deletions src/Launcher.vala
Original file line number Diff line number Diff line change
Expand Up @@ -38,40 +38,23 @@ public class Dock.Launcher : Gtk.Button {
);
}

var pinned_menu_item = new MenuItem (null, null);
pinned_menu_item.set_attribute_value ("custom", "pinned-item");

var pinned_section = new Menu ();
pinned_section.append_item (pinned_menu_item);
pinned_section.append (
_("Keep in Dock"),
MainWindow.ACTION_PREFIX + MainWindow.LAUNCHER_PINNED_ACTION_TEMPLATE.printf (app_info.get_id ())
);

var model = new Menu ();
if (action_section.get_n_items () > 0) {
model.append_section (null, action_section);
}
model.append_section (null, pinned_section);

var pinned_label = new Gtk.Label ("Keep in Dock") {
xalign = 0,
hexpand = true
};

var pinned_check_button = new Gtk.CheckButton ();

var pinned_box = new Gtk.Box (HORIZONTAL, 3);
pinned_box.append (pinned_label);
pinned_box.append (pinned_check_button);

var pinned_button = new Gtk.ToggleButton () {
child = pinned_box
};
pinned_button.add_css_class (Granite.STYLE_CLASS_MENUITEM);

popover = new Gtk.PopoverMenu.from_model (model) {
autohide = true,
position = TOP
};
popover.set_parent (this);
popover.add_child (pinned_button, "pinned-item");

var image = new Gtk.Image () {
gicon = app_info.get_icon ()
Expand All @@ -81,9 +64,6 @@ public class Dock.Launcher : Gtk.Button {
child = image;
tooltip_text = app_info.get_display_name ();

bind_property ("pinned", pinned_button, "active", BIDIRECTIONAL | SYNC_CREATE);
pinned_button.bind_property ("active", pinned_check_button, "active", SYNC_CREATE);

notify["pinned"].connect (() => ((MainWindow) get_root ()).sync_pinned ());

var gesture_click = new Gtk.GestureClick () {
Expand Down
12 changes: 12 additions & 0 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class Dock.MainWindow : Gtk.ApplicationWindow {
// First %s is the app id second %s the action name
public const string LAUNCHER_ACTION_TEMPLATE = "%s.%s";
public const string LAUNCHER_PINNED_ACTION_TEMPLATE = "%s-pinned";
public const string ACTION_GROUP_PREFIX = "win";
public const string ACTION_PREFIX = ACTION_GROUP_PREFIX + ".";

Expand Down Expand Up @@ -72,11 +73,22 @@ public class Dock.MainWindow : Gtk.ApplicationWindow {
unowned var app_id = app_info.get_id ();
app_to_launcher.insert (app_id, launcher);
box.append (launcher);

var pinned_action = new SimpleAction.stateful (
LAUNCHER_PINNED_ACTION_TEMPLATE.printf (app_id),
null,
new Variant.boolean (launcher.pinned)
);
launcher.notify["pinned"].connect (() => pinned_action.set_state (launcher.pinned));
pinned_action.change_state.connect ((new_state) => launcher.pinned = (bool) new_state);
add_action (pinned_action);

foreach (var action in app_info.list_actions ()) {
var simple_action = new SimpleAction (LAUNCHER_ACTION_TEMPLATE.printf (app_id, action), null);
simple_action.activate.connect (() => launcher.launch (action));
add_action (simple_action);
}

return app_to_launcher[app_id];
}

Expand Down

0 comments on commit ec5c575

Please sign in to comment.