diff --git a/src/main/Core/BrowserWindow/BrowserWindowModule.ts b/src/main/Core/BrowserWindow/BrowserWindowModule.ts index b7659a95e..44775243a 100644 --- a/src/main/Core/BrowserWindow/BrowserWindowModule.ts +++ b/src/main/Core/BrowserWindow/BrowserWindowModule.ts @@ -17,11 +17,9 @@ import { MacOsBrowserWindowConstructorOptionsProvider, VibrancyProvider, WindowsBrowserWindowConstructorOptionsProvider, - defaultWindowSize, } from "./BrowserWindowConstructorOptionsProvider"; import { BrowserWindowCreator } from "./BrowserWindowCreator"; import { BrowserWindowToggler } from "./BrowserWindowToggler"; -import { WindowBoundsMemory } from "./WindowBoundsMemory"; import { sendToBrowserWindow } from "./sendToBrowserWindow"; export class BrowserWindowModule { @@ -36,8 +34,6 @@ export class BrowserWindowModule { const assetPathResolver = dependencyRegistry.get("AssetPathResolver"); const ipcMain = dependencyRegistry.get("IpcMain"); - const windowBoundsMemory = new WindowBoundsMemory(dependencyRegistry.get("Screen"), {}); - const appIconFilePathResolver = new AppIconFilePathResolver(nativeTheme, assetPathResolver, operatingSystem); const defaultBrowserWindowOptions = new DefaultBrowserWindowConstructorOptionsProvider( @@ -67,13 +63,7 @@ export class BrowserWindowModule { browserWindow.setVisibleOnAllWorkspaces(settingsManager.getValue("window.visibleOnAllWorkspaces", false)); - const browserWindowToggler = new BrowserWindowToggler( - operatingSystem, - app, - browserWindow, - defaultWindowSize, - settingsManager, - ); + const browserWindowToggler = new BrowserWindowToggler(operatingSystem, app, browserWindow); eventEmitter.emitEvent("browserWindowCreated", { browserWindow }); @@ -83,13 +73,11 @@ export class BrowserWindowModule { browserWindowToggler, browserWindow, dependencyRegistry.get("SettingsManager"), - windowBoundsMemory, ); BrowserWindowModule.registerEvents( browserWindow, dependencyRegistry.get("EventSubscriber"), - windowBoundsMemory, vibrancyProvider, backgroundMaterialProvider, browserWindowToggler, @@ -108,7 +96,6 @@ export class BrowserWindowModule { browserWindowToggler: BrowserWindowToggler, browserWindow: BrowserWindow, settingsManager: SettingsManager, - windowBoundsMemory: WindowBoundsMemory, ) { const shouldHideWindowOnBlur = () => settingsManager @@ -116,14 +103,11 @@ export class BrowserWindowModule { .includes("blur"); browserWindow.on("blur", () => shouldHideWindowOnBlur() && browserWindowToggler.hide()); - browserWindow.on("moved", () => windowBoundsMemory.saveWindowBounds(browserWindow)); - browserWindow.on("resized", () => windowBoundsMemory.saveWindowBounds(browserWindow)); } private static registerEvents( browserWindow: BrowserWindow, eventSubscriber: EventSubscriber, - windowBoundsMemory: WindowBoundsMemory, vibrancyProvider: VibrancyProvider, backgroundMaterialProvider: BackgroundMaterialProvider, browserWindowToggler: BrowserWindowToggler, @@ -147,9 +131,7 @@ export class BrowserWindowModule { } }); - eventSubscriber.subscribe("hotkeyPressed", () => - browserWindowToggler.toggle(windowBoundsMemory.getBoundsNearestToCursor()), - ); + eventSubscriber.subscribe("hotkeyPressed", () => browserWindowToggler.toggle()); eventSubscriber.subscribe("settingUpdated", ({ key, value }: { key: string; value: unknown }) => { sendToBrowserWindow(browserWindow, `settingUpdated[${key}]`, { value }); diff --git a/src/main/Core/BrowserWindow/BrowserWindowToggler.test.ts b/src/main/Core/BrowserWindow/BrowserWindowToggler.test.ts index 25551bab9..175c35dc0 100644 --- a/src/main/Core/BrowserWindow/BrowserWindowToggler.test.ts +++ b/src/main/Core/BrowserWindow/BrowserWindowToggler.test.ts @@ -1,6 +1,5 @@ -import type { SettingsManager } from "@Core/SettingsManager"; import type { OperatingSystem } from "@common/Core"; -import type { App, BrowserWindow, Rectangle, Size } from "electron"; +import type { App, BrowserWindow } from "electron"; import { describe, expect, it, vi } from "vitest"; import { BrowserWindowToggler } from "./BrowserWindowToggler"; @@ -61,20 +60,15 @@ describe(BrowserWindowToggler, () => { const { browserWindow, - centerMock, focusMock, isFocusedMock, isVisibleMock, restoreMock, - setBoundsMock, showMock, webContentsSendMock, } = createBrowserWindow({ isFocused: false, isVisible: true }); - const defaultSize = { width: 100, height: 200 }; - const settingsManager = {}; - - new BrowserWindowToggler(operatingSystem, app, browserWindow, defaultSize, settingsManager).toggle(); + new BrowserWindowToggler(operatingSystem, app, browserWindow).toggle(); expect(isVisibleMock).toHaveBeenCalledOnce(); expect(isFocusedMock).toHaveBeenCalledOnce(); @@ -89,8 +83,6 @@ describe(BrowserWindowToggler, () => { expect(showMock).toHaveBeenCalledOnce(); expect(focusMock).toHaveBeenCalledOnce(); expect(webContentsSendMock).toHaveBeenCalledWith("windowFocused"); - expect(setBoundsMock).toHaveBeenCalledWith(defaultSize); - expect(centerMock).toHaveBeenCalledOnce(); }; testShowAndFocus({ operatingSystem: "Windows" }); @@ -101,21 +93,10 @@ describe(BrowserWindowToggler, () => { it("should show and focus the window if its hidden, re-centering it and resizing it to the default size", () => { const { app, showMock: appShowMock } = createApp(); - const { - browserWindow, - centerMock, - focusMock, - isVisibleMock, - restoreMock, - setBoundsMock, - showMock, - webContentsSendMock, - } = createBrowserWindow({ isFocused: false, isVisible: false }); - - const defaultSize = { width: 100, height: 200 }; - const settingsManager = {}; + const { browserWindow, focusMock, isVisibleMock, restoreMock, showMock, webContentsSendMock } = + createBrowserWindow({ isFocused: false, isVisible: false }); - new BrowserWindowToggler("Windows", app, browserWindow, defaultSize, settingsManager).toggle(); + new BrowserWindowToggler("Windows", app, browserWindow).toggle(); expect(isVisibleMock).toHaveBeenCalledOnce(); expect(appShowMock).toHaveBeenCalledOnce(); @@ -123,31 +104,15 @@ describe(BrowserWindowToggler, () => { expect(showMock).toHaveBeenCalledOnce(); expect(focusMock).toHaveBeenCalledOnce(); expect(webContentsSendMock).toHaveBeenCalledWith("windowFocused"); - expect(setBoundsMock).toHaveBeenCalledWith(defaultSize); - expect(centerMock).toHaveBeenCalledOnce(); }); it("should show and focus the window if its hidden, repositioning it with the given bounds", () => { const { app, showMock: appShowMock } = createApp(); - const { - browserWindow, - focusMock, - isVisibleMock, - restoreMock, - setBoundsMock, - showMock, - webContentsSendMock, - centerMock, - } = createBrowserWindow({ isFocused: false, isVisible: false }); - - const defaultSize = { width: 100, height: 200 }; - const bounds = { x: 10, y: 20, width: 30, height: 40 }; - - const getValueMock = vi.fn().mockReturnValue(false); - const settingsManager = { getValue: (k, d) => getValueMock(k, d) }; + const { browserWindow, focusMock, isVisibleMock, restoreMock, showMock, webContentsSendMock } = + createBrowserWindow({ isFocused: false, isVisible: false }); - new BrowserWindowToggler("Windows", app, browserWindow, defaultSize, settingsManager).toggle(bounds); + new BrowserWindowToggler("Windows", app, browserWindow).toggle(); expect(isVisibleMock).toHaveBeenCalledOnce(); expect(appShowMock).toHaveBeenCalledOnce(); @@ -155,31 +120,15 @@ describe(BrowserWindowToggler, () => { expect(showMock).toHaveBeenCalledOnce(); expect(focusMock).toHaveBeenCalledOnce(); expect(webContentsSendMock).toHaveBeenCalledWith("windowFocused"); - expect(setBoundsMock).toHaveBeenCalledWith(bounds); - expect(centerMock).not.toHaveBeenCalled(); }); - it("should show and focus the window if its hidden, repositioning it with the given bounds and re-centering it if alwaysCenter is set to true", () => { + it("should show and focus the window if its hidden", () => { const { app, showMock: appShowMock } = createApp(); - const { - browserWindow, - centerMock, - focusMock, - isVisibleMock, - restoreMock, - setBoundsMock, - showMock, - webContentsSendMock, - } = createBrowserWindow({ isFocused: false, isVisible: false }); + const { browserWindow, focusMock, isVisibleMock, restoreMock, showMock, webContentsSendMock } = + createBrowserWindow({ isFocused: false, isVisible: false }); - const defaultSize = { width: 100, height: 200 }; - const bounds = { x: 10, y: 20, width: 30, height: 40 }; - - const getValueMock = vi.fn().mockReturnValue(true); - const settingsManager = { getValue: (k, d) => getValueMock(k, d) }; - - new BrowserWindowToggler("Windows", app, browserWindow, defaultSize, settingsManager).toggle(bounds); + new BrowserWindowToggler("Windows", app, browserWindow).toggle(); expect(isVisibleMock).toHaveBeenCalledOnce(); expect(appShowMock).toHaveBeenCalledOnce(); @@ -187,8 +136,6 @@ describe(BrowserWindowToggler, () => { expect(showMock).toHaveBeenCalledOnce(); expect(focusMock).toHaveBeenCalledOnce(); expect(webContentsSendMock).toHaveBeenCalledWith("windowFocused"); - expect(setBoundsMock).toHaveBeenCalledWith(bounds); - expect(centerMock).toHaveBeenCalledOnce(); }); it("should hide the window if it is visible and focussed", () => { @@ -200,10 +147,7 @@ describe(BrowserWindowToggler, () => { isVisible: true, }); - const defaultSize = { width: 100, height: 200 }; - const settingsManager = {}; - - new BrowserWindowToggler(operatingSystem, app, browserWindow, defaultSize, settingsManager).toggle(); + new BrowserWindowToggler(operatingSystem, app, browserWindow).toggle(); expect(isVisibleMock).toHaveBeenCalledOnce(); expect(isFocusedMock).toHaveBeenCalledOnce(); diff --git a/src/main/Core/BrowserWindow/BrowserWindowToggler.ts b/src/main/Core/BrowserWindow/BrowserWindowToggler.ts index 981e8e612..841c4cc63 100644 --- a/src/main/Core/BrowserWindow/BrowserWindowToggler.ts +++ b/src/main/Core/BrowserWindow/BrowserWindowToggler.ts @@ -1,21 +1,18 @@ -import type { SettingsManager } from "@Core/SettingsManager"; import type { OperatingSystem } from "@common/Core"; -import type { App, BrowserWindow, Rectangle, Size } from "electron"; +import type { App, BrowserWindow } from "electron"; export class BrowserWindowToggler { public constructor( private readonly operatingSystem: OperatingSystem, private readonly app: App, private readonly browserWindow: BrowserWindow, - private readonly defaultSize: Size, - private readonly settingsManager: SettingsManager, ) {} - public toggle(bounds?: Rectangle): void { + public toggle(): void { if (this.isVisibleAndFocused()) { this.hide(); } else { - this.showAndFocus(bounds); + this.showAndFocus(); } } @@ -37,7 +34,7 @@ export class BrowserWindowToggler { this.browserWindow.hide(); } - public showAndFocus(bounds?: Rectangle): void { + public showAndFocus(): void { if (typeof this.app.show === "function") { this.app.show(); } @@ -49,21 +46,7 @@ export class BrowserWindowToggler { this.browserWindow.restore(); } - this.repositionWindow(bounds); - this.browserWindow.focus(); this.browserWindow.webContents.send("windowFocused"); } - - private repositionWindow(bounds?: Rectangle): void { - this.browserWindow.setBounds(bounds ?? this.defaultSize); - - if (!bounds || this.alwaysCenter()) { - this.browserWindow.center(); - } - } - - private alwaysCenter(): boolean { - return this.settingsManager.getValue("window.alwaysCenter", false); - } } diff --git a/src/main/Core/BrowserWindow/WindowBoundsMemory.test.ts b/src/main/Core/BrowserWindow/WindowBoundsMemory.test.ts deleted file mode 100644 index 8851c79a9..000000000 --- a/src/main/Core/BrowserWindow/WindowBoundsMemory.test.ts +++ /dev/null @@ -1,66 +0,0 @@ -import type { BrowserWindow, Point, Rectangle, Screen } from "electron"; -import { describe, expect, it, vi } from "vitest"; -import { WindowBoundsMemory } from "./WindowBoundsMemory"; - -describe(WindowBoundsMemory, () => { - it("should return the window bounds of the current screen if available", () => { - const screenPoint = { x: 0, y: 0 }; - - const getDisplayNearestPointMock = vi.fn().mockReturnValue({ id: 1 }); - const getCursorScreenPointMock = vi.fn().mockReturnValue(screenPoint); - - const screen = { - getDisplayNearestPoint: (p) => getDisplayNearestPointMock(p), - getCursorScreenPoint: () => getCursorScreenPointMock(), - }; - - const boundsDisplay0 = { x: 0, y: 0, width: 100, height: 100 }; - const boundsDisplay1 = { x: 100, y: 100, width: 100, height: 100 }; - const boundsDisplay2 = { x: 200, y: 200, width: 100, height: 100 }; - - const windowBoundsMemory = new WindowBoundsMemory(screen, { - "display[0]": boundsDisplay0, - "display[1]": boundsDisplay1, - "display[2]": boundsDisplay2, - }); - - expect(windowBoundsMemory.getBoundsNearestToCursor()).toEqual(boundsDisplay1); - expect(getCursorScreenPointMock).toHaveBeenCalledOnce(); - expect(getDisplayNearestPointMock).toHaveBeenCalledWith(screenPoint); - }); - - it("should return undefined if no bounds are available for the current screen", () => { - const screenPoint = { x: 0, y: 0 }; - - const getDisplayNearestPointMock = vi.fn().mockReturnValue({ id: 1 }); - const getCursorScreenPointMock = vi.fn().mockReturnValue(screenPoint); - - const screen = { - getDisplayNearestPoint: (p) => getDisplayNearestPointMock(p), - getCursorScreenPoint: () => getCursorScreenPointMock(), - }; - - const windowBoundsMemory = new WindowBoundsMemory(screen, { - "display[0]": { x: 0, y: 0, width: 100, height: 100 }, - "display[2]": { x: 200, y: 200, width: 100, height: 100 }, - }); - - expect(windowBoundsMemory.getBoundsNearestToCursor()).toBeUndefined(); - expect(getCursorScreenPointMock).toHaveBeenCalledOnce(); - expect(getDisplayNearestPointMock).toHaveBeenCalledWith(screenPoint); - }); - - it("should save the window bounds for the current screen", () => { - const getDisplayNearestPointMock = vi.fn().mockReturnValue({ id: 1138 }); - - const screen = { getDisplayNearestPoint: (p) => getDisplayNearestPointMock(p) }; - - const windowBounds = { x: 100, y: 100, width: 100, height: 100 }; - - const memory = {}; - - new WindowBoundsMemory(screen, memory).saveWindowBounds({ getBounds: () => windowBounds }); - - expect(memory).toEqual({ "display[1138]": windowBounds }); - }); -}); diff --git a/src/main/Core/BrowserWindow/WindowBoundsMemory.ts b/src/main/Core/BrowserWindow/WindowBoundsMemory.ts deleted file mode 100644 index 0033edbd3..000000000 --- a/src/main/Core/BrowserWindow/WindowBoundsMemory.ts +++ /dev/null @@ -1,30 +0,0 @@ -import type { BrowserWindow, Rectangle, Screen } from "electron"; - -export class WindowBoundsMemory { - public constructor( - private readonly screen: Screen, - private memory: Record, - ) {} - - public getBoundsNearestToCursor(): Rectangle | undefined { - const displayId = this.getDisplayIdNearestToCursor(); - return this.memory[this.getInternalDisplayId(displayId)]; - } - - public saveWindowBounds(browserWindow: BrowserWindow): void { - const displayId = this.getDisplayIdNearestToBrowserWindow(browserWindow); - this.memory[this.getInternalDisplayId(displayId)] = browserWindow.getBounds(); - } - - private getDisplayIdNearestToCursor(): number { - return this.screen.getDisplayNearestPoint(this.screen.getCursorScreenPoint()).id; - } - - private getDisplayIdNearestToBrowserWindow(browserWindow: BrowserWindow): number { - return this.screen.getDisplayNearestPoint(browserWindow.getBounds()).id; - } - - private getInternalDisplayId(displayId: number): string { - return `display[${displayId}]`; - } -} diff --git a/src/renderer/Core/I18n/getCoreResources.ts b/src/renderer/Core/I18n/getCoreResources.ts index 691dcd533..ab26f1281 100644 --- a/src/renderer/Core/I18n/getCoreResources.ts +++ b/src/renderer/Core/I18n/getCoreResources.ts @@ -273,7 +273,6 @@ export const getCoreResources = (): { namespace: string; resources: Resources { - const { t } = useTranslation("settingsWindow"); - - const { value: alwaysCenter, updateValue: setAlwaysCenter } = useSetting({ - key: "window.alwaysCenter", - defaultValue: false, - }); - - return ( - setAlwaysCenter(checked)} />} - /> - ); -}; diff --git a/src/renderer/Core/Settings/Pages/Window/Window.tsx b/src/renderer/Core/Settings/Pages/Window/Window.tsx index c340ece88..f909493ef 100644 --- a/src/renderer/Core/Settings/Pages/Window/Window.tsx +++ b/src/renderer/Core/Settings/Pages/Window/Window.tsx @@ -1,7 +1,6 @@ import { useContextBridge, useSetting } from "@Core/Hooks"; import { SettingGroup } from "@Core/Settings/SettingGroup"; import { SettingGroupList } from "../../SettingGroupList"; -import { AlwaysCenter } from "./AlwaysCenter"; import { AlwaysOnTop } from "./AlwaysOnTop"; import { BackgroundMaterial } from "./BackgroundMaterial"; import { HideWindowOn } from "./HideWindowOn"; @@ -26,7 +25,6 @@ export const Window = () => { - {["macOS", "Linux"].includes(operatingSystem) && }