Skip to content

Commit

Permalink
refactor: 让 macos_panel 触发 tauri 自带的事件 (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb authored Nov 7, 2024
1 parent 4555a6c commit 7f03fd9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 32 deletions.
31 changes: 24 additions & 7 deletions src-tauri/src/core/setup/mac.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use tauri::{ActivationPolicy, App, Emitter, Manager, WebviewWindow};
use tauri::{ActivationPolicy, App, Emitter, EventTarget, Manager, WebviewWindow};
use tauri_nspanel::{cocoa::appkit::NSWindowCollectionBehavior, panel_delegate, WebviewWindowExt};
use tauri_plugin_eco_window::MAIN_WINDOW_LABEL;
use tauri_plugin_eco_window_state::save_window_state;

#[allow(non_upper_case_globals)]
const NSWindowStyleMaskNonActivatingPanel: i32 = 1 << 7;
#[allow(non_upper_case_globals)]
const NSResizableWindowMask: i32 = 1 << 3;
const MACOS_PANEL_FOCUS: &str = "macos-panel-focus";
const WINDOW_FOCUS_EVENT: &str = "tauri://focus";
const WINDOW_BLUR_EVENT: &str = "tauri://blur";
const WINDOW_MOVED_EVENT: &str = "tauri://move";
const WINDOW_RESIZED_EVENT: &str = "tauri://resize";

pub fn platform(app: &mut App, main_window: WebviewWindow, _preference_window: WebviewWindow) {
let app_handle = app.app_handle().clone();
Expand Down Expand Up @@ -36,22 +40,35 @@ pub fn platform(app: &mut App, main_window: WebviewWindow, _preference_window: W
// 定义面板的委托 (delegate),用于监听面板窗口的事件
let delegate = panel_delegate!(EcoPanelDelegate {
window_did_become_key,
window_did_resign_key
window_did_resign_key,
window_did_resize,
window_did_move
});

// 为 delegate 设置事件监听器
delegate.set_listener(Box::new(move |delegate_name: String| {
let emit_target = EventTarget::labeled(MAIN_WINDOW_LABEL);

match delegate_name.as_str() {
// 当窗口获得键盘焦点时调用
"window_did_become_key" => {
app_handle.emit(MACOS_PANEL_FOCUS, true).unwrap();
let _ = main_window.emit_to(emit_target, WINDOW_FOCUS_EVENT, true);
}
// 当窗口失去键盘焦点时调用
"window_did_resign_key" => {
app_handle.emit(MACOS_PANEL_FOCUS, false).unwrap();
let _ = main_window.emit_to(emit_target, WINDOW_BLUR_EVENT, true);

save_window_state(app_handle.clone());
}
"window_did_resize" => {
let size = main_window.inner_size().unwrap();

let _ = main_window.emit_to(emit_target, WINDOW_RESIZED_EVENT, size);
}
"window_did_move" => {
let position = main_window.outer_position().unwrap();

let app_handle_clone = app_handle.clone();
save_window_state(app_handle_clone);
let _ = main_window.emit_to(emit_target, WINDOW_MOVED_EVENT, position);
}
_ => (),
}
Expand Down
1 change: 0 additions & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const LISTEN_KEY = {
CLIPBOARD_ITEM_SELECT_NEXT: "clipboard-item-select-next",
CLOSE_DATABASE: "close-database",
TOGGLE_LISTEN_CLIPBOARD: "toggle-listen-clipboard",
MACOS_PANEL_FOCUS: "macos-panel-focus",
CLIPBOARD_ITEM_FAVORITE: "clipboard-item-favorite",
};

Expand Down
9 changes: 4 additions & 5 deletions src/hooks/useFocus.ts → src/hooks/useTauriFocus.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { listen } from "@tauri-apps/api/event";
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";

interface Props {
onFocus?: () => void;
onBlur?: () => void;
}

export const useFocus = (props: Props) => {
export const useTauriFocus = (props: Props) => {
const { onFocus, onBlur } = props;

const { run } = useDebounceFn(
Expand All @@ -17,14 +16,14 @@ export const useFocus = (props: Props) => {
onBlur?.();
}
},
{ wait: isMac() ? 0 : 100 },
{
wait: isMac() ? 0 : 100,
},
);

useMount(async () => {
const appWindow = getCurrentWebviewWindow();

appWindow.onFocusChanged(run);

listen(LISTEN_KEY.MACOS_PANEL_FOCUS, run);
});
};
2 changes: 1 addition & 1 deletion src/pages/Clipboard/Panel/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Search: FC<HTMLAttributes<HTMLDivElement>> = (props) => {
state.search = value || void 0;
}, [value, isComposition]);

useFocus({
useTauriFocus({
onFocus() {
const { window, search } = clipboardStore;

Expand Down
2 changes: 1 addition & 1 deletion src/pages/Clipboard/Panel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ const ClipboardPanel = () => {
});

// 监听窗口焦点
useFocus({
useTauriFocus({
onBlur() {
if (state.pin) return;

Expand Down
5 changes: 3 additions & 2 deletions src/pages/General/components/MacosPermissions/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Icon from "@/components/Icon";
import ProList from "@/components/ProList";
import ProListItem from "@/components/ProListItem";
import { confirm } from "@tauri-apps/plugin-dialog";

const MacosPermissions = () => {
const { t } = useTranslation();
Expand Down Expand Up @@ -39,7 +40,7 @@ const MacosPermissions = () => {

if (opened) return;

const yes = await ask(
const confirmed = await confirm(
t(
"preference.settings.permission_settings.hints.confirm_full_disk_access",
),
Expand All @@ -56,7 +57,7 @@ const MacosPermissions = () => {
},
);

if (!yes) return;
if (!confirmed) return;

requestFullDiskAccessPermissions();
};
Expand Down
15 changes: 0 additions & 15 deletions src/utils/tauri.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
import { ask as tauriAsk } from "@tauri-apps/plugin-dialog";
import { remove } from "@tauri-apps/plugin-fs";
import { isObject } from "lodash-es";

/**
* 询问弹框
* @param message 弹窗消息
* @param options 弹窗选项
*/
export const ask: typeof tauriAsk = (message, options) => {
if (isMac() && isObject(options) && options.kind === "warning") {
options.kind = "error";
}

return tauriAsk(message, options);
};

/**
* 删除文件
Expand Down

0 comments on commit 7f03fd9

Please sign in to comment.