Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Improved set session code
Browse files Browse the repository at this point in the history
Simplify setActive to only deactivate the old session


Use state for setActive
  • Loading branch information
keianhzo committed May 18, 2020
1 parent 9d848c4 commit 08a5be8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public class WindowWidget extends UIWidget implements SessionChangeListener,
public static final int SESSION_RELEASE_DISPLAY = 0;
public static final int SESSION_DO_NOT_RELEASE_DISPLAY = 1;

@IntDef(value = { DEACTIVATE_CURRENT_SESSION, LEAVE_CURRENT_SESSION_ACTIVE})
public @interface SetSessionActiveState {}
public static final int DEACTIVATE_CURRENT_SESSION = 0;
public static final int LEAVE_CURRENT_SESSION_ACTIVE = 1;

private Surface mSurface;
private int mWidth;
Expand Down Expand Up @@ -1107,21 +1111,28 @@ public void draw(Canvas aCanvas) {
}
}

public void setSession(@NonNull Session aSession) {
setSession(aSession, SESSION_RELEASE_DISPLAY);
public void setSession(@NonNull Session aSession, @SetSessionActiveState int previousSessionState) {
setSession(aSession, SESSION_RELEASE_DISPLAY, previousSessionState);
}

public void setSession(@NonNull Session aSession, @OldSessionDisplayAction int aDisplayAction) {
public void setSession(@NonNull Session aSession, @OldSessionDisplayAction int aDisplayAction, @SetSessionActiveState int previousSessionState) {
if (mSession != aSession) {
Session oldSession = mSession;
if (oldSession != null) {
cleanListeners(oldSession);
if (previousSessionState == DEACTIVATE_CURRENT_SESSION) {
oldSession.setActive(false);
}
if (aDisplayAction == SESSION_RELEASE_DISPLAY) {
oldSession.releaseDisplay();
}
}

mSession = aSession;

setupListeners(mSession);
SessionStore.get().setActiveSession(mSession);

mViewModel.setIsPrivateSession(mSession.isPrivateMode());

if (oldSession != null) {
Expand Down Expand Up @@ -1165,10 +1176,7 @@ public void onStackSession(Session aSession) {
// e.g. tab opened via window.open()
aSession.updateLastUse();
Session current = mSession;
setupListeners(aSession);
setSession(aSession);
SessionStore.get().setActiveSession(aSession);
aSession.setActive(true);
setSession(aSession, WindowWidget.DEACTIVATE_CURRENT_SESSION);
current.captureBackgroundBitmap(getWindowWidth(), getWindowHeight()).thenAccept(aVoid -> current.setActive(false));
mWidgetManager.getWindows().showTabAddedNotification();

Expand All @@ -1178,10 +1186,7 @@ public void onStackSession(Session aSession) {
@Override
public void onUnstackSession(Session aSession, Session aParent) {
if (mSession == aSession) {
aParent.setActive(true);
setupListeners(aParent);
setSession(aParent);
SessionStore.get().setActiveSession(aParent);
setSession(aParent, WindowWidget.DEACTIVATE_CURRENT_SESSION);
SessionStore.get().destroySession(aSession);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -1213,21 +1212,14 @@ public void onTabSelect(Session aTab) {
Session moveTo = targetWindow.getSession();
moveFrom.surfaceDestroyed();
moveTo.surfaceDestroyed();
windowToMove.setupListeners(moveTo);
windowToMove.setSession(moveTo, WindowWidget.SESSION_DO_NOT_RELEASE_DISPLAY);
targetWindow.setupListeners(moveFrom);
targetWindow.setSession(moveFrom, WindowWidget.SESSION_DO_NOT_RELEASE_DISPLAY);
SessionStore.get().setActiveSession(targetWindow.getSession());
windowToMove.setSession(moveTo, WindowWidget.SESSION_DO_NOT_RELEASE_DISPLAY, WindowWidget.LEAVE_CURRENT_SESSION_ACTIVE);
targetWindow.setSession(moveFrom, WindowWidget.SESSION_DO_NOT_RELEASE_DISPLAY, WindowWidget.LEAVE_CURRENT_SESSION_ACTIVE);
windowToMove.setActiveWindow(false);
targetWindow.setActiveWindow(true);

} else {
setFirstPaint(targetWindow, aTab);
targetWindow.getSession().setActive(false);
targetWindow.setupListeners(aTab);
aTab.setActive(true);
targetWindow.setSession(aTab);
SessionStore.get().setActiveSession(aTab);
targetWindow.setSession(aTab, WindowWidget.DEACTIVATE_CURRENT_SESSION);
}
}

Expand All @@ -1238,16 +1230,12 @@ public void addTab(WindowWidget targetWindow) {
public void addTab(@NonNull WindowWidget targetWindow, @Nullable String aUri) {
Session session = SessionStore.get().createSuspendedSession(aUri, targetWindow.getSession().isPrivateMode());
setFirstPaint(targetWindow, session);
targetWindow.getSession().setActive(false);
targetWindow.setupListeners(session);
session.setActive(true);
targetWindow.setSession(session);
targetWindow.setSession(session, WindowWidget.DEACTIVATE_CURRENT_SESSION);
if (aUri == null || aUri.isEmpty()) {
session.loadHomePage();
} else {
session.loadUri(aUri);
}
SessionStore.get().setActiveSession(session);
}

public void addBackgroundTab(WindowWidget targetWindow, String aUri) {
Expand Down Expand Up @@ -1303,9 +1291,7 @@ public void onTabsClose(ArrayList<Session> aTabs) {
Session tab = available.get(0);
if (tab != null) {
setFirstPaint(window, tab);
window.setupListeners(tab);
tab.setActive(true);
window.setSession(tab);
window.setSession(tab, WindowWidget.LEAVE_CURRENT_SESSION_ACTIVE);
}

available.remove(0);
Expand All @@ -1320,8 +1306,6 @@ public void onTabsClose(ArrayList<Session> aTabs) {
cache.removeBitmap(session.getId());
SessionStore.get().destroySession(session);
}

SessionStore.get().setActiveSession(targetWindow.getSession());
}

@Override
Expand All @@ -1342,10 +1326,7 @@ public void onTabsReceived(@NonNull List<TabData> aTabs) {

if (i == 0 && !fullscreen) {
// Set the first received tab of the list the current one.
SessionStore.get().setActiveSession(session);
targetWindow.setupListeners(session);
targetWindow.getSession().setActive(false);
targetWindow.setSession(session);
targetWindow.setSession(session, WindowWidget.LEAVE_CURRENT_SESSION_ACTIVE);
}
}

Expand Down

0 comments on commit 08a5be8

Please sign in to comment.