mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-29 12:06:01 +02:00
Merge remote-tracking branch 'origin/v2' into feat/camera
This commit is contained in:
@@ -31,19 +31,29 @@ pub fn run() {
|
||||
.level(log::LevelFilter::Info)
|
||||
.build(),
|
||||
)
|
||||
.plugin(tauri_plugin_app::init())
|
||||
.plugin(tauri_plugin_fs::init())
|
||||
.plugin(tauri_plugin_clipboard::init())
|
||||
.plugin(tauri_plugin_clipboard_manager::init())
|
||||
.plugin(tauri_plugin_dialog::init())
|
||||
.plugin(tauri_plugin_http::init())
|
||||
.plugin(tauri_plugin_notification::init())
|
||||
.plugin(tauri_plugin_os::init())
|
||||
.plugin(tauri_plugin_process::init())
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.plugin(tauri_plugin_window::init())
|
||||
.setup(move |app| {
|
||||
#[cfg(desktop)]
|
||||
{
|
||||
tray::create_tray(app)?;
|
||||
tray::create_tray(app.handle())?;
|
||||
app.handle().plugin(tauri_plugin_cli::init())?;
|
||||
app.handle()
|
||||
.plugin(tauri_plugin_global_shortcut::Builder::new().build())?;
|
||||
app.handle()
|
||||
.plugin(tauri_plugin_updater::Builder::new().build())?;
|
||||
}
|
||||
#[cfg(mobile)]
|
||||
{
|
||||
app.handle().plugin(tauri_plugin_barcode_scanner::init())?;
|
||||
}
|
||||
|
||||
#[cfg(mobile)]
|
||||
@@ -55,7 +65,7 @@ pub fn run() {
|
||||
#[cfg(desktop)]
|
||||
{
|
||||
window_builder = window_builder
|
||||
.user_agent("Tauri API")
|
||||
.user_agent(&format!("Tauri API - {}", std::env::consts::OS))
|
||||
.title("Tauri API Validation")
|
||||
.inner_size(1000., 800.)
|
||||
.min_inner_size(600., 400.)
|
||||
@@ -70,6 +80,11 @@ pub fn run() {
|
||||
.decorations(false);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
window_builder = window_builder.transparent(true);
|
||||
}
|
||||
|
||||
let window = window_builder.build().unwrap();
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
@@ -118,7 +133,7 @@ pub fn run() {
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
builder = builder.menu(tauri::Menu::os_default("Tauri API Validation"));
|
||||
builder = builder.menu(tauri::menu::Menu::default);
|
||||
}
|
||||
|
||||
#[allow(unused_mut)]
|
||||
|
||||
@@ -4,140 +4,113 @@
|
||||
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use tauri::{
|
||||
CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, WindowBuilder, WindowUrl,
|
||||
menu::{Menu, MenuItem},
|
||||
tray::{ClickType, TrayIconBuilder},
|
||||
Manager, Runtime, WindowBuilder, WindowUrl,
|
||||
};
|
||||
use tauri_plugin_dialog::DialogExt;
|
||||
use tauri_plugin_shell::ShellExt;
|
||||
|
||||
pub fn create_tray(app: &tauri::App) -> tauri::Result<()> {
|
||||
let mut tray_menu1 = SystemTrayMenu::new()
|
||||
.add_item(CustomMenuItem::new("toggle", "Toggle"))
|
||||
.add_item(CustomMenuItem::new("new", "New window"))
|
||||
.add_item(CustomMenuItem::new("icon_1", "Tray Icon 1"))
|
||||
.add_item(CustomMenuItem::new("icon_2", "Tray Icon 2"));
|
||||
|
||||
pub fn create_tray<R: Runtime>(app: &tauri::AppHandle<R>) -> tauri::Result<()> {
|
||||
let toggle_i = MenuItem::with_id(app, "toggle", "Toggle", true, None);
|
||||
let new_window_i = MenuItem::with_id(app, "new-window", "New window", true, None);
|
||||
let icon_i_1 = MenuItem::with_id(app, "icon-1", "Icon 1", true, None);
|
||||
let icon_i_2 = MenuItem::with_id(app, "icon-2", "Icon 2", true, None);
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
tray_menu1 = tray_menu1.add_item(CustomMenuItem::new("set_title", "Set Title"));
|
||||
}
|
||||
let set_title_i = MenuItem::with_id(app, "set-title", "Set Title", true, None);
|
||||
let switch_i = MenuItem::with_id(app, "switch-menu", "Switch Menu", true, None);
|
||||
let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None);
|
||||
let remove_tray_i = MenuItem::with_id(app, "remove-tray", "Remove Tray icon", true, None);
|
||||
let menu1 = Menu::with_items(
|
||||
app,
|
||||
&[
|
||||
&toggle_i,
|
||||
&new_window_i,
|
||||
&icon_i_1,
|
||||
&icon_i_2,
|
||||
#[cfg(target_os = "macos")]
|
||||
&set_title_i,
|
||||
&switch_i,
|
||||
&quit_i,
|
||||
&remove_tray_i,
|
||||
],
|
||||
)?;
|
||||
let menu2 = Menu::with_items(
|
||||
app,
|
||||
&[&toggle_i, &new_window_i, &switch_i, &quit_i, &remove_tray_i],
|
||||
)?;
|
||||
|
||||
tray_menu1 = tray_menu1
|
||||
.add_item(CustomMenuItem::new("switch_menu", "Switch Menu"))
|
||||
.add_item(CustomMenuItem::new("about", "About"))
|
||||
.add_item(CustomMenuItem::new("exit_app", "Quit"))
|
||||
.add_item(CustomMenuItem::new("destroy", "Destroy"));
|
||||
|
||||
let tray_menu2 = SystemTrayMenu::new()
|
||||
.add_item(CustomMenuItem::new("toggle", "Toggle"))
|
||||
.add_item(CustomMenuItem::new("new", "New window"))
|
||||
.add_item(CustomMenuItem::new("switch_menu", "Switch Menu"))
|
||||
.add_item(CustomMenuItem::new("about", "About"))
|
||||
.add_item(CustomMenuItem::new("exit_app", "Quit"))
|
||||
.add_item(CustomMenuItem::new("destroy", "Destroy"));
|
||||
let is_menu1 = AtomicBool::new(true);
|
||||
|
||||
let handle = app.handle();
|
||||
let tray_id = "my-tray".to_string();
|
||||
SystemTray::new()
|
||||
.with_id(&tray_id)
|
||||
.with_menu(tray_menu1.clone())
|
||||
.with_tooltip("Tauri")
|
||||
.on_event(move |event| {
|
||||
let tray_handle = handle.tray_handle_by_id(&tray_id).unwrap();
|
||||
match event {
|
||||
SystemTrayEvent::LeftClick {
|
||||
position: _,
|
||||
size: _,
|
||||
..
|
||||
} => {
|
||||
let window = handle.get_window("main").unwrap();
|
||||
window.show().unwrap();
|
||||
window.set_focus().unwrap();
|
||||
let _ = TrayIconBuilder::with_id("tray-1")
|
||||
.tooltip("Tauri")
|
||||
.icon(app.default_window_icon().unwrap().clone())
|
||||
.menu(&menu1)
|
||||
.menu_on_left_click(false)
|
||||
.on_menu_event(move |app, event| match event.id.as_ref() {
|
||||
"quit" => {
|
||||
app.exit(0);
|
||||
}
|
||||
"remove-tray" => {
|
||||
app.remove_tray_by_id("tray-1");
|
||||
}
|
||||
"toggle" => {
|
||||
if let Some(window) = app.get_window("main") {
|
||||
let new_title = if window.is_visible().unwrap_or_default() {
|
||||
let _ = window.hide();
|
||||
"Show"
|
||||
} else {
|
||||
let _ = window.show();
|
||||
let _ = window.set_focus();
|
||||
"Hide"
|
||||
};
|
||||
toggle_i.set_text(new_title).unwrap();
|
||||
}
|
||||
SystemTrayEvent::MenuItemClick { id, .. } => {
|
||||
let item_handle = tray_handle.get_item(&id);
|
||||
match id.as_str() {
|
||||
"exit_app" => {
|
||||
// exit the app
|
||||
handle.exit(0);
|
||||
}
|
||||
"destroy" => {
|
||||
tray_handle.destroy().unwrap();
|
||||
}
|
||||
"toggle" => {
|
||||
let window = handle.get_window("main").unwrap();
|
||||
let new_title = if window.is_visible().unwrap() {
|
||||
window.hide().unwrap();
|
||||
"Show"
|
||||
} else {
|
||||
window.show().unwrap();
|
||||
"Hide"
|
||||
};
|
||||
item_handle.set_title(new_title).unwrap();
|
||||
}
|
||||
"new" => {
|
||||
WindowBuilder::new(&handle, "new", WindowUrl::App("index.html".into()))
|
||||
.title("Tauri")
|
||||
.build()
|
||||
.unwrap();
|
||||
}
|
||||
"set_title" => {
|
||||
#[cfg(target_os = "macos")]
|
||||
tray_handle.set_title("Tauri").unwrap();
|
||||
}
|
||||
"icon_1" => {
|
||||
#[cfg(target_os = "macos")]
|
||||
tray_handle.set_icon_as_template(true).unwrap();
|
||||
|
||||
tray_handle
|
||||
.set_icon(tauri::Icon::Raw(
|
||||
include_bytes!("../icons/tray_icon_with_transparency.png")
|
||||
.to_vec(),
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
"icon_2" => {
|
||||
#[cfg(target_os = "macos")]
|
||||
tray_handle.set_icon_as_template(true).unwrap();
|
||||
|
||||
tray_handle
|
||||
.set_icon(tauri::Icon::Raw(
|
||||
include_bytes!("../icons/icon.ico").to_vec(),
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
"switch_menu" => {
|
||||
let flag = is_menu1.load(Ordering::Relaxed);
|
||||
let (menu, tooltip) = if flag {
|
||||
(tray_menu2.clone(), "Menu 2")
|
||||
} else {
|
||||
(tray_menu1.clone(), "Tauri")
|
||||
};
|
||||
tray_handle.set_menu(menu).unwrap();
|
||||
tray_handle.set_tooltip(tooltip).unwrap();
|
||||
is_menu1.store(!flag, Ordering::Relaxed);
|
||||
}
|
||||
"about" => {
|
||||
let window = handle.get_window("main").unwrap();
|
||||
window
|
||||
.dialog()
|
||||
.message("Tauri demo app")
|
||||
.title("About app")
|
||||
.parent(&window)
|
||||
.ok_button_label("Homepage")
|
||||
.cancel_button_label("Cancel")
|
||||
.show(move |ok| {
|
||||
if ok {
|
||||
window.shell().open("https://tauri.app/", None).unwrap();
|
||||
}
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
"new-window" => {
|
||||
let _ = WindowBuilder::new(app, "new", WindowUrl::App("index.html".into()))
|
||||
.title("Tauri")
|
||||
.build();
|
||||
}
|
||||
#[cfg(target_os = "macos")]
|
||||
"set-title" => {
|
||||
if let Some(tray) = app.tray_by_id("tray-1") {
|
||||
let _ = tray.set_title(Some("Tauri"));
|
||||
}
|
||||
}
|
||||
i @ "icon-1" | i @ "icon-2" => {
|
||||
if let Some(tray) = app.tray_by_id("tray-1") {
|
||||
let _ = tray.set_icon(Some(tauri::Icon::Raw(if i == "icon-1" {
|
||||
include_bytes!("../icons/icon.ico").to_vec()
|
||||
} else {
|
||||
include_bytes!("../icons/tray_icon_with_transparency.png").to_vec()
|
||||
})));
|
||||
}
|
||||
}
|
||||
"switch-menu" => {
|
||||
let flag = is_menu1.load(Ordering::Relaxed);
|
||||
let (menu, tooltip) = if flag {
|
||||
(menu2.clone(), "Menu 2")
|
||||
} else {
|
||||
(menu1.clone(), "Tauri")
|
||||
};
|
||||
if let Some(tray) = app.tray_by_id("tray-1") {
|
||||
let _ = tray.set_menu(Some(menu));
|
||||
let _ = tray.set_tooltip(Some(tooltip));
|
||||
}
|
||||
is_menu1.store(!flag, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
_ => {}
|
||||
})
|
||||
.on_tray_event(|tray, event| {
|
||||
if event.click_type == ClickType::Left {
|
||||
let app = tray.app_handle();
|
||||
if let Some(window) = app.get_window("main") {
|
||||
let _ = window.show();
|
||||
let _ = window.set_focus();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
})
|
||||
.build(app)
|
||||
.map(|_| ())
|
||||
.build(app);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user