/* * Vencord, a modification for Discord's desktop app * Copyright (c) 2022 Vendicated and contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ import { Settings, useSettings } from "@api/Settings"; import { classNameFactory } from "@api/Styles"; import { Flex } from "@components/Flex"; import { Link } from "@components/Link"; import { Margins } from "@utils/margins"; import { classes, identity } from "@utils/misc"; import { ModalCloseButton, ModalContent, ModalHeader, ModalProps, ModalRoot, openModal } from "@utils/modal"; import { showItemInFolder } from "@utils/native"; import { LazyComponent, useAwaiter } from "@utils/react"; import type { ThemeHeader } from "@utils/themes"; import { getThemeInfo, stripBOM, type UserThemeHeader } from "@utils/themes/bd"; import { usercssParse } from "@utils/themes/usercss"; import { find, findByCodeLazy, findByPropsLazy, findLazy } from "@webpack"; import { Button, Card, ComponentTypes, FluxDispatcher, Forms, Popout, React, Select, showToast, Slider, Switch, TabBar, Text, TextArea, TextInput, useEffect, useRef, useState } from "@webpack/common"; import type { ComponentType, ReactNode, Ref, SyntheticEvent } from "react"; import type { UserstyleHeader } from "usercss-meta"; import { AddonCard } from "./AddonCard"; import { SettingsTab, wrapTab } from "./shared"; type FileInput = ComponentType<{ ref: Ref; onChange: (e: SyntheticEvent) => void; multiple?: boolean; filters?: { name?: string; extensions: string[]; }[]; }>; interface ColorPickerProps { value: number | null; showEyeDropper?: boolean; onChange(value: number | null): void; onClose?(): void; } const ColorPickerModal = LazyComponent(() => find(m => m?.type?.toString?.().includes(".showEyeDropper"))); const InviteActions = findByPropsLazy("resolveInvite"); const TrashIcon = findByCodeLazy("M5 6.99902V18.999C5 20.101 5.897 20.999"); const CogWheel = findByCodeLazy("18.564C15.797 19.099 14.932 19.498 14 19.738V22H10V19.738C9.069"); const FileInput: FileInput = findByCodeLazy("activateUploadDialogue="); // TinyColor is completely unmangled and it's duplicated in two modules! Fun! const TinyColor: tinycolor.Constructor = findByCodeLazy("this._gradientType="); const TextAreaProps = findLazy(m => typeof m.textarea === "string"); const cl = classNameFactory("vc-settings-theme-"); function Validator({ link }: { link: string; }) { const [res, err, pending] = useAwaiter(() => fetch(link).then(res => { if (res.status > 300) throw `${res.status} ${res.statusText}`; const contentType = res.headers.get("Content-Type"); if (!contentType?.startsWith("text/css") && !contentType?.startsWith("text/plain")) throw "Not a CSS file. Remember to use the raw link!"; return "Okay!"; })); const text = pending ? "Checking..." : err ? `Error: ${err instanceof Error ? err.message : String(err)}` : "Valid!"; return {text}; } function Validators({ themeLinks }: { themeLinks: string[]; }) { if (!themeLinks.length) return null; return ( <> Validator This section will tell you whether your themes can successfully be loaded
{themeLinks.map(link => ( {link} ))}
); } function ColorPicker(props: ColorPickerProps) { const [color, setColor] = useState(props.value); return ( ( { setColor(value); props.onChange(value); }} showEyeDropper={props.showEyeDropper} /> )} > {popoutProps => (
)}
); } interface UserCSSSettingsModalProps { modalProps: ModalProps; theme: UserstyleHeader; } function UserCSSSettingsModal({ modalProps, theme }: UserCSSSettingsModalProps) { // @ts-expect-error UseSettings<> can't determine this is a valid key const themeSettings = useSettings(["userCssVars"]).userCssVars[theme.id]; const controls: ReactNode[] = []; function updateSetting(key: string, value: string, setValue: (value: string) => void) { themeSettings[key] = value; setValue(value); } for (const [name, varInfo] of Object.entries(theme.vars)) { switch (varInfo.type) { case "text": { const [value, setValue] = useState(themeSettings[name]); controls.push( {varInfo.label} updateSetting(name, v, setValue)} /> ); break; } case "checkbox": { const [value, setValue] = useState(themeSettings[name]); controls.push( updateSetting(name, value ? "1" : "0", setValue)} hideBorder style={{ marginBottom: "0.5em" }} > {varInfo.label} ); break; } case "color": { const [value, setValue] = useState(themeSettings[name]); const normalizedValue = TinyColor(value).toHex(); controls.push( {varInfo.label} updateSetting(name, "#" + (v?.toString(16).padStart(6, "0") ?? "000000"), setValue)} /> ); break; } case "number": { const [value, setValue] = useState(themeSettings[name]); controls.push( {varInfo.label} updateSetting(name, v, setValue)} /> ); break; } case "select": { const [value, setValue] = useState(themeSettings[name]); const options = varInfo.options.map(option => ({ disabled: false, key: option.name, value: option.value, default: varInfo.default === option.value, label: option.label } as ComponentTypes.SelectOption)); controls.push( {varInfo.label}