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 @@
#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
// and a close tab button.
@interface TabStripCell : UICollectionViewCell
......@@ -17,6 +25,8 @@
// 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.
@property(nonatomic, copy) NSString* itemIdentifier;
// Delegate to inform the TabStrip on the cell.
@property(nonatomic, weak) id<TabStripCellDelegate> delegate;
@end
......
......@@ -57,6 +57,9 @@ const CGFloat kTitleInset = 10.0;
[closeButton.centerYAnchor
constraintEqualToAnchor:self.contentView.centerYAnchor],
]];
[closeButton addTarget:self
action:@selector(closeButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
UILabel* titleLabel = [[UILabel alloc] init];
[self.contentView addSubview:titleLabel];
......@@ -82,4 +85,9 @@ const CGFloat kTitleInset = 10.0;
self.itemIdentifier = nil;
}
// Selector registered to the close button.
- (void)closeButtonTapped:(id)sender {
[self.delegate closeButtonTappedForCell:self];
}
@end
......@@ -16,6 +16,10 @@
// Tells the receiver to show to the selected tab.
- (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
#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,
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
@interface TabStripMediator () <CRWWebStateObserver, WebStateListObserving> {
......@@ -204,6 +216,12 @@ web::WebState* GetWebStateWithId(WebStateList* web_state_list,
_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
// Calls |-populateItems:selectedItemID:| on the consumer.
......
......@@ -30,7 +30,7 @@ const CGFloat kNewTabButtonBottomImageInset = -2.0;
} // namespace
@interface TabStripViewController ()
@interface TabStripViewController () <TabStripCellDelegate>
@property(nonatomic, strong) UIButton* buttonNewTab;
// The local model backing the collection view.
......@@ -150,6 +150,7 @@ const CGFloat kNewTabButtonBottomImageInset = -2.0;
// theme.
- (void)configureCell:(TabStripCell*)cell withItem:(TabSwitcherItem*)item {
if (item) {
cell.delegate = self;
cell.itemIdentifier = item.identifier;
cell.titleLabel.text = item.title;
NSString* itemIdentifier = item.identifier;
......@@ -186,4 +187,10 @@ const CGFloat kNewTabButtonBottomImageInset = -2.0;
[self.delegate selectTab:index];
}
#pragma mark - TabStripCellDelegate
- (void)closeButtonTappedForCell:(TabStripCell*)cell {
[self.delegate closeItemWithID:cell.itemIdentifier];
}
@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