xfstestsdb/xfstestsdb/gtk/button.py

30 lines
1.1 KiB
Python

# 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