Skip to content

Commit 9a22fee

Browse files
committed
work on BaseDialog2 and Login dialog example
1 parent 622ee96 commit 9a22fee

File tree

8 files changed

+214
-32
lines changed

8 files changed

+214
-32
lines changed
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/>
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="+dialog" type="File"/>
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/>
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="1" type="DIR_SIGNIFIER"/>
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="Login.m" type="File"/>

widgets/+wt/+abstract/BaseDialog2.m

Lines changed: 142 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
classdef BaseDialog2 < wt.abstract.BaseWidget & ...
2-
wt.mixin.TitleFontStyled
1+
classdef BaseDialog2 < wt.abstract.BaseWidget
32
% Base class for a dialog
43

54
% Copyright 2022-2025 The MathWorks Inc.
65

76
% To do:
87
% - finish importing old BaseDialog
98
% - make examples
9+
% - handle theme changes
1010

1111
%% Public Properties
1212
properties (AbortSet, Access = public)
@@ -26,7 +26,7 @@
2626
Title
2727

2828
% Background Color
29-
TitleBackgroundColor
29+
% TitleBackgroundColor
3030

3131
% Background color mode
3232
%TitleBackgroundColorMode (1,1) wt.enum.AutoManualState = 'auto'
@@ -51,12 +51,12 @@
5151
obj.OuterPanel.Title = value;
5252
end
5353

54-
function value = get.TitleBackgroundColor(obj)
55-
value = obj.OuterPanel.BackgroundColor;
56-
end
57-
function set.TitleBackgroundColor(obj, value)
58-
obj.OuterPanel.BackgroundColor = value;
59-
end
54+
% function value = get.TitleBackgroundColor(obj)
55+
% value = obj.OuterPanel.BackgroundColor;
56+
% end
57+
% function set.TitleBackgroundColor(obj, value)
58+
% obj.OuterPanel.BackgroundColor = value;
59+
% end
6060

6161
% function value = get.TitleBackgroundColorMode(obj)
6262
% value = wt.enum.AutoManualState(obj.OuterPanel.BackgroundColorMode);
@@ -84,6 +84,9 @@
8484
% Outer panel for the dialog
8585
OuterPanel matlab.ui.container.Panel
8686

87+
% Inner grid to manage the content grid and status/button row
88+
InnerGrid matlab.ui.container.GridLayout
89+
8790
% Close button
8891
CloseButton matlab.ui.control.Button
8992

@@ -105,15 +108,119 @@
105108
end %properties
106109

107110

