| 1 | import { ProfileDescription } from "core/config/ProfileLifecycleManager"; |
| 2 | import { KeyboardEvent as ReactKeyboardEvent } from "react"; |
| 3 | import { getLocalStorage } from "./localStorage"; |
| 4 | |
| 5 | export type Platform = "mac" | "linux" | "windows" | "unknown"; |
| 6 | |
| 7 | export function getPlatform(): Platform { |
| 8 | const platform = window.navigator.platform.toUpperCase(); |
| 9 | if (platform.indexOf("MAC") >= 0) { |
| 10 | return "mac"; |
| 11 | } else if (platform.indexOf("LINUX") >= 0) { |
| 12 | return "linux"; |
| 13 | } else if (platform.indexOf("WIN") >= 0) { |
| 14 | return "windows"; |
| 15 | } else { |
| 16 | return "unknown"; |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | export function isMetaEquivalentKeyPressed({ |
| 21 | metaKey, |
| 22 | ctrlKey, |
| 23 | }: KeyboardEvent | ReactKeyboardEvent): boolean { |
| 24 | const platform = getPlatform(); |
| 25 | switch (platform) { |
| 26 | case "mac": |
| 27 | return metaKey; |
| 28 | case "linux": |
| 29 | case "windows": |
| 30 | return ctrlKey; |
| 31 | default: |
| 32 | return metaKey; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | export function getMetaKeyLabel(): string { |
| 37 | return getPlatform() === "mac" ? "⌘" : "Ctrl"; |
| 38 | } |
| 39 | |
| 40 | export function getAltKeyLabel(): string { |
| 41 | const platform = getPlatform(); |
| 42 | switch (platform) { |
| 43 | case "mac": |
| 44 | return "⌥"; |
| 45 | default: |
| 46 | return "Alt"; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | export function getFontSize(): number { |
| 51 | return getLocalStorage("fontSize") ?? (isJetBrains() ? 15 : 14); |
| 52 | } |
| 53 | |
| 54 | export function fontSize(n: number): string { |
| 55 | return `${getFontSize() + n}px`; |
| 56 | } |
| 57 | |
| 58 | export function isJetBrains() { |
| 59 | return getLocalStorage("ide") === "jetbrains"; |
| 60 | } |
| 61 | |
| 62 | export const isShareSessionSupported = () => !isJetBrains(); |
| 63 | |
| 64 | export function isWebEnvironment(): boolean { |
| 65 | return ( |
| 66 | typeof window !== "undefined" && |
| 67 | window.navigator && |
| 68 | window.navigator.userAgent.indexOf("Electron") === -1 |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | export function isPrerelease() { |
| 73 | const extensionVersion = getLocalStorage("extensionVersion"); |
| 74 | if (!extensionVersion) { |
| 75 | console.warn( |
| 76 | `Could not find extension version in local storage, assuming it's a prerelease`, |
| 77 | ); |
| 78 | return true; |
| 79 | } |
| 80 | const minor = parseInt(extensionVersion.split(".")[1], 10); |
| 81 | if (minor % 2 !== 0) { |
| 82 | return true; |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | export function isLocalProfile(profile: ProfileDescription): boolean { |
| 88 | return profile.profileType === "local"; |
| 89 | } |