summaryrefslogtreecommitdiffstats
path: root/macosx
diff options
context:
space:
mode:
authorDamiano Galassi <[email protected]>2019-07-25 08:10:15 +0200
committerDamiano Galassi <[email protected]>2019-07-25 08:10:15 +0200
commit5396c8b3bd401d4f574dc57de68cd4b3cc560d9f (patch)
treee62fff38a4dc4f2cfe2c7fccd265daa1778b3141 /macosx
parentc16151f9edf16376de254a43130a5f0371ee8a55 (diff)
MacGui: improve queue init.
Diffstat (limited to 'macosx')
-rw-r--r--macosx/HBQueueController.m107
-rw-r--r--macosx/HBQueueItemWorkingView.m70
2 files changed, 102 insertions, 75 deletions
diff --git a/macosx/HBQueueController.m b/macosx/HBQueueController.m
index 54b6f1a03..259f251e7 100644
--- a/macosx/HBQueueController.m
+++ b/macosx/HBQueueController.m
@@ -52,12 +52,56 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
if (self = [super initWithWindowNibName:@"Queue"])
{
- // Load the dockTile and instiante initial text fields
+ // Load the dockTile and instantiate initial text fields
_dockTile = [[HBDockTile alloc] initWithDockTile:NSApplication.sharedApplication.dockTile
image:NSApplication.sharedApplication.applicationIconImage];
// Init state
_queue = queue;
+ _queue.undoManager = [[NSUndoManager alloc] init];
+
+ [NSNotificationCenter.defaultCenter addObserverForName:HBQueueLowSpaceAlertNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
+ [self queueLowDiskSpaceAlert];
+ }];
+
+ [NSNotificationCenter.defaultCenter addObserverForName:HBQueueDidCompleteNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
+ [self queueCompletedAlerts];
+ }];
+
+ [NSNotificationCenter.defaultCenter addObserverForName:HBQueueProgressNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
+ // Update dock icon
+ double progress = [note.userInfo[HBQueueProgressNotificationPercentKey] doubleValue];
+
+#define dockTileUpdateFrequency 0.1f
+
+ if (self.dockIconProgress < 100.0 * progress)
+ {
+ double hours = [note.userInfo[HBQueueProgressNotificationHoursKey] doubleValue];
+ double minutes = [note.userInfo[HBQueueProgressNotificationMinutesKey] doubleValue];
+ double seconds = [note.userInfo[HBQueueProgressNotificationSecondsKey] doubleValue];
+
+ [self.dockTile updateDockIcon:progress hours:hours minutes:minutes seconds:seconds];
+ self.dockIconProgress += dockTileUpdateFrequency;
+ }
+ }];
+
+ [NSNotificationCenter.defaultCenter addObserverForName:HBQueueDidCompleteItemNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
+ // Restore dock icon
+ [self.dockTile updateDockIcon:-1.0 withETA:@""];
+ self.dockIconProgress = 0;
+
+ // Run the per item notification and actions
+ HBQueueItem *item = note.userInfo[HBQueueItemNotificationItemKey];
+ if (item.state == HBQueueItemStateCompleted)
+ {
+ [self sendToExternalApp:item];
+ }
+
+ if (item.state == HBQueueItemStateCompleted || item.state == HBQueueItemStateFailed)
+ {
+ [self itemCompletedAlerts:item];
+ }
+ }];
NSUserNotificationCenter.defaultUserNotificationCenter.delegate = self;
}
@@ -65,6 +109,11 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
return self;
}
+- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window
+{
+ return _queue.undoManager;
+}
+
- (void)windowDidLoad
{
if (@available (macOS 10.12, *))
@@ -72,8 +121,6 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
self.window.tabbingMode = NSWindowTabbingModeDisallowed;
}
- _queue.undoManager = self.window.undoManager;
-
// Set up the child view controllers
_splitViewController = [[NSSplitViewController alloc] init];
_splitViewController.splitView = _splitView;
@@ -98,9 +145,8 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
_splitViewController.splitView.identifier = @"HBQueueSplitViewIdentifier";
self.window.contentViewController = _splitViewController;
-
self.window.frameAutosaveName = @"HBQueueWindowFrameAutosave";
- [self.window setFrameFromString: @"HBQueueWindowFrameAutosave"];
+ [self.window setFrameFromString:@"HBQueueWindowFrameAutosave"];
// Set up observers
[self.queue.core addObserver:self forKeyPath:@"state"
@@ -109,51 +155,6 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
[self.queue addObserver:self forKeyPath:@"pendingItemsCount"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionInitial
context:HBControllerQueueCoreContext];
-
- [NSNotificationCenter.defaultCenter addObserverForName:HBQueueLowSpaceAlertNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
- [self queueLowDiskSpaceAlert];
- }];
-
- [NSNotificationCenter.defaultCenter addObserverForName:HBQueueDidCompleteNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
- // Since there are no more items to encode, go to queueCompletedAlerts
- // for user specified alerts after queue completed
- [self queueCompletedAlerts];
- }];
-
- [NSNotificationCenter.defaultCenter addObserverForName:HBQueueProgressNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
- // Update dock icon
- double progress = [note.userInfo[HBQueueProgressNotificationPercentKey] doubleValue];
-
-#define dockTileUpdateFrequency 0.1f
-
- if (self.dockIconProgress < 100.0 * progress)
- {
- double hours = [note.userInfo[HBQueueProgressNotificationHoursKey] doubleValue];
- double minutes = [note.userInfo[HBQueueProgressNotificationMinutesKey] doubleValue];
- double seconds = [note.userInfo[HBQueueProgressNotificationSecondsKey] doubleValue];
-
- [self.dockTile updateDockIcon:progress hours:hours minutes:minutes seconds:seconds];
- self.dockIconProgress += dockTileUpdateFrequency;
- }
- }];
-
- [NSNotificationCenter.defaultCenter addObserverForName:HBQueueDidCompleteItemNotification object:_queue queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note) {
- // Restore dock icon
- [self.dockTile updateDockIcon:-1.0 withETA:@""];
- self.dockIconProgress = 0;
-
- // Run the per item notification and actions
- HBQueueItem *item = note.userInfo[HBQueueItemNotificationItemKey];
- if (item.state == HBQueueItemStateCompleted)
- {
- [self sendToExternalApp:item];
- }
-
- if (item.state == HBQueueItemStateCompleted || item.state == HBQueueItemStateFailed)
- {
- [self itemCompletedAlerts:item];
- }
- }];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
@@ -450,10 +451,12 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
#pragma mark - Encode Done Actions
+NSString * const HBQueueItemNotificationPathKey = @"HBQueueItemNotificationPathKey";
+
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
// Show the file in Finder when a done notification was clicked.
- NSString *path = notification.userInfo[@"Path"];
+ NSString *path = notification.userInfo[HBQueueItemNotificationPathKey];
if ([path isKindOfClass:[NSString class]] && path.length)
{
NSURL *fileURL = [NSURL fileURLWithPath:path];
@@ -469,7 +472,7 @@ static void *HBControllerQueueCoreContext = &HBControllerQueueCoreContext;
notification.soundName = playSound ? NSUserNotificationDefaultSoundName : nil;
notification.hasActionButton = YES;
notification.actionButtonTitle = NSLocalizedString(@"Show", @"Notification -> Show in Finder");
- notification.userInfo = @{ @"Path": fileURL.path };
+ notification.userInfo = @{ HBQueueItemNotificationPathKey: fileURL.path };
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}
diff --git a/macosx/HBQueueItemWorkingView.m b/macosx/HBQueueItemWorkingView.m
index 4a217a78a..91e26a028 100644
--- a/macosx/HBQueueItemWorkingView.m
+++ b/macosx/HBQueueItemWorkingView.m
@@ -23,35 +23,59 @@
@implementation HBQueueItemWorkingView
+- (void)setUpObservers
+{
+ NSNotificationCenter * __weak center = NSNotificationCenter.defaultCenter;
+
+ self.progressToken = [center addObserverForName:HBQueueProgressNotification
+ object:nil
+ queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
+ {
+ NSString *progressInfo = note.userInfo[HBQueueProgressNotificationInfoKey];
+ double progress = [note.userInfo[HBQueueProgressNotificationPercentKey] doubleValue];
+
+ self.progressField.attributedStringValue = progressInfo.HB_smallMonospacedString;
+ self.progressBar.doubleValue = progress;
+ }];
+
+ self.completedToken = [center addObserverForName:HBQueueDidCompleteItemNotification
+ object:nil
+ queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
+ {
+ HBQueueItem *completedItem = note.userInfo[HBQueueItemNotificationItemKey];
+ if (completedItem == self.item) {
+ [self removeObservers];
+ }
+ }];
+}
+
+- (void)removeObservers
+{
+ if (self.progressToken)
+ {
+ [NSNotificationCenter.defaultCenter removeObserver:self.progressToken];
+ self.progressToken = nil;
+ }
+ if (self.completedToken)
+ {
+ [NSNotificationCenter.defaultCenter removeObserver:self.completedToken];
+ self.completedToken = nil;
+ }
+}
+
+- (void)dealloc
+{
+ [self removeObservers];
+}
+
- (void)setItem:(HBQueueItem *)item
{
[super setItem:item];
if (item.state == HBQueueItemStateWorking)
{
- NSNotificationCenter * __weak center = NSNotificationCenter.defaultCenter;
-
- self.progressToken = [center addObserverForName:HBQueueProgressNotification
- object:nil
- queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
- {
- NSString *progressInfo = note.userInfo[HBQueueProgressNotificationInfoKey];
- double progress = [note.userInfo[HBQueueProgressNotificationPercentKey] doubleValue];
-
- self.progressField.attributedStringValue = progressInfo.HB_smallMonospacedString;
- self.progressBar.doubleValue = progress;
- }];
-
- self.completedToken = [center addObserverForName:HBQueueDidCompleteItemNotification
- object:nil
- queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification * _Nonnull note)
- {
- HBQueueItem *completedItem = note.userInfo[HBQueueItemNotificationItemKey];
- if (completedItem == self.item) {
- [center removeObserver:self.progressToken];
- [center removeObserver:self.completedToken];
- }
- }];
+ [self removeObservers];
+ [self setUpObservers];
}
}