Rework the header bar for the primary window
Previously the menu was hand-composed, let's replace it with the glade files as possible. And move the menu itself out into the source file so glade doesn't keep overwriting it.
This commit is contained in:
parent
d07835b15c
commit
035dd68115
|
@ -2,40 +2,34 @@
|
|||
<!-- Generated with glade 3.22.1 -->
|
||||
<interface>
|
||||
<requires lib="gtk+" version="3.20"/>
|
||||
<menu id="primary_menu">
|
||||
<item>
|
||||
<attribute name="action">app.about</attribute>
|
||||
<attribute name="label" translatable="yes">_About TuhiGUI</attribute>
|
||||
</item>
|
||||
</menu>
|
||||
<template class="MainWindow" parent="GtkApplicationWindow">
|
||||
<property name="can_focus">False</property>
|
||||
<property name="resizable">False</property>
|
||||
<property name="default_width">1000</property>
|
||||
<property name="default_height">700</property>
|
||||
<child type="titlebar">
|
||||
<object class="GtkStack" id="stack_titlebar">
|
||||
<object class="GtkHeaderBar" id="headerbar">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="hhomogeneous">False</property>
|
||||
<property name="vhomogeneous">False</property>
|
||||
<property name="show_close_button">True</property>
|
||||
<child type="title">
|
||||
<placeholder/>
|
||||
</child>
|
||||
<child>
|
||||
<object class="GtkHeaderBar" id="headerbar">
|
||||
<object class="GtkMenuButton" id="menubutton1">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<child>
|
||||
<object class="GtkButton" id="btn_quit">
|
||||
<property name="label" translatable="yes">Quit</property>
|
||||
<object class="GtkImage">
|
||||
<property name="visible">True</property>
|
||||
<property name="can_focus">True</property>
|
||||
<property name="receives_default">True</property>
|
||||
<signal name="clicked" handler="_on_quit_button_clicked" swapped="no"/>
|
||||
<property name="can_focus">False</property>
|
||||
<property name="icon_name">open-menu-symbolic</property>
|
||||
</object>
|
||||
</child>
|
||||
</object>
|
||||
<packing>
|
||||
<property name="name">page0</property>
|
||||
<property name="title" translatable="yes">page0</property>
|
||||
<property name="pack_type">end</property>
|
||||
</packing>
|
||||
</child>
|
||||
</object>
|
||||
|
|
|
@ -21,20 +21,36 @@ from .tuhi import TuhiKeteManager
|
|||
import gi
|
||||
gi.require_version("Gtk", "3.0")
|
||||
|
||||
MENU_XML = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<interface>
|
||||
<menu id="primary-menu">
|
||||
<item>
|
||||
<attribute name="label">About</attribute>
|
||||
<attribute name="action">app.about</attribute>
|
||||
</item>
|
||||
</menu>
|
||||
</interface>
|
||||
"""
|
||||
|
||||
|
||||
@Gtk.Template(resource_path='/org/freedesktop/TuhiGui/ui/MainWindow.ui')
|
||||
class MainWindow(Gtk.ApplicationWindow):
|
||||
__gtype_name__ = 'MainWindow'
|
||||
|
||||
stack_perspectives = Gtk.Template.Child()
|
||||
primary_menu = Gtk.Template.Child()
|
||||
headerbar = Gtk.Template.Child()
|
||||
menubutton1 = Gtk.Template.Child()
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
|
||||
self._tuhi = TuhiKeteManager()
|
||||
|
||||
builder = Gtk.Builder.new_from_string(MENU_XML, -1)
|
||||
menu = builder.get_object("primary-menu")
|
||||
self.menubutton1.set_menu_model(menu)
|
||||
|
||||
ep = ErrorPerspective()
|
||||
self._add_perspective(ep)
|
||||
self.stack_perspectives.set_visible_child_name(ep.name)
|
||||
|
@ -45,8 +61,6 @@ class MainWindow(Gtk.ApplicationWindow):
|
|||
else:
|
||||
self._on_dbus_online()
|
||||
|
||||
self._add_primary_menu()
|
||||
|
||||
def _on_dbus_online(self, *args, **kwargs):
|
||||
dp = DrawingPerspective()
|
||||
self._add_perspective(dp)
|
||||
|
@ -59,12 +73,15 @@ class MainWindow(Gtk.ApplicationWindow):
|
|||
else:
|
||||
dp.device = self._tuhi.devices[0]
|
||||
active = dp
|
||||
self.headerbar.set_title(f'Tuhi - {dp.device.name}')
|
||||
|
||||
self.stack_perspectives.set_visible_child_name(active.name)
|
||||
|
||||
def _on_new_device_registered(self, setupperspective, device):
|
||||
setupperspective.disconnect_by_func(self._on_new_device_registered)
|
||||
|
||||
self.headerbar.set_title(f'Tuhi - {device.name}')
|
||||
|
||||
dp = self._get_child('drawing_perspective')
|
||||
dp.device = device
|
||||
self.stack_perspectives.set_visible_child_name(dp.name)
|
||||
|
@ -75,23 +92,5 @@ class MainWindow(Gtk.ApplicationWindow):
|
|||
def _get_child(self, name):
|
||||
return self.stack_perspectives.get_child_by_name(name)
|
||||
|
||||
def _add_primary_menu(self):
|
||||
hamburger = Gtk.Image.new_from_icon_name("open-menu-symbolic",
|
||||
Gtk.IconSize.BUTTON)
|
||||
hamburger.set_visible(True)
|
||||
button_primary_menu = Gtk.MenuButton.new()
|
||||
button_primary_menu.add(hamburger)
|
||||
button_primary_menu.set_visible(True)
|
||||
button_primary_menu.set_menu_model(self.primary_menu)
|
||||
self.headerbar.pack_end(button_primary_menu)
|
||||
# Place the button last in the titlebar.
|
||||
self.headerbar.child_set_property(button_primary_menu, "position", 0)
|
||||
|
||||
def _on_reconnect_tuhi(self, tuhi):
|
||||
self._tuhi = tuhi
|
||||
|
||||
@Gtk.Template.Callback("_on_quit_button_clicked")
|
||||
def _on_quit_button_clicked(self, button):
|
||||
window = button.get_toplevel()
|
||||
if not window.emit("delete-event", Gdk.Event.new(Gdk.EventType.DELETE)):
|
||||
window.destroy()
|
||||
|
|
Loading…
Reference in New Issue