Skip to content

Instantly share code, notes, and snippets.

@rubysolo
Forked from blackgear/clock.swift
Last active August 10, 2024 00:14
Show Gist options
  • Save rubysolo/aca1fb705c12082c64b15b3bcab80bbc to your computer and use it in GitHub Desktop.
Save rubysolo/aca1fb705c12082c64b15b3bcab80bbc to your computer and use it in GitHub Desktop.
A simple floating clock for OS X
// The MIT License
// Copyright (c) 2018 Daniel
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// How to build:
// $ swiftc -o clock -gnone -O -target x86_64-apple-macosx10.14 clock.swift
// How to run:
// $ ./clock
import Cocoa
class DraggableView: NSView {
var initialLocation: NSPoint?
override func mouseDown(with event: NSEvent) {
initialLocation = event.locationInWindow
}
override func mouseDragged(with event: NSEvent) {
guard let initialLocation = initialLocation else { return }
let currentLocation = event.locationInWindow
let offset = NSPoint(x: currentLocation.x - initialLocation.x, y: currentLocation.y - initialLocation.y)
guard let window = self.window else { return }
let newOrigin = NSPoint(x: window.frame.origin.x + offset.x, y: window.frame.origin.y + offset.y)
window.setFrameOrigin(newOrigin)
}
}
class Clock: NSObject, NSApplicationDelegate {
var window : NSWindow?
func applicationDidFinishLaunching(_ aNotification: Notification) {
let containerView = NSView(frame: NSRect(x: 0, y: 0, width: 170, height: 69))
let dateLabel = self.initLabel(
font : NSFont.monospacedDigitSystemFont(ofSize: 18, weight: .regular),
format : "YYYY-MM-dd",
interval : 10
)
containerView.addSubview(dateLabel)
dateLabel.frame = NSRect(x: 0, y: 46, width: 170, height: 23)
let timeLabel = self.initLabel(
font : NSFont.monospacedDigitSystemFont(ofSize: 36, weight: .regular),
format : "HH:mm:ss",
interval : 1
)
containerView.addSubview(timeLabel)
timeLabel.frame = NSRect(x: 0, y: 0, width: 170, height: 46)
self.window = self.initWindow(
rect: NSMakeRect(1145, 428, 170, 69),
containerView: containerView
)
}
func initLabel(font: NSFont, format: String, interval: TimeInterval) -> NSTextField {
let formatter = DateFormatter()
formatter.dateFormat = format
let label = NSTextField()
label.font = font
label.isBezeled = false
label.isEditable = false
label.drawsBackground = false
label.alignment = .center
label.textColor = NSColor(red: 1, green: 1, blue: 1, alpha: 1-(1/3)*(1/3))
let timer = Timer.scheduledTimer(withTimeInterval: interval, repeats: true) { _ in
label.stringValue = formatter.string(from: Date())
}
timer.tolerance = interval / 10
timer.fire()
return label
}
func initWindow(rect: NSRect, containerView: NSView) -> NSWindow {
let window = NSWindow(
contentRect : rect,
styleMask : .borderless,
backing : .buffered,
defer : true
)
let contentView = DraggableView()
contentView.frame = rect
window.contentView = contentView
contentView.addSubview(containerView)
window.ignoresMouseEvents = false
window.level = .floating
window.collectionBehavior = .canJoinAllSpaces
window.backgroundColor = NSColor(red: 0, green: 0, blue: 0, alpha: 1/3)
window.orderFrontRegardless()
return window
}
}
let app = NSApplication.shared
let clock = Clock()
app.delegate = clock
app.setActivationPolicy(.accessory)
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment