Skip to content

Commit 470e767

Browse files
authored
Merge pull request #147 from mathworks/143-baseapp-should-position-the-launched-app-all-on-one-screen-not-spanning-multiple
143 baseapp should position the launched app all on one screen not spanning multiple
2 parents a25a46f + b79d95d commit 470e767

File tree

9 files changed

+105
-74
lines changed

9 files changed

+105
-74
lines changed

deploy/wtDeployVersion.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.4.2
1+
2.4.3

deploy/wtPackageRelease.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
% Perform the packaging
8080
matlab.addons.toolbox.packageToolbox(opts);
8181

82+
% Add the installer to the project
83+
proj.addFile(outputFile)
84+
8285

8386
%% Open the release folder
8487

2.85 MB
Binary file not shown.
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="Widgets Toolbox 2.4.3.mltbx" 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="moveOnScreen.m" type="File"/>

widgets/+wt/+apps/BaseApp.m

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -386,79 +386,8 @@ function moveOnScreen(app)
386386
% Show output if Debug is on
387387
app.displayDebugText();
388388

389-
if strcmp(app.Figure.Units,'pixels')
390-
391-
% Get the corners of each screen
392-
g = groot;
393-
screenPos = g.MonitorPositions;
394-
screenCornerA = screenPos(:,1:2);
395-
screenCornerB = screenPos(:,1:2) + screenPos(:,3:4) - 1;
396-
397-
% Buffer for title bar
398-
titleBarHeight = 30;
399-
400-
% Get the corners of the figure (bottom left and top right)
401-
figPos = app.Figure.OuterPosition;
402-
figCornerA = figPos(1:2);
403-
figCornerB = figPos(1:2) + figPos(:,3:4) - 1;
404-
405-
% Are the corners on any screen?
406-
aIsOnScreen = all( figCornerA >= screenCornerA & ...
407-
figCornerA <= screenCornerB, 2 );
408-
bIsOnScreen = all( figCornerB >= screenCornerA & ...
409-
figCornerB <= screenCornerB, 2);
410-
411-
% Are corners on a screen?
412-
413-
% Are both corners fully on any screen?
414-
if any(aIsOnScreen) && any(bIsOnScreen)
415-
% Yes - do nothing
416-
417-
elseif any(bIsOnScreen)
418-
% No - only upper right corner is on a screen
419-
420-
% Calculate the adjustment needed, and make it
421-
figAdjust = max(figCornerA, screenCornerA(bIsOnScreen,:)) ...
422-
- figCornerA;
423-
figPos(1:2) = figPos(1:2) + figAdjust;
424-
425-
% Ensure the upper right corner still fits
426-
figPos(3:4) = min(figPos(3:4), ...
427-
screenCornerB(bIsOnScreen,:) - figPos(1:2) - [0 titleBarHeight] + 1);
428-
429-
% Move the figure
430-
app.Figure.Position = figPos;
431-
432-
elseif any(aIsOnScreen)
433-
% No - only lower left corner is on a screen
434-
435-
% Calculate the adjustment needed, and make it
436-
figAdjust = min(figCornerB, screenCornerB(aIsOnScreen,:)) ...
437-
- figCornerB;
438-
figPos(1:2) = max( screenCornerA(aIsOnScreen,:),...
439-
figPos(1:2) + figAdjust );
440-
441-
% Ensure the upper right corner still fits
442-
figPos(3:4) = min(figPos(3:4), ...
443-
screenCornerB(aIsOnScreen,:) - figPos(1:2) - [0 titleBarHeight] + 1);
444-
445-
% Move the figure
446-
app.Figure.Position = figPos;
447-
448-
else
449-
% No - Not on any screen
450-
451-
% This is slower, but uncommon anyway
452-
movegui(app.Figure,'onscreen');
453-
454-
end %if any( all(aIsOnScreen,2) & all(bIsOnScreen,2) )
455-
456-
else
457-
458-
% This is slower, but uncommon anyway
459-
movegui(app.Figure,'onscreen');
460-
461-
end %if strcmp(app.Figure.Units,'pixels')
389+
% Move it on screen
390+
wt.utility.moveOnScreen(app.Figure);
462391

