import SharedUIComponents import SwiftUI struct CodeBlockSuggestionPanel: View { @ObservedObject var suggestion: CodeSuggestionProvider @Environment(\.colorScheme) var colorScheme @AppStorage(\.suggestionCodeFont) var codeFont @AppStorage(\.suggestionDisplayCompactMode) var suggestionDisplayCompactMode @AppStorage(\.suggestionPresentationMode) var suggestionPresentationMode @AppStorage(\.hideCommonPrecedingSpacesInSuggestion) var hideCommonPrecedingSpaces @AppStorage(\.syncSuggestionHighlightTheme) var syncHighlightTheme @AppStorage(\.codeForegroundColorLight) var codeForegroundColorLight @AppStorage(\.codeForegroundColorDark) var codeForegroundColorDark @AppStorage(\.codeBackgroundColorLight) var codeBackgroundColorLight @AppStorage(\.codeBackgroundColorDark) var codeBackgroundColorDark struct ToolBar: View { @ObservedObject var suggestion: CodeSuggestionProvider var body: some View { HStack { Button(action: { suggestion.selectPreviousSuggestion() }) { Image(systemName: "chevron.left") }.buttonStyle(.plain) Text( "\(suggestion.currentSuggestionIndex + 1) / \(suggestion.suggestionCount)" ) .monospacedDigit() Button(action: { suggestion.selectNextSuggestion() }) { Image(systemName: "chevron.right") }.buttonStyle(.plain) Spacer() Button(action: { suggestion.dismissSuggestion() }) { Text("Dismiss").foregroundStyle(.tertiary).padding(.trailing, 4) }.buttonStyle(.plain) Button(action: { suggestion.rejectSuggestion() }) { Text("Reject") }.buttonStyle(CommandButtonStyle(color: .gray)) Button(action: { suggestion.acceptSuggestion() }) { Text("Accept") }.buttonStyle(CommandButtonStyle(color: .accentColor)) } .padding() .foregroundColor(.secondary) .background(.regularMaterial) } } struct CompactToolBar: View { @ObservedObject var suggestion: CodeSuggestionProvider var body: some View { HStack { Button(action: { suggestion.selectPreviousSuggestion() }) { Image(systemName: "chevron.left") }.buttonStyle(.plain) Text( "\(suggestion.currentSuggestionIndex + 1) / \(suggestion.suggestionCount)" ) .monospacedDigit() Button(action: { suggestion.selectNextSuggestion() }) { Image(systemName: "chevron.right") }.buttonStyle(.plain) Spacer() Button(action: { suggestion.dismissSuggestion() }) { Image(systemName: "xmark") }.buttonStyle(.plain) } .padding(4) .font(.caption) .foregroundColor(.secondary) .background(.regularMaterial) } } var body: some View { VStack(spacing: 0) { CustomScrollView { CodeBlock( code: suggestion.code, language: suggestion.language, startLineIndex: suggestion.startLineIndex, scenario: "suggestion", colorScheme: colorScheme, font: codeFont.value.nsFont, droppingLeadingSpaces: hideCommonPrecedingSpaces, proposedForegroundColor: { if syncHighlightTheme { if colorScheme == .light, let color = codeForegroundColorLight.value?.swiftUIColor { return color } else if let color = codeForegroundColorDark.value?.swiftUIColor { return color } } return nil }() ) .frame(maxWidth: .infinity) .background({ () -> Color in if syncHighlightTheme { if colorScheme == .light, let color = codeBackgroundColorLight.value?.swiftUIColor { return color } else if let color = codeBackgroundColorDark.value?.swiftUIColor { return color } } return Color.contentBackground }()) } if suggestionDisplayCompactMode { CompactToolBar(suggestion: suggestion) } else { ToolBar(suggestion: suggestion) } } .xcodeStyleFrame(cornerRadius: { switch suggestionPresentationMode { case .nearbyTextCursor: 6 case .floatingWidget: nil } }()) } } // MARK: - Previews #Preview("Code Block Suggestion Panel") { CodeBlockSuggestionPanel(suggestion: CodeSuggestionProvider( code: """ LazyVGrid(columns: [GridItem(.fixed(30)), GridItem(.flexible())]) { ForEach(0..