1
- classdef mlapptools
2
- % MLAPPTOOLS is a class definition
1
+ classdef (Abstract ) mlapptools
2
+ % MLAPPTOOLS A collection of static methods for customizing various aspects
3
+ % MATLAB App Designer UIFigures.
3
4
%
4
5
% MLAPPTOOLS methods:
6
+ % textAlign - utility method for modifying text alignment.
7
+ % fontWeight - utility method for modifying font weight (bold etc.).
8
+ % fontColor - utility method for modifying font color.
9
+ % setStyle - utility method for modifying styles that do not (yet) have a
10
+ % dedicated mutator.
5
11
6
- properties (Constant )
7
- querytimeout = 5 ; % Dojo query timeout period, seconds
12
+ properties (Access = private , Constant = true )
13
+ QUERY_TIMEOUT = 5 ; % Dojo query timeout period, seconds
8
14
end
9
-
10
- methods
11
- function obj = mlapptools
12
- % Dummy constructor so we don't return an empty class instance
13
- clear obj
14
- end
15
- end
16
-
17
-
18
- methods (Static )
15
+
16
+ methods (Access = public , Static = true )
19
17
function textAlign(uielement , alignment )
20
18
alignment = lower(alignment );
21
19
mlapptools .validatealignmentstr(alignment )
22
20
23
21
[win , widgetID ] = mlapptools .getWebElements(uielement );
24
22
25
- alignsetstr = sprintf(' dojo.style(dojo.query("#%s ")[0], "textAlign", "%s ")' , widgetID , alignment );
26
- win .executeJS(alignsetstr );
23
+ alignSetStr = sprintf(' dojo.style(dojo.query("#%s ")[0], "textAlign", "%s ")' , widgetID , alignment );
24
+ win .executeJS(alignSetStr );
27
25
end
28
26
29
27
@@ -32,8 +30,8 @@ function fontWeight(uielement, weight)
32
30
33
31
[win , widgetID ] = mlapptools .getWebElements(uielement );
34
32
35
- fontwtsetstr = sprintf(' dojo.style(dojo.query("#%s ")[0], "font-weight", "%s ")' , widgetID , weight );
36
- win .executeJS(fontwtsetstr );
33
+ fontWeightSetStr = sprintf(' dojo.style(dojo.query("#%s ")[0], "font-weight", "%s ")' , widgetID , weight );
34
+ win .executeJS(fontWeightSetStr );
37
35
end
38
36
39
37
@@ -42,25 +40,54 @@ function fontColor(uielement, newcolor)
42
40
43
41
[win , widgetID ] = mlapptools .getWebElements(uielement );
44
42
45
- fontwtsetstr = sprintf(' dojo.style(dojo.query("#%s ")[0], "color", "%s ")' , widgetID , newcolor );
46
- win .executeJS(fontwtsetstr );
43
+ fontColorSetStr = sprintf(' dojo.style(dojo.query("#%s ")[0], "color", "%s ")' , widgetID , newcolor );
44
+ win .executeJS(fontColorSetStr );
47
45
end
48
- end
49
-
50
-
51
- methods (Static , Access = private )
46
+
47
+
48
+ function widgetID = setStyle(hControl , styleAttr , styleValue )
49
+ % This method provides a simple interface for modifying style attributes
50
+ % of uicontrols.
51
+ %
52
+ % WARNING: Due to the large amount of available style attributes and
53
+ % corresponding settings, input checking is not performed. As this
54
+ % might lead to unexpected results or errors - USE AT YOUR OWN RISK!
55
+ [win , widgetID ] = mlapptools .getWebElements(hControl );
56
+
57
+ styleSetStr = sprintf(' dojo.style(dojo.query("#%s ")[0], "%s ", "%s ")' , widgetID , styleAttr , styleValue );
58
+ % ^ this might result in junk if widgetId=='null'.
59
+ try
60
+ win .executeJS(styleSetStr );
61
+ % ^ this might crash in case of invalid styleAttr/styleValue.
62
+ catch ME
63
+ % Test for "Invalid or unexpected token":
64
+ ME = mlapptools .checkJavascriptSyntaxError(ME , styleSetStr );
65
+ rethrow(ME );
66
+ end
67
+ end
68
+
69
+ end % Public static methods
70
+
71
+ methods (Static = true , Access = private )
52
72
function [win ] = getWebWindow(uifigurewindow )
53
- % TODO: Check that we've been passed an app designer figure window
54
73
mlapptools .togglewarnings(' off' )
74
+ % Test if uifigurewindow is a valid handle
75
+ if ~isa(uifigurewindow ,' matlab.ui.Figure' ) || ...
76
+ isempty(struct(uifigurewindow ).ControllerInfo)
77
+ msgID = ' mlapptools:getWebWindow:NotUIFigure' ;
78
+ error(msgID , ' The provided window handle is not of a UIFigure.' );
79
+ end
55
80
56
81
tic
57
- while true && (toc < mlapptools .querytimeout )
82
+ while true && (toc < mlapptools .QUERY_TIMEOUT )
58
83
try
59
- % Add check for container change in R2017a (version 9.2)
60
- if verLessThan(' matlab' , ' 9.2' )
61
- win = struct(struct(uifigurewindow ).Controller).Container.CEF;
62
- else
63
- win = struct(struct(struct(uifigurewindow ).Controller).PlatformHost).CEF;
84
+ hController = struct(struct(uifigurewindow ).Controller);
85
+ % Check for Controller version:
86
+ switch subsref(ver(' matlab' ), substruct(' .' ,' Version' ))
87
+ case {' 9.0' ,' 9.1' } % R2016a or R2016b
88
+ win = hController .Container .CEF ;
89
+ otherwise % R2017a onward
90
+ win = struct(hController .PlatformHost ).CEF;
64
91
end
65
92
break
66
93
catch err
@@ -74,11 +101,11 @@ function fontColor(uielement, newcolor)
74
101
end
75
102
mlapptools .togglewarnings(' on' )
76
103
77
- if toc >= mlapptools .querytimeout
104
+ if toc >= mlapptools .QUERY_TIMEOUT
78
105
msgID = ' mlapptools:getWidgetID:QueryTimeout' ;
79
106
error(msgID , ...
80
107
' WidgetID query timed out after %u seconds, UI needs more time to load' , ...
81
- mlapptools .querytimeout );
108
+ mlapptools .QUERY_TIMEOUT );
82
109
end
83
110
end
84
111
@@ -94,7 +121,7 @@ function fontColor(uielement, newcolor)
94
121
widgetquerystr = sprintf(' dojo.getAttr(dojo.query("[data-tag^=''%s'' ] > div")[0], "widgetid")' , data_tag );
95
122
96
123
tic
97
- while true && (toc < mlapptools .querytimeout )
124
+ while true && (toc < mlapptools .QUERY_TIMEOUT )
98
125
try
99
126
widgetID = win .executeJS(widgetquerystr );
100
127
widgetID = widgetID(2 : end - 1 );
@@ -111,11 +138,11 @@ function fontColor(uielement, newcolor)
111
138
end
112
139
mlapptools .togglewarnings(' on' )
113
140
114
- if toc >= mlapptools .querytimeout
141
+ if toc >= mlapptools .QUERY_TIMEOUT
115
142
msgID = ' mlapptools:getWidgetID:QueryTimeout' ;
116
143
error(msgID , ...
117
144
' WidgetID query timed out after %u seconds, UI needs more time to load' , ...
118
- mlapptools .querytimeout );
145
+ mlapptools .QUERY_TIMEOUT );
119
146
end
120
147
end
121
148
@@ -187,6 +214,20 @@ function validatealignmentstr(alignment)
187
214
188
215
189
216
function [newcolor ] = validateCSScolor(newcolor )
217
+ % TODO
218
+ end
219
+
220
+
221
+ function ME = checkJavascriptSyntaxError(ME ,styleSetStr )
222
+ if (strcmp(ME .identifier ,' cefclient:webwindow:jserror' ))
223
+ c = strfind(ME .message ,' Uncaught SyntaxError:' );
224
+ if ~isempty(c )
225
+ v = str2double(regexp(ME .message(c : end ),' -?\d+\.?\d*|-?\d*\.?\d+' ,' match' ));
226
+ msg = [' Syntax error: unexpected token in styleValue: ' styleSetStr(v(1 ),v(2 ))];
227
+ causeException = MException(' mlapptools:setStyle:invalidInputs' ,msg );
228
+ ME = addCause(ME ,causeException );
229
+ end
230
+ end
190
231
end
191
232
end
192
233
end
0 commit comments