From 2fe6d1e453fe24eaaf2c4b53a1650bc948708349 Mon Sep 17 00:00:00 2001 From: Artha Date: Tue, 22 Oct 2024 08:03:02 +0200 Subject: [PATCH] Re-enable placeholders --- source/fluid/code_input.d | 13 +++++++--- source/fluid/text.d | 4 ++- source/fluid/text_input.d | 51 ++++++++++++++++++++++++++++++++------- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/source/fluid/code_input.d b/source/fluid/code_input.d index c001d58..99ec3fc 100644 --- a/source/fluid/code_input.d +++ b/source/fluid/code_input.d @@ -172,12 +172,14 @@ class CodeInput : TextInput { assert(text.hasFastEdits); auto typeface = style.getTypeface; - typeface.setSize(io.dpi, style.fontSize); - + text.indentWidth = indentWidth * typeface.advance(' ').x / io.hidpiScale.x; text.resize(available); - minSize = text.size; + placeholderText.resize(available); + + minSize.x = max(placeholderText.size.x, text.size.x); + minSize.y = max(placeholderText.size.y, text.size.y); assert(this.text.isMeasured); @@ -186,7 +188,10 @@ class CodeInput : TextInput { override void drawImpl(Rectangle outer, Rectangle inner) { const style = pickStyle(); - text.draw(styles, inner.start); + if (showPlaceholder) + placeholderText.draw(styles, inner.start); + else + text.draw(styles, inner.start); } diff --git a/source/fluid/text.d b/source/fluid/text.d index fa5c9d2..d6b367b 100644 --- a/source/fluid/text.d +++ b/source/fluid/text.d @@ -347,7 +347,9 @@ struct StyledText(StyleRange = TextStyleSlice[]) { /// splitter = Function to use to split the text. Currently unsupported. /// space = Boundaries to fit the text in. /// wrap = Wrap text, on by default. - void resize(alias splitter = Typeface.defaultWordChunks)(Vector2 space, bool wrap = true) { + void resize(alias splitter = Typeface.defaultWordChunks)(Vector2 space, bool wrap = true) + in (node, "Text was not initialized; `node` was not set.") + do { auto style = node.pickStyle; auto typeface = style.getTypeface; diff --git a/source/fluid/text_input.d b/source/fluid/text_input.d index 7009aac..b86e425 100644 --- a/source/fluid/text_input.d +++ b/source/fluid/text_input.d @@ -62,9 +62,6 @@ class TextInput : InputNode!Node, FluidScrollable { /// Size of the field. auto size = Vector2(200, 0); - /// A placeholder text for the field, displayed when the field is empty. Style using `emptyStyle`. - Rope placeholder; - /// Time of the last interaction with the input. SysTime lastTouch; @@ -235,10 +232,10 @@ class TextInput : InputNode!Node, FluidScrollable { import fluid.button; - this.placeholder = placeholder; this.submitted = submitted; this.lastTouch = Clock.currTime; this.contentLabel = new typeof(contentLabel); + this.placeholder = placeholder; // Make single line the default contentLabel.isWrapDisabled = true; @@ -278,8 +275,12 @@ class TextInput : InputNode!Node, FluidScrollable { static class ContentLabel : Label { + bool showPlaceholder; + Text placeholderText; + this() { super(""); + placeholderText = Text(this, ""); } protected inout(Rope) value() inout { @@ -294,11 +295,28 @@ class TextInput : InputNode!Node, FluidScrollable { return false; } + override void resizeImpl(Vector2 space) { + + super.resizeImpl(space); + placeholderText.resize(space, !isWrapDisabled); + + if (placeholderText.size.x > minSize.x) + minSize.x = placeholderText.size.x; + + if (placeholderText.size.y > minSize.y) + minSize.y = placeholderText.size.y; + + } + override void drawImpl(Rectangle outer, Rectangle inner) { // Don't draw background const style = pickStyle(); - text.draw(style, inner.start); + + if (showPlaceholder) + placeholderText.draw(style, inner.start); + else + text.draw(style, inner.start); } @@ -383,6 +401,24 @@ class TextInput : InputNode!Node, FluidScrollable { } + /// Placeholder text that is displayed when the text input is empty. + /// Returns: Placeholder text. + /// Params: + /// value = If given, replace the current placeholder with new one. + Rope placeholder() const { + return contentLabel.placeholderText; + } + + /// ditto + Rope placeholder(Rope value) { + return contentLabel.placeholderText = value; + } + + /// ditto + Rope placeholder(string value) { + return contentLabel.placeholderText = Rope(value); + } + /// Hook called every time a piece of text is replaced. /// Params: /// start = Index at which the change occured. @@ -792,10 +828,6 @@ class TextInput : InputNode!Node, FluidScrollable { // Set the size minSize = size; - // Set the label text - version (none) // TODO TODO TODO - contentLabel.text = value == "" ? placeholder : value; - const isFill = layout.nodeAlign[0] == NodeAlign.fill; _availableWidth = isFill @@ -807,6 +839,7 @@ class TextInput : InputNode!Node, FluidScrollable { : Vector2(0, size.y); // Resize the label, and remove the spacing + contentLabel.showPlaceholder = value == ""; contentLabel.style = pickLabelStyle(style); contentLabel.resize(tree, theme, textArea);