Commit f331c0ec authored by Nazerke's avatar Nazerke Committed by Chromium LUCI CQ

[ios] Close the tab button.

This CL adds the functionality to each of the close buttons in the tab
strip.

Bug: 1128249
Change-Id: I8f1357e2d13bb46319f4b59b5d6720964ddd1e9d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2564642
Commit-Queue: Nazerke Kalidolda <nazerke@google.com>
Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#832382}
parent 2baa9792
...@@ -7,6 +7,14 @@ ...@@ -7,6 +7,14 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
@class TabStripCell;
// Informs the receiver of actions on the cell.
@protocol TabStripCellDelegate
// Informs the receiver that the close button on the cell was tapped.
- (void)closeButtonTappedForCell:(TabStripCell*)cell;
@end
// UICollectionViewCell that contains a Tab title with a leading imageView // UICollectionViewCell that contains a Tab title with a leading imageView
// and a close tab button. // and a close tab button.
@interface TabStripCell : UICollectionViewCell @interface TabStripCell : UICollectionViewCell
...@@ -17,6 +25,8 @@ ...@@ -17,6 +25,8 @@
// Unique identifier for the cell's contents. This is used to ensure that // Unique identifier for the cell's contents. This is used to ensure that
// updates in an asynchronous callback are only made if the item is the same. // updates in an asynchronous callback are only made if the item is the same.
@property(nonatomic, copy) NSString* itemIdentifier; @property(nonatomic, copy) NSString* itemIdentifier;
// Delegate to inform the TabStrip on the cell.
@property(nonatomic, weak) id<TabStripCellDelegate> delegate;
@end @end
......
...@@ -57,6 +57,9 @@ const CGFloat kTitleInset = 10.0; ...@@ -57,6 +57,9 @@ const CGFloat kTitleInset = 10.0;
[closeButton.centerYAnchor [closeButton.centerYAnchor
constraintEqualToAnchor:self.contentView.centerYAnchor], constraintEqualToAnchor:self.contentView.centerYAnchor],
]]; ]];
[closeButton addTarget:self
action:@selector(closeButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
UILabel* titleLabel = [[UILabel alloc] init]; UILabel* titleLabel = [[UILabel alloc] init];
[self.contentView addSubview:titleLabel]; [self.contentView addSubview:titleLabel];
...@@ -82,4 +85,9 @@ const CGFloat kTitleInset = 10.0; ...@@ -82,4 +85,9 @@ const CGFloat kTitleInset = 10.0;
self.itemIdentifier = nil; self.itemIdentifier = nil;
} }
// Selector registered to the close button.
- (void)closeButtonTapped:(id)sender {
[self.delegate closeButtonTappedForCell:self];
}
@end @end
...@@ -16,6 +16,10 @@ ...@@ -16,6 +16,10 @@
// Tells the receiver to show to the selected tab. // Tells the receiver to show to the selected tab.
- (void)selectTab:(int)index; - (void)selectTab:(int)index;
// Tells the receiver to close the item with identifier |itemID|. If there is
// no item with that identifier, no item is closed.
- (void)closeItemWithID:(NSString*)itemID;
@end @end
#endif // IOS_CHROME_BROWSER_UI_TAB_SWITCHER_TAB_STRIP_TAB_STRIP_CONSUMER_DELEGATE_H_ #endif // IOS_CHROME_BROWSER_UI_TAB_SWITCHER_TAB_STRIP_TAB_STRIP_CONSUMER_DELEGATE_H_
...@@ -74,6 +74,18 @@ web::WebState* GetWebStateWithId(WebStateList* web_state_list, ...@@ -74,6 +74,18 @@ web::WebState* GetWebStateWithId(WebStateList* web_state_list,
return nullptr; return nullptr;
} }
// Returns the index of the tab with |identifier| in |web_state_list|. Returns
// -1 if not found.
int GetIndexOfTabWithId(WebStateList* web_state_list, NSString* identifier) {
for (int i = 0; i < web_state_list->count(); i++) {
web::WebState* web_state = web_state_list->GetWebStateAt(i);
TabIdTabHelper* tab_helper = TabIdTabHelper::FromWebState(web_state);
if ([identifier isEqualToString:tab_helper->tab_id()])
return i;
}
return -1;
}
} // namespace } // namespace
@interface TabStripMediator () <CRWWebStateObserver, WebStateListObserving> { @interface TabStripMediator () <CRWWebStateObserver, WebStateListObserving> {
...@@ -204,6 +216,12 @@ web::WebState* GetWebStateWithId(WebStateList* web_state_list, ...@@ -204,6 +216,12 @@ web::WebState* GetWebStateWithId(WebStateList* web_state_list,
_webStateList->ActivateWebStateAt(index); _webStateList->ActivateWebStateAt(index);
} }
- (void)closeItemWithID:(NSString*)itemID {
int index = GetIndexOfTabWithId(self.webStateList, itemID);
if (index >= 0)
self.webStateList->CloseWebStateAt(index, WebStateList::CLOSE_USER_ACTION);
}
#pragma mark - Private #pragma mark - Private
// Calls |-populateItems:selectedItemID:| on the consumer. // Calls |-populateItems:selectedItemID:| on the consumer.
......
...@@ -30,7 +30,7 @@ const CGFloat kNewTabButtonBottomImageInset = -2.0; ...@@ -30,7 +30,7 @@ const CGFloat kNewTabButtonBottomImageInset = -2.0;
} // namespace } // namespace
@interface TabStripViewController () @interface TabStripViewController () <TabStripCellDelegate>
@property(nonatomic, strong) UIButton* buttonNewTab; @property(nonatomic, strong) UIButton* buttonNewTab;
// The local model backing the collection view. // The local model backing the collection view.
...@@ -150,6 +150,7 @@ const CGFloat kNewTabButtonBottomImageInset = -2.0; ...@@ -150,6 +150,7 @@ const CGFloat kNewTabButtonBottomImageInset = -2.0;
// theme. // theme.
- (void)configureCell:(TabStripCell*)cell withItem:(TabSwitcherItem*)item { - (void)configureCell:(TabStripCell*)cell withItem:(TabSwitcherItem*)item {
if (item) { if (item) {
cell.delegate = self;
cell.itemIdentifier = item.identifier; cell.itemIdentifier = item.identifier;
cell.titleLabel.text = item.title; cell.titleLabel.text = item.title;
NSString* itemIdentifier = item.identifier; NSString* itemIdentifier = item.identifier;
...@@ -186,4 +187,10 @@ const CGFloat kNewTabButtonBottomImageInset = -2.0; ...@@ -186,4 +187,10 @@ const CGFloat kNewTabButtonBottomImageInset = -2.0;
[self.delegate selectTab:index]; [self.delegate selectTab:index];
} }
#pragma mark - TabStripCellDelegate
- (void)closeButtonTappedForCell:(TabStripCell*)cell {
[self.delegate closeItemWithID:cell.itemIdentifier];
}
@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