Skip to content

Commit a87a765

Browse files
committed
Put BaseApp's moveOnScreen implementation into wt.utility.moveOnScreen. Enhance the algorithm to also force the figure onto just one screen.
1 parent a25a46f commit a87a765

4 files changed

Lines changed: 95 additions & 73 deletions

File tree

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

0 commit comments

Comments
 (0)