Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
Fix dock edge on load race bug (#21)
Browse files Browse the repository at this point in the history
Fix dock edge on load race condition
  • Loading branch information
kangelSL authored and oriondean committed Oct 15, 2019
1 parent 9508eb9 commit dca5a78
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions 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<Rect> => {
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<ScreenEdge>(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;
}

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit dca5a78

Please sign in to comment.