Skip to content

Commit

Permalink
Custom border color, title adjustment
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarGroth committed Mar 9, 2017
1 parent c56c462 commit 1176c5e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
Binary file not shown.
38 changes: 22 additions & 16 deletions FlatButton/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -730,16 +730,16 @@
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="E4h-Kk-N40" customClass="FlatButton" customModule="FlatButton_Example" customModuleProvider="target">
<rect key="frame" x="349" y="258" width="120" height="38"/>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="iGx-ll-GI1" customClass="FlatButton" customModule="FlatButton_Example" customModuleProvider="target">
<rect key="frame" x="349" y="170" width="50" height="50"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="square" title="Button" bezelStyle="shadowlessSquare" imagePosition="above" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="DJi-tN-P3q">
<buttonCell key="cell" type="square" bezelStyle="shadowlessSquare" image="NSBookmarksTemplate" imagePosition="overlaps" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="ywI-Fd-2ZO">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="cornerRadius">
<real key="value" value="4"/>
<real key="value" value="25"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="borderWidth">
<real key="value" value="1"/>
Expand All @@ -748,13 +748,13 @@
<color key="value" white="1" alpha="0.0" colorSpace="calibratedWhite"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="activeButtonColor">
<color key="value" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
<color key="value" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="iconColor">
<color key="value" red="0.2392156869" green="0.67450982329999998" blue="0.96862745289999996" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="value" red="0.42465101522842641" green="0.23259536125797645" blue="0.22464185002913825" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="activeIconColor">
<color key="value" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
<color key="value" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="textColor">
<color key="value" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
Expand All @@ -764,17 +764,23 @@
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="boolean" keyPath="momentary" value="NO"/>
<userDefinedRuntimeAttribute type="number" keyPath="offAnimationDuration">
<real key="value" value="1"/>
<real key="value" value="0.20000000000000001"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="onAnimationDuration">
<real key="value" value="0.10000000000000001"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="borderColor">
<color key="value" red="0.42465101519999998" green="0.2325953613" blue="0.22464185" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="activeBorderColor">
<color key="value" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="iGx-ll-GI1" customClass="FlatButton" customModule="FlatButton_Example" customModuleProvider="target">
<rect key="frame" x="349" y="168" width="120" height="38"/>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="E4h-Kk-N40" customClass="FlatButton" customModule="FlatButton_Example" customModuleProvider="target">
<rect key="frame" x="106" y="260" width="120" height="38"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="square" title="Button" bezelStyle="shadowlessSquare" image="NSApplicationIcon" imagePosition="only" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="ywI-Fd-2ZO">
<buttonCell key="cell" type="square" title="Button" bezelStyle="shadowlessSquare" imagePosition="above" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="DJi-tN-P3q">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand All @@ -786,10 +792,10 @@
<real key="value" value="1"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="buttonColor">
<color key="value" white="1" alpha="0.0" colorSpace="calibratedWhite"/>
<color key="value" red="0.51891875267478793" green="0.86372964800924135" blue="0.96871268749999995" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="activeButtonColor">
<color key="value" white="0.0" alpha="1" colorSpace="calibratedWhite"/>
<color key="value" red="0.0" green="0.25098040700000002" blue="0.50196081400000003" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="iconColor">
<color key="value" red="0.2392156869" green="0.67450982329999998" blue="0.96862745289999996" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
Expand All @@ -803,9 +809,9 @@
<userDefinedRuntimeAttribute type="color" keyPath="activeTextColor">
<color key="value" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="boolean" keyPath="momentary" value="NO"/>
<userDefinedRuntimeAttribute type="boolean" keyPath="momentary" value="YES"/>
<userDefinedRuntimeAttribute type="number" keyPath="offAnimationDuration">
<real key="value" value="1"/>
<real key="value" value="0.20000000000000001"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="onAnimationDuration">
<real key="value" value="0.10000000000000001"/>
Expand All @@ -819,6 +825,6 @@
</objects>
<resources>
<image name="NSActionTemplate" width="14" height="14"/>
<image name="NSApplicationIcon" width="128" height="128"/>
<image name="NSBookmarksTemplate" width="17" height="18"/>
</resources>
</document>
22 changes: 17 additions & 5 deletions FlatButton/FlatButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public class FlatButton: NSButton, CALayerDelegate {
layer?.borderWidth = borderWidth
}
}
@IBInspectable public var borderColor: NSColor = NSColor.darkGray {
didSet {
animateColor(state == NSOnState)
}
}
@IBInspectable public var activeBorderColor: NSColor = NSColor.white {
didSet {
animateColor(state == NSOnState)
}
}
@IBInspectable public var buttonColor: NSColor = NSColor.darkGray {
didSet {
animateColor(state == NSOnState)
Expand Down Expand Up @@ -86,6 +96,7 @@ public class FlatButton: NSButton, CALayerDelegate {
animateColor(state == NSOnState)
}
}

override public var title: String {
didSet {
setupTitle()
Expand Down Expand Up @@ -146,6 +157,7 @@ public class FlatButton: NSButton, CALayerDelegate {
let attributes = [NSFontAttributeName: font]
let size = title.size(withAttributes: attributes)
titleRect.origin.x = round((bounds.width - size.width)/2)
titleRect.origin.y = round((bounds.height - size.height)/2)
titleLayer.frame = titleRect
titleLayer.string = title
titleLayer.font = font
Expand Down Expand Up @@ -174,7 +186,7 @@ public class FlatButton: NSButton, CALayerDelegate {
}

// MARK: Animations

internal func removeAnimations() {
layer?.removeAllAnimations()
if layer?.sublayers != nil {
Expand All @@ -190,14 +202,14 @@ public class FlatButton: NSButton, CALayerDelegate {
let bgColor = isOn ? activeButtonColor : buttonColor
let titleColor = isOn ? activeTextColor : textColor
let imageColor = isOn ? activeIconColor : iconColor
let borderColor = bgColor
let borderColor = isOn ? activeBorderColor : self.borderColor
layer?.animate(color: bgColor.cgColor, keyPath: "backgroundColor", duration: duration)
layer?.animate(color: borderColor.cgColor, keyPath: "borderColor", duration: duration)

/* I started seeing high (~5%) background CPU usage in apps using
FlatButton, and was able to track it down to background CATextLayer animation calls
happening constantly, originating from the call below. It could be a CATextLayer bug.
For now I'm going with setting the color instantly as it fixes this issue. */
FlatButton, and was able to track it down to background CATextLayer animation calls
happening constantly, originating from the call below. It could be a CATextLayer bug.
For now I'm going with setting the color instantly as it fixes this issue. */
//titleLayer.animate(color: titleColor.cgColor, keyPath: "foregroundColor", duration: duration)
titleLayer.foregroundColor = titleColor.cgColor

Expand Down

0 comments on commit 1176c5e

Please sign in to comment.