Menus¶
Menu Bar Demo — MenuBar with PopupMenu dropdowns and z-ordering.
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 packages/graphics/examples/ui_menus.py
Source Code¶
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 packages/graphics/examples/ui_menus.py
7"""
8
9from simvx.core import (
10 AnchorPreset,
11 Colour,
12 Label,
13 MenuBar,
14 MenuItem,
15 Node,
16 Panel,
17 VBoxContainer,
18 Vec2,
19)
20from simvx.graphics import App
21
22
23class MenuDemo(Node):
24 """Root node for menu bar demo scene."""
25
26 def ready(self):
27 root = VBoxContainer(name="Root")
28 root.set_anchor_preset(AnchorPreset.FULL_RECT)
29 root.separation = 0
30 self.add_child(root)
31
32 # --- Menu bar ---
33 menubar = MenuBar(name="menubar")
34 menubar.size = Vec2(800, 28)
35 root.add_child(menubar)
36
37 status = Label("Ready")
38 status.text_colour = Colour.LIGHT_GRAY
39
40 def make_action(label):
41 def handler():
42 status.text = f"Action: {label}"
43
44 return handler
45
46 # File menu
47 menubar.add_menu(
48 "File",
49 [
50 MenuItem(text="New", callback=make_action("New"), shortcut="Ctrl+N"),
51 MenuItem(text="Open", callback=make_action("Open"), shortcut="Ctrl+O"),
52 MenuItem(text="Save", callback=make_action("Save"), shortcut="Ctrl+S"),
53 MenuItem(separator=True),
54 MenuItem(text="Quit", callback=make_action("Quit"), shortcut="Ctrl+Q"),
55 ],
56 )
57
58 # Edit menu
59 menubar.add_menu(
60 "Edit",
61 [
62 MenuItem(text="Undo", callback=make_action("Undo"), shortcut="Ctrl+Z"),
63 MenuItem(text="Redo", callback=make_action("Redo"), shortcut="Ctrl+Y"),
64 MenuItem(separator=True),
65 MenuItem(text="Cut", callback=make_action("Cut"), shortcut="Ctrl+X"),
66 MenuItem(text="Copy", callback=make_action("Copy"), shortcut="Ctrl+C"),
67 MenuItem(text="Paste", callback=make_action("Paste"), shortcut="Ctrl+V"),
68 ],
69 )
70
71 # View menu
72 menubar.add_menu(
73 "View",
74 [
75 MenuItem(text="Zoom In", callback=make_action("Zoom In"), shortcut="Ctrl++"),
76 MenuItem(text="Zoom Out", callback=make_action("Zoom Out"), shortcut="Ctrl+-"),
77 MenuItem(text="Reset Zoom", callback=make_action("Reset Zoom"), shortcut="Ctrl+0"),
78 ],
79 )
80
81 # --- Main content panel ---
82 content = Panel(name="ContentPanel")
83 content.size = Vec2(800, 540)
84 content.bg_colour = Colour.hex("#1A1A2E")
85 root.add_child(content)
86
87 center_label = Label("Main content area")
88 center_label.text_colour = Colour.GRAY
89 center_label.font_size = 16.0
90 center_label.set_anchor_preset(AnchorPreset.CENTER)
91 center_label.margin_left = -150
92 center_label.margin_right = 150
93 center_label.margin_top = -15
94 center_label.margin_bottom = 15
95 center_label.alignment = "center"
96 content.add_child(center_label)
97
98 # --- Status bar ---
99 status.set_anchor_preset(AnchorPreset.BOTTOM_WIDE)
100 status.margin_left = 10
101 status.margin_right = 10
102 status.margin_top = -30
103 status.margin_bottom = -10
104 content.add_child(status)
105
106
107if __name__ == "__main__":
108 app = App(title="SimVX Menu Demo", width=800, height=600)
109 app.run(MenuDemo())