Menu Bar Demo¶
MenuBar with PopupMenu dropdowns and z-ordering.
▶ Run in browserTags: ui
Shows a menu bar with File, Edit, and View menus that open above other content. A status label at the bottom echoes the last action.
Run: uv run python examples/features/ui/menus.py
Source¶
1"""Menu Bar Demo: MenuBar with PopupMenu dropdowns and z-ordering.
2
3Shows a menu bar with File, Edit, and View menus that open above
4other content. A status label at the bottom echoes the last action.
5
6Run: uv run python examples/features/ui/menus.py
7"""
8
9from simvx.core import (
10 AnchorPreset,
11 Colour,
12 Label,
13 MenuBar,
14 MenuItem,
15 Node,
16 Panel,
17 SizingMode,
18 VBoxContainer,
19 Vec2,
20)
21from simvx.graphics import App
22
23
24class MenuDemo(Node):
25 """Root node for menu bar demo scene."""
26
27 def on_ready(self):
28 root = VBoxContainer(name="Root")
29 root.set_anchor_preset(AnchorPreset.FULL_RECT)
30 root.separation = 0
31 root.sizing = SizingMode.EXPAND
32 self.add_child(root)
33
34 # --- Menu bar (fixed height, full width) ---
35 menubar = MenuBar(name="menubar")
36 menubar.min_size = Vec2(0, MenuBar.BAR_HEIGHT)
37 menubar.stretch_ratio = 0.0
38 root.add_child(menubar)
39
40 status = Label("Ready")
41 status.text_colour = Colour.LIGHT_GRAY
42
43 def make_action(label):
44 def handler():
45 status.text = f"Action: {label}"
46
47 return handler
48
49 # File menu
50 menubar.add_menu(
51 "File",
52 [
53 MenuItem(text="New", callback=make_action("New"), shortcut="Ctrl+N"),
54 MenuItem(text="Open", callback=make_action("Open"), shortcut="Ctrl+O"),
55 MenuItem(text="Save", callback=make_action("Save"), shortcut="Ctrl+S"),
56 MenuItem(separator=True),
57 MenuItem(text="Quit", callback=lambda: self.app.quit(), shortcut="Ctrl+Q"),
58 ],
59 )
60
61 # Edit menu
62 menubar.add_menu(
63 "Edit",
64 [
65 MenuItem(text="Undo", callback=make_action("Undo"), shortcut="Ctrl+Z"),
66 MenuItem(text="Redo", callback=make_action("Redo"), shortcut="Ctrl+Y"),
67 MenuItem(separator=True),
68 MenuItem(text="Cut", callback=make_action("Cut"), shortcut="Ctrl+X"),
69 MenuItem(text="Copy", callback=make_action("Copy"), shortcut="Ctrl+C"),
70 MenuItem(text="Paste", callback=make_action("Paste"), shortcut="Ctrl+V"),
71 ],
72 )
73
74 # View menu
75 menubar.add_menu(
76 "View",
77 [
78 MenuItem(text="Zoom In", callback=make_action("Zoom In"), shortcut="Ctrl++"),
79 MenuItem(text="Zoom Out", callback=make_action("Zoom Out"), shortcut="Ctrl+-"),
80 MenuItem(text="Reset Zoom", callback=make_action("Reset Zoom"), shortcut="Ctrl+0"),
81 ],
82 )
83
84 # --- Main content panel (takes the remaining window height) ---
85 content = Panel(name="ContentPanel")
86 content.bg_colour = Colour.hex("#1A1A2E")
87 root.add_child(content)
88
89 center_label = Label("Click File, Edit or View to open a menu")
90 center_label.text_colour = Colour.GRAY
91 center_label.font_size = 16.0
92 center_label.set_anchor_preset(AnchorPreset.CENTER)
93 center_label.margin_left = -220
94 center_label.margin_right = 220
95 center_label.margin_top = -15
96 center_label.margin_bottom = 15
97 center_label.alignment = "center"
98 content.add_child(center_label)
99
100 # --- Status bar ---
101 status.set_anchor_preset(AnchorPreset.BOTTOM_WIDE)
102 status.margin_left = 10
103 status.margin_right = 10
104 status.margin_top = -30
105 status.margin_bottom = -10
106 content.add_child(status)
107
108
109if __name__ == "__main__":
110 app = App(title="SimVX Menu Demo", width=800, height=600)
111 app.run(MenuDemo())