Skip to content

Commit a54c185

Browse files
Copilotalexr00
andcommitted
Simplify focus restoration - use window focus event instead of view state
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent c65844d commit a54c185

File tree

5 files changed

+12
-49
lines changed

5 files changed

+12
-49
lines changed

src/@types/vscode.proposed.chatParticipantAdditions.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ declare module 'vscode' {
105105
isComplete?: boolean;
106106
toolSpecificData?: ChatTerminalToolInvocationData;
107107
fromSubAgent?: boolean;
108+
presentation?: 'hidden' | 'hiddenAfterComplete' | undefined;
108109

109110
constructor(toolName: string, toolCallId: string, isError?: boolean);
110111
}

src/github/pullRequestOverview.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,6 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel<PullRequestMode
186186
if (this._panel.visible && this._item) {
187187
PullRequestOverviewPanel._onVisible.fire(this._item);
188188
}
189-
190-
// If the panel becomes active, send a message to restore focus to the comment textarea if needed
191-
if (this._panel.active) {
192-
this._postMessage({ command: 'pr.restoreFocus' });
193-
}
194189
}
195190

196191
private setVisibilityContext() {

webviews/common/constants.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

webviews/common/context.tsx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import { createContext } from 'react';
77
import { getState, setState, updateState } from './cache';
8-
import { COMMENT_TEXTAREA_FOCUS_KEY } from './constants';
98
import { getMessageHandler, MessageHandler } from './message';
109
import { CloseResult, OpenCommitChangesArgs } from '../../common/views';
1110
import { IComment } from '../../src/common/comment';
@@ -411,17 +410,6 @@ export class PRContext {
411410
pendingReview.focus();
412411
}
413412
return;
414-
case 'pr.restoreFocus':
415-
// Restore focus to the comment textarea if it had focus before the webview lost focus
416-
const commentTextarea = document.getElementById('comment-textarea');
417-
if (commentTextarea instanceof HTMLTextAreaElement) {
418-
// Check if there's a stored focus state
419-
const shouldRestoreFocus = sessionStorage.getItem(COMMENT_TEXTAREA_FOCUS_KEY) === 'true';
420-
if (shouldRestoreFocus) {
421-
commentTextarea.focus();
422-
}
423-
}
424-
return;
425413
case 'pr.submitting-review':
426414
return this.updatePR({ busy: true, lastReviewType: message.lastReviewType });
427415
case 'pr.append-review':

webviews/editorWebview/app.tsx

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import React, { useContext, useEffect, useState } from 'react';
88
import { render } from 'react-dom';
99
import { Overview } from './overview';
1010
import { PullRequest } from '../../src/github/views';
11-
import { COMMENT_TEXTAREA_FOCUS_KEY } from '../common/constants';
1211
import PullRequestContext from '../common/context';
1312

1413
export function main() {
@@ -23,31 +22,21 @@ export function Root({ children }) {
2322
setPR(ctx.pr);
2423
}, []);
2524

26-
// Track focus on the comment textarea to restore it when the webview regains focus
25+
// Restore focus to comment textarea when window regains focus if user was typing
2726
useEffect(() => {
28-
const handleFocusIn = (e: FocusEvent) => {
29-
if (e.target instanceof HTMLTextAreaElement && e.target.id === 'comment-textarea') {
30-
sessionStorage.setItem(COMMENT_TEXTAREA_FOCUS_KEY, 'true');
31-
}
32-
};
33-
34-
const handleFocusOut = (e: FocusEvent) => {
35-
if (e.target instanceof HTMLTextAreaElement && e.target.id === 'comment-textarea') {
36-
// Only clear the flag if we're switching to another element within the webview
37-
// When switching to another editor group, relatedTarget will be null and we want to keep the flag
38-
if (e.relatedTarget instanceof HTMLElement) {
39-
sessionStorage.setItem(COMMENT_TEXTAREA_FOCUS_KEY, 'false');
27+
const handleWindowFocus = () => {
28+
// Give the focus event time to settle
29+
setTimeout(() => {
30+
const commentTextarea = document.getElementById('comment-textarea') as HTMLTextAreaElement;
31+
// Only restore focus if there's content and nothing else has focus
32+
if (commentTextarea && commentTextarea.value && document.activeElement === document.body) {
33+
commentTextarea.focus();
4034
}
41-
}
35+
}, 100);
4236
};
4337

44-
document.addEventListener('focusin', handleFocusIn);
45-
document.addEventListener('focusout', handleFocusOut);
46-
47-
return () => {
48-
document.removeEventListener('focusin', handleFocusIn);
49-
document.removeEventListener('focusout', handleFocusOut);
50-
};
38+
window.addEventListener('focus', handleWindowFocus);
39+
return () => window.removeEventListener('focus', handleWindowFocus);
5140
}, []);
5241

5342
window.onscroll = debounce(() => {

0 commit comments

Comments
 (0)