Skip to content

Commit afd7a49

Browse files
authored
Merge pull request #130 from slootsjj/ButtonHeightWidth
Introduces ButtonHeight
2 parents 6a1d843 + 6eb5586 commit afd7a49

File tree

5 files changed

+238
-62
lines changed

5 files changed

+238
-62
lines changed

test/+wt/+test/ButtonGrid.m

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,44 @@ function testIconAlignment(testCase)
190190
testCase.verifyMatches(button.IconAlignment, newValue);
191191

192192
end %function
193+
194+
195+
function testButtonWidthHeight(testCase)
196+
197+
% Default size
198+
testCase.verifySetProperty("DefaultSize", 20, "20");
199+
testCase.verifySetProperty("DefaultSize", 'fit', "fit");
200+
testCase.verifySetProperty("DefaultSize", '2x', "2x");
201+
202+
% Horizontal layout
203+
testCase.verifySetProperty("ButtonWidth", "fit", {'fit' 'fit'})
204+
testCase.verifySetProperty("ButtonWidth", {10 'fit'}, {10 'fit'})
205+
testCase.verifySetProperty("ButtonWidth", [10 20 30 40], {10 20})
206+
207+
testCase.verifySetProperty("ButtonHeight", {10 'fit'}, {10})
208+
testCase.verifySetProperty("ButtonHeight", 20, {20})
209+
210+
% Additional buttons
211+
testCase.verifySetProperty("DefaultSize", 30, "30");
212+
testCase.verifySetProperty("Text", ["1", "2", "3", "4"], ["1", "2", "3", "4"])
213+
testCase.verifyEqual(testCase.Widget.ButtonWidth, {10 20 30 30})
214+
215+
% Change to vertical layout
216+
newOrientation = "vertical";
217+
testCase.verifySetProperty("Orientation", newOrientation);
218+
219+
% Vertical layout
220+
testCase.verifySetProperty("ButtonHeight", 30, {30 30 30 30})
221+
testCase.verifySetProperty("ButtonHeight", "fit", {'fit' 'fit' 'fit' 'fit'})
222+
testCase.verifySetProperty("ButtonHeight", [10 20], {10 20 'fit' 'fit'})
223+
testCase.verifySetProperty("ButtonHeight", {10 'fit'}, {10 'fit' 'fit' 'fit'})
224+
225+
testCase.verifySetProperty("ButtonHeight", [10 20 30 40 50], {10 20 30 40})
226+
227+
testCase.verifySetProperty("ButtonWidth", {10 'fit'}, {10})
228+
testCase.verifySetProperty("ButtonWidth", 20, {20})
229+
230+
end
193231

194232
end %methods (Test)
195233

test/+wt/+test/ListSelector.m

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,40 @@ function testButtonFunctions(testCase)
315315

316316

317317

318+
function testSortable(testCase)
319+
320+
% Get the listbox and button grid
321+
w = testCase.Widget;
322+
listControl = testCase.Widget.ListBox;
323+
buttonGrid = testCase.Widget.ListButtons;
324+
button = buttonGrid.Button;
325+
326+
% Add a list of items and put all on list
327+
testCase.verifySetProperty("Value", testCase.ItemNames);
328+
329+
% Select multiple items with mouse
330+
selIdx = [2 3];
331+
testCase.choose(listControl, selIdx)
332+
333+
% Move items further down
334+
testCase.press(button(4))
335+
testCase.press(button(4))
336+
337+
% Press down one too many
338+
testCase.press(button(4))
339+
340+
% Switch Sortable option off
341+
testCase.verifySetProperty("Sortable", false);
342+
testCase.verifyEquality(w.SelectedIndex, 1:numel(testCase.ItemNames));
343+
344+
% Switch Sortable option back on
345+
testCase.verifySetProperty("Sortable", true);
346+
testCase.verifyEquality(w.SelectedIndex, 1:numel(testCase.ItemNames));
347+
348+
end
349+
350+
351+
318352
function testUserButtons(testCase)
319353

320354
% Get the widget

widgets/+wt/ButtonGrid.m

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

45-
% Default size of new buttons ('1x' or 'fit')
46-
DefaultSize {mustBeMember(DefaultSize,{'1x','fit'})} = '1x'
45+
% Default size of new buttons ('1x', 'fit' or a number)
46+
DefaultSize (1,1) string {mustBeValidGridSize(DefaultSize)} = "1x"
4747

4848
end %properties
4949

@@ -160,13 +160,21 @@ function update(obj)
160160
end %for idx = 1:numNew
161161

162162
% 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
163171
if obj.Orientation == "vertical"
164-
obj.Grid.ColumnWidth = {obj.DefaultSize};
165-
obj.Grid.RowHeight = repmat({obj.DefaultSize},1,numNew);
172+
obj.Grid.RowHeight(numOld+1:numNew) = {defaultSize};
173+
obj.Grid.ColumnWidth = obj.Grid.ColumnWidth(1);
166174
else
167-
obj.Grid.ColumnWidth = repmat({obj.DefaultSize},1,numNew);
168-
obj.Grid.RowHeight = {obj.DefaultSize};
169-
end %if obj.Orientation == "vertical"
175+
obj.Grid.ColumnWidth(numOld+1:numNew) = {defaultSize};
176+
obj.Grid.RowHeight = obj.Grid.RowHeight(1);
177+
end
170178

171179
end %function
172180

@@ -180,6 +188,26 @@ function onButtonPushed(obj,evt)
180188

181189
end %function
182190

191+
function updateGridForButton(obj, prop, value)
192+
% Update main grid properties to value
193+
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
201+
if isscalar(value)
202+
value = repmat(value, 1, numel(obj.Grid.(prop)));
203+
end
204+
205+
% Update button size
206+
nElements = min(numel(value), numel(obj.Grid.(prop)));
207+
obj.Grid.(prop)(1:nElements) = value(1:nElements);
208+
209+
end %function
210+
183211
end %methods
184212

185213

@@ -191,17 +219,48 @@ function onButtonPushed(obj,evt)
191219
value = obj.Grid.ColumnWidth;
192220
end
193221
function set.ButtonWidth(obj,value)
194-
obj.Grid.ColumnWidth = value;
222+
obj.updateGridForButton("ColumnWidth", value);
195223
end
196224

197225
function value = get.ButtonHeight(obj)
198226
value = obj.Grid.RowHeight;
199227
end
200228
function set.ButtonHeight(obj,value)
201-
obj.Grid.RowHeight = value;
229+
obj.updateGridForButton("RowHeight", value);
202230
end
203231

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)