#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
fn main() -> wry::Result<()> {
use std::collections::HashMap;
#[cfg(target_os = "linux")]
use std::path::Path;
#[cfg(target_os = "macos")]
use wry::application::platform::macos::{
ActivationPolicy, CustomMenuItemExtMacOS, EventLoopExtMacOS, NativeImage,
};
#[cfg(target_os = "linux")]
use wry::application::platform::unix::WindowBuilderExtUnix;
#[cfg(target_os = "windows")]
use wry::application::platform::windows::WindowBuilderExtWindows;
use wry::{
application::{
accelerator::{Accelerator, SysMods},
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
global_shortcut::ShortcutManager,
keyboard::KeyCode,
menu::{ContextMenu, MenuItemAttributes, MenuType},
system_tray::SystemTrayBuilder,
window::{WindowBuilder, WindowId},
},
webview::{WebView, WebViewBuilder},
};
let index_html = r#"
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<h1>Welcome to WRY!</h1>
<textarea style="width: 90vw; height: 30vh;"></textarea>
</body>
</html>"#;
#[cfg(target_os = "macos")]
let mut event_loop = EventLoop::new();
#[cfg(not(target_os = "macos"))]
let event_loop = EventLoop::new();
#[cfg(target_os = "macos")]
event_loop.set_activation_policy(ActivationPolicy::Accessory);
let mut webviews: HashMap<WindowId, WebView> = HashMap::new();
let mut shortcut_manager = ShortcutManager::new(&event_loop);
let my_accelerator = Accelerator::new(SysMods::CmdShift, KeyCode::Digit0);
let global_shortcut = shortcut_manager.register(my_accelerator.clone()).unwrap();
let mut tray_menu = ContextMenu::new();
let open_new_window = tray_menu.add_item(MenuItemAttributes::new("Open new window"));
let quit_item = tray_menu.add_item(MenuItemAttributes::new("Quit"));
#[cfg(target_os = "macos")]
open_new_window
.clone()
.set_native_image(NativeImage::StatusAvailable);
#[cfg(target_os = "windows")]
let icon = include_bytes!("icon.ico").to_vec();
#[cfg(target_os = "macos")]
let icon = include_bytes!("icon.png").to_vec();
#[cfg(target_os = "linux")]
let icon = Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/icon.png");
#[cfg(target_os = "windows")]
let new_icon = include_bytes!("icon_blue.ico").to_vec();
#[cfg(target_os = "macos")]
let new_icon = include_bytes!("icon_dark.png").to_vec();
#[cfg(target_os = "linux")]
let new_icon = Path::new(env!("CARGO_MANIFEST_DIR")).join("examples/icon_dark.png");
let mut system_tray = SystemTrayBuilder::new(icon.clone(), Some(tray_menu))
.build(&event_loop)
.unwrap();
event_loop.run(move |event, event_loop, control_flow| {
*control_flow = ControlFlow::Wait;
let mut create_window_or_focus = || {
if !webviews.is_empty() {
for window in webviews.values() {
window.window().set_focus();
}
return;
}
shortcut_manager
.unregister(global_shortcut.clone())
.unwrap();
#[cfg(any(target_os = "windows", target_os = "linux"))]
let window_builder = WindowBuilder::new().with_skip_taskbar(true);
#[cfg(target_os = "macos")]
let window_builder = WindowBuilder::new();
let window = window_builder.build(event_loop).unwrap();
let id = window.id();
let webview = WebViewBuilder::new(window)
.unwrap()
.with_custom_protocol("wry.dev".into(), move |_uri| {
Ok((index_html.as_bytes().into(), "text/html".into()))
})
.with_url("wry.dev://")
.unwrap()
.build()
.unwrap();
webviews.insert(id, webview);
let mut open_new_window = open_new_window.clone();
open_new_window.set_enabled(false);
open_new_window.set_title("Window already open");
open_new_window.set_selected(true);
system_tray.set_icon(new_icon.clone());
#[cfg(target_os = "macos")]
open_new_window.set_native_image(NativeImage::StatusUnavailable);
};
match event {
Event::NewEvents(StartCause::Init) => println!("Wry has started!"),
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
..
} => {
println!("Window {:?} has received the signal to close", window_id);
let mut open_new_window = open_new_window.clone();
webviews.remove(&window_id);
open_new_window.set_enabled(true);
open_new_window.set_title("Open new window");
open_new_window.set_selected(false);
system_tray.set_icon(icon.clone());
shortcut_manager.register(my_accelerator.clone()).unwrap();
#[cfg(target_os = "macos")]
open_new_window.set_native_image(NativeImage::StatusAvailable);
}
#[cfg(target_os = "windows")]
Event::TrayEvent {
event: tao::event::TrayEvent::LeftClick,
..
} => create_window_or_focus(),
Event::MenuEvent {
menu_id,
origin: MenuType::ContextMenu,
..
} => {
if menu_id == open_new_window.clone().id() {
create_window_or_focus();
}
if menu_id == quit_item.clone().id() {
*control_flow = ControlFlow::Exit;
}
println!("Clicked on {:?}", menu_id);
}
Event::GlobalShortcutEvent(hotkey_id) if hotkey_id == my_accelerator.clone().id() => {
create_window_or_focus()
}
_ => (),
}
});
}
#[cfg(target_os = "ios")]
fn main() {
println!("This platform doesn't support system_tray.");
}