463392
end %function
464393

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
function moveOnScreen(fig)
2+
% Ensure the figure is placed on screen
3+
4+
% Copyright 2025 The MathWorks, Inc.
5+
6+
% Define arguments
7+
arguments
8+
fig (1,1) matlab.ui.Figure
9+
end
10+
11+
% Figure must be in pixel units
12+
if ~strcmp(fig.Units,'pixels')
13+
oldUnits = fig.Units;
14+
cleanupObj = onCleanup(@()set(fig,"Units",oldUnits));
15+
fig.Units = 'pixels';
16+
end
17+
18+
% Get screen positions
19+
g = groot;
20+
screenPos = g.MonitorPositions;
21+
22+
% Buffer for title bar
23+
titleBarHeight = 30;
24+
25+
% Get the corners of the figure (bottom left and top right)
26+
figPos = fig.OuterPosition;
27+
figCornerA = figPos(1:2);
28+
figCornerB = figPos(1:2) + figPos(:,3:4) - 1;
29+
30+
% Calculate the figure's area shown on each screen
31+
overlapAreas = zeros(size(screenPos, 1), 1);
32+
for i = 1:size(screenPos, 1)
33+
thisScreen = screenPos(i, :);
34+
overlapWidth = max(0, min(figPos(1) + figPos(3), thisScreen(1) + thisScreen(3)) - max(figPos(1), thisScreen(1)));
35+
overlapHeight = max(0, min(figPos(2) + figPos(4), thisScreen(2) + thisScreen(4)) - max(figPos(2), thisScreen(2)));
36+
overlapAreas(i) = overlapWidth * overlapHeight;
37+
end
38+
39+
% Determine the screen with the largest overlap
40+
[~, screenIdx] = max(overlapAreas);
41+
42+
% Get the corners of the screen
43+
screenCornerA = screenPos(screenIdx, 1:2);
44+
screenCornerB = screenCornerA + screenPos(screenIdx, 3:4) - 1;
45+
46+
% Are the corners on the screen?
47+
aIsOnScreen = all( figCornerA >= screenCornerA & ...
48+
figCornerA <= screenCornerB, 2 );
49+
bIsOnScreen = all( figCornerB >= screenCornerA & ...
50+
figCornerB <= screenCornerB, 2);
51+
52+
53+
% Are both corners fully on one screen?
54+
if aIsOnScreen && bIsOnScreen
55+
% Yes - do nothing
56+
57+
elseif bIsOnScreen
58+
% Upper right corner is on a screen
59+
% Adjust so the entire figure is on that screen
60+
61+
% Calculate the adjustment needed, and make it
62+
figAdjust = max(figCornerA, screenCornerA) - figCornerA;
63+
figPos(1:2) = figPos(1:2) + figAdjust;
64+
65+
% Ensure the upper right corner still fits
66+
requestedSize = screenCornerB - figPos(1:2) - [0 titleBarHeight] + 1;
67+
figPos(3:4) = min(figPos(3:4), requestedSize);
68+
69+
% Move the figure
70+
fig.Position = figPos;
71+
72+
else
73+
% Lower left corner is on a screen
74+
% Adjust so the entire figure is on that screen
75+
76+
% Calculate the adjustment needed, and make it
77+
figAdjust = min(figCornerB, screenCornerB) - figCornerB;
78+
figPos(1:2) = max(screenCornerA, figPos(1:2) + figAdjust);
79+
80+
% Ensure the upper right corner still fits
81+
requestedSize = screenCornerB - figPos(1:2) - [0 titleBarHeight] + 1;
82+
figPos(3:4) = min(figPos(3:4), requestedSize);
83+
84+
% Move the figure
85+
fig.Position = figPos;
86+
87+
end %if

0 commit comments

Comments
 (0)