From d629530e5cd9fdbc70d878ec99f521770c28c699 Mon Sep 17 00:00:00 2001 From: lars-berger Date: Wed, 27 Nov 2024 19:50:46 +0800 Subject: [PATCH] feat: open settings form to given route --- Cargo.lock | 1 + packages/desktop/Cargo.toml | 1 + packages/desktop/src/sys_tray.rs | 44 +++++++++---------- .../settings-ui/src/configs/WidgetConfigs.tsx | 7 +-- packages/settings-ui/src/index.tsx | 1 + 5 files changed, 29 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3ac7d230..0f35b253 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6811,6 +6811,7 @@ version = "0.0.0" dependencies = [ "anyhow", "async-trait", + "base64 0.22.1", "clap", "cocoa 0.25.0", "crossbeam", diff --git a/packages/desktop/Cargo.toml b/packages/desktop/Cargo.toml index 01e0197d..9a07c69a 100644 --- a/packages/desktop/Cargo.toml +++ b/packages/desktop/Cargo.toml @@ -15,6 +15,7 @@ tauri-build = { version = "2.0", features = [] } [dependencies] anyhow = "1" async-trait = "0.1" +base64 = "0.22" clap = { version = "4", features = ["derive"] } crossbeam = "0.8" reqwest = { version = "0.11", features = ["json"] } diff --git a/packages/desktop/src/sys_tray.rs b/packages/desktop/src/sys_tray.rs index 42679536..6595584f 100644 --- a/packages/desktop/src/sys_tray.rs +++ b/packages/desktop/src/sys_tray.rs @@ -1,6 +1,7 @@ use std::{collections::HashMap, path::PathBuf, str::FromStr, sync::Arc}; use anyhow::{bail, Context}; +use base64::prelude::*; use tauri::{ image::Image, menu::{CheckMenuItem, Menu, MenuBuilder, Submenu, SubmenuBuilder}, @@ -8,7 +9,7 @@ use tauri::{ MouseButton, MouseButtonState, TrayIcon, TrayIconBuilder, TrayIconEvent, }, - AppHandle, Manager, Url, WebviewUrl, WebviewWindowBuilder, Wry, + AppHandle, Manager, WebviewUrl, WebviewWindowBuilder, Wry, }; use tokio::task; use tracing::{error, info}; @@ -332,39 +333,38 @@ impl SysTray { // Get existing settings window if it's already open. let settings_window = app_handle.get_webview_window("settings"); - // let route = match config_path { - // None => WebviewUrl::default(), - // Some(path) => WebviewUrl::App(PathBuf::from(format!( - // "index.html#/widget/{}", - // path.to_unicode_string() - // ))), - // }; - - // let route = format!("index.html#/widget/{}", - // path.to_unicode_string()); - let route = match config_path { - None => "index.html".to_string(), + None => "/index.html".to_string(), Some(path) => { - format!("index.html#/widget/{}", path.to_unicode_string()) + format!( + "/index.html#/widget/{}", + BASE64_STANDARD.encode(path.to_unicode_string()) + ) } }; match &settings_window { None => { - WebviewWindowBuilder::new(app_handle, "settings", route) - .title("Settings - Zebar") - .focused(true) - .visible(true) - .inner_size(900., 600.) - .build() - .context("Failed to build the settings window.")?; + WebviewWindowBuilder::new( + app_handle, + "settings", + WebviewUrl::App(route.into()), + ) + .title("Settings - Zebar") + .focused(true) + .visible(true) + .inner_size(900., 600.) + .build() + .context("Failed to build the settings window.")?; Ok(()) } Some(window) => { window - .navigate(Url::parse(&route)?) + .eval(&format!( + "location.replace('{}'); location.reload();", + route + )) .context("Failed to navigate to widget edit page.")?; window diff --git a/packages/settings-ui/src/configs/WidgetConfigs.tsx b/packages/settings-ui/src/configs/WidgetConfigs.tsx index c8b617cf..e858ad87 100644 --- a/packages/settings-ui/src/configs/WidgetConfigs.tsx +++ b/packages/settings-ui/src/configs/WidgetConfigs.tsx @@ -6,6 +6,7 @@ import { DropdownMenuItem, DropdownMenuTrigger, } from '@glzr/components'; +import { useParams } from '@solidjs/router'; import { IconChevronDown } from '@tabler/icons-solidjs'; import { invoke } from '@tauri-apps/api/core'; import { listen, type Event } from '@tauri-apps/api/event'; @@ -24,6 +25,8 @@ import { WidgetConfigSidebar } from './WidgetConfigSidebar'; import { WidgetConfigForm } from './WidgetConfigForm'; export function WidgetConfigs() { + const params = useParams(); + const [configs, { mutate: mutateWidgetConfigs }] = createResource( async () => invoke>('widget_configs'), { initialValue: {} }, @@ -34,11 +37,9 @@ export function WidgetConfigs() { { initialValue: {} }, ); - createEffect(() => console.log('widgetStates', widgetStates())); - const [selectedConfigPath, setSelectedConfigPath] = createSignal< string | null - >(null); + >(params.path ? atob(params.path) : null); const [selectedPreset, setSelectedPreset] = createSignal( null, diff --git a/packages/settings-ui/src/index.tsx b/packages/settings-ui/src/index.tsx index 36fec187..f8bac784 100644 --- a/packages/settings-ui/src/index.tsx +++ b/packages/settings-ui/src/index.tsx @@ -9,6 +9,7 @@ render( () => ( + ), document.getElementById('root')!,