Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ブラウザ版のmacOS判定がおかしいのを直す #1504

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/type/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ declare global {
interface Window {
readonly [SandboxKey]: import("./preload").Sandbox;
}

interface Navigator {
// navigator.userAgentDataを認識してくれないため
userAgentData: {
readonly platform: string;
};
}
}
24 changes: 20 additions & 4 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@ import { Result } from "@/type/result";

export const isElectron = import.meta.env.VITE_TARGET === "electron";
export const isBrowser = import.meta.env.VITE_TARGET === "browser";
export const isMac =
typeof process === "undefined"
? navigator.userAgent.includes("Mac")
: process.platform === "darwin";

// electronのメイン・レンダラープロセス内、ブラウザ内どこでも使用可能なmacOS判定
function checkIsMac(): boolean {
let isMac: boolean | undefined = undefined;
if (process?.platform) {
// electronのメインプロセス用
isMac = process.platform === "darwin";
} else if (navigator?.userAgentData) {
// electronのレンダラープロセス用、Chrome系統が実装する実験的機能
isMac = navigator.userAgentData.platform.toLowerCase().includes("mac");
} else if (navigator?.platform) {
// ブラウザ用、非推奨機能
isMac = navigator.platform.toLowerCase().includes("mac");
} else {
// ブラウザ用、不正確
isMac = navigator.userAgent.toLowerCase().includes("mac");
}
return isMac;
}
export const isMac = checkIsMac();

export const engineIdSchema = z.string().brand<"EngineId">();
export type EngineId = z.infer<typeof engineIdSchema>;
Expand Down