Skip to content

Commit

Permalink
Progress with CustomToolbutton, thonny#2760
Browse files Browse the repository at this point in the history
  • Loading branch information
aivarannamaa committed Jun 27, 2023
1 parent 3f1dce9 commit 5e8948d
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 21 deletions.
26 changes: 23 additions & 3 deletions misc/aqua.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,29 @@

style = ttk.Style(root)

tb = ttk.Button(root, style="Toolbutton", text="ajoi")
tb.grid(padx=20, pady=20)
style.theme_use("clam")

print(style.layout("Toolbutton"))
style.configure("Mamma.TLabel", background="red")
style.map("Mamma.TLabel",
background=[('active','green'), ('!active','blue')],)

fr = tk.Frame(root, background="green")
fr.grid(padx=20, pady=20)

lb = tk.Label(fr, text="ajoi", background="green")
lb.grid(padx=15, pady=0)

def enter(event):
fr.configure(background="red")
lb.configure(background="red")

def leave(event):
fr.configure(background="green")
lb.configure(background="green")

fr.bind("<Enter>", enter, True)
fr.bind("<Leave>", leave, True)

#print(style.layout("Toolbutton"))

root.mainloop()
7 changes: 7 additions & 0 deletions thonny/plugins/base_ui_themes.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def clam() -> BasicUiThemeSettings:
"darkcolor": [("pressed", darker)],
},
},
"CustomToolbutton": {"configure": {"background": frame, "activebackground": darker}},
"TCheckbutton": {
"configure": {
"indicatorbackground": "#ffffff",
Expand Down Expand Up @@ -615,6 +616,12 @@ def enhanced_aqua() -> CompoundUiThemeSettings:
}
},
"Inactive.ViewTab.TLabel": {"map": {"relief": [("hover", "raised")]}},
"CustomToolbutton": {
"configure": {
"background": "systemWindowBackgroundColor",
"activebackground": "systemWindowBackgroundColor5",
}
},
},
]

Expand Down
77 changes: 61 additions & 16 deletions thonny/ui_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@
logger = getLogger(__name__)


class AquaToolbutton(tk.Label):
def __init__(self, master, command: Callable, image=None, state="normal", **kw):
class CustomToolbutton(tk.Frame):
def __init__(
self,
master,
command: Callable,
image=None,
state="normal",
text=None,
compound=None,
width=None,
pad=None,
):
if isinstance(image, (list, tuple)):
self.normal_image = image[0]
self.disabled_image = image[-1]
Expand All @@ -43,15 +53,42 @@ def __init__(self, master, command: Callable, image=None, state="normal", **kw):
self.disabled_image = image

self.state = state
style_conf = get_style_configuration("CustomToolbutton")
self.normal_background = style_conf["background"]
self.hover_background = style_conf["activebackground"]

if state == "disabled":
self.current_image = self.disabled_image
else:
self.current_image = self.normal_image

super().__init__(master, image=self.current_image, **kw)
super().__init__(master, background=self.normal_background)
self.label = tk.Label(
self,
image=self.current_image,
text=text,
compound=compound,
width=None if width is None else ems_to_pixels(width - 1),
background=self.normal_background,
)

# TODO: introduce padx and pady arguments
if isinstance(pad, int):
padx = pad
pady = pad
elif isinstance(pad, (tuple, list)):
assert len(pad) == 2
# TODO: how to use it?
padx = pad
pady = 0
else:
padx = None
pady = None

self.label.grid(row=0, column=0, padx=padx, pady=pady, sticky="nsew")
self.command = command
self.bind("<1>", self.on_click, True)
self.label.bind("<1>", self.on_click, True)
self.bind("<Enter>", self.on_enter, True)
self.bind("<Leave>", self.on_leave, True)

Expand All @@ -60,12 +97,15 @@ def on_click(self, event):
self.command()

def on_enter(self, event):
self.configure(relief="raised")
if self.state == "normal":
super().configure(background=self.hover_background)
self.label.configure(background=self.hover_background)

def on_leave(self, event):
self.configure(relief="flat")
super().configure(background=self.normal_background)
self.label.configure(background=self.normal_background)

def configure(self, cnf={}, state=None, command=None, **kw):
def configure(self, cnf={}, state=None, image=None, command=None, **kw):
if command:
self.command = command

Expand All @@ -75,14 +115,16 @@ def configure(self, cnf={}, state=None, command=None, **kw):
state = "normal"

self.state = state
if self.state == "disabled":
if image:
self.current_image = image
elif self.state == "disabled":
self.current_image = self.disabled_image
else:
self.current_image = self.normal_image

# Frame should be always state=normal as it won't display the image if "disabled"
# tkinter.Frame should be always state=normal as it won't display the image if "disabled"
# at least on mac with Tk 8.6.13
super().configure(cnf, image=self.current_image, state="normal", **kw)
self.label.configure(cnf, image=self.current_image, state="normal", **kw)


class CommonDialog(tk.Toplevel):
Expand Down Expand Up @@ -1234,9 +1276,12 @@ def showtip(self, text):
self.text = text
if self.tipwindow or not self.text:
return

# x = self.widget.winfo_pointerx() + ems_to_pixels(0)
# y = self.widget.winfo_pointery() + ems_to_pixels(0.8)
x, y, _, cy = self.widget.bbox("insert")
x = x + self.widget.winfo_rootx() + 27
y = y + cy + self.widget.winfo_rooty() + self.widget.winfo_height() + 2
x = x + self.widget.winfo_rootx()
y = y + self.widget.winfo_rooty() + self.widget.winfo_height() + ems_to_pixels(0.2)
self.tipwindow = tw = tk.Toplevel(self.widget)
if running_on_mac_os():
try:
Expand Down Expand Up @@ -1289,8 +1334,8 @@ def enter(event):
def leave(event):
toolTip.hidetip()

widget.bind("<Enter>", enter)
widget.bind("<Leave>", leave)
widget.bind("<Enter>", enter, True)
widget.bind("<Leave>", leave, True)


class NoteBox(CommonDialog):
Expand Down Expand Up @@ -2619,16 +2664,16 @@ def create_toolbutton(
pad=None,
width=None,
) -> tk.Widget:
if "aqua" in get_workbench()._current_theme_name.lower():
return AquaToolbutton(
if True:
return CustomToolbutton(
master,
command=command,
text=text,
image=image,
compound=compound,
state=state,
pad=pad,
width=width,
pad=pad,
)
else:
return ttk.Button(
Expand Down
4 changes: 2 additions & 2 deletions thonny/workbench.py
Original file line number Diff line number Diff line change
Expand Up @@ -2024,12 +2024,12 @@ def _add_toolbar_button(
state=tk.NORMAL,
text=caption,
compound="top" if self.in_simple_mode() else None,
pad=(10, 0) if self.in_simple_mode() else None,
pad=ems_to_pixels(0.5) if self.in_simple_mode() else ems_to_pixels(0.25),
width=button_width,
)

def toolbar_handler(*args):
handler(*args)
handler()
self._update_toolbar()
if self.focus_get() == button:
# previously selected widget would be better candidate, but this is
Expand Down

0 comments on commit 5e8948d

Please sign in to comment.