I want to design a panel acting a little like a popover: when mouse down outside it, it may dismiss or hide itself.
But I have no idea about how to achieve this. What I know is that a view could handle -mouseDown
, -mouseUp
, etc. But when mouse down at other place? I don't know how to catch this event.
Further discussion with Bavarious
:
I am actually doing a study on status bar. There is a question I followed. And the sample code I studied.
As the sample code did, I overwrote the view described previously and set it with the status bar item's -setView:
method. Most of my codes in my work are quite the same as the sample code. Here are some parts of codes those I think are related to my confusion (BTW, ARC is used):
...
@property (nonatomic) SEL disclickAction; // Called when "dismissed"
@property (nonatomic) SEL action; // Called when selected
@property (nonatomic, assign) id target;
...
- (void)dealloc
{
NSLog(@"%@ dealloc", self);
[self invalidate];
//[super dealloc];
}
- (void)mouseDown:(NSEvent *)theEvent
{
[self setHighlighted:![self isHighlighted]];
if (_target && _action &&
[_target respondsToSelector:_action])
{
[NSApp sendAction:_action to:_target from:self];
}
}
// Here is the code that Bavarious taught me:
- (void)setDisclickAction:(SEL)disclickAction
{
_disclickAction = disclickAction;
if (!_mouseEventMonitor)
{
if (_disclickAction)
{
self.mouseEventMonitor = [NSEvent
addLocalMonitorForEventsMatchingMask:(NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask)
handler:^NSEvent *(NSEvent *event) {
if (event.window != self.window)
{
[self actionDisclick:nil];
}
return event;
}];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(actionDisclick:)
name:NSApplicationDidResignActiveNotification
object:nil];
}
}
else if (!_disclickAction) // cancel operation
{
[NSEvent removeMonitor:_mouseEventMonitor];
_mouseEventMonitor = nil;
}
}
Here is my screen, for example (the yellow cartoon face is my status bar item):
When left mouse down at positions above:
A: The view's mouseDown
called, and then the local observer of mouse down event notified.
B: local observer of mouse down event notified.
C: Resign application event notified.
D: No event. Neither the local observer of mouse down event. And this is the problem.