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

View File

@@ -40,7 +40,8 @@ final class CBMenuBarItem: NSObject, NSWindowDelegate {
private var globalEventMonitor: EventMonitor?
override init() {
self.statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
self.statusItem = NSStatusBar.system
.statusItem(withLength: NSStatusItem.variableLength)
self.statusItem.isVisible = true
self.panel = PopoverPanel(viewController: CmdViewController())
@@ -48,38 +49,49 @@ final class CBMenuBarItem: NSObject, NSWindowDelegate {
// Events
// Shows panel and keeps the button highlighted.
localEventMonitor = LocalEventMonitor(mask: [.leftMouseDown]) { [weak self] event in
if let button = self?.statusItem.button, event.window == button.window, !event.modifierFlags.contains(.command) {
self?.statusButtonPressed(button)
return nil
}
return event
}
// On click and drag, the button should panel should desappear and un-highlight.
localDragEventMonitor = LocalEventMonitor(mask: [.leftMouseDragged, .keyDown]) { [weak self] event in
let modifiers = event.modifierFlags.rawValue
if let panel = self?.panel, panel.isKeyWindow {
if modsContains(keys: OSCmd, in: modifiers) && event.type == .leftMouseDragged {
self?.panel.resignKey()
localEventMonitor = LocalEventMonitor(mask: [.leftMouseDown])
{ [weak self] event in
if let button = self?.statusItem.button,
event.window == button.window,
!event.modifierFlags.contains(.command)
{
self?.statusButtonPressed(button)
return nil
}
}
return event
return event
}
// On click and drag, the button should panel should desappear and
// un-highlight.
localDragEventMonitor =
LocalEventMonitor(mask: [.leftMouseDragged, .keyDown])
{ [weak self] event in
let modifiers = event.modifierFlags.rawValue
if let panel = self?.panel, panel.isKeyWindow {
if modsContains(keys: OSCmd, in: modifiers) &&
event.type == .leftMouseDragged
{
self?.panel.resignKey()
}
}
return event
}
// NOTE: The need for this seems a bit questionable. Maybe, this
// works properly without global event monitor in MacOS Sequoia?
// Resign key whenever clicking outside of panel, thus hiding the
// panel and un-highlighting the button.
// In order to receive events, the program needs to be codesigned.
globalEventMonitor = GlobalEventMonitor(mask: [.leftMouseDown, .rightMouseDown]) { [weak self] event in
if let panel = self?.panel, panel.isKeyWindow {
panel.resignKey()
}
}
globalEventMonitor =
GlobalEventMonitor(mask: [.leftMouseDown, .rightMouseDown])
{ [weak self] event in
if let panel = self?.panel, panel.isKeyWindow {
panel.resignKey()
}
}
if let statusButton = statusItem.button,
let statusButtonWindow = statusButton.window {
@@ -118,7 +130,8 @@ final class CBMenuBarItem: NSObject, NSWindowDelegate {
private func dismissPanel() {
NSAnimationContext.runAnimationGroup { context in
context.duration = 0.3
context.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
context.timingFunction =
CAMediaTimingFunction(name: .easeInEaseOut)
panel.animator().alphaValue = 0
} completionHandler: { [weak self] in
self?.panel.orderOut(nil)
@@ -129,32 +142,26 @@ final class CBMenuBarItem: NSObject, NSWindowDelegate {
}
private func setPanelPosition() {
guard let statusItemView = statusItem.button?.window else {
panel.center()
return
guard let scrn = NSScreen.main,
let iFrame = statusItem.button?.window?.frame
else { return }
var x = iFrame.origin.x
let y = scrn.visibleFrame.height + scrn.visibleFrame.origin.y
if (iFrame.origin.x + panel.frame.width) >
(scrn.visibleFrame.origin.x +
scrn.visibleFrame.width)
{
x = iFrame.origin.x + iFrame.width - panel.frame.width
}
if (iFrame.origin.x - panel.frame.width) <
(scrn.visibleFrame.origin.x)
{
x = scrn.visibleFrame.origin.x
}
var targetRect = statusItemView.frame
if let screen = statusItemView.screen {
let panelWidth = panel.frame.width
if statusItemView.frame.origin.x + panelWidth > screen.visibleFrame.width {
targetRect.origin.x += statusItemView.frame.width
targetRect.origin.x -= panelWidth
targetRect.origin.x += Metrics.windowBorderSize
} else {
targetRect.origin.x -= Metrics.windowBorderSize
}
} else {
targetRect.origin.x -= Metrics.windowBorderSize
}
if targetRect.origin.x < 0 {
targetRect.origin.x = 0
}
panel.setFrameTopLeftPoint(targetRect.origin)
panel.setFrameTopLeftPoint(NSPoint(x: x, y: y))
}
private func setButtonHighlighted(to highlight: Bool) {
@@ -163,7 +170,9 @@ final class CBMenuBarItem: NSObject, NSWindowDelegate {
func setImage(title: String?, description: String) {
if title != nil {
statusItem.button!.image = NSImage(systemSymbolName: title!, accessibilityDescription: description)
statusItem.button!.image =
NSImage(systemSymbolName: title!,
accessibilityDescription: description)
return
}
statusItem.button!.image = nil
@@ -178,17 +187,23 @@ final class CBMenuBarItem: NSObject, NSWindowDelegate {
}
func setContents(to text: String) {
guard let viewContoller = panel.contentViewController as? CmdViewController else { return }
guard let viewContoller =
panel.contentViewController as? CmdViewController
else { return }
viewContoller.setText(text)
}
func setFile(_ file: CmdFile) {
guard let viewContoller = panel.contentViewController as? CmdViewController else { return }
guard let viewContoller =
panel.contentViewController as? CmdViewController
else { return }
viewContoller.setFile(file)
}
func windowDidResize(_ notification: Notification) {
if let cmdPanel = notification.object as? NSPanel, panel == cmdPanel {
if let cmdPanel = notification.object as? NSPanel,
panel == cmdPanel, cmdPanel.isVisible
{
setPanelPosition()
}
}
@@ -196,7 +211,8 @@ final class CBMenuBarItem: NSObject, NSWindowDelegate {
func windowDidMove(_ notification: Notification) {
if let statusBarButtonWindow = notification.object as? NSWindow,
let buttonWindow = statusItem.button?.window,
buttonWindow == statusBarButtonWindow
buttonWindow == statusBarButtonWindow,
panel.isVisible
{
setPanelPosition()
}