Skip to content

Commit 2d80f1c

Browse files
committed
topbar -> MenuWidget || execute option's function directly through MenuWidget instead of Widget
1 parent d6a2ce3 commit 2d80f1c

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

lightweight_charts/topbar.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,38 @@ class MenuWidget(Widget):
6363
def __init__(self, topbar, options, default, separator, align, func):
6464
super().__init__(topbar, value=default, func=func)
6565
self.options = list(options)
66+
self.func = func # Store the function for later use
6667
self.run_script(f'''
67-
{self.id} = {topbar.id}.makeMenu({list(options)}, "{default}", {jbool(separator)}, "{self.id}", "{align}")
68+
{self.id} = {topbar.id}.makeMenu({self.options}, "{default}", {jbool(separator)}, "{self.id}", "{align}");
69+
{self.id}.onItemClicked = function(option) {{
70+
// Call the Python function associated with the clicked option
71+
py_callback("{self.id}", option);
72+
}};
6873
''')
6974

70-
# TODO this will probably need to be fixed
7175
def set(self, option):
7276
if option not in self.options:
7377
raise ValueError(f"Option {option} not in menu options ({self.options})")
7478
self.value = option
7579
self.run_script(f'''
7680
{self.id}._clickHandler("{option}")
7781
''')
78-
# self.win.handlers[self.id](option)
82+
83+
# Execute the function associated with the selected option
84+
if self.func:
85+
self.func(option) # Call the function with the selected option
7986

8087
def update_items(self, *items: str):
8188
self.options = list(items)
8289
self.run_script(f'{self.id}.updateMenuItems({self.options})')
8390

91+
# This method receives the callback from JavaScript to trigger the function
92+
def py_callback(self, menu_id, option):
93+
if option in self.options:
94+
self.func(option) # Call the function associated with the selected option
95+
else:
96+
print(f"No function assigned to the option: {option}")
97+
8498

8599
class ButtonWidget(Widget):
86100
def __init__(self, topbar, button, separator, align, toggle, disabled: bool = False,

0 commit comments

Comments
 (0)