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 Combine
import SwiftUI
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow!
var appState: AppState!
private var titleCancellable: AnyCancellable?
func applicationDidFinishLaunching(_ notification: Notification) {
appState = AppState()
@ -13,10 +15,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 1200, height: 800),
styleMask: [.titled, .closable, .miniaturizable, .resizable],
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
backing: .buffered,
defer: false
)
window.titlebarAppearsTransparent = true
window.titleVisibility = .hidden
window.backgroundColor = Theme.current.base
window.title = "Swiftly"
window.contentView = hostingView
window.center()
@ -24,6 +29,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
window.makeKeyAndOrderFront(nil)
setupMenuBar()
observeDocumentTitle()
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
@ -107,8 +113,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
private func buildViewMenu() -> NSMenuItem {
let item = NSMenuItem()
let menu = NSMenu(title: "View")
let toggleItem = NSMenuItem(title: "Toggle Sidebar", action: #selector(toggleSidebar), keyEquivalent: "\\")
toggleItem.keyEquivalentModifierMask = .command
let toggleItem = NSMenuItem(title: "Toggle Sidebar", action: #selector(toggleSidebar), keyEquivalent: "b")
toggleItem.keyEquivalentModifierMask = .control
toggleItem.target = self
menu.addItem(toggleItem)
item.submenu = menu
@ -160,4 +166,22 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@objc private func toggleSidebar() {
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))
}
}
}
}