perf: ⬆️ Optimize edge hiding performance

This commit is contained in:
viarotel 2024-11-21 18:30:02 +08:00
parent 14a81de211
commit 74a91a4058

View File

@ -38,6 +38,9 @@ export class Edger {
this.handleWindowBlur = this.handleWindowBlur.bind(this) this.handleWindowBlur = this.handleWindowBlur.bind(this)
this.handleWindowFocus = this.handleWindowFocus.bind(this) this.handleWindowFocus = this.handleWindowFocus.bind(this)
// Add mouse tracking timer reference
this.mouseTrackingTimer = null
this.initialize() this.initialize()
} }
@ -104,8 +107,9 @@ export class Edger {
} }
showWindow() { showWindow() {
if (!this.isHidden || this.isAnimating) if (!this.window || this.window.isDestroyed() || !this.isHidden || this.isAnimating)
return return
clearTimeout(this.hideDebounceTimer) clearTimeout(this.hideDebounceTimer)
if (this.showDebounceTimer) if (this.showDebounceTimer)
@ -119,8 +123,9 @@ export class Edger {
} }
hideWindow() { hideWindow() {
if (this.isHidden || this.isAnimating) if (!this.window || this.window.isDestroyed() || this.isHidden || this.isAnimating)
return return
clearTimeout(this.showDebounceTimer) clearTimeout(this.showDebounceTimer)
if (this.hideDebounceTimer) if (this.hideDebounceTimer)
@ -166,6 +171,11 @@ export class Edger {
if (this.window.isAlwaysOnTop()) { if (this.window.isAlwaysOnTop()) {
this.wasAlwaysOnTop = true this.wasAlwaysOnTop = true
} }
// Add window close event listener
this.window.on('closed', () => {
this.destroy()
})
} }
handleWindowBlur() { handleWindowBlur() {
@ -182,7 +192,9 @@ export class Edger {
setAlwaysOnTop(value) { setAlwaysOnTop(value) {
try { try {
// 某些系统上可能需要特定的参数 if (!this.window || this.window.isDestroyed())
return
if (process.platform === 'darwin') { if (process.platform === 'darwin') {
this.window.setAlwaysOnTop(value, 'floating') this.window.setAlwaysOnTop(value, 'floating')
} }
@ -263,32 +275,45 @@ export class Edger {
} }
destroy() { destroy() {
// Clear all timers
this.cleanupAnimation() this.cleanupAnimation()
if (this.showDebounceTimer) { if (this.showDebounceTimer) {
clearTimeout(this.showDebounceTimer) clearTimeout(this.showDebounceTimer)
this.showDebounceTimer = null
} }
if (this.hideDebounceTimer) { if (this.hideDebounceTimer) {
clearTimeout(this.hideDebounceTimer) clearTimeout(this.hideDebounceTimer)
this.hideDebounceTimer = null
}
if (this.mouseTrackingTimer) {
clearInterval(this.mouseTrackingTimer)
this.mouseTrackingTimer = null
} }
// 清理事件监听 // Clean up event listeners
if (this.window) { if (this.window && !this.window.isDestroyed()) {
this.window.removeListener('blur', this.handleWindowBlur) this.window.removeListener('blur', this.handleWindowBlur)
this.window.removeListener('focus', this.handleWindowFocus) this.window.removeListener('focus', this.handleWindowFocus)
this.window.removeAllListeners() this.window.removeAllListeners()
// 恢复原始置顶状态 // Restore original always on top state
if (this.window.isAlwaysOnTop() !== this.wasAlwaysOnTop) { if (this.window.isAlwaysOnTop() !== this.wasAlwaysOnTop) {
this.setAlwaysOnTop(this.wasAlwaysOnTop) this.setAlwaysOnTop(this.wasAlwaysOnTop)
} }
} }
this.mouseMovementBuffer = [] this.mouseMovementBuffer = []
this.window = null
this.dockEdge = null
this.originalBounds = null
this.isHidden = false
this.isDragging = false
this.isAnimating = false
} }
startMouseTracking() { startMouseTracking() {
const trackMouse = () => { const trackMouse = () => {
if (!this.dockEdge) if (!this.dockEdge || !this.window || this.window.isDestroyed())
return return
const currentTime = Date.now() const currentTime = Date.now()
@ -297,24 +322,30 @@ export class Edger {
// Update mouse movement buffer // Update mouse movement buffer
this.updateMouseBuffer(mousePos, currentTime) this.updateMouseBuffer(mousePos, currentTime)
const windowBounds = this.window.getBounds() try {
const display = screen.getDisplayNearestPoint(mousePos) const windowBounds = this.window.getBounds()
const screenBounds = display.workArea const display = screen.getDisplayNearestPoint(mousePos)
const screenBounds = display.workArea
// Check that the mouse is stable // Check that the mouse is stable
if (this.isMouseStable()) { if (this.isMouseStable()) {
if (this.isMouseNearEdge(mousePos, windowBounds, screenBounds)) { if (this.isMouseNearEdge(mousePos, windowBounds, screenBounds)) {
this.showWindow() this.showWindow()
} }
else if (this.isMouseOutsideWindow(mousePos, windowBounds)) { else if (this.isMouseOutsideWindow(mousePos, windowBounds)) {
this.hideWindow() this.hideWindow()
}
} }
this.lastMousePosition = mousePos
}
catch (err) {
// Window was destroyed, clean up
this.destroy()
} }
this.lastMousePosition = mousePos
} }
setInterval(trackMouse, 16) this.mouseTrackingTimer = setInterval(trackMouse, 16)
} }
updateMouseBuffer(mousePos, currentTime) { updateMouseBuffer(mousePos, currentTime) {