Skip to content
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
2 changes: 1 addition & 1 deletion druid-shell/examples/quit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ fn main() {
0x100,
"E&xit",
Some(&HotKey::new(SysMods::Cmd, "q")),
None,
true,
false,
);

let mut menubar = Menu::new();
Expand Down
6 changes: 3 additions & 3 deletions druid-shell/examples/shello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,22 @@ fn main() {
0x100,
"E&xit",
Some(&HotKey::new(SysMods::Cmd, "q")),
None,
true,
false,
);
file_menu.add_item(
0x101,
"O&pen",
Some(&HotKey::new(SysMods::Cmd, "o")),
None,
true,
false,
);
file_menu.add_item(
0x102,
"S&ave",
Some(&HotKey::new(SysMods::Cmd, "s")),
None,
true,
false,
);
let mut menubar = Menu::new();
menubar.add_dropdown(Menu::new(), "Application", true);
Expand Down
67 changes: 48 additions & 19 deletions druid-shell/src/backend/gtk/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

use gtk::gdk::ModifierType;
use gtk::{
AccelGroup, Menu as GtkMenu, MenuBar as GtkMenuBar, MenuItem as GtkMenuItem,
SeparatorMenuItemBuilder,
AccelGroup, CheckMenuItem, Menu as GtkMenu, MenuBar as GtkMenuBar, MenuItem as GtkMenuItem,
};
use gtk_rs::SeparatorMenuItemBuilder;

use gtk::prelude::{GtkMenuExt, GtkMenuItemExt, MenuShellExt, WidgetExt};

