Tauri update to beta
This commit is contained in:
@@ -16,7 +16,7 @@ mod portmaster;
|
||||
mod traymenu;
|
||||
mod window;
|
||||
|
||||
use log::{debug, error, info, trace, warn};
|
||||
use log::{debug, error, info};
|
||||
use portmaster::PortmasterExt;
|
||||
use traymenu::setup_tray_menu;
|
||||
use window::{close_splash_window, create_main_window};
|
||||
@@ -111,7 +111,7 @@ fn main() {
|
||||
// Setup the single-instance event listener that will create/focus the main window
|
||||
// or the splash-screen.
|
||||
let handle = app.handle().clone();
|
||||
app.listen_global("single-instance", move |_event| {
|
||||
app.listen("single-instance", move |_event| {
|
||||
let _ = window::open_window(&handle);
|
||||
});
|
||||
|
||||
@@ -196,7 +196,7 @@ fn main() {
|
||||
);
|
||||
|
||||
api.prevent_close();
|
||||
if let Some(window) = handle.get_window(label.as_str()) {
|
||||
if let Some(window) = handle.get_webview_window(label.as_str()) {
|
||||
let _ = window.emit("exit-requested", "");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,14 @@ use std::sync::Mutex;
|
||||
|
||||
use log::{debug, error};
|
||||
use tauri::{
|
||||
image::Image,
|
||||
menu::{
|
||||
CheckMenuItem, CheckMenuItemBuilder, MenuBuilder, MenuItemBuilder, PredefinedMenuItem,
|
||||
SubmenuBuilder,
|
||||
},
|
||||
tray::{ClickType, TrayIcon, TrayIconBuilder},
|
||||
Icon, Manager, Wry,
|
||||
Manager, Wry,
|
||||
};
|
||||
use tauri_plugin_dialog::DialogExt;
|
||||
|
||||
use crate::{
|
||||
portapi::{
|
||||
@@ -26,6 +26,7 @@ use crate::{
|
||||
portmaster::PortmasterExt,
|
||||
window::{create_main_window, may_navigate_to_ui, open_window},
|
||||
};
|
||||
use tauri_plugin_dialog::DialogExt;
|
||||
|
||||
pub type AppIcon = TrayIcon<Wry>;
|
||||
|
||||
@@ -34,32 +35,32 @@ lazy_static! {
|
||||
static ref SPN_BUTTON: Mutex<Option<CheckMenuItem<Wry>>> = Mutex::new(None);
|
||||
}
|
||||
|
||||
const PM_TRAY_ICON_ID: &'static str = "pm_icon";
|
||||
|
||||
// Icons
|
||||
//
|
||||
const BLUE_ICON: &'static [u8] =
|
||||
include_bytes!("../../assets/icons/pm_light_blue_512.ico");
|
||||
const RED_ICON: &'static [u8] =
|
||||
include_bytes!("../../assets/icons/pm_light_red_512.ico");
|
||||
const YELLOW_ICON: &'static [u8] =
|
||||
include_bytes!("../../assets/icons/pm_light_yellow_512.ico");
|
||||
const GREEN_ICON: &'static [u8] =
|
||||
include_bytes!("../../assets/icons/pm_light_green_512.ico");
|
||||
const BLUE_ICON: &'static [u8] = include_bytes!("../../assets/icons/pm_light_blue_512.ico");
|
||||
const RED_ICON: &'static [u8] = include_bytes!("../../assets/icons/pm_light_red_512.ico");
|
||||
const YELLOW_ICON: &'static [u8] = include_bytes!("../../assets/icons/pm_light_yellow_512.ico");
|
||||
const GREEN_ICON: &'static [u8] = include_bytes!("../../assets/icons/pm_light_green_512.ico");
|
||||
|
||||
pub fn setup_tray_menu(
|
||||
app: &mut tauri::App,
|
||||
) -> core::result::Result<AppIcon, Box<dyn std::error::Error>> {
|
||||
// Tray menu
|
||||
let close_btn = MenuItemBuilder::with_id("close", "Exit").build(app);
|
||||
let open_btn = MenuItemBuilder::with_id("open", "Open").build(app);
|
||||
let close_btn = MenuItemBuilder::with_id("close", "Exit").build(app)?;
|
||||
let open_btn = MenuItemBuilder::with_id("open", "Open").build(app)?;
|
||||
|
||||
let spn = CheckMenuItemBuilder::with_id("spn", "Use SPN").build(app);
|
||||
let spn = CheckMenuItemBuilder::with_id("spn", "Use SPN")
|
||||
.build(app)
|
||||
.unwrap();
|
||||
|
||||
// Store the SPN button reference
|
||||
let mut button_ref = SPN_BUTTON.lock().unwrap();
|
||||
let mut button_ref = SPN_BUTTON.lock()?;
|
||||
*button_ref = Some(spn.clone());
|
||||
|
||||
let force_show_window = MenuItemBuilder::with_id("force-show", "Force Show UI").build(app);
|
||||
let reload_btn = MenuItemBuilder::with_id("reload", "Reload User Interface").build(app);
|
||||
let force_show_window = MenuItemBuilder::with_id("force-show", "Force Show UI").build(app)?;
|
||||
let reload_btn = MenuItemBuilder::with_id("reload", "Reload User Interface").build(app)?;
|
||||
let developer_menu = SubmenuBuilder::new(app, "Developer")
|
||||
.items(&[&reload_btn, &force_show_window])
|
||||
.build()?;
|
||||
@@ -70,15 +71,15 @@ pub fn setup_tray_menu(
|
||||
let menu = MenuBuilder::new(app)
|
||||
.items(&[
|
||||
&spn,
|
||||
&PredefinedMenuItem::separator(app),
|
||||
&PredefinedMenuItem::separator(app)?,
|
||||
&open_btn,
|
||||
&close_btn,
|
||||
&developer_menu,
|
||||
])
|
||||
.build()?;
|
||||
|
||||
let icon = TrayIconBuilder::new()
|
||||
.icon(Icon::Raw(RED_ICON.to_vec()))
|
||||
let icon = TrayIconBuilder::with_id(PM_TRAY_ICON_ID)
|
||||
.icon(Image::from_bytes(RED_ICON).unwrap())
|
||||
.menu(&menu)
|
||||
.on_menu_event(move |app, event| match event.id().as_ref() {
|
||||
"close" => {
|
||||
@@ -136,7 +137,6 @@ pub fn setup_tray_menu(
|
||||
}
|
||||
})
|
||||
.build(app)?;
|
||||
|
||||
Ok(icon)
|
||||
}
|
||||
|
||||
@@ -166,11 +166,11 @@ pub fn update_icon(icon: AppIcon, subsystems: HashMap<String, Subsystem>, spn_st
|
||||
},
|
||||
};
|
||||
|
||||
_ = icon.set_icon(Some(Icon::Raw(next_icon.to_vec())));
|
||||
_ = icon.set_icon(Some(Image::from_bytes(next_icon).unwrap()));
|
||||
}
|
||||
|
||||
pub async fn tray_handler(cli: PortAPI, app: tauri::AppHandle) {
|
||||
let icon = match app.tray() {
|
||||
let icon = match app.tray_by_id(PM_TRAY_ICON_ID) {
|
||||
Some(icon) => icon,
|
||||
None => {
|
||||
error!("cancel try_handler: missing try icon");
|
||||
@@ -226,7 +226,7 @@ pub async fn tray_handler(cli: PortAPI, app: tauri::AppHandle) {
|
||||
}
|
||||
};
|
||||
|
||||
_ = icon.set_icon(Some(Icon::Raw(BLUE_ICON.to_vec())));
|
||||
_ = icon.set_icon(Some(Image::from_bytes(BLUE_ICON).unwrap()));
|
||||
|
||||
let mut subsystems: HashMap<String, Subsystem> = HashMap::new();
|
||||
let mut spn_status: String = "".to_string();
|
||||
@@ -340,5 +340,5 @@ pub async fn tray_handler(cli: PortAPI, app: tauri::AppHandle) {
|
||||
_ = btn.set_checked(false);
|
||||
}
|
||||
|
||||
_ = icon.set_icon(Some(Icon::Raw(RED_ICON.to_vec())));
|
||||
_ = icon.set_icon(Some(Image::from_bytes(RED_ICON).unwrap()));
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use log::{debug, error};
|
||||
use tauri::{AppHandle, Manager, Result, UserAttentionType, Window, WindowBuilder, WindowUrl};
|
||||
use tauri::{
|
||||
AppHandle, Manager, Result, UserAttentionType, WebviewUrl, WebviewWindow, WebviewWindowBuilder,
|
||||
};
|
||||
|
||||
use crate::portmaster::PortmasterExt;
|
||||
|
||||
@@ -11,15 +13,15 @@ use crate::portmaster::PortmasterExt;
|
||||
/// if ::websocket::is_portapi_reachable returns true.
|
||||
///
|
||||
/// Either the existing or the newly created window is returned.
|
||||
pub fn create_main_window(app: &AppHandle) -> Result<Window> {
|
||||
let mut window = if let Some(window) = app.get_window("main") {
|
||||
pub fn create_main_window(app: &AppHandle) -> Result<WebviewWindow> {
|
||||
let mut window = if let Some(window) = app.get_webview_window("main") {
|
||||
debug!("[tauri] main window already created");
|
||||
|
||||
window
|
||||
} else {
|
||||
debug!("[tauri] creating main window");
|
||||
|
||||
let res = WindowBuilder::new(app, "main", WindowUrl::App("index.html".into()))
|
||||
let res = WebviewWindowBuilder::new(app, "main", WebviewUrl::App("index.html".into()))
|
||||
.visible(false)
|
||||
.build();
|
||||
|
||||
@@ -54,12 +56,12 @@ pub fn create_main_window(app: &AppHandle) -> Result<Window> {
|
||||
Ok(window)
|
||||
}
|
||||
|
||||
pub fn create_splash_window(app: &AppHandle) -> Result<Window> {
|
||||
if let Some(window) = app.get_window("splash") {
|
||||
pub fn create_splash_window(app: &AppHandle) -> Result<WebviewWindow> {
|
||||
if let Some(window) = app.get_webview_window("splash") {
|
||||
let _ = window.show();
|
||||
Ok(window)
|
||||
} else {
|
||||
let window = WindowBuilder::new(app, "splash", WindowUrl::App("index.html".into()))
|
||||
let window = WebviewWindowBuilder::new(app, "splash", WebviewUrl::App("index.html".into()))
|
||||
.center()
|
||||
.closable(false)
|
||||
.focused(true)
|
||||
@@ -76,7 +78,7 @@ pub fn create_splash_window(app: &AppHandle) -> Result<Window> {
|
||||
}
|
||||
|
||||
pub fn close_splash_window(app: &AppHandle) -> Result<()> {
|
||||
if let Some(window) = app.get_window("splash") {
|
||||
if let Some(window) = app.get_webview_window("splash") {
|
||||
return window.close();
|
||||
}
|
||||
return Err(tauri::Error::WindowNotFound);
|
||||
@@ -94,9 +96,9 @@ pub fn close_splash_window(app: &AppHandle) -> Result<()> {
|
||||
///
|
||||
/// If the Portmaster API is unreachable and there's no main window yet, we show the
|
||||
/// splash-screen window.
|
||||
pub fn open_window(app: &AppHandle) -> Result<Window> {
|
||||
pub fn open_window(app: &AppHandle) -> Result<WebviewWindow> {
|
||||
if app.portmaster().is_reachable() {
|
||||
match app.get_window("main") {
|
||||
match app.get_webview_window("main") {
|
||||
Some(win) => {
|
||||
app.portmaster().show_window();
|
||||
|
||||
@@ -122,7 +124,7 @@ pub fn open_window(app: &AppHandle) -> Result<Window> {
|
||||
/// In #[cfg(debug_assertions)] the TAURI_PM_URL environment variable will be used
|
||||
/// if set.
|
||||
/// Otherwise or in release builds, it will be navigated to http://127.0.0.1:817.
|
||||
pub fn may_navigate_to_ui(win: &mut Window, force: bool) {
|
||||
pub fn may_navigate_to_ui(win: &mut WebviewWindow, force: bool) {
|
||||
if !win.app_handle().portmaster().is_reachable() && !force {
|
||||
error!("[tauri] portmaster API is not reachable, not navigating");
|
||||
|
||||
@@ -148,6 +150,9 @@ pub fn may_navigate_to_ui(win: &mut Window, force: bool) {
|
||||
#[cfg(not(debug_assertions))]
|
||||
win.navigate("http://localhost:817".parse().unwrap());
|
||||
} else {
|
||||
error!("not navigating to user interface: current url: {}", win.url().as_str());
|
||||
error!(
|
||||
"not navigating to user interface: current url: {}",
|
||||
win.url().as_str()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user