Skip to content

Commit

Permalink
Add AM/PM support
Browse files Browse the repository at this point in the history
  • Loading branch information
vpeschenkov committed May 12, 2019
1 parent 9010d3c commit d1dccbd
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 49 deletions.
33 changes: 19 additions & 14 deletions PredatorClock/Preferences/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,48 @@ final class Preferences {
static let github = "https://github.com/vpeschenkov/predator-clock-screensaver"
static let twitter = "https://twitter.com/vpeschenkov"

public var primaryColor: NSColor? {
public var primaryColor: NSColor {
set {
if let value = newValue {
defaults.set(NSKeyedArchiver.archivedData(withRootObject: value), forKey: Key.primaryColor)
} else {
defaults.set(nil, forKey: Key.primaryColor)
}
defaults.set(NSKeyedArchiver.archivedData(withRootObject: newValue), forKey: Key.primaryColor)
defaults.synchronize()
}

get {
if let value = defaults.object(forKey: Key.primaryColor) as? Data {
return NSKeyedUnarchiver.unarchiveObject(with: value) as? NSColor
}
return nil
return NSKeyedUnarchiver.unarchiveObject(with: defaults.object(forKey: Key.primaryColor) as! Data) as! NSColor
}
}

public var reverseFilling: Bool? {
public var reverseFilling: Bool {
set {
defaults.set(newValue, forKey: Key.reverseFilling)
defaults.synchronize()
}

get {
return defaults.object(forKey: Key.reverseFilling) as? Bool
return defaults.object(forKey: Key.reverseFilling) as? Bool ?? false
}
}

public var twentyFourClockFormat: Bool? {
public var twentyFourClockFormat: Bool {
set {
defaults.set(newValue, forKey: Key.twentyFourClockFormat)
defaults.synchronize()
}

get {
return defaults.object(forKey: Key.twentyFourClockFormat) as? Bool
return defaults.object(forKey: Key.twentyFourClockFormat) as? Bool ?? false
}
}

public var isTwentyFourClockFormat: Bool {
get {
return twentyFourClockFormat
}
}

public var isTwelveFourClockFormat: Bool {
get {
return twentyFourClockFormat == false
}
}

Expand Down
13 changes: 4 additions & 9 deletions PredatorClock/Preferences/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,16 @@ final class PreferencesWindowController: NSWindowController {
@IBOutlet var colorPicker: NSColorWell!
@IBOutlet var reverseButton: NSButton!
@IBOutlet var previewView: NSView!
@IBOutlet var twentyHourButton: NSButton!

private lazy var preferences = Preferences.shared

override func windowDidLoad() {
super.windowDidLoad()

if let isReverseFiling = Preferences.shared.reverseFilling {
reverseButton.state = isReverseFiling ? .on : .off
} else {
reverseButton.state = .off
}

if let color = Preferences.shared.primaryColor {
colorPicker.color = color
}
colorPicker.color = preferences.primaryColor
reverseButton.state = preferences.reverseFilling ? .on : .off
twentyHourButton.state = preferences.twentyFourClockFormat ? .on : .off
}
}

Expand Down
1 change: 1 addition & 0 deletions PredatorClock/Preferences/PreferencesWindowController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<outlet property="colorPicker" destination="PFI-UJ-nv7" id="9cy-56-h37"/>
<outlet property="previewView" destination="TPH-Cu-qkW" id="RIv-nX-syw"/>
<outlet property="reverseButton" destination="Vnv-3I-Nmy" id="QXm-lU-bTl"/>
<outlet property="twentyHourButton" destination="wdp-Vx-SFy" id="9fp-hz-JL2"/>
<outlet property="window" destination="QvC-M9-y7g" id="7BP-Vu-9WE"/>
</connections>
</customObject>
Expand Down
96 changes: 70 additions & 26 deletions PredatorClockCore/PredatorClock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,71 @@ import QuartzCore
// MARK: - Predator Clock Screen Saver

final public class PredatorClock {
private lazy var preferences = Preferences.shared

public init() {

}

public func draw(in rect: CGRect, context: CGContext) {
// Time
let date = Date()
let calendar = Calendar.current
let hours = calendar.component(.hour, from: date)
var hours = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let width = min(rect.size.width, rect.size.height) * 0.25
// Converts time to 12-hour clock format
if preferences.isTwelveFourClockFormat {
hours = hours > 12 ? hours - 12 : hours
}

// Width
var width = CGFloat(0)
if preferences.isTwentyFourClockFormat {
width = min(rect.size.width, rect.size.height) * 0.25
} else {
width = min(rect.size.width, rect.size.height) * 0.23
}

let height = width * 1.5
let center = CGPoint(x: rect.width / 2.0, y: rect.height / 2.0)
let nummberSize = CGSize(width: width, height: height)
drawPredatorHourHight(in: CGRect(
drawHourHightNumber(in: CGRect(
x: center.x - nummberSize.width * 2.0,
y: center.y - nummberSize.height / 2.0,
width: nummberSize.width,
height: nummberSize.height
), context: context, value: Int(hours / 10))
drawPredatorHourLow(in: CGRect(
drawHourLowNumber(in: CGRect(
x: center.x - nummberSize.width,
y: center.y - nummberSize.height / 2.0,
width: nummberSize.width,
height: nummberSize.height
), context: context, value: Int(hours % 10))
drawPredatorMinuteHight(in: CGRect(
drawMinuteHightNumber(in: CGRect(
x: center.x,
y: center.y - nummberSize.height / 2.0,
width: nummberSize.width,
height: nummberSize.height
), context: context, value: Int(minutes / 10))
drawPredatorMinuteLow(in: CGRect(
drawMinuteLowNumber(in: CGRect(
x: center.x + nummberSize.width,
y: center.y - nummberSize.height / 2.0,
width: nummberSize.width,
height: nummberSize.height
), context: context, value: Int(minutes % 10))
if preferences.isTwelveFourClockFormat {
drawClockFormat(in: CGRect(
x: center.x + nummberSize.width * 2.0,
y: center.y - nummberSize.height / 2.0,
width: nummberSize.width,
height: nummberSize.height
), context: context, value: Int(hours % 12))
}
}

// MARK: - Hours

func drawPredatorHourHight(in rect: CGRect, context: CGContext, value: Int) {
func drawHourHightNumber(in rect: CGRect, context: CGContext, value: Int) {
let width = rect.size.width * 0.384
let height = width * 0.2
let shape = CGSize(width: width, height: height)
Expand All @@ -73,18 +95,27 @@ final public class PredatorClock {
let p8 = CGPoint(x: p1.x + shape.width * 0.313, y: p7.y - shape.height)
let p9 = CGPoint(x: p1.x + shape.width * 0.268, y: p6.y - shape.width * 0.713)
// Drawing
draw(p1, 90, (value >= 1))
draw(p2, 0, (value >= 1))
draw(p3, 0, (value >= 1))
draw(p4, 45, (value >= 1))
draw(p5, -45, (value >= 1))
draw(p6, -45, (value >= 2))
draw(p7, 90, (value >= 2))
draw(p8, 90, (value >= 2))
draw(p9, 45, (value >= 2))
let isEnabled = { (value: Int, part: Int) -> Bool in
if self.preferences.isTwentyFourClockFormat {
if value >= part {
return true
}
return false
}
return value >= 1
}
draw(p1, 90, isEnabled(value, 1))
draw(p2, 0, isEnabled(value, 1))
draw(p3, 0, isEnabled(value, 1))
draw(p4, 45, isEnabled(value, 1))
draw(p5, -45, isEnabled(value, 1))
draw(p6, -45, isEnabled(value, 2))
draw(p7, 90, isEnabled(value, 2))
draw(p8, 90, isEnabled(value, 2))
draw(p9, 45, isEnabled(value, 2))
}

func drawPredatorHourLow(in rect: CGRect, context: CGContext, value: Int) {
func drawHourLowNumber(in rect: CGRect, context: CGContext, value: Int) {
let width = rect.size.width * 0.384
let height = width * 0.2
let shape = CGSize(width: width, height: height)
Expand Down Expand Up @@ -116,7 +147,7 @@ final public class PredatorClock {

// MARK: - Minutes

func drawPredatorMinuteHight(in rect: CGRect, context: CGContext, value: Int) {
func drawMinuteHightNumber(in rect: CGRect, context: CGContext, value: Int) {
let width = rect.size.width * 0.384
let height = width * 0.2
let shape = CGSize(width: width, height: height)
Expand Down Expand Up @@ -146,7 +177,7 @@ final public class PredatorClock {
draw(p9, 45, (value >= 4))
}

func drawPredatorMinuteLow(in rect: CGRect, context: CGContext, value: Int) {
func drawMinuteLowNumber(in rect: CGRect, context: CGContext, value: Int) {
let width = rect.size.width * 0.384
let height = width * 0.2
let shape = CGSize(width: width, height: height)
Expand Down Expand Up @@ -176,16 +207,29 @@ final public class PredatorClock {
draw(p9, 45, (value >= 8))
}

// MARK: - AM/PM

func drawClockFormat(in rect: CGRect, context: CGContext, value: Int) {
let width = rect.size.width * 0.384
let height = width * 0.2
let shape = CGSize(width: width, height: height)
let center = CGPoint(x: rect.origin.x + rect.width / 2.0, y: rect.origin.y + rect.height / 2.0)
let draw = { (point: CGPoint, angle: CGFloat, enabled: Bool) in
self.drawPredatorLine(in: context, point:point, shape: shape, angle:angle, enabled: enabled)
}
// Define lines
let p1 = CGPoint(x: center.x - shape.width, y: rect.origin.y + rect.height)
// Drawing
draw(p1, -45, (value >= 1))
}

// MARK: - Drawing

func drawPredatorLine(in context: CGContext, point: CGPoint, shape: CGSize, angle: CGFloat, enabled: Bool) {
guard var color = Preferences.shared.primaryColor?.cgColor else {
return
}
let isReverseFiling = Preferences.shared.reverseFilling ?? false
let isEnabled = isReverseFiling == true ? !enabled : enabled
var color = preferences.primaryColor.cgColor
let isFilling = preferences.reverseFilling == true ? !enabled : enabled
context.saveGState()
if !isEnabled {
if isFilling == false {
color = color.copy(alpha: 0.3) ?? color
}
context.rotate(point, angle: angle)
Expand All @@ -200,7 +244,7 @@ final public class PredatorClock {
)))
context.closePath()
context.fillPath()
if !isEnabled {
if isFilling == false {
context.addPath(self.buildPredatorShapePath(in: CGRect(
x: point.x,
y: point.y,
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@ Please close your System Preferences after installing

## Clock rules

### 24-hour clock

### 12-hour clock

## Contributing

- If you **need help** or you'd like to **ask a general question**, open an issue.
- If you **found a bug**, open an issue.
- If you **have a feature request**, open an issue.
- If you **want to contribute**, submit a pull request.

## Aknowlegements

## Author

Victor Peschenkov, [email protected]
Expand Down

0 comments on commit d1dccbd

Please sign in to comment.