Skip to content

Commit b3d5ca6

Browse files
authored
Merge pull request #948 from wordpress-mobile/customize_toolbar_layout
Add possibility to set a custom layout for the toolbar
2 parents 2043b6d + cafefbf commit b3d5ca6

File tree

4 files changed

+44
-34
lines changed

4 files changed

+44
-34
lines changed

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

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
5555
private var sourceEditor: SourceViewEditText? = null
5656
private var dialogShortcuts: AlertDialog? = null
5757
private var isAdvanced: Boolean = false
58+
private var hasCustomLayout: Boolean = false
5859
private var isMediaToolbarAvailable: Boolean = false
5960
private var isExpanded: Boolean = false
6061
private var isMediaToolbarVisible: Boolean = false
@@ -408,33 +409,39 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
408409
val toolbarBorderColor = styles.getColor(R.styleable.AztecToolbar_toolbarBorderColor,
409410
ContextCompat.getColor(context, R.color.format_bar_divider_horizontal))
410411

411-
styles.recycle()
412+
val layout = when {
413+
styles.hasValue(R.styleable.AztecToolbar_customLayout) -> {
414+
hasCustomLayout = true
415+
styles.getResourceId(R.styleable.AztecToolbar_customLayout, 0)
416+
}
417+
isAdvanced -> R.layout.aztec_format_bar_advanced
418+
else -> R.layout.aztec_format_bar_basic
419+
}
412420

413-
val layout = if (isAdvanced) R.layout.aztec_format_bar_advanced else R.layout.aztec_format_bar_basic
421+
styles.recycle()
414422
View.inflate(context, layout, this)
415423

416424
toolbarScrolView = findViewById(R.id.format_bar_button_scroll)
417425
htmlButton = findViewById(R.id.format_bar_button_html)
418426
setBackgroundColor(toolbarBackgroundColor)
419-
findViewById<View>(R.id.format_bar_horizontal_divider).setBackgroundColor(toolbarBorderColor)
427+
findViewById<View>(R.id.format_bar_horizontal_divider)?.setBackgroundColor(toolbarBorderColor)
420428

421429
setAdvancedState()
422430
setupMediaToolbar()
423431
setupToolbarButtonsForAccessibility()
424432

425433
for (toolbarAction in ToolbarAction.values()) {
426-
val button = findViewById<ToggleButton>(toolbarAction.buttonId)
427-
button?.setOnClickListener { onToolbarAction(toolbarAction) }
434+
findViewById<ToggleButton>(toolbarAction.buttonId)?.let {
435+
it.setOnClickListener { onToolbarAction(toolbarAction) }
428436

429-
if (toolbarAction == ToolbarAction.HEADING) {
430-
setHeadingMenu(findViewById(toolbarAction.buttonId))
431-
}
432-
433-
if (toolbarAction == ToolbarAction.LIST) {
434-
setListMenu(findViewById(toolbarAction.buttonId))
437+
when (toolbarAction) {
438+
ToolbarAction.HEADING -> setHeadingMenu(it)
439+
ToolbarAction.LIST -> setListMenu(it)
440+
}
441+
if (!hasCustomLayout) {
442+
it.setBackgroundDrawableRes(toolbarAction.buttonDrawableRes)
443+
}
435444
}
436-
437-
button?.setBackgroundDrawableRes(toolbarAction.buttonDrawableRes)
438445
}
439446
}
440447

@@ -475,7 +482,7 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
475482

476483
ToolbarAction.values().forEach { action ->
477484
if (targetActions.contains(action)) {
478-
findViewById<ToggleButton>(action.buttonId).convertToButtonAccessibilityProperties()
485+
findViewById<ToggleButton>(action.buttonId)?.convertToButtonAccessibilityProperties()
479486
}
480487
}
481488
}
@@ -497,7 +504,7 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
497504
if (action != ToolbarAction.ELLIPSIS_COLLAPSE &&
498505
action != ToolbarAction.ELLIPSIS_EXPAND) {
499506
val view = findViewById<ToggleButton>(action.buttonId)
500-
if (view.isChecked) actions.add(action)
507+
if (view?.isChecked == true) actions.add(action)
501508
}
502509
}
503510

@@ -673,22 +680,22 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
673680
return listMenu
674681
}
675682

