File: toolbar_actions_bar_bubble_mac.mm

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (398 lines) | stat: -rw-r--r-- 15,533 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/ui/cocoa/extensions/toolbar_actions_bar_bubble_mac.h"

#include <utility>

#include "base/mac/foundation_util.h"
#include "base/strings/sys_string_conversions.h"
#import "chrome/browser/ui/cocoa/info_bubble_view.h"
#import "chrome/browser/ui/cocoa/info_bubble_window.h"
#include "chrome/browser/ui/toolbar/toolbar_actions_bar_bubble_delegate.h"
#include "skia/ext/skia_utils_mac.h"
#import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
#include "third_party/skia/include/core/SkColor.h"
#import "ui/base/cocoa/controls/hyperlink_button_cell.h"
#import "ui/base/cocoa/hover_button.h"
#include "ui/base/resource/resource_bundle.h"
#import "ui/base/cocoa/window_size_constants.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/image/image_skia_util_mac.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/native_theme/native_theme.h"

namespace {
BOOL g_animations_enabled = false;
CGFloat kMinWidth = 320.0;
}

@interface ToolbarActionsBarBubbleMac ()

// Handles the notification that the window will close.
- (void)windowWillClose:(NSNotification*)notification;

// Creates and returns an NSAttributed string with the specified size and
// alignment.
- (NSAttributedString*)attributedStringWithString:(const base::string16&)string
                                         fontSize:(CGFloat)fontSize
                                        alignment:(NSTextAlignment)alignment;

// Creates an NSTextField with the given string, size, and alignment, and adds
// it to the window.
- (NSTextField*)addTextFieldWithString:(const base::string16&)string
                              fontSize:(CGFloat)fontSize
                             alignment:(NSTextAlignment)alignment;

// Creates an ExtensionMessagebubbleButton the given string id, and adds it to
// the window.
- (NSButton*)addButtonWithString:(const base::string16&)string;

// Initializes the bubble's content.
- (void)layout;

// Handles a button being clicked.
- (void)onButtonClicked:(id)sender;

@end

@implementation ToolbarActionsBarBubbleMac

@synthesize actionButton = actionButton_;
@synthesize itemList = itemList_;
@synthesize dismissButton = dismissButton_;
@synthesize link = link_;
@synthesize label = label_;
@synthesize iconView = iconView_;

- (id)initWithParentWindow:(NSWindow*)parentWindow
               anchorPoint:(NSPoint)anchorPoint
          anchoredToAction:(BOOL)anchoredToAction
                  delegate:
                      (std::unique_ptr<ToolbarActionsBarBubbleDelegate>)
                          delegate {
  base::scoped_nsobject<InfoBubbleWindow> window(
      [[InfoBubbleWindow alloc]
          initWithContentRect:ui::kWindowSizeDeterminedLater
                    styleMask:NSBorderlessWindowMask
                      backing:NSBackingStoreBuffered
                        defer:NO]);
  if ((self = [super initWithWindow:window
                       parentWindow:parentWindow
                         anchoredAt:anchorPoint])) {
    acknowledged_ = NO;
    anchoredToAction_ = anchoredToAction;
    delegate_ = std::move(delegate);

    ui::NativeTheme* nativeTheme = ui::NativeTheme::GetInstanceForNativeUi();
    [[self bubble] setAlignment:info_bubble::kAlignArrowToAnchor];
    [[self bubble] setArrowLocation:info_bubble::kTopRight];
    [[self bubble] setBackgroundColor:
        skia::SkColorToCalibratedNSColor(nativeTheme->GetSystemColor(
            ui::NativeTheme::kColorId_DialogBackground))];

    if (!g_animations_enabled)
      [window setAllowedAnimations:info_bubble::kAnimateNone];

    [self setShouldCloseOnResignKey:delegate_->ShouldCloseOnDeactivate()];

    [self layout];

    [[self window] makeFirstResponder:
        (actionButton_ ? actionButton_ : dismissButton_)];
  }
  return self;
}

