-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add on-screen text alert option for quota warnings #1692
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
Open
SAASEmpiree
wants to merge
2
commits into
steipete:main
Choose a base branch
from
SAASEmpiree:feature/quota-warning-onscreen-alert
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
133 changes: 133 additions & 0 deletions
133
Sources/CodexBar/QuotaWarningAlertOverlayController.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,133 @@ | ||
| import AppKit | ||
| import CodexBarCore | ||
| import SwiftUI | ||
|
|
||
| /// Presents a transient, centered text alert when a quota warning threshold is crossed. | ||
| /// | ||
| /// Modeled after ``ScreenConfettiOverlayController``: it shows a borderless, click-through | ||
| /// panel above all spaces and auto-dismisses after a short lifetime, so it never steals focus | ||
| /// or blocks the user's work. | ||
| @MainActor | ||
| final class QuotaWarningAlertOverlayController { | ||
| private static let overlayLifetime: TimeInterval = 4.5 | ||
|
|
||
| private let logger = CodexBarLog.logger(LogCategories.sessionQuotaNotifications) | ||
| private var window: NSWindow? | ||
| private var dismissalTask: Task<Void, Never>? | ||
|
|
||
| func show(title: String, message: String) { | ||
| self.dismiss() | ||
|
|
||
| guard let screen = NSScreen.main ?? NSScreen.screens.first else { | ||
| self.logger.error("Cannot present quota warning overlay because no screens were found") | ||
| return | ||
| } | ||
|
|
||
| let frame = screen.frame | ||
| let contentView = QuotaWarningAlertOverlayView(title: title, message: message) | ||
| .allowsHitTesting(false) | ||
| let hostingView = NSHostingView(rootView: contentView) | ||
| hostingView.wantsLayer = true | ||
| hostingView.layer?.backgroundColor = NSColor.clear.cgColor | ||
|
|
||
| let window = ClickThroughAlertPanel( | ||
| contentRect: frame, | ||
| styleMask: [.borderless, .nonactivatingPanel], | ||
| backing: .buffered, | ||
| defer: false, | ||
| screen: screen) | ||
| window.contentView = hostingView | ||
| window.level = .statusBar | ||
| window.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary, .ignoresCycle, .stationary] | ||
| window.backgroundColor = .clear | ||
| window.isOpaque = false | ||
| window.hasShadow = false | ||
| window.ignoresMouseEvents = true | ||
| window.acceptsMouseMovedEvents = false | ||
| window.isMovable = false | ||
| window.isReleasedWhenClosed = false | ||
| window.canHide = false | ||
| window.hidesOnDeactivate = false | ||
| window.becomesKeyOnlyIfNeeded = false | ||
| window.isExcludedFromWindowsMenu = true | ||
| window.setFrame(frame, display: false) | ||
| window.orderFrontRegardless() | ||
| self.window = window | ||
|
|
||
| self.logger.info("Presenting quota warning overlay") | ||
|
|
||
| self.dismissalTask = Task { @MainActor [weak self] in | ||
| try? await Task.sleep(for: .seconds(Self.overlayLifetime)) | ||
| // A newer alert cancels this task in `show()`; bail out so we don't | ||
| // tear down the replacement overlay that took our place. | ||
| guard !Task.isCancelled else { return } | ||
| self?.dismiss() | ||
| } | ||
| } | ||
|
|
||
| func dismiss() { | ||
| self.dismissalTask?.cancel() | ||
| self.dismissalTask = nil | ||
|
|
||
| guard let window = self.window else { return } | ||
| window.orderOut(nil) | ||
| window.close() | ||
| self.window = nil | ||
| } | ||
| } | ||
|
|
||
| private final class ClickThroughAlertPanel: NSPanel { | ||
| override var canBecomeKey: Bool { | ||
| false | ||
| } | ||
|
|
||
| override var canBecomeMain: Bool { | ||
| false | ||
| } | ||
|
|
||
| override var acceptsFirstResponder: Bool { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| private struct QuotaWarningAlertOverlayView: View { | ||
| let title: String | ||
| let message: String | ||
|
|
||
| @State private var appeared = false | ||
|
|
||
| var body: some View { | ||
| HStack(alignment: .top, spacing: 12) { | ||
| Image(systemName: "exclamationmark.triangle.fill") | ||
| .font(.title2) | ||
| .foregroundStyle(.orange) | ||
|
|
||
| VStack(alignment: .leading, spacing: 4) { | ||
| Text(self.title) | ||
| .font(.headline) | ||
| Text(self.message) | ||
| .font(.subheadline) | ||
| .foregroundStyle(.secondary) | ||
| .fixedSize(horizontal: false, vertical: true) | ||
| } | ||
| } | ||
| .padding(.vertical, 16) | ||
| .padding(.horizontal, 20) | ||
| .frame(maxWidth: 420, alignment: .leading) | ||
| .background(.regularMaterial, in: RoundedRectangle(cornerRadius: 16, style: .continuous)) | ||
| .overlay( | ||
| RoundedRectangle(cornerRadius: 16, style: .continuous) | ||
| .strokeBorder(Color.primary.opacity(0.08))) | ||
| .shadow(color: .black.opacity(0.25), radius: 24, y: 8) | ||
| .scaleEffect(self.appeared ? 1 : 0.92) | ||
| .opacity(self.appeared ? 1 : 0) | ||
| .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center) | ||
| .padding(40) | ||
| .allowsHitTesting(false) | ||
| .task { | ||
| withAnimation(.spring(response: 0.4, dampingFraction: 0.8)) { | ||
| self.appeared = true | ||
| } | ||
| } | ||
| } | ||
| } | ||
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
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
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.
When another warning arrives while an overlay is visible,
showcancels the previousdismissalTaskbefore assigning the replacement window; because this task swallowsCancellationErrorfromTask.sleepand then still callsdismiss(), the canceled old task can resume after the new window is stored and immediately close the new alert. This is reachable for back-to-back session/weekly threshold crossings in one refresh, so the on-screen alert can flash and disappear instead of staying up for 4.5 seconds; return on cancellation or gate dismissal to the task/window that scheduled it.Useful? React with 👍 / 👎.