Skip to content

Commit 6d22d48

Browse files
committed
Add AutoresizingCustomTextEditor
1 parent ecfdfc0 commit 6d22d48

4 files changed

Lines changed: 66 additions & 41 deletions

File tree

Core/Sources/ChatGPTChatTab/ChatPanel.swift

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -540,26 +540,14 @@ struct ChatPanelInputArea: View {
540540
removeDuplicates: {
541541
$0.typedMessage == $1.typedMessage && $0.focusedField == $1.focusedField
542542
}) { viewStore in
543-
ZStack(alignment: .center) {
544-
// a hack to support dynamic height of TextEditor
545-
Text(
546-
viewStore.state.typedMessage.isEmpty ? "Hi" : viewStore.state.typedMessage
547-
).opacity(0)
548-
.font(.system(size: 14))
549-
.frame(maxWidth: .infinity, maxHeight: 400)
550-
.padding(.top, 1)
551-
.padding(.bottom, 2)
552-
.padding(.horizontal, 4)
553-
554-
CustomTextEditor(
555-
text: viewStore.$typedMessage,
556-
font: .systemFont(ofSize: 14),
557-
onSubmit: { viewStore.send(.sendButtonTapped) },
558-
completions: chatAutoCompletion
559-
)
560-
.padding(.top, 1)
561-
.padding(.bottom, -1)
562-
}
543+
AutoresizingCustomTextEditor(
544+
text: viewStore.$typedMessage,
545+
font: .systemFont(ofSize: 14),
546+
isEditable: true,
547+
maxHeight: 400,
548+
onSubmit: { viewStore.send(.sendButtonTapped) },
549+
completions: chatAutoCompletion
550+
)
563551
.focused($focusedField, equals: .textField)
564552
.bind(viewStore.$focusedField, to: $focusedField)
565553
.padding(8)

Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -372,27 +372,15 @@ extension PromptToCodePanel {
372372
)
373373
}
374374
) { viewStore in
375-
ZStack(alignment: .center) {
376-
// a hack to support dynamic height of TextEditor
377-
Text(viewStore.state.prompt.isEmpty ? "Hi" : viewStore.state.prompt)
378-
.opacity(0)
379-
.font(.system(size: 14))
380-
.frame(maxWidth: .infinity, maxHeight: 400)
381-
.padding(.top, 1)
382-
.padding(.bottom, 2)
383-
.padding(.horizontal, 4)
384-
385-
CustomTextEditor(
386-
text: viewStore.$prompt,
387-
font: .systemFont(ofSize: 14),
388-
isEditable: !viewStore.state.isResponding,
389-
onSubmit: { viewStore.send(.modifyCodeButtonTapped) }
390-
)
391-
.padding(.top, 1)
392-
.padding(.bottom, -1)
393-
.opacity(viewStore.state.isResponding ? 0.5 : 1)
394-
.disabled(viewStore.state.isResponding)
395-
}
375+
AutoresizingCustomTextEditor(
376+
text: viewStore.$prompt,
377+
font: .systemFont(ofSize: 14),
378+
isEditable: !viewStore.state.isResponding,
379+
maxHeight: 400,
380+
onSubmit: { viewStore.send(.modifyCodeButtonTapped) }
381+
)
382+
.opacity(viewStore.state.isResponding ? 0.5 : 1)
383+
.disabled(viewStore.state.isResponding)
396384
.focused($focusField, equals: .textField)
397385
.bind(viewStore.$focusField, to: $focusField)
398386
}

Tool/Package.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ let package = Package(
6161
),
6262
.package(url: "https://github.com/apple/swift-syntax.git", branch: "main"),
6363
.package(url: "https://github.com/GottaGetSwifty/CodableWrappers", from: "2.0.7"),
64+
.package(url: "https://github.com/krzyzanowskim/STTextView", from: "0.8.21"),
6465

6566
// TreeSitter
6667
.package(url: "https://github.com/intitni/SwiftTreeSitter.git", branch: "main"),
@@ -185,6 +186,7 @@ let package = Package(
185186
dependencies: [
186187
"Highlightr",
187188
"Preferences",
189+
.product(name: "STTextView", package: "STTextView"),
188190
]
189191
),
190192
.testTarget(name: "SharedUIComponentsTests", dependencies: ["SharedUIComponents"]),

Tool/Sources/SharedUIComponents/CustomTextEditor.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
11
import SwiftUI
22

3+
public struct AutoresizingCustomTextEditor: View {
4+
@Binding public var text: String
5+
public let font: NSFont
6+
public let isEditable: Bool
7+
public let maxHeight: Double
8+
public let onSubmit: () -> Void
9+
public var completions: (_ text: String, _ words: [String], _ range: NSRange) -> [String]
10+
11+
public init(
12+
text: Binding<String>,
13+
font: NSFont,
14+
isEditable: Bool,
15+
maxHeight: Double,
16+
onSubmit: @escaping () -> Void,
17+
completions: @escaping (_ text: String, _ words: [String], _ range: NSRange)
18+
-> [String] = { _, _, _ in [] }
19+
) {
20+
_text = text
21+
self.font = font
22+
self.isEditable = isEditable
23+
self.maxHeight = maxHeight
24+
self.onSubmit = onSubmit
25+
self.completions = completions
26+
}
27+
28+
public var body: some View {
29+
ZStack(alignment: .center) {
30+
// a hack to support dynamic height of TextEditor
31+
Text(text.isEmpty ? "Hi" : text).opacity(0)
32+
.font(.init(font))
33+
.frame(maxWidth: .infinity, maxHeight: maxHeight)
34+
.padding(.top, 1)
35+
.padding(.bottom, 2)
36+
.padding(.horizontal, 4)
37+
38+
CustomTextEditor(
39+
text: $text,
40+
font: font,
41+
onSubmit: onSubmit,
42+
completions: completions
43+
)
44+
.padding(.top, 1)
45+
.padding(.bottom, -1)
46+
}
47+
}
48+
}
49+
350
public struct CustomTextEditor: NSViewRepresentable {
451
public func makeCoordinator() -> Coordinator {
552
Coordinator(self)

0 commit comments

Comments
 (0)