Floating Shelf: a macOS shelf at the edge of the screen for holding files, images and text while you move between apps.

0 downloads

dbbasic-floating-shelf/FloatingShelf/FloatingShelfApp.swift
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-floating-shelf.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-floating-shelf.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-floating-shelf.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add MIT License 76c5d29 1 year ago History
FloatingShelfApp.swift · 3.1 KB · 102 lines
1import SwiftUI
2
3@main
4struct FloatingShelfApp: App {
5 @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
6
7 var body: some Scene {
8 Settings {
9 EmptyView()
10 }
11 }
12}
13
14class AppDelegate: NSObject, NSApplicationDelegate {
15 var statusItem: NSStatusItem?
16 var shelfWindow: FloatingShelfWindow?
17 var edgeWindows: [EdgeDetectorWindow] = []
18
19 func applicationDidFinishLaunching(_ notification: Notification) {
20 // Hide dock icon
21 NSApp.setActivationPolicy(.accessory)
22
23 // Create status bar item
24 statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
25 if let button = statusItem?.button {
26 button.image = NSImage(systemSymbolName: "tray.2", accessibilityDescription: "Floating Shelf")
27 }
28
29 // Create menu for status bar item
30 let menu = NSMenu()
31
32 menu.addItem(NSMenuItem(title: "Show Shelf", action: #selector(showShelf), keyEquivalent: "s"))
33 menu.addItem(NSMenuItem(title: "Hide Shelf", action: #selector(hideShelf), keyEquivalent: "h"))
34 menu.addItem(NSMenuItem.separator())
35 menu.addItem(NSMenuItem(title: "Quit Floating Shelf", action: #selector(quitApp), keyEquivalent: "q"))
36
37 statusItem?.menu = menu
38
39 // Initialize shelf window
40 shelfWindow = FloatingShelfWindow()
41
42 // Create edge detector windows
43 createEdgeWindows()
44 }
45
46 @objc func showShelf() {
47 if let screen = NSScreen.main {
48 let center = CGPoint(
49 x: screen.frame.midX,
50 y: screen.frame.midY
51 )
52 shelfWindow?.show(at: center)
53 }
54 }
55
56 @objc func hideShelf() {
57 shelfWindow?.hide()
58 }
59
60 @objc func quitApp() {
61 NSApplication.shared.terminate(nil)
62 }
63
64 func createEdgeWindows() {
65 guard let screen = NSScreen.main else { return }
66 let screenFrame = screen.frame
67 let edgeWidth: CGFloat = 30
68
69 // Left edge
70 let leftWindow = EdgeDetectorWindow(
71 frame: NSRect(x: screenFrame.minX, y: screenFrame.minY,
72 width: edgeWidth, height: screenFrame.height)
73 )
74 leftWindow.onDragDetected = { [weak self] location in
75 self?.shelfWindow?.show(at: location)
76 }
77
78 // Right edge
79 let rightWindow = EdgeDetectorWindow(
80 frame: NSRect(x: screenFrame.maxX - edgeWidth, y: screenFrame.minY,
81 width: edgeWidth, height: screenFrame.height)
82 )
83 rightWindow.onDragDetected = { [weak self] location in
84 self?.shelfWindow?.show(at: location)
85 }
86
87 // Top edge
88 let topWindow = EdgeDetectorWindow(
89 frame: NSRect(x: screenFrame.minX, y: screenFrame.maxY - edgeWidth,
90 width: screenFrame.width, height: edgeWidth)
91 )
92 topWindow.onDragDetected = { [weak self] location in
93 self?.shelfWindow?.show(at: location)
94 }
95
96 edgeWindows = [leftWindow, rightWindow, topWindow]
97 }
98
99 func applicationWillTerminate(_ notification: Notification) {
100 edgeWindows.removeAll()
101 }
102}