// // KMStatusBar.m // PDF Reader Pro Edition // // Created by 王帅 on 2018/9/30. // #import "KMStatusBar.h" #import "IAPProductsManager.h" #import @interface KMStatusBar () @property (nonatomic,retain) NSStatusItem *convertStatusItem; @property (nonatomic, strong) NSWindowController *windowC; @end @implementation KMStatusBar #pragma mark - Init Methods + (KMStatusBar *)systemStatusBar { static KMStatusBar *singleton = nil; static dispatch_once_t pred; dispatch_once(&pred, ^{ singleton = [[KMStatusBar alloc] init]; }); return singleton; } - (instancetype)init { if (self = [super init]) { if ([self isShow]) { [self showConvertStatusItem]; } } return self; } - (void)dealloc { // [_convertStatusItem release]; // [super dealloc]; } #pragma mark - Public Methods - (BOOL)isShow { if ([[NSUserDefaults standardUserDefaults] objectForKey:@"KMStatusBarIsShowKey"]) { return [[NSUserDefaults standardUserDefaults] boolForKey:@"KMStatusBarIsShowKey"]; } return YES; } - (void)setIsShow:(BOOL)isShow { [[NSUserDefaults standardUserDefaults] setBool:isShow forKey:@"KMStatusBarIsShowKey"]; [[NSUserDefaults standardUserDefaults] synchronize]; if (isShow) { [self showConvertStatusItem]; } else { [self removeConvertStatusItem]; } } #pragma mark - Private Methods - (void)showConvertStatusItem { if (!self.convertStatusItem) { NSMenu *menu = [[NSMenu alloc] init]; self.convertStatusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]; [self.convertStatusItem setImage:[NSImage imageNamed:@"KMImageNameToolbarConvert"]]; [self.convertStatusItem setHighlightMode:YES]; [self.convertStatusItem setTarget:self]; [self.convertStatusItem setMenu:menu]; NSMenuItem *item = [menu addItemWithTitle:NSLocalizedString(@"PDF to Office", nil) action:@selector(convertItemAction:) keyEquivalent:@""]; item.target = self; [menu addItem:[NSMenuItem separatorItem]]; item = [menu addItemWithTitle:NSLocalizedString(@"New From Clipboard", nil) action:@selector(newFromClipboardItemAction:) keyEquivalent:@""]; item.target = self; item = [menu addItemWithTitle:NSLocalizedString(@"New From Images", nil) action:@selector(newFromImagesItemAction:) keyEquivalent:@""]; item.target = self; item = [menu addItemWithTitle:NSLocalizedString(@"New From File", nil) action:@selector(newFromFileItemAction:) keyEquivalent:@""]; item.target = self; item = [menu addItemWithTitle:NSLocalizedString(@"New From Web Page", nil) action:@selector(newFromWebPageItemAction:) keyEquivalent:@""]; item.target = self; [menu addItem:[NSMenuItem separatorItem]]; item = [menu addItemWithTitle:NSLocalizedString(@"Crosshair Screenshot", nil) action:@selector(screenshotFromSelectionItemAction:) keyEquivalent:@""]; item.target = self; item = [menu addItemWithTitle:NSLocalizedString(@"Window Screenshot", nil) action:@selector(screenshotFromWindowItemAction:) keyEquivalent:@""]; item.target = self; item = [menu addItemWithTitle:NSLocalizedString(@"Fullscreen Screenshot", nil) action:@selector(screenshotFromEntireItemAction:) keyEquivalent:@""]; item.target = self; [menu addItem:[NSMenuItem separatorItem]]; item = [menu addItemWithTitle:NSLocalizedStringFromTable(@"Preferences…", @"", nil) action:@selector(preferencesItemAction:) keyEquivalent:@""]; item.target = self; } } - (void)removeConvertStatusItem { if (self.convertStatusItem) { [[NSStatusBar systemStatusBar] removeStatusItem:self.convertStatusItem]; self.convertStatusItem = nil; } } #pragma mark - Item Action - (void)convertItemAction:(id)sender { if ([IAPProductsManager defaultManager].isAvailableAllFunction) { NSOpenPanel *openPanel = [NSOpenPanel openPanel]; [openPanel setAllowedFileTypes:@[@"pdf",@"PDF"]]; [openPanel setAllowsMultipleSelection:NO]; [openPanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) { if (result == NSModalResponseOK) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ KMBatchWindowController *batchWindowController = [[KMBatchWindowController alloc] initWithWindowNibName:@"KMBatchWindowController"]; [batchWindowController.window makeKeyAndOrderFront:nil]; NSMutableArray *array = [NSMutableArray array]; [array addObject:openPanel.URL]; batchWindowController.inputData = array; }); } }]; } else { NSOpenPanel *openPanel = [NSOpenPanel openPanel]; [openPanel setAllowedFileTypes:@[@"pdf"]]; openPanel.message = NSLocalizedString(@"To select multiple files press cmd ⌘ button on keyboard and click on the target files one by one.", nil); [openPanel setAllowsMultipleSelection:NO]; [openPanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) { if (result == NSModalResponseOK) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ CPDFDocument *document = [[CPDFDocument alloc] initWithURL:openPanel.URL]; }); } }]; } } - (void)newFromClipboardItemAction:(id)sender { // [[self fetchBrowserWindowC] newDocumentFromClipboard:sender]; } - (void)newFromImagesItemAction:(id)sender { // [[self fetchBrowserWindowC] createPDFFromImage]; } - (void)newFromFileItemAction:(id)sender { // [[self fetchBrowserWindowC] importFromFile:sender]; } - (void)newFromWebPageItemAction:(id)sender { // [[self fetchBrowserWindowC] importFromWebPage:nil]; } - (void)screenshotFromSelectionItemAction:(id)sender { // [[self fetchBrowserWindowC] screenShot_SelectArea:nil]; } - (void)screenshotFromWindowItemAction:(id)sender { // [[self fetchBrowserWindowC] screenShot_Window:nil]; } - (void)screenshotFromEntireItemAction:(id)sender { // [[self fetchBrowserWindowC] screenShot_FullScreenDelay:nil]; } - (void)preferencesItemAction:(id)sender { [[SettingsWindowController shared] showWindow:self]; } - (KMBrowserWindowController *)fetchBrowserWindowC { CTBrowserWindow *flagWin = nil; for (NSWindow *win in NSApp.windows) { if ([win isKindOfClass:[CTBrowserWindow class]]) { if (win.isVisible) { flagWin = win; break; } } } return flagWin.windowController; } @end