1.首先先创建NSStatusBarItem ,需要将item保存到属性中,否则对象会被系统的管理机制释放掉,NSStatusBar将不会出现item.
NSStatusItem * item = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
[item.button setTarget:self];
[item.button setAction:@selector(itemClick:)];
item.button.image = [NSImage imageNamed:@"online"];
self.item = item;
2.itemClick:方法,此时的item点击事件只能将处于非活动状态的应用窗口呼出到最前面,[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps];
3.增加通知监听window的变化,系统发出通知,创建属性记录windonw的状态,BOOL 类型isMin,发出通知时,改变属性的值,记录状态的变化.
//最小化时,系统发出通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(minWindown:) name:NSWindowDidMiniaturizeNotification object:nil];
//处于最小化时,点击Dock区恢复
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mainWindown:) name:NSWindowDidBecomeMainNotification object:nil];
4.在点击事件中根据属性的值,来控制如何显示windown
- (void)mainWindown:(NSNotification *)noti {
self.isMin = NO;
}
- (void)minWindown:(NSNotification *)noti {
self.isMin = YES;
}
- (IBAction)itemClick:(NSButton *)sender{
if (self.isMin == YES) {
[self.window makeKeyAndOrderFront:self];
[self.window center];
}else {
self.isMin = NO;
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps];
}
}