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

Fix context menu on non-focused window #2418

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions druid/examples/context_menu.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2025 the Druid Authors
// SPDX-License-Identifier: Apache-2.0

//! Context menu example to increment/decrement a counter

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)
}
2 changes: 1 addition & 1 deletion druid/src/win_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl<T: Data> InnerAppState<T> {
let queue = &mut self.command_queue;
let data = &mut self.data;
let env = &self.env;
match window_id {
match window_id.or(self.menu_window) {
Some(id) => self
.windows
.get_mut(id)
Expand Down
Loading