summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/extensions/browser_actions_container_view.mm
blob: e3586b6282be0d94915186775662314e8a0ba22b (plain)
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
// 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/extensions/browser_actions_container_view.h"

#include <algorithm>
#include <utility>

#import "chrome/browser/ui/cocoa/view_id_util.h"
#include "grit/theme_resources.h"
#include "ui/base/cocoa/appkit_utils.h"
#include "ui/events/keycodes/keyboard_code_conversion_mac.h"

NSString* const kBrowserActionGrippyDragStartedNotification =
    @"BrowserActionGrippyDragStartedNotification";
NSString* const kBrowserActionGrippyDraggingNotification =
    @"BrowserActionGrippyDraggingNotification";
NSString* const kBrowserActionGrippyDragFinishedNotification =
    @"BrowserActionGrippyDragFinishedNotification";
NSString* const kBrowserActionsContainerWillAnimate =
    @"BrowserActionsContainerWillAnimate";
NSString* const kBrowserActionsContainerMouseEntered =
    @"BrowserActionsContainerMouseEntered";
NSString* const kBrowserActionsContainerAnimationEnded =
    @"BrowserActionsContainerAnimationEnded";
NSString* const kTranslationWithDelta =
    @"TranslationWithDelta";
NSString* const kBrowserActionsContainerReceivedKeyEvent =
    @"BrowserActionsContainerReceivedKeyEvent";
NSString* const kBrowserActionsContainerKeyEventKey =
    @"BrowserActionsContainerKeyEventKey";

namespace {
const CGFloat kAnimationDuration = 0.2;
const CGFloat kGrippyWidth = 3.0;
const CGFloat kMinimumContainerWidth = 3.0;
}  // namespace

@interface BrowserActionsContainerView(Private)
// Returns the cursor that should be shown when hovering over the grippy based
// on |canDragLeft_| and |canDragRight_|.
- (NSCursor*)appropriateCursorForGrippy;

// Returns the maximum allowed size for the container.
- (CGFloat)maxAllowedWidth;
@end

@implementation BrowserActionsContainerView

@synthesize canDragLeft = canDragLeft_;
@synthesize canDragRight = canDragRight_;
@synthesize grippyPinned = grippyPinned_;
@synthesize maxDesiredWidth = maxDesiredWidth_;
@synthesize userIsResizing = userIsResizing_;
@synthesize delegate = delegate_;

#pragma mark -
#pragma mark Overridden Class Functions

- (id)initWithFrame:(NSRect)frameRect {
  if ((self = [super initWithFrame:frameRect])) {
    grippyRect_ = NSMakeRect(0.0, 0.0, kGrippyWidth, NSHeight([self bounds]));
    canDragLeft_ = YES;
    canDragRight_ = YES;
    resizable_ = YES;

    resizeAnimation_.reset([[NSViewAnimation alloc] init]);
    [resizeAnimation_ setDuration:kAnimationDuration];
    [resizeAnimation_ setAnimationBlockingMode:NSAnimationNonblocking];
    [resizeAnimation_ setDelegate:self];

    [self setHidden:YES];
  }
  return self;
}

- (void)dealloc {
  if (trackingArea_.get())
    [self removeTrackingArea:trackingArea_.get()];
  [super dealloc];
}

- (void)drawRect:(NSRect)rect {
  [super drawRect:rect];
  if (highlight_) {
    ui::DrawNinePartImage(
        [self bounds], *highlight_, NSCompositeSourceOver, 1.0, true);
  }
}

- (void)viewDidMoveToWindow {
  if (isOverflow_) {
    // Yet another Cocoa oddity: Custom views in menu items in Cocoa, by
    // default, won't receive key events. However, if we make this the first
    // responder when it's moved to a window, it will, and it will behave
    // properly (i.e., will only receive key events if the menu item is
    // highlighted, not for any key event in the menu). More strangely,
    // setting this to be first responder at any other time (such as calling
    // [[containerView window] makeFirstResponder:containerView] when the menu
    // item is highlighted) does *not* work (it messes up the currently-
    // highlighted item).
    // Since this seems to have the right behavior, use it.
    [[self window] makeFirstResponder:self];
  }
}

- (void)setTrackingEnabled:(BOOL)enabled {
  if (enabled) {
    trackingArea_.reset(
        [[CrTrackingArea alloc] initWithRect:NSZeroRect
                                     options:NSTrackingMouseEnteredAndExited |
                                             NSTrackingActiveInActiveApp |
                                             NSTrackingInVisibleRect
                                       owner:self
                                    userInfo:nil]);
    [self addTrackingArea:trackingArea_.get()];
  } else if (trackingArea_.get()) {
    [self removeTrackingArea:trackingArea_.get()];
    [trackingArea_.get() clearOwner];
    trackingArea_.reset(nil);
  }
}

