Skip to content

Commit

Permalink
Migrate remaining TextInput tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthaTi committed Jan 16, 2025
1 parent 4b54e1c commit e82172f
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 87 deletions.
14 changes: 14 additions & 0 deletions source/fluid/io/hover.d
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,20 @@ class PointerAction : TreeAction, Publisher!PointerAction, IO {

}

/// Perform a double (`doubleClick`) or triple click (`tripleClick`) using the primary press action.
/// Params:
/// isActive = Trigger input actions (like a mouse release event) if true, emulate holding if false.
/// Returns:
/// True if the action was handled, false if not.
bool doubleClick(bool isActive = true) {
return click(isActive, 2);
}

/// ditto
bool tripleClick(bool isActive = true) {
return click(isActive, 3);
}

alias press = click;

override void beforeDraw(Node node, Rectangle) {
Expand Down
185 changes: 98 additions & 87 deletions tests/nodes/text_input.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ import fluid;

@safe:

Theme testTheme;

static this() {
testTheme = nullTheme.derive(
rule!TextInput(
Rule.selectionBackgroundColor = color("#02a"),
Rule.fontSize = 14.pt,
),
);
}

@("TextInput removes line feeds in single line mode")
unittest {

Expand Down Expand Up @@ -1336,11 +1347,7 @@ unittest {
auto input = textInput();
auto hover = hoverChain();
auto root = testSpace(
nullTheme.derive(
rule!TextInput(
Rule.selectionBackgroundColor = color("#02a"),
),
),
.testTheme,
chain(inputMapChain(), hover, input)
);
input.value = "Hello, World! Foo, bar, scroll this input";
Expand Down Expand Up @@ -1593,19 +1600,11 @@ unittest {

import std.math : isClose;

auto theme = nullTheme.derive(
rule!TextInput(
Rule.selectionBackgroundColor = color("#02a"),
Rule.fontSize = 14.pt,
),
);
auto input = textInput(.multiline, theme);
auto focus = focusChain();
auto input = textInput(.multiline, .testTheme);
auto hover = hoverChain();
auto root = chain(focus, hover, input);
auto root = chain(inputMapChain(), hover, input);

input.value = "Line one\nLine two\n\nLine four";
focus.currentFocus = input;
root.draw();

auto lineHeight = input.style.getTypeface.lineHeight;
Expand Down Expand Up @@ -1650,100 +1649,112 @@ unittest {

}

version (TODO)
@("TextInput: Mouse selections can select words by double clicking")
unittest {

{
// Double click
io.mousePosition = middle;
root._lastClick = SysTime.init;

foreach (i; 0..2) {

io.nextFrame();
io.release();
root.draw();

io.nextFrame();
io.press();
root.draw();

}

assert(root.selectedValue == "Line");
assert(root.selectionStart < root.selectionEnd);
auto input = textInput(.multiline, .testTheme);
auto hover = hoverChain();
auto root = chain(inputMapChain(), hover, input);

// Move it to top row
io.nextFrame();
io.mousePosition = top;
root.draw();
input.value = "Line one\nLine two\n\nLine four";
root.draw();

assert(root.selectedValue == "Line one\nLine");
assert(root.selectionStart > root.selectionEnd);
auto lineHeight = input.style.getTypeface.lineHeight;

// Move it to bottom row
io.nextFrame();
io.mousePosition = bottom;
root.draw();
// Move the caret to second line
input.caretIndex = "Line one\nLin".length;
input.updateCaretPosition();

assert(root.selectedValue == "Line two\n\nLine");
assert(root.selectionStart < root.selectionEnd);
const middle = input.caretPosition;
const top = middle - Vector2(0, lineHeight);
const blank = middle + Vector2(0, lineHeight);
const bottom = middle + Vector2(0, lineHeight * 2);

// And to the blank line
io.nextFrame();
io.mousePosition = blank;
root.draw();
// Double click in the middle
hover.point(middle)
.then((a) {
a.doubleClick(false);
assert(input.selectedValue == "Line");
assert(input.selectionStart < input.selectionEnd);

assert(root.selectedValue == "Line two\n");
assert(root.selectionStart < root.selectionEnd);
// Drag the pointer to top row
return a.move(top);
})
.then((a) {
a.doubleClick(false);
assert(input.selectedValue == "Line one\nLine");
assert(input.selectionStart > input.selectionEnd);

}
// Bottom row
return a.move(bottom);
})
.then((a) {
a.doubleClick(false);
assert(input.selectedValue == "Line two\n\nLine");
assert(input.selectionStart < input.selectionEnd);

{
// And now drag the pointer to the blank line
return a.move(blank);
})
.then((a) {
a.doubleClick(true);
})
.runWhileDrawing(root);

// Triple
io.mousePosition = middle;
root._lastClick = SysTime.init;
assert(input.selectedValue == "Line two\n");
assert(input.selectionStart < input.selectionEnd);

foreach (i; 0..3) {
}

io.nextFrame();
io.release();
root.draw();
@("TextInput: Mouse selections can select words by triple clicking")
unittest {

io.nextFrame();
io.press();
root.draw();
auto input = textInput(.multiline, .testTheme);
auto hover = hoverChain();
auto root = chain(inputMapChain(), hover, input);

}
input.value = "Line one\nLine two\n\nLine four";
root.draw();

assert(root.selectedValue == "Line two");
assert(root.selectionStart < root.selectionEnd);
auto lineHeight = input.style.getTypeface.lineHeight;

// Move it to top row
io.nextFrame();
io.mousePosition = top;
root.draw();
// Move the caret to second line
input.caretIndex = "Line one\nLin".length;
input.updateCaretPosition();

assert(root.selectedValue == "Line one\nLine two");
assert(root.selectionStart > root.selectionEnd);
const middle = input.caretPosition;
const top = middle - Vector2(0, lineHeight);
const blank = middle + Vector2(0, lineHeight);
const bottom = middle + Vector2(0, lineHeight * 2);

// Move it to bottom row
io.nextFrame();
io.mousePosition = bottom;
root.draw();
hover.point(middle)
.then((a) {
a.tripleClick(false);
assert(input.selectedValue == "Line two");
assert(input.selectionStart < input.selectionEnd);

assert(root.selectedValue == "Line two\n\nLine four");
assert(root.selectionStart < root.selectionEnd);
return a.move(top);
})
.then((a) {
a.tripleClick(false);
assert(input.selectedValue == "Line one\nLine two");
assert(input.selectionStart > input.selectionEnd);

// And to the blank line
io.nextFrame();
io.mousePosition = blank;
root.draw();
return a.move(bottom);
})
.then((a) {
a.tripleClick(false);
assert(input.selectedValue == "Line two\n\nLine four");
assert(input.selectionStart < input.selectionEnd);

assert(root.selectedValue == "Line two\n");
assert(root.selectionStart < root.selectionEnd);
return a.move(blank);
})
.then((a) {
a.tripleClick(true);
})
.runWhileDrawing(root);

}
assert(input.selectedValue == "Line two\n");
assert(input.selectionStart < input.selectionEnd);

}

0 comments on commit e82172f

Please sign in to comment.