Skip to content
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

Style reset after 2nd submenu #201

Closed
jtreminio opened this issue Dec 18, 2019 · 6 comments
Closed

Style reset after 2nd submenu #201

jtreminio opened this issue Dec 18, 2019 · 6 comments

Comments

@jtreminio
Copy link
Contributor

<?php

use PhpSchool\CliMenu\CliMenu;
use PhpSchool\CliMenu\Builder\CliMenuBuilder;

require_once(__DIR__ . '/../vendor/autoload.php');

$menu = (new CliMenuBuilder)
    ->setBackgroundColour('black')
    ->setForegroundColour('green')
    ->setTitle('Color Test #1')
    ->addLineBreak()
    ->addSubMenu('Submenu #1', function (CliMenuBuilder $b) {
        $b->setTitle('Color Test #2')
        ->addLineBreak()
        ->addSubMenu('Submenu #2', function (CliMenuBuilder $b) {
            $b->setTitle('Color Test #3')
            ->addLineBreak()
            ->addItem('Foo #6', function (CliMenu $menu) {})
            ->addItem('Foo #7', function (CliMenu $menu) {})
            ->addLineBreak();
        })
        ->addItem('Foo #4', function (CliMenu $menu) {})
        ->addItem('Foo #5', function (CliMenu $menu) {})
        ->addLineBreak();
    })
    ->addItem('Foo #2', function (CliMenu $menu) {})
    ->addItem('Foo #3', function (CliMenu $menu) {})
    ->addLineBreak()
    ->build();

$menu->open();

Peek 2019-12-18 09-33

It appears CliMenuBuilder::addSubMenu() returns false for

        if (!$menu->getStyle()->hasChangedFromDefaults()) {
            $menu->setStyle($this->menu->getStyle());
        }

I can step through and verify this, but the MenuStyle is only applied to the first submenu, nothing afterward unless I set the values again.

@AydinHassan
Copy link
Member

That's odd, definitely a bug

@AydinHassan
Copy link
Member

AydinHassan commented Dec 18, 2019

A quick look says it's broken. When building the 3rd menu, the second menu hasn't been built yet, so it's not had the first menu's style set on to it. It has the default style. So the 3rd menu inherits the seconds default style.

@jtreminio
Copy link
Contributor Author

Adding menus and items executes their code immediately (other than user-provided callbacks).

If the code is changed so that CliMenuBuilder::addSubMenu() and CliMenuBuilder::addSubMenuFromBuilder() persist anonymous functions vs an instantiated CliMenu object, and then run that code on initial submenu being opened, we can avoid this race condition.

@jtreminio
Copy link
Contributor Author

I'm thinking CliMenu could be somewhat like a container, like the old Pimple.

@AydinHassan
Copy link
Member

Not sure I’m following but I’d be interested in seeing a prototype 😀

@AydinHassan
Copy link
Member

Fixed with #210.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants