Skip to content

Commit ce8bb34

Browse files
committed
Added the "setStyle" method for setting generic styles
- Improved internal documentation of the class. - Added check for JS SyntaxError to use in setStyle. - Readme updated accordingly.
1 parent 1fc23c0 commit ce8bb34

File tree

2 files changed

+68
-7
lines changed

2 files changed

+68
-7
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ For additional information, see Iliya Romm's guest Undocumented Matlab article,
77
[`textAlign`](#textAlign) - Modify text alignment
88
[`fontWeight`](#fontWeight) - Modify font weight
99
[`fontColor`](#fontColor) - Modify font folor
10+
[`setStyle`](#setStyle) - Modify a specified style property
1011

1112
<a name="textAlign"></a>
1213
#### *mlapptools*.**textAlign**(*uielement*, *alignment*)
@@ -82,4 +83,21 @@ mlapptools.fontColor(myGUI.TextArea, 'aqua');
8283
```MATLAB
8384
myGUI = DOMdemoGUI;
8485
mlapptools.fontColor(myGUI.TextArea, 'rgb(255,165,0)');
86+
```
87+
88+
<a name="setStyle"></a>
89+
#### *mlapptools*.**setStyle**(*uielement*, *styleAttr*, *styleValue*)
90+
##### Description
91+
Set the style attribute `styleAttr` of the specified UI element, `'uielement'`, to the value `styleValue`. `styleAttr` should be any valid CSS attribute, and `styleValue` a valid setting thereof.
92+
93+
This method provides a general interface to change CSS style attributes, with minimal input testing and error reporting, so it is up to the user to provide valid inputs.
94+
95+
Valid style attributes and corresponding settings can be found [here](https://www.w3schools.com/cssref/).
96+
97+
##### Examples
98+
Using the demo GUI generated by `./Demo/DOMdemoGUI.m`
99+
```MATLAB
100+
myGUI = DOMdemoGUI;
101+
mlapptools.setStyle(myGUI.TextArea, 'background-image',...
102+
'url(https://upload.wikimedia.org/wikipedia/commons/8/80/Wikipedia-logo-v2.svg)');
85103
```

mlapptools.m

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
classdef mlapptools
2-
% MLAPPTOOLS is a class definition
2+
% MLAPPTOOLS A collection of static methods for customizing various aspects
3+
% MATLAB App Designer UIFigures.
34
%
45
% 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.
511

612
properties (Access = private, Constant = true)
713
QUERY_TIMEOUT = 5; % Dojo query timeout period, seconds
@@ -13,9 +19,10 @@
1319
clear obj
1420
end
1521
end
16-
17-
22+
1823
methods (Static)
24+
25+
methods (Access = public, Static = true)
1926
function textAlign(uielement, alignment)
2027
alignment = lower(alignment);
2128
mlapptools.validatealignmentstr(alignment)
@@ -45,10 +52,32 @@ function fontColor(uielement, newcolor)
4552
fontColorSetStr = sprintf('dojo.style(dojo.query("#%s")[0], "color", "%s")', widgetID, newcolor);
4653
win.executeJS(fontColorSetStr);
4754
end
48-
end
49-
50-
51-
methods (Static, Access = private)
55+
56+
57+
function widgetID = setStyle(hControl, styleAttr, styleValue)
58+
% This method provides a simple interface for modifying style attributes
59+
% of uicontrols.
60+
%
61+
% WARNING: Due to the large amount of available style attributes and
62+
% corresponding settings, input checking is not performed. As this
63+
% might lead to unexpected results or errors - USE AT YOUR OWN RISK!
64+
[win, widgetID] = mlapptools.getWebElements(hControl);
65+
66+
styleSetStr = sprintf('dojo.style(dojo.query("#%s")[0], "%s", "%s")', widgetID, styleAttr, styleValue);
67+
% ^ this might result in junk if widgetId=='null'.
68+
try
69+
win.executeJS(styleSetStr);
70+
% ^ this might crash in case of invalid styleAttr/styleValue.
71+
catch ME
72+
% Test for "Invalid or unexpected token":
73+
ME = mlapptools.checkJavascriptSyntaxError(ME, styleSetStr);
74+
rethrow(ME);
75+
end
76+
end
77+
78+
end % Public static methods
79+
80+
methods (Static = true, Access = private)
5281
function [win] = getWebWindow(uifigurewindow)
5382
mlapptools.togglewarnings('off')
5483
% Test if uifigurewindow is a valid handle
@@ -194,6 +223,20 @@ function validatealignmentstr(alignment)
194223

195224

196225
function [newcolor] = validateCSScolor(newcolor)
226+
% TODO
227+
end
228+
229+
230+
function ME = checkJavascriptSyntaxError(ME,styleSetStr)
231+
if (strcmp(ME.identifier,'cefclient:webwindow:jserror'))
232+
c = strfind(ME.message,'Uncaught SyntaxError:');
233+
if ~isempty(c)
234+
v = str2double(regexp(ME.message(c:end),'-?\d+\.?\d*|-?\d*\.?\d+','match'));
235+
msg = ['Syntax error: unexpected token in styleValue: ' styleSetStr(v(1),v(2))];
236+
causeException = MException('mlapptools:setStyle:invalidInputs',msg);
237+
ME = addCause(ME,causeException);
238+
end
239+
end
197240
end
198241
end
199242
end

0 commit comments

Comments
 (0)