From 6f194c7811e77bb6e2e4fa69ea329dc99e2b50ef Mon Sep 17 00:00:00 2001 From: Matthew Dapena-Tretter Date: Thu, 1 Mar 2018 10:49:58 -0800 Subject: [PATCH] Make sure there's a single source of truth for dock hover state Previously, the workspace's idea of the hovered dock and the docks' themselves could be out of sync. --- src/dock.js | 15 ++++++++++++++- src/workspace-element.js | 13 +++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/dock.js b/src/dock.js index 3e0643c9c..a9a97b43d 100644 --- a/src/dock.js +++ b/src/dock.js @@ -153,7 +153,10 @@ module.exports = class Dock { this.state = nextState this.render(this.state) - const {visible} = this.state + const {hovered, visible} = this.state + if (hovered !== prevState.hovered) { + this.emitter.emit('did-change-hovered', hovered) + } if (visible !== prevState.visible) { this.emitter.emit('did-change-visible', visible) } @@ -609,6 +612,16 @@ module.exports = class Dock { return this.paneContainer.onDidDestroyPaneItem(callback) } + // Extended: Invoke the given callback when the hovered state of the dock changes. + // + // * `callback` {Function} to be called when the hovered state changes. + // * `hovered` {Boolean} Is the dock now hovered? + // + // Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. + onDidChangeHovered (callback) { + return this.emitter.on('did-change-hovered', callback) + } + /* Section: Pane Items */ diff --git a/src/workspace-element.js b/src/workspace-element.js index 6d15cc14b..5531aafdf 100644 --- a/src/workspace-element.js +++ b/src/workspace-element.js @@ -92,7 +92,13 @@ class WorkspaceElement extends HTMLElement { window.removeEventListener('dragstart', this.handleDragStart) window.removeEventListener('dragend', this.handleDragEnd, true) window.removeEventListener('drop', this.handleDrop, true) - }) + }), + ...[this.model.getLeftDock(), this.model.getRightDock(), this.model.getBottomDock()] + .map(dock => dock.onDidChangeHovered(hovered => { + if (hovered) this.hoveredDock = dock + else if (dock === this.hoveredDock) this.hoveredDock = null + this.checkCleanupDockHoverEvents() + })) ) this.initializeContent() this.observeScrollbarStyle() @@ -190,10 +196,9 @@ class WorkspaceElement extends HTMLElement { if (this.hoveredDock && this.hoveredDock.pointWithinHoverArea(mousePosition, true)) return const docks = [this.model.getLeftDock(), this.model.getRightDock(), this.model.getBottomDock()] - this.hoveredDock = + const nextHoveredDock = docks.find(dock => dock !== this.hoveredDock && dock.pointWithinHoverArea(mousePosition)) - docks.forEach(dock => { dock.setHovered(dock === this.hoveredDock) }) - this.checkCleanupDockHoverEvents() + docks.forEach(dock => { dock.setHovered(dock === nextHoveredDock) }) } checkCleanupDockHoverEvents () {