Skip to content

Commit 59d794a

Browse files
committed
Added the setTimeout public method for modifying dojo timeout.
1 parent 3bd1037 commit 59d794a

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ published Wednesday, September 7th, 2016.
2424
[`getWebWindow`](#getWebWindow) - Extract a `webwindow` handle from a `uifigure` handle.
2525
[`getWidgetInfo`](#getWidgetInfo) - Get a list of widgets from the dijit registry.
2626
[`setStyle`](#setStyle) - Modify a specified style property.
27+
[`setTimeout`](#setTimeout) - Override the default timeout for dojo commands, for a specific `uifigure`.
2728
[`textAlign`](#textAlign) - Modify text alignment.
2829

2930
<a name="aboutDojo"></a>
@@ -109,8 +110,8 @@ mlapptools.fontWeight(myGUI.TextArea, 600);
109110
#### *mlapptools*.**getHTML**(*hUIFigure*)
110111
##### Description
111112
A method for obtaining the HTML code of a uifigure. Intended for R2017b (and onward?) where the CEF URL cannot be
112-
simply opened in a browser. The returned HTML is a deep copy, meaning that any changes will **not** modify the `uifigure`
113-
where it originated from.
113+
simply opened in a browser. The returned HTML is a deep copy, meaning that any changes will **not** modify the
114+
`uifigure` where it originated from.
114115

115116
##### Examples
116117
Using the demo GUI generated by `./Demo/DOMdemoGUI.m`
@@ -181,10 +182,24 @@ mlapptools.setStyle(myGUI.TextArea, 'background-image',...
181182
```
182183

183184

185+
<a name="setTimeout"></a>
186+
#### *mlapptools*.**setTimeout**(*hUIFig*)
187+
##### Description
188+
Modify the amount of time allotted to dojo queries before they are considered "failed due to timeout". This value is
189+
`uifigure`-specific. If left unmodified, the default value is `5` seconds.
190+
191+
##### Examples
192+
```MATLAB
193+
myGUI = DOMdemoGUI;
194+
mlapptools.setTimeout(myGUI, 3); % This will wait less for dojo queries to finish.
195+
```
196+
197+
184198
<a name="textAlign"></a>
185199
#### *mlapptools*.**textAlign**(*uiElement*, *alignment*)
186200
##### Description
187-
Set the horizontal text alignment of the specified UI element, `uiElement`, to that specified by the input alignment string, `alignment`.
201+
Set the horizontal text alignment of the specified UI element, `uiElement`, to that specified by the input alignment
202+
string, `alignment`.
188203

189204
Valid alignment strings are:
190205
* `'left'` - Left align (default)

mlapptools.m

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ function fontWeight(uiElement, weight)
9797
'mlapptools:getWebWindow:NotUIFigure',...
9898
'The provided window handle is not of a UIFigure.');
9999

100+
to = mlapptools.getTimeout(hUIFig);
100101
tic
101-
while true && (toc < mlapptools.QUERY_TIMEOUT)
102+
while true && (toc < to)
102103
try
103104
hController = struct(struct(hUIFig).Controller);
104105
% Check for Controller version:
@@ -120,24 +121,24 @@ function fontWeight(uiElement, weight)
120121
end
121122
mlapptools.toggleWarnings('on')
122123

123-
if toc >= mlapptools.QUERY_TIMEOUT
124+
if toc >= to
124125
msgID = 'mlapptools:getWidgetID:QueryTimeout';
125126
error(msgID, ...
126127
'WidgetID query timed out after %u seconds, UI needs more time to load', ...
127-
mlapptools.QUERY_TIMEOUT);
128+
to);
128129
end
129130
end % getWebWindow
130131

131-
function varargout = getWidgetInfo(hUIFig,verbose)
132+
function varargout = getWidgetInfo(hUIFig, verboseFlag)
132133
% A method for gathering information about dijit widgets.
133134

134135
%% Handle missing inputs:
135136
if nargin < 1 || isempty(hUIFig)
136137
throw(MException('getWidgetInfo:noHandleProvided',...
137138
'Please provide a valid UIFigure handle as a first input.'));
138139
end
139-
if nargin < 2 || isempty(verbose)
140-
verbose = false;
140+
if nargin < 2 || isempty(verboseFlag)
141+
verboseFlag = false;
141142
end
142143
%%
143144
win = mlapptools.getWebWindow(hUIFig);
@@ -149,7 +150,7 @@ function fontWeight(uiElement, weight)
149150
try
150151
widgets{ind1} = jsondecode(win.executeJS(sprintf('W[%d]', ind1)));
151152
catch % handle circular references:
152-
if verbose
153+
if verboseFlag
153154
disp(['Node #' num2str(ind1-1) ' with id ' win.executeJS(sprintf('W[%d].id', ind1-1))...
154155
' could not be fully converted. Attempting fallback...']);
155156
end
@@ -223,6 +224,11 @@ function fontWeight(uiElement, weight)
223224
end
224225

225226
end % setStyle
227+
228+
function setTimeout(hUIFig, newTimeoutInSec)
229+
% Sets a custom timeout for dojo queries, specified in [s].
230+
setappdata(hUIFig, 'QUERY_TIMEOUT', newTimeoutInSec);
231+
end
226232

227233
function textAlign(uiElement, alignment)
228234
% A method for manipulating text alignment.
@@ -271,8 +277,9 @@ function textAlign(uiElement, alignment)
271277
function [widgetID] = getWidgetID(win, data_tag)
272278
widgetquerystr = sprintf('dojo.getAttr(dojo.query("[data-tag^=''%s''] > div")[0], "widgetid")', data_tag);
273279

280+
to = mlapptools.getTimeout(mlapptools.figFromWebwindow(win));
274281
tic
275-
while true && (toc < mlapptools.QUERY_TIMEOUT)
282+
while true && (toc < to)
276283
try
277284
widgetID = win.executeJS(widgetquerystr);
278285
widgetID = widgetID(2:end-1);
@@ -289,14 +296,19 @@ function textAlign(uiElement, alignment)
289296
end
290297
mlapptools.toggleWarnings('on')
291298

292-
if toc >= mlapptools.QUERY_TIMEOUT
299+
if toc >= to
293300
msgID = 'mlapptools:getWidgetID:QueryTimeout';
294301
error(msgID, ...
295302
'widgetID query timed out after %u seconds, UI needs more time to load', ...
296-
mlapptools.QUERY_TIMEOUT);
303+
to);
297304
end
298305
end % getWidgetID
299306

307+
function to = getTimeout(hFig)
308+
to = getappdata(hFig,'QUERY_TIMEOUT');
309+
if isempty(to), to = mlapptools.QUERY_TIMEOUT; end
310+
end
311+
300312
function tf = isUIFigure(hList)
301313
tf = arrayfun(@(x)isa(x,'matlab.ui.Figure') && ...
302314
isstruct(struct(x).ControllerInfo), hList);
@@ -371,6 +383,15 @@ function validateAlignmentStr(alignment)
371383
error(msgID, 'Invalid font weight specified: ''%s''', weight);
372384
end
373385
end % validateFontWeight
386+
387+
function hFig = figFromWebwindow(hWebwindow)
388+
hFigs = findall(groot, 'Type', 'figure');
389+
mlapptools.toggleWarnings('off');
390+
hUIFigs = hFigs(arrayfun(@(x)isstruct(struct(x).ControllerInfo), hFigs));
391+
ww = arrayfun(@mlapptools.getWebWindow, hUIFigs);
392+
mlapptools.toggleWarnings('on');
393+
hFig = hFigs(hWebwindow == ww);
394+
end
374395

375396
end % Private Static Methods
376397

0 commit comments

Comments
 (0)