Skip to content

Commit 0e9c5a4

Browse files
committed
Fix heading menu item content description
1 parent 98ae9fe commit 0e9c5a4

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/toolbar/AztecToolbar.kt

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.wordpress.aztec.toolbar
22

3+
import android.annotation.SuppressLint
34
import android.content.Context
45
import android.os.Build
56
import android.os.Bundle
@@ -253,49 +254,50 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
253254
override fun onMenuItemClick(item: MenuItem?): Boolean {
254255
val checked = (item?.isChecked == false)
255256
item?.isChecked = checked
257+
val headingButton = findViewById<ToggleButton>(R.id.format_bar_button_heading)
256258

257259
when (item?.itemId) {
258260
// Heading Menu
259261
R.id.paragraph -> {
260262
aztecToolbarListener?.onToolbarFormatButtonClicked(AztecTextFormat.FORMAT_PARAGRAPH, false)
261263
editor?.toggleFormatting(AztecTextFormat.FORMAT_PARAGRAPH)
262-
setHeadingMenuSelector(AztecTextFormat.FORMAT_PARAGRAPH)
264+
updateHeadingMenuItem(AztecTextFormat.FORMAT_PARAGRAPH, headingButton)
263265
return true
264266
}
265267
R.id.heading_1 -> {
266268
aztecToolbarListener?.onToolbarFormatButtonClicked(AztecTextFormat.FORMAT_HEADING_1, false)
267269
editor?.toggleFormatting(AztecTextFormat.FORMAT_HEADING_1)
268-
setHeadingMenuSelector(AztecTextFormat.FORMAT_HEADING_1)
270+
updateHeadingMenuItem(AztecTextFormat.FORMAT_HEADING_1, headingButton)
269271
return true
270272
}
271273
R.id.heading_2 -> {
272274
aztecToolbarListener?.onToolbarFormatButtonClicked(AztecTextFormat.FORMAT_HEADING_2, false)
273275
editor?.toggleFormatting(AztecTextFormat.FORMAT_HEADING_2)
274-
setHeadingMenuSelector(AztecTextFormat.FORMAT_HEADING_2)
276+
updateHeadingMenuItem(AztecTextFormat.FORMAT_HEADING_2, headingButton)
275277
return true
276278
}
277279
R.id.heading_3 -> {
278280
aztecToolbarListener?.onToolbarFormatButtonClicked(AztecTextFormat.FORMAT_HEADING_3, false)
279281
editor?.toggleFormatting(AztecTextFormat.FORMAT_HEADING_3)
280-
setHeadingMenuSelector(AztecTextFormat.FORMAT_HEADING_3)
282+
updateHeadingMenuItem(AztecTextFormat.FORMAT_HEADING_3, headingButton)
281283
return true
282284
}
283285
R.id.heading_4 -> {
284286
aztecToolbarListener?.onToolbarFormatButtonClicked(AztecTextFormat.FORMAT_HEADING_4, false)
285287
editor?.toggleFormatting(AztecTextFormat.FORMAT_HEADING_4)
286-
setHeadingMenuSelector(AztecTextFormat.FORMAT_HEADING_4)
288+
updateHeadingMenuItem(AztecTextFormat.FORMAT_HEADING_4, headingButton)
287289
return true
288290
}
289291
R.id.heading_5 -> {
290292
aztecToolbarListener?.onToolbarFormatButtonClicked(AztecTextFormat.FORMAT_HEADING_5, false)
291293
editor?.toggleFormatting(AztecTextFormat.FORMAT_HEADING_5)
292-
setHeadingMenuSelector(AztecTextFormat.FORMAT_HEADING_5)
294+
updateHeadingMenuItem(AztecTextFormat.FORMAT_HEADING_5, headingButton)
293295
return true
294296
}
295297
R.id.heading_6 -> {
296298
aztecToolbarListener?.onToolbarFormatButtonClicked(AztecTextFormat.FORMAT_HEADING_6, false)
297299
editor?.toggleFormatting(AztecTextFormat.FORMAT_HEADING_6)
298-
setHeadingMenuSelector(AztecTextFormat.FORMAT_HEADING_6)
300+
updateHeadingMenuItem(AztecTextFormat.FORMAT_HEADING_6, headingButton)
299301
return true
300302
}
301303
// TODO: Uncomment when Preformat is to be added back as a feature
@@ -621,11 +623,12 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
621623
}
622624

623625
private fun selectHeadingMenuItem(textFormats: ArrayList<ITextFormat>) {
626+
val headingButton = findViewById<ToggleButton>(R.id.format_bar_button_heading)
627+
// Use unnumbered heading selector by default.
628+
updateHeadingMenuItem(AztecTextFormat.FORMAT_PARAGRAPH, headingButton)
624629
if (textFormats.size == 0) {
625630
// Select paragraph by default.
626631
headingMenu?.menu?.findItem(R.id.paragraph)?.isChecked = true
627-
// Use unnumbered heading selector by default.
628-
setHeadingMenuSelector(AztecTextFormat.FORMAT_PARAGRAPH)
629632
} else {
630633
textFormats.forEach {
631634
when (it) {
@@ -643,7 +646,7 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
643646
}
644647
}
645648

646-
setHeadingMenuSelector(it)
649+
updateHeadingMenuItem(it, headingButton)
647650

648651
return
649652
}
@@ -879,20 +882,47 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
879882
}
880883
}
881884

