Skip to content

Commit

Permalink
Re-enable placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthaTi committed Oct 22, 2024
1 parent cc656ed commit 2fe6d1e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
13 changes: 9 additions & 4 deletions source/fluid/code_input.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

}

Expand Down
4 changes: 3 additions & 1 deletion source/fluid/text.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
51 changes: 42 additions & 9 deletions source/fluid/text_input.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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);

}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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);

Expand Down

0 comments on commit 2fe6d1e

Please sign in to comment.