Skip to content

Commit

Permalink
Blackout the non filled shapes when displaying the time (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpeschenkov committed May 31, 2019
1 parent 313f85a commit 30aae0b
Show file tree
Hide file tree
Showing 12 changed files with 115 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
Expand Down
4 changes: 2 additions & 2 deletions Predator/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0.4</string>
<string>1.0.5</string>
<key>CFBundleVersion</key>
<string>11</string>
<string>12</string>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2019 Victor Peschenkov. All rights reserved.</string>
<key>NSPrincipalClass</key>
Expand Down
2 changes: 1 addition & 1 deletion Predator/PredatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class PredatorView: ScreenSaverView {
animationTimeInterval = Preferences.animationTimeInterval
startAnimation()

if Preferences.shared.isAutoInstallUpdates {
if Preferences.shared.autoUpdates {
if let updater = SUUpdater(for: Bundle(for: PredatorView.self)) {
// 1 day
if updater.lastUpdateCheckDate.timeIntervalSinceNow.distance(to: -86400) > 0 {
Expand Down
49 changes: 33 additions & 16 deletions Predator/Preferences/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import PredatorCore

final class Preferences: Configuration {

static let shared = Preferences()
static let animationTimeInterval = TimeInterval(1.0)
static let preferencesIdentifier = "PreferencesWindowController"
static let github = "https://github.com/vpeschenkov/predator-clock-screensaver"
static let twitter = "https://twitter.com/vpeschenkov"
static public let shared = Preferences()
static public let animationTimeInterval = TimeInterval(1.0)
static public let preferencesIdentifier = "PreferencesWindowController"
static public let github = "https://github.com/vpeschenkov/predator-clock-screensaver"
static public let twitter = "https://twitter.com/vpeschenkov"

public let bundle = Bundle(for: Preferences.self)

public var primaryColor: NSColor {
set {
Expand All @@ -34,63 +36,78 @@ final class Preferences: Configuration {
}
}

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

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

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

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

public var isTwelveFourClock: Bool {
public var twelveHours: Bool {
get {
return isTwentyFourClock == false
return twentyFourHours == false
}
}

public var isAutoInstallUpdates: Bool {
public var autoUpdates: Bool {
set {
defaults.set(newValue, forKey: Key.autoInstallUpdates)
defaults.synchronize()
}

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

// MARK: - Key
public var drawEmptyDigits: Bool {
set {
defaults.set(newValue, forKey: Key.drawEmptyDigits)
defaults.synchronize()
}

get {

return defaults.bool(forKey: Key.drawEmptyDigits)
}
}

// MARK: - Keys

private enum Key {
static let primaryColor = "primory-color-key"
static let reverseFilling = "reverse-filling-key"
static let twentyFourClockFormat = "twenty-four-clock-format-key"
static let autoInstallUpdates = "auto-install-updates"
static let drawEmptyDigits = "draw-empty-digits"
}

fileprivate lazy var defaults: ScreenSaverDefaults = {
guard let defaults = ScreenSaverDefaults(forModuleWithName: "com.vpeschenkov.predator-clock") else {
guard let bundleIdentifier = bundle.bundleIdentifier, let defaults = ScreenSaverDefaults(forModuleWithName: bundleIdentifier) else {
fatalError("Failed to retrieve settings")
}

defaults.register(defaults: [
Key.primaryColor: NSKeyedArchiver.archivedData(withRootObject: NSColor.red),
Key.reverseFilling: false,
Key.twentyFourClockFormat: true,
Key.autoInstallUpdates: false
Key.autoInstallUpdates: false,
Key.drawEmptyDigits: false
])
return defaults
}()
Expand Down
24 changes: 16 additions & 8 deletions Predator/Preferences/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ final class PreferencesWindowController: NSWindowController {
@IBOutlet var reverseCheckbox: NSButton!
@IBOutlet var twentyHourCheckbox: NSButton!
@IBOutlet var autoInstallUpdatesCheckbox: NSButton!
@IBOutlet var drawEmptyDigitsCheckbox: NSButton!
@IBOutlet var versionLabel: NSTextField!

private lazy var preferences = Preferences.shared
Expand All @@ -29,9 +30,11 @@ final class PreferencesWindowController: NSWindowController {
super.windowDidLoad()

colorPicker.color = preferences.primaryColor
reverseCheckbox.state = preferences.isReverse ? .on : .off
twentyHourCheckbox.state = preferences.isTwentyFourClock ? .on : .off
if let version = Bundle(for: PreferencesWindowController.self).infoDictionary?["CFBundleShortVersionString"] as? String {
reverseCheckbox.state = preferences.reverse ? .on : .off
twentyHourCheckbox.state = preferences.twentyFourHours ? .on : .off
drawEmptyDigitsCheckbox.state = preferences.drawEmptyDigits ? .on : .off

if let version = preferences.bundle.infoDictionary?["CFBundleShortVersionString"] as? String {
versionLabel.stringValue = "Predator's version \(version)"
}

Expand All @@ -43,7 +46,7 @@ final class PreferencesWindowController: NSWindowController {
object: nil
)

autoInstallUpdatesCheckbox.state = preferences.isAutoInstallUpdates ? .on : .off
autoInstallUpdatesCheckbox.state = preferences.autoUpdates ? .on : .off

let updater = SUUpdater(for: Bundle(for: PreferencesWindowController.self))
updater?.checkForUpdatesInBackground()
Expand All @@ -62,7 +65,7 @@ final class PreferencesWindowController: NSWindowController {
extension PreferencesWindowController {

@IBAction func reverseAction(_ sender: NSButton) {
preferences.isReverse = sender.state == .on ? true : false
preferences.reverse = sender.state == .on ? true : false
previewView.setNeedsDisplay(previewView.bounds)
}

Expand All @@ -71,13 +74,18 @@ extension PreferencesWindowController {
previewView.setNeedsDisplay(previewView.bounds)
}

@IBAction func twentyFourClockFormat(_ sender: NSButton) {
preferences.isTwentyFourClock = sender.state == .on ? true : false
@IBAction func twentyFourClockFormatAction(_ sender: NSButton) {
preferences.twentyFourHours = sender.state == .on ? true : false
previewView.setNeedsDisplay(previewView.bounds)
}

@IBAction func autoInstallUpdatesAction(_ sender: NSButton) {
preferences.isAutoInstallUpdates = sender.state == .on ? true : false
preferences.autoUpdates = sender.state == .on ? true : false
}

@IBAction func drawEmptyDigitsAction(_ sender: NSButton) {
preferences.drawEmptyDigits = sender.state == .on ? true : false
previewView.setNeedsDisplay(previewView.bounds)
}

@IBAction func githubAction(_ sender: Any) {
Expand Down
24 changes: 20 additions & 4 deletions Predator/Preferences/PreferencesWindowController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<connections>
<outlet property="autoInstallUpdatesCheckbox" destination="7PC-sc-V9a" id="2zu-7h-S9s"/>
<outlet property="colorPicker" destination="PFI-UJ-nv7" id="9cy-56-h37"/>
<outlet property="drawEmptyDigitsCheckbox" destination="t0E-5m-5Wc" id="zcO-98-H1p"/>
<outlet property="previewView" destination="Bnn-dY-5vI" id="8ue-e3-Zel"/>
<outlet property="reverseCheckbox" destination="Vnv-3I-Nmy" id="KW1-DP-fDw"/>
<outlet property="twentyHourCheckbox" destination="wdp-Vx-SFy" id="kMp-rq-0lE"/>
Expand All @@ -23,7 +24,7 @@
<windowStyleMask key="styleMask" titled="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="196" y="240" width="560" height="320"/>
<rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
<rect key="screenRect" x="0.0" y="0.0" width="1920" height="1057"/>
<view key="contentView" wantsLayer="YES" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="560" height="309"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
Expand Down Expand Up @@ -116,7 +117,7 @@ DQ
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="twentyFourClockFormat:" target="-2" id="lR8-Oz-no9"/>
<action selector="twentyFourClockFormatAction:" target="-2" id="oNg-Ux-azD"/>
</connections>
</button>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="7AB-yP-Tm5">
Expand Down Expand Up @@ -158,9 +159,23 @@ DQ
<box verticalHuggingPriority="750" boxType="separator" translatesAutoresizingMaskIntoConstraints="NO" id="jqU-bc-Phd">
<rect key="frame" x="16" y="95" width="528" height="5"/>
</box>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="t0E-5m-5Wc">
<rect key="frame" x="14" y="131" width="132" height="24"/>
<constraints>
<constraint firstAttribute="height" constant="20" id="1dL-3C-ZSj"/>
</constraints>
<buttonCell key="cell" type="check" title="Draw empty digits" bezelStyle="regularSquare" imagePosition="left" state="on" inset="2" id="ujV-7J-86w">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="drawEmptyDigitsAction:" target="-2" id="IyI-ab-FB1"/>
</connections>
</button>
</subviews>
<constraints>
<constraint firstItem="w0R-LT-WzY" firstAttribute="leading" secondItem="QPA-Ty-yka" secondAttribute="leading" id="3Am-Ol-5c9"/>
<constraint firstItem="t0E-5m-5Wc" firstAttribute="leading" secondItem="udT-lF-zKm" secondAttribute="leading" id="8QQ-dN-xGD"/>
<constraint firstItem="jqU-bc-Phd" firstAttribute="leading" secondItem="EiT-Mj-1SZ" secondAttribute="leading" constant="16" id="9ro-Pg-kPE"/>
<constraint firstAttribute="trailing" secondItem="Bnn-dY-5vI" secondAttribute="trailing" constant="16" id="ALb-Eb-HOP"/>
<constraint firstAttribute="bottom" secondItem="AJd-fC-cWE" secondAttribute="bottom" constant="16" id="DOa-tl-0rd"/>
Expand All @@ -170,6 +185,7 @@ DQ
<constraint firstItem="udT-lF-zKm" firstAttribute="top" secondItem="EiT-Mj-1SZ" secondAttribute="top" constant="16" id="MZn-Dj-AOl"/>
<constraint firstItem="udT-lF-zKm" firstAttribute="leading" secondItem="EiT-Mj-1SZ" secondAttribute="leading" constant="16" id="OEk-2C-3UQ"/>
<constraint firstAttribute="bottom" secondItem="eY6-hS-6X6" secondAttribute="bottom" constant="16" id="Oke-HN-RRc"/>
<constraint firstItem="t0E-5m-5Wc" firstAttribute="top" secondItem="7PC-sc-V9a" secondAttribute="bottom" constant="16" id="Onc-YA-uOL"/>
<constraint firstItem="PFI-UJ-nv7" firstAttribute="leading" secondItem="udT-lF-zKm" secondAttribute="trailing" constant="8" id="P3y-NY-qzt"/>
<constraint firstItem="7PC-sc-V9a" firstAttribute="leading" secondItem="udT-lF-zKm" secondAttribute="leading" id="TOq-M3-jlQ"/>
<constraint firstItem="wdp-Vx-SFy" firstAttribute="leading" secondItem="udT-lF-zKm" secondAttribute="leading" id="UQE-eG-ceo"/>
Expand All @@ -192,7 +208,7 @@ DQ
</window>
</objects>
<resources>
<image name="GitHub" width="429" height="256"/>
<image name="Twitter" width="429" height="256"/>
<image name="GitHub" width="858" height="512"/>
<image name="Twitter" width="858" height="512"/>
</resources>
</document>
9 changes: 5 additions & 4 deletions PredatorCore/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import Foundation

public protocol Configuration {
var primaryColor: NSColor { get }
var isReverse: Bool { get }
var isTwentyFourClock: Bool { get }
var isTwelveFourClock: Bool { get }
var isAutoInstallUpdates: Bool { get }
var reverse: Bool { get }
var twentyFourHours: Bool { get }
var twelveHours: Bool { get }
var autoUpdates: Bool { get }
var drawEmptyDigits: Bool { get }
}
50 changes: 26 additions & 24 deletions PredatorCore/PredatorClock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final public class PredatorClock {
var hours = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
// Converts time to 12-hour clock format
if configuration.isTwelveFourClock {
if configuration.twelveHours {
hours = hours > 12 ? hours - 12 : hours
hours = hours == 0 ? 12 : hours
}
Expand Down Expand Up @@ -64,7 +64,7 @@ final public class PredatorClock {
width: nummberSize.width,
height: nummberSize.height
), context: context, value: Int(minutes % 10))
if configuration.isTwelveFourClock {
if configuration.twelveHours {
drawClockFormat(in: CGRect(
x: center.x + nummberSize.width * 2.0,
y: center.y - nummberSize.height / 2.0,
Expand Down Expand Up @@ -96,7 +96,7 @@ final public class PredatorClock {
let p9 = CGPoint(x: p1.x + shape.width * 0.268, y: p6.y - shape.width * 0.713)
// Drawing
let isEnabled = { (value: Int, part: Int) -> Bool in
if self.configuration.isTwentyFourClock {
if self.configuration.twentyFourHours {
if value >= part {
return true
}
Expand Down Expand Up @@ -226,35 +226,37 @@ final public class PredatorClock {
// MARK: - Drawing

func drawPredatorLine(in context: CGContext, point: CGPoint, shape: CGSize, angle: CGFloat, enabled: Bool) {
var color = configuration.primaryColor.cgColor
let isFilling = configuration.isReverse == true ? !enabled : enabled
context.saveGState()
if isFilling == false {
color = color.copy(alpha: 0.3) ?? color
}
context.rotate(point, angle: angle)
context.setFillColor(color)
context.setStrokeColor(color.copy(alpha: 1.0) ?? color)
context.setShadow(offset: .zero, blur: 10.0, color: color)
context.addPath(self.buildPredatorShapePath(in: CGRect(
x: point.x,
y: point.y,
width: shape.width,
height: shape.height
)))
context.closePath()
context.fillPath()
if isFilling == false {
if configuration.drawEmptyDigits || enabled {
var color = configuration.primaryColor.cgColor
let reverse = configuration.reverse == true ? !enabled : enabled
context.saveGState()
if reverse == false {
color = color.copy(alpha: 0.3) ?? color
}
context.rotate(point, angle: angle)
context.setFillColor(color)
context.setStrokeColor(color.copy(alpha: 1.0) ?? color)
context.setShadow(offset: .zero, blur: 10.0, color: color)
context.addPath(self.buildPredatorShapePath(in: CGRect(
x: point.x,
y: point.y,
width: shape.width,
height: shape.height
)))
context.closePath()
context.strokePath()
context.fillPath()
if reverse == false {
context.addPath(self.buildPredatorShapePath(in: CGRect(
x: point.x,
y: point.y,
width: shape.width,
height: shape.height
)))
context.closePath()
context.strokePath()
}
context.restoreGState()
}
context.restoreGState()
}

// MARK: - Number Paths
Expand Down
Loading

1 comment on commit 30aae0b

@corybrewis
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome! 👍

Please sign in to comment.