Commit 1b1600a7 authored by Sawyer Blatz's avatar Sawyer Blatz
Browse files

Closes #2316: Adding textSize parameter to SimpleBrowserMenuItem.

parent 613b13c8
......@@ -18,13 +18,16 @@ private const val NO_ID = -1
* A simple browser menu item displaying text.
*
* @param label The visible label of this menu item.
* @param textSize: The size of the label.
* @param textColorResource: The color resource to apply to the text.
* @param listener Callback to be invoked when this menu item is clicked.
*/
class SimpleBrowserMenuItem(
private val label: String,
private val textSize: Float = NO_ID.toFloat(),
@ColorRes
private val textColorResource: Int = NO_ID,
private val listener: () -> Unit
private val listener: (() -> Unit)? = null
) : BrowserMenuItem {
override var visible: () -> Boolean = { true }
......@@ -32,12 +35,23 @@ class SimpleBrowserMenuItem(
override fun bind(menu: BrowserMenu, view: View) {
(view as TextView).text = label
if (textSize != NO_ID.toFloat()) {
view.textSize = textSize
}
if (textColorResource != NO_ID) {
view.setTextColor(ContextCompat.getColor(view.context, textColorResource))
}
view.setOnClickListener {
listener.invoke()
menu.dismiss()
if (listener != null) {
view.setOnClickListener {
listener.invoke()
menu.dismiss()
}
} else {
// Remove the ripple effect
view.background = null
}
}
}
......@@ -3,6 +3,7 @@
- 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/. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/simple_text"
style="@style/Mozac.Browser.Menu.Item.Text"
android:layout_width="match_parent"
android:layout_height="@dimen/mozac_browser_menu_item_container_layout_height"
......
......@@ -4,9 +4,14 @@
package mozilla.components.browser.menu.item
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.test.core.app.ApplicationProvider
import mozilla.components.browser.menu.BrowserMenu
import mozilla.components.browser.menu.R
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test
......@@ -18,6 +23,8 @@ import org.robolectric.RuntimeEnvironment
@RunWith(RobolectricTestRunner::class)
class SimpleBrowserMenuItemTest {
private val context: Context get() = ApplicationProvider.getApplicationContext()
@Test
fun `simple menu items are always visible by default`() {
val item = SimpleBrowserMenuItem("Hello") {
......@@ -58,4 +65,27 @@ class SimpleBrowserMenuItemTest {
assertTrue(callbackInvoked)
verify(menu).dismiss()
}
@Test
fun `simple browser menu item should have the right text, textSize, and textColorResource`() {
val item = SimpleBrowserMenuItem(
"Powered by Mozilla",
10f,
android.R.color.holo_green_dark
)
val view = inflate(item)
val textView = view.findViewById<TextView>(R.id.simple_text)
assertEquals(textView.text, "Powered by Mozilla")
assertEquals(textView.textSize, 10f)
assertEquals(textView.currentTextColor, context.getColor(android.R.color.holo_green_dark))
}
private fun inflate(item: SimpleBrowserMenuItem): View {
val view = LayoutInflater.from(context).inflate(item.getLayoutResource(), null)
val mockMenu = mock(BrowserMenu::class.java)
item.bind(mockMenu, view)
return view
}
}
\ No newline at end of file
......@@ -17,6 +17,7 @@ permalink: /changelog/
* **browser-menu**
* ⚠️ **This is a breaking API change!**: Removed redundant `BrowserMenuImageText` `contentDescription`
* Adds `textSize` parameter to `SimpleBrowserMenuItem`
* **concept-fetch**
* ⚠️ **This is a breaking API change**: the [`Response`](https://mozac.org/api/mozilla.components.concept.fetch/-response/) properties `.success` and `.clientError` were renamed to `.isSuccess` and `isClientError` respectively to match Java conventions.
......
......@@ -135,12 +135,13 @@ class ToolbarActivity : AppCompatActivity() {
// Create a menu that looks like the one in Firefox Focus
// //////////////////////////////////////////////////////////////////////////////////////////
val fenix = SimpleBrowserMenuItem("POWERED BY MOZILLA")
val share = SimpleBrowserMenuItem("Share…") { /* Do nothing */ }
val homeScreen = SimpleBrowserMenuItem("Add to Home screen") { /* Do nothing */ }
val open = SimpleBrowserMenuItem("Open in…") { /* Do nothing */ }
val settings = SimpleBrowserMenuItem("Settings") { /* Do nothing */ }
val builder = BrowserMenuBuilder(listOf(share, homeScreen, open, settings))
val builder = BrowserMenuBuilder(listOf(fenix, share, homeScreen, open, settings))
toolbar.setMenuBuilder(builder)
// //////////////////////////////////////////////////////////////////////////////////////////
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment