-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopoverViewController.swift
More file actions
104 lines (86 loc) · 4.38 KB
/
PopoverViewController.swift
File metadata and controls
104 lines (86 loc) · 4.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// ViewController.swift
// MacSitter
//
// Created by Matas Empakeris on 12/22/17.
// Copyright © 2017 Matas Empakeris. All rights reserved.
//
import Cocoa
import OGCircularBar
class PopoverViewController: NSViewController {
@IBOutlet weak var status: NSTextField!
@IBOutlet weak var barView: OGCircularBarView!
@IBOutlet weak var titleLabel: NSTextField!
private var batteryHealth: Int? = SysInfo.batteryHealth()
private var hddTotalSpace: Double? = SysInfo.totalHddSpace()
private var hddUsedSpace: Double? = SysInfo.usedHddSpace()
// TODO: sweep through and properly handle unwrapping optionals
override func viewDidLoad() {
super.viewDidLoad()
let barViewArea = NSTrackingArea.init(rect:barView.bounds, options: [NSTrackingArea.Options.mouseMoved, NSTrackingArea.Options.activeAlways], owner: self, userInfo: nil)
barView.addTrackingArea(barViewArea)
let blue = NSColor(calibratedRed: 20/255, green: 160/255, blue: 255, alpha: 1)
let red = NSColor(calibratedRed: 255, green: 0, blue: 0, alpha: 1)
let green = NSColor(calibratedRed: 0, green: 255, blue: 0, alpha: 1)
barView.addBarBackground(startAngle: 90, endAngle: -270, radius: 150, width: 15, color: blue.withAlphaComponent(0.1))
barView.addBarBackground(startAngle: 90, endAngle: -270, radius: 130, width: 15, color: red.withAlphaComponent(0.1))
if let batteryHealth = self.batteryHealth {
status.stringValue = "\(batteryHealth)"
barView.addBar(startAngle: 90, endAngle: -270, progress: CGFloat(batteryHealth/100), radius: 150, width: 15, color: blue, animationDuration: 1.5, glowOpacity: 0.4, glowRadius: 8)
} else {
status.stringValue = "N/A"
}
if let hddUsedSpace = self.hddUsedSpace,
let hddTotalSpace = self.hddTotalSpace {
barView.addBar(startAngle: 90, endAngle: -270, progress: CGFloat(hddUsedSpace/hddTotalSpace), radius: 130, width: 15, color: red, animationDuration: 1.5, glowOpacity: 0.4, glowRadius: 8)
}
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
//Note: view controller needs to be instantiated after application load, so view controller does not clobber statusitem launch
extension PopoverViewController {
static func freshController() -> PopoverViewController {
let storyboard = NSStoryboard(name: NSStoryboard.Name(rawValue: "Main"), bundle: nil)
let identifier = NSStoryboard.SceneIdentifier(rawValue: "PopoverViewController")
guard
let viewcontroller = storyboard.instantiateController(withIdentifier: identifier) as? PopoverViewController
else {
fatalError("ViewController not found")
}
return viewcontroller
}
override func mouseMoved(with event:NSEvent) {
let mouseLoc: CGPoint = NSPointToCGPoint(event.locationInWindow)
// Change label according to which ring we're hoving over.
// Coordinates must be converted from global to CAShapeLayer coordinates
if barView.bars[0].touchPath!.contains(barView.bars[0].convert(mouseLoc, from: nil)) {
if let batteryHealth = self.batteryHealth {
status.stringValue = "\(batteryHealth)%"
} else {
status.stringValue = "N/A"
}
recenter(status, inView: barView)
titleLabel.stringValue = "Battery Health"
} else if barView.bars[1].touchPath!.contains(barView.bars[1].convert(mouseLoc, from: nil)) {
if let hddUsedSpace = self.hddUsedSpace,
let hddTotalSpace = self.hddTotalSpace {
status.stringValue = "\(hddUsedSpace)\n Out Of\n \(hddTotalSpace)GB"
} else {
status.stringValue = "N/A"
}
recenter(status, inView: barView)
titleLabel.stringValue = "Used HD Space"
}
}
}
private extension PopoverViewController {
func recenter(_ textField: NSTextField, inView view: NSView) {
textField.sizeToFit()
textField.frame.origin.x = (view.bounds.origin.x + view.frame.width/2) - textField.frame.width/2
textField.frame.origin.y = view.bounds.origin.y + view.frame.height/2 - textField.frame.height/2
}
}