Skip to content

Commit f2437de

Browse files
committed
Introduce DefaultSize property again in ButtonGrid
1 parent 9fedaef commit f2437de

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

widgets/+wt/ButtonGrid.m

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
% Alignment of the icon
4343
IconAlignment (1,1) wt.enum.AlignmentState = wt.enum.AlignmentState.top
4444

45+
% Default size of new buttons ('1x', 'fit' or a number)
46+
DefaultSize (1,1) string {mustBeValidGridSize(DefaultSize)} = "1x"
47+
4548
end %properties
4649

4750

@@ -156,6 +159,23 @@ function update(obj)
156159

157160
end %for idx = 1:numNew
158161

162+
% Update layout
163+
164+
% What is the default size?
165+
defaultSize = obj.DefaultSize;
166+
if ~isnan(str2double(defaultSize))
167+
defaultSize = str2double(defaultSize);
168+
end
169+
170+
% Set button grids
171+
if obj.Orientation == "vertical"
172+
obj.Grid.RowHeight(numOld+1:numNew) = {defaultSize};
173+
obj.Grid.ColumnWidth = obj.Grid.ColumnWidth(1);
174+
else
175+
obj.Grid.ColumnWidth(numOld+1:numNew) = {defaultSize};
176+
obj.Grid.RowHeight = obj.Grid.RowHeight(1);
177+
end
178+
159179
end %function
160180

161181

@@ -171,12 +191,20 @@ function onButtonPushed(obj,evt)
171191
function updateGridForButton(obj, prop, value)
172192
% Update main grid properties to value
173193

174-
% If value is a scalar, repeat it for every button.
194+
% Convert any text array or numeric array into a cell array
195+
value = convertCharsToStrings(value);
196+
if ~iscell(value)
197+
value = num2cell(value);
198+
end
199+
200+
% If cell is scalar, repeat value for every button
175201
if isscalar(value)
176-
nCells = numel(obj.Grid.(prop));
177-
value = repmat(value, 1, nCells);
202+
value = repmat(value, 1, numel(obj.Grid.(prop)));
178203
end
179-
obj.Grid.(prop) = value;
204+
205+
% Update button size
206+
nElements = min(numel(value), numel(obj.Grid.(prop)));
207+
obj.Grid.(prop)(1:nElements) = value(1:nElements);
180208

181209
end %function
182210

@@ -204,4 +232,35 @@ function updateGridForButton(obj, prop, value)
204232
end %methods
205233

206234

207-
end % classdef
235+
end % classdef
236+
237+
238+
function mustBeValidGridSize(val)
239+
% Validate value is valid size for grid layout
240+
241+
% Value must be either 'fit' or '1x', or convertable to a number.
242+
numVal = str2double(val);
243+
244+
% Is value convertable to a number?
245+
if ~isnan(numVal)
246+
return
247+
end
248+
249+
% Is value "fit"?
250+
if strcmpi(val, "fit")
251+
return
252+
end
253+
254+
% Is value a 1x, 2x, etc?
255+
valStripped = strip(val, "x");
256+
numStripped = str2double(valStripped);
257+
if ~isnan(numStripped)
258+
return
259+
end
260+
261+
% Value was not valid. Throw a validation error
262+
ME = MException('ButtonGrid:InvalidSize', ...
263+
'Value must be a text scalar specifying the keyword ''fit'', numbers, or numbers paired with ''x'' characters.');
264+
throwAsCaller(ME);
265+
266+
end

0 commit comments

Comments
 (0)