Skip to content

Commit

Permalink
Support merging icon background color (#7625)
Browse files Browse the repository at this point in the history
  • Loading branch information
yogevbd authored and SudoPlz committed May 30, 2023
1 parent ca63882 commit 664b626
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public static TopBarOptions parse(Context context, TypefaceLoader typefaceLoader
options.leftButtonColor = ThemeColour.parse(context, json.optJSONObject("leftButtonColor"));
options.leftButtonDisabledColor = ThemeColour.parse(context, json.optJSONObject("leftButtonDisabledColor"));
options.rightButtonDisabledColor = ThemeColour.parse(context, json.optJSONObject("rightButtonDisabledColor"));
options.leftButtonBackgroundColor = ThemeColour.parse(context, json.optJSONObject("leftButtonBackgroundColor"));
options.rightButtonBackgroundColor = ThemeColour.parse(context, json.optJSONObject("rightButtonBackgroundColor"));

options.validate();
return options;
Expand All @@ -76,6 +78,8 @@ public static TopBarOptions parse(Context context, TypefaceLoader typefaceLoader
public ThemeColour leftButtonColor = new NullThemeColour();
public ThemeColour rightButtonDisabledColor = new NullThemeColour();
public ThemeColour leftButtonDisabledColor = new NullThemeColour();
public ThemeColour rightButtonBackgroundColor = new NullThemeColour();
public ThemeColour leftButtonBackgroundColor = new NullThemeColour();

public TopBarOptions copy() {
TopBarOptions result = new TopBarOptions();
Expand All @@ -94,6 +98,8 @@ void mergeWith(final TopBarOptions other) {
if (other.leftButtonColor.hasValue()) leftButtonColor = other.leftButtonColor;
if (other.rightButtonDisabledColor.hasValue()) rightButtonDisabledColor = other.rightButtonDisabledColor;
if (other.leftButtonDisabledColor.hasValue()) leftButtonDisabledColor = other.leftButtonDisabledColor;
if (other.rightButtonBackgroundColor.hasValue()) rightButtonBackgroundColor = other.rightButtonBackgroundColor;
if (other.leftButtonBackgroundColor.hasValue()) rightButtonBackgroundColor = other.rightButtonBackgroundColor;

if (other.testId.hasValue()) testId = other.testId;
if (other.visible.hasValue()) visible = other.visible;
Expand Down Expand Up @@ -122,6 +128,8 @@ public TopBarOptions mergeWithDefault(TopBarOptions defaultOptions) {
if (!leftButtonColor.hasValue()) leftButtonColor = defaultOptions.leftButtonColor;
if (!rightButtonDisabledColor.hasValue()) rightButtonDisabledColor = defaultOptions.rightButtonDisabledColor;
if (!leftButtonDisabledColor.hasValue()) leftButtonDisabledColor = defaultOptions.leftButtonDisabledColor;
if (!rightButtonBackgroundColor.hasValue()) rightButtonBackgroundColor = defaultOptions.rightButtonBackgroundColor;
if (!leftButtonBackgroundColor.hasValue()) rightButtonBackgroundColor = defaultOptions.rightButtonBackgroundColor;

if (!visible.hasValue()) visible = defaultOptions.visible;
if (!animate.hasValue()) animate = defaultOptions.animate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,12 @@ private void mergeOrientation(OrientationOptions orientationOptions) {
private void mergeButtons(TopBarOptions options, TopBarOptions optionsToMerge, View child, StackController stack) {
mergeRightButtons(options, optionsToMerge.buttons, child);
mergeLeftButton(options, optionsToMerge.buttons, child);
mergeLeftButtonsColor(child, optionsToMerge.leftButtonColor, optionsToMerge.leftButtonDisabledColor);
mergeRightButtonsColor(child, optionsToMerge.rightButtonColor, optionsToMerge.rightButtonDisabledColor);
mergeLeftButtonsColor(child, optionsToMerge.leftButtonColor, optionsToMerge.leftButtonDisabledColor, optionsToMerge.leftButtonBackgroundColor);
mergeRightButtonsColor(child, optionsToMerge.rightButtonColor, optionsToMerge.rightButtonDisabledColor, optionsToMerge.rightButtonBackgroundColor);
mergeBackButton(optionsToMerge.buttons, stack);
}

private void mergeLeftButtonsColor(View child, ThemeColour color, ThemeColour disabledColor) {
private void mergeLeftButtonsColor(View child, ThemeColour color, ThemeColour disabledColor, ThemeColour backgroundColor) {
if (color.hasValue() || disabledColor.hasValue()) {
Map<String, ButtonController> stringButtonControllerMap = componentLeftButtons.get(child);
if (stringButtonControllerMap != null) {
Expand All @@ -474,12 +474,15 @@ private void mergeLeftButtonsColor(View child, ThemeColour color, ThemeColour di
if (disabledColor.hasValue()) {
btnController.applyDisabledColor(topBarController.getView().getLeftButtonBar(), disabledColor);
}
if (backgroundColor.hasValue()) {
btnController.applyBackgroundColor(topBarController.getView().getLeftButtonBar(), backgroundColor);
}
});
}
}
}

private void mergeRightButtonsColor(View child, ThemeColour color, ThemeColour disabledColor) {
private void mergeRightButtonsColor(View child, ThemeColour color, ThemeColour disabledColor, ThemeColour backgroundColor) {
if (color.hasValue() || disabledColor.hasValue()) {
Map<String, ButtonController> stringButtonControllerMap = componentRightButtons.get(child);
if (stringButtonControllerMap != null) {
Expand All @@ -490,6 +493,9 @@ private void mergeRightButtonsColor(View child, ThemeColour color, ThemeColour d
if (disabledColor.hasValue()) {
btnController.applyDisabledColor(topBarController.getView().getRightButtonBar(), disabledColor);
}
if (backgroundColor.hasValue()) {
btnController.applyBackgroundColor(topBarController.getView().getRightButtonBar(), backgroundColor);
}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ open class ButtonController(activity: Activity,

open fun applyDisabledColor(toolbar: Toolbar, disabledColour: ThemeColour) = this.menuItem?.let { presenter.applyDisabledColor(toolbar, it, disabledColour) }

open fun applyBackgroundColor(toolbar: Toolbar, color: ThemeColour) = this.menuItem?.let { presenter.applyBackgroundColor(toolbar, it, color) }

fun addToMenu(buttonBar: ButtonBar, order: Int) {
if (button.component.hasValue() && buttonBar.containsButton(menuItem, order)) return
buttonBar.menu.removeItem(button.intId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ open class ButtonPresenter(private val context: Context, private val button: But
}
}

fun applyBackgroundColor(toolbar: Toolbar, menuItem: MenuItem, color: ThemeColour) {
button.iconBackground.color = color
applyIcon(menuItem)
}

private fun applyAccessibilityLabel(menuItem: MenuItem) {
if (button.accessibilityLabel.hasValue()) {
if (button.component.hasValue()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@ public void applyColor_shouldChangeColor() {
assertThat(findButtonView().getCurrentTextColor()).isEqualTo(Color.RED);
}

@Test
public void applyBackgroundColor_shouldChangeBackgroundColor() {
IconBackgroundDrawable mockD = mock(IconBackgroundDrawable.class);
initUUt(ImageLoaderMock.mock(mockD));
button.enabled = new Bool(true);
button.icon = new Text("icon");
button.color = new ThemeColour(new Colour(Color.RED), new Colour(Color.RED));
IconBackgroundOptions iconBackground = new IconBackgroundOptions();
iconBackground.color = new ThemeColour(new Colour(Color.GREEN),new Colour(Color.GREEN));
button.iconBackground = iconBackground;
MenuItem menuItem = spy(addMenuButton());
uut.applyOptions(titleBar, menuItem, buttonController::getView);

assertThat(((IconBackgroundDrawable)menuItem.getIcon()).getBackgroundColor()).isEqualTo(Color.GREEN);

uut.applyBackgroundColor(titleBar, menuItem, new ThemeColour(new Colour(Color.BLACK),new Colour(Color.BLACK)));
assertThat(((IconBackgroundDrawable)menuItem.getIcon()).getBackgroundColor()).isEqualTo(Color.BLACK);
}

@Test
public void applyOptions_shouldChangeIconColorTint() {
IconBackgroundDrawable mockD = mock(IconBackgroundDrawable.class);
Expand Down
4 changes: 3 additions & 1 deletion lib/ios/RNNButtonOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@

- (RNNButtonOptions *)withDefault:(RNNButtonOptions *)defaultOptions;

- (UIColor *)resolveColor;
- (Color *)resolveColor;

- (RNNButtonOptions *)withDefaultColor:(Color *)color disabledColor:(Color *)disabledColor;

- (BOOL)shouldCreateCustomView;

- (BOOL)isEnabled;

- (UIControlState)state;

@end
10 changes: 7 additions & 3 deletions lib/ios/RNNButtonOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ - (BOOL)isEnabled {
return [self.enabled withDefault:YES];
}

- (UIColor *)resolveColor {
- (Color *)resolveColor {
if (![_enabled withDefault:YES] && _disabledColor.hasValue)
return _disabledColor.get;
return _disabledColor;
else
return [_color withDefault:nil];
return _color;
}

- (RNNButtonOptions *)withDefault:(RNNButtonOptions *)defaultOptions {
Expand All @@ -116,4 +116,8 @@ - (RNNButtonOptions *)withDefaultColor:(Color *)color disabledColor:(Color *)dis
return self;
}

- (UIControlState)state {
return self.isEnabled ? UIControlStateNormal : UIControlStateDisabled;
}

@end
8 changes: 6 additions & 2 deletions lib/ios/RNNButtonsPresenter.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
defaultDisabledColor:(Color *)defaultDisabledColor
animated:(BOOL)animated;

- (void)applyLeftButtonsColor:(UIColor *)color;
- (void)applyLeftButtonsColor:(Color *)color;

- (void)applyRightButtonsColor:(UIColor *)color;
- (void)applyRightButtonsColor:(Color *)color;

- (void)applyLeftButtonsBackgroundColor:(Color *)color;

- (void)applyRightButtonsBackgroundColor:(Color *)color;

- (void)componentWillAppear;

Expand Down
20 changes: 16 additions & 4 deletions lib/ios/RNNButtonsPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,27 @@ - (void)applyRightButtons:(NSArray<RNNButtonOptions *> *)rightButtons
defaultDisabledColor:defaultDisabledColor];
}

- (void)applyLeftButtonsColor:(UIColor *)color {
- (void)applyLeftButtonsColor:(Color *)color {
for (RNNUIBarButtonItem *button in self.viewController.navigationItem.leftBarButtonItems) {
[button applyColor:color];
[button mergeColor:color];
}
}

- (void)applyRightButtonsColor:(UIColor *)color {
- (void)applyRightButtonsColor:(Color *)color {
for (RNNUIBarButtonItem *button in self.viewController.navigationItem.rightBarButtonItems) {
[button applyColor:color];
[button mergeColor:color];
}
}

- (void)applyRightButtonsBackgroundColor:(Color *)color {
for (RNNUIBarButtonItem *button in self.viewController.navigationItem.rightBarButtonItems) {
[button mergeBackgroundColor:color];
}
}

- (void)applyLeftButtonsBackgroundColor:(Color *)color {
for (RNNUIBarButtonItem *button in self.viewController.navigationItem.leftBarButtonItems) {
[button mergeBackgroundColor:color];
}
}

Expand Down
12 changes: 10 additions & 2 deletions lib/ios/RNNComponentPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,19 @@ - (void)mergeOptions:(RNNNavigationOptions *)mergeOptions
}

if (mergeOptions.topBar.leftButtonColor.hasValue) {
[_buttonsPresenter applyLeftButtonsColor:mergeOptions.topBar.leftButtonColor.get];
[_buttonsPresenter applyLeftButtonsColor:mergeOptions.topBar.leftButtonColor];
}

if (mergeOptions.topBar.rightButtonColor.hasValue) {
[_buttonsPresenter applyRightButtonsColor:mergeOptions.topBar.rightButtonColor.get];
[_buttonsPresenter applyRightButtonsColor:mergeOptions.topBar.rightButtonColor];
}

if (mergeOptions.topBar.rightButtonBackgroundColor.hasValue) {
[_buttonsPresenter applyRightButtonsBackgroundColor:mergeOptions.topBar.rightButtonBackgroundColor];
}

if (mergeOptions.topBar.leftButtonBackgroundColor.hasValue) {
[_buttonsPresenter applyLeftButtonsBackgroundColor:mergeOptions.topBar.leftButtonBackgroundColor];
}

if (mergeOptions.overlay.interceptTouchOutside.hasValue) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ios/RNNIconDrawer.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ - (UIImage *)draw:(UIImage *)image
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
return newImage;
}

@end
8 changes: 8 additions & 0 deletions lib/ios/RNNTitleViewHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

@property(nonatomic, strong) UILabel *subtitleLabel;

- (void)setTitleColor:(UIColor *)color;

- (void)setSubtitleColor:(UIColor *)color;

@end

@interface RNNTitleViewHelper : NSObject
Expand All @@ -21,4 +25,8 @@

- (void)setup;

- (void)setTitleColor:(UIColor *)color;

- (void)setSubtitleColor:(UIColor *)color;

@end
16 changes: 16 additions & 0 deletions lib/ios/RNNTitleViewHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ - (void)layoutSubviews {
self.frame.size.width, _subtitleLabel.frame.size.height)];
}

- (void)setTitleColor:(UIColor *)color {
_titleLabel.textColor = color;
}

- (void)setSubtitleColor:(UIColor *)color {
_subtitleLabel.textColor = color;
}

@end

@interface RNNTitleViewHelper ()
Expand Down Expand Up @@ -170,4 +178,12 @@ - (UILabel *)setupTitle {
return titleLabel;
}

- (void)setTitleColor:(UIColor *)color {
[_titleView setTitleColor:color];
}

- (void)setSubtitleColor:(UIColor *)color {
[_titleView setSubtitleColor:color];
}

@end
2 changes: 2 additions & 0 deletions lib/ios/RNNTopBarOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
@property(nonatomic, strong) Color *rightButtonColor;
@property(nonatomic, strong) Color *leftButtonDisabledColor;
@property(nonatomic, strong) Color *rightButtonDisabledColor;
@property(nonatomic, strong) Color *leftButtonBackgroundColor;
@property(nonatomic, strong) Color *rightButtonBackgroundColor;
@property(nonatomic, strong) Bool *drawBehind;
@property(nonatomic, strong) Bool *noBorder;
@property(nonatomic, strong) Color *borderColor;
Expand Down
6 changes: 6 additions & 0 deletions lib/ios/RNNTopBarOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ - (instancetype)initWithDict:(NSDictionary *)dict {
self.rightButtonColor = [ColorParser parse:dict key:@"rightButtonColor"];
self.leftButtonDisabledColor = [ColorParser parse:dict key:@"leftButtonDisabledColor"];
self.rightButtonDisabledColor = [ColorParser parse:dict key:@"rightButtonDisabledColor"];
self.leftButtonBackgroundColor = [ColorParser parse:dict key:@"leftButtonBackgroundColor"];
self.rightButtonBackgroundColor = [ColorParser parse:dict key:@"rightButtonBackgroundColor"];
self.drawBehind = [BoolParser parse:dict key:@"drawBehind"];
self.noBorder = [BoolParser parse:dict key:@"noBorder"];
self.borderColor = [ColorParser parse:dict key:@"borderColor"];
Expand Down Expand Up @@ -63,6 +65,10 @@ - (void)mergeOptions:(RNNTopBarOptions *)options {
self.leftButtonDisabledColor = options.leftButtonDisabledColor;
if (options.rightButtonDisabledColor.hasValue)
self.rightButtonDisabledColor = options.rightButtonDisabledColor;
if (options.leftButtonBackgroundColor.hasValue)
self.leftButtonBackgroundColor = options.leftButtonBackgroundColor;
if (options.rightButtonBackgroundColor.hasValue)
self.rightButtonBackgroundColor = options.rightButtonBackgroundColor;
if (options.drawBehind.hasValue)
self.drawBehind = options.drawBehind;
if (options.noBorder.hasValue)
Expand Down
3 changes: 2 additions & 1 deletion lib/ios/RNNUIBarButtonItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ typedef void (^RNNButtonPressCallback)(NSString *buttonId);
- (instancetype)initWithSystemItem:(RNNButtonOptions *)buttonOptions
onPress:(RNNButtonPressCallback)onPress;

- (void)applyColor:(UIColor *)color;
- (void)mergeColor:(Color *)color;
- (void)mergeBackgroundColor:(Color *)color;

- (void)notifyWillAppear;
- (void)notifyDidAppear;
Expand Down
Loading

0 comments on commit 664b626

Please sign in to comment.