Thou shalt not cross 80 columns in thy file.

This commit is contained in:
2025-03-20 13:44:11 -07:00
parent c846fc5a15
commit c1056fad93
14 changed files with 665 additions and 297 deletions
+247 -105
View File
@@ -16,6 +16,9 @@ class CmdViewController: NSViewController {
private var timer: DispatchSourceTimer?
private var keyboardEvents: EventMonitor?
private var maxWidthContraint: NSLayoutConstraint!
private var maxHeightContraint: NSLayoutConstraint!
private var visualEffectView: NSView = {
let visualEffect = NSVisualEffectView()
visualEffect.blendingMode = .behindWindow
@@ -38,14 +41,20 @@ class CmdViewController: NSViewController {
private var textLabel: NSTextField = {
let textField = NSTextField()
textField.backgroundColor = .clear // NOTE: Setting drawsBackground = false messes with geometry. Also, setting a custom cell, messes up, too.
// NOTE: Setting drawsBackground = false messes with geometry.
// Also, setting a custom cell, messes up, too.
textField.backgroundColor = .clear
textField.isSelectable = true
textField.isHidden = false
textField.isEditable = false
textField.isBezeled = false
textField.alignment = .left
textField.textColor = NSColor(name: nil) { getColors(.black, .white, for: $0) }
textField.font = NSFont.monospacedSystemFont(ofSize: NSFontDescriptor.preferredFontDescriptor(forTextStyle: .body).pointSize, weight: .regular)
textField.textColor =
NSColor(name: nil) { getColors(.black, .white, for: $0) }
textField.font = NSFont
.monospacedSystemFont(ofSize: NSFontDescriptor
.preferredFontDescriptor(forTextStyle: .body).pointSize,
weight: .regular)
textField.translatesAutoresizingMaskIntoConstraints = false
return textField
}()
@@ -60,8 +69,12 @@ class CmdViewController: NSViewController {
textField.isBezeled = false
textField.drawsBackground = false
textField.alignment = .left
textField.textColor = NSColor(name: nil) { getColors(.darkGray, .lightGray, for: $0) }
textField.font = NSFont.systemFont(ofSize: NSFontDescriptor.preferredFontDescriptor(forTextStyle: .subheadline).pointSize, weight: .bold)
textField.textColor =
NSColor(name: nil) { getColors(.darkGray, .lightGray, for: $0) }
textField.font = NSFont
.systemFont(ofSize: NSFontDescriptor
.preferredFontDescriptor(forTextStyle: .subheadline).pointSize,
weight: .bold)
textField.translatesAutoresizingMaskIntoConstraints = false
return textField
}()
@@ -80,15 +93,19 @@ class CmdViewController: NSViewController {
textField.isBezeled = false
textField.drawsBackground = false
textField.alignment = .center
textField.textColor = NSColor(name: nil) { getColors(.darkGray, .lightGray, for: $0) }
textField.font = NSFont(descriptor: NSFontDescriptor.preferredFontDescriptor(forTextStyle: .body), size: 0)
textField.textColor =
NSColor(name: nil) { getColors(.darkGray, .lightGray, for: $0) }
textField.font =
NSFont(descriptor: NSFontDescriptor
.preferredFontDescriptor(forTextStyle: .body), size: 0)
textField.translatesAutoresizingMaskIntoConstraints = false
return textField
}()
private var quitButton: NSButton = {
let button = NSButton()
button.image = systemImage("xmark.circle.fill", .title2, .large, .init(paletteColors: [.white, .systemRed]))
button.image = systemImage("xmark.circle.fill", .title2, .large,
.init(paletteColors: [.white, .systemRed]))
button.isBordered = false
button.action = #selector(terminateApp)
button.sizeToFit()
@@ -99,7 +116,9 @@ class CmdViewController: NSViewController {
private var reloadButton: NSButton = {
let button = NSButton()
button.image = systemImage("arrow.clockwise.circle.fill", .title2, .large, .init(paletteColors: [.white, .systemBlue]))
button.image = systemImage("arrow.clockwise.circle.fill", .title2,
.large,
.init(paletteColors: [.white, .systemBlue]))
button.isBordered = false
button.action = #selector(reloadWidget)
button.sizeToFit()
@@ -110,7 +129,8 @@ class CmdViewController: NSViewController {
private var startButton: NSButton = {
let button = NSButton()
button.image = systemImage("sun.max.circle.fill", .title2, .large, .init(paletteColors: [.white, .systemOrange]))
button.image = systemImage("sun.max.circle.fill", .title2, .large,
.init(paletteColors: [.white, .systemOrange]))
button.isBordered = false
button.action = #selector(openAtLogin)
button.sizeToFit()
@@ -121,7 +141,8 @@ class CmdViewController: NSViewController {
private var aboutButton: NSButton = {
let button = NSButton()
button.image = systemImage("info.circle.fill", .title2, .large, .init(paletteColors: [.white, .systemGray]))
button.image = systemImage("info.circle.fill", .title2, .large,
.init(paletteColors: [.white, .systemGray]))
button.isBordered = false
button.action = #selector(showAbout)
button.sizeToFit()
@@ -132,7 +153,12 @@ class CmdViewController: NSViewController {
private var updateButton: NSButton = {
let button = NSButton()
button.image = NSImage(systemSymbolName: "arrow.down.circle.fill", accessibilityDescription: nil)?.withSymbolConfiguration(NSImage.SymbolConfiguration(textStyle: .title2, scale: .large) .applying(.init(paletteColors: [.white, .systemGreen])))
// TODO: WHY??!!!
button.image = NSImage(systemSymbolName: "arrow.down.circle.fill",
accessibilityDescription: nil)?
.withSymbolConfiguration(NSImage
.SymbolConfiguration(textStyle: .title2, scale: .large)
.applying(.init(paletteColors: [.white, .systemGreen])))
button.isBordered = false
button.action = #selector(updateApp)
button.sizeToFit()
@@ -148,7 +174,8 @@ class CmdViewController: NSViewController {
blurView.material = .sheet
blurView.state = .active
blurView.wantsLayer = true
blurView.layer?.borderColor = NSColor.systemGray.withAlphaComponent(0.05).cgColor
blurView.layer?.borderColor =
NSColor.systemGray.withAlphaComponent(0.05).cgColor
blurView.layer?.borderWidth = 1.5
blurView.translatesAutoresizingMaskIntoConstraints = false
return blurView
@@ -190,9 +217,19 @@ class CmdViewController: NSViewController {
self.view = QuietView()
}
override func viewWillAppear() {
maxWidthContraint.constant =
NSScreen.main!.visibleFrame.size.width * 0.7
maxHeightContraint.constant =
NSScreen.main!.visibleFrame.size.height * 0.8
}
override func viewDidAppear() {
super.viewDidAppear()
// TODO: Check so that there are weird behaviors on the
textLabelScrollView.flashScrollers()
startReloadTextTimer()
keyboardEvents?.start()
@@ -207,7 +244,8 @@ class CmdViewController: NSViewController {
keyboardEvents?.stop()
if let editor = textLabel.currentEditor() { textLabel.endEditing(editor) }
if let editor = textLabel.currentEditor()
{ textLabel.endEditing(editor) }
}
private func setContraints() {
@@ -220,7 +258,9 @@ class CmdViewController: NSViewController {
func setup() {
openAtLoginToggle()
buttonsBackground.layer?.cornerRadius = quitButton.bounds.height / Metrics.buttonSpacing + Metrics.buttonSpacing
buttonsBackground.layer?.cornerRadius =
quitButton.bounds.height / Metrics.buttonSpacing +
Metrics.buttonSpacing
setupKeyEvents()
}
@@ -230,110 +270,198 @@ class CmdViewController: NSViewController {
progressView.widthAnchor.constraint(equalToConstant: 16),
progressView.heightAnchor.constraint(equalToConstant: 16),
progressView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: Metrics.padding),
progressView.centerYAnchor.constraint(equalTo: reloadsText.centerYAnchor),
progressView.leadingAnchor
.constraint(equalTo: view.leadingAnchor,
constant: Metrics.padding),
progressView.centerYAnchor
.constraint(equalTo: reloadsText.centerYAnchor),
])
NSLayoutConstraint.activate([
reloadsText.leadingAnchor.constraint(equalTo: progressView.trailingAnchor, constant: Metrics.buttonSpacing),
reloadsText.centerYAnchor.constraint(equalTo: buttonsBackground.centerYAnchor),
reloadsText.trailingAnchor.constraint(equalTo: buttonsBackground.leadingAnchor, constant: -Metrics.buttonSpacing),
reloadsText.leadingAnchor
.constraint(equalTo: progressView.trailingAnchor,
constant: Metrics.buttonSpacing),
reloadsText.centerYAnchor
.constraint(equalTo: buttonsBackground.centerYAnchor),
reloadsText.trailingAnchor
.constraint(equalTo: buttonsBackground.leadingAnchor,
constant: -Metrics.buttonSpacing),
])
}
private func setViewConstraints() {
NSLayoutConstraint.activate([
visualEffectView.topAnchor.constraint(equalTo: view.topAnchor),
visualEffectView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
visualEffectView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
visualEffectView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
visualEffectView.bottomAnchor
.constraint(equalTo: view.bottomAnchor),
visualEffectView.leadingAnchor
.constraint(equalTo: view.leadingAnchor),
visualEffectView.trailingAnchor
.constraint(equalTo: view.trailingAnchor),
])
}
private func setButtonConstraints() {
NSLayoutConstraint.activate([
quitButton.widthAnchor.constraint(equalToConstant: quitButton.bounds.width),
quitButton.heightAnchor.constraint(equalToConstant: quitButton.bounds.height),
reloadButton.widthAnchor.constraint(equalToConstant: reloadButton.bounds.width),
reloadButton.heightAnchor.constraint(equalToConstant: reloadButton.bounds.height),
startButton.widthAnchor.constraint(equalToConstant: startButton.bounds.width),
startButton.heightAnchor.constraint(equalToConstant: startButton.bounds.height),
aboutButton.widthAnchor.constraint(equalToConstant: aboutButton.bounds.width),
aboutButton.heightAnchor.constraint(equalToConstant: aboutButton.bounds.height),
updateButton.widthAnchor.constraint(equalToConstant: updateButton.bounds.width),
updateButton.heightAnchor.constraint(equalToConstant: updateButton.bounds.height),
quitButton.widthAnchor
.constraint(equalToConstant: quitButton.bounds.width),
quitButton.heightAnchor
.constraint(equalToConstant: quitButton.bounds.height),
reloadButton.widthAnchor
.constraint(equalToConstant: reloadButton.bounds.width),
reloadButton.heightAnchor
.constraint(equalToConstant: reloadButton.bounds.height),
startButton.widthAnchor
.constraint(equalToConstant: startButton.bounds.width),
startButton.heightAnchor
.constraint(equalToConstant: startButton.bounds.height),
aboutButton.widthAnchor
.constraint(equalToConstant: aboutButton.bounds.width),
aboutButton.heightAnchor
.constraint(equalToConstant: aboutButton.bounds.height),
updateButton.widthAnchor
.constraint(equalToConstant: updateButton.bounds.width),
updateButton.heightAnchor
.constraint(equalToConstant: updateButton.bounds.height),
buttonsContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -Metrics.padding),
buttonsContainer.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -Metrics.padding),
buttonsContainer.trailingAnchor
.constraint(equalTo: view.trailingAnchor,
constant: -Metrics.padding),
buttonsContainer.bottomAnchor
.constraint(equalTo: view.bottomAnchor,
constant: -Metrics.padding),
buttonsBackground.leadingAnchor.constraint(equalTo: buttonsContainer.leadingAnchor),
buttonsBackground.trailingAnchor.constraint(equalTo: buttonsContainer.trailingAnchor),
buttonsBackground.topAnchor.constraint(equalTo: buttonsContainer.topAnchor),
buttonsBackground.bottomAnchor.constraint(equalTo: buttonsContainer.bottomAnchor),
buttonsBackground.leadingAnchor
.constraint(equalTo: buttonsContainer.leadingAnchor),
buttonsBackground.trailingAnchor
.constraint(equalTo: buttonsContainer.trailingAnchor),
buttonsBackground.topAnchor
.constraint(equalTo: buttonsContainer.topAnchor),
buttonsBackground.bottomAnchor
.constraint(equalTo: buttonsContainer.bottomAnchor),
quitButton.trailingAnchor.constraint(equalTo: buttonsContainer.trailingAnchor, constant: -Metrics.buttonSpacing),
quitButton.topAnchor.constraint(equalTo: buttonsContainer.topAnchor, constant: Metrics.buttonSpacing),
quitButton.bottomAnchor.constraint(equalTo: buttonsContainer.bottomAnchor, constant: -Metrics.buttonSpacing),
quitButton.trailingAnchor
.constraint(equalTo: buttonsContainer.trailingAnchor,
constant: -Metrics.buttonSpacing),
quitButton.topAnchor
.constraint(equalTo: buttonsContainer.topAnchor,
constant: Metrics.buttonSpacing),
quitButton.bottomAnchor
.constraint(equalTo: buttonsContainer.bottomAnchor,
constant: -Metrics.buttonSpacing),
reloadButton.firstBaselineAnchor.constraint(equalTo: quitButton.firstBaselineAnchor),
reloadButton.trailingAnchor.constraint(equalTo: quitButton.leadingAnchor, constant: -Metrics.buttonSpacing),
reloadButton.firstBaselineAnchor
.constraint(equalTo: quitButton.firstBaselineAnchor),
reloadButton.trailingAnchor
.constraint(equalTo: quitButton.leadingAnchor,
constant: -Metrics.buttonSpacing),
startButton.firstBaselineAnchor.constraint(equalTo: reloadButton.firstBaselineAnchor),
startButton.trailingAnchor.constraint(equalTo: reloadButton.leadingAnchor, constant: -Metrics.buttonSpacing),
startButton.firstBaselineAnchor
.constraint(equalTo: reloadButton.firstBaselineAnchor),
startButton.trailingAnchor
.constraint(equalTo: reloadButton.leadingAnchor,
constant: -Metrics.buttonSpacing),
aboutButton.firstBaselineAnchor.constraint(equalTo: startButton.firstBaselineAnchor),
aboutButton.trailingAnchor.constraint(equalTo: startButton.leadingAnchor, constant: -Metrics.buttonSpacing),
aboutButton.firstBaselineAnchor
.constraint(equalTo: startButton.firstBaselineAnchor),
aboutButton.trailingAnchor
.constraint(equalTo: startButton.leadingAnchor,
constant: -Metrics.buttonSpacing),
updateButton.firstBaselineAnchor.constraint(equalTo: startButton.firstBaselineAnchor),
updateButton.leadingAnchor.constraint(equalTo: buttonsContainer.leadingAnchor, constant: Metrics.buttonSpacing),
updateButton.trailingAnchor.constraint(equalTo: aboutButton.leadingAnchor, constant: -Metrics.buttonSpacing),
updateButton.firstBaselineAnchor
.constraint(equalTo: startButton.firstBaselineAnchor),
updateButton.leadingAnchor
.constraint(equalTo: buttonsContainer.leadingAnchor,
constant: Metrics.buttonSpacing),
updateButton.trailingAnchor
.constraint(equalTo: aboutButton.leadingAnchor,
constant: -Metrics.buttonSpacing),
])
}
private func setTextConstraints() {
NSLayoutConstraint.activate([
statusText.centerXAnchor.constraint(equalTo: view.centerXAnchor),
statusText.centerYAnchor.constraint(equalTo: view.centerYAnchor),
statusText.centerXAnchor
.constraint(equalTo: view.centerXAnchor),
statusText
.centerYAnchor.constraint(equalTo: view.centerYAnchor),
])
let trailing = textLabel.trailingAnchor.constraint(equalTo: textLabelScrollView.trailingAnchor)
let trailing =
textLabel.trailingAnchor
.constraint(equalTo: textLabelScrollView.trailingAnchor)
trailing.priority = .init(600)
let bottom = textLabel.bottomAnchor.constraint(equalTo: textLabelScrollView.bottomAnchor)
let bottom =
textLabel.bottomAnchor
.constraint(equalTo: textLabelScrollView.bottomAnchor)
bottom.priority = .init(600)
maxWidthContraint =
textLabelScrollView.widthAnchor
.constraint(lessThanOrEqualToConstant: Metrics.contentMaxWidth)
maxHeightContraint =
textLabelScrollView.heightAnchor
.constraint(lessThanOrEqualToConstant: Metrics.contentMaxHeight)
textLabel.setContentHuggingPriority(.required, for: .horizontal)
NSLayoutConstraint.activate([
textLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: Metrics.contentMinWidth),
textLabelScrollView.widthAnchor.constraint(lessThanOrEqualToConstant: Metrics.contentMaxWidth),
textLabel.widthAnchor
.constraint(
greaterThanOrEqualToConstant: Metrics.contentMinWidth
),
// textLabelScrollView.widthAnchor
// .constraint(
// lessThanOrEqualToConstant: Metrics.contentMaxWidth
// ),
maxWidthContraint,
textLabel.heightAnchor.constraint(greaterThanOrEqualToConstant: Metrics.contentMinHeight),
textLabelScrollView.heightAnchor.constraint(lessThanOrEqualToConstant: Metrics.contentMaxHeight),
textLabel.heightAnchor
.constraint(
greaterThanOrEqualToConstant: Metrics.contentMinHeight
),
// textLabelScrollView.heightAnchor
// .constraint(
// lessThanOrEqualToConstant: Metrics.contentMaxHeight
// ),
maxHeightContraint,
textLabel.topAnchor.constraint(equalTo: textLabelScrollView.topAnchor),
textLabel.topAnchor
.constraint(equalTo: textLabelScrollView.topAnchor),
bottom,
textLabel.leadingAnchor.constraint(equalTo: textLabelScrollView.leadingAnchor),
textLabel.leadingAnchor
.constraint(equalTo: textLabelScrollView.leadingAnchor),
trailing,
textLabelScrollView.topAnchor.constraint(equalTo: view.topAnchor, constant: 10),
textLabelScrollView.bottomAnchor.constraint(equalTo: buttonsContainer.topAnchor, constant: -10),
textLabelScrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 10),
textLabelScrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
textLabelScrollView.topAnchor
.constraint(equalTo: view.topAnchor, constant: 10),
textLabelScrollView.bottomAnchor
.constraint(equalTo: buttonsContainer.topAnchor,
constant: -10),
textLabelScrollView.leadingAnchor
.constraint(equalTo: view.leadingAnchor, constant: 10),
textLabelScrollView.trailingAnchor
.constraint(equalTo: view.trailingAnchor, constant: -10),
])
}
private func resetReloadButton() {
reloadButton.image = systemImage("arrow.clockwise.circle.fill", .title2, .large, .init(paletteColors: [.white, .systemBlue]))
reloadButton.image =
systemImage("arrow.clockwise.circle.fill", .title2, .large,
.init(paletteColors: [.white, .systemBlue]))
reloadButton.action = #selector(reloadWidget)
reloadButton.toolTip = "Reload Current"
}
private func startReloadTextTimer() {
timer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
timer =
DispatchSource.makeTimerSource(queue: DispatchQueue.global())
timer?.schedule(deadline: .now(), repeating: .milliseconds(500))
timer?.setEventHandler { [weak self] in
DispatchQueue.main.async {
guard let controller = self, let file = controller.cmdFile else { return }
guard let controller = self,
let file = controller.cmdFile
else { return }
controller.setUntil("\(stringifySeconds(file.untilNextExec))")
controller.progressView.value = Double(file.untilNextExec)
}
@@ -342,45 +470,55 @@ class CmdViewController: NSViewController {
}
private func setupKeyEvents() {
keyboardEvents = LocalEventMonitor(mask: [.flagsChanged, .keyDown], handler: { [weak self] event in
let modifiers = event.modifierFlags.rawValue
let key = event.keyCode
keyboardEvents = LocalEventMonitor(mask: [.flagsChanged, .keyDown],
handler: { [weak self] event in
let modifiers = event.modifierFlags.rawValue
let key = event.keyCode
if modsContains(keys: OSShift, in: modifiers) {
self?.reloadButton.image = systemImage("arrow.clockwise.circle.fill", .title2, .large,.init(paletteColors: [.white, .systemCyan]))
self?.reloadButton.action = #selector(self?.reloadWidgets)
self?.reloadButton.toolTip = "Reload All"
} else {
self?.reloadButton.image = systemImage("arrow.clockwise.circle.fill", .title2, .large, .init(paletteColors: [.white, .systemBlue]))
self?.reloadButton.action = #selector(self?.reloadWidget)
self?.reloadButton.toolTip = "Reload Current"
}
if modsContains(keys: OSShift, in: modifiers) {
self?.reloadButton.image =
systemImage("arrow.clockwise.circle.fill", .title2,
.large,
.init(paletteColors: [.white, .systemCyan]))
self?.reloadButton.action =
#selector(self?.reloadWidgets)
self?.reloadButton.toolTip = "Reload All"
} else {
self?.reloadButton.image =
systemImage("arrow.clockwise.circle.fill", .title2,
.large,
.init(paletteColors: [.white, .systemBlue]))
self?.reloadButton.action =
#selector(self?.reloadWidget)
self?.reloadButton.toolTip = "Reload Current"
}
if modsContains(keys: OSCmd, in: modifiers) {
if key == kVK_ANSI_Q {
self?.terminateApp()
} else if key == kVK_ANSI_W {
self?.view.superview?.window?.resignKey()
} else if key == kVK_ANSI_R {
self?.reloadWidget()
} else if key == kVK_ANSI_C {
self?.textLabel.currentEditor()?.copy(nil)
if modsContains(keys: OSCmd, in: modifiers) {
if key == kVK_ANSI_Q {
self?.terminateApp()
} else if key == kVK_ANSI_W {
self?.view.superview?.window?.resignKey()
} else if key == kVK_ANSI_R {
self?.reloadWidget()
} else if key == kVK_ANSI_C {
self?.textLabel.currentEditor()?.copy(nil)
}
} else
if modsContains(keys: OSCmd | OSShift, in: modifiers) {
if key == kVK_ANSI_R {
// NOTE: We need to resign the window in order to prevent the program from intercepting the events, which result in sounds
// beign made for missing performKeyEquivalent.
self?.view.window?.resignKey()
self?.reloadWidgets()
}
} else if modsContainsNone(in: modifiers) {
if key == kVK_ANSI_Q || key == kVK_Escape {
self?.view.superview?.window?.resignKey()
}
}
} else if modsContains(keys: OSCmd | OSShift, in: modifiers) {
if key == kVK_ANSI_R {
// NOTE: We need to resign the window in order to prevent the program from intercepting the events, which result in sounds
// beign made for missing performKeyEquivalent.
self?.view.window?.resignKey()
self?.reloadWidgets()
}
} else if modsContainsNone(in: modifiers) {
if key == kVK_ANSI_Q || key == kVK_Escape {
self?.view.superview?.window?.resignKey()
}
}
return event
})
return event
})
}
func setFile(_ file: CmdFile) {
@@ -438,10 +576,14 @@ class CmdViewController: NSViewController {
private func openAtLoginToggle() {
if SMAppService.mainApp.status == .enabled {
startButton.toolTip = "Open at Login — ON"
startButton.image = systemImage("sunrise.circle.fill", .title2, .large, .init(paletteColors: [.white, .systemOrange]))
startButton.image =
systemImage("sunrise.circle.fill", .title2, .large,
.init(paletteColors: [.white, .systemOrange]))
} else {
startButton.toolTip = "Open at Login — OFF"
startButton.image = systemImage("sun.max.circle.fill", .title2, .large, .init(paletteColors: [.white, .systemOrange]))
startButton.image =
systemImage("sun.max.circle.fill", .title2, .large,
.init(paletteColors: [.white, .systemOrange]))
}
}