- (BOOL)trackingEnabled {
  return trackingArea_.get() != nullptr;
}

- (void)keyDown:(NSEvent*)theEvent {
  // If this is the overflow container, we handle three key events: left, right,
  // and space. Left and right navigate the actions within the container, and
  // space activates the current one. We have to handle this ourselves, because
  // Cocoa doesn't treat custom views with subviews in menu items differently
  // than any other menu item, so it would otherwise be impossible to navigate
  // to a particular action from the keyboard.
  ui::KeyboardCode key = ui::KeyboardCodeFromNSEvent(theEvent);
  BOOL shouldProcess = isOverflow_ &&
      (key == ui::VKEY_RIGHT || key == ui::VKEY_LEFT || key == ui::VKEY_SPACE);

  // If this isn't the overflow container, or isn't one of the keys we process,
  // forward the event on.
  if (!shouldProcess) {
    [super keyDown:theEvent];
    return;
  }

  // TODO(devlin): The keyboard navigation should be adjusted for RTL, but right
  // now we only ever display the extension items in the same way (LTR) on Mac.
  BrowserActionsContainerKeyAction action = BROWSER_ACTIONS_INVALID_KEY_ACTION;
  switch (key) {
    case ui::VKEY_RIGHT:
      action = BROWSER_ACTIONS_INCREMENT_FOCUS;
      break;
    case ui::VKEY_LEFT:
      action = BROWSER_ACTIONS_DECREMENT_FOCUS;
      break;
    case ui::VKEY_SPACE:
      action = BROWSER_ACTIONS_EXECUTE_CURRENT;
      break;
    default:
      NOTREACHED();  // Should have weeded this case out above.
  }

  DCHECK_NE(BROWSER_ACTIONS_INVALID_KEY_ACTION, action);
  NSDictionary* userInfo = @{ kBrowserActionsContainerKeyEventKey : @(action) };
  [[NSNotificationCenter defaultCenter]
      postNotificationName:kBrowserActionsContainerReceivedKeyEvent
                    object:self
                    userInfo:userInfo];
  [super keyDown:theEvent];
}

- (void)setHighlight:(scoped_ptr<ui::NinePartImageIds>)highlight {
  if (highlight || highlight_) {
    highlight_ = std::move(highlight);
    // We don't allow resizing when the container is highlighting.
    resizable_ = highlight.get() == nullptr;
    [self setNeedsDisplay:YES];
  }
}

- (BOOL)isHighlighting {
  return highlight_.get() != nullptr;
}

- (void)setIsOverflow:(BOOL)isOverflow {
  if (isOverflow_ != isOverflow) {
    isOverflow_ = isOverflow;
    resizable_ = !isOverflow_;
    [self setNeedsDisplay:YES];
  }
}

- (void)resetCursorRects {
  [self addCursorRect:grippyRect_ cursor:[self appropriateCursorForGrippy]];
}

- (BOOL)acceptsFirstResponder {
  // The overflow container needs to receive key events to handle in-item
  // navigation. The top-level container should not become first responder,
  // allowing focus travel to proceed to the first action.
  return isOverflow_;
}

- (void)mouseEntered:(NSEvent*)theEvent {
  [[NSNotificationCenter defaultCenter]
      postNotificationName:kBrowserActionsContainerMouseEntered
                    object:self];
}

- (void)mouseDown:(NSEvent*)theEvent {
  initialDragPoint_ = [self convertPoint:[theEvent locationInWindow]
                                fromView:nil];
  if (!resizable_ ||
      !NSMouseInRect(initialDragPoint_, grippyRect_, [self isFlipped]))
    return;

  userIsResizing_ = YES;

  [[self appropriateCursorForGrippy] push];
  // Disable cursor rects so that the Omnibox and other UI elements don't push
  // cursors while the user is dragging. The cursor should be grippy until
  // the |-mouseUp:| message is received.
  [[self window] disableCursorRects];

  [[NSNotificationCenter defaultCenter]
      postNotificationName:kBrowserActionGrippyDragStartedNotification
                    object:self];
}

- (void)mouseUp:(NSEvent*)theEvent {
  if (!userIsResizing_)
    return;

  [NSCursor pop];
  [[self window] enableCursorRects];

  userIsResizing_ = NO;
  [[NSNotificationCenter defaultCenter]
      postNotificationName:kBrowserActionGrippyDragFinishedNotification
                    object:self];
}

