123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545 |
- // Copyright 2019 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #import "ios/showcase/omnibox_popup/fake_autocomplete_suggestion.h"
- #import "ios/chrome/browser/net/crurl.h"
- #import "ios/chrome/browser/ui/omnibox/omnibox_suggestion_icon_util.h"
- #import "ios/chrome/browser/ui/omnibox/popup/simple_omnibox_icon.h"
- #import "url/gurl.h"
- #if !defined(__has_feature) || !__has_feature(objc_arc)
- #error "This file requires ARC support."
- #endif
- namespace {
- // Spacer attributed string for dividing parts of an autocomplete suggestion's
- // text and detail text.
- NSAttributedString* spacer() {
- return [[NSAttributedString alloc] initWithString:@" "];
- }
- // Standard attributed string for the text part of a suggestion.
- NSAttributedString* textString(NSString* text) {
- return [[NSAttributedString alloc]
- initWithString:text
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:17],
- NSForegroundColorAttributeName : [UIColor blackColor],
- }];
- }
- // Standard attributed string for the detail part of a suggestion.
- NSAttributedString* detailTextString(NSString* detailText) {
- return [[NSAttributedString alloc]
- initWithString:detailText
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:12],
- NSForegroundColorAttributeName : [UIColor colorWithWhite:0
- alpha:0.41],
- }];
- }
- // Main text for an autocomplete suggestion representing weather
- NSAttributedString* weatherText() {
- return [[NSAttributedString alloc]
- initWithString:@"weather"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:16],
- }];
- }
- // Detail text for an autocomplete suggestion representing weather
- NSAttributedString* weatherDetailText() {
- NSAttributedString* number = [[NSAttributedString alloc]
- initWithString:@"18"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:24],
- NSForegroundColorAttributeName : [UIColor grayColor],
- }];
- NSAttributedString* degreeSymbol = [[NSAttributedString alloc]
- initWithString:@"°C"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:12],
- NSBaselineOffsetAttributeName : @10.0f,
- NSForegroundColorAttributeName : [UIColor grayColor],
- }];
- NSAttributedString* date = [[NSAttributedString alloc]
- initWithString:@"ven."
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:12],
- NSForegroundColorAttributeName : [UIColor grayColor],
- }];
- NSMutableAttributedString* answer =
- [[NSMutableAttributedString alloc] initWithAttributedString:number];
- [answer appendAttributedString:degreeSymbol];
- [answer appendAttributedString:spacer()];
- [answer appendAttributedString:date];
- return [answer copy];
- }
- // Main text for an autocomplete suggestion representing stock price
- NSAttributedString* stockText() {
- NSAttributedString* search = [[NSAttributedString alloc]
- initWithString:@"goog stock"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:16],
- }];
- NSAttributedString* priceSource = [[NSAttributedString alloc]
- initWithString:@"GOOG (NASDAQ), 13:18 UTC−4"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:12],
- NSForegroundColorAttributeName : [UIColor grayColor],
- }];
- NSMutableAttributedString* answer =
- [[NSMutableAttributedString alloc] initWithAttributedString:search];
- [answer appendAttributedString:spacer()];
- [answer appendAttributedString:priceSource];
- return [answer copy];
- }
- // Detail text for an autocomplete suggestion representing stock price
- NSAttributedString* stockDetailText() {
- NSAttributedString* price = [[NSAttributedString alloc]
- initWithString:@"1 209,29"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:24],
- NSForegroundColorAttributeName : [UIColor grayColor],
- }];
- NSAttributedString* priceChange = [[NSAttributedString alloc]
- initWithString:@"-22,25 (-1,81%)"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:16],
- NSForegroundColorAttributeName : [UIColor colorWithRed:197 / 255.0
- green:57 / 255.0
- blue:41 / 255.0
- alpha:1.0],
- }];
- NSMutableAttributedString* answer =
- [[NSMutableAttributedString alloc] initWithAttributedString:price];
- [answer appendAttributedString:spacer()];
- [answer appendAttributedString:priceChange];
- return [answer copy];
- }
- // Main text for an autocomplete suggestion representing a word definition
- NSAttributedString* definitionText() {
- NSAttributedString* searchText = [[NSAttributedString alloc]
- initWithString:@"define government"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:16],
- }];
- NSAttributedString* pronunciation = [[NSAttributedString alloc]
- initWithString:@"• /ˈɡʌv(ə)nˌm(ə)nt/"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:14],
- NSForegroundColorAttributeName : [UIColor grayColor],
- }];
- NSMutableAttributedString* answer =
- [[NSMutableAttributedString alloc] initWithAttributedString:searchText];
- [answer appendAttributedString:spacer()];
- [answer appendAttributedString:pronunciation];
- return [answer copy];
- }
- // Detail text for an autocomplete suggestion representing a word definition
- NSAttributedString* definitionDetailText() {
- return [[NSAttributedString alloc]
- initWithString:@"the group of people with the authority to govern a "
- @"country or state; a particular ministry in office. "
- @"Let's expand this definition to get to three lines also."
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:14],
- NSForegroundColorAttributeName : [UIColor grayColor],
- }];
- }
- NSAttributedString* sunriseText() {
- return [[NSAttributedString alloc]
- initWithString:@"sunrise in paris"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:16],
- }];
- }
- NSAttributedString* sunriseDetailText() {
- return [[NSAttributedString alloc]
- initWithString:@"06:35"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:24],
- NSForegroundColorAttributeName : [UIColor grayColor],
- }];
- }
- NSAttributedString* knowledgeText() {
- return [[NSAttributedString alloc]
- initWithString:@"how high is mount everest"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:16],
- }];
- }
- NSAttributedString* knowledgeDetailText() {
- return [[NSAttributedString alloc]
- initWithString:@"8 848 m"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:24],
- NSForegroundColorAttributeName : [UIColor grayColor],
- }];
- }
- NSAttributedString* sportsText() {
- return [[NSAttributedString alloc]
- initWithString:@"boston celtics"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:16],
- }];
- }
- NSAttributedString* sportsDetailText() {
- return [[NSAttributedString alloc]
- initWithString:@" contre Pacers 30 mars à 00:00 UTC+1"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:14],
- NSForegroundColorAttributeName : [UIColor grayColor],
- }];
- }
- NSAttributedString* whenIsText() {
- return [[NSAttributedString alloc]
- initWithString:@"when is bastille day"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:16],
- }];
- }
- NSAttributedString* whenIsDetailText() {
- return [[NSAttributedString alloc]
- initWithString:@"dimanche 14 juillet 2019"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:20],
- NSForegroundColorAttributeName : [UIColor grayColor],
- }];
- }
- NSAttributedString* currencyText() {
- return [[NSAttributedString alloc]
- initWithString:@"100 usd"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:16],
- }];
- }
- NSAttributedString* currencyDetailText() {
- return [[NSAttributedString alloc]
- initWithString:@"100 Dollar américain = 89.01 Euro"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:20],
- NSForegroundColorAttributeName : [UIColor grayColor],
- }];
- }
- NSAttributedString* translateText() {
- return [[NSAttributedString alloc]
- initWithString:@"bonjour in chinese"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:16],
- }];
- }
- NSAttributedString* translateDetailText() {
- return [[NSAttributedString alloc]
- initWithString:@"你好 (Chinois (simplifié))"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:20],
- NSForegroundColorAttributeName : [UIColor grayColor],
- }];
- }
- NSAttributedString* calculatorText() {
- return [[NSAttributedString alloc]
- initWithString:@"= 3.46410162"
- attributes:@{
- NSFontAttributeName : [UIFont systemFontOfSize:17
- weight:UIFontWeightMedium],
- NSForegroundColorAttributeName : [UIColor blackColor],
- }];
- }
- } // namespace
- @interface FakeAutocompleteSuggestion ()
- @property(nonatomic, copy, readwrite) NSString* commonPrefix;
- @end
- @implementation FakeAutocompleteSuggestion
- @synthesize isClipboardMatch = _isClipboardMatch;
- - (instancetype)init {
- self = [super init];
- if (self) {
- _isURL = YES;
- _text = [[NSAttributedString alloc] initWithString:@""];
- _detailText = [[NSAttributedString alloc] initWithString:@""];
- _numberOfLines = 1;
- _suggestionTypeIcon =
- [[UIImage imageNamed:@"omnibox_completion_default_favicon"]
- imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
- _icon = [[SimpleOmniboxIcon alloc] init];
- _isClipboardMatch = NO;
- }
- return self;
- }
- // In the new popup, this field is not used. Instead, the icon field is used.
- - (GURL)imageURL {
- return GURL();
- }
- // In the new popup, this field is not used. Instead, the icon field is used.
- - (GURL)faviconPageURL {
- return GURL();
- }
- // In the new popup, this field is not used. Instead, the icon field, which
- // always has an image, is used.
- - (BOOL)hasImage {
- return self.imageURL.is_valid();
- }
- + (instancetype)simpleSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = textString(@"Simple suggestion");
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeSuggestionIcon
- suggestionIconType:SEARCH
- isAnswer:NO
- imageURL:[[CrURL alloc] initWithGURL:GURL()]];
- return suggestion;
- }
- + (instancetype)suggestionWithDetail {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = textString(@"Suggestion with detail");
- suggestion.detailText = detailTextString(@"Detail");
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeSuggestionIcon
- suggestionIconType:SEARCH
- isAnswer:NO
- imageURL:[[CrURL alloc] initWithGURL:GURL()]];
- return suggestion;
- }
- + (instancetype)clippingSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text =
- textString(@"Suggestion with text that clips because it is very long "
- @"and extends off the right end of the screen");
- suggestion.detailText = detailTextString(
- @"Detail about the suggestion that also clips because it is too long "
- @"for the screen and extends off of the right edge.");
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeSuggestionIcon
- suggestionIconType:SEARCH
- isAnswer:NO
- imageURL:[[CrURL alloc] initWithGURL:GURL()]];
- return suggestion;
- }
- + (instancetype)appendableSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = textString(@"Appendable suggestion");
- suggestion.isAppendable = true;
- return suggestion;
- }
- + (instancetype)otherTabSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = textString(@"Other tab suggestion");
- suggestion.isTabMatch = true;
- return suggestion;
- }
- + (instancetype)deletableSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = textString(@"Deletable suggestion");
- suggestion.supportsDeletion = YES;
- return suggestion;
- }
- + (instancetype)weatherSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = weatherText();
- suggestion.hasAnswer = YES;
- suggestion.detailText = weatherDetailText();
- // The image currently doesn't display because there is no fake
- // Image Retriever, but leaving this here in case this is ever necessary.
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeImage
- suggestionIconType:DEFAULT_FAVICON
- isAnswer:NO
- imageURL:[[CrURL alloc]
- initWithGURL:GURL("https://ssl.gstatic.com/onebox/"
- "weather/128/sunny.png")]];
- return suggestion;
- }
- + (instancetype)stockSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = stockText();
- suggestion.hasAnswer = YES;
- suggestion.detailText = stockDetailText();
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeSuggestionIcon
- suggestionIconType:STOCK
- isAnswer:NO
- imageURL:[[CrURL alloc] initWithGURL:GURL()]];
- return suggestion;
- }
- + (instancetype)definitionSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = definitionText();
- suggestion.numberOfLines = 3;
- suggestion.hasAnswer = YES;
- suggestion.detailText = definitionDetailText();
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeSuggestionIcon
- suggestionIconType:DICTIONARY
- isAnswer:NO
- imageURL:[[CrURL alloc] initWithGURL:GURL()]];
- return suggestion;
- }
- + (instancetype)sunriseSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = sunriseText();
- suggestion.hasAnswer = YES;
- suggestion.detailText = sunriseDetailText();
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeSuggestionIcon
- suggestionIconType:SUNRISE
- isAnswer:NO
- imageURL:[[CrURL alloc] initWithGURL:GURL()]];
- return suggestion;
- }
- + (instancetype)knowledgeSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = knowledgeText();
- suggestion.hasAnswer = YES;
- suggestion.detailText = knowledgeDetailText();
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeSuggestionIcon
- suggestionIconType:FALLBACK_ANSWER
- isAnswer:NO
- imageURL:[[CrURL alloc] initWithGURL:GURL()]];
- return suggestion;
- }
- + (instancetype)sportsSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = sportsText();
- suggestion.hasAnswer = YES;
- suggestion.detailText = sportsDetailText();
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeSuggestionIcon
- suggestionIconType:FALLBACK_ANSWER
- isAnswer:NO
- imageURL:[[CrURL alloc] initWithGURL:GURL()]];
- return suggestion;
- }
- + (instancetype)whenIsSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = whenIsText();
- suggestion.hasAnswer = YES;
- suggestion.detailText = whenIsDetailText();
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeSuggestionIcon
- suggestionIconType:WHEN_IS
- isAnswer:NO
- imageURL:[[CrURL alloc] initWithGURL:GURL()]];
- return suggestion;
- }
- + (instancetype)currencySuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = currencyText();
- suggestion.hasAnswer = YES;
- suggestion.detailText = currencyDetailText();
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeSuggestionIcon
- suggestionIconType:CONVERSION
- isAnswer:NO
- imageURL:[[CrURL alloc] initWithGURL:GURL()]];
- return suggestion;
- }
- + (instancetype)translateSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = translateText();
- suggestion.hasAnswer = YES;
- suggestion.detailText = translateDetailText();
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeSuggestionIcon
- suggestionIconType:TRANSLATION
- isAnswer:NO
- imageURL:[[CrURL alloc] initWithGURL:GURL()]];
- return suggestion;
- }
- + (instancetype)calculatorSuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = calculatorText();
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeSuggestionIcon
- suggestionIconType:CALCULATOR
- isAnswer:NO
- imageURL:[[CrURL alloc] initWithGURL:GURL()]];
- return suggestion;
- }
- + (instancetype)richEntitySuggestion {
- FakeAutocompleteSuggestion* suggestion =
- [[FakeAutocompleteSuggestion alloc] init];
- suggestion.text = textString(@"Avengers : Endgame");
- suggestion.detailText = detailTextString(@"Film (2019)");
- // The image currently doesn't display because there is no fake
- // Image Retriever, but leaving this here in case this is ever necessary.
- GURL imageURL = GURL("https://encrypted-tbn0.gstatic.com/"
- "images?q=tbn:ANd9GcRl35jshKCRWt76yUSKh5r0_"
- "BRbWuSU1uZOCGnzq95nJ8yXUg913LciCgz-s3reyfACsrAAYg");
- suggestion.icon = [[SimpleOmniboxIcon alloc]
- initWithIconType:OmniboxIconTypeImage
- suggestionIconType:SEARCH
- isAnswer:NO
- imageURL:[[CrURL alloc] initWithGURL:imageURL]];
- return suggestion;
- }
- @end
|