OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "base/mac/scoped_nsobject.h" |
| 6 #import "base/mac/sdk_forward_declarations.h" |
| 7 #include "base/strings/sys_string_conversions.h" |
| 8 #import "ui/base/cocoa/touch_bar_forward_declarations.h" |
| 9 #include "ui/base/models/dialog_model.h" |
| 10 #import "ui/views/cocoa/bridged_content_view.h" |
| 11 #include "ui/views/widget/widget_delegate.h" |
| 12 #include "ui/views/window/dialog_client_view.h" |
| 13 #include "ui/views/window/dialog_delegate.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 NSString* const kTouchBarDialogButtonsGroupId = |
| 18 @"com.google.chrome-DIALOG-BUTTONS-GROUP"; |
| 19 NSString* const kTouchBarOKId = @"com.google.chrome-OK"; |
| 20 NSString* const kTouchBarCancelId = @"com.google.chrome-CANCEL"; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 @interface BridgedContentView (TouchBarAdditions)<NSTouchBarDelegate> |
| 25 - (void)touchBarButtonAction:(id)sender; |
| 26 @end |
| 27 |
| 28 @implementation BridgedContentView (TouchBarAdditions) |
| 29 |
| 30 - (void)touchBarButtonAction:(id)sender { |
| 31 if (!hostedView_) |
| 32 return; |
| 33 |
| 34 views::DialogDelegate* dialog = |
| 35 hostedView_->GetWidget()->widget_delegate()->AsDialogDelegate(); |
| 36 DCHECK(dialog); |
| 37 views::DialogClientView* client = dialog->GetDialogClientView(); |
| 38 |
| 39 if ([sender tag] == ui::DIALOG_BUTTON_OK) { |
| 40 client->AcceptWindow(); |
| 41 return; |
| 42 } |
| 43 |
| 44 DCHECK_EQ([sender tag], ui::DIALOG_BUTTON_CANCEL); |
| 45 client->CancelWindow(); |
| 46 } |
| 47 |
| 48 // NSTouchBarDelegate protocol implementation. |
| 49 |
| 50 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar |
| 51 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { |
| 52 if (!hostedView_) |
| 53 return nil; |
| 54 |
| 55 if ([identifier isEqualToString:kTouchBarDialogButtonsGroupId]) { |
| 56 NSMutableArray* items = [NSMutableArray arrayWithCapacity:2]; |
| 57 for (NSTouchBarItemIdentifier i in @[ kTouchBarCancelId, kTouchBarOKId ]) { |
| 58 NSTouchBarItem* item = [self touchBar:touchBar makeItemForIdentifier:i]; |
| 59 if (item) |
| 60 [items addObject:item]; |
| 61 } |
| 62 if ([items count] == 0) |
| 63 return nil; |
| 64 return [NSGroupTouchBarItem groupItemWithIdentifier:identifier items:items]; |
| 65 } |
| 66 |
| 67 ui::DialogButton type = ui::DIALOG_BUTTON_NONE; |
| 68 if ([identifier isEqualToString:kTouchBarOKId]) |
| 69 type = ui::DIALOG_BUTTON_OK; |
| 70 else if ([identifier isEqualToString:kTouchBarCancelId]) |
| 71 type = ui::DIALOG_BUTTON_CANCEL; |
| 72 else |
| 73 return nil; |
| 74 |
| 75 ui::DialogModel* model = |
| 76 hostedView_->GetWidget()->widget_delegate()->AsDialogDelegate(); |
| 77 if (!model || !(model->GetDialogButtons() & type)) |
| 78 return nil; |
| 79 |
| 80 base::scoped_nsobject<NSCustomTouchBarItem> item([[NSClassFromString( |
| 81 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); |
| 82 NSString* title = base::SysUTF16ToNSString(model->GetDialogButtonLabel(type)); |
| 83 NSButton* button = |
| 84 [NSButton buttonWithTitle:title |
| 85 target:self |
| 86 action:@selector(touchBarButtonAction:)]; |
| 87 if (type == model->GetDefaultDialogButton()) { |
| 88 // NSAlert uses a private NSButton subclass (_NSTouchBarGroupButton) with |
| 89 // more bells and whistles. It doesn't use -setBezelColor: directly, but |
| 90 // this gives an appearance matching the default _NSTouchBarGroupButton. |
| 91 [button setBezelColor:[NSColor colorWithSRGBRed:0.168 |
| 92 green:0.51 |
| 93 blue:0.843 |
| 94 alpha:1.0]]; |
| 95 } |
| 96 [button setEnabled:model->IsDialogButtonEnabled(type)]; |
| 97 [button setTag:type]; |
| 98 [item setView:button]; |
| 99 return item.autorelease(); |
| 100 } |
| 101 |
| 102 // NSTouchBarProvider protocol implementation (via NSResponder category). |
| 103 |
| 104 - (NSTouchBar*)makeTouchBar { |
| 105 if (!hostedView_) |
| 106 return nil; |
| 107 |
| 108 ui::DialogModel* model = |
| 109 hostedView_->GetWidget()->widget_delegate()->AsDialogDelegate(); |
| 110 if (!model || !model->GetDialogButtons()) |
| 111 return nil; |
| 112 |
| 113 base::scoped_nsobject<NSTouchBar> bar( |
| 114 [[NSClassFromString(@"NSTouchBar") alloc] init]); |
| 115 [bar setDelegate:self]; |
| 116 |
| 117 // Use a group rather than individual items so they can be centered together. |
| 118 [bar setDefaultItemIdentifiers:@[ kTouchBarDialogButtonsGroupId ]]; |
| 119 |
| 120 // Setting the group as principal will center it in the TouchBar. |
| 121 [bar setPrincipalItemIdentifier:kTouchBarDialogButtonsGroupId]; |
| 122 return bar.autorelease(); |
| 123 } |
| 124 |
| 125 @end |
OLD | NEW |