From ca1cfe3dc6841f67c27acbed27b85a65b4add602 Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Wed, 19 Sep 2018 21:43:56 -0700 Subject: [PATCH] Don't center windows when restoring from serialized state --- js/actionCreators/index.ts | 3 ++- js/actionCreators/windows.ts | 14 ++++++++++---- js/actionTypes.ts | 1 + js/components/WindowManager.js | 33 +++++++++++++++++++++++++-------- js/reducers/windows.ts | 18 ++++++++++++++---- js/selectors.ts | 4 ++++ js/types.ts | 1 + js/webampLazy.js | 11 +++++------ 8 files changed, 62 insertions(+), 23 deletions(-) diff --git a/js/actionCreators/index.ts b/js/actionCreators/index.ts index 76844812..77a1552c 100644 --- a/js/actionCreators/index.ts +++ b/js/actionCreators/index.ts @@ -19,7 +19,8 @@ export { setWindowSize, toggleWindow, updateWindowPositions, - toggleMainWindowShadeMode + toggleMainWindowShadeMode, + windowsHaveBeenCentered } from "./windows"; export { play, diff --git a/js/actionCreators/windows.ts b/js/actionCreators/windows.ts index 830e43c0..f95a1805 100644 --- a/js/actionCreators/windows.ts +++ b/js/actionCreators/windows.ts @@ -12,7 +12,8 @@ import { TOGGLE_WINDOW, CLOSE_WINDOW, TOGGLE_WINDOW_SHADE_MODE, - SET_WINDOW_VISIBILITY + SET_WINDOW_VISIBILITY, + WINDOWS_HAVE_BEEN_CENTERED } from "../actionTypes"; import { getPositionDiff, SizeDiff } from "../resizeUtils"; @@ -51,7 +52,7 @@ function withWindowGraphIntegrity(action: Action): Dispatchable { applyDiff(position, positionDiff[key]) ); - dispatch(updateWindowPositions(newPositions)); + dispatch(updateWindowPositions(newPositions, false)); }; } @@ -104,7 +105,12 @@ export function toggleWindow(windowId: WindowId): Dispatchable { } export function updateWindowPositions( - positions: WindowPositions + positions: WindowPositions, + center: boolean ): Dispatchable { - return { type: UPDATE_WINDOW_POSITIONS, positions }; + return { type: UPDATE_WINDOW_POSITIONS, positions, center }; +} + +export function windowsHaveBeenCentered() { + return { type: WINDOWS_HAVE_BEEN_CENTERED }; } diff --git a/js/actionTypes.ts b/js/actionTypes.ts index 8586771c..d6eb9f4d 100644 --- a/js/actionTypes.ts +++ b/js/actionTypes.ts @@ -72,3 +72,4 @@ export const SET_WINDOW_VISIBILITY = "SET_WINDOW_VISIBILITY"; export const LOADING = "LOADING"; export const CLOSE_REQUESTED = "CLOSE_REQUESTED"; export const LOAD_SERIALIZED_STATE = "LOAD_SERIALIZED_STATE"; +export const WINDOWS_HAVE_BEEN_CENTERED = "WINDOWS_HAVE_BEEN_CENTERED"; diff --git a/js/components/WindowManager.js b/js/components/WindowManager.js index b5a91cad..43576839 100644 --- a/js/components/WindowManager.js +++ b/js/components/WindowManager.js @@ -11,8 +11,16 @@ import { applyDiff, applyMultipleDiffs } from "../snapUtils"; -import { getWindowsInfo, getWindowHidden, getWindowOpen } from "../selectors"; -import { updateWindowPositions } from "../actionCreators"; +import { + getWindowsInfo, + getWindowHidden, + getWindowOpen, + getCenterRequested +} from "../selectors"; +import { + updateWindowPositions, + windowsHaveBeenCentered +} from "../actionCreators"; import { WINDOW_HEIGHT, WINDOW_WIDTH } from "../constants"; import { calculateBoundingBox } from "../utils"; @@ -25,11 +33,14 @@ const abuts = (a, b) => { class WindowManager extends React.Component { componentDidMount() { - this.centerWindows(); + this.centerWindowsIfNeeded(); } - centerWindows = () => { - const { container } = this.props; + centerWindowsIfNeeded = () => { + const { container, centerRequested } = this.props; + if (!centerRequested) { + return; + } const rect = container.getBoundingClientRect(); const offsetLeft = rect.left + window.scrollX; @@ -78,6 +89,7 @@ class WindowManager extends React.Component { this.props.updateWindowPositions(newPositions); } + this.props.windowsHaveBeenCentered(); }; movingAndStationaryNodes(key) { @@ -210,11 +222,16 @@ WindowManager.propTypes = { const mapStateToProps = state => ({ windowsInfo: getWindowsInfo(state), getWindowHidden: getWindowHidden(state), - getWindowOpen: getWindowOpen(state) + getWindowOpen: getWindowOpen(state), + centerRequested: getCenterRequested(state) }); -const mapDispatchToProps = { - updateWindowPositions +const mapDispatchToProps = dispatch => { + return { + updateWindowPositions: positions => + dispatch(updateWindowPositions(positions)), + windowsHaveBeenCentered: () => dispatch(windowsHaveBeenCentered()) + }; }; export default connect( diff --git a/js/reducers/windows.ts b/js/reducers/windows.ts index a643a9c2..e30158a8 100644 --- a/js/reducers/windows.ts +++ b/js/reducers/windows.ts @@ -15,6 +15,7 @@ import { objectMap } from "../utils"; export interface WindowsState { focused: string; + centerRequested: boolean; genWindows: { [name: string]: WebampWindow }; positions: WindowPositions; } @@ -28,11 +29,13 @@ interface SerializedWindow { export interface WindowsSerializedStateV1 { genWindows: { [windowId: string]: SerializedWindow }; + centerRequested: boolean; positions: WindowPositions; } const defaultWindowsState: WindowsState = { focused: WINDOWS.MAIN, + centerRequested: false, genWindows: { // TODO: Remove static capabilites and derive them from ids/generic main: { @@ -174,12 +177,17 @@ const windows = ( case UPDATE_WINDOW_POSITIONS: return { ...state, - positions: { ...state.positions, ...action.positions } + positions: { + ...state.positions, + ...action.positions + }, + centerRequested: action.center }; case LOAD_SERIALIZED_STATE: { const { genWindows: serializedWindows, - positions: serializedPositions + positions: serializedPositions, + centerRequested: serializedCenterRequested } = action.serializedState.windows; return { ...state, @@ -196,7 +204,8 @@ const windows = ( return position; } return serializedPosition; - }) + }), + centerRequested: serializedCenterRequested }; } @@ -217,7 +226,8 @@ export function getSerializedState( shade: w.shade || false }; }), - positions: state.positions + positions: state.positions, + centerRequested: state.centerRequested }; } diff --git a/js/selectors.ts b/js/selectors.ts index ba0b9e2a..122202fb 100644 --- a/js/selectors.ts +++ b/js/selectors.ts @@ -427,3 +427,7 @@ export function getBalance(state: AppState): number { export function getEqualizerEnabled(state: AppState): boolean { return state.equalizer.on; } + +export function getCenterRequested(state: AppState): boolean { + return state.windows.centerRequested; +} diff --git a/js/types.ts b/js/types.ts index 70b1fe96..ec5f70ff 100644 --- a/js/types.ts +++ b/js/types.ts @@ -256,6 +256,7 @@ export type Action = | { type: "UPDATE_WINDOW_POSITIONS"; positions: WindowPositions; + center: boolean; } | { type: "CLICKED_TRACK"; diff --git a/js/webampLazy.js b/js/webampLazy.js index c7db55a4..4b6b06d6 100644 --- a/js/webampLazy.js +++ b/js/webampLazy.js @@ -16,7 +16,8 @@ import { seekBackward, seekForward, next, - previous + previous, + updateWindowPositions } from "./actionCreators"; import { LOAD_STYLE } from "./constants"; import { uniqueId, objectMap, objectForEach } from "./utils"; @@ -28,7 +29,6 @@ import { CLOSE_WINAMP, MINIMIZE_WINAMP, ADD_GEN_WINDOW, - UPDATE_WINDOW_POSITIONS, LOADED, REGISTER_VISUALIZER, SET_Z_INDEX, @@ -155,10 +155,9 @@ class Winamp { this.store.dispatch(setWindowSize(windowId, w.size)); } }); - this.store.dispatch({ - type: UPDATE_WINDOW_POSITIONS, - positions: objectMap(layout, w => w.position) - }); + this.store.dispatch( + updateWindowPositions(objectMap(layout, w => w.position), true) + ); } if (enableHotkeys) {