Skip to content

Instantly share code, notes, and snippets.

@jdahlin
Last active July 8, 2022 20:57
Show Gist options
  • Save jdahlin/fb16c6f292bf27226db09e3b1c02024f to your computer and use it in GitHub Desktop.
Save jdahlin/fb16c6f292bf27226db09e3b1c02024f to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// storyboardtest2
//
// Created by Johan Dahlin on 2022-07-08.
//
import Cocoa
// https://developer.apple.com/documentation/appkit/nsviewcontroller
class ViewController: NSViewController {
var dateformat = DateFormatter()
var currentDate = ""
@IBOutlet weak var mainLabel: NSTextField!
// https://developer.apple.com/documentation/appkit/nsviewcontroller/1434476-viewdidload
override func viewDidLoad() {
super.viewDidLoad()
// https://unicode-org.github.io/icu/userguide/format_parse/datetime/#datetime-format-syntax
self.dateformat.dateFormat = "HH:mm" // 12h as "hh:mm a"
self.timerInitialize()
self.textFieldInitialize()
}
// https://developer.apple.com/documentation/appkit/nsviewcontroller/1434415-viewwillappear
override func viewWillAppear() {
self.mainWindowInitialize(window: view.window!)
super.viewWillAppear()
}
// https://developer.apple.com/documentation/appkit/nsevent/1532495-mouseevent
override public func mouseDown(with event: NSEvent) {
// Dragging should move the window, forward events to the window
event.window?.performDrag(with: event)
}
private func mainWindowInitialize(window: NSWindow) {
// Do not render background, leave it transparent
// https://developer.apple.com/documentation/appkit/nswindow/1419751-backgroundcolor
// https://developer.apple.com/documentation/appkit/nscolor/1527217-clear
window.backgroundColor = .clear
// Make the window appear on all workspaces
// https://developer.apple.com/documentation/appkit/nswindow/collectionbehavior
window.collectionBehavior = [.canJoinAllSpaces]
// Ensure the window is always on top, clicking on another application will
// not cause it to be hidden.
// https://developer.apple.com/documentation/appkit/nswindow/level
window.level = .floating
// Do not render window title and decoration
// https://developer.apple.com/documentation/appkit/nswindow/stylemask/1644698-borderless
window.styleMask = .borderless
}
private func textFieldInitialize() {
self.mainLabel.font = NSFont.systemFont(ofSize: 80)
self.mainLabel.textColor = NSColor.systemGreen
}
private func timerInitialize() {
// Every second dispatch a timer, this means the clock is at most 1s out of sync.
// FIXME: Schedule a timer exactly when each minute changes
// https://developer.apple.com/documentation/foundation/timer/2091889-scheduledtimer
let _ = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
// Has the minute changed since last time
let date = self.dateformat.string(from: Date())
if (date != self.currentDate) {
self.textFieldUpdate(date: date)
}
}
}
private func textFieldUpdate(date: String) {
// Update the label with current time of the day, e.g: `12:34`
self.mainLabel.stringValue = date
// https://developer.apple.com/documentation/appkit/nsview/1483360-needsdisplay
self.mainLabel.needsDisplay = true
self.currentDate = date
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment