@ -22,6 +22,7 @@ import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.components
import java.lang.ref.WeakReference
import java.lang.ref.WeakReference
import mozilla.components.browser.state.selector.findCustomTab
import mozilla.components.browser.state.selector.findCustomTab
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.fenix.databinding.BrowserToolbarPopupWindowBinding
import org.mozilla.fenix.databinding.BrowserToolbarPopupWindowBinding
object ToolbarPopupWindow {
object ToolbarPopupWindow {
@ -34,7 +35,8 @@ object ToolbarPopupWindow {
) {
) {
val context = view . get ( ) ?. context ?: return
val context = view . get ( ) ?. context ?: return
val clipboard = context . components . clipboardHandler
val clipboard = context . components . clipboardHandler
if ( ! copyVisible && ! clipboard . containsURL ( ) ) return
val clipboardUrl = clipboard . getUrl ( )
if ( ! copyVisible && clipboardUrl == null ) return
val isCustomTabSession = customTabId != null
val isCustomTabSession = customTabId != null
@ -54,37 +56,41 @@ object ToolbarPopupWindow {
binding . copy . isVisible = copyVisible
binding . copy . isVisible = copyVisible
binding . paste . isVisible = clipboard . containsURL ( ) && !is CustomTabSession
val showPaste = clipboardUrl != null && !is CustomTabSession
binding . paste AndGo . isVisible =
binding . paste . isVisible = showPaste
clipboard . containsURL ( ) && !is CustomTabSession
binding . pasteAndGo . isVisible = showPaste
binding . copy . setOnClickListener {
if ( copyVisible ) {
popupWindow . dismiss ( )
binding . copy . setOnClickListener { copyView ->
clipboard . text = getUrlForClipboard (
popupWindow . dismiss ( )
it . context . components . core . store ,
clipboard . text = getUrlForClipboard (
customTabId
copyView . context . components . core . store ,
)
customTabId
view . get ( ) ?. let {
FenixSnackbar . make (
view = it ,
duration = Snackbar . LENGTH _SHORT ,
isDisplayedWithBrowserToolbar = true
)
)
. setText ( context . getString ( R . string . browser _toolbar _url _copied _to _clipboard _snackbar ) )
. show ( )
view . get ( ) ?. let { toolbarView ->
FenixSnackbar . make (
view = toolbarView ,
duration = Snackbar . LENGTH _SHORT ,
isDisplayedWithBrowserToolbar = true
)
. setText ( context . getString ( R . string . browser _toolbar _url _copied _to _clipboard _snackbar ) )
. show ( )
}
context . components . analytics . metrics . track ( Event . CopyUrlUsed )
}
}
context . components . analytics . metrics . track ( Event . CopyUrlUsed )
}
}
binding . paste . setOnClickListener {
clipboardUrl ?. let { url ->
popupWindow . dismiss ( )
binding . paste . setOnClickListener {
handlePaste ( clipboard . text !! )
popupWindow . dismiss ( )
}
handlePaste ( url )
}
binding . pasteAndGo . setOnClickListener {
binding . pasteAndGo . setOnClickListener {
popupWindow . dismiss ( )
popupWindow . dismiss ( )
handlePasteAndGo ( clipboard . text !! )
handlePasteAndGo ( url )
}
}
}
view . get ( ) ?. let {
view . get ( ) ?. let {
@ -110,4 +116,12 @@ object ToolbarPopupWindow {
selectedTab ?. readerState ?. activeUrl ?: selectedTab ?. content ?. url
selectedTab ?. readerState ?. activeUrl ?: selectedTab ?. content ?. url
}
}
}
}
private fun ClipboardHandler . getUrl ( ) : String ? {
if ( containsURL ( ) ) {
text ?. let { return it }
Logger ( " ToolbarPopupWindow " ) . error ( " Clipboard contains URL but unable to read text " )
}
return null
}
}
}