Commit d4d831dd authored by Robbie Gibson's avatar Robbie Gibson Committed by Commit Bot

[iOS][Dark Mode] Add workaround to UIKit bug with image rendering mode

The bug is that images with rendering mode
UIImageRenderingModeAlwaysTemplate do not automatically respond when
the mode changes. To fix this, the view controller will alert the
mediator whenever its own mode changes. The mediator can then use this
value to get the correct icon from the branded image provider, and
update the icon back.

Once the bug is fixed, none of this code is necessary, as the image
should update automatically through UIKit.

Bug: 998090
Change-Id: I4e39f4267724366a26b189112b55ccf9b3b222cf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1774284Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Commit-Queue: Robbie Gibson <rkgibson@google.com>
Cr-Commit-Position: refs/heads/master@{#691558}
parent be3b690b
...@@ -63,6 +63,8 @@ ...@@ -63,6 +63,8 @@
self.mediator.webStateList = self.webStateList; self.mediator.webStateList = self.webStateList;
self.mediator.bookmarkModel = self.mediator.bookmarkModel =
ios::BookmarkModelFactory::GetForBrowserState(self.browserState); ios::BookmarkModelFactory::GetForBrowserState(self.browserState);
self.viewController.adaptiveToolbarViewControllerDelegate = self.mediator;
} }
- (void)stop { - (void)stop {
......
...@@ -12,12 +12,22 @@ ...@@ -12,12 +12,22 @@
#import "ios/chrome/browser/ui/toolbar/buttons/toolbar_type.h" #import "ios/chrome/browser/ui/toolbar/buttons/toolbar_type.h"
#import "ios/chrome/browser/ui/toolbar/toolbar_consumer.h" #import "ios/chrome/browser/ui/toolbar/toolbar_consumer.h"
@class AdaptiveToolbarViewController;
@protocol ApplicationCommands; @protocol ApplicationCommands;
@protocol BrowserCommands; @protocol BrowserCommands;
@protocol PopupMenuLongPressDelegate; @protocol PopupMenuLongPressDelegate;
@class ToolbarButtonFactory; @class ToolbarButtonFactory;
@class ToolbarToolsMenuButton; @class ToolbarToolsMenuButton;
// This protocol is needed to work around an iOS 13 UIKit bug with dark mode.
// See crbug.com/998090 for more details.
@protocol AdaptiveToolbarViewControllerDelegate
// Notifies the delegate that the user interface style of the toolbar has
// changed.
- (void)userInterfaceStyleChangedForViewController:
(AdaptiveToolbarViewController*)viewController;
@end
// ViewController for the adaptive toolbar. This ViewController is the super // ViewController for the adaptive toolbar. This ViewController is the super
// class of the different implementation (primary or secondary). // class of the different implementation (primary or secondary).
// This class and its subclasses are constraining some named layout guides to // This class and its subclasses are constraining some named layout guides to
...@@ -36,6 +46,9 @@ ...@@ -36,6 +46,9 @@
@property(nonatomic, weak) id<ApplicationCommands, BrowserCommands> dispatcher; @property(nonatomic, weak) id<ApplicationCommands, BrowserCommands> dispatcher;
// Delegate for the long press gesture recognizer triggering popup menu. // Delegate for the long press gesture recognizer triggering popup menu.
@property(nonatomic, weak) id<PopupMenuLongPressDelegate> longPressDelegate; @property(nonatomic, weak) id<PopupMenuLongPressDelegate> longPressDelegate;
// Dark mode delegate for this toolbar.
@property(nonatomic, weak) id<AdaptiveToolbarViewControllerDelegate>
adaptiveToolbarViewControllerDelegate;
// Returns the tools menu button. // Returns the tools menu button.
- (ToolbarToolsMenuButton*)toolsMenuButton; - (ToolbarToolsMenuButton*)toolsMenuButton;
......
...@@ -98,11 +98,16 @@ const CGFloat kTabGridAnimationsTotalDuration = 0.5; ...@@ -98,11 +98,16 @@ const CGFloat kTabGridAnimationsTotalDuration = 0.5;
[self addLongPressGestureToView:self.view.toolsMenuButton]; [self addLongPressGestureToView:self.view.toolsMenuButton];
[self updateLayoutBasedOnTraitCollection]; [self updateLayoutBasedOnTraitCollection];
[self.adaptiveToolbarViewControllerDelegate
userInterfaceStyleChangedForViewController:self];
} }
- (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection { - (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection]; [super traitCollectionDidChange:previousTraitCollection];
[self updateLayoutBasedOnTraitCollection]; [self updateLayoutBasedOnTraitCollection];
[self.adaptiveToolbarViewControllerDelegate
userInterfaceStyleChangedForViewController:self];
} }
- (void)viewDidLayoutSubviews { - (void)viewDidLayoutSubviews {
......
...@@ -7,20 +7,21 @@ ...@@ -7,20 +7,21 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "ios/chrome/browser/ui/toolbar/adaptive_toolbar_view_controller.h"
namespace bookmarks { namespace bookmarks {
class BookmarkModel; class BookmarkModel;
} }
namespace web { namespace web {
class WebState; class WebState;
} }
class TemplateURLService; class TemplateURLService;
@protocol ToolbarConsumer; @protocol ToolbarConsumer;
class WebStateList; class WebStateList;
// A mediator object that provides the relevant properties of a web state // A mediator object that provides the relevant properties of a web state
// to a consumer. // to a consumer.
@interface ToolbarMediator : NSObject @interface ToolbarMediator : NSObject <AdaptiveToolbarViewControllerDelegate>
// Whether the search icon should be in dark mode or not. // Whether the search icon should be in dark mode or not.
@property(nonatomic, assign, getter=isIncognito) BOOL incognito; @property(nonatomic, assign, getter=isIncognito) BOOL incognito;
......
...@@ -38,6 +38,9 @@ ...@@ -38,6 +38,9 @@
// The icon for the search button. // The icon for the search button.
@property(nonatomic, strong) UIImage* searchIcon; @property(nonatomic, strong) UIImage* searchIcon;
// Whether the associated toolbar is in dark mode.
@property(nonatomic, assign) BOOL toolbarDarkMode;
@end @end
@implementation ToolbarMediator { @implementation ToolbarMediator {
...@@ -185,7 +188,7 @@ ...@@ -185,7 +188,7 @@
_incognito = incognito; _incognito = incognito;
if (self.searchIcon) { if (self.searchIcon) {
// If the searchEngine was already initialized, ask for the new image. // If the searchEngine was already initialized, ask for the new image.
[self searchEngineChanged]; [self updateSearchIcon];
} }
} }
...@@ -302,6 +305,26 @@ ...@@ -302,6 +305,26 @@
[self.consumer setShareMenuEnabled:shareMenuEnabled]; [self.consumer setShareMenuEnabled:shareMenuEnabled];
} }
// Updates the search icon in the toolbar. This depends on both the current
// search engine as well as the dark mode status of the associated toolbar.
- (void)updateSearchIcon {
SearchEngineIcon searchEngineIcon = SEARCH_ENGINE_ICON_OTHER;
if (self.templateURLService &&
self.templateURLService->GetDefaultSearchProvider() &&
self.templateURLService->GetDefaultSearchProvider()->GetEngineType(
self.templateURLService->search_terms_data()) ==
SEARCH_ENGINE_GOOGLE) {
searchEngineIcon = SEARCH_ENGINE_ICON_GOOGLE_SEARCH;
}
BOOL useDarkIcon = self.incognito || self.toolbarDarkMode;
UIImage* searchIcon =
ios::GetChromeBrowserProvider()
->GetBrandedImageProvider()
->GetToolbarSearchIcon(searchEngineIcon, useDarkIcon);
DCHECK(searchIcon);
[self.consumer setSearchIcon:searchIcon];
}
#pragma mark - BookmarkModelBridgeObserver #pragma mark - BookmarkModelBridgeObserver
// If an added or removed bookmark is the same as the current url, update the // If an added or removed bookmark is the same as the current url, update the
...@@ -338,27 +361,16 @@ ...@@ -338,27 +361,16 @@
#pragma mark - SearchEngineObserving #pragma mark - SearchEngineObserving
- (void)searchEngineChanged { - (void)searchEngineChanged {
SearchEngineIcon searchEngineIcon = SEARCH_ENGINE_ICON_OTHER; [self updateSearchIcon];
if (self.templateURLService && }
self.templateURLService->GetDefaultSearchProvider() &&
self.templateURLService->GetDefaultSearchProvider()->GetEngineType( #pragma mark - AdaptiveToolbarViewControllerDelegate
self.templateURLService->search_terms_data()) ==
SEARCH_ENGINE_GOOGLE) { - (void)userInterfaceStyleChangedForViewController:
searchEngineIcon = SEARCH_ENGINE_ICON_GOOGLE_SEARCH; (AdaptiveToolbarViewController*)viewController {
} self.toolbarDarkMode = viewController.traitCollection.userInterfaceStyle ==
BOOL useDarkIcon = self.incognito; UIUserInterfaceStyleDark;
// In iOS 13, incognito coloring overrides the userInterfaceStyle, so one [self updateSearchIcon];
// imageset holds both the regular image and the dark mode/incognito image.
// TODO(crbug.com/981889): After iOS 12 is removed, this can be cleaned up.
if (@available(iOS 13, *)) {
useDarkIcon = NO;
}
UIImage* searchIcon =
ios::GetChromeBrowserProvider()
->GetBrandedImageProvider()
->GetToolbarSearchIcon(searchEngineIcon, useDarkIcon);
DCHECK(searchIcon);
[self.consumer setSearchIcon:searchIcon];
} }
@end @end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment