Skip to content

Commit

Permalink
bump tao to 0.3 with examples (#294)
Browse files Browse the repository at this point in the history
* refactory(example): better tray & menu example

* add `system_tray_no_menu` for different behaviours

* fix windoz

* update readme and fix linux

* fmt

* bump tao

* add changelog

* use `SysMods` for `Accelerator` example
  • Loading branch information
lemarier authored Jun 21, 2021
1 parent 4d62cf5 commit cd4697e
Show file tree
Hide file tree
Showing 8 changed files with 393 additions and 93 deletions.
7 changes: 7 additions & 0 deletions .changes/update-tao.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wry": patch
---

Bump tao to `0.3` and add more examples.

_For more details, please refer to `tao` changelog._
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0"
thiserror = "1.0"
url = "2.2"
tao = { version = "0.2.6", default-features = false, features = [ "serde" ] }
tao = { version = "0.3.0", default-features = false, features = [ "serde" ] }

[dev-dependencies]
anyhow = "1.0.41"
Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ Run the `cargo run --example <file_name>` to see how each example works.
- `detect_js_ecma`: detects which versions of ECMAScript is supported by the webview.
- `menu_bar`: uses a custom menu for the application in macOS and the Window and Linux/Windows.
- `status_bar`: launch the application with tray icon and custom menu.
- `system_tray`: sample tray application with different behaviours.
- `system_tray_no_menu`: open window on tray icon left click.
- `html_test`: launch html5tests.com for debugging purpose.
- `validate_webview`: validate webview version before launching application.
Binary file added examples/icon_blue.ico
Binary file not shown.
Binary file added examples/icon_dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
126 changes: 53 additions & 73 deletions examples/menu_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,89 +6,67 @@
fn main() -> wry::Result<()> {
use wry::{
application::{
accelerator::{Accelerator, SysMods},
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
menu::{Menu, MenuItem, MenuType},
keyboard::KeyCode,
menu::{MenuBar as Menu, MenuItem, MenuItemAttributes, MenuType},
window::WindowBuilder,
},
webview::WebViewBuilder,
};
// Build our event loop
let event_loop = EventLoop::new();

// `Primary` is a platform-agnostic accelerator modifier.
// On Windows and Linux, `Primary` maps to the `Ctrl` key,
// and on macOS it maps to the `command` key.
let custom_print_menu = MenuItem::new("Print").with_accelerators("<Primary>p");
let other_test_menu = MenuItem::new("Custom").with_accelerators("<Primary>M");
let quit_menu = MenuItem::new("Quit").with_accelerators("<Primary>q");
let custom_print_menu_id = custom_print_menu.id();
let quit_menu_id = quit_menu.id();
// create main menubar menu
let mut menu_bar_menu = Menu::new();

// macOS require to have at least Copy, Paste, Select all etc..
// to works fine. You should always add them.
#[cfg(any(target_os = "linux", target_os = "macos"))]
let menu = vec![
Menu::new(
// on macOS first menu is always app name
"my custom app",
vec![
// All's non-custom menu, do NOT return event's
// they are handled by the system automatically
MenuItem::About("Todos".to_string()),
MenuItem::Services,
MenuItem::Separator,
MenuItem::Hide,
MenuItem::HideOthers,
MenuItem::ShowAll,
MenuItem::Separator,
quit_menu,
],
),
Menu::new(
"File",
vec![
custom_print_menu,
MenuItem::Separator,
other_test_menu,
MenuItem::CloseWindow,
],
),
Menu::new(
"Edit",
vec![
MenuItem::Undo,
MenuItem::Redo,
MenuItem::Separator,
MenuItem::Cut,
MenuItem::Copy,
MenuItem::Paste,
MenuItem::Separator,
MenuItem::SelectAll,
],
),
Menu::new("View", vec![MenuItem::EnterFullScreen]),
Menu::new("Window", vec![MenuItem::Minimize, MenuItem::Zoom]),
Menu::new(
"Help",
vec![MenuItem::new("Custom help").with_accelerators("<Primary><Shift>h")],
),
];
// create `first_menu`
let mut first_menu = Menu::new();

// Attention, Windows only support custom menu for now.
// If we add any `MenuItem::*` they'll not render
// We need to use custom menu with `Menu::new()` and catch
// the events in the EventLoop.
#[cfg(target_os = "windows")]
let menu = vec![
Menu::new("File", vec![other_test_menu]),
Menu::new("Other menu", vec![quit_menu]),
];
// create second menu
let mut second_menu = Menu::new();

// create third menu
let mut third_menu = Menu::new();

// create an empty menu to be used as submenu
let mut my_sub_menu = Menu::new();

let mut print_item = my_sub_menu.add_item(
MenuItemAttributes::new("Print")
.with_accelerators(&Accelerator::new(SysMods::Cmd, KeyCode::KeyP)),
);

first_menu.add_native_item(MenuItem::About("Todos".to_string()));
first_menu.add_native_item(MenuItem::Services);
first_menu.add_native_item(MenuItem::Separator);
first_menu.add_native_item(MenuItem::Hide);
first_menu.add_native_item(MenuItem::HideOthers);
first_menu.add_native_item(MenuItem::ShowAll);
let quit_item = first_menu.add_item(
MenuItemAttributes::new("Quit")
.with_accelerators(&Accelerator::new(SysMods::Cmd, KeyCode::KeyQ)),
);

third_menu.add_item(
MenuItemAttributes::new("Custom help")
.with_accelerators(&Accelerator::new(SysMods::CmdShift, KeyCode::KeyH)),
);

second_menu.add_submenu("Sub menu", true, my_sub_menu);
second_menu.add_native_item(MenuItem::Copy);
second_menu.add_native_item(MenuItem::Paste);
second_menu.add_native_item(MenuItem::SelectAll);

menu_bar_menu.add_submenu("First menu", true, first_menu);
menu_bar_menu.add_submenu("Second menu", true, second_menu);
menu_bar_menu.add_submenu("Help", true, third_menu);

// Build our event loop
let event_loop = EventLoop::new();
// Build the window
let window = WindowBuilder::new()
.with_title("Hello World")
.with_menu(menu)
.with_menu(menu_bar_menu)
.build(&event_loop)?;
// Build the webview
let webview = WebViewBuilder::new(window)?
Expand All @@ -107,14 +85,16 @@ fn main() -> wry::Result<()> {
// Catch menu events
Event::MenuEvent {
menu_id,
origin: MenuType::Menubar,
origin: MenuType::MenuBar,
} => {
// The custom menu expose an `id()` function to match with `menu_id` from the Event
if menu_id == custom_print_menu_id {
if menu_id == print_item.clone().id() {
// the webview.print() is only working on macOS for now
webview.print().expect("Unable to print");
// limit print to a single-use
print_item.set_enabled(false);
}
if menu_id == quit_menu_id {
if menu_id == quit_item.clone().id() {
// when we click on quit, let's close the app
*control_flow = ControlFlow::Exit;
}
Expand Down
Loading

0 comments on commit cd4697e

Please sign in to comment.