Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

objective c - Hide MAAttachedWindow when clicking outside

I'm using an MAAttachedWindow to display a custom window under a NSStatusItem in the Menubar. Everything works fine, but I can't find an easy way to hide it when the user clicks outside of the window. I want to implement this behavior because it's what the user expects.

This is the code used to display the MAAttachedWindow:

- (void)toggleAttachedWindowAtPoint:(NSPoint)pt {
    if (!self.attachedWindow) {  
        self.attachedWindow = [[MAAttachedWindow alloc] initWithView:logView
              attachedToPoint:pt 
               inWindow:nil 
                 onSide:MAPositionBottom 
                atDistance:5.0];

  [self.attachedWindow setLevel:kCGMaximumWindowLevel];
 }

 if(isVisible)
  [self.attachedWindow makeKeyAndOrderFront:self];
 else
  [self.attachedWindow orderOut];
}

This code gets triggered by an NSStatusItem with a custom view which intercepts a click on it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should be able to do this via the window's delegate method:

- (void)windowDidResignKey:(NSNotification *)notification

Set yourself as the window's delegate, and implement that to call through to your toggle method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...