+ (void)setAnimationEnabledForTesting:(BOOL)enabled {
  g_animations_enabled = enabled;
}

- (IBAction)showWindow:(id)sender {
  delegate_->OnBubbleShown();
  [super showWindow:sender];
}

// Private /////////////////////////////////////////////////////////////////////

- (void)windowWillClose:(NSNotification*)notification {
  if (!acknowledged_) {
    delegate_->OnBubbleClosed(
        ToolbarActionsBarBubbleDelegate::CLOSE_DISMISS_DEACTIVATION);
    acknowledged_ = YES;
  }
  // Deallocation happens asynchronously in Cocoa, but that makes testing
  // difficult. Explicitly destroy |delegate_| here so it can perform any
  // necessary cleanup.
  delegate_.reset();
  [super windowWillClose:notification];
}

- (NSAttributedString*)attributedStringWithString:(const base::string16&)string
                                         fontSize:(CGFloat)fontSize
                                        alignment:(NSTextAlignment)alignment {
  NSString* cocoaString = base::SysUTF16ToNSString(string);
  base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle(
      [[NSMutableParagraphStyle alloc] init]);
  [paragraphStyle setAlignment:alignment];
  NSDictionary* attributes = @{
    NSFontAttributeName : [NSFont systemFontOfSize:fontSize],
    NSForegroundColorAttributeName :
        [NSColor colorWithCalibratedWhite:0.2 alpha:1.0],
    NSParagraphStyleAttributeName : paragraphStyle.get()
  };
  return [[[NSAttributedString alloc] initWithString:cocoaString
                                          attributes:attributes] autorelease];
}

- (NSTextField*)addTextFieldWithString:(const base::string16&)string
                              fontSize:(CGFloat)fontSize
                             alignment:(NSTextAlignment)alignment {
  NSAttributedString* attributedString =
      [self attributedStringWithString:string
                              fontSize:fontSize
                             alignment:alignment];

  NSTextField* textField =
      [[[NSTextField alloc] initWithFrame:NSZeroRect] autorelease];
  [textField setEditable:NO];
  [textField setBordered:NO];
  [textField setDrawsBackground:NO];
  [textField setAttributedStringValue:attributedString];
  [[[self window] contentView] addSubview:textField];
  [textField sizeToFit];
  return textField;
}

- (NSButton*)addButtonWithString:(const base::string16&)string {
  NSButton* button = [[[NSButton alloc] initWithFrame:NSZeroRect] autorelease];
  NSAttributedString* buttonString =
      [self attributedStringWithString:string
                              fontSize:13.0
                             alignment:NSCenterTextAlignment];
  [button setAttributedTitle:buttonString];
  [button setBezelStyle:NSRoundedBezelStyle];
  [button setTarget:self];
  [button setAction:@selector(onButtonClicked:)];
  [[[self window] contentView] addSubview:button];
  [button sizeToFit];
  return button;
}

