diff --git a/electron/main/commons/index.ts b/electron/main/commons/index.ts index 75f208c..c39222e 100644 --- a/electron/main/commons/index.ts +++ b/electron/main/commons/index.ts @@ -6,7 +6,15 @@ import retry from "retry"; import request from "request"; import * as cheerio from "cheerio"; import { isAbsolutePath } from "../../../commons/utils/common"; -import { BrowserWindow, app, dialog, nativeImage, nativeTheme } from "electron"; +import { + BrowserWindow, + Display, + app, + dialog, + nativeImage, + nativeTheme, + screen, +} from "electron"; import { getRandomUserAgent, iconExts } from "../../commons/utils"; import URI from "urijs"; import { hideMainWindow } from "../main"; @@ -463,6 +471,29 @@ function getMainBackgorunColor() { } } +/** + * 获取窗口所在的屏幕 + */ +function getWindowInScreen(window: BrowserWindow) { + let inDisplays: Array = []; + let displays = screen.getAllDisplays(); + let bounds = window.getBounds(); + for (let display of displays) { + let workArea = display.workArea; + if ( + ((workArea.x <= bounds.x && workArea.x + workArea.width >= bounds.x) || + (workArea.x <= bounds.x + bounds.width && + workArea.x + workArea.width >= bounds.x + bounds.width)) && + ((workArea.y <= bounds.y && workArea.y + workArea.height >= bounds.y) || + (workArea.y <= bounds.y + bounds.height && + workArea.y + workArea.height >= bounds.y + bounds.height)) + ) { + inDisplays.push(display); + } + } + return inDisplays; +} + export { downloadImage, getURLInfo, @@ -478,4 +509,5 @@ export { relaunch, getUserDataPath, getMainBackgorunColor, + getWindowInScreen, }; diff --git a/electron/main/main/index.ts b/electron/main/main/index.ts index dd96bf4..d862d9d 100644 --- a/electron/main/main/index.ts +++ b/electron/main/main/index.ts @@ -10,7 +10,11 @@ import { import { createSettingWindow } from "../setting"; import { join } from "node:path"; import cacheData from "../commons/cacheData"; -import { getMainBackgorunColor, sendToWebContent } from "../commons"; +import { + getMainBackgorunColor, + getWindowInScreen, + sendToWebContent, +} from "../commons"; import { release } from "node:os"; // 窗口 @@ -85,7 +89,7 @@ function createMainWindow() { // 永远居中 alwaysCenter(); // 判断窗口位置 - let displays = getWindowInScreen(); + let displays = getWindowInScreen(mainWindow); if (displays.length === 0) { // 代表窗口的位置不再任一屏幕内,将窗口位置移动到主窗口 mainWindow.center(); @@ -304,29 +308,6 @@ function createTray(show: boolean) { } } -/** - * 获取窗口所在的屏幕 - */ -function getWindowInScreen() { - let inDisplays: Array = []; - let displays = screen.getAllDisplays(); - let bounds = global.mainWindow.getBounds(); - for (let display of displays) { - let workArea = display.workArea; - if ( - ((workArea.x <= bounds.x && workArea.x + workArea.width >= bounds.x) || - (workArea.x <= bounds.x + bounds.width && - workArea.x + workArea.width >= bounds.x + bounds.width)) && - ((workArea.y <= bounds.y && workArea.y + workArea.height >= bounds.y) || - (workArea.y <= bounds.y + bounds.height && - workArea.y + workArea.height >= bounds.y + bounds.height)) - ) { - inDisplays.push(display); - } - } - return inDisplays; -} - /** * 边缘吸附 * @param display @@ -344,7 +325,7 @@ function edgeAdsorb(display: Display | null) { // 清空方向 global.mainWindowDirection = null; // 屏幕 - let displays = display ? [display] : getWindowInScreen(); + let displays = display ? [display] : getWindowInScreen(mainWindow); if (displays.length > 1 || displays.length === 0) { return; } @@ -456,7 +437,7 @@ function autoHide(x: number, y: number, size: number, timer: boolean) { } try { // 屏幕 - let displays = getWindowInScreen(); + let displays = getWindowInScreen(mainWindow); if (displays.length > 1 || displays.length === 0) { return; } @@ -593,7 +574,7 @@ function doubleClickTaskbar( return; } // 获取屏幕 - let displays = getWindowInScreen(); + let displays = getWindowInScreen(mainWindow); if ( displays.length > 1 || displays.length === 0 || diff --git a/electron/main/search/index.ts b/electron/main/search/index.ts index 92049dd..408421c 100644 --- a/electron/main/search/index.ts +++ b/electron/main/search/index.ts @@ -1,6 +1,6 @@ -import { BrowserWindow, shell } from "electron"; +import { BrowserWindow, shell, screen } from "electron"; import { join } from "node:path"; -import { sendToWebContent } from "../commons"; +import { getWindowInScreen, sendToWebContent } from "../commons"; import cacheData from "../commons/cacheData"; // 窗口 @@ -113,7 +113,30 @@ function showQuickSearchWindowBefore() { * 显示快速搜索窗口 */ function showQuickSearchWindow() { - quickSearchWindow.show(); + // 获取鼠标所在的屏幕 + let currentDisplay = screen.getDisplayNearestPoint( + screen.getCursorScreenPoint() + ); + // 获取窗口所在的屏幕 + let windowDisplay = getWindowInScreen(quickSearchWindow); + if (windowDisplay.length === 0) { + // 代表窗口的位置不再任一屏幕内,将窗口位置移动到主窗口 + quickSearchWindow.center(); + } else if ( + (windowDisplay.length === 1 && currentDisplay.id !== windowDisplay[0].id) || + windowDisplay.length > 1 + ) { + // 在鼠标所在的屏幕显示 + let workArea = currentDisplay.workArea; + let bounds = quickSearchWindow.getBounds(); + let x = workArea.x + workArea.width / 2 - bounds.width / 2; + let y = workArea.y + workArea.height / 2 - 44 / 2; + quickSearchWindow.setPosition(x, y); + for (let i = 0; i < 10; i++) { + quickSearchWindow.setSize(global.setting.quickSearch.width, 44); + } + } + // 显示 } /**