-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuifigureOnTop.m
126 lines (104 loc) · 3.29 KB
/
uifigureOnTop.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
function wasOnTop = uifigureOnTop( uifig, isOnTop )
%UIFIGUREONTOP allows to trigger uifigure's "Always On Top" state
%
%% INPUT ARGUMENTS:
%
% * uifig - Matlab uifigure handle, scalar
% * isOnTop - logical scalar or empty array
%
%
%% USAGE:
%
% * uifigureOnTop(uifigure, true); - switch on "always on top"
% * uifigureOnTop(uifigure, false); - switch off "always on top"
% * uifigureOnTop(uifigure); - equal to uifigureOnTop( uifigure,true);
% * WasOnTop = uifigureOnTop(...); - returns boolean value "if figure WAS on top"
% * isOnTop = uifigureOnTop(uifigure,[]) - get "if figure is on top" property
%
% For Matlab windows, created via `hf=figure()` use `WinOnTop()`, see:
% https://www.mathworks.com/matlabcentral/fileexchange/42252-winontop
%
%% LIMITATIONS:
%
% * java enabled
% * figure must be visible
% * figure's "WindowStyle" should be "normal"
% * figureHandle should not be casted to double, if using HG2 (R2014b+)
%
%
% Based on https://undocumentedmatlab.com/blog/customizing-uifigures-part-1
% Written by Igor.
%
% 2019.10.27 - Initial version
%% Parse Inputs
assert(...
isscalar( uifig ) &&...
isa( uifig,'matlab.ui.Figure'),...
...
'uifigureOnTop:Bad_uifig_input',...
'%s','Provided uifig input is not an uifigure'...
);
assert(...
strcmp('on',get(uifig,'Visible')),...
'uifigureOnTop:FigInisible',...
'%s','Figure Must be Visible'...
);
assert(...
strcmp('normal',get(uifig,'WindowStyle')),...
'uifigureOnTop:FigWrongWindowStyle',...
'%s','WindowStyle Must be Normal'...
);
if ~exist('isOnTop','var'); isOnTop=true; end
assert(...
islogical( isOnTop ) && ...
isscalar( isOnTop ) || ...
isempty( isOnTop ), ...
...
'uifigureOnTop:Bad_isOnTop_input',...
'%s','Provided isOnTop input is neither boolean, nor empty'...
);
%% Flush the Event Queue of Graphic Objects and Update the Figure Window.
drawnow expose
%% Disable warnings and dig into uifigure object
% Using undocumented private properties is the only(?) way through.
% Warning 1:
% -----
% Warning: figure JavaFrame property will be obsoleted in a future
% release. <...>
% -----
warn1_id = 'MATLAB:HandleGraphics:ObsoletedProperty:JavaFrame';
prev_warn1 = warning('query',warn1_id);
clnup1=onCleanup(@() warning(prev_warn1.state,warn1_id));
warning('off',warn1_id);
% Warning 2:
% -----
% Warning: Calling STRUCT on an object prevents the object from hiding
% its implementation details <...>
% -----
warn2_id = 'MATLAB:structOnObject';
prev_warn2 = warning('query',warn2_id);
clnup2=onCleanup(@() warning(prev_warn2.state,warn2_id));
warning('off',warn2_id);
figProps = struct(uifig);
delete(clnup1);
controller = figProps.Controller;
controllerProps = struct(controller);
if isfield(controllerProps,'Container')
% older Matlab versions (see Yair's version)
container = controllerProps.Container;
else
% works in R2018b
container = struct(controllerProps.PlatformHost);
end
delete(clnup2);
%% Action
win = container.CEF;
wasOnTop = win.isAlwaysOnTop;
if isempty(wasOnTop)
wasOnTop = false;
end
if ~isempty(isOnTop)
win.setAlwaysOnTop(isOnTop);
end
end