Skip to content

Commit 5e42dc9

Browse files
committed
Add TextCursorTracker environment key
1 parent 799ae2a commit 5e42dc9

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public struct PresentingCodeSuggestion: Equatable {
3131

3232
struct CodeBlockSuggestionPanel: View {
3333
let suggestion: PresentingCodeSuggestion
34-
@Environment(TextCursorTracker.self) var textCursorTracker
34+
@Environment(\.textCursorTracker) var textCursorTracker
3535
@Environment(\.colorScheme) var colorScheme
3636
@AppStorage(\.suggestionCodeFont) var codeFont
3737
@AppStorage(\.suggestionDisplayCompactMode) var suggestionDisplayCompactMode

Core/Sources/SuggestionWidget/TextCursorTracker.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Foundation
33
import Perception
44
import SuggestionBasic
55
import XcodeInspector
6+
import SwiftUI
67

78
/// A passive tracker that observe the changes of the source editor content.
89
@Perceptible
@@ -38,8 +39,13 @@ final class TextCursorTracker {
3839
deinit {
3940
eventObservationTask?.cancel()
4041
}
42+
43+
var isPreview: Bool {
44+
ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
45+
}
4146

4247
private func observeAppChange() {
48+
if isPreview { return }
4349
editorObservationTask = []
4450
Task {
4551
await XcodeInspector.shared.safe.$focusedEditor.sink { [weak self] editor in
@@ -52,6 +58,7 @@ final class TextCursorTracker {
5258
}
5359

5460
private func observeAXNotifications(_ editor: SourceEditor) {
61+
if isPreview { return }
5562
eventObservationTask?.cancel()
5663
let content = editor.getLatestEvaluatedContent()
5764
Task { @MainActor in
@@ -70,3 +77,13 @@ final class TextCursorTracker {
7077
}
7178
}
7279

80+
struct TextCursorTrackerEnvironmentKey: EnvironmentKey {
81+
static var defaultValue: TextCursorTracker = .init()
82+
}
83+
84+
extension EnvironmentValues {
85+
var textCursorTracker: TextCursorTracker {
86+
get { self[TextCursorTrackerEnvironmentKey.self] }
87+
set { self[TextCursorTrackerEnvironmentKey.self] = newValue }
88+
}
89+
}

Core/Sources/SuggestionWidget/WidgetWindowsController.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,6 @@ public final class WidgetWindows {
651651
let store: StoreOf<WidgetFeature>
652652
let chatTabPool: ChatTabPool
653653
weak var controller: WidgetWindowsController?
654-
let cursorPositionTracker = TextCursorTracker()
655654

656655
// you should make these window `.transient` so they never show up in the mission control.
657656

@@ -721,7 +720,7 @@ public final class WidgetWindows {
721720
state: \.sharedPanelState,
722721
action: \.sharedPanel
723722
)
724-
).environment(cursorPositionTracker)
723+
)
725724
)
726725
it.setIsVisible(true)
727726
it.canBecomeKeyChecker = { [store] in
@@ -754,7 +753,7 @@ public final class WidgetWindows {
754753
state: \.suggestionPanelState,
755754
action: \.suggestionPanel
756755
)
757-
).environment(cursorPositionTracker)
756+
)
758757
)
759758
it.canBecomeKeyChecker = { false }
760759
it.setIsVisible(true)

0 commit comments

Comments
 (0)