emmental/emmental/window.py

73 lines
2.7 KiB
Python

# Copyright 2022 (c) Anna Schumaker.
"""A custom Adw.Window configured for our application."""
from gi.repository import GObject
from gi.repository import Gtk
from gi.repository import Adw
def _make_pane(orientation: Gtk.Orientation,
start_child: Gtk.Widget = None,
end_child: Gtk.Widget = None) -> Gtk.Paned:
pane = Gtk.Paned(orientation=orientation, hexpand=True, vexpand=True,
shrink_start_child=False, resize_start_child=False,
start_child=start_child, end_child=end_child)
pane.add_css_class("emmental-pane")
return pane
class Window(Adw.Window):
"""Our custom Adw.Window.
Our Window is configured with 4 regions for widgets:
* An area to place a CSD header bar
* An area for a sidebar
* An area to show now-playing information
* An area to display a tracklist
"""
header = GObject.Property(type=Gtk.Widget)
sidebar = GObject.Property(type=Gtk.Widget)
now_playing = GObject.Property(type=Gtk.Widget)
tracklist = GObject.Property(type=Gtk.Widget)
def __init__(self, version: str, **kwargs):
"""Initialize our Window."""
super().__init__(icon_name="emmental", title=version,
default_width=1600, default_height=900, **kwargs)
self._box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
self._header = Adw.Bin(child=self.header)
self._inner_pane = _make_pane(Gtk.Orientation.VERTICAL,
start_child=self.now_playing,
end_child=self.tracklist)
self._outer_pane = _make_pane(Gtk.Orientation.HORIZONTAL,
start_child=self.sidebar,
end_child=self._inner_pane)
self._toast = Adw.ToastOverlay(child=self._outer_pane)
self._outer_pane.add_css_class("emmental-padding")
if __debug__:
self.add_css_class("devel")
self.bind_property("header", self._header, "child")
self.bind_property("sidebar", self._outer_pane, "start-child")
self.bind_property("now-playing", self._inner_pane, "start-child")
self.bind_property("tracklist", self._inner_pane, "end-child")
self._box.append(self._header)
self._box.append(self._toast)
self.set_content(self._box)
def close(self, *args) -> None:
"""Close the window."""
super().close()
def post_toast(self, title: str) -> Adw.Toast:
"""Create a new Adw.Toas and add it to the window."""
toast = Adw.Toast.new(title)
self._toast.add_toast(toast)
return toast
def present(self, *args) -> None:
"""Present the window."""
super().present()