refactor: encapsulates show and hide methods (#308)

* refactor: encapsulates show and hide methods

* style: remove comments
This commit is contained in:
ayangweb
2025-03-24 17:19:19 +08:00
committed by GitHub
parent eadd0988ba
commit c7e547b5fa
3 changed files with 92 additions and 87 deletions

View File

@@ -242,25 +242,28 @@ async fn init_app_search_source<R: Runtime>(app_handle: &AppHandle<R>) -> Result
}
#[tauri::command]
async fn show_coco(app_handle: AppHandle) {
handle_open_coco(&app_handle);
}
#[tauri::command]
async fn hide_coco(app: tauri::AppHandle) {
handle_hide_coco(&app);
}
fn handle_open_coco(app: &AppHandle) {
if let Some(window) = app.get_window(MAIN_WINDOW_LABEL) {
let _ = app.emit("show-coco", ());
async fn show_coco<R: Runtime>(app_handle: AppHandle<R>) {
if let Some(window) = app_handle.get_window(MAIN_WINDOW_LABEL) {
let _ = app_handle.emit("show-coco", ());
move_window_to_active_monitor(&window);
window.show().unwrap();
window.set_visible_on_all_workspaces(true).unwrap();
window.set_always_on_top(true).unwrap();
window.set_focus().unwrap();
let _ = window.show();
let _ = window.unminimize();
let _ = window.set_focus();
}
}
#[tauri::command]
async fn hide_coco<R: Runtime>(app: AppHandle<R>) {
if let Some(window) = app.get_window(MAIN_WINDOW_LABEL) {
if let Err(err) = window.hide() {
eprintln!("Failed to hide the window: {}", err);
} else {
println!("Window successfully hidden.");
}
} else {
eprintln!("Main window not found.");
}
}
@@ -357,26 +360,15 @@ fn move_window_to_active_monitor<R: Runtime>(window: &Window<R>) {
}
}
fn handle_hide_coco(app: &AppHandle) {
if let Some(window) = app.get_window(MAIN_WINDOW_LABEL) {
if let Err(err) = window.hide() {
eprintln!("Failed to hide the window: {}", err);
} else {
println!("Window successfully hidden.");
}
} else {
eprintln!("Main window not found.");
}
}
#[allow(dead_code)]
fn open_settings(app: &tauri::AppHandle) {
use tauri::webview::WebviewBuilder;
println!("settings menu item was clicked");
let window = app.get_webview_window("settings");
if let Some(window) = window {
window.show().unwrap();
window.set_focus().unwrap();
let _ = window.show();
let _ = window.unminimize();
let _ = window.set_focus();
} else {
let window = tauri::window::WindowBuilder::new(app, "settings")
.title("Settings Window")

View File

@@ -1,14 +1,7 @@
use crate::{move_window_to_active_monitor, COCO_TAURI_STORE};
use tauri::App;
use tauri::AppHandle;
use tauri::Emitter;
use tauri::Manager;
use tauri::Runtime;
use tauri_plugin_global_shortcut::GlobalShortcutExt;
use tauri_plugin_global_shortcut::Shortcut;
use tauri_plugin_global_shortcut::ShortcutState;
use tauri_plugin_store::JsonValue;
use tauri_plugin_store::StoreExt;
use crate::{hide_coco, show_coco, COCO_TAURI_STORE};
use tauri::{async_runtime, App, AppHandle, Manager, Runtime};
use tauri_plugin_global_shortcut::{GlobalShortcutExt, Shortcut, ShortcutState};
use tauri_plugin_store::{JsonValue, StoreExt};
/// Tauri's store is a key-value database, we use it to store our registered
/// global shortcut.
@@ -106,18 +99,15 @@ fn _register_shortcut<R: Runtime>(app: &AppHandle<R>, shortcut: Shortcut) {
dbg!("shortcut pressed");
let main_window = app.get_window(MAIN_WINDOW_LABEL).unwrap();
if let ShortcutState::Pressed = event.state() {
let app_handle = app.clone();
if main_window.is_visible().unwrap() {
dbg!("hiding window");
main_window.hide().unwrap();
async_runtime::spawn(async move {
hide_coco(app_handle).await;
});
} else {
let _ = app.emit("show-coco", ());
dbg!("showing window");
move_window_to_active_monitor(&main_window);
main_window.set_visible_on_all_workspaces(true).unwrap();
main_window.set_always_on_top(true).unwrap();
main_window.set_focus().unwrap();
main_window.show().unwrap();
async_runtime::spawn(async move {
show_coco(app_handle).await;
});
}
}
}
@@ -138,17 +128,16 @@ fn _register_shortcut_upon_start(app: &App, shortcut: Shortcut) {
if scut == &shortcut {
let window = app.get_window(MAIN_WINDOW_LABEL).unwrap();
if let ShortcutState::Pressed = event.state() {
if window.is_visible().unwrap() {
window.hide().unwrap();
} else {
let _ = app.emit("show-coco", ());
let app_handle = app.clone();
// dbg!("showing window");
move_window_to_active_monitor(&window);
window.set_visible_on_all_workspaces(true).unwrap();
window.set_always_on_top(true).unwrap();
window.set_focus().unwrap();
window.show().unwrap();
if window.is_visible().unwrap() {
async_runtime::spawn(async move {
hide_coco(app_handle).await;
});
} else {
async_runtime::spawn(async move {
show_coco(app_handle).await;
});
}
}
}

View File

@@ -2,33 +2,33 @@ import { useState } from "react";
import { isTauri } from "@tauri-apps/api/core";
export interface EventPayloads {
'language-changed': {
"language-changed": {
language: string;
};
'theme-changed': string;
'tauri://focus': void;
'endpoint-changed': {
"theme-changed": string;
"tauri://focus": void;
"endpoint-changed": {
endpoint: string;
endpoint_http: string;
endpoint_websocket: string;
};
'auth-changed': {
"auth-changed": {
auth: {
[key: string]: any;
};
};
'userInfo-changed': {
"userInfo-changed": {
userInfo: {
[key: string]: any;
};
};
'open_settings': string | '';
'tab_index': string | '';
'login_or_logout': any;
'change-startup-store': {
open_settings: string | "";
tab_index: string | "";
login_or_logout: any;
"change-startup-store": {
defaultStartupWindow: string;
};
'show-coco': void;
"show-coco": void;
}
// Platform adapter interface
@@ -51,7 +51,9 @@ export interface PlatformAdapter {
getScreenshotableWindows: () => Promise<any[]>;
captureMonitorScreenshot: (id: number) => Promise<string>;
captureWindowScreenshot: (id: number) => Promise<string>;
openFileDialog: (options: { multiple: boolean }) => Promise<string | string[] | null>;
openFileDialog: (options: {
multiple: boolean;
}) => Promise<string | string[] | null>;
getFileMetadata: (path: string) => Promise<any>;
getFileIcon: (path: string, size: number) => Promise<string>;
checkUpdate: () => Promise<any>;
@@ -60,7 +62,9 @@ export interface PlatformAdapter {
getWebviewWindow: () => Promise<any>;
setWindowTheme: (theme: string | null) => Promise<void>;
getWindowTheme: () => Promise<string>;
onThemeChanged: (callback: (payload: { payload: string }) => void) => Promise<void>;
onThemeChanged: (
callback: (payload: { payload: string }) => void
) => Promise<void>;
getWindowByLabel: (label: string) => Promise<{
show: () => Promise<void>;
setFocus: () => Promise<void>;
@@ -82,7 +86,9 @@ export const createTauriAdapter = (): PlatformAdapter => {
async setWindowSize(width: number, height: number): Promise<void> {
if (isTauri()) {
const { getCurrentWebviewWindow } = await import("@tauri-apps/api/webviewWindow");
const { getCurrentWebviewWindow } = await import(
"@tauri-apps/api/webviewWindow"
);
const { LogicalSize } = await import("@tauri-apps/api/dpi");
const window = await getCurrentWebviewWindow();
if (window) {
@@ -100,10 +106,14 @@ export const createTauriAdapter = (): PlatformAdapter => {
async showWindow(): Promise<void> {
if (isTauri()) {
const { getCurrentWebviewWindow } = await import("@tauri-apps/api/webviewWindow");
const { getCurrentWebviewWindow } = await import(
"@tauri-apps/api/webviewWindow"
);
const window = await getCurrentWebviewWindow();
if (window) {
await window.show();
await window.unminimize();
await window.setFocus();
}
}
},
@@ -148,7 +158,9 @@ export const createTauriAdapter = (): PlatformAdapter => {
async checkScreenRecordingPermission() {
if (isTauri()) {
const { checkScreenRecordingPermission } = await import("tauri-plugin-macos-permissions-api");
const { checkScreenRecordingPermission } = await import(
"tauri-plugin-macos-permissions-api"
);
return checkScreenRecordingPermission();
}
return false;
@@ -156,14 +168,18 @@ export const createTauriAdapter = (): PlatformAdapter => {
async requestScreenRecordingPermission() {
if (isTauri()) {
const { requestScreenRecordingPermission } = await import("tauri-plugin-macos-permissions-api");
const { requestScreenRecordingPermission } = await import(
"tauri-plugin-macos-permissions-api"
);
return requestScreenRecordingPermission();
}
},
async getScreenshotableMonitors() {
if (isTauri()) {
const { getScreenshotableMonitors } = await import("tauri-plugin-screenshots-api");
const { getScreenshotableMonitors } = await import(
"tauri-plugin-screenshots-api"
);
return getScreenshotableMonitors();
}
return [];
@@ -171,7 +187,9 @@ export const createTauriAdapter = (): PlatformAdapter => {
async getScreenshotableWindows() {
if (isTauri()) {
const { getScreenshotableWindows } = await import("tauri-plugin-screenshots-api");
const { getScreenshotableWindows } = await import(
"tauri-plugin-screenshots-api"
);
return getScreenshotableWindows();
}
return [];
@@ -179,7 +197,9 @@ export const createTauriAdapter = (): PlatformAdapter => {
async captureMonitorScreenshot(id: number) {
if (isTauri()) {
const { getMonitorScreenshot } = await import("tauri-plugin-screenshots-api");
const { getMonitorScreenshot } = await import(
"tauri-plugin-screenshots-api"
);
return getMonitorScreenshot(id);
}
return "";
@@ -187,7 +207,9 @@ export const createTauriAdapter = (): PlatformAdapter => {
async captureWindowScreenshot(id: number) {
if (isTauri()) {
const { getWindowScreenshot } = await import("tauri-plugin-screenshots-api");
const { getWindowScreenshot } = await import(
"tauri-plugin-screenshots-api"
);
return getWindowScreenshot(id);
}
return "";
@@ -244,7 +266,9 @@ export const createTauriAdapter = (): PlatformAdapter => {
async getWebviewWindow() {
if (isTauri()) {
const { getCurrentWebviewWindow } = await import("@tauri-apps/api/webviewWindow");
const { getCurrentWebviewWindow } = await import(
"@tauri-apps/api/webviewWindow"
);
return getCurrentWebviewWindow();
}
return null;
@@ -262,7 +286,7 @@ export const createTauriAdapter = (): PlatformAdapter => {
if (window) {
return window.theme();
}
return 'light';
return "light";
},
async onThemeChanged(callback) {
@@ -407,7 +431,7 @@ export const createWebAdapter = (): PlatformAdapter => {
async getWindowTheme() {
console.log("Web mode simulated get window theme");
return 'light';
return "light";
},
async onThemeChanged(callback) {
@@ -446,6 +470,6 @@ export default platformAdapter;
// Custom hook for using platform adapter
export const usePlatformAdapter = () => {
const [adapter] = useState<PlatformAdapter>(platformAdapter);
return adapter;
};
};