Desktop: Fix secondary windows not removed from state if closed while focused (#11740)

renovate/branches/renovate/thiserror-1.x-lockfile
Henry Heino 2025-01-30 06:58:12 -08:00 committed by GitHub
parent db81064c98
commit 25aab57af5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 21 deletions

View File

@ -3,6 +3,7 @@ import reducer, { defaultState, defaultWindowId, MAX_HISTORY, State } from './re
import { BaseItemEntity, FolderEntity, NoteEntity, TagEntity } from './services/database/types';
import Note from './models/Note';
import BaseModel from './BaseModel';
import Folder from './models/Folder';
// const { ALL_NOTES_FILTER_ID } = require('./reserved-ids');
function initTestState(folders: FolderEntity[], selectedFolderIndex: number, notes: NoteEntity[], selectedNoteIndexes: number[], tags: TagEntity[] = null, selectedTagIndex: number = null) {
@ -814,4 +815,35 @@ describe('reducer', () => {
checkCurrentState(windowId);
}
});
test('closing the focused window should set the focus to a different window', async () => {
const folder = await Folder.save({ title: 'Test' });
const notes = await createNTestNotes(3, folder);
let state = initTestState([folder], 0, notes, [0]);
const secondaryWindowId = 'window1';
state = createBackgroundWindow(state, secondaryWindowId, notes[2], notes);
state = reducer(state, {
type: 'WINDOW_FOCUS',
windowId: secondaryWindowId,
});
expect(state.windowId).toBe(secondaryWindowId);
expect(state.selectedNoteIds).toEqual([notes[2].id]);
// Closing the focused window should set focus to the other window
state = reducer(state, {
type: 'WINDOW_CLOSE',
windowId: secondaryWindowId,
});
// There should be no background windows
expect(state.backgroundWindows).toEqual({});
// The other window should be focused
expect(state.windowId).toBe(defaultWindowId);
expect(state.selectedNoteIds).toEqual([notes[0].id]);
});
});

View File

@ -902,6 +902,27 @@ type WindowAction = {
};
const handleWindowActions = (draft: Draft<State>, action: WindowAction) => {
const handleFocus = (windowId: string) => {
// Only allow bringing a background window to the foreground
if (draft.windowId !== windowId) {
const previousWindowId = draft.windowId;
const focusingWindowState = draft.backgroundWindows[windowId];
const previousWindowState = { ...defaultWindowState };
for (const key of Object.keys(focusingWindowState)) {
const stateKey = key as keyof WindowState;
type AssignableWindowState = Record<keyof WindowState, unknown>;
(previousWindowState as AssignableWindowState)[stateKey] = draft[stateKey];
(draft as AssignableWindowState)[stateKey] = focusingWindowState[stateKey];
}
delete draft.backgroundWindows[windowId];
draft.backgroundWindows[previousWindowId] = previousWindowState;
}
};
switch (action.type) {
case 'WINDOW_OPEN': {
@ -926,29 +947,15 @@ const handleWindowActions = (draft: Draft<State>, action: WindowAction) => {
};
break;
}
case 'WINDOW_FOCUS': {
// Only allow bringing a background window to the foreground
if (draft.windowId !== action.windowId) {
const windowId = action.windowId;
const previousWindowId = draft.windowId;
const focusingWindowState = draft.backgroundWindows[windowId];
const previousWindowState = { ...defaultWindowState };
for (const key of Object.keys(focusingWindowState)) {
const stateKey = key as keyof WindowState;
type AssignableWindowState = Record<keyof WindowState, unknown>;
(previousWindowState as AssignableWindowState)[stateKey] = draft[stateKey];
(draft as AssignableWindowState)[stateKey] = focusingWindowState[stateKey];
}
delete draft.backgroundWindows[windowId];
draft.backgroundWindows[previousWindowId] = previousWindowState;
}
case 'WINDOW_FOCUS':
handleFocus(action.windowId);
break;
}
case 'WINDOW_CLOSE': {
const isFocusedWindow = draft.windowId === action.windowId;
if (isFocusedWindow) {
const firstBackgroundWindow = Object.keys(draft.backgroundWindows)[0];
handleFocus(firstBackgroundWindow);
}
delete draft.backgroundWindows[action.windowId];
break;
}