Files
sublime-music/sublime/ui/common/icon_button.py
2020-06-06 15:05:17 -06:00

129 lines
3.6 KiB
Python

from typing import Any, Optional
from gi.repository import Gtk
class IconButton(Gtk.Button):
def __init__(
self,
icon_name: Optional[str],
tooltip_text: str = "",
relief: bool = False,
icon_size: Gtk.IconSize = Gtk.IconSize.BUTTON,
label: str = None,
**kwargs,
):
Gtk.Button.__init__(self, **kwargs)
self.icon_size = icon_size
self.box = Gtk.Box(
orientation=Gtk.Orientation.HORIZONTAL, name="icon-button-box"
)
self.image = None
self.has_icon = False
if icon_name:
self.image = Gtk.Image.new_from_icon_name(icon_name, self.icon_size)
self.has_icon = True
self.box.pack_start(self.image, False, False, 0)
if label is not None:
self.box.add(Gtk.Label(label=label))
if not relief:
self.props.relief = Gtk.ReliefStyle.NONE
self.add(self.box)
self.set_tooltip_text(tooltip_text)
def set_icon(self, icon_name: Optional[str]):
if icon_name:
if self.image is None:
self.image = Gtk.Image.new_from_icon_name(icon_name, self.icon_size)
else:
self.image.set_from_icon_name(icon_name, self.icon_size)
if not self.has_icon:
self.box.pack_start(self.image, False, False, 0)
self.show_all()
self.has_icon = True
else:
if self.has_icon:
self.box.remove(self.image)
self.has_icon = False
class IconToggleButton(Gtk.ToggleButton):
def __init__(
self,
icon_name: Optional[str],
tooltip_text: str = "",
relief: bool = False,
icon_size: Gtk.IconSize = Gtk.IconSize.BUTTON,
label: str = None,
**kwargs,
):
Gtk.ToggleButton.__init__(self, **kwargs)
self.icon_size = icon_size
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, name="icon-button-box")
self.image = Gtk.Image.new_from_icon_name(icon_name, self.icon_size)
box.add(self.image)
if label is not None:
box.add(Gtk.Label(label=label))
if not relief:
self.props.relief = Gtk.ReliefStyle.NONE
self.add(box)
self.set_tooltip_text(tooltip_text)
def set_icon(self, icon_name: Optional[str]):
self.image.set_from_icon_name(icon_name, self.icon_size)
def get_active(self) -> bool:
return super().get_active()
def set_active(self, active: bool):
super().set_active(active)
class IconMenuButton(Gtk.MenuButton):
def __init__(
self,
icon_name: Optional[str] = None,
tooltip_text: str = "",
relief: bool = True,
icon_size: Gtk.IconSize = Gtk.IconSize.BUTTON,
label: str = None,
popover: Any = None,
**kwargs,
):
Gtk.MenuButton.__init__(self, **kwargs)
if popover:
self.set_use_popover(True)
self.set_popover(popover)
self.icon_size = icon_size
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, name="icon-button-box")
self.image = Gtk.Image.new_from_icon_name(icon_name, self.icon_size)
box.add(self.image)
if label is not None:
box.add(Gtk.Label(label=label))
self.props.relief = Gtk.ReliefStyle.NORMAL
self.add(box)
self.set_tooltip_text(tooltip_text)
def set_icon(self, icon_name: Optional[str]):
self.image.set_from_icon_name(icon_name, self.icon_size)
def set_from_file(self, icon_file: Optional[str]):
self.image.set_from_file(icon_file)