Skip to content

Commit

Permalink
feat: 将平台判断放在 store 当中,以供组件内同步使用
Browse files Browse the repository at this point in the history
  • Loading branch information
ayangweb committed Jul 9, 2024
1 parent 7e4be12 commit a824440
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/hooks/useTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const useTheme = () => {
const toggleTheme = async (theme?: Theme) => {
const nextTheme = theme ?? (isDark ? "light" : "dark");

if (await isWin()) {
if (isWin()) {
const yes = await ask("切换主题需要重启 app 才能生效!", {
okLabel: "重启",
cancelLabel: "取消",
Expand Down
2 changes: 1 addition & 1 deletion src/layouts/Default/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const DefaultLayout = () => {
const [sidebarClassName, setSidebarClassName] = useState("pt-48");

useMount(async () => {
if (await isWin()) {
if (isWin()) {
setSidebarClassName("pt-32");
}

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

useMount(async () => {
if (await isWin()) {
if (isWin()) {
state.delay = 100;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ const Item: FC<ItemProps> = (props) => {
event: downloadImage,
},
{
label: (await isMac()) ? "在 Finder 中显示" : "在文件资源管理器中显示",
label: isMac() ? "在 Finder 中显示" : "在文件资源管理器中显示",
hide: type !== "files",
event: openFinder,
},
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Clipboard/History/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const ClipboardHistory = () => {
size: { width: screenWidth, height: screenHeight },
} = monitor;

const factor = (await isWin()) ? 1 : scaleFactor;
const factor = isWin() ? 1 : scaleFactor;

x = Math.min(x * factor, screenWidth - width);
y = Math.min(y * factor, screenHeight - height);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const readImage = async (): Promise<ClipboardPayload> => {

let search = await systemOCR(image);

if (await isWin()) {
if (isWin()) {
const { content, qr } = JSON.parse(search) as WinOCR;

if (isEmpty(qr)) {
Expand Down
3 changes: 3 additions & 0 deletions src/stores/global.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { GlobalStore } from "@/types/store";
import { getName, getVersion } from "@tauri-apps/api/app";
import { type } from "@tauri-apps/api/os";
import proxyWithPersist, {
PersistStrategy,
type ProxyPersistStorageEngine,
Expand Down Expand Up @@ -35,4 +36,6 @@ subscribeKey(globalStore._persist, "loaded", async (loaded) => {
name: await getName(),
version: await getVersion(),
};

globalStore.platform = await type();
});
8 changes: 4 additions & 4 deletions src/types/store.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { OsType } from "@tauri-apps/api/os";

export type Theme = "auto" | "light" | "dark";

export interface GlobalStore {
theme: Theme;
autoStart: boolean;
wakeUpKey: string;
appInfo?: {
name: string;
version: string;
};
appInfo?: { name: string; version: string };
platform?: OsType;
}

export interface ClipboardStore {
Expand Down
23 changes: 12 additions & 11 deletions src/utils/is.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { type } from "@tauri-apps/api/os";

/**
* 是否为开发环境
*/
Expand All @@ -8,21 +6,24 @@ export const isDev = () => {
};

/**
* 是否为 windows 系统
* 是否为 macos 系统
*/
export const isWin = async () => {
const osType = await type();

return osType === "Windows_NT";
export const isMac = () => {
return globalStore.platform === "Darwin";
};

/**
* 是否为 mac 系统
* 是否为 windows 系统
*/
export const isMac = async () => {
const osType = await type();
export const isWin = () => {
return globalStore.platform === "Windows_NT";
};

return osType === "Darwin";
/**
* 是否为 linux 系统
*/
export const isLinux = () => {
return globalStore.platform === "Linux";
};

/**
Expand Down

0 comments on commit a824440

Please sign in to comment.