Skip to content

Commit

Permalink
fix: Close main menus when submenu commands are chosen
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlin7 committed Oct 8, 2024
1 parent 2dd07d1 commit 89e16e4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
11 changes: 9 additions & 2 deletions src/biscuit/common/menu/command.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from __future__ import annotations

import tkinter as tk
import typing

from biscuit.common.ui import Menubutton

if typing.TYPE_CHECKING:
from .menu import Menu


class Command(Menubutton):
"""A menu item
Expand All @@ -20,6 +26,7 @@ def __init__(self, master, text, command=lambda *_: ..., *args, **kwargs) -> Non

super().__init__(master, *args, **kwargs)
self.command = command
self.menu = master.master

self.config(
text=text,
Expand All @@ -28,10 +35,10 @@ def __init__(self, master, text, command=lambda *_: ..., *args, **kwargs) -> Non
cursor="hand2",
padx=20,
pady=2,
**self.base.theme.menu.item
**self.base.theme.menu.item,
)
self.bind("<Button-1>", self.on_click)

def on_click(self, *_) -> None:
self.master.hide()
self.menu.event_chosen()
self.command()
8 changes: 8 additions & 0 deletions src/biscuit/common/menu/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tkinter as tk
from typing import Callable, Union

from biscuit.common.menu import submenu
from biscuit.common.menu.submenu import SubMenuItem

from ..ui import Frame, Toplevel
Expand All @@ -24,6 +25,8 @@ class Menu(Toplevel):
Supports adding commands, checkables and separators"""

submenu = False

def __init__(self, master, name: str = None, *args, **kwargs) -> None:
"""Create a new menu
Expand Down Expand Up @@ -184,3 +187,8 @@ def clear(self) -> None:

self.menu_items = []
self.row = 0

def event_chosen(self, *_) -> None:
"""Event handler for when a menu item is chosen"""

self.hide()
15 changes: 12 additions & 3 deletions src/biscuit/common/menu/submenu.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import tkinter as tk

from biscuit.common.icons import Icons
Expand All @@ -23,24 +25,31 @@ def __init__(self, main, master, text, *args, **kwargs) -> None:
iconsize=10,
iconside=tk.RIGHT,
*args,
**kwargs
**kwargs,
)
self.main = main
self.text_label.config(padx=14)

from .menu import Menu

class SubMenu(Menu):
def __init__(self, master, *args, **kwargs) -> None:
submenu = True

def __init__(self, master, main: Menu, *args, **kwargs) -> None:
super().__init__(master, *args, **kwargs)
self.main = main

def get_coords(self) -> None:
return (
self.master.winfo_rootx() + self.master.winfo_width(),
self.master.winfo_rooty(),
)

self.menu = SubMenu(self, text)
def event_chosen(self) -> None:
super().event_chosen()
self.main.event_chosen()

self.menu = SubMenu(self, main, text)

self.bind("<Enter>", self.on_enter)
self.bind("<Leave>", self.on_leave)
Expand Down

0 comments on commit 89e16e4

Please sign in to comment.