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

HIG-compliant for GTK menus #1931

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
122 changes: 90 additions & 32 deletions gtk/src/toga_gtk/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,23 +76,6 @@ def create(self):
self.actions = None

def gtk_startup(self, data=None):
# Set up the default commands for the interface.
self.interface.commands.add(
Command(
lambda _: self.interface.about(),
"About " + self.interface.name,
group=toga.Group.HELP,
),
Command(None, "Preferences", group=toga.Group.APP),
# Quit should always be the last item, in a section on its own
Command(
lambda _: self.interface.exit(),
"Quit " + self.interface.name,
shortcut=toga.Key.MOD_1 + "q",
group=toga.Group.APP,
section=sys.maxsize,
),
)
self._create_app_commands()

self.interface._startup()
Expand All @@ -119,8 +102,37 @@ def gtk_startup(self, data=None):
)

def _create_app_commands(self):
# No extra menus
pass
# Set up the default commands for the interface.
self.interface.commands.add(
Command(
None,
"Preferences",
group=toga.Group.APP,
section=sys.maxsize,
order=0,
),
Command(
None,
"Keyboard Shortcuts",
group=toga.Group.APP,
section=sys.maxsize,
order=1,
),
Command(
None,
"Help",
group=toga.Group.APP,
section=sys.maxsize,
order=2,
),
Command(
lambda _: self.interface.about(),
"About " + self.interface.name,
group=toga.Group.APP,
section=sys.maxsize,
order=3,
),
)

def gtk_activate(self, data=None):
pass
Expand All @@ -130,20 +142,31 @@ def create_menus(self):
self._menu_items = {}
self._menu_groups = {}

# Create the menu for the top level menubar.
menubar = Gio.Menu()
# Create the menus
popover = Gtk.PopoverMenu()
primary_menu_content = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, margin=10, spacing=10)
popover.add(primary_menu_content)
popover.set_position(Gtk.PositionType.BOTTOM)

menu_icon = Gtk.Image(icon_name="open-menu-symbolic", icon_size=Gtk.IconSize.MENU)
menu_button = Gtk.MenuButton(image=menu_icon, popover=popover)
menu_button.show_all()

app_header_bar = self.interface.main_window._impl.header_bar
app_header_bar.pack_end(menu_button)

section = None
for cmd in self.interface.commands:
if cmd == GROUP_BREAK:
section = None
elif cmd == SECTION_BREAK:
section = None
else:
submenu = self._submenu(cmd.group, menubar)
# submenu = self._submenu(cmd.group, primary_menu_box)

if section is None:
section = Gio.Menu()
submenu.append_section(None, section)
# if section is None:
# section = Gio.Menu()
# submenu.append_section(None, section)

cmd_id = "command-%s" % id(cmd)
action = Gio.SimpleAction.new(cmd_id, None)
Expand All @@ -155,16 +178,51 @@ def create_menus(self):
self._menu_items[action] = cmd
self.native.add_action(action)

item = Gio.MenuItem.new(cmd.text, "app." + cmd_id)
item = Gtk.ModelButton(action_name="app." + cmd_id, label=cmd.text)

if cmd.shortcut:
item.set_attribute_value(
"accel", GLib.Variant("s", gtk_accel(cmd.shortcut))
self.native.set_accels_for_action(
"app." + cmd_id, [gtk_accel(cmd.shortcut)]
)

section.append_item(item)

# Set the menu for the app.
self.native.set_menubar(menubar)
primary_menu_content.add(item)
primary_menu_content.show_all()

# # Create the menu for the top level menubar.
# menubar = Gio.Menu()
# section = None
# for cmd in self.interface.commands:
# if cmd == GROUP_BREAK:
# section = None
# elif cmd == SECTION_BREAK:
# section = None
# else:
# submenu = self._submenu(cmd.group, menubar)

# if section is None:
# section = Gio.Menu()
# submenu.append_section(None, section)

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

# cmd._impl.native.append(action)
# cmd._impl.set_enabled(cmd.enabled)
# self._menu_items[action] = cmd
# self.native.add_action(action)

# item = Gio.MenuItem.new(cmd.text, "app." + cmd_id)
# if cmd.shortcut:
# item.set_attribute_value(
# "accel", GLib.Variant("s", gtk_accel(cmd.shortcut))
# )

# section.append_item(item)

# # Set the menu for the app.
# self.native.set_menubar(menubar)

def _submenu(self, group, menubar):
try:
Expand Down
13 changes: 10 additions & 3 deletions gtk/src/toga_gtk/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ def __init__(self, interface, title, position, size):

self.native.set_default_size(size[0], size[1])

self.set_title(title)
self.set_position(position)

# Set the window deletable/closeable.
self.native.set_deletable(self.interface.closeable)

# Added to set Window Resizable - removes Window Maximize button from
# Window Decorator when resizable == False
self.native.set_resizable(self.interface.resizeable)

# Initial the HeaderBar
self.header_bar = Gtk.HeaderBar()
self.header_bar.props.show_close_button = True
self.header_bar.props.title = title
self.header_bar.show()
self.native.set_titlebar(self.header_bar)

self.set_title(title)
self.set_position(position)

self.toolbar_native = None
self.toolbar_items = None

Expand Down
Loading