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 : @[ 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 // Set the bezel color similar to how NSAlert does it. | |
89 [button setBezelColor:[NSColor colorWithSRGBRed:0.168 | |
90 green:0.51 | |
91 blue:0.843 | |
92 alpha:1.0]]; | |
Avi (use Gerrit)
2016/11/03 14:30:50
My comment still stands; _is_ this how NSAlert doe
tapted
2016/11/03 23:43:57
updated the comment:
// NSAlert uses a private
| |
93 } | |
94 [button setEnabled:model->IsDialogButtonEnabled(type)]; | |
95 [button setTag:type]; | |
96 [item setView:button]; | |
97 return item.autorelease(); | |
98 } | |
99 | |
100 // NSTouchBarProvider protocol implementation (via NSResponder category). | |
101 | |
102 - (NSTouchBar*)makeTouchBar { | |
103 if (!hostedView_) | |
104 return nil; | |
105 | |
106 ui::DialogModel* model = | |
107 hostedView_->GetWidget()->widget_delegate()->AsDialogDelegate(); | |
108 if (!model || !model->GetDialogButtons()) | |
109 return nil; | |
110 | |
111 base::scoped_nsobject<NSTouchBar> bar( | |
112 [[NSClassFromString(@"NSTouchBar") alloc] init]); | |
113 [bar setDelegate:self]; | |
114 | |
115 // Use a group rather than individual items so they can be centered together. | |
116 [bar setDefaultItemIdentifiers:@[ kTouchBarDialogButtonsGroupId ]]; | |
117 | |
118 // Setting the group as principal will center it in the TouchBar. | |
119 [bar setPrincipalItemIdentifier:kTouchBarDialogButtonsGroupId]; | |
120 return bar.autorelease(); | |
121 } | |
122 | |
123 @end | |
OLD | NEW |