KMStatusBar.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. KMBatchOperateBaseWindowController *baseWindowController = [[KMBatchOperateBaseWindowController alloc] initWithWindowNibName:@"KMBatchOperateBaseWindowController"];
  100. KMBatchOperateFile *file = [[KMBatchOperateFile alloc] initWithFilePath:openPanel.URL.path type:KMBatchOperationTypeConvert];
  101. NSMutableArray *arr = [NSMutableArray array];
  102. [arr addObject:file];
  103. [baseWindowController.window makeKeyAndOrderFront:nil];
  104. [baseWindowController checkNeedPasswordSwitchToOperateTypeWithOperateType:KMBatchOperationTypeConvert files:arr];
  105. [baseWindowController switchToConvertTypeWithConvertType:[IAPProductsManager defaultManager].isAvailableAdvancedPDFToOffice? KMConvertWithPDFTypeWordAdvance:KMConvertWithPDFTypeWordStandard];
  106. });
  107. }
  108. }];
  109. } else {
  110. NSOpenPanel *openPanel = [NSOpenPanel openPanel];
  111. [openPanel setAllowedFileTypes:@[@"pdf"]];
  112. openPanel.message = NSLocalizedString(@"To select multiple files press cmd ⌘ button on keyboard and click on the target files one by one.", nil);
  113. [openPanel setAllowsMultipleSelection:NO];
  114. [openPanel beginSheetModalForWindow:[NSApp mainWindow] completionHandler:^(NSInteger result) {
  115. if (result == NSModalResponseOK) {
  116. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  117. CPDFDocument *document = [[CPDFDocument alloc] initWithURL:openPanel.URL];
  118. KMConvertWindowController *com = [[KMConvertWindowController alloc] initWithDocumemtV:document currentPage:nil convertT:KMConvertTypeWord handle:^{
  119. KMBatchOperateWindowController *batchWindowController = [KMBatchOperateWindowController sharedWindowController];
  120. // var needShowHint = false
  121. // if KMConvertOperationQueue.sharedQueue.operations.count > 0 {
  122. // needShowHint = true
  123. // } else {
  124. // var needContinue = false
  125. // if KMBatchOperateManager.defaultManager.files?.count ?? 0 > 0 {
  126. // let arr: [String] = converFilesToPath(files: KMBatchOperateManager.defaultManager.files!)
  127. // let ss = self.document?.documentURL.path
  128. // if arr.contains(ss!) {
  129. // needContinue = false
  130. // }else {
  131. // needContinue = true
  132. // }
  133. // }else {
  134. // needContinue = true
  135. // }
  136. //
  137. // if needContinue {
  138. // let ss = self.document?.documentURL.path
  139. // let file = KMBatchOperateFile(filePath: ss!, type: .Convert)
  140. // file.password = document?.password ?? ""
  141. // KMBatchOperateManager.defaultManager.files?.append(file)
  142. // }
  143. // batchWindowController.switchToOperateType(.Convert, files: KMBatchOperateManager.defaultManager.files!)
  144. // batchWindowController.switchToConvertType(convertType: type)
  145. // }
  146. //
  147. [batchWindowController.window makeKeyAndOrderFront:nil];
  148. }];
  149. com.batchButton.hidden = true;
  150. // [com showWindow:nil];
  151. [[self fetchBrowserWindowC].window beginSheet:com.window completionHandler:^(NSModalResponse returnCode) {
  152. }];
  153. self.windowC = com;
  154. });
  155. }
  156. }];
  157. }
  158. }
  159. - (void)newFromClipboardItemAction:(id)sender {
  160. // [[self fetchBrowserWindowC] newDocumentFromClipboard:sender];
  161. }
  162. - (void)newFromImagesItemAction:(id)sender {
  163. // [[self fetchBrowserWindowC] createPDFFromImage];
  164. }
  165. - (void)newFromFileItemAction:(id)sender {
  166. // [[self fetchBrowserWindowC] importFromFile:sender];
  167. }
  168. - (void)newFromWebPageItemAction:(id)sender {
  169. // [[self fetchBrowserWindowC] importFromWebPage:nil];
  170. }
  171. - (void)screenshotFromSelectionItemAction:(id)sender {
  172. // [[self fetchBrowserWindowC] screenShot_SelectArea:nil];
  173. }
  174. - (void)screenshotFromWindowItemAction:(id)sender {
  175. // [[self fetchBrowserWindowC] screenShot_Window:nil];
  176. }
  177. - (void)screenshotFromEntireItemAction:(id)sender {
  178. // [[self fetchBrowserWindowC] screenShot_FullScreenDelay:nil];
  179. }
  180. - (void)preferencesItemAction:(id)sender {
  181. [[KMPreferenceController shared] showWindow:self];
  182. }
  183. - (KMBrowserWindowController *)fetchBrowserWindowC {
  184. CTBrowserWindow *flagWin = nil;
  185. for (NSWindow *win in NSApp.windows) {
  186. if ([win isKindOfClass:[CTBrowserWindow class]]) {
  187. if (win.isVisible) {
  188. flagWin = win;
  189. break;
  190. }
  191. }
  192. }
  193. return flagWin.windowController;
  194. }
  195. @end