Adds custom CSS support to settings dialog

Enables users to inject and persist custom CSS for personalized UI tweaks,
applying changes immediately. Improves appearance customization options
by expanding the color palette and adding a custom color picker.
This commit is contained in:
bincxz committed 2025-12-12 04:02:34 +08:00
1 parent 340a851880
commit 7c07290cfd
4 files changed
+122 -16

No files matched your search

+4
View File
@@ -64,6 +64,8 @@ function App() {
updateKeyBinding,
resetKeyBinding,
resetAllKeyBindings,
customCSS,
setCustomCSS,
} = useSettingsState();
const {
@@ -542,6 +544,8 @@ function App() {
onUpdateKeyBinding={updateKeyBinding}
onResetKeyBinding={resetKeyBinding}
onResetAllKeyBindings={resetAllKeyBindings}
customCSS={customCSS}
onCustomCSSChange={setCustomCSS}
/>
{/* Protocol Select Dialog for QuickSwitcher */}
+20
View File
@@ -10,6 +10,7 @@ STORAGE_KEY_TERM_FONT_SIZE,
STORAGE_KEY_TERM_SETTINGS,
STORAGE_KEY_HOTKEY_SCHEME,
STORAGE_KEY_CUSTOM_KEY_BINDINGS,
STORAGE_KEY_CUSTOM_CSS,
} from '../../infrastructure/config/storageKeys';
import { TERMINAL_THEMES } from '../../infrastructure/config/terminalThemes';
import { TERMINAL_FONTS, DEFAULT_FONT_SIZE } from '../../infrastructure/config/fonts';
@@ -53,6 +54,9 @@ export const useSettingsState = () => {
const [customKeyBindings, setCustomKeyBindings] = useState<CustomKeyBindings>(() =>
localStorageAdapter.read<CustomKeyBindings>(STORAGE_KEY_CUSTOM_KEY_BINDINGS) || {}
);
const [customCSS, setCustomCSS] = useState<string>(() =>
localStorageAdapter.readString(STORAGE_KEY_CUSTOM_CSS) || ''
);
useEffect(() => {
applyThemeTokens(theme, primaryColor);
@@ -84,6 +88,20 @@ export const useSettingsState = () => {
localStorageAdapter.write(STORAGE_KEY_CUSTOM_KEY_BINDINGS, customKeyBindings);
}, [customKeyBindings]);
// Apply and persist custom CSS
useEffect(() => {
localStorageAdapter.writeString(STORAGE_KEY_CUSTOM_CSS, customCSS);
// Apply custom CSS to document
let styleEl = document.getElementById('netcatty-custom-css') as HTMLStyleElement | null;
if (!styleEl) {
styleEl = document.createElement('style');
styleEl.id = 'netcatty-custom-css';
document.head.appendChild(styleEl);
}
styleEl.textContent = customCSS;
}, [customCSS]);
// Get merged key bindings (defaults + custom overrides)
const keyBindings = useMemo((): KeyBinding[] => {
return DEFAULT_KEY_BINDINGS.map(binding => {
@@ -178,5 +196,7 @@ export const useSettingsState = () => {
updateKeyBinding,
resetKeyBinding,
resetAllKeyBindings,
customCSS,
setCustomCSS,
};
};
+97 -16
View File
@@ -62,14 +62,36 @@ interface SettingsDialogProps {
onUpdateKeyBinding?: (bindingId: string, scheme: 'mac' | 'pc', newKey: string) => void;
onResetKeyBinding?: (bindingId: string, scheme?: 'mac' | 'pc') => void;
onResetAllKeyBindings?: () => void;
customCSS?: string;
onCustomCSSChange?: (css: string) => void;
}
// More comprehensive color palette
const COLORS = [
// Blues
{ name: "Sky Blue", value: "199 89% 48%" },
{ name: "Blue", value: "221.2 83.2% 53.3%" },
{ name: "Indigo", value: "234 89% 62%" },
// Purples
{ name: "Violet", value: "262.1 83.3% 57.8%" },
{ name: "Purple", value: "271 81% 56%" },
{ name: "Fuchsia", value: "292 84% 61%" },
// Pinks & Reds
{ name: "Pink", value: "330 81% 60%" },
{ name: "Rose", value: "346.8 77.2% 49.8%" },
{ name: "Red", value: "0 72% 51%" },
// Oranges & Yellows
{ name: "Orange", value: "24.6 95% 53.1%" },
{ name: "Amber", value: "38 92% 50%" },
{ name: "Yellow", value: "48 96% 53%" },
// Greens
{ name: "Lime", value: "84 81% 44%" },
{ name: "Green", value: "142.1 76.2% 36.3%" },
{ name: "Emerald", value: "160 84% 39%" },
{ name: "Teal", value: "173 80% 40%" },
{ name: "Cyan", value: "186 94% 42%" },
// Neutrals
{ name: "Slate", value: "215 20% 55%" },
];
const FONT_WEIGHTS = [
@@ -235,6 +257,8 @@ const SettingsDialog: React.FC<SettingsDialogProps> = ({
onUpdateKeyBinding,
onResetKeyBinding,
onResetAllKeyBindings,
customCSS = "",
onCustomCSSChange,
}) => {
const [importText, setImportText] = useState("");
const [editingBindingId, setEditingBindingId] = useState<string | null>(null);
@@ -477,29 +501,30 @@ const SettingsDialog: React.FC<SettingsDialogProps> = ({
{/* Appearance Tab */}
<SettingsTabContent value="appearance">
<SectionHeader title="UI Theme" />
<div className="grid grid-cols-2 gap-4 max-w-sm">
<ThemeCard
active={theme === "light"}
onClick={() => onThemeChange("light")}
icon={<Sun size={24} className="text-orange-500" />}
label="Light"
/>
<ThemeCard
active={theme === "dark"}
onClick={() => onThemeChange("dark")}
icon={<Moon size={24} className="text-blue-400" />}
label="Dark"
/>
<div className="space-y-0 divide-y divide-border rounded-lg border bg-card px-4">
<SettingRow
label="Dark Mode"
description="Toggle between light and dark theme"
>
<div className="flex items-center gap-2">
<Sun size={14} className="text-muted-foreground" />
<Toggle
checked={theme === "dark"}
onChange={(v) => onThemeChange(v ? "dark" : "light")}
/>
<Moon size={14} className="text-muted-foreground" />
</div>
</SettingRow>
</div>
<SectionHeader title="Accent Color" />
<div className="flex flex-wrap gap-4">
<div className="flex flex-wrap gap-2">
{COLORS.map((c) => (
<button
key={c.name}
onClick={() => onPrimaryColorChange(c.value)}
className={cn(
"w-12 h-12 rounded-full flex items-center justify-center transition-all shadow-sm",
"w-6 h-6 rounded-full flex items-center justify-center transition-all shadow-sm",
primaryColor === c.value
? "ring-2 ring-offset-2 ring-foreground scale-110"
: "hover:scale-105",
@@ -510,11 +535,67 @@ const SettingsDialog: React.FC<SettingsDialogProps> = ({
{primaryColor === c.value && (
<Check
className="text-white drop-shadow-md"
size={18}
size={10}
/>
)}
</button>
))}
{/* Custom color picker */}
<label
className={cn(
"w-6 h-6 rounded-full flex items-center justify-center transition-all shadow-sm cursor-pointer",
"bg-gradient-to-br from-pink-500 via-purple-500 to-blue-500",
!COLORS.some(c => c.value === primaryColor)
? "ring-2 ring-offset-2 ring-foreground scale-110"
: "hover:scale-105",
)}
title="Custom color"
>
<input
type="color"
className="sr-only"
onChange={(e) => {
// Convert hex to HSL
const hex = e.target.value;
const r = parseInt(hex.slice(1, 3), 16) / 255;
const g = parseInt(hex.slice(3, 5), 16) / 255;
const b = parseInt(hex.slice(5, 7), 16) / 255;
const max = Math.max(r, g, b), min = Math.min(r, g, b);
let h = 0, s = 0;
const l = (max + min) / 2;
if (max !== min) {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = ((g - b) / d + (g < b ? 6 : 0)) / 6; break;
case g: h = ((b - r) / d + 2) / 6; break;
case b: h = ((r - g) / d + 4) / 6; break;
}
}
const hsl = `${Math.round(h * 360)} ${Math.round(s * 100)}% ${Math.round(l * 100)}%`;
onPrimaryColorChange(hsl);
}}
/>
{!COLORS.some(c => c.value === primaryColor) ? (
<Check className="text-white drop-shadow-md" size={10} />
) : (
<Palette size={12} className="text-white drop-shadow-md" />
)}
</label>
</div>
<SectionHeader title="Custom CSS" />
<div className="space-y-2">
<p className="text-xs text-muted-foreground">
Add custom CSS to personalize the app appearance. Changes apply immediately.
</p>
<textarea
value={customCSS}
onChange={(e) => onCustomCSSChange(e.target.value)}
placeholder={`/* Example: */\n.terminal { background: #1a1a2e !important; }\n:root { --radius: 0.25rem; }`}
className="w-full h-32 px-3 py-2 text-xs font-mono bg-muted/50 border border-border rounded-lg resize-y focus:outline-none focus:ring-2 focus:ring-primary/50"
spellCheck={false}
/>
</div>
</SettingsTabContent>
+1
View File
@@ -13,6 +13,7 @@ export const STORAGE_KEY_TERM_FONT_SIZE = 'netcatty_term_font_size_v1';
export const STORAGE_KEY_TERM_SETTINGS = 'netcatty_term_settings_v1';
export const STORAGE_KEY_HOTKEY_SCHEME = 'netcatty_hotkey_scheme_v1';
export const STORAGE_KEY_CUSTOM_KEY_BINDINGS = 'netcatty_custom_key_bindings_v1';
export const STORAGE_KEY_CUSTOM_CSS = 'netcatty_custom_css_v1';
export const STORAGE_KEY_PORT_FORWARDING = 'netcatty_port_forwarding_v1';
export const STORAGE_KEY_KNOWN_HOSTS = 'netcatty_known_hosts_v1';
export const STORAGE_KEY_SHELL_HISTORY = 'netcatty_shell_history_v1';