123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- //
- // KMStatusBar.m
- // PDF Reader Pro Edition
- //
- // Created by 王帅 on 2018/9/30.
- //
- #import "KMStatusBar.h"
- #import "IAPProductsManager.h"
- #import <PDF_Reader_Pro-Swift.h>
- @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(), ^{
- KMBatchOperateBaseWindowController *baseWindowController = [[KMBatchOperateBaseWindowController alloc] initWithWindowNibName:@"KMBatchOperateBaseWindowController"];
- KMBatchOperateFile *file = [[KMBatchOperateFile alloc] initWithFilePath:openPanel.URL.path type:KMBatchOperationTypeConvert];
- NSMutableArray *arr = [NSMutableArray array];
- [arr addObject:file];
- [baseWindowController.window makeKeyAndOrderFront:nil];
- [baseWindowController checkNeedPasswordSwitchToOperateTypeWithOperateType:KMBatchOperationTypeConvert files:arr];
- [baseWindowController switchToConvertTypeWithConvertType:[IAPProductsManager defaultManager].isAvailableAdvancedPDFToOffice? KMConvertWithPDFTypeWordAdvance:KMConvertWithPDFTypeWordStandard];
- });
- }
- }];
- } 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];
- KMConvertWindowController *com = [[KMConvertWindowController alloc] initWithDocumemtV:document currentPage:nil convertT:KMConvertTypeWord handle:^{
- KMBatchOperateWindowController *batchWindowController = [KMBatchOperateWindowController sharedWindowController];
-
- // var needShowHint = false
- // if KMConvertOperationQueue.sharedQueue.operations.count > 0 {
- // needShowHint = true
- // } else {
- // var needContinue = false
- // if KMBatchOperateManager.defaultManager.files?.count ?? 0 > 0 {
- // let arr: [String] = converFilesToPath(files: KMBatchOperateManager.defaultManager.files!)
- // let ss = self.document?.documentURL.path
- // if arr.contains(ss!) {
- // needContinue = false
- // }else {
- // needContinue = true
- // }
- // }else {
- // needContinue = true
- // }
- //
- // if needContinue {
- // let ss = self.document?.documentURL.path
- // let file = KMBatchOperateFile(filePath: ss!, type: .Convert)
- // file.password = document?.password ?? ""
- // KMBatchOperateManager.defaultManager.files?.append(file)
- // }
- // batchWindowController.switchToOperateType(.Convert, files: KMBatchOperateManager.defaultManager.files!)
- // batchWindowController.switchToConvertType(convertType: type)
- // }
- //
- [batchWindowController.window makeKeyAndOrderFront:nil];
- }];
- com.batchButton.hidden = true;
- // [com showWindow:nil];
- [[self fetchBrowserWindowC].window beginSheet:com.window completionHandler:^(NSModalResponse returnCode) {
-
- }];
- self.windowC = com;
- });
- }
- }];
- }
- }
- - (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 {
- [[KMPreferenceController 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
|