Skip to content

Commit

Permalink
For mozilla-mobile#986 - Make View extension function for determining…
Browse files Browse the repository at this point in the history
… menu orientation
  • Loading branch information
ekager committed Jun 5, 2019
1 parent 704f1e6 commit 6d3c300
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/org/mozilla/fenix/ext/View.kt
Expand Up @@ -7,6 +7,7 @@ package org.mozilla.fenix.ext
import android.graphics.Rect
import android.view.TouchDelegate
import android.view.View
import mozilla.components.browser.menu.BrowserMenu
import mozilla.components.support.ktx.android.content.res.pxToDp

fun View?.increaseTapArea(extraDps: Int) {
Expand All @@ -22,3 +23,11 @@ fun View?.increaseTapArea(extraDps: Int) {
parent.touchDelegate = TouchDelegate(touchRect, this)
}
}

fun View.getMenuDirectionForLocation(): BrowserMenu.Orientation {
val location = IntArray(2)
getLocationInWindow(location)
return if (location[1] > (rootView.measuredHeight / 2))
BrowserMenu.Orientation.UP else
BrowserMenu.Orientation.DOWN
}
Expand Up @@ -16,11 +16,11 @@ import kotlinx.android.synthetic.main.collection_home_list_row.view.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import mozilla.components.browser.menu.BrowserMenu
import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
import org.mozilla.fenix.R
import org.mozilla.fenix.ThemeManager
import org.mozilla.fenix.ext.getMenuDirectionForLocation
import org.mozilla.fenix.ext.increaseTapArea
import org.mozilla.fenix.ext.urlToTrimmedHost
import org.mozilla.fenix.home.sessioncontrol.CollectionAction
Expand Down Expand Up @@ -57,15 +57,11 @@ class CollectionViewHolder(
collection_overflow_button.run {
increaseTapArea(buttonIncreaseDps)
setOnClickListener {
val location = IntArray(2)
it.getLocationInWindow(location)
collectionMenu.menuBuilder
.build(view.context)
.show(
anchor = it,
orientation = if (location[1] > (rootView.measuredHeight / 2))
BrowserMenu.Orientation.UP else
BrowserMenu.Orientation.DOWN
orientation = it.getMenuDirectionForLocation()
)
}
}
Expand Down
Expand Up @@ -9,10 +9,10 @@ import android.view.View
import androidx.recyclerview.widget.RecyclerView
import io.reactivex.Observer
import kotlinx.android.synthetic.main.tab_header.view.*
import mozilla.components.browser.menu.BrowserMenu
import mozilla.components.browser.menu.BrowserMenuBuilder
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.getMenuDirectionForLocation
import org.mozilla.fenix.home.sessioncontrol.SessionControlAction
import org.mozilla.fenix.home.sessioncontrol.TabAction
import org.mozilla.fenix.home.sessioncontrol.onNext
Expand Down Expand Up @@ -48,7 +48,7 @@ class TabHeaderViewHolder(
setOnClickListener {
tabsMenu.menuBuilder
.build(view.context)
.show(anchor = it, orientation = BrowserMenu.Orientation.DOWN)
.show(anchor = it, orientation = it.getMenuDirectionForLocation())
}
}
}
Expand Down
Expand Up @@ -20,12 +20,12 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import mozilla.appservices.places.BookmarkRoot
import mozilla.components.browser.icons.IconRequest
import mozilla.components.browser.menu.BrowserMenu
import mozilla.components.concept.storage.BookmarkNode
import mozilla.components.concept.storage.BookmarkNodeType
import org.mozilla.fenix.R
import org.mozilla.fenix.ThemeManager
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getMenuDirectionForLocation
import org.mozilla.fenix.ext.increaseTapArea
import kotlin.coroutines.CoroutineContext

Expand Down Expand Up @@ -176,13 +176,9 @@ class BookmarkAdapter(val emptyView: View, val actionEmitter: Observer<BookmarkA

bookmark_overflow.increaseTapArea(bookmarkOverflowExtraDips)
bookmark_overflow.setOnClickListener {
val location = IntArray(2)
it.getLocationInWindow(location)
bookmarkItemMenu.menuBuilder.build(containerView.context).show(
anchor = it,
orientation = if (location[1] > (it.rootView.measuredHeight / 2))
BrowserMenu.Orientation.UP else
BrowserMenu.Orientation.DOWN
orientation = it.getMenuDirectionForLocation()
)
}
bookmark_title.text = if (item.title.isNullOrBlank()) item.url else item.title
Expand Down Expand Up @@ -317,7 +313,7 @@ class BookmarkAdapter(val emptyView: View, val actionEmitter: Observer<BookmarkA
bookmark_overflow.setOnClickListener {
bookmarkItemMenu.menuBuilder.build(containerView.context).show(
anchor = it,
orientation = BrowserMenu.Orientation.DOWN
orientation = it.getMenuDirectionForLocation()
)
}
bookmark_layout.setOnLongClickListener(null)
Expand Down Expand Up @@ -384,7 +380,7 @@ class BookmarkAdapter(val emptyView: View, val actionEmitter: Observer<BookmarkA
bookmark_overflow.setOnClickListener {
bookmarkItemMenu.menuBuilder.build(containerView.context).show(
anchor = it,
orientation = BrowserMenu.Orientation.DOWN
orientation = it.getMenuDirectionForLocation()
)
}
}
Expand Down
Expand Up @@ -15,10 +15,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import mozilla.components.browser.icons.IconRequest
import mozilla.components.browser.menu.BrowserMenu
import org.mozilla.fenix.R
import org.mozilla.fenix.ThemeManager
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.getMenuDirectionForLocation
import org.mozilla.fenix.library.history.HistoryAction
import org.mozilla.fenix.library.history.HistoryItem
import org.mozilla.fenix.library.history.HistoryItemMenu
Expand Down Expand Up @@ -72,7 +72,7 @@ class HistoryListItemViewHolder(
menuButton.setOnClickListener {
historyMenu.menuBuilder.build(view.context).show(
anchor = it,
orientation = BrowserMenu.Orientation.DOWN)
orientation = it.getMenuDirectionForLocation())
}
}

Expand Down

0 comments on commit 6d3c300

Please sign in to comment.