Commit 263882a6 authored by Robbie Gibson's avatar Robbie Gibson Committed by Commit Bot

[iOS] Cleanup Search Widget, use shared utils

There is now a shared class in ios/chrome/common (somewhere extensions
can access) that does the same thing as CreateSameConstraints. We should
use that instead.

Also, we can get rid of unnecessary @synthesize and method prototypes.

Bug: 938317
Change-Id: I54b7a3b855015c377ebdbb8c0be2d0e47698a811
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1503417
Commit-Queue: Robbie Gibson <rkgibson@google.com>
Reviewed-by: default avatarOlivier Robin <olivierrobin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638064}
parent 048ecbe3
...@@ -50,12 +50,12 @@ source_set("search_widget") { ...@@ -50,12 +50,12 @@ source_set("search_widget") {
"copied_content_view.mm", "copied_content_view.mm",
"search_action_view.h", "search_action_view.h",
"search_action_view.mm", "search_action_view.mm",
"search_widget_constants.h",
"search_widget_constants.mm",
"search_widget_view.h", "search_widget_view.h",
"search_widget_view.mm", "search_widget_view.mm",
"search_widget_view_controller.h", "search_widget_view_controller.h",
"search_widget_view_controller.mm", "search_widget_view_controller.mm",
"ui_util.h",
"ui_util.mm",
] ]
deps = [ deps = [
......
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
#import <NotificationCenter/NotificationCenter.h> #import <NotificationCenter/NotificationCenter.h>
#include "base/logging.h" #include "base/logging.h"
#import "ios/chrome/search_widget_extension/ui_util.h" #import "ios/chrome/common/ui_util/constraints_ui_util.h"
#import "ios/chrome/search_widget_extension/search_widget_constants.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
...@@ -41,12 +42,6 @@ const CGFloat kURLButtonMargin = 10; ...@@ -41,12 +42,6 @@ const CGFloat kURLButtonMargin = 10;
@implementation CopiedContentView @implementation CopiedContentView
@synthesize copiedContentLabel = _copiedContentLabel;
@synthesize copiedText = _copiedText;
@synthesize openCopiedContentTitleLabel = _openCopiedContentTitleLabel;
@synthesize hairlineView = _hairlineView;
@synthesize copiedButtonView = _copiedButtonView;
- (instancetype)initWithActionTarget:(id)target - (instancetype)initWithActionTarget:(id)target
actionSelector:(SEL)actionSelector { actionSelector:(SEL)actionSelector {
DCHECK(target); DCHECK(target);
...@@ -71,8 +66,7 @@ const CGFloat kURLButtonMargin = 10; ...@@ -71,8 +66,7 @@ const CGFloat kURLButtonMargin = 10;
@[ primaryEffectView, secondaryEffectView ]) { @[ primaryEffectView, secondaryEffectView ]) {
[self addSubview:effectView]; [self addSubview:effectView];
effectView.translatesAutoresizingMaskIntoConstraints = NO; effectView.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint AddSameConstraints(self, effectView);
activateConstraints:ui_util::CreateSameConstraints(self, effectView)];
effectView.userInteractionEnabled = NO; effectView.userInteractionEnabled = NO;
} }
...@@ -109,26 +103,24 @@ const CGFloat kURLButtonMargin = 10; ...@@ -109,26 +103,24 @@ const CGFloat kURLButtonMargin = 10;
[_copiedButtonView.leadingAnchor [_copiedButtonView.leadingAnchor
constraintEqualToAnchor:self.leadingAnchor constraintEqualToAnchor:self.leadingAnchor
constant:ui_util::kContentMargin], constant:kContentMargin],
[_copiedButtonView.trailingAnchor [_copiedButtonView.trailingAnchor
constraintEqualToAnchor:self.trailingAnchor constraintEqualToAnchor:self.trailingAnchor
constant:-ui_util::kContentMargin], constant:-kContentMargin],
[_copiedButtonView.topAnchor [_copiedButtonView.topAnchor constraintEqualToAnchor:self.topAnchor
constraintEqualToAnchor:self.topAnchor constant:kContentMargin],
constant:ui_util::kContentMargin], [_copiedButtonView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor
[_copiedButtonView.bottomAnchor constant:-kContentMargin],
constraintEqualToAnchor:self.bottomAnchor
constant:-ui_util::kContentMargin],
[_openCopiedContentTitleLabel.topAnchor [_openCopiedContentTitleLabel.topAnchor
constraintEqualToAnchor:_copiedButtonView.topAnchor constraintEqualToAnchor:_copiedButtonView.topAnchor
constant:kURLButtonMargin], constant:kURLButtonMargin],
[_openCopiedContentTitleLabel.leadingAnchor [_openCopiedContentTitleLabel.leadingAnchor
constraintEqualToAnchor:_copiedButtonView.leadingAnchor constraintEqualToAnchor:_copiedButtonView.leadingAnchor
constant:ui_util::kContentMargin], constant:kContentMargin],
[_openCopiedContentTitleLabel.trailingAnchor [_openCopiedContentTitleLabel.trailingAnchor
constraintEqualToAnchor:_copiedButtonView.trailingAnchor constraintEqualToAnchor:_copiedButtonView.trailingAnchor
constant:-ui_util::kContentMargin], constant:-kContentMargin],
[_copiedContentLabel.topAnchor [_copiedContentLabel.topAnchor
constraintEqualToAnchor:_openCopiedContentTitleLabel.bottomAnchor], constraintEqualToAnchor:_openCopiedContentTitleLabel.bottomAnchor],
......
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
#import <NotificationCenter/NotificationCenter.h> #import <NotificationCenter/NotificationCenter.h>
#include "base/logging.h" #include "base/logging.h"
#import "ios/chrome/search_widget_extension/ui_util.h" #import "ios/chrome/common/ui_util/constraints_ui_util.h"
#import "ios/chrome/search_widget_extension/search_widget_constants.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
...@@ -45,8 +46,7 @@ const CGFloat kIconSize = 35; ...@@ -45,8 +46,7 @@ const CGFloat kIconSize = 35;
[self addSubview:effectView]; [self addSubview:effectView];
effectView.translatesAutoresizingMaskIntoConstraints = NO; effectView.translatesAutoresizingMaskIntoConstraints = NO;
effectView.userInteractionEnabled = NO; effectView.userInteractionEnabled = NO;
[NSLayoutConstraint AddSameConstraints(self, effectView);
activateConstraints:ui_util::CreateSameConstraints(self, effectView)];
} }
UIView* circleView = [[UIView alloc] initWithFrame:CGRectZero]; UIView* circleView = [[UIView alloc] initWithFrame:CGRectZero];
...@@ -66,12 +66,11 @@ const CGFloat kIconSize = 35; ...@@ -66,12 +66,11 @@ const CGFloat kIconSize = 35;
UIStackView* stack = [[UIStackView alloc] UIStackView* stack = [[UIStackView alloc]
initWithArrangedSubviews:@[ circleView, labelView ]]; initWithArrangedSubviews:@[ circleView, labelView ]];
stack.axis = UILayoutConstraintAxisVertical; stack.axis = UILayoutConstraintAxisVertical;
stack.spacing = ui_util::kIconSpacing; stack.spacing = kIconSpacing;
stack.alignment = UIStackViewAlignmentCenter; stack.alignment = UIStackViewAlignmentCenter;
stack.translatesAutoresizingMaskIntoConstraints = NO; stack.translatesAutoresizingMaskIntoConstraints = NO;
[secondaryEffectView.contentView addSubview:stack]; [secondaryEffectView.contentView addSubview:stack];
[NSLayoutConstraint activateConstraints:ui_util::CreateSameConstraints( AddSameConstraints(secondaryEffectView, stack);
secondaryEffectView, stack)];
UIImage* iconImage = [UIImage imageNamed:imageName]; UIImage* iconImage = [UIImage imageNamed:imageName];
iconImage = iconImage =
[iconImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; [iconImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
......
// Copyright 2017 The Chromium Authors. All rights reserved. // Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef IOS_CHROME_SEARCH_WIDGET_EXTENSION_UI_UTIL_H_ #ifndef IOS_CHROME_SEARCH_WIDGET_EXTENSION_SEARCH_WIDGET_CONSTANTS_H_
#define IOS_CHROME_SEARCH_WIDGET_EXTENSION_UI_UTIL_H_ #define IOS_CHROME_SEARCH_WIDGET_EXTENSION_SEARCH_WIDGET_CONSTANTS_H_
namespace ui_util {
// The spacing to use between action icons. // The spacing to use between action icons.
extern CGFloat const kIconSpacing; extern CGFloat const kIconSpacing;
...@@ -13,11 +11,4 @@ extern CGFloat const kIconSpacing; ...@@ -13,11 +11,4 @@ extern CGFloat const kIconSpacing;
// The spacing between content and edges. // The spacing between content and edges.
extern CGFloat const kContentMargin; extern CGFloat const kContentMargin;
// Returns constraints to make two views' size and center equal by pinning #endif // IOS_CHROME_SEARCH_WIDGET_EXTENSION_SEARCH_WIDGET_CONSTANTS_H_
// leading, trailing, top and bottom anchors.
NSArray<NSLayoutConstraint*>* CreateSameConstraints(UIView* view1,
UIView* view2);
} // namespace ui_util
#endif // IOS_CHROME_SEARCH_WIDGET_EXTENSION_UI_UTIL_H_
// Copyright 2017 The Chromium Authors. All rights reserved. // Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#include "ios/chrome/search_widget_extension/ui_util.h" #include "ios/chrome/search_widget_extension/search_widget_constants.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
namespace ui_util {
CGFloat const kIconSpacing = 5; CGFloat const kIconSpacing = 5;
CGFloat const kContentMargin = 12; CGFloat const kContentMargin = 12;
NSArray<NSLayoutConstraint*>* CreateSameConstraints(UIView* view1,
UIView* view2) {
return @[
[view1.leadingAnchor constraintEqualToAnchor:view2.leadingAnchor],
[view1.trailingAnchor constraintEqualToAnchor:view2.trailingAnchor],
[view1.topAnchor constraintEqualToAnchor:view2.topAnchor],
[view1.bottomAnchor constraintEqualToAnchor:view2.bottomAnchor]
];
}
} // namespace ui_util
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include "base/logging.h" #include "base/logging.h"
#import "ios/chrome/search_widget_extension/copied_content_view.h" #import "ios/chrome/search_widget_extension/copied_content_view.h"
#import "ios/chrome/search_widget_extension/search_action_view.h" #import "ios/chrome/search_widget_extension/search_action_view.h"
#import "ios/chrome/search_widget_extension/ui_util.h" #import "ios/chrome/search_widget_extension/search_widget_constants.h"
#if !defined(__has_feature) || !__has_feature(objc_arc) #if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support." #error "This file requires ARC support."
...@@ -124,7 +124,7 @@ const CGFloat kMaxContentSize = 421; ...@@ -124,7 +124,7 @@ const CGFloat kMaxContentSize = 421;
[self.actionsContent [self.actionsContent
systemLayoutSizeFittingSize:UILayoutFittingCompressedSize] systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]
.height; .height;
return height + 2 * ui_util::kContentMargin; return height + 2 * kContentMargin;
} }
- (CGFloat)copiedURLSectionHeight { - (CGFloat)copiedURLSectionHeight {
...@@ -169,7 +169,7 @@ const CGFloat kMaxContentSize = 421; ...@@ -169,7 +169,7 @@ const CGFloat kMaxContentSize = 421;
actionsContentStack.axis = UILayoutConstraintAxisHorizontal; actionsContentStack.axis = UILayoutConstraintAxisHorizontal;
actionsContentStack.alignment = UIStackViewAlignmentTop; actionsContentStack.alignment = UIStackViewAlignmentTop;
actionsContentStack.distribution = UIStackViewDistributionFillEqually; actionsContentStack.distribution = UIStackViewDistributionFillEqually;
actionsContentStack.spacing = ui_util::kIconSpacing; actionsContentStack.spacing = kIconSpacing;
actionsContentStack.layoutMargins = UIEdgeInsetsZero; actionsContentStack.layoutMargins = UIEdgeInsetsZero;
actionsContentStack.layoutMarginsRelativeArrangement = YES; actionsContentStack.layoutMarginsRelativeArrangement = YES;
actionsContentStack.translatesAutoresizingMaskIntoConstraints = NO; actionsContentStack.translatesAutoresizingMaskIntoConstraints = NO;
...@@ -186,12 +186,12 @@ const CGFloat kMaxContentSize = 421; ...@@ -186,12 +186,12 @@ const CGFloat kMaxContentSize = 421;
NSLayoutConstraint* actionsLeadingConstraint = NSLayoutConstraint* actionsLeadingConstraint =
[self.actionsContent.leadingAnchor [self.actionsContent.leadingAnchor
constraintEqualToAnchor:self.actionsSection.leadingAnchor constraintEqualToAnchor:self.actionsSection.leadingAnchor
constant:ui_util::kContentMargin]; constant:kContentMargin];
actionsLeadingConstraint.priority = UILayoutPriorityDefaultHigh; actionsLeadingConstraint.priority = UILayoutPriorityDefaultHigh;
NSLayoutConstraint* actionsTrailingConstraint = NSLayoutConstraint* actionsTrailingConstraint =
[self.actionsContent.trailingAnchor [self.actionsContent.trailingAnchor
constraintEqualToAnchor:self.actionsSection.trailingAnchor constraintEqualToAnchor:self.actionsSection.trailingAnchor
constant:-ui_util::kContentMargin]; constant:-kContentMargin];
actionsTrailingConstraint.priority = UILayoutPriorityDefaultHigh; actionsTrailingConstraint.priority = UILayoutPriorityDefaultHigh;
[NSLayoutConstraint activateConstraints:@[ [NSLayoutConstraint activateConstraints:@[
......
...@@ -39,42 +39,10 @@ NSString* const kXCallbackURLHost = @"x-callback-url"; ...@@ -39,42 +39,10 @@ NSString* const kXCallbackURLHost = @"x-callback-url";
@property(nonatomic, assign) BOOL supportsSearchByImage; @property(nonatomic, assign) BOOL supportsSearchByImage;
@property(nonatomic, readonly) BOOL copiedContentBehaviorEnabled; @property(nonatomic, readonly) BOOL copiedContentBehaviorEnabled;
// Updates the widget with latest data from the clipboard. Returns whether any
// visual updates occurred.
- (BOOL)updateWidget;
// Opens the main application with the given |command|.
- (void)openAppWithCommand:(NSString*)command;
// Opens the main application with the given |command|, |text|, and |image|.
- (void)openAppWithCommand:(NSString*)command
text:(NSString*)text
imageData:(NSData*)imageData;
// Returns the dictionary of commands to pass via user defaults to open the main
// application for a given |command| and optional |text| and |image|.
+ (NSDictionary*)dictForCommand:(NSString*)command
text:(NSString*)text
imageData:(NSData*)imageData;
// Register a display of the widget in the app_group NSUserDefaults.
// Metrics on the widget usage will be sent (if enabled) on the next Chrome
// startup.
- (void)registerWidgetDisplay;
// Sets the copied content type. |copiedText| should be provided if the content
// type requires textual data, otherwise it should be nil. Likewise,
// |copiedImage| should be provided if the content type requires image data.
// Also saves the data and returns YES if the screen needs updating and NO
// otherwise.
- (BOOL)setCopiedContentType:(CopiedContentType)type
copiedText:(NSString*)copiedText
copiedImage:(UIImage*)copiedImage;
@end @end
@implementation SearchWidgetViewController @implementation SearchWidgetViewController
@synthesize widgetView = _widgetView;
@synthesize copiedText = _copiedText;
@synthesize copiedContentType = _copiedContentType;
@synthesize clipboardRecentContent = _clipboardRecentContent;
- (instancetype)init { - (instancetype)init {
self = [super init]; self = [super init];
if (self) { if (self) {
...@@ -132,6 +100,9 @@ NSString* const kXCallbackURLHost = @"x-callback-url"; ...@@ -132,6 +100,9 @@ NSString* const kXCallbackURLHost = @"x-callback-url";
completionHandler([self updateWidget] ? NCUpdateResultNewData completionHandler([self updateWidget] ? NCUpdateResultNewData
: NCUpdateResultNoData); : NCUpdateResultNoData);
} }
// Updates the widget with latest data from the clipboard. Returns whether any
// visual updates occurred.
- (BOOL)updateWidget { - (BOOL)updateWidget {
NSUserDefaults* sharedDefaults = app_group::GetGroupUserDefaults(); NSUserDefaults* sharedDefaults = app_group::GetGroupUserDefaults();
NSString* fieldTrialKey = NSString* fieldTrialKey =
...@@ -271,10 +242,14 @@ NSString* const kXCallbackURLHost = @"x-callback-url"; ...@@ -271,10 +242,14 @@ NSString* const kXCallbackURLHost = @"x-callback-url";
#pragma mark - internal #pragma mark - internal
// Opens the main application with the given |command|.
- (void)openAppWithCommand:(NSString*)command { - (void)openAppWithCommand:(NSString*)command {
return [self openAppWithCommand:command text:nil imageData:nil]; return [self openAppWithCommand:command text:nil imageData:nil];
} }
// Register a display of the widget in the app_group NSUserDefaults.
// Metrics on the widget usage will be sent (if enabled) on the next Chrome
// startup.
- (void)registerWidgetDisplay { - (void)registerWidgetDisplay {
NSUserDefaults* sharedDefaults = app_group::GetGroupUserDefaults(); NSUserDefaults* sharedDefaults = app_group::GetGroupUserDefaults();
NSInteger numberOfDisplay = NSInteger numberOfDisplay =
...@@ -283,6 +258,7 @@ NSString* const kXCallbackURLHost = @"x-callback-url"; ...@@ -283,6 +258,7 @@ NSString* const kXCallbackURLHost = @"x-callback-url";
forKey:app_group::kSearchExtensionDisplayCount]; forKey:app_group::kSearchExtensionDisplayCount];
} }
// Opens the main application with the given |command|, |text|, and |image|.
- (void)openAppWithCommand:(NSString*)command - (void)openAppWithCommand:(NSString*)command
text:(NSString*)text text:(NSString*)text
imageData:(NSData*)imageData { imageData:(NSData*)imageData {
...@@ -312,6 +288,8 @@ NSString* const kXCallbackURLHost = @"x-callback-url"; ...@@ -312,6 +288,8 @@ NSString* const kXCallbackURLHost = @"x-callback-url";
[self.extensionContext openURL:openURL completionHandler:nil]; [self.extensionContext openURL:openURL completionHandler:nil];
} }
// Returns the dictionary of commands to pass via user defaults to open the main
// application for a given |command| and optional |text| and |image|.
+ (NSDictionary*)dictForCommand:(NSString*)command + (NSDictionary*)dictForCommand:(NSString*)command
text:(NSString*)text text:(NSString*)text
imageData:(NSData*)imageData { imageData:(NSData*)imageData {
...@@ -343,6 +321,11 @@ NSString* const kXCallbackURLHost = @"x-callback-url"; ...@@ -343,6 +321,11 @@ NSString* const kXCallbackURLHost = @"x-callback-url";
return baseKeys; return baseKeys;
} }
// Sets the copied content type. |copiedText| should be provided if the content
// type requires textual data, otherwise it should be nil. Likewise,
// |copiedImage| should be provided if the content type requires image data.
// Also saves the data and returns YES if the screen needs updating and NO
// otherwise.
- (BOOL)setCopiedContentType:(CopiedContentType)type - (BOOL)setCopiedContentType:(CopiedContentType)type
copiedText:(NSString*)copiedText copiedText:(NSString*)copiedText
copiedImage:(UIImage*)copiedImage { copiedImage:(UIImage*)copiedImage {
......
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