Skip to content

Commit

Permalink
feat: add clipboard and orientation management
Browse files Browse the repository at this point in the history
save each prompt to the clipboard and disable openAI input in landscape

#18
  • Loading branch information
bsorrentino committed Apr 1, 2023
1 parent a69858e commit 2911f42
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 90 deletions.
4 changes: 4 additions & 0 deletions PROMPT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# OpenAI Prompt

* `make a new sequence that represent an OAuth2 classic flow`

47 changes: 27 additions & 20 deletions PlantUML/PlantUML+OpenAI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import SwiftUI
import OpenAIKit

extension PlantUMLContentView {
func ToggleOpenAIButton() -> some View {

var ToggleOpenAIButton: some View {

Button {
viewState.isOpenAIVisible.toggle()
}
label: {
Label( "OpenAI Editor", systemImage: "brain" )
.labelStyle(.iconOnly)
.foregroundColor( viewState.isEditorVisible ? .blue : .gray)

}
label: {
Label( "OpenAI Editor", systemImage: "brain" )
.environment(\.symbolVariants, .fill)
.labelStyle(.iconOnly)
.foregroundColor( viewState.isOpenAIVisible ? .blue : .gray)
}
}

}
Expand Down Expand Up @@ -73,16 +74,15 @@ class OpenAIService : ObservableObject {

}()

@MainActor
func generateEdit( input: String, instruction: String ) async -> String? {

guard let openAI, case .Ready = status else {
return nil
}

Task { @MainActor in
self.status = .Editing
}

self.status = .Editing

do {
let editParameter = EditParameters(
model: "text-davinci-edit-001",
Expand All @@ -99,7 +99,9 @@ class OpenAIService : ObservableObject {
return result
}
catch {

status = .Error( error.localizedDescription )

return nil
}
}
Expand Down Expand Up @@ -135,19 +137,19 @@ struct OpenAIView : View {

var body: some View {

VStack {
VStack(spacing:0) {
HStack(spacing: 10) {
Button( action: { tabs = .Input } ) {
Label( "OpenAI", systemImage: "")
}
Divider().frame(height: 20 )
Button( action: { tabs = .Result } ) {
Label( "Result", systemImage: "")
}
Divider().frame(height: 20 )
Button( action: { tabs = .Prompt } ) {
Label( "Prompt", systemImage: "")
}
Divider().frame(height: 20 )
Button( action: { tabs = .Result } ) {
Label( "Result", systemImage: "")
}
}
if case .Input = tabs {
Input_Fragment
Expand All @@ -160,7 +162,7 @@ struct OpenAIView : View {
Prompt_Fragment
}
}
.padding()
.padding( EdgeInsets(top: 0, leading: 5, bottom: 5, trailing: 0))

}
}
Expand All @@ -170,7 +172,7 @@ extension OpenAIView {

var Input_Fragment: some View {

VStack(spacing:5) {
ZStack(alignment: .bottomTrailing ) {
TextEditor(text: $instruction)
.font(.title3.monospaced() )
.lineSpacing(15)
Expand Down Expand Up @@ -207,7 +209,7 @@ extension OpenAIView {
},
label: {
if isEditing {
ProgressView("AI editing....")
ProgressView()
}
else {
Label( "Submit", systemImage: "arrow.right")
Expand All @@ -216,6 +218,7 @@ extension OpenAIView {
.disabled( isEditing )

}
.padding()
}
.padding()
}
Expand All @@ -228,7 +231,10 @@ extension OpenAIView {
var Prompt_Fragment: some View {

List( service.prompt.elements, id: \.self ) { prompt in
Text( prompt )
HStack {
Text( prompt )
CopyToClipboardButton( value: prompt )
}
}
}

Expand Down Expand Up @@ -262,5 +268,6 @@ struct OpenAIView_Previews: PreviewProvider {
OpenAIView( service: OpenAIService(),
result: Binding.constant(""),
onUndo: { } )
.frame(height: 200)
}
}
136 changes: 66 additions & 70 deletions PlantUML/PlantUMLContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,22 @@ struct PlantUMLContentView: View {
}

}

@Environment(\.scenePhase) var scene
@Environment(\.interfaceOrientation) var interfaceOrientation: InterfaceOrientationHolder
@Environment(\.editMode) private var editMode
@Environment(\.openURL) private var openURL
@State var keyboardTab: String = "general"


@StateObject var document: PlantUMLDocumentProxy

@StateObject var viewState = ViewState()

@State private var isScaleToFit = true
@State private var fontSize = CGFloat(12)
@State private var showLine:Bool = false

@State private var diagramImage:UIImage?

@StateObject private var service = OpenAIService()

@State var keyboardTab: String = "general"
@State private var isScaleToFit = true
@State private var fontSize = CGFloat(12)
@State private var showLine:Bool = false
@State private var saving = false

@State private var openAIResult:String = ""

@StateObject private var service = OpenAIService()

var PlantUMLDiagramViewFit: some View {
PlantUMLDiagramView( url: document.buildURL(), contentMode: .fit )
}


var EditorView_Fragment: some View {

PlantUMLLineEditorView( items: $document.items,
fontSize: $fontSize,
showLine: $showLine) { onHide, onPressSymbol in
PlantUMLKeyboardView( selectedTab: $keyboardTab,
onHide: onHide,
onPressSymbol: onPressSymbol)
}
.onChange(of: document.items ) { _ in
saving = true
document.updateRequest.send()
}
}
@State private var diagramImage:UIImage?

var OpenAIView_Fragment: some View {

Expand All @@ -96,24 +72,6 @@ struct PlantUMLContentView: View {

}

func DiagramView_Fragment( size: CGSize ) -> some View {

Group {
if isScaleToFit {
PlantUMLDiagramViewFit
.frame( width: size.width, height: size.height )
}
else {
ScrollView([.horizontal, .vertical], showsIndicators: true) {
PlantUMLDiagramView( url: document.buildURL(), contentMode: .fill )
.frame( minWidth: size.width)
}
.frame( minWidth: size.width, minHeight: size.height )
}
}

}

var body: some View {

VStack {
Expand All @@ -126,10 +84,10 @@ struct PlantUMLContentView: View {
DiagramView_Fragment( size: geometry.size )
}
}
if viewState.isOpenAIVisible {
Divider()
if viewState.isOpenAIVisible && interfaceOrientation.value.isPortrait {
// Divider()
OpenAIView_Fragment
.frame( height: 300 )
.frame( height: 200 )

}
}
Expand All @@ -141,26 +99,30 @@ struct PlantUMLContentView: View {
viewState.forceUpdate()
}
}
//.navigationBarTitleDisplayMode(.inline)
.onRotate(perform: { orientation in
if (orientation.isPortrait && viewState.isDiagramVisible) ||
(orientation.isLandscape && viewState.isEditorVisible)
{
viewState.isEditorVisible.toggle()
}
})
//.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItemGroup(placement: .navigationBarLeading) {
}
ToolbarItemGroup(placement: .navigationBarTrailing) {
HStack( spacing: 0 ) {
SavingStateView( saving: saving )

HStack(alignment: .center, spacing: 5) {
ToggleOpenAIButton()
Divider().background(Color.blue)
if interfaceOrientation.value.isPortrait {
HStack(alignment: .center, spacing: 5) {
ToggleOpenAIButton
Divider().background(Color.blue)
}
.frame(height:20)
}
.frame(height:20)


ToggleEditorButton()
if viewState.isEditorVisible {
HStack {
Expand All @@ -182,10 +144,26 @@ struct PlantUMLContentView: View {
}

//
// MARK: - Editor actions -
// MARK: - Editor extension -
//
extension PlantUMLContentView {

var EditorView_Fragment: some View {

PlantUMLLineEditorView( items: $document.items,
fontSize: $fontSize,
showLine: $showLine) { onHide, onPressSymbol in
PlantUMLKeyboardView( selectedTab: $keyboardTab,
onHide: onHide,
onPressSymbol: onPressSymbol)
}
.onChange(of: document.items ) { _ in
saving = true
document.updateRequest.send()
}
}


// [SwiftUI Let View disappear automatically](https://stackoverflow.com/a/60820491/521197)
struct SavedStateView: View {
@Binding var visible: Bool
Expand Down Expand Up @@ -240,18 +218,14 @@ extension PlantUMLContentView {
Button( action: { fontSize += 1 } ) {
Image( systemName: "textformat.size.larger")
}
.padding( EdgeInsets(top:0, leading: 5,bottom: 0, trailing: 0))
Divider().background(Color.blue).frame(height:20)
Divider()
.background(Color.blue)
.frame(height:20)
.padding( .leading, 5)
Button( action: { fontSize -= 1} ) {
Image( systemName: "textformat.size.smaller")
}
.padding( EdgeInsets(top:0, leading: 5,bottom: 0, trailing: 0))
}
// .overlay {
// RoundedRectangle(cornerRadius: 16)
// .stroke(.blue, lineWidth: 1)
// }
//.padding()
}

func ToggleEditorButton() -> some View {
Expand All @@ -278,7 +252,7 @@ extension PlantUMLContentView {
Button( action: {
document.save()
},
label: {
label: {
Label( "Save", systemImage: "arrow.down.doc.fill" )
.labelStyle(.titleOnly)
})
Expand All @@ -291,6 +265,28 @@ extension PlantUMLContentView {
//
extension PlantUMLContentView {

var PlantUMLDiagramViewFit: some View {
PlantUMLDiagramView( url: document.buildURL(), contentMode: .fit )
}

func DiagramView_Fragment( size: CGSize ) -> some View {

Group {
if isScaleToFit {
PlantUMLDiagramViewFit
.frame( width: size.width, height: size.height )
}
else {
ScrollView([.horizontal, .vertical], showsIndicators: true) {
PlantUMLDiagramView( url: document.buildURL(), contentMode: .fill )
.frame( minWidth: size.width)
}
.frame( minWidth: size.width, minHeight: size.height )
}
}

}

func ShareDiagramButton() -> some View {

Button(action: {
Expand Down

0 comments on commit 2911f42

Please sign in to comment.