Skip to content

Mac: Context menu on non-focused window disappears without action on click #2417

@AlexKnauth

Description

@AlexKnauth

When the application window does not have focus, and I open a context menu by right-clicking the window, the window stays non-focused on Mac.

The context menu still appears, still highlights the options under my mouse, etc. But when I click on an option from the context menu in this state, the context menu disappears without performing the action that it would normally perform. For an action that doesn't display any immediate sign of it working other than the context menu closing, this can fool users into thinking they used the context menu to perform the action, when they didn't.

I can think of 2 reasonable sets of behaviors:

  1. Perform the action and close the context menu.
  2. Give focus without performing the action or closing, allowing the user to click again now that it has focus.

With the current confusing behavior being:

  • "Silently" fail with no action and close the context menu.

On Windows, right-clicking the window gives focus immediately. To someone not paying attention to which window has focus, this looks most like (1).

Below is a small Context Menu Example showing this behavior. On control-clicking, it gets focus and behaves as expected. But on right-clicking with a mouse, it does not get focus on Mac, and this confusing behavior appears:

use druid::widget::prelude::*;
use druid::widget::{Align, Controller, Label};
use druid::{
    AppLauncher, Data, Env, Lens, LocalizedString, Menu, MenuItem, Selector, Widget, WidgetExt,
    WindowDesc,
};

const WINDOW_TITLE: LocalizedString<CounterState> = LocalizedString::new("Context Menu Example");

#[derive(Clone, Data, Lens)]
struct CounterState {
    i: i64,
}

struct CounterController;

const CONTEXT_MENU_COUNTER_INCREMENT: Selector = Selector::new("context-menu-counter-increment");
const CONTEXT_MENU_COUNTER_DECREMENT: Selector = Selector::new("context-menu-counter-decrement");

impl<W: Widget<CounterState>> Controller<CounterState, W> for CounterController {
    fn event(
        &mut self,
        child: &mut W,
        ctx: &mut EventCtx,
        event: &Event,
        data: &mut CounterState,
        env: &Env,
    ) {
        match event {
            Event::MouseUp(event) => {
                if event.button.is_right() || (event.button.is_left() && event.mods.ctrl()) {
                    ctx.show_context_menu::<CounterState>(
                        Menu::new("Counter")
                            .entry(
                                MenuItem::new("Increment").command(CONTEXT_MENU_COUNTER_INCREMENT),
                            )
                            .entry(
                                MenuItem::new("Decrement").command(CONTEXT_MENU_COUNTER_DECREMENT),
                            ),
                        event.pos,
                    );
                }
            }
            Event::Command(command) => {
                if command.is(CONTEXT_MENU_COUNTER_INCREMENT) {
                    data.i += 1;
                } else if command.is(CONTEXT_MENU_COUNTER_DECREMENT) {
                    data.i -= 1;
                }
            }
            _ => {}
        }
        // Always pass on the event!
        child.event(ctx, event, data, env)
    }
}

fn main() {
    // describe the main window
    let main_window = WindowDesc::new(build_root_widget())
        .title(WINDOW_TITLE)
        .window_size((400.0, 400.0));

    // create the initial app state
    let initial_state = CounterState { i: 0 };

    // start the application
    AppLauncher::with_window(main_window)
        .launch(initial_state)
        .expect("Failed to launch application");
}

fn build_root_widget() -> impl Widget<CounterState> {
    // a label that will determine its text based on the current app data.
    let label = Label::new(|data: &CounterState, _env: &Env| format!("Counter: {}", data.i));

    // center the a widget in the available space
    Align::centered(label).controller(CounterController)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions