File: toolbar_button_cocoa.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 (386 lines) | stat: -rw-r--r-- 13,816 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
// Copyright (c) 2011 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/toolbar/toolbar_button_cocoa.h"

#include "base/mac/foundation_util.h"
#include "base/mac/sdk_forward_declarations.h"
#include "chrome/app/vector_icons/vector_icons.h"
#import "chrome/browser/ui/cocoa/image_button_cell.h"
#import "chrome/browser/ui/cocoa/l10n_util.h"
#import "chrome/browser/ui/cocoa/view_id_util.h"
#include "skia/ext/skia_utils_mac.h"
#import "ui/base/cocoa/nsview_additions.h"
#include "ui/base/material_design/material_design_controller.h"
#include "ui/base/theme_provider.h"
#include "ui/gfx/image/image_skia_util_mac.h"
#include "ui/gfx/paint_vector_icon.h"

namespace {

// Toolbar buttons are 24x24 in Material Design.
const NSRect kMDButtonBounds = NSMakeRect(0, 0, 24, 24);

// The size of a toolbar button icon in Material Design. A toolbar button image
// consists of a border and background, with a centered icon.
const NSSize kMDButtonIconSize = NSMakeSize(16, 16);

}  // namespace

// An NSCustomImageRep subclass that creates the "three dots" image of the
// Material Design browser tools icon.
@interface BrowserToolsImageRep : NSCustomImageRep
@property (retain, nonatomic) NSColor* fillColor;
// NSCustomImageRep delegate method that performs the drawing.
+ (void)drawBrowserToolsIcon:(BrowserToolsImageRep*)imageRep;
@end

@implementation BrowserToolsImageRep

@synthesize fillColor = fillColor_;

- (void)dealloc {
  [fillColor_ release];
  [super dealloc];
}

+ (void)drawBrowserToolsIcon:(BrowserToolsImageRep*)imageRep {
  [imageRep.fillColor set];
  NSBezierPath* dotPath =
      [NSBezierPath bezierPathWithOvalInRect:NSMakeRect(6.5, 1.5, 3, 3)];
  CGContextRef context = static_cast<CGContextRef>(
      [[NSGraphicsContext currentContext] graphicsPort]);
  // Draw the three dots by drawing |dotPath| in three different locations.
  for (NSUInteger i = 0; i < 3; i++) {
    [dotPath fill];
    CGContextTranslateCTM(context, 0, 5);
  }
}

@end

// An NSCustomImageRep subclass that draws a Material Design background behind
// and border around a centered icon image.
@interface ToolbarButtonImageRep : NSCustomImageRep
@property (retain, nonatomic) NSImage* icon;
@property (assign, nonatomic) ToolbarButtonImageBackgroundStyle style;
// NSCustomImageRep delegate method that performs the drawing.
+ (void)drawImage:(ToolbarButtonImageRep*)imageRep;
@end

@implementation ToolbarButtonImageRep

@synthesize icon = icon_;
@synthesize style = style_;

- (void)dealloc {
  [icon_ release];
  [super dealloc];
}

+ (void)drawImage:(ToolbarButtonImageRep*)imageRep {
  ToolbarButtonImageBackgroundStyle displayStyle = [imageRep style];

  // Non-default styles draw a background.
  if (displayStyle != ToolbarButtonImageBackgroundStyle::DEFAULT) {
    // Create the path used for the background fill.
    const int kCornerRadius = 2;
    NSBezierPath* roundedRectPath =
        [NSBezierPath bezierPathWithRoundedRect:kMDButtonBounds
                                        xRadius:kCornerRadius
                                        yRadius:kCornerRadius];

    // Determine the fill color.
    NSColor* fillColor = nil;
    const CGFloat kEightPercentAlpha = 0.08;
    const CGFloat kTwelvePercentAlpha = 0.12;
    const CGFloat kSixteenPercentAlpha = 0.16;
    switch (displayStyle) {
      case ToolbarButtonImageBackgroundStyle::HOVER:
        fillColor = [NSColor colorWithCalibratedWhite:0
                                                alpha:kEightPercentAlpha];
        break;
      case ToolbarButtonImageBackgroundStyle::HOVER_THEMED:
        fillColor = [NSColor colorWithCalibratedWhite:1
                                                alpha:kTwelvePercentAlpha];
        break;
      case ToolbarButtonImageBackgroundStyle::PRESSED:
        fillColor = [NSColor colorWithCalibratedWhite:0
                                                alpha:kTwelvePercentAlpha];
        break;
      case ToolbarButtonImageBackgroundStyle::PRESSED_THEMED:
        fillColor = [NSColor colorWithCalibratedWhite:1
                                                alpha:kSixteenPercentAlpha];
        break;
      case ToolbarButtonImageBackgroundStyle::DEFAULT:
        NOTREACHED();
    }

    // Fill the path.
    [fillColor set];
    [roundedRectPath fill];
  }

  // Center the icon within the button.
  NSSize iconSize = [imageRep.icon size];
  CGFloat iconInset = (kMDButtonBounds.size.width - iconSize.width) / 2;
  NSRect iconDestRect = NSInsetRect(kMDButtonBounds, iconInset, iconInset);
  [imageRep.icon drawInRect:iconDestRect
                   fromRect:NSZeroRect
                  operation:NSCompositeSourceOver
                   fraction:1];
}

