Skip to content

Commit e3af320

Browse files
committed
Refactoring
- Cleaned up getWidgetID: separated some experimental fallback functionality into getWidgetIDFromDijit. - Made use of the WidgetID class where appropriate. - Added a warning for unsupported UI objects. - Removed incorrect comment. - Added some useful debugging code at the end. - TODO: make uniform indentation width (2 or 4). - TODO: be consistent instead of "hWin, win, hWebwindow" etc.
1 parent b8735a1 commit e3af320

File tree

1 file changed

+62
-40
lines changed

1 file changed

+62
-40
lines changed

mlapptools.m

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,23 @@ function fontWeight(uiElement, weight)
9696
end % getHTML
9797

9898
function [win, widgetID] = getWebElements(uiElement)
99-
% A method for obtaining the webwindow handle and the widgetID corresponding
99+
% A method for obtaining the webwindow handle and the widget ID corresponding
100100
% to the provided uifigure control.
101101
% Get a handle to the webwindow
102102
win = mlapptools.getWebWindow(uiElement);
103+
mlapptools.waitTillWebwindowLoaded(win);
103104

104105
% Find which element of the DOM we want to edit
105-
widgetID = mlapptools.getWidgetID(win, mlapptools.getDataTag(uiElement));
106+
switch uiElement.Type
107+
case 'uitreenode'
108+
p = uiElement.Parent;
109+
if ~isa(p,'matlab.ui.container.Tree')
110+
p.expand(); % The row must be visible to apply changes
111+
end
112+
widgetID = WidgetID('data-test-id', char(struct(uiElement).NodeId));
113+
otherwise % default:
114+
widgetID = mlapptools.getWidgetID(win, mlapptools.getDataTag(uiElement));
115+
end
106116
end % getWebElements
107117

108118
function [win] = getWebWindow(hUIObj)
@@ -198,7 +208,7 @@ function fontWeight(uiElement, weight)
198208
% 3-parameter call:
199209
% widgetID = setStyle(hControl, styleAttr, styleValue)
200210
% 4-parameter call:
201-
% setStyle(hWin, styleAttr, styleValue, ID_struct)
211+
% setStyle(hWin, styleAttr, styleValue, ID_obj)
202212

203213
narginchk(3,4);
204214
% Unpack inputs:
@@ -209,15 +219,15 @@ function fontWeight(uiElement, weight)
209219
case 3
210220
hControl = varargin{1};
211221
% Get a handle to the webwindow
212-
[win, ID_struct] = mlapptools.getWebElements(hControl);
222+
[win, ID_obj] = mlapptools.getWebElements(hControl);
213223
case 4
214-
% If we know the ID_struct, the webwindow handle must be available
224+
% By the time we have a WidgetID object, the webwindow handle is available
215225
win = varargin{1};
216-
ID_struct = varargin{4};
226+
ID_obj = varargin{4};
217227
end
218228

219229
styleSetStr = sprintf('dojo.style(dojo.query("[%s = ''%s'']")[0], "%s", "%s")',...
220-
ID_struct.ID_attr, ID_struct.ID_val, styleAttr, styleValue);
230+
ID_obj.ID_attr, ID_obj.ID_val, styleAttr, styleValue);
221231
% ^ this might result in junk if widgetId=='null'.
222232
try
223233
win.executeJS(styleSetStr);
@@ -230,7 +240,7 @@ function fontWeight(uiElement, weight)
230240

231241
% Assign outputs:
232242
if nargout >= 1
233-
varargout{1} = ID_struct;
243+
varargout{1} = ID_obj;
234244
end
235245

236246
end % setStyle
@@ -349,47 +359,52 @@ function textAlign(uiElement, alignment)
349359
hFig = hFigs(hWebwindow == ww);
350360
end % figFromWebwindow
351361

