|
5 | 5 | package mozilla.components.browser.toolbar
|
6 | 6 |
|
7 | 7 | import android.content.Context
|
| 8 | +import android.support.annotation.VisibleForTesting |
8 | 9 | import android.util.AttributeSet
|
9 |
| -import android.view.inputmethod.EditorInfo |
10 |
| -import android.widget.EditText |
11 |
| -import android.widget.FrameLayout |
| 10 | +import android.view.View |
| 11 | +import android.view.ViewGroup |
| 12 | +import mozilla.components.browser.toolbar.display.DisplayToolbar |
| 13 | +import mozilla.components.browser.toolbar.edit.EditToolbar |
12 | 14 | import mozilla.components.concept.toolbar.Toolbar
|
| 15 | +import mozilla.components.support.ktx.android.view.dp |
| 16 | +import mozilla.components.support.ktx.android.view.forEach |
13 | 17 |
|
14 | 18 | /**
|
15 | 19 | * A customizable toolbar for browsers.
|
| 20 | + * |
| 21 | + * The toolbar can switch between two modes: display and edit. The display mode displays the current |
| 22 | + * URL and controls for navigation. In edit mode the current URL can be edited. Those two modes are |
| 23 | + * implemented by the DisplayToolbar and EditToolbar classes. |
| 24 | + * |
| 25 | + * +----------------+ |
| 26 | + * | BrowserToolbar | |
| 27 | + * +--------+-------+ |
| 28 | + * + |
| 29 | + * +-------+-------+ |
| 30 | + * | | |
| 31 | + * +---------v------+ +-------v--------+ |
| 32 | + * | DisplayToolbar | | EditToolbar | |
| 33 | + * +----------------+ +----------------+ |
| 34 | + * |
16 | 35 | */
|
17 | 36 | class BrowserToolbar @JvmOverloads constructor(
|
18 | 37 | context: Context,
|
19 | 38 | attrs: AttributeSet? = null,
|
20 | 39 | defStyleAttr: Int = 0
|
21 |
| -) : FrameLayout(context, attrs, defStyleAttr), Toolbar { |
| 40 | +) : ViewGroup(context, attrs, defStyleAttr), Toolbar { |
| 41 | + |
| 42 | + // displayToolbar and editToolbar are only visible internally and mutable so that we can mock |
| 43 | + // them in tests. |
| 44 | + @VisibleForTesting internal var displayToolbar = DisplayToolbar(context, this) |
| 45 | + @VisibleForTesting internal var editToolbar = EditToolbar(context, this) |
| 46 | + |
| 47 | + private var state: State = State.DISPLAY |
| 48 | + private var url: String = "" |
| 49 | + private var listener: ((String) -> Unit)? = null |
| 50 | + |
| 51 | + init { |
| 52 | + addView(displayToolbar) |
| 53 | + addView(editToolbar) |
| 54 | + |
| 55 | + updateState(State.DISPLAY) |
| 56 | + } |
| 57 | + |
| 58 | + // We layout the toolbar ourselves to avoid the overhead from using complex ViewGroup implementations |
| 59 | + override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { |
| 60 | + forEach { child -> |
| 61 | + child.layout(left, top, right, bottom) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // We measure the views manually to avoid overhead by using complex ViewGroup implementations |
| 66 | + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { |
| 67 | + // Our toolbar will always use the full width and a fixed height |
| 68 | + val width = MeasureSpec.getSize(widthMeasureSpec) |
| 69 | + val height = dp(TOOLBAR_HEIGHT_DP) |
| 70 | + |
| 71 | + setMeasuredDimension(width, height) |
| 72 | + |
| 73 | + // Let the children measure themselves using our fixed size |
| 74 | + val childWidthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY) |
| 75 | + val childHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY) |
| 76 | + |
| 77 | + forEach { child -> child.measure(childWidthSpec, childHeightSpec) } |
| 78 | + } |
| 79 | + |
| 80 | + override fun onBackPressed(): Boolean { |
| 81 | + if (state == State.EDIT) { |
| 82 | + displayMode() |
| 83 | + return true |
| 84 | + } |
| 85 | + return false |
| 86 | + } |
22 | 87 |
|
23 | 88 | override fun displayUrl(url: String) {
|
24 |
| - val urlView = getUrlViewComponent() |
25 |
| - urlView.setText(url) |
26 |
| - urlView.setSelection(0, url.length) |
| 89 | + // We update the display toolbar immediately. We do not do that for the edit toolbar to not |
| 90 | + // mess with what the user is entering. Instead we will remember the value and update the |
| 91 | + // edit toolbar whenever we switch to it. |
| 92 | + displayToolbar.updateUrl(url) |
| 93 | + |
| 94 | + this.url = url |
| 95 | + } |
| 96 | + |
| 97 | + override fun displayProgress(progress: Int) { |
| 98 | + displayToolbar.updateProgress(progress) |
27 | 99 | }
|
28 | 100 |
|
29 | 101 | override fun setOnUrlChangeListener(listener: (String) -> Unit) {
|
30 |
| - val urlView = getUrlViewComponent() |
31 |
| - urlView.setOnEditorActionListener { _, actionId, _ -> |
32 |
| - if (actionId == EditorInfo.IME_ACTION_GO) { |
33 |
| - listener.invoke(urlView.text.toString()) |
34 |
| - } |
35 |
| - true |
| 102 | + this.listener = listener |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Switches to URL editing mode. |
| 107 | + */ |
| 108 | + fun editMode() { |
| 109 | + editToolbar.updateUrl(url) |
| 110 | + |
| 111 | + updateState(State.EDIT) |
| 112 | + |
| 113 | + editToolbar.focus() |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Switches to URL displaying mode. |
| 118 | + */ |
| 119 | + fun displayMode() { |
| 120 | + updateState(State.DISPLAY) |
| 121 | + } |
| 122 | + |
| 123 | + internal fun onUrlEntered(url: String) { |
| 124 | + displayMode() |
| 125 | + |
| 126 | + listener?.invoke(url) |
| 127 | + } |
| 128 | + |
| 129 | + private fun updateState(state: State) { |
| 130 | + this.state = state |
| 131 | + |
| 132 | + val (show, hide) = when (state) { |
| 133 | + State.DISPLAY -> Pair(displayToolbar, editToolbar) |
| 134 | + State.EDIT -> Pair(editToolbar, displayToolbar) |
36 | 135 | }
|
| 136 | + |
| 137 | + show.visibility = View.VISIBLE |
| 138 | + hide.visibility = View.GONE |
| 139 | + } |
| 140 | + |
| 141 | + private enum class State { |
| 142 | + DISPLAY, |
| 143 | + EDIT |
37 | 144 | }
|
38 | 145 |
|
39 |
| - internal fun getUrlViewComponent(): EditText { |
40 |
| - return this.findViewById(R.id.urlView) |
| 146 | + companion object { |
| 147 | + private const val TOOLBAR_HEIGHT_DP = 56 |
41 | 148 | }
|
42 | 149 | }
|
0 commit comments