orbtk icon indicating copy to clipboard operation
orbtk copied to clipboard

Popup not responding as expected to open/close

Open lukors opened this issue 4 years ago • 4 comments

Describe the bug When I open/close a popup, it does not change even though the value on the widget changes as expected.

To Reproduce Replace the minimal example with this code:

use orbtk::prelude::*;

fn main() {
    Application::new()
        .window(|ctx| {
            Window::new()
                .title("OrbTk - minimal example")
                .position((100.0, 100.0))
                .size(200.0, 200.0)
                .child(MainView::new().build(ctx))
                .build(ctx)
        })
        .run();
}

enum Message {
    TogglePopup,
}

static ID_POPUP: &str = "id_popup";

widget!(MainView<MainViewState> {});

impl Template for MainView {
    fn template(self, id: Entity, ctx: &mut BuildContext) -> Self {
        let menu_node = Popup::new()
            .id(ID_POPUP)
            .margin(Thickness {
                left: 0.,
                top: 50.,
                right: 0.,
                bottom: 0.,
            })
            .width(200.)
            .target(id)
            .open(true)
            .child(
                TextBlock::new()
                    .text("Popup")
                    .build(ctx),
            ).build(ctx);

        self.child(
            Button::new()
                .style("button")
                .on_click(move |ctx, _| {
                    ctx.send_message(Message::TogglePopup, id);
                    true
                })
                .text("Toggle")
                .build(ctx),
        ).child(menu_node)
    }
}


#[derive(Debug, Default, AsAny)]
struct MainViewState {
    popup: Entity,
}

impl State for MainViewState {
    fn init(&mut self, _: &mut Registry, ctx: &mut Context) {
        self.popup = ctx.child(ID_POPUP).entity();
    }
    
    fn messages(
        &mut self,
        mut messages: MessageReader,
        _registry: &mut Registry,
        ctx: &mut Context,
    ) {
        for message in messages.read::<Message>() {
            match message {
                Message::TogglePopup => {
                    let mut popup_widget = ctx.get_widget(self.popup);

                    let current_open = *popup_widget.get::<bool>("open");
                    println!("Current open: {}", current_open);

                    popup_widget.set::<bool>("open", !current_open);
                }
            };
        }
    }
}

cargo run --example minimal

Run the program and press the toggle button. Look in the console to see that the value changes, and look in the UI to see that the popup does not close.

Expected behavior The popup should hide/show itself when you press the button.

Desktop:

  • OS: Windows 10
  • Version 2004

Additional context This seems to work fine when not using the message system. So if you instead do this in the update loop like you would do it before the message system it works. Let me know if you would like example code for this.

I'm probably just doing something wrong or using something in the wrong way, but it seems weird.

lukors avatar Nov 01 '20 16:11 lukors

Thank you for the report.

I think I know what the problem is. update is called before messages and the popup logic is handled in the update. There is a ongoing extended Popup https://github.com/redox-os/orbtk/pull/304. I will check that it will work also with messages.

FloVanGH avatar Nov 01 '20 17:11 FloVanGH

That makes sense, that's probably the issue.

I saw that the tab widget or whatever it's called can react to messages sent directly to it.

  1. Is this the functionality that will solve this?
  2. Is this functionality of being able to react to received messages intended to be added to all widgets?

Thanks! :)

lukors avatar Nov 01 '20 20:11 lukors

Is this the functionality that will solve this?

Yes it should sole the problem and make a cleaner API.

Is this functionality of being able to react to received messages intended to be added to all widgets?

That’s my plan. But I have to check if it is possible on all widgets. I think the message concept is a better pattern as the old one.

FloVanGH avatar Nov 01 '20 21:11 FloVanGH

I haven't used it much yet, but it seems like it's going to reduce the amount of code in my application and simplify it. Looks very promising, so I hope it works out!

It's good to know this is the plan, then I know how to move forward with my application. :)

lukors avatar Nov 02 '20 09:11 lukors