Skip to content

Commit

Permalink
perf: add placeholder for TextEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed May 27, 2024
1 parent ca75db8 commit 6832184
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ private struct CustomOpenAIServiceConfigurationView: View {
// models
TextEditorCell(
title: "service.configuration.custom_openai.supported_models.title",
text: viewModel.$availableModels ?? ""
text: viewModel.$availableModels ?? "",
placeholder: "service.configuration.custom_openai.model.placeholder"
)

Picker(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ import SwiftUI
// MARK: - TextEditorCell

struct TextEditorCell: View {
// MARK: Internal

let title: LocalizedStringKey
@Binding var text: String

let corner = RoundedRectangle(cornerRadius: 5)
let placeholder: LocalizedStringKey

var body: some View {
HStack(alignment: .center, spacing: 20) {
Text(title)
TextEditor(text: $text)
TextEditorWithPlaceholder(text: $text, placeholder: placeholder, alignment: .topTrailing)
.padding(.horizontal, 3)
.padding(.vertical, 5)
.font(.body)
.lineSpacing(5)
.scrollContentBackground(.hidden)
.scrollContentBackground(.hidden) // Refer https://stackoverflow.com/a/62848618/8378840
.scrollIndicators(.hidden)
.background(Color.clear)
.clipShape(corner)
Expand All @@ -35,4 +36,30 @@ struct TextEditorCell: View {
}
.padding(10)
}

// MARK: Private

private let corner = RoundedRectangle(cornerRadius: 5)
}

// MARK: - TextEditorWithPlaceholder

struct TextEditorWithPlaceholder: View {
@Binding var text: String
let placeholder: LocalizedStringKey?
var alignment: Alignment = .leading

var body: some View {
HStack {
ZStack(alignment: alignment) {
if let placeholder = placeholder, text.isEmpty {
Text(placeholder)
.foregroundColor(Color(NSColor.placeholderTextColor))
.padding(3)
}

TextEditor(text: $text)
}
}
}
}

0 comments on commit 6832184

Please sign in to comment.