63 lines
1.5 KiB
Python
63 lines
1.5 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
import gi
|
||
|
gi.require_version('Gtk', '3.0')
|
||
|
from gi.repository import Gtk, Gdk
|
||
|
|
||
|
from .image import ScaleImage
|
||
|
|
||
|
class ToggleButton(Gtk.Button):
|
||
|
def __init__(self, files = [], width = 26, height = 26):
|
||
|
Gtk.Button.__init__(self)
|
||
|
|
||
|
self.is_active = False
|
||
|
|
||
|
self.set_name('ToggleButton')
|
||
|
self.set_size_request(width, height)
|
||
|
|
||
|
self.set_valign(Gtk.Align.CENTER)
|
||
|
|
||
|
# 针对macos的设置, 但只解决了普通状态下的边框问题, 鼠标经过的样式还在
|
||
|
self.set_relief(Gtk.ReliefStyle.NONE)
|
||
|
|
||
|
css_provider = Gtk.CssProvider()
|
||
|
br = '\n'
|
||
|
style = f"""
|
||
|
#ToggleButton {{
|
||
|
padding: 4px;
|
||
|
border: 0;
|
||
|
border-radius: 50%;
|
||
|
background-image: url('{files[0]}');
|
||
|
background-size: 100%;
|
||
|
background-color: transparent;
|
||
|
border-color:transparent;
|
||
|
outline: transparent;
|
||
|
}}
|
||
|
|
||
|
#ToggleButton.active {{
|
||
|
background-image: url('{files[1]}');
|
||
|
}}
|
||
|
|
||
|
#ToggleButton:hover {{
|
||
|
background-color: rgba(255,255,255,.1);
|
||
|
}}
|
||
|
"""
|
||
|
|
||
|
css_provider.load_from_data(style.encode('UTF-8'))
|
||
|
|
||
|
self.style_ctx = self.get_style_context()
|
||
|
self.style_ctx.add_provider(css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
|
||
|
|
||
|
|
||
|
|
||
|
def toggle(self, state = False):
|
||
|
|
||
|
if self.is_active == state:
|
||
|
return
|
||
|
|
||
|
self.is_active = state
|
||
|
|
||
|
if self.is_active:
|
||
|
self.style_ctx.add_class('active')
|
||
|
else:
|
||
|
self.style_ctx.remove_class('active')
|