From dca5a78d4ab7418d0bef72710c6ef8d2997a16f8 Mon Sep 17 00:00:00 2001 From: kangelSL <51117162+kangelSL@users.noreply.github.com> Date: Tue, 15 Oct 2019 15:21:22 +0100 Subject: [PATCH] Fix dock edge on load race bug (#21) Fix dock edge on load race condition --- src/useDockWindow.ts | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/useDockWindow.ts b/src/useDockWindow.ts index a438f1e..5668160 100644 --- a/src/useDockWindow.ts +++ b/src/useDockWindow.ts @@ -1,40 +1,41 @@ -import {MonitorDetails, MonitorInfo, Rect} from "openfin/_v2/api/system/monitor"; +import { MonitorDetails, MonitorInfo, Rect } from "openfin/_v2/api/system/monitor"; import Bounds from "openfin/_v2/api/window/bounds"; -import {Transition} from "openfin/_v2/api/window/transition"; -import {_Window} from "openfin/_v2/api/window/window"; -import {useEffect, useState} from "react"; +import { Transition } from "openfin/_v2/api/window/transition"; +import { _Window } from "openfin/_v2/api/window/window"; +import { useEffect, useState } from "react"; -import {IDimensions, IUseDockWindowOptions} from "../index"; -import {ScreenEdge} from "./ScreenEdge"; +import { IDimensions, IUseDockWindowOptions } from "../index"; +import { ScreenEdge } from "./ScreenEdge"; import transitions from "./useDockWindow.transitions"; import usePreviousValue from "./utils/usePreviousValue"; -let isAnimating = false; +interface IBoundsChangedEvent extends fin.WindowBoundsEvent { + changeType: number; + reason?: "animation" | "self"; +} const getMonitorRect = async (bounds: Bounds): Promise => { const monitorInfo: MonitorInfo = await fin.System.getMonitorInfo(); return monitorInfo.nonPrimaryMonitors - .concat(monitorInfo.primaryMonitor) - .map((info: MonitorDetails) => info.availableRect) - .find((rect) => bounds.left >= rect.left && (bounds.left + bounds.width) <= rect.right && - bounds.top >= rect.top && (bounds.top + bounds.height) <= rect.bottom) + .concat(monitorInfo.primaryMonitor) + .map((info: MonitorDetails) => info.availableRect) + .find((rect) => bounds.left >= rect.left && (bounds.left + bounds.width) <= rect.right && + bounds.top >= rect.top && (bounds.top + bounds.height) <= rect.bottom) || monitorInfo.primaryMonitor.availableRect; }; -export default (initialEdge = ScreenEdge.NONE, toMove: _Window = fin.Window.getCurrentSync(), - allowUserToUndock: boolean = true, stretchToFit?: IDimensions, - options?: IUseDockWindowOptions) => { +export default ( + initialEdge = ScreenEdge.NONE, toMove: _Window = fin.Window.getCurrentSync(), + allowUserToUndock: boolean = true, stretchToFit?: IDimensions, options?: IUseDockWindowOptions, +) => { const [edge, setEdge] = useState(initialEdge); const [isUndocking, setIsUndocking] = useState(false); const previousEdge = usePreviousValue(edge); useEffect(() => { - const handleBoundsChanged = (event: fin.WindowBoundsEvent) => { + const handleBoundsChanged = (event: IBoundsChangedEvent) => { // Don't reset edge if we're the ones moving it or only a resize bound event has occurred - if (isAnimating || event.changeType === 1) { - if (isAnimating) { - isAnimating = false; - } + if (event.reason && event.reason === "animation" || event.changeType === 1) { return; } @@ -68,7 +69,6 @@ export default (initialEdge = ScreenEdge.NONE, toMove: _Window = fin.Window.getC useEffect(() => { const performDockTransition = async () => { - isAnimating = true; // set flag to prevent bounds listener from resetting edge to NONE const bounds: Bounds = await toMove.getBounds(); const monitorRect: Rect = await getMonitorRect(bounds);