- (void)layout {
  // First, construct the pieces of the bubble that have a fixed width: the
  // heading, and the button strip (the extra view (icon and/or (linked) text),
  // the action button, and the dismiss button).
  NSTextField* heading =
      [self addTextFieldWithString:delegate_->GetHeadingText()
                          fontSize:13.0
                         alignment:NSLeftTextAlignment];
  NSSize headingSize = [heading frame].size;

  NSSize extraViewIconSize = NSZeroSize;
  NSSize extraViewTextSize = NSZeroSize;

  std::unique_ptr<ToolbarActionsBarBubbleDelegate::ExtraViewInfo>
      extra_view_info = delegate_->GetExtraViewInfo();

  if (extra_view_info) {
    gfx::VectorIconId resource_id = extra_view_info->resource_id;
    // The extra view icon is optional.
    if (resource_id != gfx::VectorIconId::VECTOR_ICON_NONE) {
      NSImage* image = gfx::Image(gfx::CreateVectorIcon(resource_id, 16,
                                                        gfx::kChromeIconGrey))
                           .ToNSImage();
      NSRect frame = NSMakeRect(0, 0, image.size.width, image.size.height);
      iconView_ = [[[NSImageView alloc] initWithFrame:frame] autorelease];
      [iconView_ setImage:image];
      extraViewIconSize = frame.size;

      [[[self window] contentView] addSubview:iconView_];
    }

    const base::string16& text = extra_view_info->text;
    if (!text.empty()) {  // The extra view text is optional.
      if (extra_view_info->is_text_linked) {
        NSAttributedString* linkString =
            [self attributedStringWithString:text
                                    fontSize:13.0
                                   alignment:NSLeftTextAlignment];
        link_ = [HyperlinkButtonCell buttonWithString:linkString.string];
        [link_ setTarget:self];
        [link_ setAction:@selector(onButtonClicked:)];
        [[[self window] contentView] addSubview:link_];
        [link_ sizeToFit];
      } else {
        label_ = [self addTextFieldWithString:text
                                     fontSize:13.0
                                    alignment:NSLeftTextAlignment];
      }
      extraViewTextSize = label_ ? [label_ frame].size : [link_ frame].size;
    }
  }

  base::string16 cancelStr = delegate_->GetDismissButtonText();
  NSSize dismissButtonSize = NSZeroSize;
  if (!cancelStr.empty()) {  // A cancel/dismiss button is optional.
    dismissButton_ = [self addButtonWithString:cancelStr];
    dismissButtonSize =
        NSMakeSize(NSWidth([dismissButton_ frame]),
                   NSHeight([dismissButton_ frame]));
  }

  base::string16 actionStr = delegate_->GetActionButtonText();
  NSSize actionButtonSize = NSZeroSize;
  if (!actionStr.empty()) {  // The action button is optional.
    actionButton_ = [self addButtonWithString:actionStr];
    actionButtonSize =
        NSMakeSize(NSWidth([actionButton_ frame]),
                   NSHeight([actionButton_ frame]));
  }

  DCHECK(actionButton_ || dismissButton_);
  CGFloat buttonStripHeight =
      std::max(actionButtonSize.height, dismissButtonSize.height);

  const CGFloat kButtonPadding = 5.0;
  CGFloat buttonStripWidth = 0;
  if (actionButton_)
    buttonStripWidth += actionButtonSize.width + kButtonPadding;
  if (dismissButton_)
    buttonStripWidth += dismissButtonSize.width + kButtonPadding;
  if (iconView_)
    buttonStripWidth += extraViewIconSize.width + kButtonPadding;
  if (link_ || label_)
    buttonStripWidth += extraViewTextSize.width + kButtonPadding;

  CGFloat headingWidth = headingSize.width;
  CGFloat windowWidth =
      std::max(std::max(kMinWidth, buttonStripWidth), headingWidth);

  NSTextField* content =
      [self addTextFieldWithString:delegate_->GetBodyText(anchoredToAction_)
                          fontSize:12.0
                         alignment:NSLeftTextAlignment];
  [content setFrame:NSMakeRect(0, 0, windowWidth, 0)];
  // The content should have the same (max) width as the heading, which means
  // the text will most likely wrap.
  NSSize contentSize = NSMakeSize(windowWidth,
                                  [GTMUILocalizerAndLayoutTweaker
                                       sizeToFitFixedWidthTextField:content]);

  const CGFloat kItemListIndentation = 10.0;
  base::string16 itemListStr = delegate_->GetItemListText();
  NSSize itemListSize;
  if (!itemListStr.empty()) {
    itemList_ =
        [self addTextFieldWithString:itemListStr
                            fontSize:12.0
                           alignment:NSLeftTextAlignment];
    CGFloat listWidth = windowWidth - kItemListIndentation;
    [itemList_ setFrame:NSMakeRect(0, 0, listWidth, 0)];
    itemListSize = NSMakeSize(listWidth,
                              [GTMUILocalizerAndLayoutTweaker
                                   sizeToFitFixedWidthTextField:itemList_]);
  }

  const CGFloat kHorizontalPadding = 15.0;
  const CGFloat kVerticalPadding = 10.0;

  // Next, we set frame for all the different pieces of the bubble, from bottom
  // to top.
  windowWidth += kHorizontalPadding * 2;
  CGFloat currentHeight = kVerticalPadding;
  CGFloat currentMaxWidth = windowWidth - kHorizontalPadding;
  if (actionButton_) {
    [actionButton_ setFrame:NSMakeRect(
        currentMaxWidth - actionButtonSize.width,
        currentHeight,
        actionButtonSize.width,
        actionButtonSize.height)];
    currentMaxWidth -= (actionButtonSize.width + kButtonPadding);
  }
  if (dismissButton_) {
    [dismissButton_ setFrame:NSMakeRect(
        currentMaxWidth - dismissButtonSize.width,
        currentHeight,
        dismissButtonSize.width,
        dismissButtonSize.height)];
    currentMaxWidth -= (dismissButtonSize.width + kButtonPadding);
  }
  int leftAlignXPos = kHorizontalPadding;
  if (iconView_) {
    CGFloat extraViewIconHeight =
        currentHeight + (buttonStripHeight - extraViewIconSize.height) / 2.0;

    [iconView_
        setFrame:NSMakeRect(leftAlignXPos, extraViewIconHeight,
                            extraViewIconSize.width, extraViewIconSize.height)];
    leftAlignXPos += extraViewIconSize.width + kButtonPadding;
  }
  if (label_ || link_) {
    CGFloat extraViewTextHeight =
        currentHeight + (buttonStripHeight - extraViewTextSize.height) / 2.0;
    NSRect frame =
        NSMakeRect(leftAlignXPos, extraViewTextHeight, extraViewTextSize.width,
                   extraViewTextSize.height);
    if (link_) {
      [link_ setFrame:frame];
    } else {
      [label_ setFrame:frame];
    }
  }
  // Buttons have some inherit padding of their own, so we don't need quite as
  // much space here.
  currentHeight += buttonStripHeight + kVerticalPadding / 2;

  if (itemList_) {
    [itemList_ setFrame:NSMakeRect(kHorizontalPadding + kItemListIndentation,
                                   currentHeight,
                                   itemListSize.width,
                                   itemListSize.height)];
    currentHeight += itemListSize.height + kVerticalPadding;
  }

  [content setFrame:NSMakeRect(kHorizontalPadding,
                               currentHeight,
                               contentSize.width,
                               contentSize.height)];
  currentHeight += contentSize.height + kVerticalPadding;
  [heading setFrame:NSMakeRect(kHorizontalPadding,
                               currentHeight,
                               headingSize.width,
                               headingSize.height)];

  // Update window frame.
  NSRect windowFrame = [[self window] frame];
  NSSize windowSize =
      NSMakeSize(windowWidth,
                 currentHeight + headingSize.height + kVerticalPadding * 2);
  // We need to convert the size to be in the window's coordinate system. Since
  // all we're doing is converting a size, and all views within a window share
  // the same size metrics, it's okay that the size calculation came from
  // multiple different views. Pick a view to convert it.
  windowSize = [heading convertSize:windowSize toView:nil];
  windowFrame.size = windowSize;
  [[self window] setFrame:windowFrame display:YES];
}

- (void)onButtonClicked:(id)sender {
  if (acknowledged_)
    return;
  ToolbarActionsBarBubbleDelegate::CloseAction action =
      ToolbarActionsBarBubbleDelegate::CLOSE_EXECUTE;
  if (link_ && sender == link_) {
    action = ToolbarActionsBarBubbleDelegate::CLOSE_LEARN_MORE;
  } else if (dismissButton_ && sender == dismissButton_) {
    action = ToolbarActionsBarBubbleDelegate::CLOSE_DISMISS_USER_ACTION;
  } else {
    DCHECK_EQ(sender, actionButton_);
    action = ToolbarActionsBarBubbleDelegate::CLOSE_EXECUTE;
  }
  acknowledged_ = YES;
  delegate_->OnBubbleClosed(action);
  [self close];
}

@end