Scoop API

scoop:scoop.ui.component:0.1.0

platform/desktop/native/controllers/file_button.scoop

Selected production source · platform/desktop/native/controllers/file_button.scoop

1package scoop.ui.component
3import scoop.platform.fs.Path
4import scoop.ui.*
5import scoop.ui.style.ThemeSource
6import scoop.window.*
8public enum NativeFileRequest {
9 Open(OpenFileOptions), Save(SaveFileOptions)
12public class NativeFileButtonProps public constructor(
13 public val content: ButtonContent,
14 public val request: NativeFileRequest,
15 public val target: DialogTarget,
16 public val onResult: (FileDialogResult<Path>) -> Unit,
17 public val enabled: Boolean = true,
18 public val style: ButtonStyle = buttonStyle(),
19 public val theme: ThemeSource? = None,
20 public val focus: FocusOptions = FocusOptions(),
21 public val invalid: Boolean = false,
22 public val variant: ButtonVariant = ButtonVariant.Default,
23 public val size: ButtonSize = ButtonSize.Default,
24 public val group: GroupPosition? = None,
25 public val labelTarget: FieldTarget? = None,
26 public val semantics: ComponentSemantics = ComponentSemantics()
29public class NativeFileButton public constructor(
30 private val props: Props<NativeFileButtonProps>,
31 scope: ComponentScope
32): Component(scope) {
33 private val controller = scope.fileDialogController()
34 private val completed: (FileDialogResult<Path>) -> Unit =
35 { result -> props.value.onResult(result) }
36 private val click: () -> Unit =
37 {
38 val input = props.value
39 if (input.enabled && !controller.isPending)
40 when (input.request) {
41 case Open(val options) -> {
42 controller.openFile(input.target, completed, options)
43 }
44 case Save(val options) -> {
45 controller.saveFile(input.target, completed, options)
46 }
47 }
48 }
49 public override fun render(): View {
50 val input = props.value
51 return component(
52 ButtonProps(
53 input.content,
54 click,
55 input.enabled && !controller.isPending,
56 input.style,
57 input.theme,
58 input.focus,
59 input.invalid,
60 input.variant,
61 input.size,
62 input.group,
63 input.labelTarget,
64 input.semantics
65 ),
66 { current, mount -> Button(current, mount) },
67 comparison = Some(buttonComparison)
68 )
69 }
72public class NativeOpenFileButtonBuilder internal constructor(): ButtonBuilder() {
73 public var options: OpenFileOptions = OpenFileOptions()
76public class NativeSaveFileButtonBuilder internal constructor(): ButtonBuilder() {
77 public var options: SaveFileOptions = SaveFileOptions()
80public fun nativeOpenFileButton(
81 text: String,
82 target: DialogTarget,
83 onResult: (FileDialogResult<Path>) -> Unit,
84 configure: NativeOpenFileButtonBuilder.() -> Unit = {}
85): View {
86 val builder = NativeOpenFileButtonBuilder()
87 builder.configure()
88 return nativeFileButton(
89 text,
90 target,
91 onResult,
92 NativeFileRequest.Open(builder.options),
93 builder
94 )
97public fun nativeSaveFileButton(
98 text: String,
99 target: DialogTarget,
100 onResult: (FileDialogResult<Path>) -> Unit,
101 configure: NativeSaveFileButtonBuilder.() -> Unit = {}
102): View {
103 val builder = NativeSaveFileButtonBuilder()
104 builder.configure()
105 return nativeFileButton(
106 text,
107 target,
108 onResult,
109 NativeFileRequest.Save(builder.options),
110 builder
111 )
114private fun nativeFileButton(
115 text: String,
116 target: DialogTarget,
117 onResult: (FileDialogResult<Path>) -> Unit,
118 request: NativeFileRequest,
119 builder: ButtonBuilder
120): View =
121 component(
122 NativeFileButtonProps(
123 ButtonContent.Label(LabelProps(LabelText.Plain(text, None), builder.fonts)),
124 request,
125 target,
126 onResult,
127 builder.enabled,
128 builder.style,
129 builder.theme,
130 builder.focus,
131 builder.invalid,
132 builder.variant,
133 builder.size,
134 builder.group,
135 builder.labelTarget,
136 builder.semantics
137 ),
138 { props, scope -> NativeFileButton(props, scope) },
139 builder.key
140 )