352-
function [ID_struct] = getWidgetID(win, data_tag)
362+
function [ID_obj] = getWidgetID(win, data_tag)
353363
% This method returns a structure containing some uniquely-identifying information
354364
% about a DOM node.
355365
widgetquerystr = sprintf('dojo.getAttr(dojo.query("[data-tag^=''%s''] > div")[0], "widgetid")', data_tag);
356-
mlapptools.waitTillWebwindowLoaded(win);
357366
try % should work for most UI objects
358367
ID = win.executeJS(widgetquerystr);
359-
ID_struct = struct('ID_attr', mlapptools.DEF_ID_ATTRIBUTE, 'ID_val', ID(2:end-1));
368+
ID_obj = WidgetID(mlapptools.DEF_ID_ATTRIBUTE, ID(2:end-1));
360369
catch % fallback for problematic objects
361-
% We retry by using the dijit registry:
362-
win.executeJS(['var W; require(["dijit/registry"], '...
363-
'function(registry){W = registry.toArray().map(x => x.domNode.childNodes);});']);
364-
nWidgets = jsondecode(win.executeJS('W.length'));
365-
try
366-
for ind1 = 0:nWidgets-1
367-
nChild = jsondecode(win.executeJS(sprintf('W[%d].length',ind1)));
368-
for ind2 = 0:nChild-1
369-
tmp = win.executeJS(sprintf('W[%d][%d].dataset',ind1,ind2));
370-
if isempty(tmp)
371-
continue
372-
else
373-
tmp = jsondecode(tmp);
374-
end
375-
if isfield(tmp,'tag') && strcmp(tmp.tag,data_tag)
376-
ID = win.executeJS(sprintf('dojo.getAttr(W[%d][%d].parentNode,"widgetid")',ind1,ind2));
377-
error('Bailout!');
378-
end
379-
end
370+
warning('This widget is unsupported.');
371+
% ID_obj = mlapptools.getWidgetIDFromDijit(win, data_tag);
372+
end
373+
end % getWidgetID
374+
375+
function ID_obj = getWidgetIDFromDijit(win, data_tag)
376+
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% EXPERIMENTAL METHOD!!!
377+
win.executeJS(['var W; require(["dijit/registry"], '...
378+
'function(registry){W = registry.toArray().map(x => x.domNode.childNodes);});']);
379+
nWidgets = jsondecode(win.executeJS('W.length'));
380+
try
381+
for ind1 = 0:nWidgets-1
382+
nChild = jsondecode(win.executeJS(sprintf('W[%d].length',ind1)));
383+
for ind2 = 0:nChild-1
384+
tmp = win.executeJS(sprintf('W[%d][%d].dataset',ind1,ind2));
385+
if isempty(tmp)
386+
continue
387+
else
388+
tmp = jsondecode(tmp);
380389
end
381-
catch
382-
if strcmp(tmp.type,'matlab.ui.container.TreeNode')
383-
tmp = jsondecode(win.executeJS(sprintf(...
384-
'dojo.byId(%s).childNodes[0].childNodes[0].childNodes[0].childNodes[%d].dataset',...
385-
ID(2:end-1),ind2-1)));
386-
ID_struct = struct('ID_attr', 'data-reactid', 'ID_val', tmp.reactid);
390+
if isfield(tmp,'tag') && strcmp(tmp.tag,data_tag)
391+
ID = win.executeJS(sprintf('dojo.getAttr(W[%d][%d].parentNode,"widgetid")',ind1,ind2));
392+
error('Bailout!');
387393
end
388-
% do nothing - bailout.
389394
end
390395
end
391-
392-
end % getWidgetID
396+
ID_obj = WidgetID('','');
397+
catch
398+
% Fix for the case of top-level tree nodes:
399+
switch tmp.type
400+
case 'matlab.ui.container.TreeNode'
401+
tmp = jsondecode(win.executeJS(sprintf(...
402+
'dojo.byId(%s).childNodes[0].childNodes[0].childNodes[0].childNodes[%d].dataset',...
403+
ID(2:end-1),ind2-1)));
404+
ID_obj = WidgetID('data-reactid', tmp.reactid);
405+
end
406+
end
407+
end % getWidgetIDFromDijit
393408

394409
function to = getTimeout(hFig)
395410
to = getappdata(hFig, mlapptools.TAG_TIMEOUT);
@@ -523,4 +538,11 @@ function waitTillWebwindowLoaded(hWebwindow, hFig)
523538

524539
end % Private Static Methods
525540

526-
end % classdef
541+
end % classdef
542+
543+
%{
544+
Useful debugging code:
545+
546+
jsprops = sort(jsondecode(win.executeJS('Object.keys(this)')));
547+
548+
%}

0 commit comments

Comments
 (0)