You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
while/try/catch block for UIFigure presence too inclusive #1
The while/try/catch block used to wait for the existence of the parent UIFigure seems overly inclusive and the condition for exiting the block can lead to false positives.
e.g.
rez ='';
while ~strcmp(rez, sprintf('"%s"', alignment))
try% Get a handle to the webwindow
win =mlapptools.getwebwindow(uielement.Parent);
% Find which element of the DOM we want to edit
data_tag =mlapptools.getdatatag(uielement);
% Manipulate the DOM via a JS command
widgetID =mlapptools.getwidgetID(win, data_tag);
alignsetstr = sprintf('dojo.style(dojo.query("#%s")[0], "textAlign", "%s")', widgetID, alignment);
rez =win.executeJS(alignsetstr);
catcherr
pause(1); % Give the figure (webpage) some more time to loadendend
There is likely a better way to query and wait for the existence of the figure window, which can be shifted into a generic helper method.
The while block can also lead to false positives, such as the situation resolved in cb9c465. Though we were operating on the incorrect DOM, textAlign would execute without error/warning because we're setting a "meaningless" property which is enough to satisfy the while criteria.
We could respond to a "page finished" callback that is issued by the CEF (browser) asynchronously from MATLAB's main thread (I didn't know MATLAB could handle that tbh). So in our app code we define a flag that tracks the loading state:
properties (GetAccess=public, SetAccess =private)
didPageFinishLoading =false;
end
Next we define a private function that can set this value to true:
function confirmLoadingFinished(app,varargin)
app.didPageFinishLoading =true;
end
Then we set the above function as our MATLAB-side callback using:
function startupFcn(app)
TIMEOUT =10; tic;
whiletrue&& (toc<TIMEOUT)
try
win = struct(struct(struct(app).UIFigure).Controller).Container.CEF;
win.PageLoadFinishedCallback = @app.confirmLoadingFinished;
break;
catchex
disp(['Not ready yet! Error: 'ex.message]);
pause(0.5); % Give the figure (webpage) some more time to load, but not too much so we % don't miss the opportunity to set the callback.endend
TIMEOUT =10; tic; %[sec], safety measure for missing the callback.while ~app.didPageFinishLoading && (toc<TIMEOUT)
pause(0.1);
endifapp.didPageFinishLoading
pause(0.1); % Can't explain why, but this is required. Might not be enough.
applyStyles(app, win); % this is when all data_tag / widgetId / dojo things happenelse
warning('Timeout reached! Styles not applied!');
endend
P.S. 1
Instead of timeout "as time" one can define a timeout as a number of iterations:
N_ITER_BEFORE_TIMEOUT =100;
for ind =1:N_ITER_BEFORE_TIMEOUT
assert(ind<=N_ITER_BEFORE_TIMEOUT,'Timeout reached!');
try% Get a handle to the webwindow
... % (same as before)endend
The strcmp(rez, sprintf('"%s"', alignment)) test can be moved to the end of the function to report if the manipulation [seems to have] worked.
P.S. 2
If you want syntax highlighting in your code blocks, start them with " matlab" instead of just ""
I definitely like the idea of the callback, but you end up still needing at least one of the try/catch blocks and you also have the additional requirement of adding a property and method to every app we'd want to have robust handling in.
I feel like there has to be a way to query this directly from the DOM, given the existence of dojo/ready and dojo/domReady! the capability clearly exists, I just have no idea how to get to it from MATLAB.
There are 3 conditions that we seem to be racing:
UIFigure container getting completely rendered
MATLAB:nonExistentField
Reference to non-existent field 'Container'.
Once the UIFigure window is completely rendered, figurewindow.Controller becomes non-empty so we can obtain the webwindow object from figurewindow.Controller.Container.CEF. This webwindow object handle is needed for the above loading confirmation callback.
Dojo not initialized
cefclient:webwindow:jserror
Error executing JavaScript command:
JavaScript error: Uncaught ReferenceError: dojo is not defined at line 1 column 0 in undefined
Once we go to obtain the widget ID, we're racing Dojo's initialization
UI Object's web element not initialized
cefclient:webwindow:jserror
Error executing JavaScript command:
JavaScript error: Uncaught TypeError: Cannot read property 'widgetid' of null at line 152 column 68 in http://localhost:31415/toolbox/matlab/uitools/uifigureappjs/release/dojo/dojo.js
Once we get past Dojo initializing, now we're racing the initialization of the web element itself
@sco1 I'd like to mention that I initially tried including a call to dojo.version while waiting for win to become available, however this didn't work, as the availability of dojo happens before the elements are completely initialized, thus giving the "null element" error you mentioned.
It seems you found something better for checking the readiness of the DOM. If however, your suggestion has the same effect, I can't think of anything other than the "callback + pause" combination, or try to query some element in a loop until it succeeds.
Activity
Dev-iL commentedon Aug 21, 2016
We could respond to a "page finished" callback that is issued by the CEF (browser) asynchronously from MATLAB's main thread (I didn't know MATLAB could handle that tbh). So in our app code we define a flag that tracks the loading state:
Next we define a
privatefunction that can set this value totrue:Then we set the above function as our MATLAB-side callback using:
And we get:
P.S. 1
Instead of timeout "as time" one can define a timeout as a number of iterations:
The
strcmp(rez, sprintf('"%s"', alignment))test can be moved to the end of the function to report if the manipulation [seems to have] worked.P.S. 2
If you want syntax highlighting in your code blocks, start them with "
matlab" instead of just ""Add polling for widget & webwindow presence
Fix logic errors, add another WidgetID race
sco1 commentedon Aug 22, 2016
I definitely like the idea of the callback, but you end up still needing at least one of the try/catch blocks and you also have the additional requirement of adding a property and method to every app we'd want to have robust handling in.
I feel like there has to be a way to query this directly from the DOM, given the existence of
dojo/readyanddojo/domReady!the capability clearly exists, I just have no idea how to get to it from MATLAB.There are 3 conditions that we seem to be racing:
UIFigurecontainer getting completely renderedOnce the
UIFigurewindow is completely rendered,figurewindow.Controllerbecomes non-empty so we can obtain thewebwindowobject fromfigurewindow.Controller.Container.CEF. Thiswebwindowobject handle is needed for the above loading confirmation callback.Once we go to obtain the widget ID, we're racing Dojo's initialization
Once we get past Dojo initializing, now we're racing the initialization of the web element itself
The testing code:
Dev-iL commentedon Aug 22, 2016
@sco1 I'd like to mention that I initially tried including a call to
dojo.versionwhile waiting forwinto become available, however this didn't work, as the availability ofdojohappens before the elements are completely initialized, thus giving the "null element" error you mentioned.It seems you found something better for checking the readiness of the DOM. If however, your suggestion has the same effect, I can't think of anything other than the "callback + pause" combination, or
tryto query some element in a loop until it succeeds.Merge pull request #1 from Dev-iL/feature-uitable-support
Merge pull request #8 from Dev-iL/develop
sco1 commentedon Jan 7, 2018
Should be fixed with #8 (947c54e)