Adds toolbar with autocomplete and menu to BrowserFragment
parent
6f17107e87
commit
5a3fc1325f
@ -0,0 +1,106 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.components.toolbar
|
||||
|
||||
import android.content.Context
|
||||
import mozilla.components.browser.domains.autocomplete.ShippedDomainsProvider
|
||||
import mozilla.components.browser.menu.BrowserMenuBuilder
|
||||
import mozilla.components.browser.menu.item.BrowserMenuItemToolbar
|
||||
import mozilla.components.browser.menu.item.BrowserMenuSwitch
|
||||
import mozilla.components.browser.menu.item.SimpleBrowserMenuItem
|
||||
import mozilla.components.browser.session.SessionManager
|
||||
import mozilla.components.feature.session.SessionUseCases
|
||||
import org.mozilla.fenix.R
|
||||
|
||||
/**
|
||||
* Component group for all functionality related to the browser toolbar.
|
||||
*/
|
||||
class Toolbar(
|
||||
private val context: Context,
|
||||
private val sessionUseCases: SessionUseCases,
|
||||
private val sessionManager: SessionManager
|
||||
) {
|
||||
|
||||
/**
|
||||
* Helper class for building browser menus.app
|
||||
*/
|
||||
val menuBuilder by lazy { BrowserMenuBuilder(menuItems) }
|
||||
|
||||
/**
|
||||
* Provides autocomplete functionality for shipped / provided domain lists.
|
||||
*/
|
||||
val shippedDomainsProvider by lazy {
|
||||
ShippedDomainsProvider().also { it.initialize(context) }
|
||||
}
|
||||
|
||||
private val menuToolbar by lazy {
|
||||
val back = BrowserMenuItemToolbar.Button(
|
||||
mozilla.components.ui.icons.R.drawable.mozac_ic_back,
|
||||
iconTintColorResource = R.color.icons,
|
||||
contentDescription = "Back"
|
||||
) {
|
||||
sessionUseCases.goBack.invoke()
|
||||
}
|
||||
|
||||
val forward = BrowserMenuItemToolbar.Button(
|
||||
mozilla.components.ui.icons.R.drawable.mozac_ic_forward,
|
||||
iconTintColorResource = R.color.icons,
|
||||
contentDescription = "Forward"
|
||||
) {
|
||||
sessionUseCases.goForward.invoke()
|
||||
}
|
||||
|
||||
val refresh = BrowserMenuItemToolbar.Button(
|
||||
mozilla.components.ui.icons.R.drawable.mozac_ic_refresh,
|
||||
iconTintColorResource = R.color.icons,
|
||||
contentDescription = "Refresh"
|
||||
) {
|
||||
sessionUseCases.reload.invoke()
|
||||
}
|
||||
|
||||
BrowserMenuItemToolbar(listOf(back, forward, refresh))
|
||||
}
|
||||
|
||||
private val menuItems by lazy {
|
||||
listOf(
|
||||
menuToolbar,
|
||||
SimpleBrowserMenuItem(context.getString(R.string.browser_menu_help)) {
|
||||
// TODO Help
|
||||
},
|
||||
|
||||
SimpleBrowserMenuItem(context.getString(R.string.browser_menu_settings)) {
|
||||
openSettingsActivity()
|
||||
},
|
||||
|
||||
SimpleBrowserMenuItem(context.getString(R.string.browser_menu_library)) {
|
||||
// TODO Your Library
|
||||
},
|
||||
|
||||
BrowserMenuSwitch(context.getString(R.string.browser_menu_desktop_site), {
|
||||
sessionManager.selectedSessionOrThrow.desktopMode
|
||||
}) { checked ->
|
||||
sessionUseCases.requestDesktopSite.invoke(checked)
|
||||
}.apply {
|
||||
visible = { sessionManager.selectedSession != null }
|
||||
},
|
||||
|
||||
SimpleBrowserMenuItem(context.getString(R.string.browser_menu_find_in_page)) {
|
||||
// TODO Find in Page
|
||||
},
|
||||
|
||||
SimpleBrowserMenuItem(context.getString(R.string.browser_menu_private_tab)) {
|
||||
// TODO Private Tab
|
||||
},
|
||||
|
||||
SimpleBrowserMenuItem(context.getString(R.string.browser_menu_new_tab)) {
|
||||
// TODO New Tab
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private fun openSettingsActivity() {
|
||||
// TODO Open Settings
|
||||
}
|
||||
}
|
@ -1,15 +1,59 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.ext
|
||||
|
||||
import android.content.ActivityNotFoundException
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.Intent.*
|
||||
import androidx.annotation.StringRes
|
||||
import mozilla.components.support.base.log.Log
|
||||
import mozilla.components.support.base.log.Log.Priority.WARN
|
||||
import org.mozilla.fenix.FenixApplication
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.components.Components
|
||||
|
||||
/**
|
||||
* Get the BrowserApplication object from a context.
|
||||
*/
|
||||
val Context.application: FenixApplication
|
||||
get() = applicationContext as FenixApplication
|
||||
|
||||
/**
|
||||
* Get the requireComponents of this application.
|
||||
*/
|
||||
val Context.components: Components
|
||||
get() = application.components
|
||||
get() = application.components
|
||||
|
||||
fun Context.getPreferenceKey(@StringRes resourceId: Int): String =
|
||||
resources.getString(resourceId)
|
||||
|
||||
/**
|
||||
* Shares content via [ACTION_SEND] intent.
|
||||
*
|
||||
* @param text the data to be shared [EXTRA_TEXT]
|
||||
* @param subject of the intent [EXTRA_TEXT]
|
||||
* @return true it is able to share false otherwise.
|
||||
*/
|
||||
fun Context.share(text: String, subject: String = ""): Boolean {
|
||||
return try {
|
||||
val intent = Intent(ACTION_SEND).apply {
|
||||
type = "text/plain"
|
||||
putExtra(EXTRA_SUBJECT, subject)
|
||||
putExtra(EXTRA_TEXT, text)
|
||||
flags = FLAG_ACTIVITY_NEW_TASK
|
||||
}
|
||||
|
||||
val shareIntent = Intent.createChooser(intent, getString(R.string.menu_share_with)).apply {
|
||||
flags = FLAG_ACTIVITY_NEW_TASK
|
||||
}
|
||||
|
||||
startActivity(shareIntent)
|
||||
true
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
Log.log(WARN, message = "No activity to share to found", throwable = e, tag = "Reference-Browser")
|
||||
false
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,13 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.ext
|
||||
|
||||
import androidx.fragment.app.Fragment
|
||||
import org.mozilla.fenix.components.Components
|
||||
|
||||
val Fragment.requireComponents: Components
|
||||
/**
|
||||
* Get the requireComponents of this application.
|
||||
*/
|
||||
val androidx.fragment.app.Fragment.requireComponents: Components
|
||||
get() = requireContext().components
|
@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="20"
|
||||
android:viewportHeight="20">
|
||||
<path
|
||||
android:fillColor="#0C0C0D"
|
||||
android:fillType="evenOdd"
|
||||
android:pathData="M19.457,9.043L10.707,0.293C10.3165,-0.0974 9.6835,-0.0974 9.293,0.293L0.543,9.043C0.164,9.4354 0.1694,10.0591 0.5552,10.4448C0.9409,10.8306 1.5646,10.836 1.957,10.457L3,9.414L3,17C3,18.6569 4.3431,20 6,20L14,20C15.6569,20 17,18.6569 17,17L17,9.414L18.043,10.457C18.4354,10.836 19.0591,10.8306 19.4448,10.4448C19.8306,10.0591 19.836,9.4354 19.457,9.043ZM15,17C15,17.5523 14.5523,18 14,18C14.6667,18 14.1667,18 12.5,18C10.8333,18 9.1667,18 7.5,18L6,18C5.4477,18 5,17.5523 5,17L5,7.414L10,2.414L15,7.414L15,17Z"
|
||||
android:strokeWidth="1" />
|
||||
</vector>
|
Loading…
Reference in New Issue