111+
%% Public methods
112+
methods (Access = public)
113+
114+
function positionOver(obj, refComp)
115+
% Positions the dialog centered over a given reference component
116+
117+
arguments
118+
obj (1,1) wt.abstract.BaseDialog2
119+
refComp (1,1) matlab.graphics.Graphics
120+
end
121+
122+
% Reference component size and position
123+
refPos = getpixelposition(refComp, true);
124+
refSize = refPos(3:4);
125+
refCornerA = refPos(1:2);
126+
%refCornerB = refPos(1:2) + refPos(:,3:4) - 1;
127+
128+
% Dialog size
129+
dlgPos = getpixelposition(obj);
130+
dlgSize = dlgPos(3:4);
131+
132+
% Does it fit entirely within the reference component?
133+
if all(refSize >= dlgSize)
134+
% Yes - center it over the component
135+
136+
% Calculate lower-left corner
137+
dlgPos = floor((refSize - dlgSize) / 2) + refCornerA;
138+
139+
else
140+
% NO - position within the figure
141+
142+
% Get the corners of the figure (bottom left and top right)
143+
figPos = getpixelposition(obj.Parent);
144+
figSize = figPos(3:4);
145+
146+
% Start with dialog position in lower-left of widget
147+
dlgPos = refCornerA;
148+
dlgCornerB = dlgPos + dlgSize;
149+
150+
% Move left and down as needed to fit in figure
151+
adj = figSize - dlgCornerB;
152+
adj(adj>0) = 0;
153+
dlgPos = max(dlgPos + adj, [1 1]);
154+
dlgCornerB = dlgPos + dlgSize;
155+
156+
% If it doesn't fit in the figure, shrink it
157+
adj = figSize - dlgCornerB;
158+
adj(adj>0) = 0;
159+
dlgSize = dlgSize + adj;
160+
161+
end %if
162+
163+
% Set final position
164+
obj.Position = [dlgPos dlgSize];
165+
166+
end %function
167+
168+
169+
function labels = addRowLabels(obj, names, parent, column, startRow)
170+
% Add a group of standard row labels to the grid (or specified
171+
% grid)
172+
173+
arguments
174+
obj %#ok<INUSA>
175+
names (:,1) string
176+
parent = obj.Grid
177+
column = 1
178+
startRow = 1
179+
end
180+
181+
numRows = numel(names);
182+
labels = gobjects(1,numRows);
183+
hasText = false(1,numRows);
184+
for idx = 1:numel(names)
185+
thisName = names(idx);
186+
hasText(idx) = strlength(thisName) > 0;
187+
if hasText(idx)
188+
h = uilabel(parent);
189+
h.HorizontalAlignment = "right";
190+
h.Text = thisName;
191+
h.Layout.Column = column;
192+
h.Layout.Row = idx + startRow - 1;
193+
labels(idx) = h;
194+
end
195+
end
196+
197+
% Remove the empty spaces
198+
labels(~hasText) = [];
199+
200+
end %function
201+
202+
203+
function b = addButtons(obj, labels)
204+
% Adds buttons to the lower dialog area
205+
206+
b = wt.ButtonGrid(obj.InnerGrid,"Text",labels,"Icon",[]);
207+
b.ButtonWidth(:) = {'fit'};
208+
b.Layout.Row = 2;
209+
b.Layout.Column = 2;
210+
211+
end %function
212+
213+
end %methods
214+
215+
108216
%% Protected methods
109217
methods (Access = protected)
110218

111219
function setup(obj)
112-
% Configure the widget
220+
% Configure the dialog
113221

114222
% Defaults
115223
obj.Position(3:4) = [350,200];
116-
obj.TitleFontSize = 16;
117224

118225
% Outer grid to enable the dialog panel to fill the component
119226
obj.OuterGrid = uigridlayout(obj,[1 1]);
@@ -122,33 +229,44 @@ function setup(obj)
122229
% Outer dialog panel
123230
obj.OuterPanel = uipanel(obj.OuterGrid);
124231
obj.OuterPanel.Title = "Dialog Title";
232+
obj.OuterPanel.FontSize = 14;
233+
obj.OuterPanel.FontWeight = "bold";
125234
obj.OuterPanel.BorderWidth = 1;
126235
obj.OuterPanel.AutoResizeChildren = false;
127236
obj.OuterPanel.ResizeFcn = @(~,~)onOuterPanelResize(obj);
128237
obj.OuterPanel.ButtonDownFcn = @(~,evt)onTitleButtonDown(obj,evt);
129-
130-
% Inner Grid to manage building blocks
131-
obj.Grid = uigridlayout(obj.OuterPanel,[1 1]);
132-
obj.Grid.Padding = 10;
133-
obj.Grid.RowSpacing = 5;
134-
obj.Grid.ColumnSpacing = 5;
135-
obj.Grid.Scrollable = true;
136238

137239
% Close Button
138240
obj.CloseButton = uibutton(obj.OuterPanel);
139241
obj.CloseButton.Text = "";
140242
obj.CloseButton.IconAlignment = "center";
141243
obj.CloseButton.ButtonPushedFcn = @(src,evt)obj.onClosePushed();
244+
245+
% Inner Grid to manage content and button area
246+
obj.InnerGrid = uigridlayout(obj.OuterPanel,[2 2]);
247+
obj.InnerGrid.Padding = 10;
248+
obj.InnerGrid.RowHeight = {'1x','fit'};
249+
obj.InnerGrid.ColumnWidth = {'1x','fit'};
250+
obj.InnerGrid.RowSpacing = 5;
251+
252+
% Grid to place dialog content
253+
obj.Grid = uigridlayout(obj.InnerGrid,[1 1]);
254+
obj.Grid.Layout.Row = 1;
255+
obj.Grid.Layout.Column = [1 2];
256+
obj.Grid.Padding = 0;
257+
obj.Grid.RowSpacing = 5;
258+
obj.Grid.ColumnSpacing = 5;
259+
obj.Grid.Scrollable = true;
142260

