seamless title bar, ctrl+b sidebar toggle

This commit is contained in:
jess 2026-04-04 22:08:11 -07:00
parent c7b42d2f15
commit dd9283d8e6
1 changed files with 27 additions and 3 deletions

View File

@ -1,9 +1,11 @@
import Cocoa import Cocoa
import Combine
import SwiftUI import SwiftUI
class AppDelegate: NSObject, NSApplicationDelegate { class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow! var window: NSWindow!
var appState: AppState! var appState: AppState!
private var titleCancellable: AnyCancellable?
func applicationDidFinishLaunching(_ notification: Notification) { func applicationDidFinishLaunching(_ notification: Notification) {
appState = AppState() appState = AppState()
@ -13,10 +15,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
window = NSWindow( window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 1200, height: 800), contentRect: NSRect(x: 0, y: 0, width: 1200, height: 800),
styleMask: [.titled, .closable, .miniaturizable, .resizable], styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered, backing: .buffered,
defer: false defer: false
) )
window.titlebarAppearsTransparent = true
window.titleVisibility = .hidden
window.backgroundColor = Theme.current.base
window.title = "Swiftly" window.title = "Swiftly"
window.contentView = hostingView window.contentView = hostingView
window.center() window.center()
@ -24,6 +29,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
window.makeKeyAndOrderFront(nil) window.makeKeyAndOrderFront(nil)
setupMenuBar() setupMenuBar()
observeDocumentTitle()
} }
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
@ -107,8 +113,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
private func buildViewMenu() -> NSMenuItem { private func buildViewMenu() -> NSMenuItem {
let item = NSMenuItem() let item = NSMenuItem()
let menu = NSMenu(title: "View") let menu = NSMenu(title: "View")
let toggleItem = NSMenuItem(title: "Toggle Sidebar", action: #selector(toggleSidebar), keyEquivalent: "\\") let toggleItem = NSMenuItem(title: "Toggle Sidebar", action: #selector(toggleSidebar), keyEquivalent: "b")
toggleItem.keyEquivalentModifierMask = .command toggleItem.keyEquivalentModifierMask = .control
toggleItem.target = self toggleItem.target = self
menu.addItem(toggleItem) menu.addItem(toggleItem)
item.submenu = menu item.submenu = menu
@ -160,4 +166,22 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@objc private func toggleSidebar() { @objc private func toggleSidebar() {
NotificationCenter.default.post(name: .toggleSidebar, object: nil) NotificationCenter.default.post(name: .toggleSidebar, object: nil)
} }
private func observeDocumentTitle() {
titleCancellable = appState.$documentText
.receive(on: RunLoop.main)
.sink { [weak self] text in
guard let self = self else { return }
let firstLine = text.components(separatedBy: "\n").first?
.trimmingCharacters(in: .whitespaces) ?? ""
if firstLine.isEmpty {
self.window.title = "Swiftly"
} else {
let clean = firstLine.replacingOccurrences(
of: "^#+\\s*", with: "", options: .regularExpression
)
self.window.title = clean.isEmpty ? "Swiftly" : String(clean.prefix(60))
}
}
}
} }