Skip to content

Commit dd3d0bd

Browse files
Merge pull request #3151 from RedisInsight/feature/RI-5051
#RI-5051 - restore window size and position
2 parents 6274ad6 + 2af6170 commit dd3d0bd

File tree

5 files changed

+60
-6
lines changed

5 files changed

+60
-6
lines changed

redisinsight/desktop/src/lib/window/browserWindow.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import contextMenu from 'electron-context-menu'
2-
import { BrowserWindow } from 'electron'
2+
import { BrowserWindow, Rectangle } from 'electron'
33
import { v4 as uuidv4 } from 'uuid'
44

55
import { IParsedDeepLink } from 'desktopSrc/lib/app/deep-link.handlers'
66
import { configMain as config } from 'desktopSrc/config'
7-
import { updateTray } from 'desktopSrc/lib'
8-
import { resolveHtmlPath } from 'desktopSrc/utils'
7+
import { electronStore, updateTray } from 'desktopSrc/lib'
8+
import { resolveHtmlPath, getFittedBounds } from 'desktopSrc/utils'
9+
import { ElectronStorageItem } from 'uiSrc/electron/constants'
910
import { initWindowHandlers } from './window.handlers'
1011

1112
export const windows = new Map<string, BrowserWindow>()
@@ -14,6 +15,7 @@ export const focusWindow = (win: BrowserWindow) => {
1415
if (win.isMinimized()) win.restore()
1516
win.focus()
1617
}
18+
export const NEW_WINDOW_OFFSET = 24
1719

1820
export enum WindowType {
1921
Splash = 'splash',
@@ -37,17 +39,26 @@ export const createWindow = async ({
3739
}: ICreateWindow) => {
3840
let x
3941
let y
42+
let { width, height } = options
43+
4044
const currentWindow = BrowserWindow.getFocusedWindow()
45+
const isNewMainWindow = currentWindow && currentWindow?.getTitle() !== config.splashWindow.title
4146

42-
if (currentWindow && currentWindow?.getTitle() !== config.splashWindow.title) {
47+
if (isNewMainWindow) {
4348
const [currentWindowX, currentWindowY] = currentWindow.getPosition()
44-
x = currentWindowX + 24
45-
y = currentWindowY + 24
49+
const [currentWindowWidth, currentWindowHeight] = currentWindow?.getSize()
50+
x = currentWindowX + NEW_WINDOW_OFFSET
51+
y = currentWindowY + NEW_WINDOW_OFFSET
52+
width = currentWindowWidth
53+
height = currentWindowHeight
4654
}
55+
4756
const newWindow: BrowserWindow | null = new BrowserWindow({
4857
...options,
4958
x,
5059
y,
60+
width,
61+
height,
5162
webPreferences: {
5263
...options.webPreferences,
5364
preload: options.preloadPath
@@ -59,6 +70,14 @@ export const createWindow = async ({
5970
return newWindow
6071
}
6172

73+
const savedBounds = electronStore?.get(ElectronStorageItem.bounds)
74+
if (!isNewMainWindow && savedBounds) {
75+
const bounds = getFittedBounds(savedBounds as Rectangle)
76+
if (bounds) {
77+
newWindow.setBounds(bounds)
78+
}
79+
}
80+
6281
newWindow.loadURL(resolveHtmlPath(htmlFileName, options?.parsedDeepLink))
6382

6483
initWindowHandlers(newWindow, prevWindow, windows, id)

redisinsight/desktop/src/lib/window/window.handlers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export const initWindowHandlers = (
5555
})
5656

5757
newWindow.on('close', (event) => {
58+
electronStore?.set(ElectronStorageItem.bounds, newWindow.getNormalBounds())
59+
5860
if (!getIsQuiting() && getDisplayAppInTrayValue() && windows.size === 1) {
5961
event.preventDefault()
6062
newWindow?.hide()

redisinsight/desktop/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './resolveHtmlPath'
22
export * from './wrapErrorSensitiveData'
33
export * from './getAssetPath'
44
export * from './showOrCreateWindow'
5+
export * from './window-size'
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Rectangle, screen } from 'electron'
2+
3+
export const getFittedBounds = (bounds: Rectangle): Rectangle | null => {
4+
try {
5+
const options: any = {}
6+
const area = screen.getDisplayMatching(bounds).workArea
7+
8+
if (
9+
bounds.x >= area.x
10+
&& bounds.y >= area.y
11+
&& bounds.x + bounds.width <= area.x + area.width
12+
&& bounds.y + bounds.height <= area.y + area.height
13+
) {
14+
options.x = bounds.x
15+
options.y = bounds.y
16+
} else return null
17+
18+
// If the saved size is still valid, use it.
19+
if (bounds.width <= area.width) {
20+
options.width = bounds.width
21+
} else return null
22+
23+
if (bounds.height <= area.height) {
24+
options.height = bounds.height
25+
} else return null
26+
27+
return options as Rectangle
28+
} catch {
29+
return null
30+
}
31+
}

redisinsight/ui/src/electron/constants/storageElectron.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ enum ElectronStorageItem {
77
updatePreviousVersion = 'updatePreviousVersion',
88
zoomFactor = 'zoomFactor',
99
themeSource = 'themeSource',
10+
bounds = 'bounds'
1011
}
1112

1213
export default ElectronStorageItem

0 commit comments

Comments
 (0)