Chromium Code Reviews| Index: ui/views/cocoa/bridged_content_view.mm |
| diff --git a/ui/views/cocoa/bridged_content_view.mm b/ui/views/cocoa/bridged_content_view.mm |
| index 7c49cd0444346d7291793796970f16a6d7a7c505..5f83d2d2a732a9cb70107b40bda12e379c45bc7a 100644 |
| --- a/ui/views/cocoa/bridged_content_view.mm |
| +++ b/ui/views/cocoa/bridged_content_view.mm |
| @@ -16,6 +16,7 @@ |
| #include "ui/base/ime/input_method.h" |
| #include "ui/base/ime/text_edit_commands.h" |
| #include "ui/base/ime/text_input_client.h" |
| +#include "ui/base/models/dialog_model.h" |
| #include "ui/compositor/canvas_painter.h" |
| #import "ui/events/cocoa/cocoa_event_utils.h" |
| #include "ui/events/event_utils.h" |
| @@ -35,14 +36,60 @@ |
| #include "ui/views/view.h" |
| #include "ui/views/widget/native_widget_mac.h" |
| #include "ui/views/widget/widget.h" |
| +#include "ui/views/widget/widget_delegate.h" |
| +#include "ui/views/window/dialog_delegate.h" |
| #include "ui/views/word_lookup_client.h" |
| using views::MenuController; |
| +@interface NSButton (SierraAPI) |
| +@property(nullable, copy) NSColor* bezelColor; |
| ++ (instancetype)buttonWithTitle:(NSString*)title |
| + target:(id)target |
| + action:(SEL)action; |
| +@end |
| + |
| +#if !defined(MAC_OS_X_VERSION_10_12_1) |
| + |
| +typedef NSObject NSTouchBar; |
| +typedef NSObject NSTouchBarItem; |
| +typedef NSObject NSCustomTouchBarItem; |
| +typedef NSObject NSGroupTouchBarItem; |
| +typedef NSString* NSTouchBarItemIdentifier; |
| + |
| +@interface NSObject (FakeNSTouchBar) |
| +@property(copy) NSArray* defaultItemIdentifiers; |
| +@property(copy, nullable) NSTouchBarItemIdentifier principalItemIdentifier; |
| +@property(nullable, weak) id<NSTouchBarDelegate> delegate; |
| +@end |
| + |
| +@interface NSObject (FakeNSGroupTouchBarItem) |
| ++ (NSGroupTouchBarItem*)groupItemWithIdentifier: |
| + (NSTouchBarItemIdentifier)identifier |
| + items:(NSArray*)items; |
| +@end |
| + |
| +@interface NSObject (FakeNSCustomTouchBarItem) |
| +@property(readwrite, strong) NSView* view; |
| +@end |
| + |
| +#else |
| + |
| +@class NSTouchBar; |
| +@class NSTouchBarItem; |
| +@class NSCustomTouchBarItem; |
| +@class NSGroupTouchBarItem; |
| + |
| +#endif |
| + |
| namespace { |
| NSString* const kFullKeyboardAccessChangedNotification = |
| @"com.apple.KeyboardUIModeDidChange"; |
| +NSString* const kTouchBarDialogButtonsGroupId = |
| + @"com.google.chrome-DIALOG-BUTTONS-GROUP"; |
| +NSString* const kTouchBarOKId = @"com.google.chrome-OK"; |
| +NSString* const kTouchBarCancelId = @"com.google.chrome-CANCEL"; |
| // Returns true if all four corners of |rect| are contained inside |path|. |
| bool IsRectInsidePath(NSRect rect, NSBezierPath* path) { |
| @@ -1418,6 +1465,96 @@ NSAttributedString* GetAttributedString( |
| client->EndDrag(); |
| } |
| +// TouchBar action. |
| + |
| +- (void)touchBarButtonAction:(id)sender { |
| + if (!hostedView_) |
| + return; |
| + |
| + views::DialogDelegate* dialog = |
| + hostedView_->GetWidget()->widget_delegate()->AsDialogDelegate(); |
| + DCHECK(dialog); |
| + |
| + if ([sender tag] == ui::DIALOG_BUTTON_OK) { |
| + dialog->Accept(); |
| + return; |
| + } |
| + |
| + DCHECK_EQ([sender tag], ui::DIALOG_BUTTON_CANCEL); |
| + dialog->Cancel(); |
| +} |
| + |
| +// NSTouchBarDelegate protocol implementation. |
| + |
| +- (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar |
| + makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { |
| + if (!hostedView_) |
| + return nil; |
| + |
| + if ([identifier isEqualToString:kTouchBarDialogButtonsGroupId]) { |
| + NSMutableArray* items = [NSMutableArray arrayWithCapacity:2]; |
| + for (NSTouchBarItemIdentifier i : @[ kTouchBarCancelId, kTouchBarOKId ]) { |
| + NSTouchBarItem* item = [self touchBar:touchBar makeItemForIdentifier:i]; |
| + if (item) |
| + [items addObject:item]; |
| + } |
| + if ([items count] == 0) |
| + return nil; |
| + return [NSGroupTouchBarItem groupItemWithIdentifier:identifier items:items]; |
| + } |
| + |
| + ui::DialogButton type = ui::DIALOG_BUTTON_NONE; |
| + if ([identifier isEqualToString:kTouchBarOKId]) |
| + type = ui::DIALOG_BUTTON_OK; |
| + else if ([identifier isEqualToString:kTouchBarCancelId]) |
| + type = ui::DIALOG_BUTTON_CANCEL; |
| + else |
| + return nil; |
| + |
| + ui::DialogModel* model = |
| + hostedView_->GetWidget()->widget_delegate()->AsDialogDelegate(); |
| + if (!model || !(model->GetDialogButtons() & type)) |
| + return nil; |
| + |
| + base::scoped_nsobject<NSCustomTouchBarItem> item([[NSClassFromString( |
| + @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); |
| + NSString* title = base::SysUTF16ToNSString(model->GetDialogButtonLabel(type)); |
| + NSButton* button = |
| + [NSButton buttonWithTitle:title |
| + target:self |
| + action:@selector(touchBarButtonAction:)]; |
| + if (type == model->GetDefaultDialogButton()) { |
| + [button setKeyEquivalent:@"\n"]; |
|
tapted
2016/11/02 09:28:57
I'd hoped this would give the blue color, but actu
Avi (use Gerrit)
2016/11/02 14:50:41
Can we hopper up NSAlert and see what it's doing t
tapted
2016/11/03 04:36:29
Dumped my findings to http://crbug.com/661581 - it
|
| + [button setBezelColor:[NSColor colorWithSRGBRed:0.168 |
| + green:0.51 |
| + blue:0.843 |
| + alpha:1.0]]; |
| + } |
| + [button setEnabled:model->IsDialogButtonEnabled(type)]; |
| + [button setTag:type]; |
| + [item setView:button]; |
| + return item.autorelease(); |
| +} |
| + |
| +// NSTouchBarProvider protocol implementation (via NSResonder category). |
| + |
| +- (NSTouchBar*)makeTouchBar { |
| + if (!hostedView_) |
| + return nil; |
| + |
| + ui::DialogModel* model = |
| + hostedView_->GetWidget()->widget_delegate()->AsDialogDelegate(); |
| + if (!model || !model->GetDialogButtons()) |
| + return nil; |
| + |
| + base::scoped_nsobject<NSTouchBar> bar( |
| + [[NSClassFromString(@"NSTouchBar") alloc] init]); |
| + [bar setDelegate:self]; |
| + [bar setDefaultItemIdentifiers:@[ kTouchBarDialogButtonsGroupId ]]; |
| + [bar setPrincipalItemIdentifier:kTouchBarDialogButtonsGroupId]; |
| + return bar.autorelease(); |
| +} |
| + |
| // NSAccessibility informal protocol implementation. |
| - (id)accessibilityAttributeValue:(NSString*)attribute { |