From 30dd6fbd2835832d4b6955d220e69f3beef4143d Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Thu, 5 Sep 2019 18:58:59 +0200 Subject: [PATCH] Restore window size/position if the app is exited while in fullscreen (#1778) * Restore window size/position if the app is exited while in fullscreen * Refactor resize and fullscreen placements to window for better restoring * Fix showing the top bar while in theatre mode * Fixes a crash when quitting the app while in resize mode --- .../ui/widgets/NavigationBarWidget.java | 59 ++++++++----------- .../vrbrowser/ui/widgets/TopBarWidget.java | 2 +- .../vrbrowser/ui/widgets/WindowWidget.java | 48 +++++++++++++++ .../mozilla/vrbrowser/ui/widgets/Windows.java | 38 +++++++++--- 4 files changed, 104 insertions(+), 43 deletions(-) diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java index ac13d1e6b..c96372f08 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/NavigationBarWidget.java @@ -64,12 +64,8 @@ public class NavigationBarWidget extends UIWidget implements GeckoSession.Naviga private ViewGroup mResizeModeContainer; private WindowWidget mAttachedWindow; private boolean mIsLoading; - private boolean mIsInFullScreenMode; - private boolean mIsResizing; private boolean mIsInVRVideo; private boolean mAutoEnteredVRVideo; - private WidgetPlacement mPlacementBeforeResize; - private WidgetPlacement mPlacementBeforeFullscreen; private Runnable mResizeBackHandler; private Runnable mFullScreenBackHandler; private Runnable mVRVideoBackHandler; @@ -131,9 +127,6 @@ private void initialize(@NonNull Context aContext) { mBrightnessButton = findViewById(R.id.brightnessButton); mFullScreenResizeButton = findViewById(R.id.fullScreenResizeEnterButton); mProjectionButton = findViewById(R.id.projectionButton); - mPlacementBeforeResize = new WidgetPlacement(aContext); - mPlacementBeforeFullscreen = new WidgetPlacement(aContext); - mResizeBackHandler = () -> exitResizeMode(ResizeAction.RESTORE_SIZE); @@ -365,10 +358,10 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) { @Override public void detachFromWindow() { - if (mIsResizing) { + if (mAttachedWindow != null && mAttachedWindow.isResizing()) { exitResizeMode(ResizeAction.RESTORE_SIZE); } - if (mIsInFullScreenMode) { + if (mAttachedWindow != null && mAttachedWindow.isFullScreen()) { exitFullScreenMode(); } @@ -434,7 +427,6 @@ protected void onDraw(Canvas canvas) { } private void setFullScreenSize() { - mPlacementBeforeFullscreen.copyFrom(mAttachedWindow.getPlacement()); final float minScale = WidgetPlacement.floatDimension(getContext(), R.dimen.window_fullscreen_min_scale); // Set browser fullscreen size float aspect = SettingsStore.getInstance(getContext()).getWindowAspect(); @@ -451,13 +443,14 @@ private void setFullScreenSize() { } private void enterFullScreenMode() { - if (mIsInFullScreenMode) { + if (mAttachedWindow.isFullScreen()) { return; } + mAttachedWindow.saveBeforeFullscreenPlacement(); setFullScreenSize(); mWidgetManager.pushBackHandler(mFullScreenBackHandler); - mIsInFullScreenMode = true; + mAttachedWindow.setIsFullScreen(true); AnimationHelper.fadeIn(mFullScreenModeContainer, AnimationHelper.FADE_ANIMATION_DURATION, null); AnimationHelper.fadeOut(mNavigationContainer, 0, null); @@ -491,7 +484,7 @@ private void enterFullScreenMode() { } private void exitFullScreenMode() { - if (!mIsInFullScreenMode) { + if (!mAttachedWindow.isFullScreen()) { mWidgetManager.setTrayVisible(true); return; } @@ -504,10 +497,10 @@ private void exitFullScreenMode() { } }, 50); - mAttachedWindow.getPlacement().copyFrom(mPlacementBeforeFullscreen); + mAttachedWindow.restoreBeforeFullscreenPlacement(); mWidgetManager.updateWidget(mAttachedWindow); - mIsInFullScreenMode = false; + mAttachedWindow.setIsFullScreen(false); mWidgetManager.popBackHandler(mFullScreenBackHandler); AnimationHelper.fadeIn(mNavigationContainer, AnimationHelper.FADE_ANIMATION_DURATION, null); @@ -521,14 +514,14 @@ private void exitFullScreenMode() { } private void enterResizeMode() { - if (mIsResizing) { + if (mAttachedWindow.isResizing()) { return; } - mIsResizing = true; - mPlacementBeforeResize.copyFrom(mAttachedWindow.getPlacement()); + mAttachedWindow.setIsResizing(true); + mAttachedWindow.saveBeforeResizePlacement(); startWidgetResize(); AnimationHelper.fadeIn(mResizeModeContainer, AnimationHelper.FADE_ANIMATION_DURATION, null); - if (mIsInFullScreenMode) { + if (mAttachedWindow.isFullScreen()) { AnimationHelper.fadeOut(mFullScreenModeContainer, 0, null); } else { AnimationHelper.fadeOut(mNavigationContainer, 0, null); @@ -574,24 +567,24 @@ enum ResizeAction { } private void exitResizeMode(ResizeAction aResizeAction) { - if (!mIsResizing) { + if (!mAttachedWindow.isResizing()) { return; } if (aResizeAction == ResizeAction.RESTORE_SIZE) { - mAttachedWindow.getPlacement().copyFrom(mPlacementBeforeResize); + mAttachedWindow.restoreBeforeResizePlacement(); mWidgetManager.updateWidget(mAttachedWindow); mWidgetManager.updateVisibleWidgets(); } - mIsResizing = false; + mAttachedWindow.setIsResizing(false); finishWidgetResize(); - if (mIsInFullScreenMode) { + if (mAttachedWindow.isFullScreen()) { AnimationHelper.fadeIn(mFullScreenModeContainer, AnimationHelper.FADE_ANIMATION_DURATION, null); } else { AnimationHelper.fadeIn(mNavigationContainer, AnimationHelper.FADE_ANIMATION_DURATION, null); } AnimationHelper.fadeOut(mResizeModeContainer, 0, () -> updateWidget()); mWidgetManager.popBackHandler(mResizeBackHandler); - mWidgetManager.setTrayVisible(!mIsInFullScreenMode); + mWidgetManager.setTrayVisible(!mAttachedWindow.isFullScreen()); closeFloatingMenus(); if (aResizeAction == ResizeAction.KEEP_SIZE) { @@ -822,10 +815,10 @@ public void onSecurityChange(GeckoSession geckoSession, SecurityInformation secu @Override public void onFullScreen(GeckoSession session, boolean aFullScreen) { if (aFullScreen) { - if (!mIsInFullScreenMode) { + if (!mAttachedWindow.isFullScreen()) { enterFullScreenMode(); } - if (mIsResizing) { + if (mAttachedWindow.isResizing()) { exitResizeMode(ResizeAction.KEEP_SIZE); } AtomicBoolean autoEnter = new AtomicBoolean(false); @@ -847,7 +840,7 @@ public void onFullScreen(GeckoSession session, boolean aFullScreen) { // WidgetManagerDelegate.UpdateListener @Override public void onWidgetUpdate(Widget aWidget) { - if (aWidget == mAttachedWindow && !mIsResizing) { + if (aWidget == mAttachedWindow && !mAttachedWindow.isResizing()) { handleWindowResize(); } } @@ -875,9 +868,9 @@ public void onCurrentSessionChange(GeckoSession aSession, int aId) { handleSessionState(); boolean isFullScreen = mSessionStack.isInFullScreen(aSession); - if (isFullScreen && !mIsInFullScreenMode) { + if (isFullScreen && !mAttachedWindow.isFullScreen()) { enterFullScreenMode(); - } else if (!isFullScreen && mIsInFullScreenMode) { + } else if (!isFullScreen && mAttachedWindow.isFullScreen()) { exitVRVideo(); exitFullScreenMode(); } @@ -1026,10 +1019,10 @@ public void onHistoryViewHidden(WindowWidget aWindow) { @Override public void onBookmarksClicked() { - if (mIsResizing) { + if (mAttachedWindow.isResizing()) { exitResizeMode(ResizeAction.RESTORE_SIZE); - } else if (mIsInFullScreenMode) { + } else if (mAttachedWindow.isFullScreen()) { exitFullScreenMode(); } else if (mIsInVRVideo) { @@ -1044,10 +1037,10 @@ public void onPrivateBrowsingClicked() { @Override public void onHistoryClicked() { - if (mIsResizing) { + if (mAttachedWindow.isResizing()) { exitResizeMode(ResizeAction.RESTORE_SIZE); - } else if (mIsInFullScreenMode) { + } else if (mAttachedWindow.isFullScreen()) { exitFullScreenMode(); } else if (mIsInVRVideo) { diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/TopBarWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/TopBarWidget.java index 9b3d0fe52..4368fa83c 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/TopBarWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/TopBarWidget.java @@ -167,7 +167,7 @@ public void onCurrentSessionChange(GeckoSession aSession, int aId) { @Override public void setVisible(boolean aIsVisible) { - if (mVisible == aIsVisible) { + if (mVisible == aIsVisible || mWidgetManager == null) { return; } mVisible = aIsVisible; diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java index fced5e0b0..85e97b9c0 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/WindowWidget.java @@ -131,6 +131,10 @@ default void onBookmarksHidden(WindowWidget aWindow) {} boolean mClickedAfterFocus = false; boolean mIsBookmarksVisible = false; boolean mIsHistoryVisible = false; + private WidgetPlacement mPlacementBeforeFullscreen; + private WidgetPlacement mPlacementBeforeResize; + private boolean mIsResizing; + private boolean mIsFullScreen; public interface WindowDelegate { void onFocusRequest(@NonNull WindowWidget aWindow); @@ -163,6 +167,10 @@ public WindowWidget(Context aContext, int windowId, boolean privateMode) { mHandle = ((WidgetManagerDelegate)aContext).newWidgetHandle(); mWidgetPlacement = new WidgetPlacement(aContext); + mPlacementBeforeFullscreen = new WidgetPlacement(aContext); + mPlacementBeforeResize = new WidgetPlacement(aContext); + mIsResizing = false; + mIsFullScreen = false; initializeWidgetPlacement(mWidgetPlacement); mTopBar = new TopBarWidget(aContext); @@ -781,6 +789,46 @@ protected void updateBorder() { } } + public void saveBeforeFullscreenPlacement() { + mPlacementBeforeFullscreen.copyFrom(mWidgetPlacement); + } + + public void restoreBeforeFullscreenPlacement() { + mWidgetPlacement.copyFrom(mPlacementBeforeFullscreen); + } + + public WidgetPlacement getBeforeFullscreenPlacement() { + return mPlacementBeforeFullscreen; + } + + public void saveBeforeResizePlacement() { + mPlacementBeforeResize.copyFrom(mWidgetPlacement); + } + + public void restoreBeforeResizePlacement() { + mWidgetPlacement.copyFrom(mPlacementBeforeResize); + } + + public WidgetPlacement getBeforeResizePlacement() { + return mPlacementBeforeResize; + } + + public void setIsResizing(boolean isResizing) { + mIsResizing = isResizing; + } + + public boolean isResizing() { + return mIsResizing; + } + + public void setIsFullScreen(boolean isFullScreen) { + mIsFullScreen = isFullScreen; + } + + public boolean isFullScreen() { + return mIsFullScreen; + } + public void setWindowDelegate(WindowDelegate aDelegate) { mWindowDelegate = aDelegate; } diff --git a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/Windows.java b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/Windows.java index bc2c9789b..f52fcd032 100644 --- a/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/Windows.java +++ b/app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/Windows.java @@ -41,12 +41,28 @@ class WindowState { float worldWidth; public void load(WindowWidget aWindow) { - placement = aWindow.getWindowPlacement(); + if (aWindow == mFullscreenWindow) { + placement = mPrevWindowPlacement; + + } else { + placement = aWindow.getWindowPlacement(); + } sessionStack = aWindow.getSessionStack(); currentSessionId = aWindow.getSessionStack().getCurrentSessionId(); - textureWidth = aWindow.getPlacement().width; - textureHeight = aWindow.getPlacement().height; - worldWidth = aWindow.getPlacement().worldWidth; + WidgetPlacement placement; + if (aWindow.isFullScreen()) { + placement = aWindow.getBeforeFullscreenPlacement(); + + } else if (aWindow.isResizing()) { + placement = aWindow.getBeforeResizePlacement(); + + } else { + placement = aWindow.getPlacement(); + } + + textureWidth = placement.width; + textureHeight = placement.height; + worldWidth = placement.worldWidth; } } @@ -802,15 +818,19 @@ private WindowWidget createWindow() { } public void enterResizeMode() { - for (WindowWidget window : getCurrentWindows()) { - window.getTopBar().setVisible(false); + if (mFullscreenWindow == null) { + for (WindowWidget window : getCurrentWindows()) { + window.getTopBar().setVisible(false); + } } } public void exitResizeMode() { - for (WindowWidget window : getCurrentWindows()) { - if (getCurrentWindows().size() > 1 || isInPrivateMode()) { - window.getTopBar().setVisible(window != mFullscreenWindow); + if (mFullscreenWindow == null) { + for (WindowWidget window : getCurrentWindows()) { + if (getCurrentWindows().size() > 1 || isInPrivateMode()) { + window.getTopBar().setVisible(true); + } } } }