123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- //
- // AutoSavePopController.m
- // PDF Reader Pro Edition
- //
- // Created by Niehaoyu on 2023/08/14.
- //
- #import "AutoSavePopController.h"
- #import "AutoSaveFileItem.h"
- #import <PDF_Reader_Pro-Swift.h>
- @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
|