Skip to content

Commit

Permalink
perf: 👷 Optimize the edge hiding function
Browse files Browse the repository at this point in the history
  • Loading branch information
viarotel committed Dec 27, 2024
1 parent 7c76e67 commit d5caaa9
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions electron/helpers/edger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export class Edger {
this.lastMousePosition = null
this.mouseMovementBuffer = []
this.lastAnimationTime = 0
this.isMaximized = window.isMaximized()
this.lastMaximizedState = this.isMaximized

// Animation configs
this.animationDuration = 300
Expand Down Expand Up @@ -176,6 +178,25 @@ export class Edger {
this.window.on('closed', () => {
this.destroy()
})

// Listening to maximize event
this.window.on('maximize', () => {
this.isMaximized = true
this.lastMaximizedState = true
// 如果之前已贴边,则取消贴边状态
if (this.dockEdge) {
this.undock()
}
})

// Listening to restore event
this.window.on('unmaximize', () => {
this.isMaximized = false
// Add a short delay to avoid triggering welt checks
setTimeout(() => {
this.lastMaximizedState = false
}, 100)
})
}

handleWindowBlur() {
Expand Down Expand Up @@ -207,7 +228,7 @@ export class Edger {
}
}

// 改进获取隐藏位置的方法,确保窗口边缘始终可见
// Improved access to hidden location method to ensure that the window edge is always visible
getHiddenBounds() {
const currentBounds = this.window.getBounds()
const display = screen.getDisplayNearestPoint({
Expand All @@ -217,7 +238,7 @@ export class Edger {
const screenBounds = display.workArea

let hiddenBounds = { ...currentBounds }
const minVisiblePixels = 3 // 确保至少有3个像素可见
const minVisiblePixels = 3 // To ensure that at least three pixels is visible

switch (this.dockEdge) {
case 'right':
Expand All @@ -231,7 +252,7 @@ export class Edger {
break
}

// 确保窗口不会完全隐藏
// Make sure the window does not completely hidden
hiddenBounds = {
x: Math.round(hiddenBounds.x),
y: Math.round(hiddenBounds.y),
Expand All @@ -242,7 +263,7 @@ export class Edger {
return hiddenBounds
}

// 添加窗口位置恢复方法
// Add the window position recovery method
restoreWindowPosition() {
if (!this.dockEdge || !this.originalBounds)
return
Expand All @@ -253,7 +274,7 @@ export class Edger {
})
const screenBounds = display.workArea

// 确保窗口在屏幕范围内
// Make sure the window within the scope of the screen
const restoredBounds = { ...this.originalBounds }

switch (this.dockEdge) {
Expand Down Expand Up @@ -557,6 +578,11 @@ export class Edger {
}

checkEdgeSnap() {
// If the window is maximized state or just recover from maximization, do not perform welt
if (this.isMaximized || this.lastMaximizedState) {
return
}

if (this.isDragging && this.dockEdge) {
this.checkUndock()
return
Expand Down Expand Up @@ -588,6 +614,11 @@ export class Edger {
}

dockToEdge(edge, bounds) {
// If the maximum window does not perform welt
if (this.isMaximized) {
return
}

this.dockEdge = edge
this.originalBounds = bounds

Expand Down

0 comments on commit d5caaa9

Please sign in to comment.