676-
fun getSelectedHeadingMenuItem(): ITextFormat? {
677-
if (headingMenu?.menu?.findItem(R.id.paragraph)?.isChecked!!) return AztecTextFormat.FORMAT_PARAGRAPH
678-
else if (headingMenu?.menu?.findItem(R.id.heading_1)?.isChecked!!) return AztecTextFormat.FORMAT_HEADING_1
679-
else if (headingMenu?.menu?.findItem(R.id.heading_2)?.isChecked!!) return AztecTextFormat.FORMAT_HEADING_2
680-
else if (headingMenu?.menu?.findItem(R.id.heading_3)?.isChecked!!) return AztecTextFormat.FORMAT_HEADING_3
681-
else if (headingMenu?.menu?.findItem(R.id.heading_4)?.isChecked!!) return AztecTextFormat.FORMAT_HEADING_4
682-
else if (headingMenu?.menu?.findItem(R.id.heading_5)?.isChecked!!) return AztecTextFormat.FORMAT_HEADING_5
683-
else if (headingMenu?.menu?.findItem(R.id.heading_6)?.isChecked!!) return AztecTextFormat.FORMAT_HEADING_6
684-
// TODO: Uncomment when Preformat is to be added back as a feature
685-
// else if (headingMenu?.menu?.findItem(R.id.preformat)?.isChecked!!) return AztecTextFormat.FORMAT_PREFORMAT
686-
return null
683+
fun getSelectedHeadingMenuItem(): ITextFormat? = when {
684+
headingMenu?.menu?.findItem(R.id.paragraph)?.isChecked == true -> AztecTextFormat.FORMAT_PARAGRAPH
685+
headingMenu?.menu?.findItem(R.id.heading_1)?.isChecked == true -> AztecTextFormat.FORMAT_HEADING_1
686+
headingMenu?.menu?.findItem(R.id.heading_2)?.isChecked == true -> AztecTextFormat.FORMAT_HEADING_2
687+
headingMenu?.menu?.findItem(R.id.heading_3)?.isChecked == true -> AztecTextFormat.FORMAT_HEADING_3
688+
headingMenu?.menu?.findItem(R.id.heading_4)?.isChecked == true -> AztecTextFormat.FORMAT_HEADING_4
689+
headingMenu?.menu?.findItem(R.id.heading_5)?.isChecked == true -> AztecTextFormat.FORMAT_HEADING_5
690+
headingMenu?.menu?.findItem(R.id.heading_6)?.isChecked == true -> AztecTextFormat.FORMAT_HEADING_6
691+
// TODO: Uncomment when Preformat is to be added back as a feature
692+
// else if (headingMenu?.menu?.findItem(R.id.preformat)?.isChecked!!) return AztecTextFormat.FORMAT_PREFORMAT
693+
else -> null
687694
}
688695

689696
fun getSelectedListMenuItem(): ITextFormat? {
690-
if (listMenu?.menu?.findItem(R.id.list_unordered)?.isChecked!!) return AztecTextFormat.FORMAT_UNORDERED_LIST
691-
else if (listMenu?.menu?.findItem(R.id.list_ordered)?.isChecked!!) return AztecTextFormat.FORMAT_ORDERED_LIST
697+
if (listMenu?.menu?.findItem(R.id.list_unordered)?.isChecked == true) return AztecTextFormat.FORMAT_UNORDERED_LIST
698+
else if (listMenu?.menu?.findItem(R.id.list_ordered)?.isChecked == true) return AztecTextFormat.FORMAT_ORDERED_LIST
692699
return null
693700
}
694701

@@ -721,7 +728,7 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
721728
}
722729

723730
private fun selectHeadingMenuItem(textFormats: ArrayList<ITextFormat>) {
724-
val headingButton = findViewById<ToggleButton>(ToolbarAction.HEADING.buttonId)
731+
val headingButton = findViewById<ToggleButton>(ToolbarAction.HEADING.buttonId) ?: return
725732
// Use unnumbered heading selector by default.
726733
updateHeadingMenuItem(AztecTextFormat.FORMAT_PARAGRAPH, headingButton)
727734
headingMenu?.menu?.findItem(R.id.paragraph)?.isChecked = true
@@ -743,7 +750,7 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
743750
}
744751

745752
private fun selectListMenuItem(textFormats: ArrayList<ITextFormat>) {
746-
val listButton = findViewById<ToggleButton>(ToolbarAction.LIST.buttonId)
753+
val listButton = findViewById<ToggleButton>(ToolbarAction.LIST.buttonId) ?: return
747754
updateListMenuItem(AztecTextFormat.FORMAT_NONE, listButton)
748755
listMenu?.menu?.findItem(R.id.list_none)?.isChecked = true
749756
foreach@ for (it in textFormats) {
@@ -832,10 +839,10 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
832839
}
833840

834841
private fun setupMediaToolbar() {
835-
val mediaToolbarContainer : LinearLayout = findViewById(R.id.media_button_container)
842+
if (!isMediaToolbarAvailable) return
843+
val mediaToolbarContainer: LinearLayout = findViewById(R.id.media_button_container)
836844
mediaToolbarContainer.visibility = if (isMediaToolbarAvailable) View.VISIBLE else View.GONE
837845
buttonMediaCollapsed = findViewById(R.id.format_bar_button_media_collapsed)
838-
if (!isMediaToolbarAvailable) return
839846

840847
mediaToolbar = findViewById(R.id.media_toolbar)
841848
stylingToolbar = findViewById(R.id.styling_toolbar)

aztec/src/main/res/layout/aztec_format_bar_advanced.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
android:id="@+id/media_button_container"
3939
android:layout_width="wrap_content"
4040
android:layout_height="match_parent"
41-
android:orientation="horizontal">
41+
android:orientation="horizontal"
42+
android:visibility="gone">
4243

4344
<org.wordpress.aztec.toolbar.RippleToggleButton
4445
android:id="@+id/format_bar_button_media_collapsed"

aztec/src/main/res/layout/aztec_format_bar_basic.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
android:id="@+id/media_button_container"
3737
android:layout_width="wrap_content"
3838
android:layout_height="match_parent"
39-
android:orientation="horizontal">
39+
android:orientation="horizontal"
40+
android:visibility="gone">
4041

4142
<org.wordpress.aztec.toolbar.RippleToggleButton
4243
android:id="@+id/format_bar_button_media_collapsed"

aztec/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<attr name="toolbarIconNormalColor" format="reference|color" />
4444
<attr name="toolbarIconHighlightColor" format="reference|color" />
4545
<attr name="toolbarIconDisabledColor" format="reference|color" />
46+
<attr name="customLayout" format="reference" />
4647
</declare-styleable>
4748

4849
<declare-styleable name="SourceViewEditText">

0 commit comments

Comments
 (0)