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 menu updates in the multiwin example. #926

Merged
merged 13 commits into from
May 27, 2020
Merged
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Google LLC
Raph Levien
Hilmar Gústafsson
Dmitry Borodin
Kaiyin Zhong
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
- Removed references to cairo on macOS. ([#943] by [@xStrom])
- Updated screenshots in `README.md`. ([#967] by [@xStrom])
- Added a section about dependencies to `CONTRIBUTING.md`. ([#990] by [@xStrom])
- Fixed menu inconsistency across multiple windows in the multiwin example. ([#926] by [@kindlychung])

### Maintenance

Expand Down Expand Up @@ -216,6 +217,7 @@ This means that druid no longer requires cairo on macOS and uses Core Graphics i
[#920]: https://github.com/xi-editor/druid/pull/920
[#924]: https://github.com/xi-editor/druid/pull/924
[#925]: https://github.com/xi-editor/druid/pull/925
[#926]: https://github.com/xi-editor/druid/pull/926
[#928]: https://github.com/xi-editor/druid/pull/928
[#930]: https://github.com/xi-editor/druid/pull/930
[#931]: https://github.com/xi-editor/druid/pull/931
Expand Down
58 changes: 34 additions & 24 deletions druid/examples/multiwin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ use druid::widget::prelude::*;
use druid::widget::{
Align, BackgroundBrush, Button, Controller, ControllerHost, Flex, Label, Padding,
};
use druid::Target::Global;
use druid::{
commands as sys_cmds, AppDelegate, AppLauncher, Application, Color, Command, ContextMenu, Data,
DelegateCtx, LocalizedString, MenuDesc, MenuItem, Selector, Target, WindowDesc, WindowId,
};

use log::info;

const MENU_COUNT_ACTION: Selector = Selector::new("menu-count-action");
Expand All @@ -46,7 +46,9 @@ pub fn main() {
LocalizedString::new("multiwin-demo-window-title").with_placeholder("Many windows!"),
);
AppLauncher::with_window(main_window)
.delegate(Delegate)
.delegate(Delegate {
windows: Vec::new(),
})
.launch(State::default())
.expect("launch failed");
}
Expand All @@ -55,14 +57,10 @@ fn ui_builder() -> impl Widget<State> {
let text = LocalizedString::new("hello-counter")
.with_arg("count", |data: &State, _env| data.menu_count.into());
let label = Label::new(text);
let inc_button = Button::<State>::new("Add menu item").on_click(|ctx, data, _env| {
data.menu_count += 1;
ctx.set_menu(make_menu::<State>(data));
});
let dec_button = Button::<State>::new("Remove menu item").on_click(|ctx, data, _env| {
data.menu_count = data.menu_count.saturating_sub(1);
ctx.set_menu(make_menu::<State>(data));
});
let inc_button = Button::<State>::new("Add menu item")
.on_click(|ctx, _data, _env| ctx.submit_command(MENU_INCREMENT_ACTION, Global));
let dec_button = Button::<State>::new("Remove menu item")
.on_click(|ctx, _data, _env| ctx.submit_command(MENU_DECREMENT_ACTION, Global));
let new_button = Button::<State>::new("New window").on_click(|ctx, _data, _env| {
ctx.submit_command(sys_cmds::NEW_FILE, Target::Global);
});
Expand Down Expand Up @@ -132,6 +130,9 @@ impl<W: Widget<State>> Widget<State> for Glow<W> {
}

struct ContextMenuController;
struct Delegate {
windows: Vec<WindowId>,
}

impl<T, W: Widget<T>> Controller<T, W> for ContextMenuController {
fn event(&mut self, child: &mut W, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) {
Expand All @@ -145,46 +146,50 @@ impl<T, W: Widget<T>> Controller<T, W> for ContextMenuController {
}
}

struct Delegate;

impl AppDelegate<State> for Delegate {
fn command(
&mut self,
ctx: &mut DelegateCtx,
target: Target,
_target: Target,
cmd: &Command,
data: &mut State,
_env: &Env,
) -> bool {
match (target, &cmd.selector) {
(_, &sys_cmds::NEW_FILE) => {
let window = WindowDesc::new(ui_builder)
match cmd.selector {
sys_cmds::NEW_FILE => {
let new_win = WindowDesc::new(ui_builder)
.menu(make_menu(data))
.window_size((data.selected as f64 * 100.0 + 300.0, 500.0));
ctx.new_window(window);
ctx.new_window(new_win);
false
}
(Target::Window(id), &MENU_COUNT_ACTION) => {
MENU_COUNT_ACTION => {
data.selected = *cmd.get_object().unwrap();
let menu = make_menu::<State>(data);
ctx.set_menu(menu, id);
for id in &self.windows {
ctx.set_menu(menu.clone(), *id);
}
false
}
// wouldn't it be nice if a menu (like a button) could just mutate state
// directly if desired?
(Target::Window(id), &MENU_INCREMENT_ACTION) => {
MENU_INCREMENT_ACTION => {
data.menu_count += 1;
let menu = make_menu::<State>(data);
ctx.set_menu(menu, id);
for id in &self.windows {
ctx.set_menu(menu.clone(), *id);
}
false
}
(Target::Window(id), &MENU_DECREMENT_ACTION) => {
MENU_DECREMENT_ACTION => {
data.menu_count = data.menu_count.saturating_sub(1);
let menu = make_menu::<State>(data);
ctx.set_menu(menu, id);
for id in &self.windows {
ctx.set_menu(menu.clone(), *id);
}
false
}
(_, &MENU_SWITCH_GLOW_ACTION) => {
MENU_SWITCH_GLOW_ACTION => {
data.glow_hot = !data.glow_hot;
false
}
Expand All @@ -200,7 +205,9 @@ impl AppDelegate<State> for Delegate {
_ctx: &mut DelegateCtx,
) {
info!("Window added, id: {:?}", id);
self.windows.push(id);
}

fn window_removed(
&mut self,
id: WindowId,
Expand All @@ -209,6 +216,9 @@ impl AppDelegate<State> for Delegate {
_ctx: &mut DelegateCtx,
) {
info!("Window removed, id: {:?}", id);
if let Some(pos) = self.windows.iter().position(|x| *x == id) {
self.windows.remove(pos);
}
}
}

Expand Down