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

Option to disable appearance change on focus change #126

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion Timer/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele

func applicationDidFinishLaunching(_ aNotification: Notification) {
let controller = MVTimerController()
controller.isMainController = true
controllers.append(controller)
self.addBadgeToDock(controller: controller)

Expand Down Expand Up @@ -81,6 +82,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
@objc func newDocument(_ sender: AnyObject?) {
let controller = MVTimerController(closeToWindow: NSApplication.shared.keyWindow)
controller.window?.level = self.windowLevel()
controller.isMainController = controllers.isEmpty
controllers.append(controller)
}

Expand Down Expand Up @@ -109,6 +111,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
}

private func registerDefaults() {
UserDefaults.standard.register(defaults: [MVUserDefaultsKeys.staysOnTop: false])
UserDefaults.standard.register(defaults: [
MVUserDefaultsKeys.staysOnTop: false,
MVUserDefaultsKeys.appearanceChangeOnFocusChange: true
])
}
}
16 changes: 14 additions & 2 deletions Timer/MVClockView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ class MVClockView: NSControl {
self.updateClockFaceView()
}

func appearanceChangeOnFocusChange(_ change: Bool) {
clockFaceView.appearanceChangeOnFocus = change
arrowView.appearanceChangeOnFocus = change
progressView.appearanceChangeOnFocus = change
self.needsDisplay = true
}

private func updateClockFaceView(highlighted: Bool = false) {
clockFaceView.update(highlighted: highlighted)
}
Expand Down Expand Up @@ -526,6 +533,7 @@ class MVClockProgressView: NSView {
self.needsDisplay = true
}
}
var appearanceChangeOnFocus: Bool = true

convenience init() {
self.init(frame: NSRect(x: 0, y: 0, width: 116, height: 116))
Expand Down Expand Up @@ -583,7 +591,7 @@ class MVClockProgressView: NSView {
transform.translate(x: -center.x, y: -center.y)
(transform as NSAffineTransform).concat()

let image = NSImage(named: windowHasFocus ? "progress" : "progress-unfocus")
let image = NSImage(named: windowHasFocus || !appearanceChangeOnFocus ? "progress" : "progress-unfocus")
image?.draw(in: self.bounds)

ctx?.restoreGraphicsState()
Expand All @@ -601,6 +609,7 @@ class MVClockArrowView: NSControl {
}
}
var actionMouseUp: Selector?
var appearanceChangeOnFocus: Bool = true
private var center = CGPoint.zero

convenience init(center: CGPoint) {
Expand Down Expand Up @@ -647,7 +656,7 @@ class MVClockArrowView: NSControl {
path.transform(using: transform)

let windowHasFocus = self.window?.isKeyWindow ?? false
if windowHasFocus {
if windowHasFocus || !appearanceChangeOnFocus {
let ratio: CGFloat = 0.5
NSColor(
srgbRed: 0.1734 + ratio * (0.2235 - 0.1734),
Expand Down Expand Up @@ -737,13 +746,16 @@ class MVClockArrowView: NSControl {

class MVClockFaceView: NSView {
private var _image: NSImage?
var appearanceChangeOnFocus: Bool = true

func update(highlighted: Bool = false) {
// Load the appropriate image for the clock face
let imageName: String

if highlighted {
imageName = "clock-highlighted"
} else if !appearanceChangeOnFocus {
imageName = "clock"
} else {
let windowHasFocus = self.window?.isKeyWindow ?? false
imageName = windowHasFocus ? "clock" : "clock-unfocus"
Expand Down
30 changes: 30 additions & 0 deletions Timer/MVMainView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class MVMainView: NSView {
private var contextMenu: NSMenu?
public var menuItem: NSMenuItem?
private var soundMenuItems: [NSMenuItem] = []
var appearanceChangeOnFocusMenuItem: NSMenuItem?

// swiftlint:disable unused_setter_value
override var menu: NSMenu? {
Expand Down Expand Up @@ -52,9 +53,27 @@ class MVMainView: NSView {
submenu.addItem(soundItem)
}
self.soundMenuItems.first?.state = .on

let menuItemViewConfig = NSMenuItem(
title: "View",
action: nil,
keyEquivalent: ""
)
let submenuViewConfig = NSMenu()
submenuViewConfig.autoenablesItems = false

appearanceChangeOnFocusMenuItem = NSMenuItem(
title: "Change appearance when not in focus",
action: #selector(self.toggleViewItemState),
keyEquivalent: ""
)
submenuViewConfig.addItem(appearanceChangeOnFocusMenuItem!)

self.contextMenu?.addItem(menuItem!)
self.contextMenu?.addItem(menuItemSoundChoice)
self.contextMenu?.setSubmenu(submenu, for: menuItemSoundChoice)
self.contextMenu?.addItem(menuItemViewConfig)
self.contextMenu?.setSubmenu(submenuViewConfig, for: menuItemViewConfig)

let notificationCenter = NotificationCenter.default

Expand Down Expand Up @@ -102,6 +121,17 @@ class MVMainView: NSView {
}
}

@objc func toggleViewItemState(_ sender: NSMenuItem) {
var value = sender.state == .on ? true : false
value.toggle()
switch sender {
case appearanceChangeOnFocusMenuItem:
self.controller?.setViewState(value, forKey: MVUserDefaultsKeys.appearanceChangeOnFocusChange)
default:
break
}
}

deinit {
NotificationCenter.default.removeObserver(self)
}
Expand Down
32 changes: 32 additions & 0 deletions Timer/MVTimerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class MVTimerController: NSWindowController {
private var audioPlayer: AVAudioPlayer? // player must be kept in memory
private var soundURL = Bundle.main.url(forResource: "alert-sound", withExtension: "caf")

var isMainController: Bool = false

convenience init() {
let mainView = MVMainView(frame: NSRect.zero)

Expand All @@ -25,6 +27,8 @@ class MVTimerController: NSWindowController {
self.windowFrameAutosaveName = "TimerWindowAutosaveFrame"

window.makeKeyAndOrderFront(self)

loadViewStateFromUserDefaults()
}

convenience init(closeToWindow: NSWindow?) {
Expand Down Expand Up @@ -105,4 +109,32 @@ class MVTimerController: NSWindowController {
self.soundURL = nil
}
}

func setViewState(_ value: Bool, forKey viewConfigKey: String) {
setViewState(value, forKey: viewConfigKey, save: isMainController)
}

private func setViewState(_ value: Bool, forKey viewConfigKey: String, save: Bool) {
let state: NSControl.StateValue = value ? .on : .off
switch viewConfigKey {
case MVUserDefaultsKeys.appearanceChangeOnFocusChange:
mainView.appearanceChangeOnFocusMenuItem?.state = state
clockView.appearanceChangeOnFocusChange(value)
default:
break
}
if save {
UserDefaults.standard.set(value, forKey: viewConfigKey)
}
}

private func loadViewStateFromUserDefaults() {
let keys: [String] = [
MVUserDefaultsKeys.appearanceChangeOnFocusChange
]
for key in keys {
let value = UserDefaults.standard.bool(forKey: key)
setViewState(value, forKey: key, save: false)
}
}
}
1 change: 1 addition & 0 deletions Timer/MVUserDefaultsKeys.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
struct MVUserDefaultsKeys {
static let staysOnTop = "staysOnTop"
static let appearanceChangeOnFocusChange = "appearanceChangeOnFocusChange"
}