Skip to content

Commit d3a76ba

Browse files
authored
Merge pull request #206 from jtreminio/feature/item-style-2
Checkbox + Radio item-level styling - CR Changes
2 parents 0c48311 + 8179b35 commit d3a76ba

File tree

11 files changed

+404
-251
lines changed

11 files changed

+404
-251
lines changed

examples/checkbox-item.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
})
2424
->addSubMenu('Interpreted', function (CliMenuBuilder $b) use ($itemCallable) {
2525
$b->setTitle('Interpreted Languages')
26-
->checkboxStyle(function (CheckboxStyle $style) {
26+
->modifyCheckboxStyle(function (CheckboxStyle $style) {
2727
$style->setMarkerOff('[○] ')
2828
->setMarkerOn('[●] ');
2929
})

examples/radio-item.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
})
2424
->addSubMenu('Interpreted', function (CliMenuBuilder $b) use ($itemCallable) {
2525
$b->setTitle('Interpreted Languages')
26-
->radioStyle(function (RadioStyle $style) {
26+
->modifyRadioStyle(function (RadioStyle $style) {
2727
$style->setMarkerOff('[ ] ')
2828
->setMarkerOn('[✔] ');
2929
})

src/Builder/CliMenuBuilder.php

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,12 @@ public function addSubMenu(string $text, \Closure $callback) : self
202202
$menu->setStyle($this->menu->getStyle());
203203
}
204204

205-
if (!$menu->getCheckboxStyle()->getIsCustom()) {
206-
$menu->checkboxStyle(function (CheckboxStyle $style) {
207-
$style->fromArray($this->menu->getCheckboxStyle()->toArray());
208-
});
205+
if (!$menu->getCheckboxStyle()->hasChangedFromDefaults()) {
206+
$menu->setCheckboxStyle(clone $this->menu->getCheckboxStyle());
209207
}
210208

211-
if (!$menu->getRadioStyle()->getIsCustom()) {
212-
$menu->radioStyle(function (RadioStyle $style) {
213-
$style->fromArray($this->menu->getRadioStyle()->toArray());
214-
});
209+
if (!$menu->getRadioStyle()->hasChangedFromDefaults()) {
210+
$menu->setRadioStyle(clone $this->menu->getRadioStyle());
215211
}
216212

217213
$this->menu->addItem($item = new MenuMenuItem(
@@ -236,13 +232,13 @@ public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : s
236232
$menu->setStyle($this->menu->getStyle());
237233
}
238234

239-
$menu->checkboxStyle(function (CheckboxStyle $style) {
240-
$style->fromArray($this->menu->getCheckboxStyle()->toArray());
241-
});
235+
if (!$menu->getCheckboxStyle()->hasChangedFromDefaults()) {
236+
$menu->setCheckboxStyle(clone $this->menu->getCheckboxStyle());
237+
}
242238

243-
$menu->radioStyle(function (RadioStyle $style) {
244-
$style->fromArray($this->menu->getRadioStyle()->toArray());
245-
});
239+
if (!$menu->getRadioStyle()->hasChangedFromDefaults()) {
240+
$menu->setRadioStyle(clone $this->menu->getRadioStyle());
241+
}
246242

247243
$this->menu->addItem($item = new MenuMenuItem(
248244
$text,
@@ -331,13 +327,13 @@ public function addSplitItem(\Closure $callback) : self
331327

332328
$callback($builder);
333329

334-
$builder->checkboxStyle(function (CheckboxStyle $style) {
335-
$style->fromArray($this->menu->getCheckboxStyle()->toArray());
336-
});
330+
if (!$builder->getCheckboxStyle()->hasChangedFromDefaults()) {
331+
$builder->setCheckboxStyle(clone $this->menu->getCheckboxStyle());
332+
}
337333

338-
$builder->radioStyle(function (RadioStyle $style) {
339-
$style->fromArray($this->menu->getRadioStyle()->toArray());
340-
});
334+
if (!$builder->getRadioStyle()->hasChangedFromDefaults()) {
335+
$builder->setRadioStyle(clone $this->menu->getRadioStyle());
336+
}
341337

342338
$this->menu->addItem($splitItem = $builder->build());
343339

@@ -567,16 +563,40 @@ public function build() : CliMenu
567563
return $this->menu;
568564
}
569565

570-
public function checkboxStyle(callable $itemCallable) : self
566+
public function getCheckboxStyle() : CheckboxStyle
567+
{
568+
return $this->menu->getCheckboxStyle();
569+
}
570+
571+
public function setCheckboxStyle(CheckboxStyle $style) : self
572+
{
573+
$this->menu->setCheckboxStyle($style);
574+
575+
return $this;
576+
}
577+
578+
public function modifyCheckboxStyle(callable $itemCallable) : self
579+
{
580+
$itemCallable($this->menu->getCheckboxStyle());
581+
582+
return $this;
583+
}
584+
585+
public function getRadioStyle() : RadioStyle
586+
{
587+
return $this->menu->getRadioStyle();
588+
}
589+
590+
public function setRadioStyle(RadioStyle $style) : self
571591
{
572-
$this->menu->checkboxStyle($itemCallable);
592+
$this->menu->setRadioStyle($style);
573593

574594
return $this;
575595
}
576596

577-
public function radioStyle(callable $itemCallable) : self
597+
public function modifyRadioStyle(callable $itemCallable) : self
578598
{
579-
$this->menu->radioStyle($itemCallable);
599+
$itemCallable($this->menu->getRadioStyle());
580600

581601
return $this;
582602
}

src/Builder/SplitItemBuilder.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ public function addSubMenu(string $text, \Closure $callback) : self
129129
$menu = $builder->build();
130130
$menu->setParent($this->menu);
131131

132-
$menu->checkboxStyle(function (CheckboxStyle $style) {
133-
$style->fromArray($this->menu->getCheckboxStyle()->toArray());
134-
});
132+
if (!$menu->getCheckboxStyle()->hasChangedFromDefaults()) {
133+
$menu->setCheckboxStyle(clone $this->menu->getCheckboxStyle());
134+
}
135135

136-
$menu->radioStyle(function (RadioStyle $style) {
137-
$style->fromArray($this->menu->getRadioStyle()->toArray());
138-
});
136+
if (!$menu->getRadioStyle()->hasChangedFromDefaults()) {
137+
$menu->setRadioStyle(clone $this->menu->getRadioStyle());
138+
}
139139

140140
$this->splitItem->addItem(new MenuMenuItem(
141141
$text,
@@ -169,16 +169,40 @@ public function build() : SplitItem
169169
return $this->splitItem;
170170
}
171171

172-
public function checkboxStyle(callable $itemCallable) : self
172+
public function getCheckboxStyle() : CheckboxStyle
173+
{
174+
return $this->checkboxStyle;
175+
}
176+
177+
public function setCheckboxStyle(CheckboxStyle $style) : self
178+
{
179+
$this->checkboxStyle = $style;
180+
181+
return $this;
182+
}
183+
184+
public function modifyCheckboxStyle(callable $itemCallable) : self
185+
{
186+
$itemCallable($this->menu->getCheckboxStyle());
187+
188+
return $this;
189+
}
190+
191+
public function getRadioStyle() : RadioStyle
192+
{
193+
return $this->radioStyle;
194+
}
195+
196+
public function setRadioStyle(RadioStyle $style) : self
173197
{
174-
$this->menu->checkboxStyle($itemCallable);
198+
$this->radioStyle = $style;
175199

176200
return $this;
177201
}
178202

179-
public function radioStyle(callable $itemCallable) : self
203+
public function modifyRadioStyle(callable $itemCallable) : self
180204
{
181-
$this->menu->radioStyle($itemCallable);
205+
$itemCallable($this->menu->getRadioStyle());
182206

183207
return $this;
184208
}

src/CliMenu.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,9 @@ public function getCheckboxStyle() : CheckboxStyle
660660
return $this->checkboxStyle;
661661
}
662662

663-
public function checkboxStyle(callable $itemCallable) : self
663+
public function setCheckboxStyle(CheckboxStyle $style) : self
664664
{
665-
$itemCallable($this->checkboxStyle);
665+
$this->checkboxStyle = $style;
666666

667667
return $this;
668668
}
@@ -672,9 +672,9 @@ public function getRadioStyle() : RadioStyle
672672
return $this->radioStyle;
673673
}
674674

675-
public function radioStyle(callable $itemCallable) : self
675+
public function setRadioStyle(RadioStyle $style) : self
676676
{
677-
$itemCallable($this->radioStyle);
677+
$this->radioStyle = $style;
678678

679679
return $this;
680680
}

src/MenuItem/CheckboxItem.php

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,18 @@
99

1010
class CheckboxItem implements MenuItemInterface, ToggableItemInterface
1111
{
12-
use ToggableTrait;
12+
/**
13+
* @var callable
14+
*/
15+
private $selectAction;
16+
17+
private $text = '';
18+
19+
private $showItemExtra = false;
20+
21+
private $disabled = false;
22+
23+
private $checked = false;
1324

1425
/**
1526
* @var CheckboxStyle;
@@ -89,4 +100,81 @@ public function setStyle(CheckboxStyle $style) : self
89100

90101
return $this;
91102
}
103+
104+
/**
105+
* Toggles checked state
106+
*/
107+
public function toggle() : void
108+
{
109+
$this->checked = !$this->checked;
110+
}
111+
112+
/**
113+
* Sets checked state to true
114+
*/
115+
public function setChecked() : void
116+
{
117+
$this->checked = true;
118+
}
119+
120+
/**
121+
* Sets checked state to false
122+
*/
123+
public function setUnchecked() : void
124+
{
125+
$this->checked = false;
126+
}
127+
128+
/**
129+
* Whether or not the item is checked
130+
*/
131+
public function getChecked() : bool
132+
{
133+
return $this->checked;
134+
}
135+
136+
/**
137+
* Return the raw string of text
138+
*/
139+
public function getText() : string
140+
{
141+
return $this->text;
142+
}
143+
144+
/**
145+
* Set the raw string of text
146+
*/
147+
public function setText(string $text) : void
148+
{
149+
$this->text = $text;
150+
}
151+
152+
/**
153+
* Can the item be selected
154+
*/
155+
public function canSelect() : bool
156+
{
157+
return !$this->disabled;
158+
}
159+
160+
public function showsItemExtra() : bool
161+
{
162+
return $this->showItemExtra;
163+
}
164+
165+
/**
166+
* Enable showing item extra
167+
*/
168+
public function showItemExtra() : void
169+
{
170+
$this->showItemExtra = true;
171+
}
172+
173+
/**
174+
* Disable showing item extra
175+
*/
176+
public function hideItemExtra() : void
177+
{
178+
$this->showItemExtra = false;
179+
}
92180
}

0 commit comments

Comments
 (0)