def override_color(widget: Gtk.Widget, fg: Color = None, bg: Color = None): """Widget needs provider prop""" if fg: fg = f"color: {fg.as_HEX()};" else: fg = "" if bg: bg = f"background-color: {bg.as_HEX()};" else: bg = "" css = f""" .override {{ {fg} {bg} }} textview text {{ {fg} {bg} }} """ widget.provider.load_from_data(bytes(css, encoding="UTF-8")) widget.get_style_context().add_provider( widget.provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) widget.get_style_context().add_class("override")
def snapshot_widget(root: Gtk.Widget) -> dict: style = root.get_style_context() snap = { 'type': type(root).__name__, 'label': get_label_for_widget(root), 'classes': style.list_classes()} if isinstance(root, Gtk.Container): snap['children'] = list(map(snapshot_widget, root.get_children())) return snap
def snapshot_widget(root: Gtk.Widget) -> dict: style = root.get_style_context() snap = { 'type': type(root).__name__, 'label': get_label_for_widget(root), 'classes': style.list_classes() } if isinstance(root, Gtk.Container): snap['children'] = list(map(snapshot_widget, root.get_children())) return snap
def set_texture(self, widget: Gtk.Widget, texture_name: str) -> bool: """ Clears existing CSS texture class(es) from the given Gtk.Widget and gives it the CSS texture class that corresponds with the given background name, if it exists. :param widget: Gtk.Widget object to change the background of. :param texture_name: String name of the texture to apply. :return: Boolean True if a corresponding class was found and applied, False otherwise. """ button_style: Gtk.StyleContext = widget.get_style_context() for class_name in button_style.list_classes(): if class_name.startswith("texture_"): button_style.remove_class(class_name) texture_data = self._ram_data.textures.get(texture_name, None) if texture_data: widget.get_style_context().add_class(texture_data.class_name) widget.show() return True else: return False
def __init__(self, widget: Gtk.Widget, style_class_name: str) -> None: super().__init__(widget.get_style_context(), style_class_name)