Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Clipbara/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ final class AppState {
self?.previewItem = nil
}
setupHotkey()

// Render the panel once off screen so the first hotkey press is instant.
Task { @MainActor [weak self] in
try? await Task.sleep(for: .milliseconds(300))
guard let self, let container = self.modelContainer else { return }
self.panelController.prewarm(modelContainer: container, appState: self)
}
}

func togglePanel() {
Expand Down
25 changes: 25 additions & 0 deletions Clipbara/Panel/PanelController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,31 @@ final class PanelController {
}
}

/// Builds the panel and renders its first frame off screen so the first
/// hotkey press only has to animate. Called shortly after launch.
func prewarm(modelContainer: ModelContainer, appState: AppState) {
guard panel == nil else { return }
self.appState = appState

let screenFrame = (NSScreen.main ?? NSScreen.screens.first!).visibleFrame
let frame = panelFrame(in: screenFrame, itemCount: 0, y: screenFrame.origin.y)
.offsetBy(dx: 0, dy: -baseHeight)

let warm = ClipbaraPanel(contentRect: frame)
warm.alphaValue = 0
warm.contentView = NSHostingView(
rootView: HistoryPanelView()
.environment(appState)
.modelContainer(modelContainer)
)
warm.orderFrontRegardless()
warm.contentView?.layoutSubtreeIfNeeded()
warm.displayIfNeeded()
warm.orderOut(nil)
warm.alphaValue = 1
panel = warm
}

func showPanel(modelContainer: ModelContainer, appState: AppState) {
guard !isVisible else { return }
self.appState = appState
Expand Down
Loading