diff --git a/src/main/presenter/deeplinkPresenter/index.ts b/src/main/presenter/deeplinkPresenter/index.ts index e898f629b..54f426a68 100644 --- a/src/main/presenter/deeplinkPresenter/index.ts +++ b/src/main/presenter/deeplinkPresenter/index.ts @@ -191,6 +191,15 @@ export class DeeplinkPresenter implements IDeeplinkPresenter { console.log('modelId:', modelId) console.log('systemPrompt:', systemPrompt) console.log('autoSend:', autoSend) + + const focusedWindow = presenter.windowPresenter.getFocusedWindow() + if (focusedWindow) { + focusedWindow.show() + focusedWindow.focus() + } else { + presenter.windowPresenter.show() + } + eventBus.sendToRenderer(DEEPLINK_EVENTS.START, SendTarget.DEFAULT_TAB, { msg, modelId, diff --git a/src/main/presenter/tabPresenter.ts b/src/main/presenter/tabPresenter.ts index 158d1c120..3b5d2504a 100644 --- a/src/main/presenter/tabPresenter.ts +++ b/src/main/presenter/tabPresenter.ts @@ -609,6 +609,9 @@ export class TabPresenter implements ITabPresenter { // Re-adding ensures it's on top in most view hierarchies window.contentView.addChildView(view) this.updateViewBounds(window, view) + if (!view.webContents.isDestroyed()) { + view.webContents.focus() + } } /** diff --git a/src/main/presenter/windowPresenter/index.ts b/src/main/presenter/windowPresenter/index.ts index 2d55cc6e6..a041f2f26 100644 --- a/src/main/presenter/windowPresenter/index.ts +++ b/src/main/presenter/windowPresenter/index.ts @@ -310,6 +310,7 @@ export class WindowPresenter implements IWindowPresenter { this.handleWindowRestore(targetWindow.id).catch((error) => { console.error(`Error handling restore logic after showing window ${targetWindow!.id}:`, error) }) + this.focusActiveTab(targetWindow.id) } /** @@ -374,6 +375,26 @@ export class WindowPresenter implements IWindowPresenter { return focusedWindow ? focusedWindow.id === windowId : false } + /** + * 将焦点传递给指定窗口的活动标签页 + * @param windowId 窗口 ID + */ + private focusActiveTab(windowId: number): void { + try { + setTimeout(async () => { + const tabPresenterInstance = presenter.tabPresenter as TabPresenter + const tabsData = await tabPresenterInstance.getWindowTabsData(windowId) + const activeTab = tabsData.find((tab) => tab.isActive) + if (activeTab) { + console.log(`Focusing active tab ${activeTab.id} in window ${windowId}`) + await tabPresenterInstance.switchTab(activeTab.id) + } + }, 50) + } catch (error) { + console.error(`Error focusing active tab in window ${windowId}:`, error) + } + } + /** * 向所有有效窗口的主 WebContents 和所有标签页的 WebContents 发送消息。 * @param channel IPC 通道名。 @@ -537,6 +558,7 @@ export class WindowPresenter implements IWindowPresenter { if (!shellWindow.isDestroyed()) { shellWindow.webContents.send('window-focused', windowId) } + this.focusActiveTab(windowId) }) // 窗口失去焦点 @@ -584,6 +606,7 @@ export class WindowPresenter implements IWindowPresenter { this.handleWindowRestore(windowId).catch((error) => { console.error(`Error handling restore logic for window ${windowId}:`, error) }) + this.focusActiveTab(windowId) eventBus.sendToMain(WINDOW_EVENTS.WINDOW_RESTORED, windowId) } shellWindow.on('restore', handleRestore) @@ -971,6 +994,7 @@ export class WindowPresenter implements IWindowPresenter { console.log(` - Switching to tab ${targetTabData.id}`) await tabPresenterInstance.switchTab(targetTabData.id) } + // switchTab 已经会调用 bringViewToFront 来设置焦点,无需额外调用 } catch (error) { console.error('Error switching to target window/tab:', error) // 继续,因为消息发送成功 diff --git a/src/renderer/src/components/ChatInput.vue b/src/renderer/src/components/ChatInput.vue index 5eb64397e..74b32e855 100644 --- a/src/renderer/src/components/ChatInput.vue +++ b/src/renderer/src/components/ChatInput.vue @@ -1021,7 +1021,16 @@ function onKeydown(e: KeyboardEvent) { defineExpose({ setText: (text: string) => { inputText.value = text - editor.chain().setContent(text).focus('end').run() + nextTick(() => { + editor.chain().clearContent().insertContent(text).run() + nextTick(() => { + editor.view.updateState(editor.state) + setTimeout(() => { + const docSize = editor.state.doc.content.size + editor.chain().focus().setTextSelection(docSize).run() + }, 10) + }) + }) } })