Skip to content

Commit f7c0a55

Browse files
committed
work on BaseExternalDialog - positioning over figure
1 parent d15387f commit f7c0a55

File tree

1 file changed

+41
-76
lines changed

1 file changed

+41
-76
lines changed

widgets/+wt/+abstract/BaseExternalDialog.m

Lines changed: 41 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
%% Public Properties
2323
properties (AbortSet, Access = public)
2424

25+
% Dialog Size
26+
Size double {mustBePositive} = [350 200]
27+
2528
% Modal (block other figure interaction)
2629
Modal (1,1) logical = false
2730

@@ -36,9 +39,6 @@
3639
% Position on screen [left bottom width height]
3740
DialogPosition
3841

39-
% Dialog Size
40-
Size (1,2) double
41-
4242
% Dialog Title
4343
Title
4444

@@ -70,11 +70,14 @@
7070
function value = get.Size(obj)
7171
if isscalar(obj.DialogFigure)
7272
value = obj.DialogFigure.Position(3:4);
73+
else
74+
value = obj.Size;
7375
end
7476
end
7577
function set.Size(obj, value)
76-
if isscalar(obj.DialogFigure)
77-
obj.DialogFigure.Position(3:4) = value;
78+
obj.Size = value;
79+
if isscalar(obj.DialogFigure) %#ok<MCSUP>
80+
obj.DialogFigure.Position(3:4) = value; %#ok<MCSUP>
7881
end
7982
end
8083

@@ -203,7 +206,7 @@
203206
properties (Transient, NonCopyable, Hidden, SetAccess = protected)
204207

205208
% Figure tied to the dialog lifecycle
206-
Figure matlab.ui.Figure
209+
CallingFigure matlab.ui.Figure
207210

208211
% This dialog's figure
209212
DialogFigure matlab.ui.Figure
@@ -229,72 +232,6 @@ function delete(obj)
229232
%% Public methods
230233
methods (Sealed, Access = public)
231234

232-
function positionOver(obj, refComp)
233-
% Positions the dialog centered over a given reference component
234-
235-
arguments
236-
obj (1,1) wt.abstract.BaseInternalDialog
237-
refComp (1,1) matlab.graphics.Graphics
238-
end
239-
240-
% Reference component size and position
241-
refPos = getpixelposition(refComp, true);
242-
refSize = refPos(3:4);
243-
244-
% Lower left corner depends if it's a figure
245-
if isa(refComp, "matlab.ui.Figure")
246-
refCornerA = [1 1];
247-
else
248-
refCornerA = refPos(1:2);
249-
end
250-
251-
% Dialog size
252-
dlgPos = getpixelposition(obj);
253-
dlgSize = dlgPos(3:4);
254-
255-
% Does it fit entirely within the reference component?
256-
if all(refSize >= dlgSize)
257-
% Yes - center it over the component
258-
259-
% Calculate lower-left corner
260-
dlgPos = floor((refSize - dlgSize) / 2) + refCornerA;
261-
262-
else
263-
% NO - position within the figure
264-
265-
% Get the corners of the figure (bottom left and top right)
266-
figPos = getpixelposition(obj.Parent);
267-
figSize = figPos(3:4);
268-
269-
% Start with dialog position in lower-left of widget
270-
dlgPos = refCornerA;
271-
dlgCornerB = dlgPos + dlgSize;
272-
273-
% Move left and down as needed to fit in figure
274-
adj = figSize - dlgCornerB;
275-
adj(adj>0) = 0;
276-
dlgPos = max(dlgPos + adj, [1 1]);
277-
dlgCornerB = dlgPos + dlgSize;
278-
279-
% If it doesn't fit in the figure, shrink it
280-
adj = figSize - dlgCornerB;
281-
adj(adj>0) = 0;
282-
dlgSize = dlgSize + adj;
283-
284-
end %if
285-
286-
% Disable warning
287-
warnState = warning('off','MATLAB:ui:components:noPositionSetWhenInLayoutContainer');
288-
289-
% Set final position
290-
obj.Position = [dlgPos dlgSize];
291-
292-
% Restore warning
293-
warning(warnState)
294-
295-
end %function
296-
297-
298235
function labels = addRowLabels(obj, names, parent, column, startRow)
299236
% Add a group of standard row labels to the grid (or specified
300237
% grid)
@@ -412,16 +349,21 @@ function setup(obj)
412349
% Configure the dialog
413350

414351
% Store the parent figure
415-
obj.Figure = ancestor(obj,'figure');
352+
obj.CallingFigure = ancestor(obj,'figure');
353+
354+
% Get the size input
355+
sizeInput = obj.Size;
416356

417357
% Create a new figure for this dialog
418358
obj.DialogFigure = uifigure();
419359
obj.DialogFigure.AutoResizeChildren = false;
420360
obj.DialogFigure.Units = "pixels";
361+
obj.DialogFigure.Position(3:4) = sizeInput;
362+
obj.positionOverCallingFigure()
421363

422364
% Apply the same theme (R2025a and later)
423365
if ~isMATLABReleaseOlderThan("R2025a")
424-
obj.DialogFigure.Theme = obj.Figure.Theme;
366+
obj.DialogFigure.Theme = obj.CallingFigure.Theme;
425367
end
426368

427369
% Give the figure a grid layout
@@ -452,7 +394,7 @@ function setup(obj)
452394
obj.Grid.Scrollable = true;
453395

454396
% Add modal image over the app's figure
455-
obj.ModalImage = uiimage(obj.Figure);
397+
obj.ModalImage = uiimage(obj.CallingFigure);
456398
obj.ModalImage.ImageSource = "overlay_gray.png";
457399
obj.ModalImage.ScaleMethod = "stretch";
458400
obj.ModalImage.Tooltip = "Close the dialog box to continue using the app.";
@@ -529,6 +471,29 @@ function checkDeletionCriteria(obj)
529471

530472
end %function
531473

474+
475+
function positionOverCallingFigure(obj)
476+
% Positions the dialog centered over the reference figure
477+
478+
% Reference component size and position
479+
refPos = getpixelposition(obj.CallingFigure, true);
480+
refSize = refPos(3:4);
481+
refCornerA = refPos(1:2);
482+
483+
% Dialog size
484+
dlgSize = obj.DialogFigure.Position(3:4);
485+
486+
% center it over the figure
487+
488+
% Calculate lower-left corner
489+
dlgPos = floor((refSize - dlgSize) / 2) + refCornerA;
490+
491+
% Set final position
492+
obj.DialogFigure.Position = [dlgPos dlgSize];
493+
494+
495+
end %function
496+
532497
end %methods
533498

534499

@@ -542,7 +507,7 @@ function updateModalImage(obj)
542507
if obj.Modal
543508

544509
% Set position to match the figure
545-
posF = getpixelposition(obj.Figure);
510+
posF = getpixelposition(obj.CallingFigure);
546511
szF = posF(3:4);
547512
obj.ModalImage.Position = [1 1 szF];
548513

0 commit comments

Comments
 (0)