AutoSavePopController.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // AutoSavePopController.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2023/08/14.
  6. //
  7. #import "AutoSavePopController.h"
  8. #import "AutoSaveFileItem.h"
  9. #import <PDF_Reader_Pro-Swift.h>
  10. @interface AutoSavePopController ()<
  11. NSTableViewDelegate,
  12. NSTableViewDataSource,
  13. NSCollectionViewDelegate,
  14. NSCollectionViewDataSource>
  15. @property (assign) IBOutlet NSTextField *titleLbl;
  16. @property (assign) IBOutlet NSView *tableBGView;
  17. @property (assign) IBOutlet NSView *headerView;
  18. @property (assign) IBOutlet NSTextField *headerNameLbl;
  19. @property (assign) IBOutlet NSTextField *headerDateLbl;
  20. @property (assign) IBOutlet NSView *headerLineView1;
  21. @property (assign) IBOutlet NSView *headerLineView2;
  22. @property (assign) IBOutlet NSButton *headerCheckBtn;
  23. @property (assign) IBOutlet NSCollectionView *collectionView;
  24. @property (assign) IBOutlet NSButton *cancelBtn;
  25. @property (assign) IBOutlet NSButton *saveAsBtn;
  26. @end
  27. @implementation AutoSavePopController
  28. - (void)dealloc {
  29. [NSDistributedNotificationCenter.defaultCenter removeObserver:self];
  30. #if DEBUG
  31. NSLog(@"## %@ Dealloc", self.className);
  32. #endif
  33. }
  34. - (id)init {
  35. if (self = [super initWithWindowNibName:@"AutoSavePopController"]) {
  36. }
  37. return self;
  38. }
  39. - (void)windowDidLoad {
  40. [super windowDidLoad];
  41. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  42. self.titleLbl.stringValue = NSLocalizedString(@"Document cache records will be cleared if you choose 'Cancel'", nil);
  43. self.headerNameLbl.stringValue = NSLocalizedString(@"File Name", nil);
  44. self.headerDateLbl.stringValue = NSLocalizedString(@"Autosave Time", nil);
  45. self.cancelBtn.title = NSLocalizedString(@"Cancel", nil);
  46. self.saveAsBtn.title = NSLocalizedString(@"Save as", nil);
  47. self.tableBGView.wantsLayer = YES;
  48. self.tableBGView.layer.cornerRadius = 5.;
  49. self.tableBGView.layer.masksToBounds = YES;
  50. self.tableBGView.layer.borderWidth = 0.5;
  51. self.headerView.wantsLayer = YES;
  52. self.headerLineView1.wantsLayer = self.headerLineView2.wantsLayer = YES;
  53. self.collectionView.delegate = self;
  54. self.collectionView.dataSource = self;
  55. [self.collectionView registerClass:[AutoSaveFileItem class] forItemWithIdentifier:@"autoSaveFileItem"];
  56. [self refreshSaveBtnState];
  57. [self updateViewColor];
  58. [NSDistributedNotificationCenter.defaultCenter addObserver:self selector:@selector(themeChanged:) name:@"AppleInterfaceThemeChangedNotification" object: nil];
  59. }
  60. - (void)updateViewColor {
  61. self.tableBGView.layer.backgroundColor = [NSColor colorWithRed:246/255. green:246/255. blue:246/255. alpha:1.].CGColor;
  62. if ([KMAppearance isDarkMode]) {
  63. self.tableBGView.layer.borderColor = [NSColor colorWithRed:66/255. green:67/255. blue:69/255. alpha:1.].CGColor;
  64. self.headerView.layer.backgroundColor = [NSColor colorWithRed:54/255. green:54/255. blue:54/255. alpha:1.].CGColor;
  65. self.headerLineView1.layer.backgroundColor = [NSColor colorWithRed:62/255. green:62/255. blue:62/255. alpha:1.].CGColor;
  66. self.headerLineView2.layer.backgroundColor = [NSColor colorWithRed:62/255. green:62/255. blue:62/255. alpha:1.].CGColor;
  67. } else {
  68. self.tableBGView.layer.borderColor = [NSColor colorWithRed:218/255. green:219/255. blue:222/255. alpha:1.].CGColor;
  69. self.headerView.layer.backgroundColor = [NSColor colorWithRed:228/255. green:228/255. blue:228/255. alpha:1.].CGColor;
  70. self.headerLineView1.layer.backgroundColor = [NSColor colorWithRed:218/255. green:219/255. blue:222/255. alpha:1.].CGColor;
  71. self.headerLineView2.layer.backgroundColor = [NSColor colorWithRed:218/255. green:219/255. blue:222/255. alpha:1.].CGColor;
  72. }
  73. [self.collectionView reloadData];
  74. }
  75. - (void)refreshSaveBtnState {
  76. self.headerCheckBtn.state = NSControlStateValueOff;
  77. // [AutoSaveManager manager].opend
  78. if ([AutoSaveManager manager].opendPaths.count > 0) {
  79. self.saveAsBtn.enabled = YES;
  80. if ([AutoSaveManager manager].opendPaths.count == [AutoSaveManager manager].autoSavePaths.count) {
  81. self.headerCheckBtn.state = NSControlStateValueOn;
  82. }
  83. } else {
  84. self.saveAsBtn.enabled = NO;
  85. }
  86. }
  87. #pragma mark - IBAction
  88. - (IBAction)headerCheckBtnAction:(NSButton *)sender {
  89. if (self.headerCheckBtn.state == NSControlStateValueOn) {
  90. [[AutoSaveManager manager].opendPaths removeAllObjects];
  91. [[AutoSaveManager manager].opendPaths addObjectsFromArray:[AutoSaveManager manager].autoSavePaths];
  92. } else {
  93. [[AutoSaveManager manager].opendPaths removeAllObjects];
  94. }
  95. [self.collectionView reloadData];
  96. [self refreshSaveBtnState];
  97. }
  98. - (IBAction)cancelAction:(NSButton *)sender {
  99. if (self.cancelHandle) {
  100. self.cancelHandle(self);
  101. }
  102. }
  103. - (IBAction)saveAsAction:(NSButton *)sender {
  104. if (self.confirmHandle) {
  105. self.confirmHandle(self);
  106. }
  107. }
  108. #pragma mark - NSTableViewDelegate & NSTableView
  109. - (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  110. return [AutoSaveManager manager].autoSavePaths.count;
  111. }
  112. - (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath {
  113. AutoSaveFileItem *item = [collectionView makeItemWithIdentifier:@"autoSaveFileItem" forIndexPath:indexPath];
  114. if ((NSInteger)[AutoSaveManager manager].autoSavePaths.count > indexPath.item) {
  115. item.filePath = [[AutoSaveManager manager].autoSavePaths objectAtIndex:indexPath.item];
  116. }
  117. __weak typeof(self) weakSelf = self;
  118. item.stateHandle = ^{
  119. [weakSelf refreshSaveBtnState];
  120. };
  121. return item;
  122. }
  123. - (NSSize)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  124. return CGSizeMake(CGRectGetWidth(self.collectionView.frame), 32);
  125. }
  126. - (CGSize)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  127. return CGSizeMake(CGRectGetWidth(self.collectionView.frame), 0);
  128. }
  129. - (NSEdgeInsets)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  130. return NSEdgeInsetsMake(0, 0, 0, 0);
  131. }
  132. - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  133. return 0.01;
  134. }
  135. - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
  136. return 0.01;
  137. }
  138. #pragma mark - DarkMode
  139. - (void)themeChanged:(NSNotification *)notification {
  140. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  141. [self updateViewColor];
  142. });
  143. }
  144. @end