emmental/emmental/window.py

94 lines
3.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
from .action import ActionEntry
from . import layout
def _make_pane(orientation: Gtk.Orientation, position: int = 0,
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,
position=position, margin_start=8)
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)
show_sidebar = GObject.Property(type=bool, default=False)
now_playing = GObject.Property(type=Gtk.Widget)
now_playing_size = GObject.Property(type=int, default=250)
tracklist = GObject.Property(type=Gtk.Widget)
user_editing = GObject.Property(type=bool, default=False)
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,
position=self.now_playing_size,
start_child=self.now_playing,
end_child=self.tracklist)
self._layout = layout.Layout(content=self._inner_pane,
sidebar=self.sidebar)
self._toast = Adw.ToastOverlay(child=self._layout)
self._layout.add_css_class("emmental-padding")
if __debug__:
self.add_css_class("devel")
self.bind_property("header", self._header, "child")
self.bind_property("sidebar", self._layout, "sidebar")
self.bind_property("show-sidebar", self._layout, "show-sidebar",
GObject.BindingFlags.BIDIRECTIONAL)
self.bind_property("now-playing", self._inner_pane, "start-child")
self.bind_property("now-playing-size", self._inner_pane, "position",
GObject.BindingFlags.BIDIRECTIONAL)
self.bind_property("tracklist", self._inner_pane, "end-child")
self.connect("notify::focus-widget", self.__notify_focus_widget)
self._box.append(self._header)
self._box.append(self._toast)
self.set_content(self._box)
def __notify_focus_widget(self, win: Gtk.Window, param) -> None:
self.user_editing = isinstance(win.get_property("focus-widget"),
Gtk.Editable)
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()
@property
def accelerators(self) -> list[ActionEntry]:
"""Get a list of accelerators for the Window."""
return [ActionEntry("reset-focus", self.set_focus, "Escape")]