[macOS] Use NSAlert APIs to show alert suppression checkbox (#3360)

* [macOS] Simplify branching for termination prevention

* [macOS] Use NSAlert APIs to show alert suppression checkbox
This commit is contained in:
Dzmitry Neviadomski
2022-07-03 00:38:27 +03:00
committed by GitHub
parent e7627dc209
commit 3e73913a1d

View File

@@ -868,47 +868,57 @@ static void removeKeRangerRansomware()
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*)sender
{ {
if (!self.fQuitRequested && [self.fDefaults boolForKey:@"CheckQuit"]) if (self.fQuitRequested || ![self.fDefaults boolForKey:@"CheckQuit"])
{ {
NSUInteger active = 0, downloading = 0; return NSTerminateNow;
for (Torrent* torrent in self.fTorrents) }
NSUInteger active = 0, downloading = 0;
for (Torrent* torrent in self.fTorrents)
{
if (torrent.active && !torrent.stalled)
{ {
if (torrent.active && !torrent.stalled) active++;
if (!torrent.allDownloaded)
{ {
active++; downloading++;
if (!torrent.allDownloaded)
{
downloading++;
}
} }
} }
if ([self.fDefaults boolForKey:@"CheckQuitDownloading"] ? downloading > 0 : active > 0)
{
NSAlert* alert = [[NSAlert alloc] init];
alert.alertStyle = NSAlertStyleInformational;
alert.messageText = NSLocalizedString(@"Are you sure you want to quit?", "Confirm Quit panel -> title");
alert.informativeText = active == 1 ?
NSLocalizedString(
@"There is an active transfer that will be paused on quit."
" The transfer will automatically resume on the next launch.",
"Confirm Quit panel -> message") :
[NSString stringWithFormat:NSLocalizedString(
@"There are %lu active transfers that will be paused on quit."
" The transfers will automatically resume on the next launch.",
"Confirm Quit panel -> message"),
active];
[alert addButtonWithTitle:NSLocalizedString(@"Quit", "Confirm Quit panel -> button")];
[alert addButtonWithTitle:NSLocalizedString(@"Cancel", "Confirm Quit panel -> button")];
[alert beginSheetModalForWindow:self.fWindow completionHandler:^(NSModalResponse returnCode) {
[NSApp replyToApplicationShouldTerminate:returnCode == NSAlertFirstButtonReturn];
}];
return NSTerminateLater;
}
} }
return NSTerminateNow; BOOL preventedByTransfer = [self.fDefaults boolForKey:@"CheckQuitDownloading"] ? downloading > 0 : active > 0;
if (!preventedByTransfer)
{
return NSTerminateNow;
}
NSAlert* alert = [[NSAlert alloc] init];
alert.alertStyle = NSAlertStyleInformational;
alert.messageText = NSLocalizedString(@"Are you sure you want to quit?", "Confirm Quit panel -> title");
alert.informativeText = active == 1 ?
NSLocalizedString(
@"There is an active transfer that will be paused on quit."
" The transfer will automatically resume on the next launch.",
"Confirm Quit panel -> message") :
[NSString stringWithFormat:NSLocalizedString(
@"There are %lu active transfers that will be paused on quit."
" The transfers will automatically resume on the next launch.",
"Confirm Quit panel -> message"),
active];
[alert addButtonWithTitle:NSLocalizedString(@"Quit", "Confirm Quit panel -> button")];
[alert addButtonWithTitle:NSLocalizedString(@"Cancel", "Confirm Quit panel -> button")];
alert.showsSuppressionButton = YES;
[alert beginSheetModalForWindow:self.fWindow completionHandler:^(NSModalResponse returnCode) {
if (alert.suppressionButton.state == NSControlStateValueOn)
{
[self.fDefaults setBool:NO forKey:@"CheckQuit"];
}
[NSApp replyToApplicationShouldTerminate:returnCode == NSAlertFirstButtonReturn];
}];
return NSTerminateLater;
} }
- (void)applicationWillTerminate:(NSNotification*)notification - (void)applicationWillTerminate:(NSNotification*)notification