// // AutoSavePopController.m // PDF Reader Pro Edition // // Created by Niehaoyu on 2023/08/14. // #import "AutoSavePopController.h" #import "AutoSaveFileItem.h" #import @interface AutoSavePopController ()< NSTableViewDelegate, NSTableViewDataSource, NSCollectionViewDelegate, NSCollectionViewDataSource> @property (assign) IBOutlet NSTextField *titleLbl; @property (assign) IBOutlet NSView *tableBGView; @property (assign) IBOutlet NSView *headerView; @property (assign) IBOutlet NSTextField *headerNameLbl; @property (assign) IBOutlet NSTextField *headerDateLbl; @property (assign) IBOutlet NSView *headerLineView1; @property (assign) IBOutlet NSView *headerLineView2; @property (assign) IBOutlet NSButton *headerCheckBtn; @property (assign) IBOutlet NSCollectionView *collectionView; @property (assign) IBOutlet NSButton *cancelBtn; @property (assign) IBOutlet NSButton *saveAsBtn; @end @implementation AutoSavePopController - (void)dealloc { [NSDistributedNotificationCenter.defaultCenter removeObserver:self]; #if DEBUG NSLog(@"## %@ Dealloc", self.className); #endif } - (id)init { if (self = [super initWithWindowNibName:@"AutoSavePopController"]) { } return self; } - (void)windowDidLoad { [super windowDidLoad]; // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. self.titleLbl.stringValue = NSLocalizedString(@"Document cache records will be cleared if you choose 'Cancel'", nil); self.headerNameLbl.stringValue = NSLocalizedString(@"File Name", nil); self.headerDateLbl.stringValue = NSLocalizedString(@"Autosave Time", nil); self.cancelBtn.title = NSLocalizedString(@"Cancel", nil); self.saveAsBtn.title = NSLocalizedString(@"Save as", nil); self.tableBGView.wantsLayer = YES; self.tableBGView.layer.cornerRadius = 5.; self.tableBGView.layer.masksToBounds = YES; self.tableBGView.layer.borderWidth = 0.5; self.headerView.wantsLayer = YES; self.headerLineView1.wantsLayer = self.headerLineView2.wantsLayer = YES; self.collectionView.delegate = self; self.collectionView.dataSource = self; [self.collectionView registerClass:[AutoSaveFileItem class] forItemWithIdentifier:@"autoSaveFileItem"]; [self refreshSaveBtnState]; [self updateViewColor]; [NSDistributedNotificationCenter.defaultCenter addObserver:self selector:@selector(themeChanged:) name:@"AppleInterfaceThemeChangedNotification" object: nil]; } - (void)updateViewColor { self.tableBGView.layer.backgroundColor = [NSColor colorWithRed:246/255. green:246/255. blue:246/255. alpha:1.].CGColor; if ([KMAppearance isDarkMode]) { self.tableBGView.layer.borderColor = [NSColor colorWithRed:66/255. green:67/255. blue:69/255. alpha:1.].CGColor; self.headerView.layer.backgroundColor = [NSColor colorWithRed:54/255. green:54/255. blue:54/255. alpha:1.].CGColor; self.headerLineView1.layer.backgroundColor = [NSColor colorWithRed:62/255. green:62/255. blue:62/255. alpha:1.].CGColor; self.headerLineView2.layer.backgroundColor = [NSColor colorWithRed:62/255. green:62/255. blue:62/255. alpha:1.].CGColor; } else { self.tableBGView.layer.borderColor = [NSColor colorWithRed:218/255. green:219/255. blue:222/255. alpha:1.].CGColor; self.headerView.layer.backgroundColor = [NSColor colorWithRed:228/255. green:228/255. blue:228/255. alpha:1.].CGColor; self.headerLineView1.layer.backgroundColor = [NSColor colorWithRed:218/255. green:219/255. blue:222/255. alpha:1.].CGColor; self.headerLineView2.layer.backgroundColor = [NSColor colorWithRed:218/255. green:219/255. blue:222/255. alpha:1.].CGColor; } [self.collectionView reloadData]; } - (void)refreshSaveBtnState { self.headerCheckBtn.state = NSControlStateValueOff; // [AutoSaveManager manager].opend if ([AutoSaveManager manager].opendPaths.count > 0) { self.saveAsBtn.enabled = YES; if ([AutoSaveManager manager].opendPaths.count == [AutoSaveManager manager].autoSavePaths.count) { self.headerCheckBtn.state = NSControlStateValueOn; } } else { self.saveAsBtn.enabled = NO; } } #pragma mark - IBAction - (IBAction)headerCheckBtnAction:(NSButton *)sender { if (self.headerCheckBtn.state == NSControlStateValueOn) { [[AutoSaveManager manager].opendPaths removeAllObjects]; [[AutoSaveManager manager].opendPaths addObjectsFromArray:[AutoSaveManager manager].autoSavePaths]; } else { [[AutoSaveManager manager].opendPaths removeAllObjects]; } [self.collectionView reloadData]; [self refreshSaveBtnState]; } - (IBAction)cancelAction:(NSButton *)sender { if (self.cancelHandle) { self.cancelHandle(self); } } - (IBAction)saveAsAction:(NSButton *)sender { if (self.confirmHandle) { self.confirmHandle(self); } } #pragma mark - NSTableViewDelegate & NSTableView - (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return [AutoSaveManager manager].autoSavePaths.count; } - (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath { AutoSaveFileItem *item = [collectionView makeItemWithIdentifier:@"autoSaveFileItem" forIndexPath:indexPath]; if ((NSInteger)[AutoSaveManager manager].autoSavePaths.count > indexPath.item) { item.filePath = [[AutoSaveManager manager].autoSavePaths objectAtIndex:indexPath.item]; } __weak typeof(self) weakSelf = self; item.stateHandle = ^{ [weakSelf refreshSaveBtnState]; }; return item; } - (NSSize)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(CGRectGetWidth(self.collectionView.frame), 32); } - (CGSize)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { return CGSizeMake(CGRectGetWidth(self.collectionView.frame), 0); } - (NSEdgeInsets)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return NSEdgeInsetsMake(0, 0, 0, 0); } - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 0.01; } - (CGFloat)collectionView:(NSCollectionView *)collectionView layout:(NSCollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { return 0.01; } #pragma mark - DarkMode - (void)themeChanged:(NSNotification *)notification { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self updateViewColor]; }); } @end