-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
main.rs
72 lines (66 loc) · 1.95 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
mod cmd;
mod menu;
use serde::Serialize;
use tauri::{CustomMenuItem, Manager, SystemTrayMenuItem, WindowBuilder, WindowUrl};
#[derive(Serialize)]
struct Reply {
data: String,
}
fn main() {
tauri::Builder::default()
.on_page_load(|window, _| {
let window_ = window.clone();
window.listen("js-event", move |event| {
println!("got js-event with message '{:?}'", event.payload());
let reply = Reply {
data: "something else".to_string(),
};
window_
.emit("rust-event", Some(reply))
.expect("failed to emit");
});
})
.menu(menu::get_menu())
.on_menu_event(|event| {
println!("{:?}", event.menu_item_id());
})
.system_tray(vec![
SystemTrayMenuItem::Custom(CustomMenuItem::new("toggle".into(), "Toggle")),
SystemTrayMenuItem::Custom(CustomMenuItem::new("new".into(), "New window")),
])
.on_system_tray_event(|app, event| {
match event.menu_item_id().as_str() {
"toggle" => {
let window = app.get_window("main").unwrap();
if window.is_visible().unwrap() {
window.hide().unwrap();
} else {
window.show().unwrap();
}
}
"new" => app
.create_window(
"new".into(),
WindowUrl::App("index.html".into()),
|window_builder, webview_attributes| {
(window_builder.title("Tauri"), webview_attributes)
},
)
.unwrap(),
_ => {}
}
})
.invoke_handler(tauri::generate_handler![
cmd::log_operation,
cmd::perform_request
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}