Skip to content

Commit

Permalink
Make the window light by default and other tweaks
Browse files Browse the repository at this point in the history
This is in preparation for macOS 10.14 where we will adhere to the system dark mdoe.
  • Loading branch information
sindresorhus committed Jun 13, 2018
1 parent 0981714 commit eaaafe2
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 125 deletions.
2 changes: 1 addition & 1 deletion Gifski/AppDelegate.swift
@@ -1,7 +1,7 @@
import Cocoa

extension NSColor {
static let appTheme = NSColor(named: NSColor.Name("Theme"))!
static let appTheme = NSColor.controlAccent
}

extension Defaults.Keys {
Expand Down
49 changes: 46 additions & 3 deletions Gifski/Base.lproj/MainMenu.xib
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="13771" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14113" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" customObjectInstantitationMethod="direct">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="13771"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14113"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
Expand All @@ -12,7 +12,6 @@
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Gifski" customModuleProvider="target"/>
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<menu title="Main Menu" systemMenu="main" id="AYu-sK-qS6">
<items>
<menuItem title="Gifski" id="1Xt-HY-uBw">
Expand Down Expand Up @@ -75,6 +74,50 @@
</items>
</menu>
</menuItem>
<menuItem title="Edit" id="Z9x-J4-sr3">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Edit" id="A7Z-RM-CEr">
<items>
<menuItem title="Undo" keyEquivalent="z" id="XxF-g6-8gj">
<connections>
<action selector="undo:" target="-1" id="cdb-ie-fRX"/>
</connections>
</menuItem>
<menuItem title="Redo" keyEquivalent="Z" id="pNa-kf-ZFr">
<connections>
<action selector="redo:" target="-1" id="HT3-OM-fOn"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="iSG-fK-eGG"/>
<menuItem title="Cut" keyEquivalent="x" id="kFw-0b-l3a">
<connections>
<action selector="cut:" target="-1" id="Ff2-iu-Jsw"/>
</connections>
</menuItem>
<menuItem title="Copy" keyEquivalent="c" id="phR-7h-7SM">
<connections>
<action selector="copy:" target="-1" id="dGT-YT-eKC"/>
</connections>
</menuItem>
<menuItem title="Paste" keyEquivalent="v" id="9u3-zH-AIQ">
<connections>
<action selector="paste:" target="-1" id="Fep-GZ-Tyv"/>
</connections>
</menuItem>
<menuItem title="Delete" id="y4j-Br-oa2">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="delete:" target="-1" id="I38-Ir-zfJ"/>
</connections>
</menuItem>
<menuItem title="Select All" keyEquivalent="a" id="pXP-lC-qeb">
<connections>
<action selector="selectAll:" target="-1" id="qKw-tG-RCC"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
<menuItem title="Window" id="aUF-d1-5bR">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Window" systemMenu="window" id="Td7-aD-5lo">
Expand Down
5 changes: 4 additions & 1 deletion Gifski/MainWindowController.swift
Expand Up @@ -38,13 +38,16 @@ final class MainWindowController: NSWindowController {
self.init(window: window)

with(window) {
$0.appearance = .dark
$0.appearance = NSAppearance(named: .vibrantLight)
$0.titleVisibility = .hidden
$0.styleMask = [.titled, .closable, .miniaturizable, .fullSizeContentView]
$0.tabbingMode = .disallowed
$0.titlebarAppearsTransparent = true
$0.isMovableByWindowBackground = true
$0.isRestorable = false

let vibrancyView = $0.contentView?.insertVibrancyView()
vibrancyView?.state = .active
}

view?.addSubview(circularProgress)
Expand Down
12 changes: 6 additions & 6 deletions Gifski/VideoDropView.swift
Expand Up @@ -10,12 +10,12 @@ class DropView: SSView {
}

lazy private var dropLabel = with(Label()) {
$0.textColor = .textColorDarkMode
$0.font = .systemFont(ofSize: 14, weight: .light)
$0.textColor = .disabledControlTextColor
$0.font = .systemFont(ofSize: 14, weight: .regular)
}

var highlightColor: NSColor {
return .selectedControlColor
return .controlAccent
}

var acceptedTypes: [NSPasteboard.PasteboardType] {
Expand All @@ -25,7 +25,7 @@ class DropView: SSView {
private var isDraggingHighlighted: Bool = false {
didSet {
needsDisplay = true
dropLabel.animateTextColor(to: isDraggingHighlighted ? highlightColor : .textColorDarkMode, duration: 0.3)
dropLabel.animateTextColor(to: isDraggingHighlighted ? highlightColor : .disabledControlTextColor, duration: 0.2)
}
}

Expand Down Expand Up @@ -94,11 +94,11 @@ final class VideoDropView: DropView {
}

override func onEntered(_ sender: NSDraggingInfo) -> Bool {
return sender.fileURLs(types: System.supportedVideoTypes).count == 1
return sender.draggingPasteboard().fileURLs(types: System.supportedVideoTypes).count == 1
}

override func onPerform(_ sender: NSDraggingInfo) -> Bool {
if let url = sender.fileURLs(types: System.supportedVideoTypes).first {
if let url = sender.draggingPasteboard().fileURLs(types: System.supportedVideoTypes).first {
onComplete?([url])
return true
}
Expand Down
155 changes: 41 additions & 114 deletions Gifski/util.swift
Expand Up @@ -43,6 +43,18 @@ struct Meta {
}


/// macOS 10.14 polyfills
extension NSColor {
static let controlAccent: NSColor = {
if #available(macOS 10.14, *) {
return NSColor.controlAccent
} else {
return NSColor(red: 0.10, green: 0.47, blue: 0.98, alpha: 1)
}
}()
}


/// This is useful as `awakeFromNib` is not called for programatically created views
class SSView: NSView {
var didAppearWasCalled = false
Expand Down Expand Up @@ -113,6 +125,23 @@ extension NSWindowController {
}


extension NSView {
@discardableResult
func insertVibrancyView(
withAppearance appearance: NSAppearance.Name = .vibrantLight,
material: NSVisualEffectView.Material = .appearanceBased
) -> NSVisualEffectView {
let view = NSVisualEffectView(frame: bounds)
view.autoresizingMask = [.width, .height]
view.blendingMode = .behindWindow
view.appearance = NSAppearance(named: appearance)
view.material = material
addSubview(view, positioned: .below, relativeTo: nil)
return view
}
}


extension NSWindow {
var toolbarView: NSView? {
return standardWindowButton(.closeButton)?.superview
Expand Down Expand Up @@ -140,92 +169,6 @@ extension NSWindowController: NSWindowDelegate {
}


extension NSAppearance {
static let aqua = NSAppearance(named: .aqua)!
static let light = NSAppearance(named: .vibrantLight)!
static let dark = NSAppearance(named: .vibrantDark)!

static var system: NSAppearance {
let isDark = UserDefaults.standard.string(forKey: "AppleInterfaceStyle") == "Dark"
return NSAppearance(named: isDark ? .vibrantDark : .vibrantLight)!
}
}


extension NSColor {
static let textColorDarkMode = NSColor(red: 0.7, green: 0.7, blue: 0.7, alpha: 1)
}


extension NSColor {
/// Get the complementary color of the current color
var complementary: NSColor {
guard let ciColor = CIColor(color: self) else {
return self
}

let compRed = 1 - ciColor.red
let compGreen = 1 - ciColor.green
let compBlue = 1 - ciColor.blue

return NSColor(red: compRed, green: compGreen, blue: compBlue, alpha: alphaComponent)
}
}


extension NSColor {
typealias HSBAColor = (hue: Double, saturation: Double, brightness: Double, alpha: Double)
var hsba: HSBAColor {
var hue: CGFloat = 0
var saturation: CGFloat = 0
var brightness: CGFloat = 0
var alpha: CGFloat = 0
getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha)
return HSBAColor(Double(hue), Double(saturation), Double(brightness), Double(alpha))
}

private func colorWithBrightness(factor: Double) -> NSColor {
let x = hsba
return NSColor(
hue: CGFloat(x.hue),
saturation: CGFloat(x.saturation),
brightness: CGFloat(x.brightness * factor),
alpha: CGFloat(x.alpha)
)
}

func lightening(by percent: Double) -> NSColor {
return colorWithBrightness(factor: 1 + percent)
}

func darkening(by percent: Double) -> NSColor {
return colorWithBrightness(factor: 1 - percent)
}
}


extension NSView {
/**
Iterate through subviews of a specific type and change properties on them
```
view.forEachSubview(ofType: NSTextField.self) {
$0.textColor = .white
}
```
*/
func forEachSubview<T>(ofType type: T.Type, deep: Bool = true, closure: (T) -> Void) {
for view in subviews {
if let view = view as? T {
closure(view)
} else if deep {
view.forEachSubview(ofType: type, deep: deep, closure: closure)
}
}
}
}


extension NSAlert {
/// Show a modal alert sheet on a window
/// If the window is nil, it will be a app-modal alert
Expand Down Expand Up @@ -369,7 +312,7 @@ extension AVAssetImageGenerator {
}
}


/// TODO: Remove this when targeting macOS 10.14
extension CMTime {
static var zero: CMTime = kCMTimeZero
static var invalid: CMTime = kCMTimeInvalid
Expand Down Expand Up @@ -553,7 +496,6 @@ extension CALayer {
extension CALayer {
/// This is required for CALayers that are created independently of a view
func setAutomaticContentsScale() {
/// TODO: This should ideally use the screen the layer is currently on. I think we need to first find the window the layer is positioned and then the screen from that.
contentsScale = NSScreen.main?.backingScaleFactor ?? 2
}
}
Expand Down Expand Up @@ -679,24 +621,22 @@ func unimplemented(function: StaticString = #function, file: String = #file, lin
}


extension NSDraggingInfo {
extension NSPasteboard {
/// Get the file URLs from dragged and dropped files
func fileURLs(types: [String] = ["public.item"]) -> [URL] {
guard draggingPasteboard().types?.contains(.fileURL) == true else {
return []
func fileURLs(types: [String] = []) -> [URL] {
var options: [NSPasteboard.ReadingOptionKey: Any] = [
.urlReadingFileURLsOnly: true
]

if !types.isEmpty {
options[.urlReadingContentsConformToTypes] = types
}

if let urls = draggingPasteboard().readObjects(
forClasses: [NSURL.self],
options: [
.urlReadingFileURLsOnly: true,
.urlReadingContentsConformToTypes: types
]
) as? [URL] {
return urls
guard let urls = readObjects(forClasses: [NSURL.self], options: options) as? [URL] else {
return []
}

return []
return urls
}
}

Expand Down Expand Up @@ -773,19 +713,6 @@ extension NSMenuItem {
}


extension UserDefaults {
@nonobjc
subscript(key: String) -> Any? {
get {
return object(forKey: key)
}
set {
set(newValue, forKey: key)
}
}
}


extension NSControl {
typealias ActionClosure = ((NSControl) -> Void)

Expand Down

0 comments on commit eaaafe2

Please sign in to comment.