OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 | |
Mark Mentovai
2011/08/11 00:51:17
#include guards and #pragma once.
| |
5 #import <Cocoa/Cocoa.h> | |
6 | |
7 // The design of this class is extraordinarily poor. Apologies to all clients in | |
8 // advance. Unfortunately, the lack of multiple inheritance and our desire to | |
9 // avoid runtime hacks makes this convoluted dance necessary. | |
10 // | |
11 // Buttons that want to be draggable should implement the Mixin protocol below | |
12 // and keep an instance of the Impl as an ivar. The button should forward mouse | |
13 // events to the impl, which will tell the button whether or not to call super | |
14 // and let the event be handled normally. | |
15 // | |
16 // If the impl decides to do work on the event, methods of the mixin protocol | |
17 // may be called. Some of the methods declared in that protocol have base | |
18 // implementations. If the method is not implemented by the button, that base | |
19 // implementation will be called. Otherwise, the button's implementation will | |
20 // be called first and the DraggableButtonResult will be used to determine | |
21 // whether the base implementation should be called. This requires the client to | |
22 // understand what the base does. | |
23 | |
24 enum DraggableButtonResult { | |
25 // Return values for Impl methods. | |
26 kDraggableButtonImplDidWork, | |
27 kDraggableButtonMixinCallSuper, | |
28 | |
29 // Return values for Mixin methods. | |
30 kDraggableButtonMixinDidWork, | |
31 kDraggableButtonImplUseBase, | |
32 }; | |
33 | |
34 // Mixin Protocol ////////////////////////////////////////////////////////////// | |
35 | |
36 // Buttons that make use of the below impl need to conform to this protocol. | |
37 @protocol DraggableButtonMixin | |
38 | |
39 @required | |
40 | |
41 // Called when a drag should start. Implement this to do any pasteboard | |
42 // manipulation and begin the drag, usually with | |
43 // -dragImage:at:offset:event:. Subclasses must call one of the blocking | |
44 // -drag* methods of NSView when implementing this method. | |
45 - (void)beginDrag:(NSEvent*)dragEvent; | |
46 | |
47 @optional | |
48 | |
49 // Called if the actsOnMouseDown property is set. Fires the button's action and | |
50 // tracks the click. | |
51 - (DraggableButtonResult)performMouseDownAction:(NSEvent*)theEvent; | |
52 | |
53 // Implement if you want to do any extra work on mouseUp, after a mouseDown | |
54 // action has already fired. | |
55 - (DraggableButtonResult)secondaryMouseUpAction:(BOOL)wasInside; | |
56 | |
57 // Resets the draggable state of the button after dragging is finished. This is | |
Mark Mentovai
2011/08/11 00:51:17
I thought you were a one-space-after-period kind o
Robert Sesek
2011/08/11 16:06:00
I am. I just didn't feel like fixing all the old c
| |
58 // called by DraggableButtonImpl when the beginDrag call returns. | |
59 - (DraggableButtonResult)endDrag; | |
60 | |
61 // Decides whether we should treat the click as a cue to start dragging, or | |
Mark Mentovai
2011/08/11 00:51:17
We, and on line 70.
| |
62 // instead call the mouseDown/mouseUp handler as appropriate. | |
63 // Implement if you want to do something tricky when making the decision. | |
64 - (DraggableButtonResult)deltaIndicatesDragStartWithXDelta:(float)xDelta | |
65 yDelta:(float)yDelta | |
Mark Mentovai
2011/08/11 00:51:17
Seems like you could have lined up the colons here
Robert Sesek
2011/08/11 16:06:00
Eh. Methods that fly off to the right like that ar
| |
66 xHysteresis:(float)xHysteresis | |
67 yHysteresis:(float)yHysteresis | |
68 indicates:(BOOL*)result; | |
69 | |
70 // Decides if we now have enough information to stop tracking the mouse. | |
71 // It's deltaIndicatesDragStartWithXDelta, however, that decides whether it's a | |
72 // drag or not. Implement if you want to do something tricky when making the | |
73 // decision. | |
74 - (DraggableButtonResult)deltaIndicatesConclusionReachedWithXDelta:(float)xDelta | |
75 yDelta:(float)yDelta | |
76 xHysteresis:(float)xHysteresis | |
77 yHysteresis:(float)yHysteresis | |
78 indicates:(BOOL*)result; | |
79 | |
80 @end | |
Mark Mentovai
2011/08/11 00:51:17
Not too bad so far.
| |
81 | |
82 // Impl Interface ////////////////////////////////////////////////////////////// | |
83 | |
84 // Class for buttons that can be drag sources. If the mouse is clicked and moved | |
85 // more than a given distance, this class will call |-beginDrag:| instead of | |
86 // |-performClick:|. Subclasses should override these two methods. | |
87 @interface DraggableButtonImpl : NSObject { | |
88 @private | |
89 // The button for which this class is implementing stuff. | |
90 NSButton<DraggableButtonMixin>* button_; | |
91 | |
92 // Is this a draggable type of button? | |
93 BOOL draggable_; | |
94 | |
95 // Has the action already fired for this click? | |
96 BOOL actionHasFired_; | |
97 | |
98 // Does button action happen on mouse down when possible? | |
99 BOOL actsOnMouseDown_; | |
100 | |
101 NSTimeInterval durationMouseWasDown_; | |
102 NSTimeInterval whenMouseDown_; | |
103 } | |
104 | |
105 @property NSTimeInterval durationMouseWasDown; | |
Mark Mentovai
2011/08/11 00:51:17
atomic? 107 too.
| |
106 | |
107 @property NSTimeInterval whenMouseDown; | |
108 | |
109 // Whether the action has already fired for this click. | |
110 @property(nonatomic) BOOL actionHasFired; | |
111 | |
112 // Enable or disable dragability for special buttons like "Other Bookmarks". | |
113 @property(nonatomic) BOOL draggable; | |
114 | |
115 // If it has a popup menu, for example, we want to perform the action on mouse | |
116 // down, if possible (as long as user still gets chance to drag, if | |
117 // appropriate). | |
118 @property(nonatomic) BOOL actsOnMouseDown; | |
119 | |
120 // Designated initializer. | |
121 - (id)initWithButton:(NSButton<DraggableButtonMixin>*)button; | |
122 | |
123 // NSResponder implementation. NSButton subclasses should invoke these methods | |
124 // and only call super if the return value indicates such. | |
125 - (DraggableButtonResult)mouseDown:(NSEvent*)event; | |
Mark Mentovai
2011/08/11 00:51:17
Hmm. I guess this is technically unportable, but i
Robert Sesek
2011/08/11 16:06:00
Why wouldn't this be "portable"? This inherits fro
Mark Mentovai
2011/08/11 16:11:45
rsesek wrote:
| |
126 - (DraggableButtonResult)mouseUp:(NSEvent*)event; | |
127 | |
128 @end | |
OLD | NEW |