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 @@
self.mediator.webStateList = self.webStateList;
self.mediator.bookmarkModel =
ios::BookmarkModelFactory::GetForBrowserState(self.browserState);
self.viewController.adaptiveToolbarViewControllerDelegate = self.mediator;
}
- (void)stop {
......
......@@ -12,12 +12,22 @@
#import "ios/chrome/browser/ui/toolbar/buttons/toolbar_type.h"
#import "ios/chrome/browser/ui/toolbar/toolbar_consumer.h"
@class AdaptiveToolbarViewController;
@protocol ApplicationCommands;
@protocol BrowserCommands;
@protocol PopupMenuLongPressDelegate;
@class ToolbarButtonFactory;
@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
// class of the different implementation (primary or secondary).
// This class and its subclasses are constraining some named layout guides to
......@@ -36,6 +46,9 @@
@property(nonatomic, weak) id<ApplicationCommands, BrowserCommands> dispatcher;
// Delegate for the long press gesture recognizer triggering popup menu.
@property(nonatomic, weak) id<PopupMenuLongPressDelegate> longPressDelegate;
// Dark mode delegate for this toolbar.
@property(nonatomic, weak) id<AdaptiveToolbarViewControllerDelegate>
adaptiveToolbarViewControllerDelegate;
// Returns the tools menu button.
- (ToolbarToolsMenuButton*)toolsMenuButton;
......
......@@ -98,11 +98,16 @@ const CGFloat kTabGridAnimationsTotalDuration = 0.5;
[self addLongPressGestureToView:self.view.toolsMenuButton];
[self updateLayoutBasedOnTraitCollection];
[self.adaptiveToolbarViewControllerDelegate
userInterfaceStyleChangedForViewController:self];
}
- (void)traitCollectionDidChange:(UITraitCollection*)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
[self updateLayoutBasedOnTraitCollection];
[self.adaptiveToolbarViewControllerDelegate
userInterfaceStyleChangedForViewController:self];
}
- (void)viewDidLayoutSubviews {
......
......@@ -7,20 +7,21 @@
#import <Foundation/Foundation.h>
#import "ios/chrome/browser/ui/toolbar/adaptive_toolbar_view_controller.h"
namespace bookmarks {
class BookmarkModel;
}
namespace web {
class WebState;
}
class TemplateURLService;
@protocol ToolbarConsumer;
class WebStateList;
// A mediator object that provides the relevant properties of a web state
// to a consumer.
@interface ToolbarMediator : NSObject
@interface ToolbarMediator : NSObject <AdaptiveToolbarViewControllerDelegate>
// Whether the search icon should be in dark mode or not.
@property(nonatomic, assign, getter=isIncognito) BOOL incognito;
......
......@@ -38,6 +38,9 @@
// The icon for the search button.
@property(nonatomic, strong) UIImage* searchIcon;
// Whether the associated toolbar is in dark mode.
@property(nonatomic, assign) BOOL toolbarDarkMode;
@end
@implementation ToolbarMediator {
......@@ -185,7 +188,7 @@
_incognito = incognito;
if (self.searchIcon) {
// If the searchEngine was already initialized, ask for the new image.
[self searchEngineChanged];
[self updateSearchIcon];
}
}
......@@ -302,6 +305,26 @@
[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
// If an added or removed bookmark is the same as the current url, update the
......@@ -338,27 +361,16 @@
#pragma mark - SearchEngineObserving
- (void)searchEngineChanged {
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;
// In iOS 13, incognito coloring overrides the userInterfaceStyle, so one
// 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];
[self updateSearchIcon];
}
#pragma mark - AdaptiveToolbarViewControllerDelegate
- (void)userInterfaceStyleChangedForViewController:
(AdaptiveToolbarViewController*)viewController {
self.toolbarDarkMode = viewController.traitCollection.userInterfaceStyle ==
UIUserInterfaceStyleDark;
[self updateSearchIcon];
}
@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