-
Notifications
You must be signed in to change notification settings - Fork 106
Checkbox + Radio item-level styling (3rd try) #211
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
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; | ||
|
||
|
@@ -187,7 +189,7 @@ public function addSubMenu(string $text, \Closure $callback) : self | |
|
||
$menu = $builder->build(); | ||
$menu->setParent($this->menu); | ||
|
||
$this->menu->addItem($item = new MenuMenuItem( | ||
$text, | ||
$menu, | ||
|
@@ -290,7 +292,7 @@ public function addSplitItem(\Closure $callback) : self | |
} | ||
|
||
$callback($builder); | ||
|
||
$this->menu->addItem($splitItem = $builder->build()); | ||
|
||
$this->processSplitItemShortcuts($splitItem); | ||
|
@@ -405,34 +407,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); | ||
|
@@ -551,6 +525,44 @@ 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; | ||
} | ||
|
||
/** | ||
* Pass styles from current menu to sub-menu | ||
* only if sub-menu style has not be customized | ||
|
@@ -560,6 +572,18 @@ private function propagateStyles(CliMenu $menu, array $items = []) | |
$currentItems = !empty($items) ? $items : $menu->getItems(); | ||
|
||
foreach ($currentItems as $item) { | ||
if ($item instanceof CheckboxItem | ||
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. The late style binding here really helps reduce complexity. As more item types are refactored to support item-level styling, they should be added here to make them work properly. |
||
&& !$item->getStyle()->hasChangedFromDefaults() | ||
) { | ||
$item->setStyle(clone $menu->getCheckboxStyle()); | ||
} | ||
|
||
if ($item instanceof RadioItem | ||
&& !$item->getStyle()->hasChangedFromDefaults() | ||
) { | ||
$item->setStyle(clone $menu->getRadioStyle()); | ||
} | ||
Comment on lines
+575
to
+585
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 don't like this and suspect it will get uglier. But also I'm happy for us to fix this 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. I mean refactor, just because of all the |
||
|
||
// Apply current style to children, if they are not customized | ||
if ($item instanceof MenuMenuItem) { | ||
$subMenu = $item->getSubMenu(); | ||
|
@@ -568,6 +592,14 @@ private function propagateStyles(CliMenu $menu, array $items = []) | |
$subMenu->setStyle(clone $menu->getStyle()); | ||
} | ||
|
||
if (!$subMenu->getCheckboxStyle()->hasChangedFromDefaults()) { | ||
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. Each submenu object gets a copy of radio and checkbox style object to keep. This is what is handed out to children items. |
||
$subMenu->setCheckboxStyle(clone $menu->getCheckboxStyle()); | ||
} | ||
|
||
if (!$subMenu->getRadioStyle()->hasChangedFromDefaults()) { | ||
$subMenu->setRadioStyle(clone $menu->getRadioStyle()); | ||
} | ||
|
||
$this->propagateStyles($subMenu); | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I prefer
setCheckedMarker
andsetUncheckedMarker
. We can address that later.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was hoping to keep the naming scheme identical across Selectable, Checkbox and Radio items.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However, since removed all the interfaces it doesn't matter much anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd prefer to keep it more semantic. And I think in the end, we might push this rendering back in to the item.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've got PR for SelectableItem styling ready. Should I hold off on it, or should I submit? The code will follow this PR.