-
Notifications
You must be signed in to change notification settings - Fork 106
Checkbox + Radio item-level styling #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e58bb70
34ea4b3
6b72992
0c48311
8179b35
d3a76ba
7a8a963
da43609
0f1db5f
4ea4242
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
use PhpSchool\CliMenu\MenuItem\SplitItem; | ||
use PhpSchool\CliMenu\MenuItem\StaticItem; | ||
use PhpSchool\CliMenu\MenuStyle; | ||
use PhpSchool\CliMenu\Style\CheckboxStyle; | ||
use PhpSchool\CliMenu\Style\RadioStyle; | ||
use PhpSchool\CliMenu\Terminal\TerminalFactory; | ||
use PhpSchool\Terminal\Terminal; | ||
|
||
|
@@ -138,7 +140,10 @@ public function addCheckboxItem( | |
bool $showItemExtra = false, | ||
bool $disabled = false | ||
) : self { | ||
$this->addMenuItem(new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled)); | ||
$item = (new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled)) | ||
->setStyle($this->menu->getCheckboxStyle()); | ||
|
||
$this->addMenuItem($item); | ||
|
||
return $this; | ||
} | ||
|
@@ -149,7 +154,10 @@ public function addRadioItem( | |
bool $showItemExtra = false, | ||
bool $disabled = false | ||
) : self { | ||
$this->addMenuItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled)); | ||
$item = (new RadioItem($text, $itemCallable, $showItemExtra, $disabled)) | ||
->setStyle($this->menu->getRadioStyle()); | ||
|
||
$this->addMenuItem($item); | ||
|
||
return $this; | ||
} | ||
|
@@ -194,6 +202,14 @@ public function addSubMenu(string $text, \Closure $callback) : self | |
$menu->setStyle($this->menu->getStyle()); | ||
} | ||
|
||
if (!$menu->getCheckboxStyle()->hasChangedFromDefaults()) { | ||
$menu->setCheckboxStyle(clone $this->menu->getCheckboxStyle()); | ||
} | ||
|
||
if (!$menu->getRadioStyle()->hasChangedFromDefaults()) { | ||
$menu->setRadioStyle(clone $this->menu->getRadioStyle()); | ||
} | ||
|
||
$this->menu->addItem($item = new MenuMenuItem( | ||
$text, | ||
$menu, | ||
|
@@ -216,6 +232,14 @@ public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : s | |
$menu->setStyle($this->menu->getStyle()); | ||
} | ||
|
||
if (!$menu->getCheckboxStyle()->hasChangedFromDefaults()) { | ||
$menu->setCheckboxStyle(clone $this->menu->getCheckboxStyle()); | ||
} | ||
|
||
if (!$menu->getRadioStyle()->hasChangedFromDefaults()) { | ||
$menu->setRadioStyle(clone $this->menu->getRadioStyle()); | ||
} | ||
Comment on lines
+235
to
+241
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could extract this to a private method, since we do it below as well. But we can do that later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Above even. |
||
|
||
$this->menu->addItem($item = new MenuMenuItem( | ||
$text, | ||
$menu, | ||
|
@@ -296,13 +320,15 @@ private function processIndividualShortcut(MenuItemInterface $item, callable $ca | |
public function addSplitItem(\Closure $callback) : self | ||
{ | ||
$builder = new SplitItemBuilder($this->menu); | ||
$builder->setCheckboxStyle(clone $this->menu->getCheckboxStyle()) | ||
->setRadioStyle(clone $this->menu->getRadioStyle()); | ||
|
||
if ($this->autoShortcuts) { | ||
$builder->enableAutoShortcuts($this->autoShortcutsRegex); | ||
} | ||
|
||
$callback($builder); | ||
|
||
$this->menu->addItem($splitItem = $builder->build()); | ||
|
||
$this->processSplitItemShortcuts($splitItem); | ||
|
@@ -417,34 +443,6 @@ public function setSelectedMarker(string $marker) : self | |
return $this; | ||
} | ||
|
||
public function setUncheckedMarker(string $marker) : self | ||
{ | ||
$this->style->setUncheckedMarker($marker); | ||
|
||
return $this; | ||
} | ||
|
||
public function setCheckedMarker(string $marker) : self | ||
{ | ||
$this->style->setCheckedMarker($marker); | ||
|
||
return $this; | ||
} | ||
|
||
public function setUnradioMarker(string $marker) : self | ||
{ | ||
$this->style->setUnradioMarker($marker); | ||
|
||
return $this; | ||
} | ||
|
||
public function setRadioMarker(string $marker) : self | ||
{ | ||
$this->style->setRadioMarker($marker); | ||
|
||
return $this; | ||
} | ||
|
||
public function setItemExtra(string $extra) : self | ||
{ | ||
$this->style->setItemExtra($extra); | ||
|
@@ -558,4 +556,42 @@ public function build() : CliMenu | |
|
||
return $this->menu; | ||
} | ||
|
||
public function getCheckboxStyle() : CheckboxStyle | ||
{ | ||
return $this->menu->getCheckboxStyle(); | ||
} | ||
|
||
public function setCheckboxStyle(CheckboxStyle $style) : self | ||
{ | ||
$this->menu->setCheckboxStyle($style); | ||
|
||
return $this; | ||
} | ||
|
||
public function modifyCheckboxStyle(callable $itemCallable) : self | ||
{ | ||
$itemCallable($this->menu->getCheckboxStyle()); | ||
|
||
return $this; | ||
} | ||
|
||
public function getRadioStyle() : RadioStyle | ||
{ | ||
return $this->menu->getRadioStyle(); | ||
} | ||
|
||
public function setRadioStyle(RadioStyle $style) : self | ||
{ | ||
$this->menu->setRadioStyle($style); | ||
|
||
return $this; | ||
} | ||
|
||
public function modifyRadioStyle(callable $itemCallable) : self | ||
{ | ||
$itemCallable($this->menu->getRadioStyle()); | ||
|
||
return $this; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
use PhpSchool\CliMenu\MenuItem\SelectableItem; | ||
use PhpSchool\CliMenu\MenuItem\SplitItem; | ||
use PhpSchool\CliMenu\MenuItem\StaticItem; | ||
use PhpSchool\CliMenu\Style\CheckboxStyle; | ||
use PhpSchool\CliMenu\Style\RadioStyle; | ||
|
||
/** | ||
* @author Aydin Hassan <aydin@hotmail.co.uk> | ||
|
@@ -65,7 +67,10 @@ public function addCheckboxItem( | |
bool $showItemExtra = false, | ||
bool $disabled = false | ||
) : self { | ||
$this->splitItem->addItem(new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled)); | ||
$item = (new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled)) | ||
->setStyle($this->menu->getCheckboxStyle()); | ||
|
||
$this->splitItem->addItem($item); | ||
|
||
return $this; | ||
} | ||
|
@@ -76,7 +81,10 @@ public function addRadioItem( | |
bool $showItemExtra = false, | ||
bool $disabled = false | ||
) : self { | ||
$this->splitItem->addItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled)); | ||
$item = (new RadioItem($text, $itemCallable, $showItemExtra, $disabled)) | ||
->setStyle($this->menu->getRadioStyle()); | ||
|
||
$this->splitItem->addItem($item); | ||
|
||
return $this; | ||
} | ||
|
@@ -108,6 +116,14 @@ public function addSubMenu(string $text, \Closure $callback) : self | |
$menu = $builder->build(); | ||
$menu->setParent($this->menu); | ||
|
||
if (!$menu->getCheckboxStyle()->hasChangedFromDefaults()) { | ||
$menu->setCheckboxStyle(clone $this->menu->getCheckboxStyle()); | ||
} | ||
|
||
if (!$menu->getRadioStyle()->hasChangedFromDefaults()) { | ||
$menu->setRadioStyle(clone $this->menu->getRadioStyle()); | ||
} | ||
|
||
$this->splitItem->addItem(new MenuMenuItem( | ||
$text, | ||
$menu, | ||
|
@@ -139,4 +155,42 @@ public function build() : SplitItem | |
{ | ||
return $this->splitItem; | ||
} | ||
|
||
public function getCheckboxStyle() : CheckboxStyle | ||
{ | ||
return $this->menu->getCheckboxStyle(); | ||
} | ||
|
||
public function setCheckboxStyle(CheckboxStyle $style) : self | ||
{ | ||
$this->menu->setCheckboxStyle($style); | ||
|
||
return $this; | ||
} | ||
|
||
public function modifyCheckboxStyle(callable $itemCallable) : self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually think we should drop this. It's took me a while but I think I get what you are doing. If you change the styles in a splititem builder, they persist back to the parent menu. I think it makes the code very confusing. I would rather we just not allow to modify the styles in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style is always limited only to the current scope and any children:
The parent should never be affected from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually you're right, there's a bug where changing in a child updates the parent:
I'll fix and add a test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Phew 😅 Thought I was going crazy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I see, because This is why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I think the problem is that I had a fundamental misunderstanding on the SplitItem. I was thinking this was a separate menu ala submenu, but it's simply a horizontal partitioner of the current menu. You're completely right, SplitItemBuilder shouldn't have any of the radio/checkbox style methods, and should not have its own style instance(s). |
||
{ | ||
$itemCallable($this->menu->getCheckboxStyle()); | ||
|
||
return $this; | ||
} | ||
|
||
public function getRadioStyle() : RadioStyle | ||
{ | ||
return $this->menu->getRadioStyle(); | ||
} | ||
|
||
public function setRadioStyle(RadioStyle $style) : self | ||
{ | ||
$this->menu->setRadioStyle($style); | ||
|
||
return $this; | ||
} | ||
|
||
public function modifyRadioStyle(callable $itemCallable) : self | ||
{ | ||
$itemCallable($this->menu->getRadioStyle()); | ||
|
||
return $this; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.