Skip to content

Commit

Permalink
Introduce ActionRow widget
Browse files Browse the repository at this point in the history
This is a GtkListBoxRow with the added functionality of having
a title, subtitle, child widgets, and a way to activate child widgets.
  • Loading branch information
TheEvilSkeleton committed Mar 8, 2025
1 parent 8e5d304 commit 5f8233d
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
74 changes: 74 additions & 0 deletions GTG/gtk/action_row.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -----------------------------------------------------------------------------
# Getting Things GNOME! - a personal organizer for the GNOME desktop
# Copyright (c) 2025 - The GTG Team
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------------

from gi.repository import GObject, Gtk
from typing import Any

from GTG.gtk.browser import GnomeConfig


@Gtk.Template(filename=GnomeConfig.ACTION_ROW)
class ActionRow(Gtk.ListBoxRow):

__gtype_name__ = 'ActionRow'

box = Gtk.Template.Child()
subtitle_label = Gtk.Template.Child()

title = GObject.Property(type=str)

handler_id = None

@GObject.Property(type=str, default="")
def subtitle(self) -> str:
return self._subtitle

@subtitle.setter
def subtitle(self, subtitle: str) -> None:
self._subtitle = subtitle

self.subtitle_label.set_visible(bool(subtitle))

@GObject.Property(type=Gtk.Widget, default=None)
def activatable_widget(self) -> Gtk.Widget | None:
return self._activatable_widget

@activatable_widget.setter
def activatable_widget(self, activatable_widget: Gtk.Widget | None) -> None:
self._activatable_widget = activatable_widget

self.set_activatable(bool(activatable_widget))

parent = self.get_parent()
if not isinstance(parent, Gtk.ListBox):
raise TypeError(f"Parent must be a {Gtk.ListBox}.")

if activatable_widget:
self.handler_id = parent.connect("row-activated", lambda _, row: row.activatable_widget.activate())
else:
if self.handler_id:
parent.disconnect(handler_id)

def do_add_child(self, builder: Gtk.Builder, child: GObject.Object, type: str) -> None:
match type:
case "suffix" | None:
self.box.append(child)
case "prefix":
self.box.prepend(child)
case _:
raise TypeError("Invalid type")
1 change: 1 addition & 0 deletions GTG/gtk/browser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
class GnomeConfig():
current_rep = os.path.dirname(os.path.abspath(__file__))
data = os.path.join(current_rep, '..', 'data')
ACTION_ROW = os.path.join(data, "action_row.ui")
BROWSER_UI_FILE = os.path.join(data, "main_window.ui")
HELP_OVERLAY_UI_FILE = os.path.join(data, "help_overlay.ui")
MENUS_UI_FILE = os.path.join(data, "context_menus.ui")
Expand Down
42 changes: 42 additions & 0 deletions GTG/gtk/data/action_row.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="ActionRow" parent="GtkListBoxRow">
<property name="activatable">False</property>
<property name="selectable">False</property>
<accessibility>
<relation name="labelled-by">title_label</relation>
<relation name="described-by">subtitle_label</relation>
</accessibility>
<property name="child">
<object class="GtkBox" id="box">
<property name="spacing">12</property>
<property name="margin_start">12</property>
<property name="margin_end">12</property>
<property name="margin_top">8</property>
<property name="margin_bottom">8</property>
<child>
<object class="GtkBox">
<property name="orientation">vertical</property>
<property name="valign">center</property>
<child>
<object class="GtkLabel" id="title_label">
<property name="xalign">0</property>
<property name="label" bind-source="ActionRow" bind-property="title" bind-flags="bidirectional"/>
</object>
</child>
<child>
<object class="GtkLabel" id="subtitle_label">
<property name="xalign">0</property>
<property name="visible">False</property>
<property name="label" bind-source="ActionRow" bind-property="subtitle" bind-flags="bidirectional"/>
<style>
<class name="dim-label"/>
</style>
</object>
</child>
</object>
</child>
</object>
</property>
</template>
</interface>
2 changes: 2 additions & 0 deletions GTG/gtk/meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
gtg_gtk_sources = [
'__init__.py',
'action_row.py',
'application.py',
'colors.py',
'errorhandler.py',
Expand Down Expand Up @@ -46,6 +47,7 @@ gtg_browser_sources = [
]

gtg_data_sources = [
'data/action_row.ui',
'data/backends.ui',
'data/calendar.ui',
'data/context_menus.ui',
Expand Down

0 comments on commit 5f8233d

Please sign in to comment.