Skip to content

Commit

Permalink
fix(widget): when closing widget, remove process if prompt is hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
johnlindquist committed Nov 6, 2024
1 parent a742b71 commit 3208019
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/main/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,9 @@ export const createMessageMap = (processInfo: ProcessAndPrompt) => {
w.destroy();

remove(widgetState.widgets, ({ id }) => id === widgetId);
if (!prompt?.isVisible()) {
processes.removeByPid(w.pid);
}
};

widget?.webContents.on('before-input-event', (event, input) => {
Expand Down Expand Up @@ -620,7 +623,9 @@ export const createMessageMap = (processInfo: ProcessAndPrompt) => {
}

log.info(`${widgetId}: Widget closed`);
prompt?.focusPrompt();
if (prompt?.isVisible()) {
prompt?.focusPrompt();
}

widget.removeAllListeners();
widget.destroy();
Expand All @@ -633,6 +638,10 @@ export const createMessageMap = (processInfo: ProcessAndPrompt) => {
widgetId,
});
}

if (!prompt?.isVisible()) {
processes.removeByPid(widget.pid);
}
}),

WIDGET_CAPTURE_PAGE: onChildChannelOverride(async ({ child }, { channel, value }) => {
Expand Down
6 changes: 4 additions & 2 deletions src/shared/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ const initWidgets = {

export const widgetState: typeof initWidgets = proxy(initWidgets);

export const findWidget = (id: string, reason = '') => {
export const findWidget = (id: string, reason = ''): BrowserWindow & { pid: number } | null => {
const options = widgetState.widgets.find((opts) => opts.id === id);
if (!options) {
log.warn(`${reason}: widget not found: ${id}`);
return null;
}

return BrowserWindow.fromId(options.wid);
const window = BrowserWindow.fromId(options.wid);
(window as any).pid = options.pid; //hack
return window as BrowserWindow & { pid: number };
};

const subWidgets = subscribeKey(widgetState, 'widgets', (widgets) => {
Expand Down

0 comments on commit 3208019

Please sign in to comment.