Skip to content

Instantly share code, notes, and snippets.

@krismuniz
Created December 4, 2022 02:16
Show Gist options
  • Save krismuniz/e6aa14a887bfefd4bbfec5968cfad286 to your computer and use it in GitHub Desktop.
Save krismuniz/e6aa14a887bfefd4bbfec5968cfad286 to your computer and use it in GitHub Desktop.
Interesting Conversations with GPT

I Ask

var body: some View {
    VStack(alignment: .leading, spacing: 12) {
        Text("MacDiffusion")
            .font(.title)
            .multilineTextAlignment(.center)
        if (image != nil) {
            image
                .imageScale(.large)
                .foregroundColor(.accentColor)
        }
        if (progressLabel != nil) {
            ProgressView(progressLabel ?? "...", value: progress, total: 1)
        }
        TextField("Enter a prompt", text: $prompt)
        Button("Generate Image") {
            self.generate()
        }
    }
    .padding()
}

GPT Replies

This code defines a View in SwiftUI that contains a vertical stack of views. The stack has left alignment and a spacing of 12 points. The first view in the stack is a text view that displays the string "MacDiffusion" in a large font with centered text.

If the image variable is not nil, the code adds an image view to the stack that displays the image, scales it to a large size, and applies an accent color.

If the progressLabel variable is not nil, the code adds a ProgressView to the stack that displays the progressLabel string, the current progress value, and the total progress value.

The code then adds a text field to the stack that allows the user to enter a prompt, and a button that triggers the generate() function when tapped.

Finally, the code applies padding to the stack to add some space around its edges.

I Ask

How do I call self.generate() when the user presses enter when focused in the textfield?

GPT Replies

You can add a .onCommit modifier to the TextField view and specify the generate() function as the action to perform when the user commits the input. For example:

TextField("Enter a prompt", text: $prompt)
    .onCommit(perform: generate)

This will cause the generate() function to be called whenever the user presses the "enter" key while the text field is focused.

I Ask

Do you mean .onSubmit?

GPT Replies

Yes, you are correct. The .onCommit modifier is used for triggering an action when the user commits a change to the text field, but it does not necessarily trigger when the user presses the "enter" key. Instead, you should use the .onSubmit modifier to trigger an action when the user presses the "enter" key while the text field is focused.

Here is how you can use the .onSubmit modifier to call the generate() function when the user presses the "enter" key while the text field is focused:

TextField("Enter a prompt", text: $prompt)
    .onSubmit(perform: generate)

This will cause the generate() function to be called whenever the user presses the "enter" key while the text field is focused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment