Skip to content

Commit

Permalink
feat: add shortcut to switch modes
Browse files Browse the repository at this point in the history
  • Loading branch information
aheizi committed Feb 28, 2025
1 parent e1836f8 commit 3d69964
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
7 changes: 7 additions & 0 deletions webview-ui/src/components/chat/ChatTextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ interface ChatTextAreaProps {
onHeightChange?: (height: number) => void
mode: Mode
setMode: (value: Mode) => void
modeShortcutText: string
}

const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
Expand All @@ -48,6 +49,7 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
onHeightChange,
mode,
setMode,
modeShortcutText,
},
ref,
) => {
Expand Down Expand Up @@ -816,6 +818,11 @@ const ChatTextArea = forwardRef<HTMLTextAreaElement, ChatTextAreaProps>(
minWidth: "70px",
flex: "0 0 auto",
}}>
<option
disabled
style={{ ...optionStyle, fontStyle: "italic", opacity: 0.6, padding: "2px 8px" }}>
{modeShortcutText}
</option>
{getAllModes(customModes).map((mode) => (
<option key={mode.slug} value={mode.slug} style={{ ...optionStyle }}>
{mode.name}
Expand Down
38 changes: 38 additions & 0 deletions webview-ui/src/components/chat/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import TaskHeader from "./TaskHeader"
import AutoApproveMenu from "./AutoApproveMenu"
import { AudioType } from "../../../../src/shared/WebviewMessage"
import { validateCommand } from "../../utils/command-validation"
import { modes } from "../../../../src/shared/modes"

interface ChatViewProps {
isHidden: boolean
Expand All @@ -38,6 +39,9 @@ interface ChatViewProps {

export const MAX_IMAGES_PER_MESSAGE = 20 // Anthropic limits to 20 images

const isMac = navigator.platform.toUpperCase().indexOf("MAC") >= 0
const modeShortcutText = `(${isMac ? "⌘" : "Ctrl"}. for next mode)`

const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryView }: ChatViewProps) => {
const {
version,
Expand Down Expand Up @@ -963,6 +967,39 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
isWriteToolAction,
])

// Add this new function to handle mode switching
const switchToNextMode = useCallback(() => {
const currentModeIndex = modes.findIndex((m) => m.slug === mode)
const nextModeIndex = (currentModeIndex + 1) % modes.length
setMode(modes[nextModeIndex].slug)

// Show toast notification when mode changes
vscode.postMessage({
type: "showInformationMessage",
message: `Switched to ${modes[nextModeIndex].name} mode`,
})
}, [mode, setMode])

// Add keyboard event handler
const handleKeyDown = useCallback(
(event: KeyboardEvent) => {
// Check for Command + . (period)
if ((event.metaKey || event.ctrlKey) && event.key === ".") {
event.preventDefault() // Prevent default browser behavior
switchToNextMode()
}
},
[switchToNextMode],
)

// Add event listener
useEffect(() => {
window.addEventListener("keydown", handleKeyDown)
return () => {
window.removeEventListener("keydown", handleKeyDown)
}
}, [handleKeyDown])

return (
<div
style={{
Expand Down Expand Up @@ -1171,6 +1208,7 @@ const ChatView = ({ isHidden, showAnnouncement, hideAnnouncement, showHistoryVie
}}
mode={mode}
setMode={setMode}
modeShortcutText={modeShortcutText}
/>

<div id="chat-view-portal" />
Expand Down

0 comments on commit 3d69964

Please sign in to comment.