-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from ricobeck/develop
Develop
- Loading branch information
Showing
8 changed files
with
155 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// ColoredButton.swift | ||
// KFAboutWindowPreview | ||
// | ||
// Created by Gunnar Herzog on 22/09/2016. | ||
// Copyright © 2016 KF Interactive. All rights reserved. | ||
// | ||
|
||
import AppKit | ||
|
||
@available(OSX 10.10, *) | ||
public class ColoredButton: NSButton { | ||
public var color: NSColor = .black { | ||
didSet { | ||
redraw() | ||
} | ||
} | ||
|
||
required public init?(coder: NSCoder) { | ||
super.init(coder: coder) | ||
redraw() | ||
} | ||
|
||
public override var title: String { | ||
didSet { | ||
redraw() | ||
} | ||
} | ||
|
||
private func redraw() { | ||
wantsLayer = true | ||
layerContentsRedrawPolicy = .onSetNeedsDisplay | ||
layer?.cornerRadius = 10 | ||
layer?.borderWidth = 1.0 | ||
layer?.borderColor = color.cgColor | ||
|
||
let paragraphStyle = NSParagraphStyle.default().mutableCopy() as! NSMutableParagraphStyle | ||
paragraphStyle.alignment = .center | ||
attributedTitle = NSAttributedString(string: title, attributes: [NSFontAttributeName: font!, NSParagraphStyleAttributeName: paragraphStyle, NSForegroundColorAttributeName: color]) | ||
} | ||
|
||
override public var wantsUpdateLayer:Bool{ | ||
return true | ||
} | ||
|
||
override public func updateLayer() { | ||
if isHighlighted { | ||
layer?.backgroundColor = color.lighter.cgColor | ||
} else { | ||
layer?.backgroundColor = nil | ||
} | ||
super.updateLayer() | ||
} | ||
|
||
override public var intrinsicContentSize: NSSize { | ||
let width = attributedTitle.size().width | ||
|
||
var size = super.intrinsicContentSize | ||
size.width = width + 3.0 * (layer?.cornerRadius ?? 0) | ||
return size | ||
} | ||
} | ||
|
||
extension NSColor { | ||
var lighter: NSColor { | ||
return NSColor(calibratedRed: redComponent, green: greenComponent, blue: blueComponent, alpha: 0.15) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.