diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index bc04505..d39cbcf 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -4,14 +4,13 @@ mod macos_interop; mod music; mod state; +mod windows; use migration::{Migrator, MigratorTrait}; use rodio::OutputStreamBuilder; use sea_orm::Database; use state::AppDataNew; -use tauri::{Manager, State, Theme}; -use tauri_plugin_decorum::WebviewWindowExt; -use window_vibrancy::{NSVisualEffectMaterial, apply_mica, apply_vibrancy}; +use tauri::{Manager}; #[cfg_attr(mobile, tauri::mobile_entry_point)] pub async fn run() { @@ -58,26 +57,7 @@ pub async fn run() { } let main_window = app.get_webview_window("main").unwrap(); - main_window.create_overlay_titlebar().unwrap(); - if cfg!(target_os = "macos") { - #[cfg(target_os = "macos")] - main_window.make_transparent().unwrap(); - - #[cfg(target_os = "macos")] - apply_vibrancy(&main_window, NSVisualEffectMaterial::HudWindow, None, None) - .expect("Unsupported platform! 'apply_vibrancy' is only supported on macOS"); - } else if cfg!(target_os = "windows") { - #[cfg(target_os = "windows")] - apply_mica( - &main_window, - if main_window.theme().unwrap() == Theme::Dark { - Some(true) - } else { - None - }, - ) - .expect("Unsupported platform! 'apply_mica' is only supported on Windows"); - } + windows::make_window_effects(main_window); Ok(()) }) @@ -92,6 +72,7 @@ pub async fn run() { music::pause, music::resume, music::get_position, + windows::make_window_effect ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/src-tauri/src/windows/README.md b/src-tauri/src/windows/README.md new file mode 100644 index 0000000..5c0c86e --- /dev/null +++ b/src-tauri/src/windows/README.md @@ -0,0 +1 @@ +This is the module for application windows, not Windows (the OS) interop. \ No newline at end of file diff --git a/src-tauri/src/windows/commands.rs b/src-tauri/src/windows/commands.rs new file mode 100644 index 0000000..6a1345e --- /dev/null +++ b/src-tauri/src/windows/commands.rs @@ -0,0 +1,12 @@ +use tauri::{AppHandle, Manager}; +use crate::windows; + +#[tauri::command] +pub fn make_window_effect( + app: AppHandle, + name: &str +) -> Result<(), String> { + let main_window = app.get_webview_window(name).unwrap(); + windows::make_window_effects(main_window); + Ok(()) +} diff --git a/src-tauri/src/windows/core.rs b/src-tauri/src/windows/core.rs new file mode 100644 index 0000000..aa1d3bb --- /dev/null +++ b/src-tauri/src/windows/core.rs @@ -0,0 +1,27 @@ +use tauri::{WebviewWindow}; +use tauri_plugin_decorum::WebviewWindowExt; +use window_vibrancy::{apply_vibrancy, apply_mica, NSVisualEffectMaterial}; + +pub fn make_window_effects(window: WebviewWindow) { + window.create_overlay_titlebar().unwrap(); + if cfg!(target_os = "macos") { + #[cfg(target_os = "macos")] + window.make_transparent().unwrap(); + + #[cfg(target_os = "macos")] + apply_vibrancy(&window, NSVisualEffectMaterial::HudWindow, None, None) + .expect("Unsupported platform! 'apply_vibrancy' is only supported on macOS"); + } else if cfg!(target_os = "windows") { + #[cfg(target_os = "windows")] + apply_mica( + &window, + if window.theme().unwrap() == Theme::Dark { + Some(true) + } else { + None + }, + ) + .expect("Unsupported platform! 'apply_mica' is only supported on Windows"); + } + +} diff --git a/src-tauri/src/windows/mod.rs b/src-tauri/src/windows/mod.rs new file mode 100644 index 0000000..2d7a0ec --- /dev/null +++ b/src-tauri/src/windows/mod.rs @@ -0,0 +1,5 @@ +pub(crate) mod core; +pub(crate) mod commands; + +pub use core::*; +pub use commands::*; diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index b01175d..ce8cb80 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -7,10 +7,12 @@ export const routes: Routes = [ { path: "", component: AppLayout, - children: [{ path: "", component: CurrentlyPlaying }], - }, - { - path: "settings", - component: Settings, + children: [ + { path: "", component: CurrentlyPlaying }, + { + path: "settings", + component: Settings, + }, + ], }, ]; diff --git a/src/app/services/windows.ts b/src/app/services/windows.ts new file mode 100644 index 0000000..7449c07 --- /dev/null +++ b/src/app/services/windows.ts @@ -0,0 +1,46 @@ +// This is the module for application windows, not Windows (the OS) interop. +import { WebviewWindow } from "@tauri-apps/api/webviewWindow"; +import { invoke } from "@tauri-apps/api/core"; + +export async function launchWindow( + label: string, + windowOptions?: ConstructorParameters[1], +) { + const window = new WebviewWindow(label, windowOptions); + + void window + .once("tauri://created", function () { + setTimeout(() => { + invoke("make_window_effect", { + name: label, + }); + }); + }) + .catch((err) => console.error(err)); + + void window + .once("tauri://error", function (e) { + console.error(e); + }) + .catch((err) => console.error(err)); + + return window; +} + +export async function launchSettingsWindow() { + return await launchWindow("settings", { + title: "Settings", + center: true, + fullscreen: false, + // Eventually want to change this to `false` + resizable: true, + alwaysOnTop: true, + focus: true, + focusable: true, + skipTaskbar: true, + visible: true, + hiddenTitle: true, + titleBarStyle: "overlay", + url: "/settings", + }); +} diff --git a/src/app/utils/menu.ts b/src/app/utils/menu.ts index 4d7c2d0..b8ba92b 100644 --- a/src/app/utils/menu.ts +++ b/src/app/utils/menu.ts @@ -1,6 +1,6 @@ import { Menu, MenuItem, Submenu } from "@tauri-apps/api/menu"; import { exit } from "@tauri-apps/plugin-process"; -import { launchSettingsWindow } from "./settings"; +import { launchSettingsWindow } from "../services/windows"; export async function getMenu() { // Will become the application submenu on MacOS diff --git a/src/app/utils/settings.ts b/src/app/utils/settings.ts deleted file mode 100644 index 553017d..0000000 --- a/src/app/utils/settings.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { WebviewWindow } from "@tauri-apps/api/webviewWindow"; -import { LogicalPosition, LogicalSize } from "@tauri-apps/api/dpi"; - -export async function launchSettingsWindow() { - const appWindow = new WebviewWindow("settings", { - title: "Settings", - center: true, - fullscreen: false, - // Eventually want to change this to `false` - resizable: true, - alwaysOnTop: true, - focus: true, - focusable: true, - skipTaskbar: true, - visible: true, - url: "/settings", - }); - - void appWindow - .once("tauri://created", function () {}) - .then(() => { - appWindow.setSize( - new LogicalSize({ - height: 640, - width: 800, - }), - ); - appWindow.setPosition( - new LogicalPosition({ - x: 0, - y: 0, - }), - ); - }) - .catch((err) => console.error(err)); - - void appWindow - .once("tauri://error", function (e) { - console.error(e); - }) - .catch((err) => console.error(err)); -}