Skip to content

Commit

Permalink
Confirm quitting the app during conversion (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Sep 16, 2019
1 parent 948d255 commit cf0f6eb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
20 changes: 20 additions & 0 deletions Gifski/AppDelegate.swift
Expand Up @@ -52,6 +52,26 @@ final class AppDelegate: NSObject, NSApplicationDelegate {

func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { true }

func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
if mainWindowController.isConverting {
let response = NSAlert.showModal(
for: mainWindowController.window,
message: "Do you want to continue converting?",
informativeText: "Gifski is currently converting a video. If you quit, the conversion will be cancelled.",
buttonTitles: [
"Continue",
"Quit"
]
)

if response == .alertFirstButtonReturn {
return .terminateCancel
}
}

return .terminateNow
}

func application(_ application: NSApplication, willPresentError error: Error) -> Error {
Crashlytics.recordNonFatalError(error: error)
return error
Expand Down
11 changes: 11 additions & 0 deletions Gifski/Vendor/CircularProgress+Util.swift
Expand Up @@ -214,3 +214,14 @@ extension CABasicAnimation {
return animation
}
}


extension NSWindow {
/// Whether the window or its owning app is showing a modal or sheet.
/// This can be useful to disable any unintended interaction underneath it,
/// for example, drag and drop or mouse hover.
var isShowingModalOrSheet: Bool {
NSApp.modalWindow != nil ||
attachedSheet != nil
}
}
10 changes: 9 additions & 1 deletion Gifski/Vendor/CircularProgress.swift
Expand Up @@ -370,7 +370,15 @@ public final class CircularProgress: NSView {
}

override public func mouseEntered(with event: NSEvent) {
guard isCancellable && !isCancelled && !isFinished else {
guard window?.isShowingModalOrSheet != true else {
return
}

guard
isCancellable,
!isCancelled,
!isFinished
else {
super.mouseEntered(with: event)
return
}
Expand Down
18 changes: 15 additions & 3 deletions Gifski/util.swift
Expand Up @@ -252,21 +252,24 @@ extension NSAlert {
message: String,
informativeText: String? = nil,
detailText: String? = nil,
style: NSAlert.Style = .warning
style: NSAlert.Style = .warning,
buttonTitles: [String] = []
) -> NSApplication.ModalResponse {
NSAlert(
message: message,
informativeText: informativeText,
detailText: detailText,
style: style
style: style,
buttonTitles: buttonTitles
).runModal(for: window)
}

convenience init(
message: String,
informativeText: String? = nil,
detailText: String? = nil,
style: NSAlert.Style = .warning
style: NSAlert.Style = .warning,
buttonTitles: [String] = []
) {
self.init()
self.messageText = message
Expand Down Expand Up @@ -304,6 +307,8 @@ extension NSAlert {
self.informativeText += "\n\(detailText)"
}
}

self.addButtons(withTitles: buttonTitles)
}

/// Runs the alert as a window-modal sheet, or as an app-modal (window-indepedendent) alert if the window is `nil` or not given.
Expand All @@ -319,6 +324,13 @@ extension NSAlert {

return NSApp.runModal(for: window)
}

/// Adds buttons with the given titles to the alert.
func addButtons(withTitles buttonTitles: [String]) {
for buttonTitle in buttonTitles {
addButton(withTitle: buttonTitle)
}
}
}


Expand Down

0 comments on commit cf0f6eb

Please sign in to comment.