882-
private fun setHeadingMenuSelector(textFormat: ITextFormat) {
885+
private fun updateHeadingMenuItem(textFormat: ITextFormat, headingButton: ToggleButton) {
886+
var backgroundRes = R.drawable.format_bar_button_heading_selector
887+
var contentDescriptionRes = R.string.format_bar_description_heading
888+
var check = true
883889
when (textFormat) {
884-
AztecTextFormat.FORMAT_HEADING_1 -> findViewById<ToggleButton>(R.id.format_bar_button_heading).setBackgroundResource(R.drawable.format_bar_button_heading_1_selector)
885-
AztecTextFormat.FORMAT_HEADING_2 -> findViewById<ToggleButton>(R.id.format_bar_button_heading).setBackgroundResource(R.drawable.format_bar_button_heading_2_selector)
886-
AztecTextFormat.FORMAT_HEADING_3 -> findViewById<ToggleButton>(R.id.format_bar_button_heading).setBackgroundResource(R.drawable.format_bar_button_heading_3_selector)
887-
AztecTextFormat.FORMAT_HEADING_4 -> findViewById<ToggleButton>(R.id.format_bar_button_heading).setBackgroundResource(R.drawable.format_bar_button_heading_4_selector)
888-
AztecTextFormat.FORMAT_HEADING_5 -> findViewById<ToggleButton>(R.id.format_bar_button_heading).setBackgroundResource(R.drawable.format_bar_button_heading_5_selector)
889-
AztecTextFormat.FORMAT_HEADING_6 -> findViewById<ToggleButton>(R.id.format_bar_button_heading).setBackgroundResource(R.drawable.format_bar_button_heading_6_selector)
890-
AztecTextFormat.FORMAT_PARAGRAPH -> findViewById<ToggleButton>(R.id.format_bar_button_heading).setBackgroundResource(R.drawable.format_bar_button_heading_selector)
890+
AztecTextFormat.FORMAT_HEADING_1 -> {
891+
backgroundRes = R.drawable.format_bar_button_heading_1_selector
892+
contentDescriptionRes = R.string.heading_1
893+
}
894+
AztecTextFormat.FORMAT_HEADING_2 -> {
895+
backgroundRes = R.drawable.format_bar_button_heading_2_selector
896+
contentDescriptionRes = R.string.heading_2
897+
}
898+
AztecTextFormat.FORMAT_HEADING_3 -> {
899+
backgroundRes = R.drawable.format_bar_button_heading_3_selector
900+
contentDescriptionRes = R.string.heading_3
901+
}
902+
AztecTextFormat.FORMAT_HEADING_4 -> {
903+
backgroundRes = R.drawable.format_bar_button_heading_4_selector
904+
contentDescriptionRes = R.string.heading_4
905+
}
906+
AztecTextFormat.FORMAT_HEADING_5 -> {
907+
backgroundRes = R.drawable.format_bar_button_heading_5_selector
908+
contentDescriptionRes = R.string.heading_5
909+
}
910+
AztecTextFormat.FORMAT_HEADING_6 -> {
911+
backgroundRes = R.drawable.format_bar_button_heading_6_selector
912+
contentDescriptionRes = R.string.heading_6
913+
}
914+
AztecTextFormat.FORMAT_PARAGRAPH -> {
915+
// keep default background and contentDescription
916+
check = false
917+
}
891918
else -> {
892-
// Use unnumbered heading selector by default.
893-
findViewById<ToggleButton>(R.id.format_bar_button_heading).setBackgroundResource(R.drawable.format_bar_button_heading_selector)
919+
// ignore for unknown textFormats
920+
return
894921
}
895922
}
923+
headingButton.setBackgroundResource(backgroundRes)
924+
headingButton.contentDescription = context.getString(contentDescriptionRes)
925+
headingButton.isChecked = check
896926
}
897927

898928
private fun showCollapsedToolbar() {
@@ -959,6 +989,7 @@ class AztecToolbar : FrameLayout, OnMenuItemClickListener {
959989
toolbarButtonPlugins.forEach { button -> if (button !is IMediaToolbarButton) button.toolbarStateAboutToChange(this, !isEnabled) }
960990
}
961991

992+
@SuppressLint("InflateParams")
962993
private fun showDialogShortcuts() {
963994
val layout = LayoutInflater.from(context).inflate(R.layout.dialog_shortcuts, null)
964995
val builder = AlertDialog.Builder(context)

0 commit comments

Comments
 (0)