Skip to content

Checkbox + Radio item-level styling - CR Changes #206

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

Merged
merged 1 commit into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/checkbox-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
})
->addSubMenu('Interpreted', function (CliMenuBuilder $b) use ($itemCallable) {
$b->setTitle('Interpreted Languages')
->checkboxStyle(function (CheckboxStyle $style) {
->modifyCheckboxStyle(function (CheckboxStyle $style) {
$style->setMarkerOff('[○] ')
->setMarkerOn('[●] ');
})
Expand Down
2 changes: 1 addition & 1 deletion examples/radio-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
})
->addSubMenu('Interpreted', function (CliMenuBuilder $b) use ($itemCallable) {
$b->setTitle('Interpreted Languages')
->radioStyle(function (RadioStyle $style) {
->modifyRadioStyle(function (RadioStyle $style) {
$style->setMarkerOff('[ ] ')
->setMarkerOn('[✔] ');
})
Expand Down
68 changes: 44 additions & 24 deletions src/Builder/CliMenuBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,12 @@ public function addSubMenu(string $text, \Closure $callback) : self
$menu->setStyle($this->menu->getStyle());
}

if (!$menu->getCheckboxStyle()->getIsCustom()) {
$menu->checkboxStyle(function (CheckboxStyle $style) {
$style->fromArray($this->menu->getCheckboxStyle()->toArray());
});
if (!$menu->getCheckboxStyle()->hasChangedFromDefaults()) {
$menu->setCheckboxStyle(clone $this->menu->getCheckboxStyle());
}

if (!$menu->getRadioStyle()->getIsCustom()) {
$menu->radioStyle(function (RadioStyle $style) {
$style->fromArray($this->menu->getRadioStyle()->toArray());
});
if (!$menu->getRadioStyle()->hasChangedFromDefaults()) {
$menu->setRadioStyle(clone $this->menu->getRadioStyle());
}

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

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

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

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

$callback($builder);

$builder->checkboxStyle(function (CheckboxStyle $style) {
$style->fromArray($this->menu->getCheckboxStyle()->toArray());
});
if (!$builder->getCheckboxStyle()->hasChangedFromDefaults()) {
$builder->setCheckboxStyle(clone $this->menu->getCheckboxStyle());
}

$builder->radioStyle(function (RadioStyle $style) {
$style->fromArray($this->menu->getRadioStyle()->toArray());
});
if (!$builder->getRadioStyle()->hasChangedFromDefaults()) {
$builder->setRadioStyle(clone $this->menu->getRadioStyle());
}

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

Expand Down Expand Up @@ -567,16 +563,40 @@ public function build() : CliMenu
return $this->menu;
}

public function checkboxStyle(callable $itemCallable) : self
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->checkboxStyle($itemCallable);
$this->menu->setRadioStyle($style);

return $this;
}

public function radioStyle(callable $itemCallable) : self
public function modifyRadioStyle(callable $itemCallable) : self
{
$this->menu->radioStyle($itemCallable);
$itemCallable($this->menu->getRadioStyle());

return $this;
}
Expand Down
44 changes: 34 additions & 10 deletions src/Builder/SplitItemBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ public function addSubMenu(string $text, \Closure $callback) : self
$menu = $builder->build();
$menu->setParent($this->menu);

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

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

$this->splitItem->addItem(new MenuMenuItem(
$text,
Expand Down Expand Up @@ -169,16 +169,40 @@ public function build() : SplitItem
return $this->splitItem;
}

public function checkboxStyle(callable $itemCallable) : self
public function getCheckboxStyle() : CheckboxStyle
{
return $this->checkboxStyle;
}

public function setCheckboxStyle(CheckboxStyle $style) : self
{
$this->checkboxStyle = $style;

return $this;
}

public function modifyCheckboxStyle(callable $itemCallable) : self
{
$itemCallable($this->menu->getCheckboxStyle());

return $this;
}

public function getRadioStyle() : RadioStyle
{
return $this->radioStyle;
}

public function setRadioStyle(RadioStyle $style) : self
{
$this->menu->checkboxStyle($itemCallable);
$this->radioStyle = $style;

return $this;
}

public function radioStyle(callable $itemCallable) : self
public function modifyRadioStyle(callable $itemCallable) : self
{
$this->menu->radioStyle($itemCallable);
$itemCallable($this->menu->getRadioStyle());

return $this;
}
Expand Down
8 changes: 4 additions & 4 deletions src/CliMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,9 +660,9 @@ public function getCheckboxStyle() : CheckboxStyle
return $this->checkboxStyle;
}

public function checkboxStyle(callable $itemCallable) : self
public function setCheckboxStyle(CheckboxStyle $style) : self
{
$itemCallable($this->checkboxStyle);
$this->checkboxStyle = $style;

return $this;
}
Expand All @@ -672,9 +672,9 @@ public function getRadioStyle() : RadioStyle
return $this->radioStyle;
}

public function radioStyle(callable $itemCallable) : self
public function setRadioStyle(RadioStyle $style) : self
{
$itemCallable($this->radioStyle);
$this->radioStyle = $style;

return $this;
}
Expand Down
90 changes: 89 additions & 1 deletion src/MenuItem/CheckboxItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@

class CheckboxItem implements MenuItemInterface, ToggableItemInterface
{
use ToggableTrait;
/**
* @var callable
*/
private $selectAction;

private $text = '';

private $showItemExtra = false;

private $disabled = false;

private $checked = false;

/**
* @var CheckboxStyle;
Expand Down Expand Up @@ -89,4 +100,81 @@ public function setStyle(CheckboxStyle $style) : self

return $this;
}

/**
* Toggles checked state
*/
public function toggle() : void
{
$this->checked = !$this->checked;
}

/**
* Sets checked state to true
*/
public function setChecked() : void
{
$this->checked = true;
}

/**
* Sets checked state to false
*/
public function setUnchecked() : void
{
$this->checked = false;
}

/**
* Whether or not the item is checked
*/
public function getChecked() : bool
{
return $this->checked;
}

/**
* Return the raw string of text
*/
public function getText() : string
{
return $this->text;
}

/**
* Set the raw string of text
*/
public function setText(string $text) : void
{
$this->text = $text;
}

/**
* Can the item be selected
*/
public function canSelect() : bool
{
return !$this->disabled;
}

public function showsItemExtra() : bool
{
return $this->showItemExtra;
}

/**
* Enable showing item extra
*/
public function showItemExtra() : void
{
$this->showItemExtra = true;
}

/**
* Disable showing item extra
*/
public function hideItemExtra() : void
{
$this->showItemExtra = false;
}
}
Loading