| |
| |
| |
| |
| |
|
|
| import type React from 'react'; |
| import { useCallback, useState } from 'react'; |
| import { Box, Text } from 'ink'; |
| import { Colors } from '../colors.js'; |
| import { themeManager, DEFAULT_THEME } from '../themes/theme-manager.js'; |
| import { RadioButtonSelect } from './shared/RadioButtonSelect.js'; |
| import { DiffRenderer } from './messages/DiffRenderer.js'; |
| import { colorizeCode } from '../utils/CodeColorizer.js'; |
| import type { LoadedSettings } from '../../config/settings.js'; |
| import { SettingScope } from '../../config/settings.js'; |
| import { |
| getScopeItems, |
| getScopeMessageForSetting, |
| } from '../../utils/dialogScopeUtils.js'; |
| import { useKeypress } from '../hooks/useKeypress.js'; |
|
|
| interface ThemeDialogProps { |
| |
| onSelect: (themeName: string | undefined, scope: SettingScope) => void; |
|
|
| |
| onHighlight: (themeName: string | undefined) => void; |
| |
| settings: LoadedSettings; |
| availableTerminalHeight?: number; |
| terminalWidth: number; |
| } |
|
|
| export function ThemeDialog({ |
| onSelect, |
| onHighlight, |
| settings, |
| availableTerminalHeight, |
| terminalWidth, |
| }: ThemeDialogProps): React.JSX.Element { |
| const [selectedScope, setSelectedScope] = useState<SettingScope>( |
| SettingScope.User, |
| ); |
|
|
| |
| const [highlightedThemeName, setHighlightedThemeName] = useState< |
| string | undefined |
| >(settings.merged.ui?.theme || DEFAULT_THEME.name); |
|
|
| |
| const customThemes = |
| selectedScope === SettingScope.User |
| ? settings.user.settings.ui?.customThemes || {} |
| : settings.merged.ui?.customThemes || {}; |
| const builtInThemes = themeManager |
| .getAvailableThemes() |
| .filter((theme) => theme.type !== 'custom'); |
| const customThemeNames = Object.keys(customThemes); |
| const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1); |
| |
| const themeItems = [ |
| ...builtInThemes.map((theme) => ({ |
| label: theme.name, |
| value: theme.name, |
| themeNameDisplay: theme.name, |
| themeTypeDisplay: capitalize(theme.type), |
| })), |
| ...customThemeNames.map((name) => ({ |
| label: name, |
| value: name, |
| themeNameDisplay: name, |
| themeTypeDisplay: 'Custom', |
| })), |
| ]; |
| const [selectInputKey, setSelectInputKey] = useState(Date.now()); |
|
|
| |
| const selectedThemeName = settings.merged.ui?.theme || DEFAULT_THEME.name; |
| const initialThemeIndex = themeItems.findIndex( |
| (item) => item.value === selectedThemeName, |
| ); |
| |
| const safeInitialThemeIndex = initialThemeIndex >= 0 ? initialThemeIndex : 0; |
|
|
| const scopeItems = getScopeItems(); |
|
|
| const handleThemeSelect = useCallback( |
| (themeName: string) => { |
| onSelect(themeName, selectedScope); |
| }, |
| [onSelect, selectedScope], |
| ); |
|
|
| const handleThemeHighlight = (themeName: string) => { |
| setHighlightedThemeName(themeName); |
| onHighlight(themeName); |
| }; |
|
|
| const handleScopeHighlight = useCallback((scope: SettingScope) => { |
| setSelectedScope(scope); |
| setSelectInputKey(Date.now()); |
| }, []); |
|
|
| const handleScopeSelect = useCallback( |
| (scope: SettingScope) => { |
| handleScopeHighlight(scope); |
| setFocusedSection('theme'); |
| }, |
| [handleScopeHighlight], |
| ); |
|
|
| const [focusedSection, setFocusedSection] = useState<'theme' | 'scope'>( |
| 'theme', |
| ); |
|
|
| useKeypress( |
| (key) => { |
| if (key.name === 'tab') { |
| setFocusedSection((prev) => (prev === 'theme' ? 'scope' : 'theme')); |
| } |
| if (key.name === 'escape') { |
| onSelect(undefined, selectedScope); |
| } |
| }, |
| { isActive: true }, |
| ); |
|
|
| |
| const otherScopeModifiedMessage = getScopeMessageForSetting( |
| 'ui.theme', |
| selectedScope, |
| settings, |
| ); |
|
|
| |
| |
| const PREVIEW_PANE_WIDTH_PERCENTAGE = 0.55; |
| |
| |
| const PREVIEW_PANE_WIDTH_SAFETY_MARGIN = 0.9; |
| |
| const TOTAL_HORIZONTAL_PADDING = 4; |
| const colorizeCodeWidth = Math.max( |
| Math.floor( |
| (terminalWidth - TOTAL_HORIZONTAL_PADDING) * |
| PREVIEW_PANE_WIDTH_PERCENTAGE * |
| PREVIEW_PANE_WIDTH_SAFETY_MARGIN, |
| ), |
| 1, |
| ); |
|
|
| const DIALOG_PADDING = 2; |
| const selectThemeHeight = themeItems.length + 1; |
| const SCOPE_SELECTION_HEIGHT = 4; |
| const SPACE_BETWEEN_THEME_SELECTION_AND_APPLY_TO = 1; |
| const TAB_TO_SELECT_HEIGHT = 2; |
| availableTerminalHeight = availableTerminalHeight ?? Number.MAX_SAFE_INTEGER; |
| availableTerminalHeight -= 2; |
| availableTerminalHeight -= TAB_TO_SELECT_HEIGHT; |
|
|
| let totalLeftHandSideHeight = |
| DIALOG_PADDING + |
| selectThemeHeight + |
| SCOPE_SELECTION_HEIGHT + |
| SPACE_BETWEEN_THEME_SELECTION_AND_APPLY_TO; |
|
|
| let showScopeSelection = true; |
| let includePadding = true; |
|
|
| |
| if (totalLeftHandSideHeight > availableTerminalHeight) { |
| includePadding = false; |
| totalLeftHandSideHeight -= DIALOG_PADDING; |
| } |
|
|
| if (totalLeftHandSideHeight > availableTerminalHeight) { |
| |
| totalLeftHandSideHeight -= SCOPE_SELECTION_HEIGHT; |
| showScopeSelection = false; |
| } |
|
|
| |
| const currentFocusedSection = !showScopeSelection ? 'theme' : focusedSection; |
|
|
| |
| |
| const PREVIEW_PANE_FIXED_VERTICAL_SPACE = 8; |
|
|
| |
| availableTerminalHeight = Math.max( |
| availableTerminalHeight, |
| totalLeftHandSideHeight, |
| ); |
| const availableTerminalHeightCodeBlock = |
| availableTerminalHeight - |
| PREVIEW_PANE_FIXED_VERTICAL_SPACE - |
| (includePadding ? 2 : 0) * 2; |
|
|
| |
| const availableHeightForPanes = Math.max( |
| 0, |
| availableTerminalHeightCodeBlock - 1, |
| ); |
|
|
| |
| const codeBlockHeight = Math.ceil(availableHeightForPanes * 0.6); |
| const diffHeight = Math.floor(availableHeightForPanes * 0.4); |
| return ( |
| <Box |
| borderStyle="round" |
| borderColor={Colors.Gray} |
| flexDirection="column" |
| paddingTop={includePadding ? 1 : 0} |
| paddingBottom={includePadding ? 1 : 0} |
| paddingLeft={1} |
| paddingRight={1} |
| width="100%" |
| > |
| <Box flexDirection="row"> |
| {/* Left Column: Selection */} |
| <Box flexDirection="column" width="45%" paddingRight={2}> |
| <Text bold={currentFocusedSection === 'theme'} wrap="truncate"> |
| {currentFocusedSection === 'theme' ? '> ' : ' '}Select Theme{' '} |
| <Text color={Colors.Gray}>{otherScopeModifiedMessage}</Text> |
| </Text> |
| <RadioButtonSelect |
| key={selectInputKey} |
| items={themeItems} |
| initialIndex={safeInitialThemeIndex} |
| onSelect={handleThemeSelect} |
| onHighlight={handleThemeHighlight} |
| isFocused={currentFocusedSection === 'theme'} |
| maxItemsToShow={8} |
| showScrollArrows={true} |
| showNumbers={currentFocusedSection === 'theme'} |
| /> |
| |
| {/* Scope Selection */} |
| {showScopeSelection && ( |
| <Box marginTop={1} flexDirection="column"> |
| <Text bold={currentFocusedSection === 'scope'} wrap="truncate"> |
| {currentFocusedSection === 'scope' ? '> ' : ' '}Apply To |
| </Text> |
| <RadioButtonSelect |
| items={scopeItems} |
| initialIndex={0} // Default to User Settings |
| onSelect={handleScopeSelect} |
| onHighlight={handleScopeHighlight} |
| isFocused={currentFocusedSection === 'scope'} |
| showNumbers={currentFocusedSection === 'scope'} |
| /> |
| </Box> |
| )} |
| </Box> |
| |
| {/* Right Column: Preview */} |
| <Box flexDirection="column" width="55%" paddingLeft={2}> |
| <Text bold>Preview</Text> |
| {/* Get the Theme object for the highlighted theme, fall back to default if not found */} |
| {(() => { |
| const previewTheme = |
| themeManager.getTheme( |
| highlightedThemeName || DEFAULT_THEME.name, |
| ) || DEFAULT_THEME; |
| return ( |
| <Box |
| borderStyle="single" |
| borderColor={Colors.Gray} |
| paddingTop={includePadding ? 1 : 0} |
| paddingBottom={includePadding ? 1 : 0} |
| paddingLeft={1} |
| paddingRight={1} |
| flexDirection="column" |
| > |
| {colorizeCode( |
| `# function |
| def fibonacci(n): |
| a, b = 0, 1 |
| for _ in range(n): |
| a, b = b, a + b |
| return a`, |
| 'python', |
| codeBlockHeight, |
| colorizeCodeWidth, |
| )} |
| <Box marginTop={1} /> |
| <DiffRenderer |
| diffContent={`--- a/util.py |
| +++ b/util.py |
| @@ -1,2 +1,2 @@ |
| - print("Hello, " + name) |
| + print(f"Hello, {name}!") |
| `} |
| availableTerminalHeight={diffHeight} |
| terminalWidth={colorizeCodeWidth} |
| theme={previewTheme} |
| /> |
| </Box> |
| ); |
| })()} |
| </Box> |
| </Box> |
| <Box marginTop={1}> |
| <Text color={Colors.Gray} wrap="truncate"> |
| (Use Enter to select |
| {showScopeSelection ? ', Tab to change focus' : ''}) |
| </Text> |
| </Box> |
| </Box> |
| ); |
| } |
|
|