diff --git a/app/src/main/java/rocks/vale/webview/MainActivity.kt b/app/src/main/java/rocks/vale/webview/MainActivity.kt index 2bbbd9c..d252786 100755 --- a/app/src/main/java/rocks/vale/webview/MainActivity.kt +++ b/app/src/main/java/rocks/vale/webview/MainActivity.kt @@ -19,20 +19,22 @@ import android.webkit.WebResourceRequest import android.webkit.WebSettings import android.webkit.WebView import android.webkit.WebViewClient -import android.widget.PopupMenu import androidx.activity.OnBackPressedCallback -import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.widget.PopupMenu import androidx.core.net.toUri import androidx.webkit.WebSettingsCompat import androidx.webkit.WebViewCompat import androidx.webkit.WebViewFeature import androidx.webkit.UserAgentMetadata +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.snackbar.Snackbar import rocks.vale.webview.about.AboutActivity import rocks.vale.webview.config.BrowserConfig import rocks.vale.webview.config.CacheMode +import rocks.vale.webview.config.ChromeItem import rocks.vale.webview.config.ConfigStore +import rocks.vale.webview.config.ToolbarPosition import rocks.vale.webview.console.ConsoleEntry import rocks.vale.webview.console.ConsoleLevel import rocks.vale.webview.console.ConsoleLog @@ -61,6 +63,12 @@ class MainActivity : AppCompatActivity(), BrowserChromeView.Listener { */ private var appliedConfig: BrowserConfig? = null + /** The toolbar position already applied, so the root is only reordered when it changes. */ + private var appliedPosition: ToolbarPosition? = null + + /** The toolbar layout already applied, so the row is only rebuilt when it changes. */ + private var appliedItems: List? = null + private var isLoading = false private var lastFailedUrl: String? = null @@ -154,11 +162,35 @@ class MainActivity : AppCompatActivity(), BrowserChromeView.Listener { setAcceptCookie(!config.blockCookies) } + if (config.chromeItems != appliedItems) { + appliedItems = config.chromeItems + binding.browserChrome.setItems(config.chromeItems) + } + binding.browserChrome.setHomeEnabled(config.homepage.isNotEmpty()) + applyChromePosition(config.chromePosition) + if (previous != null && previous != config) { webView.reload() } } + /** Moves the chrome above or below the page by reordering the activity's root. */ + private fun applyChromePosition(position: ToolbarPosition) { + if (position == appliedPosition) return + appliedPosition = position + + val chrome = binding.browserChrome + val dockedAtBottom = position == ToolbarPosition.BOTTOM + + binding.root.removeView(chrome) + if (dockedAtBottom) { + binding.root.addView(chrome) + } else { + binding.root.addView(chrome, 0) + } + chrome.setDockedAtBottom(dockedAtBottom) + } + @SuppressLint("SetJavaScriptEnabled") @Suppress("DEPRECATION") private fun applyWebSettings(config: BrowserConfig) { @@ -279,6 +311,14 @@ class MainActivity : AppCompatActivity(), BrowserChromeView.Listener { override fun onMoreClicked(anchor: View) { PopupMenu(this, anchor).apply { menuInflater.inflate(R.menu.menu_browser, menu) + + // Actions shown on the toolbar drop out of the menu, so nothing is offered twice. + val items = currentConfig.chromeItems + menu.findItem(R.id.action_home)?.isVisible = ChromeItem.HOME !in items + menu.findItem(R.id.action_console)?.isVisible = ChromeItem.CONSOLE !in items + menu.findItem(R.id.action_settings)?.isVisible = ChromeItem.SETTINGS !in items + menu.findItem(R.id.action_webview_info)?.isVisible = ChromeItem.WEBVIEW_INFO !in items + menu.findItem(R.id.action_home)?.isEnabled = currentConfig.homepage.isNotEmpty() val errors = consoleLog.errorCount menu.findItem(R.id.action_console)?.title = if (errors > 0) { @@ -286,24 +326,36 @@ class MainActivity : AppCompatActivity(), BrowserChromeView.Listener { } else { getString(R.string.menu_console) } + setOnMenuItemClickListener { onMenuItemClicked(it.itemId) } show() } } + override fun onActionClicked(item: ChromeItem) { + when (item) { + ChromeItem.HOME -> openHome() + ChromeItem.CONSOLE -> openConsole() + ChromeItem.SETTINGS -> openSettings() + ChromeItem.WEBVIEW_INFO -> showWebViewInfo() + // The remaining slots have their own listeners in the chrome view. + ChromeItem.BACK, ChromeItem.FORWARD, ChromeItem.URL_BAR, ChromeItem.RELOAD -> Unit + } + } + private fun onMenuItemClicked(itemId: Int): Boolean = when (itemId) { R.id.action_home -> { - webView.loadUrl(currentConfig.homepage) + openHome() true } R.id.action_console -> { - ConsoleSheet.show(this, consoleLog) + openConsole() true } R.id.action_settings -> { - startActivity(Intent(this, SettingsActivity::class.java)) + openSettings() true } @@ -320,6 +372,20 @@ class MainActivity : AppCompatActivity(), BrowserChromeView.Listener { else -> false } + /** Shared by the toolbar button and the menu entry. */ + private fun openHome() { + val homepage = currentConfig.homepage + if (homepage.isNotEmpty()) webView.loadUrl(homepage) + } + + private fun openConsole() { + ConsoleSheet.show(this, consoleLog) + } + + private fun openSettings() { + startActivity(Intent(this, SettingsActivity::class.java)) + } + private val browserClient = object : WebViewClient() { override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) { @@ -449,7 +515,7 @@ class MainActivity : AppCompatActivity(), BrowserChromeView.Listener { val provider = WebViewCompat.getCurrentWebViewPackage(this) val unavailable = getString(R.string.webview_info_unavailable) - AlertDialog.Builder(this) + MaterialAlertDialogBuilder(this) .setTitle(R.string.menu_webview_info) .setMessage( getString( diff --git a/app/src/main/java/rocks/vale/webview/config/BrowserConfig.kt b/app/src/main/java/rocks/vale/webview/config/BrowserConfig.kt index 2af0ecf..ab1911a 100755 --- a/app/src/main/java/rocks/vale/webview/config/BrowserConfig.kt +++ b/app/src/main/java/rocks/vale/webview/config/BrowserConfig.kt @@ -26,6 +26,8 @@ data class BrowserConfig( val textEncoding: String, val algorithmicDarkening: Boolean, val colorScheme: ColorScheme, + val chromeItems: List, + val chromePosition: ToolbarPosition, val cacheMode: CacheMode, val thirdPartyCookies: Boolean, val blockCookies: Boolean, diff --git a/app/src/main/java/rocks/vale/webview/config/ChromeItem.kt b/app/src/main/java/rocks/vale/webview/config/ChromeItem.kt new file mode 100644 index 0000000..9185fb0 --- /dev/null +++ b/app/src/main/java/rocks/vale/webview/config/ChromeItem.kt @@ -0,0 +1,38 @@ +package rocks.vale.webview.config + +/** + * A slot on the browser chrome. Every slot can be shown, hidden and reordered, so there are no + * fixed controls. The overflow menu button is deliberately excluded so the menu stays reachable. + */ +enum class ChromeItem { + BACK, + FORWARD, + URL_BAR, + RELOAD, + HOME, + CONSOLE, + SETTINGS, + WEBVIEW_INFO, + ; + + companion object { + /** The chrome as it ships: back, forward, address bar and reload. */ + val DEFAULT: List = listOf(BACK, FORWARD, URL_BAR, RELOAD) + + private const val SEPARATOR = "," + + fun fromKey(key: String?): ChromeItem? = values().firstOrNull { it.name == key } + + /** + * Parses a stored, comma-separated list. A missing value means "never configured" and gives + * [DEFAULT]; an empty value means the user turned everything off and gives an empty list. + */ + fun parse(keys: String?): List { + if (keys == null) return DEFAULT + return keys.split(SEPARATOR).mapNotNull { fromKey(it.trim()) }.distinct() + } + + /** The inverse of [parse]. */ + fun join(items: List): String = items.joinToString(SEPARATOR) { it.name } + } +} diff --git a/app/src/main/java/rocks/vale/webview/config/ConfigKeys.kt b/app/src/main/java/rocks/vale/webview/config/ConfigKeys.kt index 9ccc259..a8ee076 100755 --- a/app/src/main/java/rocks/vale/webview/config/ConfigKeys.kt +++ b/app/src/main/java/rocks/vale/webview/config/ConfigKeys.kt @@ -31,6 +31,10 @@ object ConfigKeys { const val ALGORITHMIC_DARKENING = "algorithmic_darkening" const val COLOR_SCHEME = "color_scheme" + // Browser chrome + const val CHROME_ITEMS = "chrome_items" + const val CHROME_POSITION = "chrome_position" + // Privacy and security const val THIRD_PARTY_COOKIES = "third_party_cookies" const val BLOCK_COOKIES = "block_cookies" diff --git a/app/src/main/java/rocks/vale/webview/config/ConfigStore.kt b/app/src/main/java/rocks/vale/webview/config/ConfigStore.kt index 166d3b6..2dc8f80 100755 --- a/app/src/main/java/rocks/vale/webview/config/ConfigStore.kt +++ b/app/src/main/java/rocks/vale/webview/config/ConfigStore.kt @@ -51,6 +51,10 @@ class ConfigStore(context: Context) { ).orEmpty(), algorithmicDarkening = preferences.getBoolean(ConfigKeys.ALGORITHMIC_DARKENING, false), colorScheme = ColorScheme.fromKey(preferences.getString(ConfigKeys.COLOR_SCHEME, null)), + chromeItems = ChromeItem.parse(preferences.getString(ConfigKeys.CHROME_ITEMS, null)), + chromePosition = ToolbarPosition.fromKey( + preferences.getString(ConfigKeys.CHROME_POSITION, null), + ), cacheMode = CacheMode.fromKey(preferences.getString(ConfigKeys.CACHE_MODE, null)), thirdPartyCookies = preferences.getBoolean(ConfigKeys.THIRD_PARTY_COOKIES, true), blockCookies = preferences.getBoolean(ConfigKeys.BLOCK_COOKIES, false), @@ -71,6 +75,16 @@ class ConfigStore(context: Context) { preferences.edit { clear() } } + /** Persists the toolbar layout chosen on the console's customisation dialog. */ + fun saveChromeItems(items: List) { + preferences.edit { putString(ConfigKeys.CHROME_ITEMS, ChromeItem.join(items)) } + } + + /** Writes a plain string option; used by the Material choice rows on the settings screen. */ + fun saveValue(key: String, value: String) { + preferences.edit { putString(key, value) } + } + /** Blank means 'not set', so a cleared search URL cannot leave the URL bar broken. */ private fun SharedPreferences.stringOrDefault(key: String, default: String): String = getString(key, null)?.takeIf { it.isNotBlank() } ?: default diff --git a/app/src/main/java/rocks/vale/webview/config/ToolbarPosition.kt b/app/src/main/java/rocks/vale/webview/config/ToolbarPosition.kt new file mode 100644 index 0000000..5c6f72c --- /dev/null +++ b/app/src/main/java/rocks/vale/webview/config/ToolbarPosition.kt @@ -0,0 +1,14 @@ +package rocks.vale.webview.config + +/** Where the browser chrome is docked relative to the page. */ +enum class ToolbarPosition { + TOP, + BOTTOM, + ; + + companion object { + /** Falls back to [TOP] so an unknown or missing stored value cannot break startup. */ + fun fromKey(key: String?): ToolbarPosition = + values().firstOrNull { it.name == key } ?: TOP + } +} diff --git a/app/src/main/java/rocks/vale/webview/console/ConsoleSheet.kt b/app/src/main/java/rocks/vale/webview/console/ConsoleSheet.kt index 6e11a6f..3bccbba 100644 --- a/app/src/main/java/rocks/vale/webview/console/ConsoleSheet.kt +++ b/app/src/main/java/rocks/vale/webview/console/ConsoleSheet.kt @@ -2,8 +2,11 @@ package rocks.vale.webview.console import android.content.Context import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup import androidx.core.view.isVisible import androidx.recyclerview.widget.LinearLayoutManager +import com.google.android.material.R as MaterialR import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -65,6 +68,12 @@ object ConsoleSheet { val dialog = BottomSheetDialog(context) dialog.setContentView(binding.root) + // The sheet view must be full height; otherwise the content is measured against whatever the wrapping layout hands it, and the list ends up clipped to ~half. + dialog.findViewById(MaterialR.id.design_bottom_sheet)?.let { sheet -> + sheet.layoutParams = sheet.layoutParams.apply { + height = ViewGroup.LayoutParams.MATCH_PARENT + } + } dialog.behavior.apply { // Opens as a half-height panel and can be dragged up to fill the screen. isFitToContents = false diff --git a/app/src/main/java/rocks/vale/webview/settings/BrowserSettingsFragment.kt b/app/src/main/java/rocks/vale/webview/settings/BrowserSettingsFragment.kt index 6e1f933..c05d595 100755 --- a/app/src/main/java/rocks/vale/webview/settings/BrowserSettingsFragment.kt +++ b/app/src/main/java/rocks/vale/webview/settings/BrowserSettingsFragment.kt @@ -1,24 +1,30 @@ package rocks.vale.webview.settings import android.os.Bundle +import android.text.InputType +import android.view.WindowManager import android.webkit.CookieManager import android.webkit.WebStorage import android.webkit.WebView +import androidx.annotation.ArrayRes import androidx.annotation.StringRes -import androidx.appcompat.app.AlertDialog import androidx.preference.Preference -import androidx.preference.ListPreference import androidx.preference.PreferenceCategory import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceGroup import androidx.preference.SeekBarPreference import androidx.webkit.WebViewFeature +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.snackbar.Snackbar import rocks.vale.webview.R import rocks.vale.webview.config.BrowserConfig +import rocks.vale.webview.config.ChromeItem import rocks.vale.webview.config.ColorScheme import rocks.vale.webview.config.ConfigKeys import rocks.vale.webview.config.ConfigStore +import rocks.vale.webview.databinding.DialogTextInputBinding +import rocks.vale.webview.databinding.DialogToolbarItemsBinding +import rocks.vale.webview.databinding.ItemToolbarEditBinding import rocks.vale.webview.util.UserAgent import rocks.vale.webview.util.WebViewSupport import rocks.vale.webview.util.applyToAppTheme @@ -49,11 +55,63 @@ class BrowserSettingsFragment : PreferenceFragmentCompat() { confirmReset() true } - // Applied straight away so the activity is recreated with the new theme (and the matching - // prefers-color-scheme) rather than waiting for the next launch. - findPreference(ConfigKeys.COLOR_SCHEME)?.setOnPreferenceChangeListener { _, value -> - ColorScheme.fromKey(value as? String).applyToAppTheme() - true + // Choice rows are shown with a Material dialog rather than the preference framework's own. + bindChoicePreference( + key = ConfigKeys.COLOR_SCHEME, + titleRes = R.string.settings_color_scheme_title, + entriesRes = R.array.color_scheme_entries, + valuesRes = R.array.color_scheme_values, + readValue = { configStore.load().colorScheme.name }, + ) { value -> + configStore.saveValue(ConfigKeys.COLOR_SCHEME, value) + ColorScheme.fromKey(value).applyToAppTheme() + } + bindChoicePreference( + key = ConfigKeys.CACHE_MODE, + titleRes = R.string.settings_cache_mode_title, + entriesRes = R.array.cache_mode_entries, + valuesRes = R.array.cache_mode_values, + readValue = { configStore.load().cacheMode.name }, + ) { value -> configStore.saveValue(ConfigKeys.CACHE_MODE, value) } + bindChoicePreference( + key = ConfigKeys.CHROME_POSITION, + titleRes = R.string.settings_chrome_position_title, + entriesRes = R.array.chrome_position_entries, + valuesRes = R.array.chrome_position_values, + readValue = { configStore.load().chromePosition.name }, + ) { value -> configStore.saveValue(ConfigKeys.CHROME_POSITION, value) } + + bindTextPreference( + key = ConfigKeys.HOMEPAGE, + titleRes = R.string.settings_homepage_title, + inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_URI, + readValue = { configStore.load().homepage }, + ) { value -> configStore.saveValue(ConfigKeys.HOMEPAGE, value) } + bindTextPreference( + key = ConfigKeys.SEARCH_URL_TEMPLATE, + titleRes = R.string.settings_search_url_title, + inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_URI, + readValue = { configStore.load().searchUrlTemplate }, + ) { value -> configStore.saveValue(ConfigKeys.SEARCH_URL_TEMPLATE, value) } + bindTextPreference( + key = ConfigKeys.USER_AGENT, + titleRes = R.string.settings_user_agent_title, + inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS, + readValue = { configStore.load().userAgentOverride }, + ) { value -> configStore.saveValue(ConfigKeys.USER_AGENT, value) } + bindTextPreference( + key = ConfigKeys.DEFAULT_TEXT_ENCODING, + titleRes = R.string.settings_text_encoding_title, + inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS, + readValue = { configStore.load().textEncoding }, + ) { value -> configStore.saveValue(ConfigKeys.DEFAULT_TEXT_ENCODING, value) } + + findPreference(ConfigKeys.CHROME_ITEMS)?.apply { + setOnPreferenceClickListener { + showToolbarDialog() + true + } + summary = chromeItemsSummary() } // Bounds set here because android:min and android:max are ignored before API 26. findPreference(ConfigKeys.TEXT_ZOOM)?.apply { @@ -71,9 +129,155 @@ class BrowserSettingsFragment : PreferenceFragmentCompat() { } /** - * Removes rows whose backing WebView API is unavailable, so the screen never offers a switch that - * silently does nothing. The matching options are still read from and written to storage; only - * their UI is withheld. + * Wires a row to a single-choice Material dialog. + */ + private fun bindChoicePreference( + key: String, + @StringRes titleRes: Int, + @ArrayRes entriesRes: Int, + @ArrayRes valuesRes: Int, + readValue: () -> String, + onChosen: (String) -> Unit, + ) { + val preference = findPreference(key) ?: return + val entries = resources.getStringArray(entriesRes) + val values = resources.getStringArray(valuesRes) + + fun refreshSummary() { + preference.summary = entries.getOrNull(values.indexOf(readValue())) + ?: entries.firstOrNull().orEmpty() + } + refreshSummary() + + preference.setOnPreferenceClickListener { + MaterialAlertDialogBuilder(requireContext()) + .setTitle(titleRes) + .setSingleChoiceItems(entries, values.indexOf(readValue()).coerceAtLeast(0)) { dialog, which -> + onChosen(values[which]) + refreshSummary() + dialog.dismiss() + } + .setNegativeButton(android.R.string.cancel, null) + .show() + true + } + } + + /** Wires a row to a Material text-entry dialog. */ + private fun bindTextPreference( + key: String, + @StringRes titleRes: Int, + inputType: Int, + readValue: () -> String, + onChosen: (String) -> Unit, + ) { + findPreference(key)?.setOnPreferenceClickListener { + showTextDialog(titleRes, inputType, readValue(), onChosen) + true + } + } + + private fun showTextDialog( + @StringRes titleRes: Int, + inputType: Int, + current: String, + onChosen: (String) -> Unit, + ) { + val binding = DialogTextInputBinding.inflate(layoutInflater) + // No hint: the dialog title already names the field. + binding.textInput.inputType = inputType + binding.textInput.setText(current) + binding.textInput.setSelection(current.length) + + val dialog = MaterialAlertDialogBuilder(requireContext()) + .setTitle(titleRes) + .setView(binding.root) + .setNegativeButton(android.R.string.cancel, null) + .setPositiveButton(android.R.string.ok) { _, _ -> + onChosen(binding.textInput.text?.toString().orEmpty()) + } + .create() + + dialog.setOnShowListener { + binding.textInput.requestFocus() + dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) + } + dialog.show() + } + + /** + * Lets the user include, exclude and reorder toolbar slots. + */ + private fun showToolbarDialog() { + val current = configStore.load().chromeItems + val ordered = (current + ChromeItem.values().toList()).distinct().toMutableList() + val enabled = current.toMutableSet() + val binding = DialogToolbarItemsBinding.inflate(layoutInflater) + + fun rebuild() { + binding.toolbarItems.removeAllViews() + ordered.forEachIndexed { index, item -> + val row = ItemToolbarEditBinding.inflate(layoutInflater, binding.toolbarItems, false) + row.itemLabel.setText(item.labelRes()) + + row.itemCheck.isChecked = item in enabled + row.itemCheck.setOnCheckedChangeListener { _, isChecked -> + if (isChecked) enabled.add(item) else enabled.remove(item) + } + + row.itemUp.isEnabled = index > 0 + row.itemUp.setOnClickListener { moveItem(ordered, index, index - 1, ::rebuild) } + + row.itemDown.isEnabled = index < ordered.lastIndex + row.itemDown.setOnClickListener { moveItem(ordered, index, index + 1, ::rebuild) } + + binding.toolbarItems.addView(row.root) + } + } + rebuild() + + MaterialAlertDialogBuilder(requireContext()) + .setTitle(R.string.settings_chrome_buttons_title) + .setView(binding.root) + .setNegativeButton(android.R.string.cancel, null) + .setPositiveButton(android.R.string.ok) { _, _ -> + configStore.saveChromeItems(ordered.filter { it in enabled }) + findPreference(ConfigKeys.CHROME_ITEMS)?.summary = chromeItemsSummary() + } + .show() + } + + private fun moveItem( + items: MutableList, + from: Int, + to: Int, + onMoved: () -> Unit, + ) { + if (to !in items.indices) return + items.add(to, items.removeAt(from)) + onMoved() + } + + private fun chromeItemsSummary(): String { + val items = configStore.load().chromeItems + if (items.isEmpty()) return getString(R.string.settings_chrome_items_none) + return items.joinToString(", ") { getString(it.labelRes()) } + } + + private fun ChromeItem.labelRes(): Int = when (this) { + ChromeItem.BACK -> R.string.action_back + ChromeItem.FORWARD -> R.string.action_forward + ChromeItem.URL_BAR -> R.string.settings_chrome_item_address_bar + ChromeItem.RELOAD -> R.string.action_reload + ChromeItem.HOME -> R.string.menu_home + ChromeItem.CONSOLE -> R.string.menu_console + ChromeItem.SETTINGS -> R.string.menu_settings + ChromeItem.WEBVIEW_INFO -> R.string.menu_webview_info + } + + /** + * Removes rows whose backing WebView API is unavailable, so the screen never offers a switch that silently does nothing. + * Mtching options are still read from and written to storage – only their UI is withheld. */ private fun pruneUnsupportedPreferences() { if (!WebViewFeature.isFeatureSupported(WebViewFeature.ALGORITHMIC_DARKENING)) { @@ -116,7 +320,7 @@ class BrowserSettingsFragment : PreferenceFragmentCompat() { } private fun confirmClearData() { - AlertDialog.Builder(requireContext()) + MaterialAlertDialogBuilder(requireContext()) .setTitle(R.string.settings_clear_data_confirm_title) .setMessage(R.string.settings_clear_data_confirm_message) .setNegativeButton(android.R.string.cancel, null) @@ -143,7 +347,7 @@ class BrowserSettingsFragment : PreferenceFragmentCompat() { } private fun confirmReset() { - AlertDialog.Builder(requireContext()) + MaterialAlertDialogBuilder(requireContext()) .setTitle(R.string.settings_reset_confirm_title) .setNegativeButton(android.R.string.cancel, null) .setPositiveButton(R.string.settings_reset_confirm) { _, _ -> resetSettings() } diff --git a/app/src/main/java/rocks/vale/webview/ui/BrowserChromeView.kt b/app/src/main/java/rocks/vale/webview/ui/BrowserChromeView.kt index c4bc56c..003f4c7 100755 --- a/app/src/main/java/rocks/vale/webview/ui/BrowserChromeView.kt +++ b/app/src/main/java/rocks/vale/webview/ui/BrowserChromeView.kt @@ -7,7 +7,9 @@ import android.view.LayoutInflater import android.view.View import android.view.inputmethod.EditorInfo import android.widget.LinearLayout +import androidx.core.view.isVisible import rocks.vale.webview.R +import rocks.vale.webview.config.ChromeItem import rocks.vale.webview.databinding.BrowserChromeBinding /** @@ -28,6 +30,9 @@ class BrowserChromeView @JvmOverloads constructor( fun onReloadOrStopClicked() fun onNavigate(query: String) fun onMoreClicked(anchor: View) + + /** An optional toolbar button was tapped; [item] says which. */ + fun onActionClicked(item: ChromeItem) } private val binding = BrowserChromeBinding.inflate(LayoutInflater.from(context), this) @@ -44,6 +49,13 @@ class BrowserChromeView @JvmOverloads constructor( binding.refreshButton.setOnClickListener { listener?.onReloadOrStopClicked() } binding.moreButton.setOnClickListener { listener?.onMoreClicked(it) } + binding.homeButton.setOnClickListener { listener?.onActionClicked(ChromeItem.HOME) } + binding.consoleButton.setOnClickListener { listener?.onActionClicked(ChromeItem.CONSOLE) } + binding.settingsButton.setOnClickListener { listener?.onActionClicked(ChromeItem.SETTINGS) } + binding.webviewInfoButton.setOnClickListener { + listener?.onActionClicked(ChromeItem.WEBVIEW_INFO) + } + binding.urlInput.setOnEditorActionListener { _, actionId, event -> if (isSubmitAction(actionId, event)) { listener?.onNavigate(binding.urlInput.text.toString()) @@ -57,6 +69,52 @@ class BrowserChromeView @JvmOverloads constructor( setLoading(loading = false) } + /** + * Lays the row out from [items], in order. + * The overflow button is always appended last so the menu cannot be lost. + */ + fun setItems(items: List) { + val row = binding.chromeRow + row.removeAllViews() + items.forEach { item -> + val view = viewFor(item) + view.visibility = VISIBLE + row.addView(view) + } + row.addView(binding.moreButton) + } + + private fun viewFor(item: ChromeItem): View = when (item) { + ChromeItem.BACK -> binding.backButton + ChromeItem.FORWARD -> binding.forwardButton + ChromeItem.URL_BAR -> binding.urlInput + ChromeItem.RELOAD -> binding.refreshButton + ChromeItem.HOME -> binding.homeButton + ChromeItem.CONSOLE -> binding.consoleButton + ChromeItem.SETTINGS -> binding.settingsButton + ChromeItem.WEBVIEW_INFO -> binding.webviewInfoButton + } + + /** The home button is dimmed when no homepage is set, matching the menu entry. */ + fun setHomeEnabled(enabled: Boolean) { + binding.homeButton.isEnabled = enabled + binding.homeButton.alpha = if (enabled) ENABLED_ALPHA else DISABLED_ALPHA + } + + /** + * Mirrors the progress bar and divider so the chrome reads correctly when docked below the page. + */ + fun setDockedAtBottom(dockedAtBottom: Boolean) { + val order = if (dockedAtBottom) { + listOf(binding.chromeDivider, binding.progress, binding.chromeRow) + } else { + listOf(binding.chromeRow, binding.progress, binding.chromeDivider) + } + + removeAllViews() + order.forEach { addView(it) } + } + /** Ignored while the user is editing the field, so typing is never overwritten. */ fun setUrl(url: String) { if (!binding.urlInput.hasFocus()) { diff --git a/app/src/main/res/drawable/ic_arrow_down.xml b/app/src/main/res/drawable/ic_arrow_down.xml new file mode 100644 index 0000000..f102c40 --- /dev/null +++ b/app/src/main/res/drawable/ic_arrow_down.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/src/main/res/drawable/ic_arrow_up.xml b/app/src/main/res/drawable/ic_arrow_up.xml new file mode 100644 index 0000000..dd0aded --- /dev/null +++ b/app/src/main/res/drawable/ic_arrow_up.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/src/main/res/drawable/ic_console.xml b/app/src/main/res/drawable/ic_console.xml new file mode 100644 index 0000000..16ced34 --- /dev/null +++ b/app/src/main/res/drawable/ic_console.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/src/main/res/drawable/ic_home.xml b/app/src/main/res/drawable/ic_home.xml new file mode 100644 index 0000000..ffe2bc6 --- /dev/null +++ b/app/src/main/res/drawable/ic_home.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/src/main/res/drawable/ic_info.xml b/app/src/main/res/drawable/ic_info.xml new file mode 100644 index 0000000..3fb3576 --- /dev/null +++ b/app/src/main/res/drawable/ic_info.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/src/main/res/drawable/ic_settings.xml b/app/src/main/res/drawable/ic_settings.xml new file mode 100644 index 0000000..b0261a6 --- /dev/null +++ b/app/src/main/res/drawable/ic_settings.xml @@ -0,0 +1,10 @@ + + + + diff --git a/app/src/main/res/layout/browser_chrome.xml b/app/src/main/res/layout/browser_chrome.xml index 61bf060..e60b486 100755 --- a/app/src/main/res/layout/browser_chrome.xml +++ b/app/src/main/res/layout/browser_chrome.xml @@ -8,6 +8,7 @@ tools:parentTag="android.widget.LinearLayout"> + + + + + + + + diff --git a/app/src/main/res/layout/console_sheet.xml b/app/src/main/res/layout/console_sheet.xml index 583fcda..da14f98 100644 --- a/app/src/main/res/layout/console_sheet.xml +++ b/app/src/main/res/layout/console_sheet.xml @@ -85,7 +85,7 @@ android:id="@+id/console_empty" android:layout_width="match_parent" android:layout_height="wrap_content" - android:layout_gravity="center" + android:layout_gravity="center_horizontal|top" android:gravity="center" android:padding="32dp" android:textAppearance="?attr/textAppearanceBodyMedium" diff --git a/app/src/main/res/layout/dialog_text_input.xml b/app/src/main/res/layout/dialog_text_input.xml new file mode 100644 index 0000000..10617ae --- /dev/null +++ b/app/src/main/res/layout/dialog_text_input.xml @@ -0,0 +1,28 @@ + + + + + + + + diff --git a/app/src/main/res/layout/dialog_toolbar_items.xml b/app/src/main/res/layout/dialog_toolbar_items.xml new file mode 100644 index 0000000..59efe2f --- /dev/null +++ b/app/src/main/res/layout/dialog_toolbar_items.xml @@ -0,0 +1,13 @@ + + + + + diff --git a/app/src/main/res/layout/item_toolbar_edit.xml b/app/src/main/res/layout/item_toolbar_edit.xml new file mode 100644 index 0000000..75dbd94 --- /dev/null +++ b/app/src/main/res/layout/item_toolbar_edit.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index 7720c2e..bfe805f 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -27,4 +27,15 @@ LIGHT DARK + + + + @string/settings_chrome_position_top + @string/settings_chrome_position_bottom + + + + TOP + BOTTOM + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0226e90..7da12c2 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -59,10 +59,11 @@ General Page content Display + Browser chrome Privacy and security Advanced - Homepage + Homepage URL Leave blank to open on an empty page. Search URL Use %s as the placeholder for the search query. @@ -127,6 +128,16 @@ Text encoding Encoding used for pages that do not declare one. + + Toolbar buttons + None + Address bar + Move up + Move down + Toolbar position + Top + Bottom + Block all cookies Refuse cookies from every site. diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index b53670e..d1e9040 100755 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -45,8 +45,8 @@ @color/surface @bool/window_light_status_bar - - @style/PreferenceThemeOverlay + + @style/PreferenceThemeOverlay.v14.Material diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index a794ad3..0b27514 100755 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -11,16 +11,12 @@ - - @@ -69,13 +65,9 @@ - + app:title="@string/settings_color_scheme_title" /> - @@ -138,15 +128,25 @@ app:summary="@string/settings_wide_viewport_summary" app:title="@string/settings_wide_viewport_title" /> - + + + + + + + + - + app:title="@string/settings_cache_mode_title" /> (), ChromeItem.parse("")) + } + + @Test + fun joinRoundTripsWithParse() { + val items = listOf(ChromeItem.BACK, ChromeItem.CONSOLE, ChromeItem.RELOAD) + assertEquals(items, ChromeItem.parse(ChromeItem.join(items))) + } + + @Test + fun toolbarPositionFallsBackToTop() { + assertEquals(ToolbarPosition.BOTTOM, ToolbarPosition.fromKey("BOTTOM")) + assertEquals(ToolbarPosition.TOP, ToolbarPosition.fromKey("TOP")) + assertEquals(ToolbarPosition.TOP, ToolbarPosition.fromKey(null)) + } +}