Skip to content

Commit 50ba5e1

Browse files
committed
Refactor Escape key handling to consistently close the main view when the prompt field is not focused, and update the header button tooltip to reflect the Esc shortcut
1 parent 8bd20f3 commit 50ba5e1

3 files changed

Lines changed: 46 additions & 14 deletions

File tree

‎packages/ui/src/components/editor/panel/PromptField/PromptField.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
318318
[styles['container__inner--selecting']]: is_text_selecting
319319
})}
320320
onKeyDown={(e) => {
321+
if (e.key == 'Escape' && !e.ctrlKey && !e.metaKey) {
322+
e.stopPropagation()
323+
return
324+
}
321325
if (e.key == 'f' && (e.ctrlKey || e.metaKey)) {
322326
e.stopPropagation()
323327
props.on_search_click()

‎packages/vscode/src/views/panel/frontend/Main/MainView/MainView.tsx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect } from 'react'
12
import styles from './MainView.module.scss'
23
import { Configurations as UiConfigurations } from '@ui/components/editor/panel/Configurations'
34
import { Presets as UiPresets } from '@ui/components/editor/panel/Presets'
@@ -96,6 +97,25 @@ type Props = {
9697
}
9798

9899
export const MainView: React.FC<Props> = (props) => {
100+
useEffect(() => {
101+
const handle_key_down = (e: KeyboardEvent) => {
102+
if (
103+
e.key == 'Escape' &&
104+
!e.metaKey &&
105+
!e.ctrlKey &&
106+
!e.altKey &&
107+
!e.shiftKey
108+
) {
109+
props.on_show_home()
110+
}
111+
}
112+
113+
window.addEventListener('keydown', handle_key_down)
114+
return () => {
115+
window.removeEventListener('keydown', handle_key_down)
116+
}
117+
}, [])
118+
99119
const is_in_code_completions_prompt_type =
100120
(props.mode == MODE.WEB && props.web_prompt_type == 'code-completions') ||
101121
(props.mode == MODE.API && props.api_prompt_type == 'code-completions')

‎packages/vscode/src/views/panel/frontend/Main/MainView/components/Header/Header.tsx‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const Header: React.FC<Props> = (props) => {
4141
mode: props.mode,
4242
handle_heading_click,
4343
on_web_prompt_type_change: props.on_web_prompt_type_change,
44-
on_api_prompt_type_change: props.on_api_prompt_type_change,
44+
on_api_prompt_type_change: props.on_api_prompt_type_change
4545
})
4646

4747
const calculate_dropdown_max_width = () => {
@@ -77,7 +77,7 @@ export const Header: React.FC<Props> = (props) => {
7777
<UiIconButton
7878
codicon_icon="chevron-left"
7979
on_click={props.on_show_home}
80-
title="Return"
80+
title="Return (Esc)"
8181
/>
8282
<button
8383
className={styles['header__left__toggler']}
@@ -92,35 +92,43 @@ export const Header: React.FC<Props> = (props) => {
9292
<div className={styles.header__right__dropdown}>
9393
{props.mode == MODE.WEB && (
9494
<UiDropdown
95-
options={Object.entries(web_mode_labels).map(([value, label]) => ({
96-
value: value as WebPromptType,
97-
label,
98-
shortcut: `${is_mac ? '⇧⌥' : 'Shift+Alt+'}${label.charAt(0)}`,
99-
}))}
95+
options={Object.entries(web_mode_labels).map(
96+
([value, label]) => ({
97+
value: value as WebPromptType,
98+
label,
99+
shortcut: `${is_mac ? '⇧⌥' : 'Shift+Alt+'}${label.charAt(0)}`
100+
})
101+
)}
100102
selected_value={props.web_prompt_type}
101103
on_change={props.on_web_prompt_type_change}
102104
max_width={dropdown_max_width}
103105
menu_max_width="calc(100vw - 52px)"
104106
info={is_narrow_viewport ? undefined : 'prompt type'}
105107
title={
106-
is_mac ? 'Change prompt type (⇧⌥)' : 'Change prompt type (Shift+Alt)'
108+
is_mac
109+
? 'Change prompt type (⇧⌥)'
110+
: 'Change prompt type (Shift+Alt)'
107111
}
108112
/>
109113
)}
110114
{props.mode == MODE.API && (
111115
<UiDropdown
112-
options={Object.entries(api_mode_labels).map(([value, label]) => ({
113-
value: value as ApiPromptType,
114-
label,
115-
shortcut: `${is_mac ? '⇧⌥' : 'Shift+Alt+'}${label.charAt(0)}`,
116-
}))}
116+
options={Object.entries(api_mode_labels).map(
117+
([value, label]) => ({
118+
value: value as ApiPromptType,
119+
label,
120+
shortcut: `${is_mac ? '⇧⌥' : 'Shift+Alt+'}${label.charAt(0)}`
121+
})
122+
)}
117123
selected_value={props.api_prompt_type}
118124
on_change={props.on_api_prompt_type_change}
119125
max_width={dropdown_max_width}
120126
menu_max_width="calc(100vw - 60px)"
121127
info={is_narrow_viewport ? undefined : 'prompt type'}
122128
title={
123-
is_mac ? 'Change prompt type (⇧⌥)' : 'Change prompt type (Shift+Alt)'
129+
is_mac
130+
? 'Change prompt type (⇧⌥)'
131+
: 'Change prompt type (Shift+Alt)'
124132
}
125133
/>
126134
)}

0 commit comments

Comments
 (0)