gtk: Add a custom StatusToggle button

This is a Gtk.Button that has been customized to act similarily to a
Gtk.ToggleButton, but instead of giving the button a "pressed in" look
it changes the opacity of the displayed icon.

I'm planning to use this in the window headerbar in a set of buttons
designed to controll testcase filter state.

Signed-off-by: Anna Schumaker <anna@nowheycreamery.com>
This commit is contained in:
Anna Schumaker 2023-08-02 15:53:29 -04:00
parent e865728357
commit 37079ca7f5
2 changed files with 80 additions and 0 deletions

51
tests/gtk/test_button.py Normal file
View File

@ -0,0 +1,51 @@
# Copyright 2023 (c) Anna Schumaker.
"""Tests our custom button classes."""
import unittest
import xfstestsdb.gtk.button
from gi.repository import Gtk
class TestStatusToggle(unittest.TestCase):
"""Test our StatusToggle button."""
def setUp(self):
"""Set up common variables."""
self.button = xfstestsdb.gtk.button.StatusToggle("icon-name",
"css-class")
def test_button(self):
"""Test the StatusToggle button."""
self.assertIsInstance(self.button, Gtk.Button)
self.assertEqual(self.button.icon_name, "icon-name")
self.assertTrue(self.button.has_css_class("css-class"))
self.assertFalse(self.button.props.has_frame)
self.assertFalse(self.button.active)
button2 = xfstestsdb.gtk.button.StatusToggle("icon-name", "css-class",
active=True)
self.assertTrue(button2.active)
self.assertAlmostEqual(button2.props.child.props.opacity, 1.0)
def test_image(self):
"""Test that the image is set up correctly, and opacity changes."""
self.assertIsInstance(self.button.props.child, Gtk.Image)
self.assertEqual(self.button.props.child.props.icon_name, "icon-name")
self.button.active = True
self.assertAlmostEqual(self.button.props.child.props.opacity, 1.0)
self.button.active = False
self.assertAlmostEqual(self.button.props.child.props.opacity, 0.4)
def test_clicked(self):
"""Test clicking the CSSToggle button."""
self.assertFalse(self.button.active)
self.assertAlmostEqual(self.button.props.child.props.opacity, 0.4)
self.button.emit("clicked")
self.assertTrue(self.button.active)
self.assertAlmostEqual(self.button.props.child.props.opacity, 1.0)
self.button.emit("clicked")
self.assertFalse(self.button.active)
self.assertAlmostEqual(self.button.props.child.props.opacity, 0.4)

29
xfstestsdb/gtk/button.py Normal file
View File

@ -0,0 +1,29 @@
# Copyright 2023 (c) Anna Schumaker.
"""Custom, reusable Button classes."""
import typing
from gi.repository import GObject
from gi.repository import Gtk
class StatusToggle(Gtk.Button):
"""A toggle button that adds or removes a CSS class when pressed."""
active = GObject.Property(type=bool, default=False)
icon_name = GObject.Property(type=str)
def __init__(self, icon_name: str, css_class: str,
*, active: bool = False):
"""Initialize a StatusToggle button."""
super().__init__(icon_name=icon_name, has_frame=False,
child=Gtk.Image(icon_name=icon_name, opacity=0.4))
self.connect("notify::active", self.__notify_active)
self.add_css_class(css_class)
self.active = active
def __notify_active(self, toggle: typing.Self,
param: GObject.ParamSpec) -> None:
self.props.child.set_opacity(1.0 if self.active else 0.4)
def do_clicked(self) -> None:
"""Adjust image opacity when the button is toggled."""
self.active = not self.active