@end

@interface ToolbarButton ()
// Returns an image that draws the browser tools button icon using vector
// commands.
- (NSImage*)browserToolsIconForFillColor:(SkColor)fillColor;
// Returns an button image by combining |iconImage| with the specified button
// background.
- (NSImage*)imageForIcon:(NSImage*)iconImage
     withBackgroundStyle:(ToolbarButtonImageBackgroundStyle)style;
// Implemented to set the button's icon when added to the browser window. We
// can't set the image before this because its appearance depends upon the
// browser window's theme.
- (void)viewDidMoveToWindow;

@end


@implementation ToolbarButton

@synthesize handleMiddleClick = handleMiddleClick_;

+ (NSSize)toolbarButtonSize {
  return kMDButtonBounds.size;
}

- (void)otherMouseDown:(NSEvent*)theEvent {
  if (![self shouldHandleEvent:theEvent]) {
    [super otherMouseDown:theEvent];
    return;
  }

  NSEvent* nextEvent = theEvent;
  BOOL isInside;

  // Loop until middle button is released. Also, the mouse cursor is outside of
  // the button, the button should not be highlighted.
  do {
    NSPoint mouseLoc = [self convertPoint:[nextEvent locationInWindow]
                                 fromView:nil];
    isInside = [self mouse:mouseLoc inRect:[self bounds]];
    [self highlight:isInside];
    [self setState:isInside ? NSOnState : NSOffState];

    NSUInteger mask = NSOtherMouseDraggedMask | NSOtherMouseUpMask;
    nextEvent = [[self window] nextEventMatchingMask:mask];
  } while (!([nextEvent buttonNumber] == 2 &&
             [nextEvent type] == NSOtherMouseUp));

  // Discard the events before the middle button up event.
  // If we don't discard it, the events will be re-processed later.
  [[self window] discardEventsMatchingMask:NSAnyEventMask
                               beforeEvent:nextEvent];

  [self highlight:NO];
  [self setState:NSOffState];
  if (isInside)
    [self sendAction:[self action] to:[self target]];
}

- (BOOL)shouldHandleEvent:(NSEvent*)theEvent {
  // |buttonNumber| is the mouse button whose action triggered theEvent.
  // 2 corresponds to the middle mouse button.
  return handleMiddleClick_ && [theEvent buttonNumber] == 2;
}

- (void)drawFocusRingMask {
  // Match the hover image's bezel.
  [[NSBezierPath bezierPathWithRoundedRect:NSInsetRect([self bounds], 2, 2)
                                   xRadius:2
                                   yRadius:2] fill];
}

- (const gfx::VectorIcon*)vectorIcon {
  BOOL isRTL = cocoa_l10n_util::ShouldDoExperimentalRTLLayout();
  switch ([self viewID]) {
    case VIEW_ID_BACK_BUTTON:
      return isRTL ? &kNavigateForwardIcon : &kNavigateBackIcon;
    case VIEW_ID_FORWARD_BUTTON:
      return isRTL ? &kNavigateBackIcon : &kNavigateForwardIcon;
    case VIEW_ID_HOME_BUTTON:
      return &kNavigateHomeIcon;
    case VIEW_ID_APP_MENU:
      return &kBrowserToolsIcon;
    default:
      break;
  }

  return nullptr;
}

- (SkColor)vectorIconColor:(BOOL)themeIsDark {
  return themeIsDark ? SK_ColorWHITE : SkColorSetRGB(0x5A, 0x5A, 0x5A);
}

- (NSImage*)browserToolsIconForFillColor:(SkColor)fillColor {
  // Create a |BrowserToolsImageRep| to draw the browser tools icon using
  // the provided fill color.
  base::scoped_nsobject<BrowserToolsImageRep> imageRep(
      [[BrowserToolsImageRep alloc]
          initWithDrawSelector:@selector(drawBrowserToolsIcon:)
                      delegate:[BrowserToolsImageRep class]]);
  [imageRep setFillColor:skia::SkColorToSRGBNSColor(fillColor)];

  // Create the image from the image rep.
  NSImage* browserToolsIcon =
      [[[NSImage alloc] initWithSize:kMDButtonIconSize] autorelease];
  [browserToolsIcon setCacheMode:NSImageCacheAlways];
  [browserToolsIcon addRepresentation:imageRep];

  return browserToolsIcon;
}

- (NSImage*)imageForIcon:(NSImage*)iconImage
     withBackgroundStyle:(ToolbarButtonImageBackgroundStyle)style {
  // Create a |ToolbarButtonImageRep| to draw the button image using
  // the provided icon and background style.
  base::scoped_nsobject<ToolbarButtonImageRep> imageRep(
      [[ToolbarButtonImageRep alloc]
          initWithDrawSelector:@selector(drawImage:)
                      delegate:[ToolbarButtonImageRep class]]);
  [imageRep setIcon:iconImage];
  [imageRep setStyle:style];

  // Create the image from the image rep.
  NSImage* image =
      [[[NSImage alloc] initWithSize:kMDButtonBounds.size] autorelease];
  [image setCacheMode:NSImageCacheAlways];
  [image addRepresentation:imageRep];

  return image;
}

