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

Commit

Permalink
For #15543 - Adjust the height of the tabs tray depending on the numb…
Browse files Browse the repository at this point in the history
…er of tabs (#15749)
  • Loading branch information
Mugurell committed Oct 21, 2020
1 parent aae43b8 commit 358ca2c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,13 @@ class TabTrayDialogFragment : AppCompatDialogFragment(), UserInteractionHandler

if (newConfig.orientation != currentOrientation) {
tabTrayView.dismissMenu()
tabTrayView.expand()
tabTrayView.updateBottomSheetBehavior()

if (requireContext().settings().gridTabView) {
// Update the number of columns to use in the grid view when the screen
// orientation changes.
tabTrayView.updateTabsTrayLayout()
}

currentOrientation = newConfig.orientation
}
}
Expand Down Expand Up @@ -205,8 +204,7 @@ class TabTrayDialogFragment : AppCompatDialogFragment(), UserInteractionHandler
),
store = tabTrayDialogStore,
isPrivate = isPrivate,
startingInLandscape = requireContext().resources.configuration.orientation ==
Configuration.ORIENTATION_LANDSCAPE,
isInLandscape = ::isInLandscape,
lifecycleOwner = viewLifecycleOwner
) { private ->
val filter: (TabSessionState) -> Boolean = { state -> private == state.content.private }
Expand Down Expand Up @@ -450,6 +448,10 @@ class TabTrayDialogFragment : AppCompatDialogFragment(), UserInteractionHandler
}
}

private fun isInLandscape(): Boolean {
return requireContext().resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
}

companion object {
private const val ELEVATION = 80f
}
Expand Down
55 changes: 42 additions & 13 deletions app/src/main/java/org/mozilla/fenix/tabtray/TabTrayView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import mozilla.components.browser.state.selector.getNormalOrPrivateTabs
import mozilla.components.browser.state.selector.normalTabs
import mozilla.components.browser.state.selector.privateTabs
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.TabSessionState
import mozilla.components.browser.tabstray.TabViewHolder
import mozilla.components.feature.syncedtabs.SyncedTabsFeature
import mozilla.components.support.base.feature.ViewBoundFeatureWrapper
Expand All @@ -54,6 +55,7 @@ import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.tabtray.SaveToCollectionsButtonAdapter.MultiselectModeChange
import org.mozilla.fenix.tabtray.TabTrayDialogFragmentState.Mode
import java.text.NumberFormat
import kotlin.math.max
import mozilla.components.browser.storage.sync.Tab as SyncTab

/**
Expand All @@ -66,7 +68,7 @@ class TabTrayView(
private val interactor: TabTrayInteractor,
store: TabTrayDialogFragmentStore,
isPrivate: Boolean,
startingInLandscape: Boolean,
private val isInLandscape: () -> Boolean,
lifecycleOwner: LifecycleOwner,
private val filterTabs: (Boolean) -> Unit
) : LayoutContainer, TabLayout.OnTabSelectedListener {
Expand Down Expand Up @@ -145,20 +147,14 @@ class TabTrayView(

view.tab_layout.addOnTabSelectedListener(this)

val tabs = if (isPrivate) {
view.context.components.core.store.state.privateTabs
} else {
view.context.components.core.store.state.normalTabs
}
val tabs = getTabs(isPrivate)

val selectedBrowserTabIndex = tabs
.indexOfFirst { it.id == view.context.components.core.store.state.selectedTabId }

if (tabs.size > EXPAND_AT_SIZE || startingInLandscape) {
expand()
}
updateBottomSheetBehavior()

setTopOffset(startingInLandscape)
setTopOffset(isInLandscape())

if (view.context.settings().syncedTabsInTabsTray) {
syncedTabsFeature.set(
Expand Down Expand Up @@ -280,6 +276,27 @@ class TabTrayView(
}
}

private fun getTabs(isPrivate: Boolean): List<TabSessionState> = if (isPrivate) {
view.context.components.core.store.state.privateTabs
} else {
view.context.components.core.store.state.normalTabs
}

private fun getTabsNumberInAnyMode(): Int {
return max(
view.context.components.core.store.state.normalTabs.size,
view.context.components.core.store.state.privateTabs.size
)
}

private fun getTabsNumberForExpandingTray(): Int {
return if (container.context.settings().gridTabView) {
EXPAND_AT_GRID_SIZE
} else {
EXPAND_AT_LIST_SIZE
}
}

private fun handleTabClicked(tab: SyncTab) {
interactor.onSyncedTabClicked(tab)
}
Expand Down Expand Up @@ -312,8 +329,17 @@ class TabTrayView(
components.analytics.metrics.track(eventToSend)
}

fun expand() {
behavior.state = BottomSheetBehavior.STATE_EXPANDED
/**
* Updates the bottom sheet height based on the number tabs or screen orientation.
* Show the bottom sheet fully expanded if it is in landscape mode or the number of
* tabs are greater or equal to the expand size limit.
*/
fun updateBottomSheetBehavior() {
if (isInLandscape() || getTabsNumberInAnyMode() >= getTabsNumberForExpandingTray()) {
behavior.state = BottomSheetBehavior.STATE_EXPANDED
} else {
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
}
}

enum class TabChange {
Expand Down Expand Up @@ -668,7 +694,10 @@ class TabTrayView(
private const val TAB_COUNT_SHOW_CFR = 6
private const val DEFAULT_TAB_ID = 0
private const val PRIVATE_TAB_ID = 1
private const val EXPAND_AT_SIZE = 3
// Minimum number of list items for which to show the tabs tray as expanded.
private const val EXPAND_AT_LIST_SIZE = 4
// Minimum number of grid items for which to show the tabs tray as expanded.
private const val EXPAND_AT_GRID_SIZE = 3
private const val SLIDE_OFFSET = 0
private const val SELECTION_DELAY = 500
private const val NORMAL_HANDLE_PERCENT_WIDTH = 0.1F
Expand Down

0 comments on commit 358ca2c

Please sign in to comment.