From dd9283d8e624d20e50fc756f1e969323dd972923 Mon Sep 17 00:00:00 2001 From: jess Date: Sat, 4 Apr 2026 22:08:11 -0700 Subject: [PATCH] seamless title bar, ctrl+b sidebar toggle --- src/AppDelegate.swift | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/AppDelegate.swift b/src/AppDelegate.swift index f84d6f7..6420152 100644 --- a/src/AppDelegate.swift +++ b/src/AppDelegate.swift @@ -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)) + } + } + } }