Skip to content

Commit

Permalink
Use a consistent pattern on backends that use the same menu activatio…
Browse files Browse the repository at this point in the history
…n approach.
  • Loading branch information
freakboy3742 committed Nov 7, 2023
1 parent f8fa58c commit cf9eee3
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 25 deletions.
11 changes: 1 addition & 10 deletions gtk/src/toga_gtk/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@
from .window import Window


def gtk_menu_item_activate(cmd):
"""Convert a GTK menu item activation into a command invocation."""

def _handler(action, data):
cmd.action()

return _handler


class MainWindow(Window):
def create(self):
self.native = Gtk.ApplicationWindow()
Expand Down Expand Up @@ -151,7 +142,7 @@ def create_menus(self):

cmd_id = "command-%s" % id(cmd)
action = Gio.SimpleAction.new(cmd_id, None)
action.connect("activate", gtk_menu_item_activate(cmd))
action.connect("activate", cmd._impl.gtk_activate)

cmd._impl.native.append(action)
cmd._impl.set_enabled(cmd.enabled)
Expand Down
6 changes: 6 additions & 0 deletions gtk/src/toga_gtk/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ def __init__(self, interface):
self.interface = interface
self.native = []

def gtk_activate(self, action, data):
self.interface.action()

def gtk_clicked(self, action):
self.interface.action()

def set_enabled(self, value):
enabled = self.interface.enabled
for widget in self.native:
Expand Down
11 changes: 1 addition & 10 deletions gtk/src/toga_gtk/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@
from .libs import Gdk, Gtk


def gtk_toolbar_item_clicked(cmd):
"""Convert a GTK toolbar item click into a command invocation."""

def _handler(widget):
cmd.action()

return _handler


class Window:
def __init__(self, interface, title, position, size):
self.interface = interface
Expand Down Expand Up @@ -100,7 +91,7 @@ def create_toolbar(self):
item_impl.set_label(cmd.text)
if cmd.tooltip:
item_impl.set_tooltip_text(cmd.tooltip)
item_impl.connect("clicked", gtk_toolbar_item_clicked(cmd))
item_impl.connect("clicked", cmd._impl.gtk_clicked)
cmd._impl.native.append(item_impl)
self.toolbar_items[cmd] = item_impl
self.native_toolbar.insert(item_impl, -1)
Expand Down
2 changes: 1 addition & 1 deletion winforms/src/toga_winforms/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def create_menus(self):
else:
submenu = self._submenu(cmd.group, menubar)
item = WinForms.ToolStripMenuItem(cmd.text)
item.Click += WeakrefCallable(cmd._impl.winforms_handler)
item.Click += WeakrefCallable(cmd._impl.winforms_Click)
if cmd.shortcut is not None:
try:
item.ShortcutKeys = toga_to_winforms_key(cmd.shortcut)
Expand Down
6 changes: 3 additions & 3 deletions winforms/src/toga_winforms/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ def __init__(self, interface):
self.interface = interface
self.native = []

def winforms_Click(self, sender, event):
return self.interface.action()

def set_enabled(self, value):
if self.native:
for widget in self.native:
widget.Enabled = self.interface.enabled

def winforms_handler(self, sender, event):
return self.interface.action()
2 changes: 1 addition & 1 deletion winforms/src/toga_winforms/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def create_toolbar(self):
if cmd.icon is not None:
item.Image = cmd.icon._impl.native.ToBitmap()
item.Enabled = cmd.enabled
item.Click += WeakrefCallable(cmd._impl.winforms_handler)
item.Click += WeakrefCallable(cmd._impl.winforms_Click)
cmd._impl.native.append(item)
self.toolbar_native.Items.Add(item)

Expand Down

0 comments on commit cf9eee3

Please sign in to comment.