Skip to content

Commit f5f0014

Browse files
committed
Merge pull request brotherbard#60 from Uncommon/action
Open the action menu on mousedown
2 parents 07d4bef + 6ae5d07 commit f5f0014

File tree

1 file changed

+9
-70
lines changed

1 file changed

+9
-70
lines changed

PBSourceViewCell.m

Lines changed: 9 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
@interface PBSourceViewCell()
1414
- (NSRect)infoButtonRectForBounds:(NSRect)bounds;
15+
- (void)mouseEntered:(NSEvent *)event;
16+
- (void)mouseExited:(NSEvent *)event;
1517
@end
1618

1719
@implementation PBSourceViewCell
@@ -91,91 +93,28 @@ - (void)drawInteriorWithFrame:(NSRect)bounds inView:(NSView *)controlView {
9193
}
9294
}
9395

94-
95-
//- (NSUInteger)hitTestForEvent:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView {
96-
// if (showsActionButton) {
97-
// NSPoint point = [controlView convertPoint:[event locationInWindow] fromView:nil];
98-
// //
99-
// // NSRect titleRect = [self titleRectForBounds:cellFrame];
100-
// // if (NSMouseInRect(point, titleRect, [controlView isFlipped])) {
101-
// // return NSCellHitContentArea | NSCellHitEditableTextArea;
102-
// // }
103-
// //
104-
// // NSRect imageRect = [self imageRectForBounds:cellFrame];
105-
// // if (NSMouseInRect(point, imageRect, [controlView isFlipped])) {
106-
// // return NSCellHitContentArea;
107-
// // }
108-
// //
109-
// // // Did we hit the sub title?
110-
// // NSAttributedString *attributedSubTitle = [self attributedSubTitle];
111-
// // if ([attributedSubTitle length] > 0) {
112-
// // NSRect attributedSubTitleRect = [self rectForSubTitleBasedOnTitleRect:titleRect inBounds:cellFrame];
113-
// // if (NSMouseInRect(point, attributedSubTitleRect, [controlView isFlipped])) {
114-
// // // Notice that this text isn't an editable area. Clicking on it won't begin an editing session.
115-
// // return NSCellHitContentArea;
116-
// // }
117-
// // }
118-
//
119-
// // How about the info button?
120-
// NSRect infoButtonRect = [self infoButtonRectForBounds:cellFrame];
121-
// if (NSMouseInRect(point, infoButtonRect, [controlView isFlipped])) {
122-
// return NSCellHitContentArea | NSCellHitTrackableArea;
123-
// }
124-
// }
125-
//
126-
// return [super hitTestForEvent:event inRect:cellFrame ofView:controlView];
127-
//}
128-
129-
//+ (BOOL)prefersTrackingUntilMouseUp {
130-
// // NSCell returns NO for this by default. If you want to have trackMouse:inRect:ofView:untilMouseUp: always track until the mouse is up, then you MUST return YES. Otherwise, strange things will happen.
131-
// return YES;
132-
//}
133-
13496
// Mouse tracking -- the only part we want to track is the "info" button
13597
- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)flag {
136-
// [self setControlView:controlView];
137-
//
138-
NSRect infoButtonRect = [self infoButtonRectForBounds:cellFrame];
139-
if ([theEvent type] != NSLeftMouseUp) {
140-
// This is VERY simple event tracking. We simply check to see if the mouse is in the "i" button or not and dispatch entered/exited mouse events
141-
NSPoint point = [controlView convertPoint:[theEvent locationInWindow] fromView:nil];
142-
BOOL mouseInButton = NSMouseInRect(point, infoButtonRect, [controlView isFlipped]);
143-
if (iMouseDownInInfoButton != mouseInButton) {
144-
iMouseDownInInfoButton = mouseInButton;
145-
[controlView setNeedsDisplayInRect:cellFrame];
146-
}
147-
if ([theEvent type] == NSMouseEntered || [theEvent type] == NSMouseExited) {
148-
[NSApp sendEvent:theEvent];
149-
}
150-
// Note that we process mouse entered and exited events and dispatch them to properly handle updates
151-
theEvent = [[controlView window] nextEventMatchingMask:(NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSMouseEnteredMask | NSMouseExitedMask)];
152-
}
153-
154-
// Another way of implementing the above code would be to keep an NSButtonCell as an ivar, and simply call trackMouse:inRect:ofView:untilMouseUp: on it, if the tracking area was inside of it.
155-
15698
NSPoint locationOfTouch = [controlView convertPoint:[theEvent locationInWindow] fromView:nil];
15799

158100
BOOL mouseInButton = NSMouseInRect(locationOfTouch, [self infoButtonRectForBounds:cellFrame], [controlView isFlipped]);
159101
if (mouseInButton) {
102+
[self mouseEntered:theEvent];
160103
// show menu
161104
NSMenu *menu = [self menuForEvent:theEvent inRect:cellFrame ofView:controlView];
162105
if (menu){
163106
[NSMenu popUpContextMenu:menu withEvent:theEvent forView:controlView];
107+
return YES;
164108
}
165109
}
166110

167-
if (iMouseDownInInfoButton) {
168-
// Send the action, and redisplay
169-
iMouseDownInInfoButton = NO;
170-
[controlView setNeedsDisplayInRect:cellFrame];
171-
}
111+
if (iMouseDownInInfoButton) {
112+
// Send the action, and redisplay
113+
iMouseDownInInfoButton = NO;
114+
[controlView setNeedsDisplayInRect:cellFrame];
115+
}
172116

173117
return [super trackMouse:theEvent inRect:cellFrame ofView:controlView untilMouseUp:flag];
174-
175-
176-
//
177-
// // We return YES since the mouse was released while we were tracking. Not returning YES when you processed the mouse up is an easy way to introduce bugs!
178-
// return YES;
179118
}
180119

181120

0 commit comments

Comments
 (0)