Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 24, 2020
1 parent e6c97bc commit f5d385e
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 22 deletions.
3 changes: 2 additions & 1 deletion Gifski/AppDelegate.swift
@@ -1,6 +1,7 @@
import Cocoa
import UserNotifications
import Firebase
import FirebaseCore
import FirebaseCrashlytics
import DockProgress

@NSApplicationMain
Expand Down
8 changes: 6 additions & 2 deletions Gifski/ConversionCompletedViewController.swift
Expand Up @@ -125,13 +125,17 @@ final class ConversionCompletedViewController: NSViewController {
for: copyButton.window,
message: "The GIF was copied to the clipboard.",
informativeText: "However…",
buttonTitles: ["Continue"]
buttonTitles: [
"Continue"
],
defaultButtonIndex: -1
)

NSAlert.showModal(
for: copyButton.window,
message: "Please read!",
informativeText: "Many apps like Chrome and Slack do not properly handle copied animated GIFs and will paste them as non-animated PNG.\n\nInstead, drag and drop the GIF into such apps."
informativeText: "Many apps like Chrome and Slack do not properly handle copied animated GIFs and will paste them as non-animated PNG.\n\nInstead, drag and drop the GIF into such apps.",
defaultButtonIndex: -1
)
}

Expand Down
16 changes: 8 additions & 8 deletions Gifski/ConversionCompletedViewController.xib
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="15702" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="17156" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15702"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="17156"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down Expand Up @@ -53,24 +53,24 @@
<rect key="frame" x="30" y="22" width="300" height="30"/>
<subviews>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="e7w-oB-lHq">
<rect key="frame" x="0.0" y="7" width="93" height="23"/>
<buttonCell key="cell" type="roundTextured" title="Copy" bezelStyle="texturedRounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="wrr-rz-aBk">
<rect key="frame" x="-6" y="2" width="105" height="32"/>
<buttonCell key="cell" type="push" title="Copy" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="wrr-rz-aBk">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
<string key="keyEquivalent">c</string>
<modifierMask key="keyEquivalentModifierMask" command="YES"/>
</buttonCell>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="xNp-Z5-9zb">
<rect key="frame" x="103" y="7" width="94" height="23"/>
<buttonCell key="cell" type="roundTextured" title="Share" bezelStyle="texturedRounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="5vU-7T-yAi">
<rect key="frame" x="97" y="2" width="106" height="32"/>
<buttonCell key="cell" type="push" title="Share" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="5vU-7T-yAi">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
</button>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Olx-b9-5Qw">
<rect key="frame" x="207" y="7" width="93" height="23"/>
<buttonCell key="cell" type="roundTextured" title="Save As..." bezelStyle="texturedRounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Kiv-8z-1qn">
<rect key="frame" x="201" y="2" width="105" height="32"/>
<buttonCell key="cell" type="push" title="Save As..." bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Kiv-8z-1qn">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand Down
6 changes: 5 additions & 1 deletion Gifski/EditVideoViewController.swift
@@ -1,6 +1,7 @@
import Cocoa
import AVKit
import Defaults
import FirebaseCrashlytics

final class EditVideoViewController: NSViewController {
enum PredefinedSizeItem {
Expand Down Expand Up @@ -405,8 +406,11 @@ final class EditVideoViewController: NSViewController {
switch error {
case .cancelled:
break
case .notEnoughFrames:
self.setEstimatedFileSize(self.getNaiveEstimate().attributedString)
default:
error.presentAsModalSheet(for: self.view.window)
Crashlytics.recordNonFatalError(error: error)
self.setEstimatedFileSize("Error: \(error.localizedDescription)".truncating(to: 86).attributedString.withColor(.secondaryLabelColor).withFontSize(NSFont.smallSystemFontSize.double))
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions Gifski/Gifski.swift
Expand Up @@ -257,10 +257,7 @@ final class Gifski {
self.progress.totalUnitCount = Int64(result.totalCount)
let image = result.image

guard
let data = image.dataProvider?.data,
let buffer = CFDataGetBytePtr(data)
else {
guard let bytePointer = image.bytePointer else {
completionHandlerOnce(.failure(.generateFrameFailed(
NSError.appError("Could not get byte pointer of image data provider.")
)))
Expand All @@ -273,7 +270,7 @@ final class Gifski {
width: UInt32(image.width),
bytesPerRow: UInt32(image.bytesPerRow),
height: UInt32(image.height),
pixels: buffer,
pixels: bytePointer,
presentationTimestamp: max(0, result.actualTime.seconds - startTime)
)
} catch {
Expand Down
3 changes: 2 additions & 1 deletion Gifski/MainWindowController.swift
Expand Up @@ -33,7 +33,8 @@ final class MainWindowController: NSWindowController {
""",
buttonTitles: [
"Get Started"
]
],
defaultButtonIndex: -1
)
}

Expand Down
68 changes: 64 additions & 4 deletions Gifski/Utilities.swift
Expand Up @@ -221,23 +221,44 @@ extension NSAlert {
informativeText: String? = nil,
detailText: String? = nil,
style: Style = .warning,
buttonTitles: [String] = []
buttonTitles: [String] = [],
defaultButtonIndex: Int? = nil
) -> NSApplication.ModalResponse {
NSAlert(
message: message,
informativeText: informativeText,
detailText: detailText,
style: style,
buttonTitles: buttonTitles
buttonTitles: buttonTitles,
defaultButtonIndex: defaultButtonIndex
).runModal(for: window)
}

/// The index in the `buttonTitles` array for the button to use as default.
/// Set `-1` to not have any default. Useful for really destructive actions.
var defaultButtonIndex: Int {
get {
buttons.firstIndex { $0.keyEquivalent == "\r" } ?? -1
}
set {
// Clear the default button indicator from other buttons.
for button in buttons where button.keyEquivalent == "\r" {
button.keyEquivalent = ""
}

if newValue != -1 {
buttons[newValue].keyEquivalent = "\r"
}
}
}

convenience init(
message: String,
informativeText: String? = nil,
detailText: String? = nil,
style: Style = .warning,
buttonTitles: [String] = []
buttonTitles: [String] = [],
defaultButtonIndex: Int? = nil
) {
self.init()
self.messageText = message
Expand Down Expand Up @@ -272,7 +293,11 @@ extension NSAlert {
self.accessoryView = scrollView
}

self.addButtons(withTitles: buttonTitles)
addButtons(withTitles: buttonTitles)

if let defaultButtonIndex = defaultButtonIndex {
self.defaultButtonIndex = defaultButtonIndex
}
}

/// 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 Down Expand Up @@ -3620,3 +3645,38 @@ extension NSAttributedString {
addingAttributes([.font: font.withSize(CGFloat(fontSize))])
}
}


extension CGImage {
/// Returns a read-only pointer to the bytes of the image.
var bytePointer: UnsafePointer<UInt8>? {
guard let data = dataProvider?.data else {
return nil
}

return CFDataGetBytePtr(data)
}
}


extension String {
var trimmedTrailing: Self {
replacingOccurrences(of: #"\s+$"#, with: "", options: .regularExpression)
}

/**
```
"Unicorn".truncating(to: 4)
//=> "Uni…"
```
*/
func truncating(to number: Int, truncationIndicator: Self = "") -> Self {
if number <= 0 {
return ""
} else if count > number {
return String(prefix(number - truncationIndicator.count)).trimmedTrailing + truncationIndicator
} else {
return self
}
}
}

0 comments on commit f5d385e

Please sign in to comment.