Expand All @@ -39,6 +39,7 @@ enum MenuItem {
name: String,
id: u32,
key: Option<HotKey>,
selected: Option<bool>,
enabled: bool,
},
SubMenu(String, Menu),
Expand All @@ -65,14 +66,14 @@ impl Menu {
id: u32,
text: &str,
key: Option<&HotKey>,
selected: Option<bool>,
enabled: bool,
_selected: bool,
) {
// TODO: implement selected items
self.items.push(MenuItem::Entry {
name: strip_access_key(text),
id,
key: key.cloned(),
selected,
enabled,
});
}
Expand All @@ -93,23 +94,19 @@ impl Menu {
name,
id,
key,
selected,
enabled,
} => {
let item = GtkMenuItem::with_label(&name);
item.set_sensitive(enabled);

if let Some(k) = key {
register_accelerator(&item, accel_group, k);
}

let handle = handle.clone();
item.connect_activate(move |_| {
if let Some(state) = handle.state.upgrade() {
state.handler.borrow_mut().command(id);
if let Some(state) = selected {
let item = CheckMenuItem::with_label(&name);
if state {
item.activate();
}
});

menu.append(&item);
add_menu_entry(menu, handle, accel_group, &item, id, key, enabled);
} else {
let entry = GtkMenuItem::with_label(&name);
add_menu_entry(menu, handle, accel_group, &entry, id, key, enabled);
}
}
MenuItem::SubMenu(name, submenu) => {
let item = GtkMenuItem::with_label(&name);
Expand Down Expand Up @@ -144,7 +141,39 @@ impl Menu {
}
}

fn register_accelerator(item: &GtkMenuItem, accel_group: &AccelGroup, menu_key: HotKey) {
fn add_menu_entry<
M: gtk::prelude::IsA<gtk::MenuShell>,
I: gtk::prelude::IsA<gtk::MenuItem> + gtk::prelude::IsA<gtk::Widget>,
>(
menu: &mut M,
handle: &WindowHandle,
accel_group: &AccelGroup,
item: &I,
id: u32,
key: Option<HotKey>,
enabled: bool,
) {
item.set_sensitive(enabled);

if let Some(k) = key {
register_accelerator(item, accel_group, k);
}

let handle = handle.clone();
item.connect_activate(move |_| {
if let Some(state) = handle.state.upgrade() {
state.handler.borrow_mut().command(id);
}
});

menu.append(item);
}

fn register_accelerator<M: GtkMenuItemExt + WidgetExt>(
item: &M,
accel_group: &AccelGroup,
menu_key: HotKey,
) {
let gdk_keyval = match &menu_key.key {
KbKey::Character(text) => text.chars().next().unwrap() as u32,
k => {
Expand Down
14 changes: 10 additions & 4 deletions druid-shell/src/backend/mac/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ pub struct Menu {
pub menu: id,
}

fn make_menu_item(id: u32, text: &str, key: Option<&HotKey>, enabled: bool, selected: bool) -> id {
fn make_menu_item(
id: u32,
text: &str,
key: Option<&HotKey>,
selected: Option<bool>,
enabled: bool,
) -> id {
let key_equivalent = key.map(HotKey::key_equivalent).unwrap_or("");
let stripped_text = strip_access_key(text);
unsafe {
Expand All @@ -49,7 +55,7 @@ fn make_menu_item(id: u32, text: &str, key: Option<&HotKey>, enabled: bool, sele
let () = msg_send![item, setEnabled: NO];
}

if selected {
if let Some(true) = selected {
let () = msg_send![item, setState: 1_isize];
}
item
Expand Down Expand Up @@ -90,10 +96,10 @@ impl Menu {
id: u32,
text: &str,
key: Option<&HotKey>,
selected: Option<bool>,
enabled: bool,
selected: bool,
) {
let menu_item = make_menu_item(id, text, key, enabled, selected);
let menu_item = make_menu_item(id, text, key, selected, enabled);
unsafe {
self.menu.addItem_(menu_item);
}
Expand Down
19 changes: 12 additions & 7 deletions druid-shell/src/backend/wayland/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@ impl Menu {
Menu
}

pub fn add_dropdown(&mut self, menu: Menu, text: &str, _enabled: bool) {}
pub fn add_dropdown(&mut self, menu: Menu, text: &str, _enabled: bool) {
tracing::warn!("unimplemented");
}

pub fn add_item(
&mut self,
id: u32,
text: &str,
key: Option<&HotKey>,
enabled: bool,
_selected: bool,
_id: u32,
_text: &str,
_key: Option<&HotKey>,
_selected: Option<bool>,
_enabled: bool,
) {
tracing::warn!("unimplemented");
}

pub fn add_separator(&mut self) {}
pub fn add_separator(&mut self) {
tracing::warn!("unimplemented");
}
}
2 changes: 1 addition & 1 deletion druid-shell/src/backend/web/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl Menu {
_id: u32,
_text: &str,
_key: Option<&HotKey>,
_selected: Option<bool>,
_enabled: bool,
_selected: bool,
) {
tracing::warn!("unimplemented");
}
Expand Down
4 changes: 2 additions & 2 deletions druid-shell/src/backend/windows/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ impl Menu {
id: u32,
text: &str,
key: Option<&HotKey>,
selected: Option<bool>,
enabled: bool,
selected: bool,
) {
let mut anno_text = text.to_string();
if let Some(key) = key {
Expand All @@ -112,7 +112,7 @@ impl Menu {
if !enabled {
flags |= MF_GRAYED;
}
if selected {
if let Some(true) = selected {
flags |= MF_CHECKED;
}
AppendMenuW(
Expand Down
2 changes: 1 addition & 1 deletion druid-shell/src/backend/x11/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ impl Menu {
_id: u32,
_text: &str,
_key: Option<&HotKey>,
_selected: Option<bool>,
_enabled: bool,
_selected: bool,
) {
// TODO(x11/menus): implement Menu::add_item (currently a no-op)
tracing::warn!("Menu::add_item is currently unimplemented for X11 backend.");
Expand Down
4 changes: 2 additions & 2 deletions druid-shell/src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ impl Menu {
id: u32,
text: &str,
key: Option<&HotKey>,
selected: Option<bool>,
enabled: bool,
selected: bool,
) {
self.0.add_item(id, text, key, enabled, selected)
self.0.add_item(id, text, key, selected, enabled)
}

/// Add a separator to the menu.
Expand Down
14 changes: 5 additions & 9 deletions druid/src/menu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ impl MenuBuildCtx {
id: u32,
text: &str,
key: Option<&HotKey>,
selected: Option<bool>,
enabled: bool,
selected: bool,
) {
self.current.add_item(id, text, key, enabled, selected);
self.current.add_item(id, text, key, selected, enabled);
}

fn add_separator(&mut self) {
Expand Down Expand Up @@ -700,11 +700,7 @@ impl<T: Data> MenuItem<T> {
let new_state = MenuItemState {
title: self.title.display_text(),
hotkey: self.hotkey.as_mut().and_then(|h| h(data, env)),
selected: self
.selected
.as_mut()
.map(|s| s(data, env))
.unwrap_or(false),
selected: self.selected.as_mut().map(|s| s(data, env)),
enabled: self.enabled.as_mut().map(|e| e(data, env)).unwrap_or(true),
};
let ret = self.old_state.as_ref() != Some(&new_state);
Expand Down Expand Up @@ -798,8 +794,8 @@ impl<T: Data> MenuVisitor<T> for MenuItem<T> {
self.id.0.map(|x| x.get()).unwrap_or(0),
&state.title,
state.hotkey.as_ref(),
state.enabled,
state.selected,
state.enabled,
);
}
}
Expand All @@ -820,7 +816,7 @@ impl<T: Data> MenuVisitor<T> for Separator {
struct MenuItemState {
title: ArcStr,
hotkey: Option<HotKey>,
selected: bool,
selected: Option<bool>,
enabled: bool,
}

Expand Down