Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update InfoButton.swift #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 55 additions & 45 deletions InfoButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,99 +11,109 @@ import Cocoa

@IBDesignable
public class InfoButton : NSControl, NSPopoverDelegate {
var mainSize: CGFloat!


@IBInspectable var showOnHover: Bool = false
@IBInspectable var fillMode: Bool = true
@IBInspectable var animatePopover: Bool = false
@IBInspectable var content: String = ""
@IBInspectable var primaryColor: NSColor = NSColor.scrollBarColor()
var secondaryColor: NSColor = NSColor.whiteColor()

@IBInspectable var primaryColor: NSColor = NSColor.scrollBarColor

var secondaryColor: NSColor = NSColor.white
var popover: NSPopover!
var trackingArea: NSTrackingArea!
var mainSize: CGFloat!

var mouseInside = false {
didSet {
self.needsDisplay = true
if showOnHover {
if popover == nil {

if (showOnHover) {
if (popover == nil) {
popover = NSPopover(content: self.content, doesAnimate: self.animatePopover)
}
if mouseInside {
popover.showRelativeToRect(self.frame, ofView: self.superview!, preferredEdge: NSRectEdge.MaxX)
} else {

if (mouseInside) {
popover.show(relativeTo: self.frame, of: self.superview!, preferredEdge: NSRectEdge.maxX)
}
else {
popover.close()
}

}
}
}

var trackingArea: NSTrackingArea!

override public func updateTrackingAreas() {
super.updateTrackingAreas()
if trackingArea != nil {

if (trackingArea != nil) {
self.removeTrackingArea(trackingArea)
}
trackingArea = NSTrackingArea(rect: self.bounds, options: [NSTrackingAreaOptions.MouseEnteredAndExited, NSTrackingAreaOptions.ActiveAlways], owner: self, userInfo: nil)
trackingArea = NSTrackingArea(rect: self.bounds, options: [NSTrackingAreaOptions.mouseEnteredAndExited, NSTrackingAreaOptions.activeAlways], owner: self, userInfo: nil)
self.addTrackingArea(trackingArea)
}

private var stringAttributeDict = [String: AnyObject]()
private var circlePath: NSBezierPath!

var popover: NSPopover!


required public init?(coder: NSCoder) {
super.init(coder: coder)

let frameSize = self.frame.size
if frameSize.width != frameSize.height {

if (frameSize.width != frameSize.height) {
self.frame.size.height = self.frame.size.width
}

self.mainSize = self.frame.size.height
stringAttributeDict[NSFontAttributeName] = NSFont.systemFontOfSize(mainSize * 0.6)
stringAttributeDict[NSFontAttributeName] = NSFont.systemFont(ofSize: mainSize * 0.6)

let inSet: CGFloat = 2
let rect = NSMakeRect(inSet, inSet, mainSize - inSet * 2, mainSize - inSet * 2)
circlePath = NSBezierPath(ovalInRect: rect)
circlePath = NSBezierPath(ovalIn: rect)
}


override public func drawRect(dirtyRect: NSRect) {
var activeColor: NSColor!
if mouseInside || (popover != nil && popover!.shown){
override public func draw(_ dirtyRect: NSRect) {
var activeColor: NSColor = primaryColor.withAlphaComponent(0.35)

if (mouseInside || (popover != nil && popover!.isShown)) {
activeColor = primaryColor
} else {
activeColor = primaryColor.colorWithAlphaComponent(0.35)
}

if fillMode {
if (fillMode) {
activeColor.setFill()
circlePath.fill()
stringAttributeDict[NSForegroundColorAttributeName] = secondaryColor
} else {
}
else {
activeColor.setStroke()
circlePath.stroke()
stringAttributeDict[NSForegroundColorAttributeName] = (mouseInside ? primaryColor : primaryColor.colorWithAlphaComponent(0.35))
stringAttributeDict[NSForegroundColorAttributeName] = activeColor
}

let attributedString = NSAttributedString(string: "?", attributes: stringAttributeDict)
let stringLocation = NSMakePoint(mainSize / 2 - attributedString.size().width / 2, mainSize / 2 - attributedString.size().height / 2)
attributedString.drawAtPoint(stringLocation)
attributedString.draw(at: stringLocation)
}

override public func mouseDown(theEvent: NSEvent) {
if popover == nil {

override public func mouseDown(with event: NSEvent) {

if (popover == nil) {
popover = NSPopover(content: self.content, doesAnimate: self.animatePopover)
popover.delegate = self
}
if popover.shown {

if (popover.isShown) {
popover.close()
} else {
popover.showRelativeToRect(self.frame, ofView: self.superview!, preferredEdge: NSRectEdge.MaxX)
}
else {
popover.show(relativeTo: self.frame, of: self.superview!, preferredEdge: NSRectEdge.maxX)
}
}

override public func mouseEntered(theEvent: NSEvent) { mouseInside = true }
override public func mouseExited(theEvent: NSEvent) { mouseInside = false }

public func popoverDidClose(_ notification: Notification) { self.needsDisplay = true }

override public func mouseEntered(with event: NSEvent) { mouseInside = true }
override public func mouseExited(with event: NSEvent) { mouseInside = false }

}

Expand All @@ -113,29 +123,29 @@ extension NSPopover {
convenience init(content: String, doesAnimate: Bool) {
self.init()

self.behavior = NSPopoverBehavior.Transient
self.behavior = NSPopoverBehavior.transient
self.animates = doesAnimate
self.contentViewController = NSViewController()
self.contentViewController!.view = NSView(frame: NSZeroRect)//remove this ??

let popoverMargin = CGFloat(20)
let textField: NSTextField = {
content in

let textField = NSTextField(frame: NSZeroRect)

textField.editable = false
textField.isEditable = false
textField.stringValue = content
textField.bordered = false
textField.isBordered = false
textField.drawsBackground = false
textField.sizeToFit()
textField.setFrameOrigin(NSMakePoint(popoverMargin, popoverMargin))
return textField

}(content)

self.contentViewController!.view.addSubview(textField)
var viewSize = textField.frame.size; viewSize.width += (popoverMargin * 2); viewSize.height += (popoverMargin * 2)
self.contentSize = viewSize

}
}
//NSMinXEdge NSMinYEdge NSMaxXEdge NSMaxYEdge