Skip to content

Commit

Permalink
ModelEntry for TextArea
Browse files Browse the repository at this point in the history
Refer to issue: msink#8

Co-authored-by: Mervyn McCreight <[email protected]>
  • Loading branch information
andreas-mausch and mervyn-mccreight committed Nov 11, 2018
1 parent b08cd35 commit cd49b05
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
15 changes: 13 additions & 2 deletions libui-ktx/src/main/kotlin/widgets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ class SearchField(modelEntry: ModelEntry<String>) : TextField(uiNewSearchEntry()
* @param[readonly] specifies that a text should be read-only */
inline fun Container.textarea(
wrap: Boolean = true,
modelEntry: ModelEntry<String> = ModelEntry(""),
init: TextArea.() -> Unit = {}
): TextArea = add(TextArea(wrap)
): TextArea = add(TextArea(modelEntry, wrap)
.apply(init))

/** Wrapper class for [uiMultilineEntry] - a multiline plain text editing widget */
class TextArea(wrap: Boolean = true) : Control<uiMultilineEntry>(
class TextArea(val modelEntry: ModelEntry<String>, wrap: Boolean = true) : Control<uiMultilineEntry>(
if (wrap) uiNewMultilineEntry() else uiNewNonWrappingMultilineEntry()) {

/** The current text in the area. */
Expand All @@ -132,6 +133,16 @@ class TextArea(wrap: Boolean = true) : Control<uiMultilineEntry>(
get() = uiMultilineEntryReadOnly(ptr) != 0
set(readonly) = uiMultilineEntrySetReadOnly(ptr, if (readonly) 1 else 0)

init {
this.value = modelEntry.get()
modelEntry.addListener({ newValue -> this.value = newValue })
uiMultilineEntryOnChanged(ptr, staticCFunction { _, ref ->
with(ref.to<TextArea>()) {
this.modelEntry.update(this.value)
}
}, ref.asCPointer())
}

/** Adds the text to the end of the area. */
fun append(text: String) = uiMultilineEntryAppend(ptr, text)

Expand Down
14 changes: 11 additions & 3 deletions samples/data-binding-2/src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fun main(args: Array<String>) {
val textFieldModel = ModelEntry<String>("TextField")
val passwordFieldModel = ModelEntry<String>("PasswordField")
val searchFieldModel = ModelEntry<String>("SearchField")
val textAreaModel = ModelEntry<String>("TextArea")
}

val model = Model()
Expand All @@ -14,20 +15,22 @@ fun main(args: Array<String>) {
val updateValues: ChangeListener<String> = {
values.value = "${model.textFieldModel.value}\n" +
"${model.passwordFieldModel.value}\n" +
"${model.searchFieldModel.value}"
"${model.searchFieldModel.value}\n" +
"${model.textAreaModel.value}"
}

model.textFieldModel.addListener(updateValues)
model.passwordFieldModel.addListener(updateValues)
model.searchFieldModel.addListener(updateValues)
model.textAreaModel.addListener(updateValues)

appWindow(
title = "Data-binding Example #2",
width = 640,
height = 480
) {
vbox {
group("Entries") { stretchy = true }.form {
group("Controls") { stretchy = true }.form {
textfield(modelEntry = model.textFieldModel) {
label = "TextField"
}
Expand All @@ -39,7 +42,12 @@ fun main(args: Array<String>) {
searchfield(modelEntry = model.searchFieldModel) {
label = "SearchField"
}

textarea(modelEntry = model.textAreaModel) {
label = "TextArea"
}
}
separator()
group("Values") { stretchy = true }.form {
values = textarea() {
label = "Values"
stretchy = true
Expand Down

0 comments on commit cd49b05

Please sign in to comment.