Skip to content

A full-featured download manager.

4.0 KB
Raw Blame
import { RunMode } from '@shared/constants'
import type { MotrixAppSettings } from '@shared/types/settings'
import { z } from 'zod'
import {
  byteUnitSystemSchema,
  DEFAULT_BYTE_UNIT_PREFERENCE,
} from './byte-unit-system'
import { DirectoryPreferencesSchema } from './directory-preferences'
import { languagePreferenceSchema } from './locale'
import { settingsInputObject } from './settings-input'
import { sidebarColorSchema } from './sidebar-color'
import {
  speedLimitUnitsInputSchema,
  speedLimitUnitsSchema,
} from './speed-limit-units'
import { DEFAULT_TRAY_ICON_COLOR, trayIconColorSchema } from './tray-icon-color'
import { windowMinimumDimensionSchema } from './window-bounds'

export const appUpdateChannelSchema = z.enum(['stable', 'beta'])
export const fileDeletionModeSchema = z.enum(['trash', 'permanent'])
export type FileDeletionMode = z.infer<typeof fileDeletionModeSchema>
export const notificationBadgeStyleSchema = z.enum(['count', 'dot', 'hidden'])
export type NotificationBadgeStyle = z.infer<
  typeof notificationBadgeStyleSchema
>

export const MAGNET_FILE_SELECTION_TIMEOUT_MIN_SECONDS = 10
export const MAGNET_FILE_SELECTION_TIMEOUT_MAX_SECONDS = 3600
export const magnetFileSelectionTimeoutSecondsSchema = z
  .number()
  .int()
  .min(MAGNET_FILE_SELECTION_TIMEOUT_MIN_SECONDS)
  .max(MAGNET_FILE_SELECTION_TIMEOUT_MAX_SECONDS)

export const appSettingsSchema = z.object({
  launchAtStartup: z.boolean().catch(false),
  showMainWindowAtLogin: z.boolean().catch(false),
  mainWindowMinWidth: windowMinimumDimensionSchema.catch(914),
  mainWindowMinHeight: windowMinimumDimensionSchema.catch(672),
  theme: z.enum(['system', 'light', 'dark']).catch('system'),
  sidebarColor: sidebarColorSchema.catch('cyan'),
  reduceMotion: z.boolean().catch(false),
  hardwareAcceleration: z.boolean().catch(true),
  language: languagePreferenceSchema.catch('system'),
  byteUnitSystem: byteUnitSystemSchema.catch(DEFAULT_BYTE_UNIT_PREFERENCE),
  speedLimitUnits: speedLimitUnitsSchema.catch(speedLimitUnitsSchema.parse({})),
  // Empty string is a sentinel: SettingsManager (main/server) seeds the
  // absolute platform download directory on first load. The renderer never
  // observes '' because settings are loaded before the UI mounts.
  defaultSaveDir: z.string().catch(''),
  fileDeletionMode: fileDeletionModeSchema.catch('trash'),
  directoryPreferences: DirectoryPreferencesSchema.catch({
    favorites: [],
    recent: [],
  }),
  notifyOnComplete: z.boolean().catch(true),
  notifyOnError: z.boolean().catch(true),
  notifyInAppOnComplete: z.boolean().catch(true),
  notifyInAppOnError: z.boolean().catch(true),
  notificationBadgeStyle: notificationBadgeStyleSchema.catch('count'),
  autofillClipboardLinks: z.boolean().catch(true),
  protocols: z
    .object({
      magnet: z.boolean().catch(true),
    })
    .catch({ magnet: true }),
  runMode: z.enum(RunMode).catch(RunMode.Standard),
  lightweightMode: z.boolean().catch(false),
  showDownloadProgress: z.boolean().catch(false),
  keepStatsBarVisible: z.boolean().catch(false),
  traySpeedometer: z.boolean().catch(true),
  trayIconColor: trayIconColorSchema.catch(DEFAULT_TRAY_ICON_COLOR),
  magnetFileSelection: z.boolean().catch(true),
  magnetFileSelectionAutoDownload: z.boolean().catch(false),
  magnetFileSelectionTimeoutSeconds:
    magnetFileSelectionTimeoutSecondsSchema.catch(60),
  browserBridgeEnabled: z.boolean().catch(true),
  liquidGlassEffect: z.boolean().catch(false),
  warnBeforeQuit: z.boolean().catch(true),
  checkForUpdatesOnLaunch: z.boolean().catch(true),
  updateChannel: appUpdateChannelSchema.catch('stable'),
})

export const DEFAULT_APP_SETTINGS: MotrixAppSettings = appSettingsSchema.parse(
  {}
)

export const appSettingsInputSchema = settingsInputObject(
  appSettingsSchema
).extend({
  speedLimitUnits: speedLimitUnitsInputSchema,
  defaultSaveDir: z
    .string()
    .refine((value) => value.trim().length > 0 && !value.includes('\0'), {
      params: { settingIssue: 'directory' },
    }),
  protocols: settingsInputObject(
    appSettingsSchema.shape.protocols.removeCatch()
  ),
})