nex_squid/src-tauri/src/widget.rs

480 lines
16 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

//! Desktop widget (桌面小工具): a borderless desktop-layer window showing
//! live system stats, styled after the fnOS desktop monitor. It sits above
//! the wallpaper and icons but below ordinary app windows, and lives next to
//! the tray, so it keeps running while the main window is hidden.
//!
//! Settings persist in `widget.json` beside `manage.json`; the widget window
//! and the main window each load this module's commands, and every change is
//! broadcast on `widget://settings` so both views stay in sync.
use serde::{Deserialize, Serialize};
use squid_core::{Locale, Settings};
use std::fs;
use std::path::PathBuf;
use std::sync::{Mutex, MutexGuard};
use tauri::{AppHandle, Emitter, Manager, WebviewUrl, WebviewWindowBuilder};
pub const WIDGET_LABEL: &str = "widget";
/// `HWND_BOTTOM` + no-activate flags: the widget lives on the desktop layer —
/// above wallpaper and icons, below every ordinary app window.
#[cfg(windows)]
mod desktop_layer {
#[link(name = "user32")]
extern "system" {
fn SetWindowPos(
hwnd: *mut core::ffi::c_void,
insert_after: *mut core::ffi::c_void,
x: i32,
y: i32,
cx: i32,
cy: i32,
flags: u32,
) -> i32;
}
const HWND_BOTTOM: *mut core::ffi::c_void = 1usize as *mut core::ffi::c_void;
const SWP_NOMOVE: u32 = 0x0002;
const SWP_NOSIZE: u32 = 0x0001;
const SWP_NOACTIVATE: u32 = 0x0010;
pub fn lower(hwnd: *mut core::ffi::c_void) {
// SAFETY: hwnd comes from Tauri's own window handle; SetWindowPos with
// NOMOVE|NOSIZE|NOACTIVATE only edits the z-order.
unsafe {
SetWindowPos(
hwnd,
HWND_BOTTOM,
0,
0,
0,
0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE,
);
}
}
}
/// Park the widget at the bottom of the window stack (desktop layer).
/// macOS/Linux keep plain floating behaviour for now (no equivalent need:
/// the widget story is Windows-first).
pub fn lower_window(app: &AppHandle) {
#[cfg(windows)]
{
let Some(window) = app.get_webview_window(WIDGET_LABEL) else {
return;
};
if let Ok(hwnd) = window.hwnd() {
desktop_layer::lower(hwnd.0.cast());
}
}
#[cfg(not(windows))]
{
let _ = app;
}
}
fn default_interval_ms() -> u64 {
2_000
}
fn default_corner() -> String {
"top-right".to_string()
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct WidgetPrefs {
/// Master switch: hides the window entirely when off.
pub enabled: bool,
pub panel_run: bool,
pub panel_cpu: bool,
pub panel_network: bool,
pub panel_disk: bool,
pub panel_storage: bool,
/// 90–100 (%): window opacity; at most 10% of the desktop may bleed
/// through so text stays readable over busy wallpapers.
pub opacity: u8,
/// Sampling cadence for the widget window.
pub interval_ms: u64,
/// `top-right` | `top-left` | `bottom-right` | `bottom-left`.
pub corner: String,
/// Remember the collapsed header-only state across restarts.
pub remember_collapsed: bool,
pub collapsed: bool,
/// Last user-dragged position; None re-docks to `corner`.
pub position: Option<(i32, i32)>,
}
impl Default for WidgetPrefs {
fn default() -> Self {
Self {
enabled: true,
panel_run: true,
panel_cpu: true,
panel_network: true,
panel_disk: true,
panel_storage: true,
opacity: 100,
interval_ms: default_interval_ms(),
corner: default_corner(),
remember_collapsed: true,
collapsed: false,
position: None,
}
}
}
pub struct WidgetState {
prefs: Mutex<WidgetPrefs>,
}
impl Default for WidgetState {
fn default() -> Self {
Self {
prefs: Mutex::new(load_prefs()),
}
}
}
impl WidgetState {
fn lock<T>(value: &Mutex<T>) -> Result<MutexGuard<'_, T>, String> {
value
.lock()
.map_err(|_| "The application state is unavailable; restart Squid.".to_string())
}
pub fn current(&self) -> WidgetPrefs {
self.prefs.lock().map(|p| p.clone()).unwrap_or_default()
}
}
fn prefs_path() -> PathBuf {
Settings::config_dir().join("widget.json")
}
fn load_prefs() -> WidgetPrefs {
let mut prefs = fs::read_to_string(prefs_path())
.ok()
.and_then(|text| serde_json::from_str::<WidgetPrefs>(&text).ok())
.unwrap_or_default();
// Migration: older builds allowed 60–100; the 10%-translucency cap now
// floors everything below 90 (including the old 92 default).
prefs.opacity = prefs.opacity.clamp(90, 100);
// 折叠记忆 off: always boot expanded (save_prefs already stops persisting
// the fold state, this covers configs written while it was on).
if !prefs.remember_collapsed {
prefs.collapsed = false;
}
prefs
}
fn save_prefs(state: &WidgetState) -> Result<(), String> {
let prefs = WidgetState::lock(&state.prefs)?.clone();
let path = prefs_path();
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(|error| error.to_string())?;
}
// 折叠记忆 off: the live fold state still applies this session, but the
// next launch always starts expanded.
let stored = WidgetPrefs {
collapsed: if prefs.remember_collapsed {
prefs.collapsed
} else {
false
},
..prefs
};
let text = serde_json::to_string_pretty(&stored).map_err(|error| error.to_string())?;
fs::write(path, text).map_err(|error| error.to_string())
}
/// Dock to a screen corner (widget width ~350px) or restore the remembered
/// drag position. Called on creation and whenever the corner changes. All
/// math is in physical pixels — the webview reports `outerPosition` there.
fn place_window(app: &AppHandle, prefs: &WidgetPrefs) {
let Some(window) = app.get_webview_window(WIDGET_LABEL) else {
return;
};
let Ok(Some(monitor)) = window
.current_monitor()
.or_else(|_| window.primary_monitor())
else {
return;
};
// Work area excludes the macOS menu bar and Dock, so the widget cannot
// park its header under the menu bar or behind the Dock.
let work = monitor.work_area();
let outer = window
.outer_size()
.map(|s| (s.width as i32, s.height as i32))
.unwrap_or((350, 620));
let margin = (16.0 * monitor.scale_factor()) as i32;
let min_x = work.position.x;
let min_y = work.position.y;
let max_x = (work.position.x + work.size.width as i32 - outer.0).max(min_x);
let max_y = (work.position.y + work.size.height as i32 - outer.1).max(min_y);
let pos = if let Some((x, y)) = prefs.position {
// Clamp into the monitor so an unplugged-secondary position can not
// hide the widget off-screen.
(x.clamp(min_x, max_x), y.clamp(min_y, max_y))
} else {
let x = match prefs.corner.as_str() {
"top-left" | "bottom-left" => min_x + margin,
_ => max_x - margin,
};
let y = match prefs.corner.as_str() {
"bottom-left" | "bottom-right" => max_y - margin,
_ => min_y + margin,
};
(x.clamp(min_x, max_x), y.clamp(min_y, max_y))
};
let _ = window.set_position(tauri::PhysicalPosition::new(pos.0, pos.1));
}
/// Bring the widget window into the state `prefs` describes (create/show/hide).
/// The window is deliberately *not* always-on-top: it is parked at the bottom
/// of the z-order (desktop layer), above wallpaper/icons but under every
/// ordinary app window, so it can never cover the user's work.
fn sync_window(app: &AppHandle, prefs: &WidgetPrefs) {
if !prefs.enabled {
if let Some(window) = app.get_webview_window(WIDGET_LABEL) {
let _ = window.hide();
}
return;
}
if app.get_webview_window(WIDGET_LABEL).is_some() {
if let Some(window) = app.get_webview_window(WIDGET_LABEL) {
let _ = window.show();
}
place_window(app, prefs);
lower_window(app);
return;
}
let builder = WebviewWindowBuilder::new(
app,
WIDGET_LABEL,
WebviewUrl::App("index.html?view=widget".into()),
)
.title("Squid 实时监控")
.inner_size(350.0, 640.0)
.resizable(false)
.decorations(false)
.transparent(true)
.always_on_top(false)
.skip_taskbar(true)
.focused(false)
.visible(false)
.shadow(false);
// macOS: BelowNormal keeps the widget under ordinary app windows, every
// Space keeps it beside the desktop, and a non-key window means clicking
// a card never pulls focus away from the app the user is working in.
// `accept_first_mouse` keeps the first click actionable anyway.
#[cfg(target_os = "macos")]
let builder = builder
.always_on_bottom(true)
.visible_on_all_workspaces(true)
.focusable(false)
.accept_first_mouse(true);
match builder.build() {
Ok(window) => {
let _ = window.set_ignore_cursor_events(false);
place_window(app, prefs);
let _ = window.show();
lower_window(app);
}
Err(error) => {
eprintln!("widget window failed to open: {error}");
}
}
}
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct WidgetView {
prefs: WidgetPrefs,
language: &'static str,
}
#[tauri::command]
pub fn widget_get(app: AppHandle) -> WidgetView {
let (settings, _) = Settings::load();
WidgetView {
prefs: app.state::<WidgetState>().current(),
language: Locale::detect(&settings).code(),
}
}
#[derive(Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WidgetPatch {
pub enabled: Option<bool>,
pub panel_run: Option<bool>,
pub panel_cpu: Option<bool>,
pub panel_network: Option<bool>,
pub panel_disk: Option<bool>,
pub panel_storage: Option<bool>,
pub opacity: Option<u8>,
pub interval_ms: Option<u64>,
pub corner: Option<String>,
pub remember_collapsed: Option<bool>,
pub collapsed: Option<bool>,
pub position: Option<Option<(i32, i32)>>,
/// `position_changed` marks a drag-persist (no re-docking after set).
#[serde(default)]
pub keep_position: Option<bool>,
/// Re-run corner placement (used after the window auto-resizes and the
/// user has not dragged it anywhere).
#[serde(default)]
pub redock: Option<bool>,
}
#[tauri::command]
pub fn widget_set(app: AppHandle, patch: WidgetPatch) -> Result<WidgetPrefs, String> {
let state = app.state::<WidgetState>();
let (prefs, corner_changed) = {
let mut prefs = WidgetState::lock(&state.prefs)?;
if let Some(value) = patch.enabled {
prefs.enabled = value;
}
if let Some(value) = patch.panel_run {
prefs.panel_run = value;
}
if let Some(value) = patch.panel_cpu {
prefs.panel_cpu = value;
}
if let Some(value) = patch.panel_network {
prefs.panel_network = value;
}
if let Some(value) = patch.panel_disk {
prefs.panel_disk = value;
}
if let Some(value) = patch.panel_storage {
prefs.panel_storage = value;
}
if let Some(value) = patch.opacity {
// At most 10% translucent: anything more reads as "see-through"
// over busy wallpapers.
prefs.opacity = value.clamp(90, 100);
}
if let Some(value) = patch.interval_ms {
prefs.interval_ms = value.clamp(1_000, 10_000);
}
let corner_changed = patch.corner.is_some();
if let Some(value) = patch.corner {
if matches!(
value.as_str(),
"top-right" | "top-left" | "bottom-right" | "bottom-left"
) {
prefs.corner = value;
// Choosing a corner overrides any manual drag position.
prefs.position = None;
}
}
if let Some(value) = patch.remember_collapsed {
prefs.remember_collapsed = value;
}
if let Some(value) = patch.collapsed {
prefs.collapsed = value;
}
if let Some(value) = patch.position {
prefs.position = value;
}
(prefs.clone(), corner_changed)
};
save_prefs(&state)?;
let keep = patch.keep_position.unwrap_or(false);
let redock = patch.redock.unwrap_or(false);
if redock || (!keep && (corner_changed || patch.enabled == Some(true))) {
place_window(&app, &prefs);
}
sync_window(&app, &prefs);
if keep {
// The drag click raised the window; settle it back onto the desktop
// layer so it keeps covering nothing but the wallpaper.
lower_window(&app);
}
let _ = app.emit("widget://settings", &prefs);
Ok(prefs)
}
/// Hide the widget window (header button) without flipping the master switch:
/// a transient fold-away. Remembered only when 折叠记忆 is on.
#[tauri::command]
pub fn widget_hide(app: AppHandle) -> Result<(), String> {
if let Some(window) = app.get_webview_window(WIDGET_LABEL) {
window.hide().map_err(|error| error.to_string())
} else {
Ok(())
}
}
/// Create the window at startup when enabled; the main page can also open it
/// later through widget_set.
pub fn spawn_startup(app: &AppHandle) {
let prefs = app.state::<WidgetState>().current();
sync_window(app, &prefs);
}
/// Tray shortcut: flip the master switch and broadcast, same path as the
/// settings page so every view stays in sync.
pub fn toggle_from_tray(app: &AppHandle) -> Result<(), String> {
let state = app.state::<WidgetState>();
let target = !state.current().enabled;
widget_set(
app.clone(),
WidgetPatch {
enabled: Some(target),
..Default::default()
},
)?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::{WidgetPatch, WidgetPrefs};
#[test]
fn prefs_round_trip_uses_camel_case_keys() {
let prefs = WidgetPrefs {
opacity: 88,
corner: "bottom-left".into(),
position: Some((100, 200)),
..WidgetPrefs::default()
};
let text = serde_json::to_string(&prefs).unwrap();
// keys the frontend types.ts promises must appear serialized
for key in [
"\"panelRun\"",
"\"panelCpu\"",
"\"intervalMs\"",
"\"rememberCollapsed\"",
"\"collapsed\"",
] {
assert!(text.contains(key), "missing {key} in {text}");
}
let back: WidgetPrefs = serde_json::from_str(&text).unwrap();
assert_eq!(back.opacity, 88);
assert_eq!(back.corner, "bottom-left");
assert_eq!(back.position, Some((100, 200)));
}
#[test]
fn patch_accepts_sparse_frontend_payloads() {
let patch: WidgetPatch = serde_json::from_str(r#"{"opacity":92,"redock":true}"#).unwrap();
assert_eq!(patch.opacity, Some(92));
assert_eq!(patch.redock, Some(true));
assert!(patch.enabled.is_none());
assert!(patch.position.is_none());
}
#[test]
fn legacy_configs_without_new_fields_still_load() {
let prefs: WidgetPrefs = serde_json::from_str("{}").unwrap();
assert!(prefs.enabled);
assert!(prefs.panel_cpu);
assert_eq!(prefs.interval_ms, 2_000);
assert!(prefs.position.is_none());
}
}