143261
% Apply theme colors
144262
if ~isMATLABReleaseOlderThan("R2025a")
263+
obj.OuterPanel.ForegroundColor = obj.getThemeColor("--mw-color-primary");
145264
obj.OuterPanel.BorderColor = obj.getThemeColor("--mw-borderColor-secondary");
146265
obj.OuterPanel.BackgroundColor = obj.getThemeColor("--mw-backgroundColor-secondary");
147-
% obj.CloseButton.BackgroundColor = obj.getThemeColor("--mw-backgroundColor-iconuiFill-primary");
148266
else
267+
obj.OuterPanel.ForegroundColor = [0.38 0.38 0.38];
149268
obj.OuterPanel.BorderColor = [.5 .5 .5];
150269
obj.OuterPanel.BackgroundColor = [.9 .9 .9];
151-
% obj.CloseButton.BackgroundColor = [.38 .38 .38];
152270
end
153271

154272
% Apply close button color
@@ -171,9 +289,6 @@ function setup(obj)
171289
% Bring the dialog back to the top
172290
uistack(obj,"top");
173291

174-
% Update component lists
175-
obj.TitleFontStyledComponents = obj.OuterPanel;
176-
177292
% Ensure it fits in the figure
178293
obj.resizeToFitFigure();
179294

@@ -185,8 +300,11 @@ function setup(obj)
185300

186301
function update(obj)
187302

188-
303+
% Ensure it fits in the figure
304+
obj.resizeToFitFigure();
189305

306+
% Reposition the close button
307+
obj.repositionCloseButton();
190308

191309
end %function
192310

@@ -252,14 +370,6 @@ function attachLifecycleListeners(obj, owners)
252370

253371
end %function
254372

255-
256-
function color = getDefaultTitleColor(obj)
257-
% Returns the default color for 'auto' mode (R2025a and later)
258-
259-
color = obj.getThemeColor("--mw-color-primary");
260-
261-
end %function
262-
263373
end %methods
264374

265375

widgets/+wt/+dialog/Login.m

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
classdef Login < wt.abstract.BaseDialog2
2+
% Implements a simple login dialog
3+
4+
5+
%% Internal Properties
6+
properties (Transient, NonCopyable, Hidden, SetAccess = private)
7+
8+
LoginField matlab.ui.control.EditField
9+
10+
PasswordField wt.PasswordField
11+
12+
end %properties
13+
14+
15+
%% Protected methods
16+
methods (Access = protected)
17+
18+
function setup(obj)
19+
% Configure the widget
20+
21+
% Defaults
22+
obj.Size = [300,140];
23+
24+
% Call superclass method
25+
obj.setup@wt.abstract.BaseDialog2();
26+
27+
% Add buttons
28+
b = addButtons(obj, ["Login","Cancel"]);
29+
30+
% Configure grid
31+
obj.Grid.RowHeight = {25,25};
32+
obj.Grid.ColumnWidth = {'fit','1x'};
33+
34+
% Set title
35+
obj.Title = "Login";
36+
37+
% Add labels
38+
col = 1;
39+
startRow = 1;
40+
lbl = obj.addRowLabels(["Username:","Password"], ...
41+
obj.Grid, col, startRow);
42+
43+
% Add controls
44+
obj.LoginField = uieditfield(obj.Grid);
45+
obj.LoginField.Layout.Row = 1;
46+
obj.LoginField.Layout.Column = 2;
47+
48+
obj.PasswordField = wt.PasswordField(obj.Grid);
49+
obj.PasswordField.Layout.Row = 2;
50+
obj.PasswordField.Layout.Column = 2;
51+
52+
end %function
53+
54+
end %methods
55+
56+
end %classdef

0 commit comments

Comments
 (0)