Skip to content

Commit f73b566

Browse files
committed
Add font weight adjustment, support for defaults
Add 'initial' parameter to supported strings for existing methods so the user has the ability to revert changes to MATLAB's default
1 parent f9fe47a commit f73b566

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

mlapptools.m

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,35 @@ function textAlign(uielement, alignment)
3434
pause(1); % Give the figure (webpage) some more time to load
3535
end
3636
end
37+
end
38+
39+
40+
function fontWeight(uielement, weight)
41+
weight = mlapptools.validatefontweight(weight);
3742

43+
wt = '';
44+
while ~strcmp(wt, sprintf('"%s"', weight))
45+
try
46+
% Get a handle to the webwindow
47+
win = mlapptools.getwebwindow(uielement.Parent);
48+
49+
% Find which element of the DOM we want to edit
50+
data_tag = mlapptools.getdatatag(uielement);
51+
52+
% Manipulate the DOM via a JS command
53+
widgetID = mlapptools.getwidgetID(win, data_tag);
54+
55+
fontwtsetstr = sprintf('dojo.style(dojo.query("#%s")[0], "font-weight", "%s")', widgetID, weight);
56+
wt = win.executeJS(fontwtsetstr);
57+
catch err
58+
% TODO: Check the error so we're not catching errors indiscriminately
59+
pause(1); % Give the figure (webpage) some more time to load
60+
end
61+
end
3862
end
3963
end
4064

65+
4166
methods (Static, Access = private)
4267
function [win] = getwebwindow(uifigurewindow)
4368
% TODO: Check that we've been passed an app designer figure window
@@ -78,11 +103,34 @@ function validatealignmentstr(alignment)
78103
class('Dev-il'), class(alignment));
79104
end
80105

81-
validstr = {'left', 'right', 'center', 'justify'};
106+
validstr = {'left', 'right', 'center', 'justify', 'initial'};
82107
if ~any(ismember(validstr, alignment))
83108
msgID = 'mlapptools:alignstring:InvalidAlignmentString';
84109
error(msgID, 'Invalid string alignment specified: ''%s''', alignment);
85110
end
86111
end
112+
113+
function [weight] = validatefontweight(weight)
114+
if ischar(weight)
115+
weight = lower(weight);
116+
validstrs = {'normal', 'bold', 'bolder', 'lighter', 'initial'};
117+
118+
if ~any(ismember(weight, validstrs))
119+
msgID = 'mlapptools:fontWeight:InvalidFontWeightString';
120+
error(msgID, 'Invalid font weight specified: ''%s''', alignment);
121+
end
122+
elseif isnumeric(weight)
123+
weight = round(weight, -2);
124+
if weight < 100
125+
weight = 100;
126+
elseif weight > 900
127+
weight = 900;
128+
end
129+
130+
weight = num2str(weight);
131+
else
132+
% Throw error
133+
end
134+
end
87135
end
88136
end

0 commit comments

Comments
 (0)