Skip to content

Commit

Permalink
Restore window size/position if the app is exited while in fullscreen (
Browse files Browse the repository at this point in the history
…#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
  • Loading branch information
keianhzo committed Sep 5, 2019
1 parent c639a36 commit 30dd6fb
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 43 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

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

Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -491,7 +484,7 @@ private void enterFullScreenMode() {
}

private void exitFullScreenMode() {
if (!mIsInFullScreenMode) {
if (!mAttachedWindow.isFullScreen()) {
mWidgetManager.setTrayVisible(true);
return;
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Expand Up @@ -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;
Expand Down
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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;
}
}

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

0 comments on commit 30dd6fb

Please sign in to comment.