Skip to content

Commit 15c01e6

Browse files
committed
add wt.MenuButton and wt.eventdata.MenuSelectedData
1 parent f6a8e7d commit 15c01e6

File tree

6 files changed

+185
-0
lines changed

6 files changed

+185
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Info>
3+
<Category UUID="FileClassCategory">
4+
<Label UUID="design"/>
5+
</Category>
6+
</Info>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Info location="MenuSelectedData.m" type="File"/>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Info>
3+
<Category UUID="FileClassCategory">
4+
<Label UUID="design"/>
5+
</Category>
6+
</Info>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Info location="MenuButton.m" type="File"/>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
classdef MenuSelectedData < event.EventData & dynamicprops
2+
% Event data for menu selection
3+
%
4+
% Syntax:
5+
% obj = wt.eventdata.MenuSelectedData(eventData)
6+
%
7+
8+
% Copyright 2025 The MathWorks Inc.
9+
10+
%% Properties
11+
properties (SetAccess = protected)
12+
Menu matlab.ui.container.Menu
13+
Text (1,1) string
14+
Tag (1,1) string
15+
end %properties
16+
17+
18+
%% Constructor / destructor
19+
methods
20+
function obj = MenuSelectedData(eventData)
21+
22+
obj.Menu = eventData.Source;
23+
obj.Text = eventData.Source.Text;
24+
obj.Tag = eventData.Source.Tag;
25+
26+
end %constructor
27+
end %methods
28+
29+
end % classdef

widgets/+wt/MenuButton.m

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
classdef MenuButton < wt.abstract.BaseWidget
2+
% Implements a button that provides a custom context menu
3+
4+
% Copyright 2025 The MathWorks Inc.
5+
6+
7+
%% Events
8+
events (HasCallbackProperty, NotifyAccess = protected)
9+
10+
% Triggered when a menu item is selected
11+
MenuSelected
12+
13+
% Triggered when the button is pushed
14+
ButtonPushed
15+
16+
end %events
17+
18+
19+
%% Read-only properties
20+
properties (SetAccess = protected)
21+
22+
% Root-level menu
23+
Menu matlab.ui.container.ContextMenu
24+
25+
end %properties
26+
27+
28+
%% Internal Properties
29+
properties (Transient, NonCopyable, Hidden, SetAccess = protected)
30+
31+
% The button
32+
Button matlab.ui.control.Button
33+
34+
end %properties
35+
36+
37+
%% Public methods
38+
methods
39+
40+
function newItem = addMenuItems(obj, names, tags)
41+
% Add simple menu items
42+
43+
arguments
44+
obj
45+
names (:,1) string
46+
tags (:,1) string = repmat("",size(names))
47+
end
48+
49+
% Validate tags is the same length as names
50+
validateattributes(tags, {'string'}, {'vector', 'numel', length(names)});
51+
52+
% Remove the dummy menu items
53+
oldItems = obj.Menu.Children;
54+
if numel(oldItems) == 2
55+
isDummy = startsWith({oldItems.Tag}, "dummy");
56+
delete(oldItems(isDummy))
57+
end
58+
59+
% Create menu items based on provided names and tags
60+
newItem = matlab.ui.container.Menu.empty(0,1);
61+
for idx = 1:length(names)
62+
newItem(idx,1) = uimenu("Parent", obj.Menu,...
63+
"Text", names(idx),...
64+
"Tag", tags(idx),...
65+
"MenuSelectedFcn", @(~,evt)onMenuSelected(obj,evt) );
66+
end
67+
68+
end %function
69+
70+
end %methods
71+
72+
73+
%% Protected methods
74+
methods (Access = protected)
75+
76+
function setup(obj)
77+
78+
% Call superclass method
79+
obj.setup@wt.abstract.BaseWidget()
80+
81+
% Set default size
82+
obj.Position(3:4) = [30 30];
83+
84+
% Create the button
85+
obj.Button = uibutton(obj.Grid);
86+
obj.Button.Text = "...";
87+
88+
% Set the callback for the button
89+
obj.Button.ButtonPushedFcn = @(~,~)obj.onButtonPushed();
90+
91+
% Create the context menu
92+
obj.Menu = matlab.ui.container.ContextMenu;
93+
94+
% Add two dummy items
95+
obj.addMenuItems(["Item 1","Item 2"], ["dummy1","dummy2"]);
96+
97+
% Find the figure and attach it
98+
fig = ancestor(obj,'figure');
99+
obj.Menu.Parent = fig;
100+
101+
end %function
102+
103+
104+
function update(~)
105+
106+
end %function
107+
108+
109+
function onButtonPushed(obj)
110+
111+
% Notify listeners
112+
notify(obj, 'ButtonPushed');
113+
114+
% Find the figure and attach the context menu
115+
fig = ancestor(obj,'figure');
116+
obj.Menu.Parent = fig;
117+
118+
% Find the button's location in the figure
119+
pos = getpixelposition(obj.Button, true);
120+
pos = [pos(1)+pos(3), pos(2)]; %lower-right
121+
122+
% Open the menu
123+
obj.Menu.open(pos);
124+
125+
end %function
126+
127+
128+
function onMenuSelected(obj,evt)
129+
130+
% Create eventdata for listeners / callback
131+
evtOut = wt.eventdata.MenuSelectedData(evt);
132+
133+
% Notify listeners
134+
notify(obj, 'MenuSelected', evtOut);
135+
136+
end %function
137+
138+
end %methods
139+
140+
end %classdef

0 commit comments

Comments
 (0)