Changeset 262271 in webkit


Ignore:
Timestamp:
May 28, 2020 4:06:48 PM (4 years ago)
Author:
Jonathan Bedard
Message:

watchOS: Upstream WKTextInputListViewController
https://bugs.webkit.org/show_bug.cgi?id=212495
<rdar://problem/63733949>

Reviewed by Wenson Hsieh.

  • UIProcess/ios/forms/WKTextInputListViewController.h: Moved from WebKitAdditions.
  • UIProcess/ios/forms/WKTextInputListViewController.mm: Ditto.
Location:
trunk/Source/WebKit
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r262268 r262271  
     12020-05-28  Jonathan Bedard  <jbedard@apple.com>
     2
     3        watchOS: Upstream WKTextInputListViewController
     4        https://bugs.webkit.org/show_bug.cgi?id=212495
     5        <rdar://problem/63733949>
     6
     7        Reviewed by Wenson Hsieh.
     8
     9        * UIProcess/ios/forms/WKTextInputListViewController.h: Moved from WebKitAdditions.
     10        * UIProcess/ios/forms/WKTextInputListViewController.mm: Ditto.
     11
    1122020-05-28  Simon Fraser  <simon.fraser@apple.com>
    213
  • trunk/Source/WebKit/UIProcess/ios/forms/WKTextInputListViewController.h

    r229745 r262271  
    11/*
    2  * Copyright (C) 2018 Apple Inc. All rights reserved.
     2 * Copyright (C) 2020 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    2626#pragma once
    2727
    28 #if USE(APPLE_INTERNAL_SDK)
    29 #import <WebKitAdditions/WKTextInputListViewControllerAdditions.h>
    30 #endif
     28#if PLATFORM(WATCHOS)
     29
     30#import "WKQuickboardListViewController.h"
     31
     32typedef NS_ENUM(NSInteger, WKNumberPadInputMode) {
     33    WKNumberPadInputModeNone,
     34    WKNumberPadInputModeNumbersAndSymbols,
     35    WKNumberPadInputModeTelephone,
     36    WKNumberPadInputModeNumbersOnly
     37};
     38
     39@class WKTextInputListViewController;
     40
     41@protocol WKTextInputListViewControllerDelegate <WKQuickboardViewControllerDelegate>
     42
     43- (WKNumberPadInputMode)numericInputModeForListViewController:(WKTextInputListViewController *)controller;
     44- (NSString *)textContentTypeForListViewController:(WKTextInputListViewController *)controller;
     45- (NSArray<UITextSuggestion *> *)textSuggestionsForListViewController:(WKTextInputListViewController *)controller;
     46- (void)listViewController:(WKTextInputListViewController *)controller didSelectTextSuggestion:(UITextSuggestion *)suggestion;
     47- (BOOL)allowsDictationInputForListViewController:(PUICQuickboardViewController *)controller;
     48
     49@end
     50
     51@interface WKTextInputListViewController : WKQuickboardListViewController
     52
     53- (instancetype)initWithDelegate:(id <WKTextInputListViewControllerDelegate>)delegate NS_DESIGNATED_INITIALIZER;
     54- (void)reloadTextSuggestions;
     55
     56@property (nonatomic, weak) id <WKTextInputListViewControllerDelegate> delegate;
     57
     58@end
     59
     60@interface WKTextInputListViewController (Testing)
     61
     62- (void)enterText:(NSString *)text;
     63
     64@end
     65
     66#endif // PLATFORM(WATCHOS)
  • trunk/Source/WebKit/UIProcess/ios/forms/WKTextInputListViewController.mm

    r229745 r262271  
    11/*
    2  * Copyright (C) 2018 Apple Inc. All rights reserved.
     2 * Copyright (C) 2020 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    2727#import "WKTextInputListViewController.h"
    2828
    29 #if USE(APPLE_INTERNAL_SDK)
    30 #import <WebKitAdditions/WKTextInputListViewControllerAdditions.mm>
     29#if PLATFORM(WATCHOS)
     30
     31#import "WKNumberPadViewController.h"
     32#import <PepperUICore/PUICQuickboardArouetViewController.h>
     33#import <PepperUICore/PUICQuickboardListViewControllerSubclass.h>
     34#import <PepperUICore/PUICQuickboardListViewSpecs.h>
     35#import <PepperUICore/PUICResources.h>
     36#import <PepperUICore/PUICTableView.h>
     37#import <PepperUICore/PUICTableViewCell.h>
     38#import <wtf/RetainPtr.h>
     39
     40#if HAVE(QUICKBOARD_COLLECTION_VIEWS)
     41#import <PepperUICore/PUICQuickboardListCollectionViewItemCell.h>
    3142#endif
     43
     44static const CGFloat textSuggestionButtonHeight = 44;
     45static const CGFloat textSuggestionLabelHorizontalMargin = 9;
     46static NSString *textSuggestionCellReuseIdentifier = @"WebKitQuickboardTextSuggestionCell";
     47
     48@implementation WKTextInputListViewController {
     49    RetainPtr<WKNumberPadViewController> _numberPadViewController;
     50}
     51
     52@dynamic delegate;
     53
     54- (instancetype)initWithDelegate:(id <WKTextInputListViewControllerDelegate>)delegate
     55{
     56    if (!(self = [super initWithDelegate:delegate]))
     57        return nil;
     58
     59    self.textContentType = [self.delegate textContentTypeForListViewController:self];
     60    return self;
     61}
     62
     63- (void)willPresentArouetViewController:(PUICQuickboardArouetViewController *)quickboard
     64{
     65    NSString *initialText = [self.delegate initialValueForViewController:self];
     66    if (initialText.length)
     67        [quickboard setInputText:initialText selectionRange:NSMakeRange(initialText.length, 0)];
     68
     69    quickboard.minTextLengthForEnablingAccept = 0;
     70}
     71
     72- (NSArray *)additionalTrayButtons
     73{
     74    if ([self.delegate numericInputModeForListViewController:self] == WKNumberPadInputModeNone)
     75        return @[ ];
     76
     77    UIImage *numberPadButtonIcon = [PUICResources imageNamed:@"QuickboardAddPhoneNumber" inBundle:[NSBundle bundleWithIdentifier:@"com.apple.PepperUICore"] shouldCache:YES];
     78
     79    auto numberPadButton = adoptNS([[PUICQuickboardListTrayButton alloc] initWithFrame:CGRectZero tintColor:nil defaultHeight:self.specs.defaultButtonHeight]);
     80    [numberPadButton setImage:numberPadButtonIcon forState:UIControlStateNormal];
     81    [numberPadButton addTarget:self action:@selector(presentNumberPadViewController) forControlEvents:UIControlEventTouchUpInside];
     82    return @[ numberPadButton.get() ];
     83}
     84
     85- (void)presentNumberPadViewController
     86{
     87    if (_numberPadViewController)
     88        return;
     89
     90    WKNumberPadInputMode mode = [self.delegate numericInputModeForListViewController:self];
     91    if (mode == WKNumberPadInputModeNone) {
     92        ASSERT_NOT_REACHED();
     93        return;
     94    }
     95
     96    NSString *initialText = [self.delegate initialValueForViewController:self];
     97    _numberPadViewController = adoptNS([[WKNumberPadViewController alloc] initWithDelegate:self.delegate initialText:initialText inputMode:mode]);
     98    [self presentViewController:_numberPadViewController.get() animated:YES completion:nil];
     99}
     100
     101- (void)reloadTextSuggestions
     102{
     103    [self reloadListItems];
     104}
     105
     106- (void)enterText:(NSString *)text
     107{
     108    [self.delegate quickboard:self textEntered:[[[NSAttributedString alloc] initWithString:text] autorelease]];
     109}
     110
     111#pragma mark - Quickboard subclassing
     112
     113- (BOOL)supportsDictationInput
     114{
     115    return [self.delegate allowsDictationInputForListViewController:self];
     116}
     117
     118- (void)didSelectListItemAtIndexPath:(NSIndexPath *)indexPath
     119{
     120    [self _didSelectListItem:indexPath.row];
     121}
     122
     123// FIXME: This method can be removed when <rdar://problem/57807445> lands in a build.
     124- (void)didSelectListItem:(NSInteger)itemNumber
     125{
     126    [self _didSelectListItem:itemNumber];
     127}
     128
     129- (void)_didSelectListItem:(NSInteger)itemNumber
     130{
     131    NSArray<UITextSuggestion *> *textSuggestions = [self.delegate textSuggestionsForListViewController:self];
     132    if (textSuggestions.count <= static_cast<NSUInteger>(itemNumber)) {
     133        ASSERT_NOT_REACHED();
     134        return;
     135    }
     136
     137    [self.delegate listViewController:self didSelectTextSuggestion:[textSuggestions objectAtIndex:itemNumber]];
     138}
     139
     140- (NSInteger)numberOfListItems
     141{
     142    return [self.delegate textSuggestionsForListViewController:self].count;
     143}
     144
     145- (CGFloat)heightForListItem:(NSInteger)itemNumber width:(CGFloat)width
     146{
     147    return textSuggestionButtonHeight;
     148}
     149
     150// FIXME: This method can be removed when <rdar://problem/57807445> lands in a build.
     151- (PUICQuickboardListItemCell *)cellForListItem:(NSInteger)itemNumber
     152{
     153    NSArray<UITextSuggestion *> *textSuggestions = [self.delegate textSuggestionsForListViewController:self];
     154    if (textSuggestions.count <= static_cast<NSUInteger>(itemNumber)) {
     155        ASSERT_NOT_REACHED();
     156        return nil;
     157    }
     158
     159    UITextSuggestion *textSuggestion = [textSuggestions objectAtIndex:itemNumber];
     160    auto reusableCell = retainPtr([self.listView dequeueReusableCellWithIdentifier:textSuggestionCellReuseIdentifier]);
     161    if (!reusableCell) {
     162        reusableCell = adoptNS([[WKQuickboardListItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:textSuggestionCellReuseIdentifier]);
     163        [reusableCell itemLabel].numberOfLines = 1;
     164        [reusableCell itemLabel].lineBreakMode = NSLineBreakByTruncatingTail;
     165        [reusableCell itemLabel].allowsDefaultTighteningForTruncation = YES;
     166    }
     167
     168    CGFloat viewWidth = CGRectGetWidth(self.listView.bounds);
     169    [reusableCell configureForText:textSuggestion.displayText width:viewWidth];
     170
     171    // The default behavior of -configureForText:width: causes the text label to run off the end of the cell.
     172    // Adjust for this by shrinking the label width to actually fit the bounds of the cell.
     173    [reusableCell itemLabel].frame = UIRectInset([reusableCell contentView].bounds, 0, textSuggestionLabelHorizontalMargin, 0, textSuggestionLabelHorizontalMargin);
     174    return reusableCell.autorelease();
     175}
     176
     177- (NSString *)listItemCellReuseIdentifier
     178{
     179    return textSuggestionCellReuseIdentifier;
     180}
     181
     182#if HAVE(QUICKBOARD_COLLECTION_VIEWS)
     183
     184- (Class)listItemCellClass
     185{
     186    return [WKQuickboardListCollectionViewItemCell class];
     187}
     188
     189- (PUICQuickboardListCollectionViewItemCell *)itemCellForListItem:(NSInteger)itemNumber forIndexPath:(NSIndexPath *)indexPath
     190{
     191    NSArray<UITextSuggestion *> *textSuggestions = [self.delegate textSuggestionsForListViewController:self];
     192    if (textSuggestions.count <= static_cast<NSUInteger>(itemNumber)) {
     193        ASSERT_NOT_REACHED();
     194        return nil;
     195    }
     196
     197    UITextSuggestion *textSuggestion = [textSuggestions objectAtIndex:itemNumber];
     198    auto reusableCell = retainPtr([self.collectionView dequeueReusableCellWithReuseIdentifier:textSuggestionCellReuseIdentifier forIndexPath:indexPath]);
     199
     200    [reusableCell bodyLabel].numberOfLines = 1;
     201    [reusableCell bodyLabel].lineBreakMode = NSLineBreakByTruncatingTail;
     202    [reusableCell bodyLabel].allowsDefaultTighteningForTruncation = YES;
     203    [reusableCell setText:textSuggestion.displayText];
     204
     205    // The default behavior of -configureForText:width: causes the text label to run off the end of the cell.
     206    // Adjust for this by shrinking the label width to actually fit the bounds of the cell.
     207    [reusableCell bodyLabel].frame = UIRectInset([reusableCell contentView].bounds, 0, textSuggestionLabelHorizontalMargin, 0, textSuggestionLabelHorizontalMargin);
     208    return reusableCell.autorelease();
     209}
     210
     211#endif // HAVE(QUICKBOARD_COLLECTION_VIEWS)
     212
     213- (BOOL)supportsArouetInput
     214{
     215    return [self.delegate numericInputModeForListViewController:self] == WKNumberPadInputModeNone;
     216}
     217
     218@end
     219
     220#endif // PLATFORM(WATCHOS)
Note: See TracChangeset for help on using the changeset viewer.