Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: call order for menu entry functions #21

Merged

Conversation

EdBuilds
Copy link
Contributor

Hello,
This pr fixes the menu entry callback order issue, raised in issue 20.
Before the fix, the callbacks are called with this order:

enter_root called with root menu as parameter
> sub1
enter_root called with root menu as parameter
/root> sub2
enter_sub1 called with sub1 menu as parameter
/root/sub1> exit
exit_sub2 called with sub2 menu as parameter
/root> exit
exit_sub1 called with sub1 menu as parameter

After this pr it looks like this:

enter_root called with root menu as parameter
> sub1
enter_sub1 called with sub1 menu as parameter
/root> sub2
enter_sub2 called with sub2 menu as parameter
/root/sub1> exit
exit_sub2 called with sub2 menu as parameter
/root> exit
exit_sub1 called with sub1 menu as parameter

Solved it by calling the ItemType::Menu member's callback, instead of looking it up from the manager.

For reference here is the menu definition I used to test the fix:

const ROOT_MENU: Menu<Output, Context> = Menu {
    label: "root",
    items: &[
        &Item {
            item_type: ItemType::Menu(&Menu {
                label: "sub1",
                items: &[
                    &Item {
                        item_type: ItemType::Menu(&Menu {
                            label: "sub2",
                            items: &[
                            ],
                            entry: Some(enter_sub2),
                            exit: Some(exit_sub2),
                        }),
                        command: "sub2",
                        help: Some("enter sub2-menu"),
                    },
                ],
                entry: Some(enter_sub1),
                exit: Some(exit_sub1),
            }),
            command: "sub1",
            help: Some("enter sub1-menu"),
        },
    ],
    entry: Some(enter_root),
    exit: Some(exit_root),
};
fn enter_root(menu: &Menu<Output, Context>, interface: &mut Output, _context: &mut Context) {
    writeln!(interface, "enter_root called with {} menu as parameter", menu.label).unwrap();
}

fn exit_root(menu: &Menu<Output, Context>, interface: &mut Output, _context: &mut Context) {
    writeln!(interface, "exit_root called with {} menu as parameter", menu.label).unwrap();
}
fn enter_sub1(menu: &Menu<Output, Context>, interface: &mut Output, _context: &mut Context) {
    writeln!(interface, "enter_sub1 called with {} menu as parameter", menu.label).unwrap();
}

fn exit_sub1(menu: &Menu<Output, Context>, interface: &mut Output, _context: &mut Context) {
    writeln!(interface, "exit_sub1 called with {} menu as parameter", menu.label).unwrap();
}
fn enter_sub2(menu: &Menu<Output, Context>, interface: &mut Output, _context: &mut Context) {
    writeln!(interface, "enter_sub2 called with {} menu as parameter", menu.label).unwrap();
}

fn exit_sub2(menu: &Menu<Output, Context>, interface: &mut Output, _context: &mut Context) {
    writeln!(interface, "exit_sub2 called with {} menu as parameter", menu.label).unwrap();
}

@thejpster thejpster added this pull request to the merge queue Aug 22, 2024
Merged via the queue into rust-embedded-community:master with commit 5fe8573 Aug 22, 2024
2 checks passed
@thejpster
Copy link
Member

Thank you!

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

Successfully merging this pull request may close these issues.

2 participants