Skip to content

Commit

Permalink
Add allCaps option to TopBar buttons (#6397)
Browse files Browse the repository at this point in the history
TopBar buttons are allCaps by default on Android. This option can be used to turn this option off, making camel case buttons possible.
  • Loading branch information
guyca authored Jul 15, 2020
1 parent 7add403 commit a561a80
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ButtonOptions {
public String id = "btn" + CompatUtils.generateViewId();
public Text accessibilityLabel = new NullText();
public Text text = new NullText();
public Bool allCaps = new NullBool();
public Bool enabled = new NullBool();
public Bool disableIconTint = new NullBool();
public Number showAsAction = new NullNumber();
Expand All @@ -53,6 +54,7 @@ public boolean equals(ButtonOptions other) {
return Objects.equals(id, other.id) &&
accessibilityLabel.equals(other.accessibilityLabel) &&
text.equals(other.text) &&
allCaps.equals(other.allCaps) &&
enabled.equals(other.enabled) &&
disableIconTint.equals(other.disableIconTint) &&
showAsAction.equals(other.showAsAction) &&
Expand All @@ -71,6 +73,7 @@ private static ButtonOptions parseJson(JSONObject json, TypefaceLoader typefaceM
button.id = take(json.optString("id"), "btn" + CompatUtils.generateViewId());
button.accessibilityLabel = TextParser.parse(json, "accessibilityLabel");
button.text = TextParser.parse(json, "text");
button.allCaps = BoolParser.parse(json, "allCaps");
button.enabled = BoolParser.parse(json, "enabled");
button.disableIconTint = BoolParser.parse(json, "disableIconTint");
button.showAsAction = parseShowAsAction(json);
Expand Down Expand Up @@ -153,6 +156,7 @@ private static Number parseShowAsAction(JSONObject json) {

public void mergeWith(ButtonOptions other) {
if (other.text.hasValue()) text = other.text;
if (other.allCaps.hasValue()) allCaps = other.allCaps;
if (other.accessibilityLabel.hasValue()) accessibilityLabel = other.accessibilityLabel;
if (other.enabled.hasValue()) enabled = other.enabled;
if (other.disableIconTint.hasValue()) disableIconTint = other.disableIconTint;
Expand All @@ -171,6 +175,7 @@ public void mergeWith(ButtonOptions other) {

public void mergeWithDefault(ButtonOptions defaultOptions) {
if (!text.hasValue()) text = defaultOptions.text;
if (!allCaps.hasValue()) allCaps = defaultOptions.allCaps;
if (!accessibilityLabel.hasValue()) accessibilityLabel = defaultOptions.accessibilityLabel;
if (!enabled.hasValue()) enabled = defaultOptions.enabled;
if (!disableIconTint.hasValue()) disableIconTint = defaultOptions.disableIconTint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ open class ButtonPresenter(private val button: ButtonOptions, private val iconRe
applyOptionsDirectlyOnView(titleBar, menuItem) {
applyTestId(it)
applyTextColor(it)
applyAllCaps(it)
}
}

Expand Down Expand Up @@ -96,6 +97,10 @@ open class ButtonPresenter(private val button: ButtonOptions, private val iconRe
}
}

private fun applyAllCaps(view: View) {
if (view is TextView) view.isAllCaps = button.allCaps.get(true)
}

private fun applyOptionsDirectlyOnView(titleBar: TitleBar, menuItem: MenuItem, onViewFound: (View) -> Unit) {
titleBar.doOnPreDraw {
if (button.hasComponent()) onViewFound(menuItem.actionView!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,20 @@ public void beforeEach() {
@Test
public void applyOptions_buttonIsAddedToMenu() {
addButtonAndApplyOptions();

assertThat(findButtonView().getText().toString()).isEqualTo(BTN_TEXT);
}

@Test
public void applyOptions_appliesColorOnButtonTextView() {
button.color = new Colour(Color.RED);
addButtonAndApplyOptions();

assertThat(findButtonView().getCurrentTextColor()).isEqualTo(Color.RED);
}

@Test
public void apply_disabledColor() {
button.enabled = new Bool(false);
addButtonAndApplyOptions();

assertThat(findButtonView().getCurrentTextColor()).isEqualTo(ButtonPresenter.DISABLED_COLOR);
}

Expand All @@ -81,6 +78,13 @@ private void addButtonAndApplyOptions() {
uut.applyOptions(titleBar, menuItem, buttonController::getView);
}

@Test
public void apply_allCaps() {
button.allCaps = new Bool(false);
addButtonAndApplyOptions();
assertThat(findButtonView().isAllCaps()).isEqualTo(false);
}

private TextView findButtonView() {
ShadowLooper.idleMainLooper();
return (TextView) ViewUtils.findChildrenByClass(
Expand Down
4 changes: 4 additions & 0 deletions lib/src/interfaces/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ export interface OptionsTopBarBackground {
}

export interface OptionsTopBarButton {
/**
* (Android only) Sets a textual button to be ALL CAPS. default value is true
*/
allCaps?: boolean;
/**
* Button id for reference press event
*/
Expand Down

0 comments on commit a561a80

Please sign in to comment.