-
Notifications
You must be signed in to change notification settings - Fork 841
Add enhanced floating dashboard with horizontal compact layout #452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
liuyixin-louis
wants to merge
1
commit into
steipete:main
from
liuyixin-louis:feature/floating-dashboard-enhanced
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
179 changes: 179 additions & 0 deletions
179
Sources/CodexBar/FloatingDashboardWindowController.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| import AppKit | ||
| import Observation | ||
| import SwiftUI | ||
|
|
||
| @MainActor | ||
| final class FloatingDashboardWindowController { | ||
| private var panel: NSPanel? | ||
| private let store: UsageStore | ||
| private let settings: SettingsStore | ||
| private nonisolated(unsafe) var moveObserver: NSObjectProtocol? | ||
| private var lastHorizontal: Bool | ||
|
|
||
| init(store: UsageStore, settings: SettingsStore) { | ||
| self.store = store | ||
| self.settings = settings | ||
| self.lastHorizontal = settings.floatingDashboardHorizontal | ||
| } | ||
|
|
||
| func show() { | ||
| if let panel { | ||
| panel.orderFront(nil) | ||
| return | ||
| } | ||
|
|
||
| let panel = NSPanel( | ||
| contentRect: .zero, | ||
| styleMask: [.borderless, .nonactivatingPanel], | ||
| backing: .buffered, | ||
| defer: true) | ||
| panel.level = .floating | ||
| panel.collectionBehavior = [.canJoinAllSpaces, .stationary, .ignoresCycle] | ||
| panel.isMovableByWindowBackground = true | ||
| panel.backgroundColor = .clear | ||
| panel.hasShadow = true | ||
| panel.isOpaque = false | ||
|
|
||
| let view = FloatingDashboardView(store: self.store, settings: self.settings) | ||
| let hosting = NSHostingView(rootView: view) | ||
| panel.contentView = hosting | ||
|
|
||
| let size = hosting.fittingSize | ||
| panel.setContentSize(size) | ||
| self.applyPosition(to: panel, size: size) | ||
|
|
||
| panel.orderFront(nil) | ||
| self.panel = panel | ||
|
|
||
| self.moveObserver = NotificationCenter.default.addObserver( | ||
| forName: NSWindow.didMoveNotification, | ||
| object: panel, | ||
| queue: .main) | ||
| { [weak self] _ in | ||
| Task { @MainActor [weak self] in | ||
| self?.savePosition() | ||
| } | ||
| } | ||
|
|
||
| self.observeStoreChanges() | ||
| } | ||
|
|
||
| func hide() { | ||
| if let observer = self.moveObserver { | ||
| NotificationCenter.default.removeObserver(observer) | ||
| self.moveObserver = nil | ||
| } | ||
| self.panel?.orderOut(nil) | ||
| self.panel = nil | ||
| } | ||
|
|
||
| func toggle() { | ||
| if self.panel != nil { | ||
| self.hide() | ||
| } else { | ||
| self.show() | ||
| } | ||
| } | ||
|
|
||
| private func applyPosition(to panel: NSPanel, size: NSSize) { | ||
| if let saved = self.settings.floatingDashboardPosition, | ||
| let x = saved["x"], | ||
| let y = saved["y"] | ||
| { | ||
| panel.setFrameOrigin(NSPoint(x: x, y: y)) | ||
| } else { | ||
| let screen = NSScreen.main ?? NSScreen.screens.first | ||
| let visibleFrame = screen?.visibleFrame ?? NSRect(x: 0, y: 0, width: 1440, height: 900) | ||
| let x = visibleFrame.maxX - size.width - 20 | ||
| let y = visibleFrame.minY + 20 | ||
| panel.setFrameOrigin(NSPoint(x: x, y: y)) | ||
| } | ||
| } | ||
|
|
||
| private func savePosition() { | ||
| guard let frame = self.panel?.frame else { return } | ||
| self.settings.floatingDashboardPosition = [ | ||
| "x": Double(frame.origin.x), | ||
| "y": Double(frame.origin.y), | ||
| ] | ||
| } | ||
|
|
||
| private func observeStoreChanges() { | ||
| withObservationTracking { | ||
| _ = self.store.menuObservationToken | ||
| _ = self.settings.floatingDashboardHorizontal | ||
| } onChange: { [weak self] in | ||
| Task { @MainActor [weak self] in | ||
| guard let self, self.panel != nil else { return } | ||
| self.resizePanel() | ||
| self.observeStoreChanges() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func resizePanel() { | ||
| guard self.panel != nil else { return } | ||
| // If the layout orientation changed, recreate the panel to avoid constraint crashes | ||
| let currentHorizontal = self.settings.floatingDashboardHorizontal | ||
| if currentHorizontal != self.lastHorizontal { | ||
| self.lastHorizontal = currentHorizontal | ||
| self.recreatePanel() | ||
| return | ||
| } | ||
| guard let panel, let hosting = panel.contentView as? NSHostingView<FloatingDashboardView> else { return } | ||
| let origin = panel.frame.origin | ||
| let newSize = hosting.fittingSize | ||
| panel.setContentSize(newSize) | ||
| panel.setFrameOrigin(origin) | ||
| } | ||
|
|
||
| private func recreatePanel() { | ||
| let savedOrigin = self.panel?.frame.origin | ||
| self.hide() | ||
|
|
||
| let panel = NSPanel( | ||
| contentRect: .zero, | ||
| styleMask: [.borderless, .nonactivatingPanel], | ||
| backing: .buffered, | ||
| defer: true) | ||
| panel.level = .floating | ||
| panel.collectionBehavior = [.canJoinAllSpaces, .stationary, .ignoresCycle] | ||
| panel.isMovableByWindowBackground = true | ||
| panel.backgroundColor = .clear | ||
| panel.hasShadow = true | ||
| panel.isOpaque = false | ||
|
|
||
| let view = FloatingDashboardView(store: self.store, settings: self.settings) | ||
| let hosting = NSHostingView(rootView: view) | ||
| panel.contentView = hosting | ||
|
|
||
| let size = hosting.fittingSize | ||
| panel.setContentSize(size) | ||
| if let origin = savedOrigin { | ||
| panel.setFrameOrigin(origin) | ||
| } else { | ||
| self.applyPosition(to: panel, size: size) | ||
| } | ||
|
|
||
| panel.orderFront(nil) | ||
| self.panel = panel | ||
|
|
||
| self.moveObserver = NotificationCenter.default.addObserver( | ||
| forName: NSWindow.didMoveNotification, | ||
| object: panel, | ||
| queue: .main) | ||
| { [weak self] _ in | ||
| Task { @MainActor [weak self] in | ||
| self?.savePosition() | ||
| } | ||
| } | ||
|
|
||
| self.observeStoreChanges() | ||
| } | ||
|
|
||
| deinit { | ||
| if let observer = self.moveObserver { | ||
| NotificationCenter.default.removeObserver(observer) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
onChangepath inobserveStoreChanges()already re-registers observation after each change, butrecreatePanel()registers again here; when users toggle layout (which callsrecreatePanel()), each toggle leaves an extra active tracker, so later store updates invoke duplicate resize/recreate work and create avoidable UI churn.Useful? React with 👍 / 👎.