portico/Sources/Example/ClipboardDemo.swift

33 lines
1.2 KiB
Swift

import Portico
/// Demonstrates ``Clipboard``: an `Entry`-backed compose field feeds a Copy
/// button, a Paste button reads the clipboard back into `@State`, and a
/// reactive `Label` displays ``Clipboard/changeCount`` live - including
/// changes made by other applications - with no subscription of any kind.
struct ClipboardDemoPage: View {
@Environment(\.clipboard) private var clipboard
@State private var composeText = "Hello from Portico"
@State private var pasted: String?
var body: some View {
VStack(spacing: 12) {
Label("Clipboard demo")
.title1()
Entry()
.text($composeText)
.placeholderText("Type something to copy")
HStack(spacing: 8) {
Button("Copy") { clipboard.setText(composeText) }
.suggestedAction()
Button("Paste") { Task { pasted = try? await clipboard.text() } }
}
Label { "Pasted: \(pasted ?? "(nothing yet)")" }
.dimmed()
Label { "Clipboard changed \(clipboard.changeCount) time(s)" }
.dimmed()
}
.margin(24)
.hexpand(true)
.vexpand(true)
}
}