blob: c6b1fa63472f2a1ae689ba62690c7bc4aa994f64 (
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
|
// 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 "base/basictypes.h"
#import "base/memory/scoped_nsobject.h"
#import "chrome/browser/ui/cocoa/view_id_util.h"
NSString* const kBrowserActionGrippyDragStartedNotification =
@"BrowserActionGrippyDragStartedNotification";
NSString* const kBrowserActionGrippyDraggingNotification =
@"BrowserActionGrippyDraggingNotification";
NSString* const kBrowserActionGrippyDragFinishedNotification =
@"BrowserActionGrippyDragFinishedNotification";
namespace {
const CGFloat kAnimationDuration = 0.2;
const CGFloat kGrippyWidth = 4.0;
const CGFloat kMinimumContainerWidth = 10.0;
} // namespace
@interface BrowserActionsContainerView(Private)
// Returns the cursor that should be shown when hovering over the grippy based
// on |canDragLeft_| and |canDragRight_|.
- (NSCursor*)appropriateCursorForGrippy;
@end
@implementation BrowserActionsContainerView
@synthesize animationEndFrame = animationEndFrame_;
@synthesize canDragLeft = canDragLeft_;
@synthesize canDragRight = canDragRight_;
@synthesize grippyPinned = grippyPinned_;
@synthesize maxWidth = maxWidth_;
@synthesize userIsResizing = userIsResizing_;
#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;
[self setHidden:YES];
}
return self;
}
- (void)setResizable:(BOOL)resizable {
if (resizable == resizable_)
return;
resizable_ = resizable;
[self setNeedsDisplay:YES];
}
- (BOOL)isResizable {
return resizable_;
}
- (void)resetCursorRects {
[self discardCursorRects];
[self addCursorRect:grippyRect_ cursor:[self appropriateCursorForGrippy]];
}
- (BOOL)acceptsFirstResponder {
return YES;
}
- (void)mouseDown:(NSEvent*)theEvent {
initialDragPoint_ = [self convertPoint:[theEvent locationInWindow]
fromView:nil];
if (!resizable_ ||
!NSMouseInRect(initialDragPoint_, grippyRect_, [self isFlipped]))
return;
lastXPos_ = [self frame].origin.x;
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);
canDragLeft_ = (withDelta <= initialDragPoint_.x) &&
(NSWidth(containerFrame) < maxWidth_);
if ((dX < 0.0 && !canDragLeft_) || (dX > 0.0 && !canDragRight_))
return;
containerFrame.size.width =
std::max(NSWidth(containerFrame) - dX, kMinimumContainerWidth);
if (NSWidth(containerFrame) == kMinimumContainerWidth)
return;
containerFrame.origin.x += dX;
[self setFrame:containerFrame];
[self setNeedsDisplay:YES];
[[NSNotificationCenter defaultCenter]
postNotificationName:kBrowserActionGrippyDraggingNotification
object:self];
lastXPos_ += dX;
}
- (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];
lastXPos_ = frame.origin.x;
CGFloat dX = frame.size.width - width;
frame.size.width = width;
NSRect newFrame = NSOffsetRect(frame, dX, 0);
if (animate) {
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:kAnimationDuration];
[[self animator] setFrame:newFrame];
[NSAnimationContext endGrouping];
animationEndFrame_ = newFrame;
} else {
[self setFrame:newFrame];
[self setNeedsDisplay:YES];
}
}
- (CGFloat)resizeDeltaX {
return [self frame].origin.x - lastXPos_;
}
#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;
}
@end
|