KMStatusBar.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // KMStatusBar.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by 王帅 on 2018/9/30.
  6. //
  7. #import "KMStatusBar.h"
  8. #import "IAPProductsManager.h"
  9. #import <PDF_Reader_Pro-Swift.h>
  10. @interface KMStatusBar ()
  11. @property (nonatomic,retain) NSStatusItem *convertStatusItem;
  12. @property (nonatomic, strong) NSWindowController *windowC;
  13. @end
  14. @implementation KMStatusBar
  15. #pragma mark - Init Methods
  16. + (KMStatusBar *)systemStatusBar {
  17. static KMStatusBar *singleton = nil;
  18. static dispatch_once_t pred;
  19. dispatch_once(&pred, ^{
  20. singleton = [[KMStatusBar alloc] init];
  21. });
  22. return singleton;
  23. }
  24. - (instancetype)init {
  25. if (self = [super init]) {
  26. if ([self isShow]) {
  27. [self showConvertStatusItem];
  28. }
  29. }
  30. return self;
  31. }
  32. - (void)dealloc {
  33. // [_convertStatusItem release];
  34. // [super dealloc];
  35. }
  36. #pragma mark - Public Methods
  37. - (BOOL)isShow {
  38. if ([[NSUserDefaults standardUserDefaults] objectForKey:@"KMStatusBarIsShowKey"]) {
  39. return [[NSUserDefaults standardUserDefaults] boolForKey:@"KMStatusBarIsShowKey"];
  40. }
  41. return YES;
  42. }
  43. - (void)setIsShow:(BOOL)isShow {
  44. [[NSUserDefaults standardUserDefaults] setBool:isShow forKey:@"KMStatusBarIsShowKey"];
  45. [[NSUserDefaults standardUserDefaults] synchronize];
  46. if (isShow) {
  47. [self showConvertStatusItem];
  48. } else {
  49. [self removeConvertStatusItem];
  50. }
  51. }
  52. #pragma mark - Private Methods
  53. - (void)showConvertStatusItem {
  54. if (!self.convertStatusItem) {
  55. NSMenu *menu = [[NSMenu alloc] init];
  56. self.convertStatusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
  57. [self.convertStatusItem setImage:[NSImage imageNamed:@"KMImageNameToolbarConvert"]];
  58. [self.convertStatusItem setHighlightMode:YES];
  59. [self.convertStatusItem setTarget:self];
  60. [self.convertStatusItem setMenu:menu];
  61. NSMenuItem *item = [menu addItemWithTitle:NSLocalizedString(@"PDF to Office", nil) action:@selector(convertItemAction:) keyEquivalent:@""];
  62. item.target = self;
  63. [menu addItem:[NSMenuItem separatorItem]];
  64. item = [menu addItemWithTitle:NSLocalizedString(@"New From Clipboard", nil) action:@selector(newFromClipboardItemAction:) keyEquivalent:@""];
  65. item.target = self;
  66. item = [menu addItemWithTitle:NSLocalizedString(@"New From Images", nil) action:@selector(newFromImagesItemAction:) keyEquivalent:@""];
  67. item.target = self;
  68. item = [menu addItemWithTitle:NSLocalizedString(@"New From File", nil) action:@selector(newFromFileItemAction:) keyEquivalent:@""];
  69. item.target = self;
  70. item = [menu addItemWithTitle:NSLocalizedString(@"New From Web Page", nil) action:@selector(newFromWebPageItemAction:) keyEquivalent:@""];
  71. item.target = self;
  72. [menu addItem:[NSMenuItem separatorItem]];
  73. item = [menu addItemWithTitle:NSLocalizedString(@"Crosshair Screenshot", nil) action:@selector(screenshotFromSelectionItemAction:) keyEquivalent:@""];
  74. item.target = self;
  75. item = [menu addItemWithTitle:NSLocalizedString(@"Window Screenshot", nil) action:@selector(screenshotFromWindowItemAction:) keyEquivalent:@""];
  76. item.target = self;
  77. item = [menu addItemWithTitle:NSLocalizedString(@"Fullscreen Screenshot", nil) action:@selector(screenshotFromEntireItemAction:) keyEquivalent:@""];
  78. item.target = self;
  79. [menu addItem:[NSMenuItem separatorItem]];
  80. item = [menu addItemWithTitle:NSLocalizedStringFromTable(@"Preferences…", @"", nil) action:@selector(preferencesItemAction:) keyEquivalent:@""];
  81. item.target = self;
  82. }
  83. }
  84. - (void)removeConvertStatusItem {
  85. if (self.convertStatusItem) {
  86. [[NSStatusBar systemStatusBar] removeStatusItem:self.convertStatusItem];
  87. self.convertStatusItem = nil;
  88. }
  89. }
  90. #pragma mark - Item Action
  91. - (void)convertItemAction:(id)sender {
  92. if ([IAPProductsManager defaultManager].isAvailableAllFunction) {
  93. NSOpenPanel *openPanel = [NSOpenPanel openPanel];
  94. [openPanel setAllowedFileTypes:@[@"pdf",@"PDF"]];
  95. [openPanel setAllowsMultipleSelection:NO];
  96. [openPanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  97. if (result == NSModalResponseOK) {
  98. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  99. KMBatchWindowController *batchWindowController = [[KMBatchWindowController alloc] initWithWindowNibName:@"KMBatchWindowController"];
  100. [batchWindowController.window makeKeyAndOrderFront:nil];
  101. NSMutableArray *array = [NSMutableArray array];
  102. [array addObject:openPanel.URL];
  103. batchWindowController.inputData = array;
  104. });
  105. }
  106. }];
  107. } else {
  108. NSOpenPanel *openPanel = [NSOpenPanel openPanel];
  109. [openPanel setAllowedFileTypes:@[@"pdf"]];
  110. openPanel.message = NSLocalizedString(@"To select multiple files press cmd ⌘ button on keyboard and click on the target files one by one.", nil);
  111. [openPanel setAllowsMultipleSelection:NO];
  112. [openPanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  113. if (result == NSModalResponseOK) {
  114. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  115. CPDFDocument *document = [[CPDFDocument alloc] initWithURL:openPanel.URL];
  116. });
  117. }
  118. }];
  119. }
  120. }
  121. - (void)newFromClipboardItemAction:(id)sender {
  122. // [[self fetchBrowserWindowC] newDocumentFromClipboard:sender];
  123. }
  124. - (void)newFromImagesItemAction:(id)sender {
  125. // [[self fetchBrowserWindowC] createPDFFromImage];
  126. }
  127. - (void)newFromFileItemAction:(id)sender {
  128. // [[self fetchBrowserWindowC] importFromFile:sender];
  129. }
  130. - (void)newFromWebPageItemAction:(id)sender {
  131. // [[self fetchBrowserWindowC] importFromWebPage:nil];
  132. }
  133. - (void)screenshotFromSelectionItemAction:(id)sender {
  134. // [[self fetchBrowserWindowC] screenShot_SelectArea:nil];
  135. }
  136. - (void)screenshotFromWindowItemAction:(id)sender {
  137. // [[self fetchBrowserWindowC] screenShot_Window:nil];
  138. }
  139. - (void)screenshotFromEntireItemAction:(id)sender {
  140. // [[self fetchBrowserWindowC] screenShot_FullScreenDelay:nil];
  141. }
  142. - (void)preferencesItemAction:(id)sender {
  143. [[SettingsWindowController shared] showWindow:self];
  144. }
  145. - (KMBrowserWindowController *)fetchBrowserWindowC {
  146. CTBrowserWindow *flagWin = nil;
  147. for (NSWindow *win in NSApp.windows) {
  148. if ([win isKindOfClass:[CTBrowserWindow class]]) {
  149. if (win.isVisible) {
  150. flagWin = win;
  151. break;
  152. }
  153. }
  154. }
  155. return flagWin.windowController;
  156. }
  157. @end