- (NSImage*)image {
  // setImage: stores the image in an ivar.
  return image_.get();
}

- (void)setImage:(NSImage*)anImage {
  // We want to set the default image as the image for kDefaultState. Setting it
  // as the default image (via setImage:) can cause ghosting from the two
  // default images being drawn over each other. However we also need to keep
  // the default image around for resetButtonStateImages, so stick it in an
  // ivar.
  image_.reset([anImage retain]);
  [self resetButtonStateImages];
}

- (void)resetButtonStateImages {
  NSImage* normalIcon = nil;
  NSImage* disabledIcon = nil;
  BOOL isDarkTheme = NO;

  const gfx::VectorIcon* icon = [self vectorIcon];
  if (!icon) {
    // If the button does not have a vector icon (e.g. it's an extension
    // button), use its image. The hover, etc. images will be created using
    // imageForIcon:withBackgroundStyle: so do the same for the default image.
    // If we don't do this, the icon may not appear in the same place as in the
    // other states, causing the icon to appear to shift as you mouse over the
    // button.
    NSImage* defaultImage =
        [self imageForIcon:[self image]
              withBackgroundStyle:ToolbarButtonImageBackgroundStyle::DEFAULT];
    normalIcon = disabledIcon = defaultImage;
  } else {
    // Compute the normal and disabled vector icon colors.
    isDarkTheme = [[self window] hasDarkTheme];
    const SkColor vectorIconColor = [self vectorIconColor:isDarkTheme];
    CGFloat normalAlpha = isDarkTheme ? 0xCC : 0xFF;
    const SkColor normalColor = SkColorSetA(vectorIconColor, normalAlpha);
    const SkColor disabledColor = SkColorSetA(vectorIconColor, 0x33);

    // Create the normal and disabled state icons. These icons are always the
    // same shape but use a different color.
    if (icon == &kBrowserToolsIcon) {
      normalIcon = [self browserToolsIconForFillColor:normalColor];
      disabledIcon = [self browserToolsIconForFillColor:disabledColor];
    } else {
      normalIcon = NSImageFromImageSkia(
          gfx::CreateVectorIcon(*icon,
                                kMDButtonIconSize.width,
                                normalColor));

      // The home button has no icon for its disabled state.
      if (icon != &kNavigateReloadIcon) {
        disabledIcon = NSImageFromImageSkia(
            gfx::CreateVectorIcon(*icon,
                                  kMDButtonIconSize.width,
                                  disabledColor));
      }
    }
  }

  ImageButtonCell* theCell = base::mac::ObjCCast<ImageButtonCell>([self cell]);
  // Set the image for the default state, which is just the icon.
  [theCell setImage:normalIcon forButtonState:image_button_cell::kDefaultState];

  // Determine the appropriate image background style for the hover and pressed
  // states.
  ToolbarButtonImageBackgroundStyle hoverStyle =
      ToolbarButtonImageBackgroundStyle::HOVER;
  ToolbarButtonImageBackgroundStyle pressedStyle =
      ToolbarButtonImageBackgroundStyle::PRESSED;

  // Use the themed style for custom themes and Incognito mode.
  const ui::ThemeProvider* themeProvider = [[self window] themeProvider];
  bool incongitoMode = themeProvider && themeProvider->InIncognitoMode();
  if (isDarkTheme || incongitoMode) {
    hoverStyle = ToolbarButtonImageBackgroundStyle::HOVER_THEMED;
    pressedStyle = ToolbarButtonImageBackgroundStyle::PRESSED_THEMED;
  }

  // Create and set the hover state image.
  NSImage* hoverImage =
      [self imageForIcon:normalIcon withBackgroundStyle:hoverStyle];
  [theCell setImage:hoverImage
     forButtonState:image_button_cell::kHoverState];

  // Create and set the pressed state image.
  NSImage* pressedImage =
      [self imageForIcon:normalIcon withBackgroundStyle:pressedStyle];
  [theCell setImage:pressedImage
     forButtonState:image_button_cell::kPressedState];

  // Set the disabled state image.
  [theCell setImage:disabledIcon
     forButtonState:image_button_cell::kDisabledState];

  [self setNeedsDisplay:YES];
}

- (void)viewDidMoveToWindow {
  // In Material Design we want to catch when the button is attached to its
  // window so that we can configure its appearance based on the window's
  // theme.
  if ([self window]) {
    [self resetButtonStateImages];
  }
}

// ThemedWindowDrawing implementation.

- (void)windowDidChangeTheme {
  // Update the hover and pressed image backgrounds to match the current theme.
  [self resetButtonStateImages];
}

- (void)windowDidChangeActive {
}

@end