- (void)mouseDragged:(NSEvent*)theEvent {
  if (!userIsResizing_)
    return;

  NSPoint location = [self convertPoint:[theEvent locationInWindow]
                               fromView:nil];
  NSRect containerFrame = [self frame];
  CGFloat dX = [theEvent deltaX];
  CGFloat withDelta = location.x - dX;
  canDragRight_ = (withDelta >= initialDragPoint_.x) &&
      (NSWidth(containerFrame) > kMinimumContainerWidth);
  CGFloat maxAllowedWidth = [self maxAllowedWidth];
  containerFrame.size.width =
      std::max(NSWidth(containerFrame) - dX, kMinimumContainerWidth);
  canDragLeft_ = withDelta <= initialDragPoint_.x &&
      NSWidth(containerFrame) < maxDesiredWidth_ &&
      NSWidth(containerFrame) < maxAllowedWidth;

  if ((dX < 0.0 && !canDragLeft_) || (dX > 0.0 && !canDragRight_))
    return;

  if (NSWidth(containerFrame) <= kMinimumContainerWidth)
    return;

  grippyPinned_ = NSWidth(containerFrame) >= maxAllowedWidth;
  containerFrame.origin.x += dX;

  [self setFrame:containerFrame];
  [self setNeedsDisplay:YES];

  [[NSNotificationCenter defaultCenter]
      postNotificationName:kBrowserActionGrippyDraggingNotification
                    object:self];
}

- (void)animationDidEnd:(NSAnimation*)animation {
  // We notify asynchronously so that the animation fully finishes before any
  // listeners do work.
  [self performSelector:@selector(notifyAnimationEnded)
              withObject:self
              afterDelay:0];
}

- (void)animationDidStop:(NSAnimation*)animation {
  // We notify asynchronously so that the animation fully finishes before any
  // listeners do work.
  [self performSelector:@selector(notifyAnimationEnded)
              withObject:self
              afterDelay:0];
}

- (void)notifyAnimationEnded {
  [[NSNotificationCenter defaultCenter]
      postNotificationName:kBrowserActionsContainerAnimationEnded
                    object:self];
}

- (ViewID)viewID {
  return VIEW_ID_BROWSER_ACTION_TOOLBAR;
}

#pragma mark -
#pragma mark Public Methods

- (void)resizeToWidth:(CGFloat)width animate:(BOOL)animate {
  width = std::max(width, kMinimumContainerWidth);
  NSRect frame = [self frame];

  CGFloat maxAllowedWidth = [self maxAllowedWidth];
  width = std::min(maxAllowedWidth, width);

  CGFloat dX = frame.size.width - width;
  frame.size.width = width;
  NSRect newFrame = NSOffsetRect(frame, dX, 0);

  grippyPinned_ = width == maxAllowedWidth;

  [self stopAnimation];

  if (animate) {
    NSDictionary* animationDictionary = @{
      NSViewAnimationTargetKey : self,
      NSViewAnimationStartFrameKey : [NSValue valueWithRect:[self frame]],
      NSViewAnimationEndFrameKey : [NSValue valueWithRect:newFrame]
    };
    [resizeAnimation_ setViewAnimations:@[ animationDictionary ]];
    [resizeAnimation_ startAnimation];

    [[NSNotificationCenter defaultCenter]
        postNotificationName:kBrowserActionsContainerWillAnimate
                      object:self];
  } else {
    [self setFrame:newFrame];
    [self setNeedsDisplay:YES];
  }
}

- (NSRect)animationEndFrame {
  if ([resizeAnimation_ isAnimating]) {
    NSRect endFrame = [[[[resizeAnimation_ viewAnimations] objectAtIndex:0]
        valueForKey:NSViewAnimationEndFrameKey] rectValue];
    return endFrame;
  } else {
    return [self frame];
  }
}

- (BOOL)isAnimating {
  return [resizeAnimation_ isAnimating];
}

- (void)stopAnimation {
  if ([resizeAnimation_ isAnimating])
    [resizeAnimation_ stopAnimation];
}

#pragma mark -
#pragma mark Private Methods

// Returns the cursor to display over the grippy hover region depending on the
// current drag state.
- (NSCursor*)appropriateCursorForGrippy {
  NSCursor* retVal;
  if (!resizable_ || (!canDragLeft_ && !canDragRight_)) {
    retVal = [NSCursor arrowCursor];
  } else if (!canDragLeft_) {
    retVal = [NSCursor resizeRightCursor];
  } else if (!canDragRight_) {
    retVal = [NSCursor resizeLeftCursor];
  } else {
    retVal = [NSCursor resizeLeftRightCursor];
  }
  return retVal;
}

- (CGFloat)maxAllowedWidth {
  return delegate_ ? delegate_->GetMaxAllowedWidth() : CGFLOAT_MAX;
}

@end