Skip to content

Commit

Permalink
ブラウザ版のmacOS判定がおかしいのを直す (#1504)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba authored Aug 17, 2023
1 parent 47cd67d commit 9bf78c7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
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

0 comments on commit 9bf78c7

Please sign in to comment.