Skip to content

Commit

Permalink
update to Swift 4
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlZeo committed Sep 12, 2017
1 parent 861ae48 commit 45802b2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 23 deletions.
10 changes: 6 additions & 4 deletions FlatButton.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand All @@ -275,7 +275,7 @@
PRODUCT_BUNDLE_IDENTIFIER = org.cindori.FlatButton;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
VERSIONING_SYSTEM = "apple-generic";
VERSION_INFO_PREFIX = "";
};
Expand Down Expand Up @@ -328,6 +328,7 @@
SDKROOT = macosx;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand Down Expand Up @@ -370,6 +371,7 @@
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand All @@ -383,7 +385,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.10;
PRODUCT_BUNDLE_IDENTIFIER = "org.cindori.FlatButton-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Debug;
};
Expand All @@ -397,7 +399,7 @@
MACOSX_DEPLOYMENT_TARGET = 10.10;
PRODUCT_BUNDLE_IDENTIFIER = "org.cindori.FlatButton-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 4.0;
};
name = Release;
};
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
Expand All @@ -45,6 +46,7 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>FlatButton-Example.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
<key>FlatButton.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>
40 changes: 21 additions & 19 deletions FlatButton/FlatButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ internal extension CALayer {
internal extension NSColor {
internal func tintedColor() -> NSColor {
var h = CGFloat(), s = CGFloat(), b = CGFloat(), a = CGFloat()
let rgbColor = usingColorSpaceName(NSCalibratedRGBColorSpace)
let rgbColor = usingColorSpaceName(NSColorSpaceName.calibratedRGB)
rgbColor?.getHue(&h, saturation: &s, brightness: &b, alpha: &a)
return NSColor(hue: h, saturation: s, brightness: b == 0 ? 0.2 : b * 0.8, alpha: a)
}
Expand All @@ -43,21 +43,21 @@ open class FlatButton: NSButton, CALayerDelegate {
internal var mouseDown = Bool()
@IBInspectable public var momentary: Bool = true {
didSet {
animateColor(state == NSOnState)
animateColor(state == .on)
}
}
@IBInspectable public var onAnimationDuration: Double = 0
@IBInspectable public var offAnimationDuration: Double = 0.1
@IBInspectable public var glowRadius: CGFloat = 0 {
didSet {
containerLayer.shadowRadius = glowRadius
animateColor(state == NSOnState)
animateColor(state == .on)
}
}
@IBInspectable public var glowOpacity: Float = 0 {
didSet {
containerLayer.shadowOpacity = glowOpacity
animateColor(state == NSOnState)
animateColor(state == .on)
}
}
@IBInspectable public var cornerRadius: CGFloat = 4 {
Expand All @@ -72,42 +72,42 @@ open class FlatButton: NSButton, CALayerDelegate {
}
@IBInspectable public var borderColor: NSColor = NSColor.darkGray {
didSet {
animateColor(state == NSOnState)
animateColor(state == .on)
}
}
@IBInspectable public var activeBorderColor: NSColor = NSColor.white {
didSet {
animateColor(state == NSOnState)
animateColor(state == .on)
}
}
@IBInspectable public var buttonColor: NSColor = NSColor.darkGray {
didSet {
animateColor(state == NSOnState)
animateColor(state == .on)
}
}
@IBInspectable public var activeButtonColor: NSColor = NSColor.white {
didSet {
animateColor(state == NSOnState)
animateColor(state == .on)
}
}
@IBInspectable public var iconColor: NSColor = NSColor.gray {
didSet {
animateColor(state == NSOnState)
animateColor(state == .on)
}
}
@IBInspectable public var activeIconColor: NSColor = NSColor.black {
didSet {
animateColor(state == NSOnState)
animateColor(state == .on)
}
}
@IBInspectable public var textColor: NSColor = NSColor.gray {
didSet {
animateColor(state == NSOnState)
animateColor(state == .on)
}
}
@IBInspectable public var activeTextColor: NSColor = NSColor.gray {
didSet {
animateColor(state == NSOnState)
animateColor(state == .on)
}
}

Expand Down Expand Up @@ -194,7 +194,7 @@ open class FlatButton: NSButton, CALayerDelegate {
}

func positionTitleAndImage() {
let attributes = [NSFontAttributeName: font as Any]
let attributes = [NSAttributedStringKey.font: font as Any]
let titleSize = title.size(withAttributes: attributes)
var titleRect = NSMakeRect(0, 0, titleSize.width, titleSize.height)
var imageRect = iconLayer.frame
Expand Down Expand Up @@ -315,10 +315,11 @@ open class FlatButton: NSButton, CALayerDelegate {
// MARK: Interaction

public func setOn(_ isOn: Bool) {
let nextState = isOn ? NSOnState : NSOffState
// let nextState = isOn ? .on : .off
let nextState = isOn ? NSControl.StateValue.on : NSControl.StateValue.off
if nextState != state {
state = nextState
animateColor(state == NSOnState)
animateColor(state == .on)
}
}

Expand All @@ -329,19 +330,19 @@ open class FlatButton: NSButton, CALayerDelegate {
override open func mouseDown(with event: NSEvent) {
if isEnabled {
mouseDown = true
setOn(state == NSOnState ? false : true)
setOn(state == .on ? false : true)
}
}

override open func mouseEntered(with event: NSEvent) {
if mouseDown {
setOn(state == NSOnState ? false : true)
setOn(state == .on ? false : true)
}
}

override open func mouseExited(with event: NSEvent) {
if mouseDown {
setOn(state == NSOnState ? false : true)
setOn(state == .on ? false : true)
mouseDown = false
}
}
Expand All @@ -350,7 +351,7 @@ open class FlatButton: NSButton, CALayerDelegate {
if mouseDown {
mouseDown = false
if momentary {
setOn(state == NSOnState ? false : true)
setOn(state == .on ? false : true)
}
_ = target?.perform(action, with: self)
}
Expand Down Expand Up @@ -386,3 +387,4 @@ open class FlatButton: NSButton, CALayerDelegate {


}

0 comments on commit 45802b2

Please sign in to comment.