-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix dark mode buttons support on iOS (#7552)
- Loading branch information
Showing
8 changed files
with
153 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#import <Foundation/Foundation.h> | ||
#import "RNNButtonOptions.h" | ||
#import "RNNIconDrawer.h" | ||
#import "UIImage+utils.h" | ||
|
||
@interface RNNBaseIconCreator : NSObject | ||
|
||
- (instancetype)initWithIconDrawer:(RNNIconDrawer *)iconDrawer; | ||
|
||
- (UIImage *)create:(RNNButtonOptions *)buttonOptions; | ||
|
||
@property (nonatomic, retain) RNNIconDrawer* iconDrawer; | ||
|
||
@end | ||
|
||
@interface RNNBaseIconCreator (Private) | ||
|
||
- (UIImage *)createIcon:(RNNButtonOptions *)buttonOptions | ||
tintColor:(UIColor *)tintColor | ||
backgroundColor:(UIColor *)backgroundColor; | ||
|
||
- (CGSize)resolveIconSize:(RNNButtonOptions *)buttonOptions; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#import "RNNBaseIconCreator.h" | ||
#import "UIImage+utils.h" | ||
|
||
@implementation RNNBaseIconCreator | ||
|
||
- (instancetype)initWithIconDrawer:(RNNIconDrawer *)iconDrawer { | ||
self = [super init]; | ||
self.iconDrawer = iconDrawer; | ||
return self; | ||
} | ||
|
||
- (UIImage *)create:(RNNButtonOptions *)buttonOptions { | ||
if (buttonOptions.isEnabled) | ||
return [self createEnabledIcon:buttonOptions]; | ||
else | ||
return [self createDisabledIcon:buttonOptions]; | ||
} | ||
|
||
- (UIImage *)createEnabledIcon:(RNNButtonOptions *)buttonOptions { | ||
UIColor *backgroundColor = [buttonOptions.iconBackground.color withDefault:UIColor.clearColor]; | ||
UIColor *tintColor = [buttonOptions.color withDefault:nil]; | ||
|
||
return [self createIcon:buttonOptions tintColor:tintColor backgroundColor:backgroundColor]; | ||
} | ||
|
||
- (UIImage *)createDisabledIcon:(RNNButtonOptions *)buttonOptions { | ||
UIColor *backgroundColor = [self resolveDisabledBackgroundColor:buttonOptions]; | ||
UIColor *tintColor = [self resolveDisabledIconColor:buttonOptions]; | ||
|
||
return [self createIcon:buttonOptions tintColor:tintColor backgroundColor:backgroundColor]; | ||
} | ||
|
||
- (UIColor *)resolveDisabledIconColor:(RNNButtonOptions *)buttonOptions { | ||
if (![buttonOptions.enabled withDefault:YES] && buttonOptions.disabledColor.hasValue) | ||
return buttonOptions.disabledColor.get; | ||
else | ||
return [buttonOptions.color withDefault:nil]; | ||
} | ||
|
||
- (UIColor *)resolveDisabledBackgroundColor:(RNNButtonOptions *)buttonOptions { | ||
if (![buttonOptions.enabled withDefault:YES] && | ||
buttonOptions.iconBackground.disabledColor.hasValue) | ||
return buttonOptions.iconBackground.disabledColor.get; | ||
else | ||
return [buttonOptions.iconBackground.color withDefault:nil]; | ||
} | ||
|
||
- (UIImage *)createIcon:(RNNButtonOptions *)buttonOptions | ||
tintColor:(UIColor *)tintColor | ||
backgroundColor:(UIColor *)backgroundColor { | ||
@throw @"createIcon should be implemented by subclass"; | ||
return nil; | ||
} | ||
|
||
- (CGSize)resolveIconSize:(RNNButtonOptions *)buttonOptions { | ||
CGFloat width = | ||
[buttonOptions.iconBackground.width withDefault:@(buttonOptions.icon.get.size.width)] | ||
.floatValue; | ||
CGFloat height = | ||
[buttonOptions.iconBackground.height withDefault:@(buttonOptions.icon.get.size.height)] | ||
.floatValue; | ||
return CGSizeMake(width, height); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#import <Foundation/Foundation.h> | ||
#import "RNNBaseIconCreator.h" | ||
|
||
@interface RNNDynamicIconCreator : RNNBaseIconCreator | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#import "RNNDynamicIconCreator.h" | ||
|
||
@implementation RNNDynamicIconCreator | ||
|
||
- (UIImage *)createIcon:(RNNButtonOptions *)buttonOptions | ||
tintColor:(UIColor *)tintColor | ||
backgroundColor:(UIColor *)backgroundColor API_AVAILABLE(ios(13)) { | ||
UIImage *iconImage = buttonOptions.icon.get; | ||
CGSize iconSize = [self resolveIconSize:buttonOptions]; | ||
CGFloat cornerRadius = [buttonOptions.iconBackground.cornerRadius withDefault:@(0)].floatValue; | ||
UIColor *lightColor = [tintColor resolvedColorWithTraitCollection:[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight]]; | ||
UIColor *darkColor = [tintColor resolvedColorWithTraitCollection:[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark]]; | ||
UIColor *lightBackgroundColor = [backgroundColor resolvedColorWithTraitCollection:[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight]]; | ||
UIColor *darkBackgroundColor = [backgroundColor resolvedColorWithTraitCollection:[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark]]; | ||
UIImage *darkImage = [[self.iconDrawer draw:iconImage | ||
imageColor:darkColor | ||
backgroundColor:lightBackgroundColor | ||
size:iconSize | ||
cornerRadius:cornerRadius] imageWithInsets:buttonOptions.iconInsets.UIEdgeInsets]; | ||
UIImage *lightImage = [[self.iconDrawer draw:iconImage | ||
imageColor:lightColor | ||
backgroundColor:darkBackgroundColor | ||
size:iconSize | ||
cornerRadius:cornerRadius] imageWithInsets:buttonOptions.iconInsets.UIEdgeInsets]; | ||
[lightImage.imageAsset registerImage:darkImage withTraitCollection:[UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark]]; | ||
|
||
return lightImage; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
#import "RNNButtonOptions.h" | ||
#import "RNNIconDrawer.h" | ||
#import <Foundation/Foundation.h> | ||
#import "RNNBaseIconCreator.h" | ||
|
||
@interface RNNIconCreator : NSObject | ||
|
||
- (instancetype)initWithIconDrawer:(RNNIconDrawer *)iconDrawer; | ||
|
||
- (UIImage *)create:(RNNButtonOptions *)buttonOptions; | ||
@interface RNNIconCreator : RNNBaseIconCreator | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,18 @@ | ||
#import "RNNIconCreator.h" | ||
#import "UIImage+utils.h" | ||
|
||
@implementation RNNIconCreator { | ||
RNNIconDrawer *_iconDrawer; | ||
} | ||
|
||
- (instancetype)initWithIconDrawer:(RNNIconDrawer *)iconDrawer { | ||
self = [super init]; | ||
_iconDrawer = iconDrawer; | ||
return self; | ||
} | ||
|
||
- (UIImage *)create:(RNNButtonOptions *)buttonOptions { | ||
if (buttonOptions.isEnabled) | ||
return [self createEnabledIcon:buttonOptions]; | ||
else | ||
return [self createDisabledIcon:buttonOptions]; | ||
} | ||
|
||
- (UIImage *)createEnabledIcon:(RNNButtonOptions *)buttonOptions { | ||
UIColor *backgroundColor = [buttonOptions.iconBackground.color withDefault:UIColor.clearColor]; | ||
UIColor *tintColor = [buttonOptions.color withDefault:nil]; | ||
|
||
return [self createIcon:buttonOptions tintColor:tintColor backgroundColor:backgroundColor]; | ||
} | ||
|
||
- (UIImage *)createDisabledIcon:(RNNButtonOptions *)buttonOptions { | ||
UIColor *backgroundColor = [self resolveDisabledBackgroundColor:buttonOptions]; | ||
UIColor *tintColor = [self resolveDisabledIconColor:buttonOptions]; | ||
|
||
return [self createIcon:buttonOptions tintColor:tintColor backgroundColor:backgroundColor]; | ||
} | ||
|
||
- (UIColor *)resolveDisabledIconColor:(RNNButtonOptions *)buttonOptions { | ||
if (![buttonOptions.enabled withDefault:YES] && buttonOptions.disabledColor.hasValue) | ||
return buttonOptions.disabledColor.get; | ||
else | ||
return [buttonOptions.color withDefault:nil]; | ||
} | ||
|
||
- (UIColor *)resolveDisabledBackgroundColor:(RNNButtonOptions *)buttonOptions { | ||
if (![buttonOptions.enabled withDefault:YES] && | ||
buttonOptions.iconBackground.disabledColor.hasValue) | ||
return buttonOptions.iconBackground.disabledColor.get; | ||
else | ||
return [buttonOptions.iconBackground.color withDefault:nil]; | ||
} | ||
@implementation RNNIconCreator | ||
|
||
- (UIImage *)createIcon:(RNNButtonOptions *)buttonOptions | ||
tintColor:(UIColor *)tintColor | ||
backgroundColor:(UIColor *)backgroundColor { | ||
UIImage *iconImage = buttonOptions.icon.get; | ||
CGSize iconSize = [self resolveIconSize:buttonOptions]; | ||
CGFloat cornerRadius = [buttonOptions.iconBackground.cornerRadius withDefault:@(0)].floatValue; | ||
|
||
return [[_iconDrawer draw:iconImage | ||
return [[self.iconDrawer draw:iconImage | ||
imageColor:tintColor | ||
backgroundColor:backgroundColor | ||
size:iconSize | ||
cornerRadius:cornerRadius] imageWithInsets:buttonOptions.iconInsets.UIEdgeInsets]; | ||
} | ||
|
||
- (CGSize)resolveIconSize:(RNNButtonOptions *)buttonOptions { | ||
CGFloat width = | ||
[buttonOptions.iconBackground.width withDefault:@(buttonOptions.icon.get.size.width)] | ||
.floatValue; | ||
CGFloat height = | ||
[buttonOptions.iconBackground.height withDefault:@(buttonOptions.icon.get.size.height)] | ||
.floatValue; | ||
return CGSizeMake(width, height); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters