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

Response.context_menu now returns the response of the context menu, if open #3904

Merged
merged 4 commits into from Jan 29, 2024

Conversation

AufarZakiev
Copy link
Contributor

@AufarZakiev AufarZakiev commented Jan 27, 2024

Hi everyone! It's a great pleasure to work with such a library. It feels like a breath of fresh air!

Problem:

The current implementation of context_menu consumes self and returns it. However, the underlying menu::context_menu requires only a reference to self, so it is safe to change the context_menu signature.

Fix:

  1. Change signature to take a ref to self.
  2. Return the Option<InnerResponse<()>> from the underlying method call

Pros:

  1. No let response = response.context_menu(|| {...}) pattern
  2. Better consistency with other show-like methods, as it is in the Window::show
  3. Less ownership gymnastics

Cons:

  1. Breaking change
  2. Smth else what I don't see ?

Additional info:

  • This method is also addressed in this PR.
  • check.sh fails only on cargo check --quiet -p eframe --no-default-features --features wgpu with "The platform you're compiling for is not supported by winit" error, should it be launched at all ?

@AufarZakiev AufarZakiev marked this pull request as ready for review January 27, 2024 14:25
@YgorSouza
Copy link
Contributor

Another downside is that it makes it harder to use the BitOr operator.

@AufarZakiev
Copy link
Contributor Author

AufarZakiev commented Jan 28, 2024

With the fix BitOr could be used on original response itself, isn't it?

I see the point in another downside: method chaining becomes impossible. We won't be able to call smth like my_response.context_menu(...).highlight().
However, I think, such chaining is OK on builder pattern, when the object does not exist yet (e.g. Label, TextEdit, etc.). It is not so necessary when dealing with an already constructed object, such as Response

@YgorSouza
Copy link
Contributor

I meant if you wanted to union the responses from multiple widgets as a single expression while inserting context menus and tooltips for each of them, like this:

fn my_composite_widget(ui: &mut Ui) -> Response {
    let ctx_menu = |ui: &mut Ui, option| {
        ui.label(option);
    };
    ui.button("Button 1")
        .context_menu(|ui| ctx_menu(ui, "Menu 1"))
        | ui.button("Button 2")
            .on_hover_text("Tooltip 2")
            .context_menu(|ui| ctx_menu(ui, "Menu 2"))
        | ui.button("Button 3")
            .context_menu(|ui| ctx_menu(ui, "Menu 3"))
}

Though I suppose this could be solved by adding an inspect method like in the standard library. And it would be easy for anyone to create a trait containing a method like this and implement it for Response.

Copy link
Owner

@emilk emilk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense, since the context_menu method needs to return something to the user

@AufarZakiev
Copy link
Contributor Author

AufarZakiev commented Jan 29, 2024

@YgorSouza, does it this code fit your needs?

fn my_composite_widget(ui: &mut Ui) -> egui::Response {
    let ctx_menu = |ui: &mut Ui, option| {
        ui.label(option);
    };
    let btn1 = ui.button("Button 1");
    let btn2 = ui.button("Button 2").on_hover_text("Tooltip 2");
    let btn3 = ui.button("Button 3");

    btn1.context_menu(|ui| ctx_menu(ui, "Menu 1"));
    btn2.context_menu(|ui| ctx_menu(ui, "Menu 2"));
    btn3.context_menu(|ui| ctx_menu(ui, "Menu 3"));

    return btn1 | btn2 | btn3;
}

I see it a little wordy but more consistent. If there is a way to make it both very laconic and avoid let response = pattern - it would be great.

Thinking out loud:
I feel that context_menu, on_hover_text, etc. should be applicable to the Response object AND to the Button itself.

  • Response's methods may take a ref
  • Button's methods may consume self, as it is done with shortcut_text, rounding, etc.

I will play around a bit trying to implement this idea.

@AufarZakiev
Copy link
Contributor Author

@emilk , I have merged last changes from the master branch. Could you please set labels for this PR? I guess I have no rights for that

@emilk
Copy link
Owner

emilk commented Jan 29, 2024

@YgorSouza you can also use https://crates.io/crates/tap

@emilk emilk added the egui label Jan 29, 2024
@emilk emilk changed the title Fix context_menu's self arg Response.context_menu now returns the response of the context menu, if open Jan 29, 2024
@emilk emilk merged commit 01597fe into emilk:master Jan 29, 2024
19 of 20 checks passed
@AufarZakiev AufarZakiev deleted the fix-context-menu-self-arg branch January 30, 2024 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants