use crabshell_reactive::Signal;
use super::{Counter, rect_at};
use super::{Harness, node_at};
use crate::{
Button, Clipboard, Column, Content, CursorShape, Icon, Key, KeyEvent, Modifiers, Point,
PointerButton, Rect, Row, ScrollView, Span, Style, Text, TextInput, Window,
};
fn copied(window: &Window) -> Option<String> {
let ctrl_c = KeyEvent {
key: Key::Character('c'),
text: None,
modifiers: Modifiers {
ctrl: true,
..Modifiers::default()
},
repeat: false,
};
match window.key(&ctrl_c) {
Some(Clipboard::Copy(text)) => Some(text),
_ => None,
}
}
fn field(h: &mut Harness, text: Signal<String>) -> Window {
let row = Row::new().child(TextInput::new(text).style(Style::new().width(100.0)));
let window = h.window(h.panel().child(row), 200.0, 30.0);
window.keyboard_enter();
window
}
fn click(window: &Window, x: f32, time: u32) {
window.pointer_motion_at(Point::new(x, 10.0), time);
window.pointer_button_at(PointerButton::Left, true, time);
window.pointer_button_at(PointerButton::Left, false, time + 10);
}
#[test]
fn dragging_in_a_field_selects_to_the_pointer_even_outside_it() {
let mut h = Harness::new();
let text = h.cx.signal(String::from("abcdef"));
let window = field(&mut h, text);
window.pointer_motion_at(Point::new(1.0, 10.0), 0);
window.pointer_button_at(PointerButton::Left, true, 0);
assert_eq!(copied(&window), None, "a press only places the caret");
window.pointer_motion_at(Point::new(190.0, 25.0), 20);
h.frame(&window);
assert_eq!(copied(&window).as_deref(), Some("abcdef"));
window.pointer_motion_at(Point::new(1.0, 10.0), 40);
assert_eq!(copied(&window), None);
window.pointer_motion_at(Point::new(190.0, 10.0), 60);
window.pointer_button_at(PointerButton::Left, false, 80);
window.pointer_motion_at(Point::new(1.0, 10.0), 100);
assert_eq!(copied(&window).as_deref(), Some("abcdef"));
}
#[test]
fn dragging_a_long_field_scrolls_its_text() {
let mut h = Harness::new();
let text = h.cx.signal("w".repeat(60));
let window = field(&mut h, text);
click(&window, 1.0, 0);
window.key(&KeyEvent {
key: Key::Home,
text: None,
modifiers: Modifiers::default(),
repeat: false,
});
h.frame(&window);
window.pointer_motion_at(Point::new(1.0, 10.0), 0);
window.pointer_button_at(PointerButton::Left, true, 1000);
for step in 1..40 {
window.pointer_motion_at(Point::new(150.0, 10.0), 1000 + step * 10);
h.frame(&window);
}
window.pointer_button_at(PointerButton::Left, false, 1500);
assert_eq!(copied(&window), Some("w".repeat(60)));
}
#[test]
fn a_double_click_selects_the_word_under_the_pointer() {
let mut h = Harness::new();
let text = h.cx.signal(String::from("one two"));
let window = field(&mut h, text);
let x = (1..100)
.map(|x| x as f32)
.find(|&x| {
click(&window, x, 10_000 * x as u32);
window.field().is_some_and(|field| field.cursor == 5)
})
.expect("a place inside the second word");
click(&window, x, 0);
click(&window, x, 200);
assert_eq!(copied(&window).as_deref(), Some("two"));
click(&window, x, 1000);
click(&window, x, 1600);
assert_eq!(copied(&window), None);
}
#[test]
fn shift_click_extends_the_selection() {
let mut h = Harness::new();
let text = h.cx.signal(String::from("abcdef"));
let window = field(&mut h, text);
click(&window, 1.0, 0);
window.set_modifiers(Modifiers {
shift: true,
..Modifiers::default()
});
click(&window, 99.0, 1000);
assert_eq!(copied(&window).as_deref(), Some("abcdef"));
window.set_modifiers(Modifiers::default());
click(&window, 99.0, 2000);
assert_eq!(copied(&window), None, "a plain click drops it");
}
#[test]
fn dragging_a_field_in_a_scroll_view_selects_instead_of_scrolling() {
let mut h = Harness::new();
let text = h.cx.signal(String::from("abcdef"));
let offset = h.cx.signal(0.0_f32);
let content = Column::new()
.child(TextInput::new(text).style(Style::new().width(100.0)))
.child(Icon::new("x").size(200));
let view = ScrollView::new(content).offset(offset);
let window = h.window(h.panel().child(view), 200.0, 30.0);
window.keyboard_enter();
let field = node_at(window.view().root(), &[0, 0, 0]).rect();
window.pointer_motion_at(Point::new(field.x + 1.0, field.y + 5.0), 0);
window.pointer_button_at(PointerButton::Left, true, 0);
window.pointer_motion_at(Point::new(field.x + 1.0, field.y + 25.0), 50);
window.pointer_motion_at(Point::new(150.0, 29.0), 100);
window.pointer_button_at(PointerButton::Left, false, 150);
h.frame(&window);
assert_eq!(offset.get(), 0.0);
assert_eq!(copied(&window).as_deref(), Some("abcdef"));
}
fn label(h: &mut Harness, child: impl crate::Widget) -> Window {
let window = h.window(h.panel().child(Row::new().child(child)), 200.0, 30.0);
window.keyboard_enter();
window
}
fn drag(window: &Window, from: Point, to: Point, time: u32) {
window.pointer_motion_at(from, time);
window.pointer_button_at(PointerButton::Left, true, time);
window.pointer_motion_at(to, time + 10);
window.pointer_button_at(PointerButton::Left, false, time + 20);
}
fn selection(window: &Window, path: &[usize]) -> Vec<Rect> {
match node_at(window.view().root(), path).content() {
Content::Text(text) => text.selection(),
_ => panic!("not a text"),
}
}
#[test]
fn a_selectable_text_selects_by_dragging_and_copies_while_focused() {
let mut h = Harness::new();
let window = label(&mut h, Text::new("hello world").selectable(true));
window.pointer_motion(Point::new(5.0, 10.0));
assert_eq!(window.cursor(), CursorShape::Text);
drag(&window, Point::new(1.0, 10.0), Point::new(190.0, 10.0), 0);
h.frame(&window);
assert_eq!(
window.focused().as_deref(),
Some("PanelWindow[DP-1]/Row/Text[0]")
);
assert_eq!(copied(&window).as_deref(), Some("hello world"));
let text = rect_at(&window, &[0, 0]);
let shown = selection(&window, &[0, 0]);
assert_eq!(shown.len(), 1);
assert!(shown[0].width > text.width / 2.0, "{shown:?} in {text:?}");
}
#[test]
fn a_text_selects_nothing_unless_it_is_selectable() {
let mut h = Harness::new();
let on = h.cx.signal(false);
let clicks = Counter::default();
let c = clicks.clone();
let button =
Button::new(Text::new("hello").selectable(move || on.get())).on_click(move |_| c.hit());
let window = label(&mut h, button);
drag(&window, Point::new(1.0, 10.0), Point::new(3.0, 10.0), 0);
assert_eq!(clicks.get(), 1, "the press goes to the button");
assert_eq!(window.focused(), None);
on.set(true);
h.frame(&window);
drag(
&window,
Point::new(1.0, 10.0),
Point::new(190.0, 10.0),
1000,
);
assert_eq!(clicks.get(), 1);
assert_eq!(copied(&window).as_deref(), Some("hello"));
on.set(false);
h.frame(&window);
assert_eq!(window.focused(), None);
assert!(selection(&window, &[0, 0, 0]).is_empty());
}
#[test]
fn a_press_elsewhere_or_a_new_text_drops_the_selection() {
let mut h = Harness::new();
let words = h.cx.signal(String::from("hello world"));
let row = Row::new()
.child(Text::new(move || words.get()).selectable(true))
.child(Icon::new("x").size(20));
let window = label(&mut h, row);
let icon = rect_at(&window, &[0, 0, 1]);
drag(&window, Point::new(1.0, 10.0), Point::new(190.0, 10.0), 0);
assert_eq!(copied(&window).as_deref(), Some("hello world"));
window.pointer_motion_at(Point::new(icon.x + 5.0, 10.0), 1000);
window.pointer_button_at(PointerButton::Left, true, 1000);
window.pointer_button_at(PointerButton::Left, false, 1010);
h.frame(&window);
assert_eq!(window.focused(), None);
assert!(selection(&window, &[0, 0, 0]).is_empty());
drag(
&window,
Point::new(1.0, 10.0),
Point::new(190.0, 10.0),
2000,
);
words.set(String::from("bye bye bye bye"));
h.frame(&window);
assert_eq!(copied(&window), None);
}
#[test]
fn tab_skips_a_selectable_text() {
let mut h = Harness::new();
let text = h.cx.signal(String::new());
let row = Row::new()
.child(Text::new("label").selectable(true))
.child(TextInput::new(text));
let window = label(&mut h, row);
window.key(&KeyEvent {
key: Key::Tab,
text: None,
modifiers: Modifiers::default(),
repeat: false,
});
assert_eq!(
window.focused().as_deref(),
Some("PanelWindow[DP-1]/Row/Row[0]/TextInput[1]")
);
}
#[test]
fn a_selection_runs_across_the_lines_of_a_text() {
let mut h = Harness::new();
let window = label(&mut h, Text::new("one\ntwo").max_lines(2));
assert!(selection(&window, &[0, 0]).is_empty());
let window = label(&mut h, Text::new("one\ntwo").max_lines(2).selectable(true));
let text = rect_at(&window, &[0, 0]);
drag(
&window,
Point::new(text.x + 1.0, text.y + 2.0),
Point::new(190.0, text.bottom() - 2.0),
0,
);
h.frame(&window);
assert_eq!(copied(&window).as_deref(), Some("one\ntwo"));
let shown = selection(&window, &[0, 0]);
assert_eq!(shown.len(), 2, "{shown:?}");
assert!(shown[1].y > shown[0].y);
let second = Point::new(text.x + 5.0, text.bottom() - 4.0);
for time in [5000, 5100] {
window.pointer_motion_at(second, time);
window.pointer_button_at(PointerButton::Left, true, time);
window.pointer_button_at(PointerButton::Left, false, time + 10);
}
assert_eq!(copied(&window).as_deref(), Some("two"));
assert_eq!(selection(&window, &[0, 0]).len(), 1, "the first line isn't");
}
#[test]
fn shift_click_after_new_text_extends_from_its_start() {
let mut h = Harness::new();
let words = h.cx.signal(String::from("hello world"));
let window = label(&mut h, Text::new(move || words.get()).selectable(true));
drag(&window, Point::new(1.0, 10.0), Point::new(190.0, 10.0), 0);
assert_eq!(copied(&window).as_deref(), Some("hello world"));
words.set(String::from("bye bye bye bye"));
h.frame(&window);
window.set_modifiers(Modifiers {
shift: true,
..Modifiers::default()
});
click(&window, 1.0, 1000);
assert_eq!(copied(&window), None);
}
#[test]
fn a_double_click_in_a_password_selects_all_of_it() {
let mut h = Harness::new();
let text = h.cx.signal(String::from("ab cd"));
let row = Row::new().child(
TextInput::new(text)
.password(true)
.style(Style::new().width(100.0)),
);
let window = h.window(h.panel().child(row), 200.0, 30.0);
window.keyboard_enter();
click(&window, 99.0, 0);
click(&window, 99.0, 100);
window.key(&KeyEvent {
key: Key::Character('x'),
text: Some(String::from("x")),
modifiers: Modifiers::default(),
repeat: false,
});
assert_eq!(text.get(), "x", "where its spaces are stays hidden");
}
#[test]
fn a_span_change_that_keeps_the_text_keeps_the_selection() {
let mut h = Harness::new();
let bold = h.cx.signal(false);
let words = h.cx.signal(String::from("world"));
let spans = move || {
let word = Span::new(words.get());
vec![
Span::new("hello "),
if bold.get() { word.bold() } else { word },
]
};
let window = label(&mut h, Text::rich(spans).selectable(true));
drag(&window, Point::new(1.0, 10.0), Point::new(190.0, 10.0), 0);
assert_eq!(copied(&window).as_deref(), Some("hello world"));
bold.set(true);
h.frame(&window);
assert_eq!(copied(&window).as_deref(), Some("hello world"));
assert_eq!(selection(&window, &[0, 0]).len(), 1);
words.set(String::from("there, everyone"));
h.frame(&window);
assert_eq!(copied(&window), None);
}
fn presses(window: &Window, at: Point, times: &[u32], hold: bool) {
for (i, &time) in times.iter().enumerate() {
window.pointer_motion_at(at, time);
window.pointer_button_at(PointerButton::Left, true, time);
if !(hold && i == times.len() - 1) {
window.pointer_button_at(PointerButton::Left, false, time + 10);
}
}
}
fn x_of(window: &Window, at: usize) -> f32 {
(1..100)
.map(|x| x as f32)
.find(|&x| {
click(window, x, 10_000 * x as u32);
window.field().is_some_and(|field| field.cursor == at)
})
.expect("a place for the byte")
}
#[test]
fn a_triple_click_selects_the_whole_field_or_text_line() {
let mut h = Harness::new();
let text = h.cx.signal(String::from("one two three"));
let window = field(&mut h, text);
let x = x_of(&window, 5);
presses(&window, Point::new(x, 10.0), &[1_000_000, 1_000_100], false);
assert_eq!(copied(&window).as_deref(), Some("two"));
presses(&window, Point::new(x, 10.0), &[1_000_200], false);
assert_eq!(copied(&window).as_deref(), Some("one two three"));
presses(&window, Point::new(x, 10.0), &[1_000_300], false);
assert_eq!(copied(&window), None);
presses(
&window,
Point::new(x, 10.0),
&[2_000_000, 2_000_100, 2_000_600],
false,
);
assert_eq!(copied(&window), None);
let window = label(
&mut h,
Text::new("one two\nthree four")
.max_lines(2)
.selectable(true),
);
let rect = rect_at(&window, &[0, 0]);
let second = Point::new(rect.x + 3.0, rect.bottom() - 4.0);
presses(&window, second, &[0, 100, 200], false);
assert_eq!(copied(&window).as_deref(), Some("three four"));
}
#[test]
fn a_drag_after_a_double_click_extends_by_whole_words() {
let mut h = Harness::new();
let text = h.cx.signal(String::from("one two three"));
let window = field(&mut h, text);
let (two, three, one) = (x_of(&window, 5), x_of(&window, 10), x_of(&window, 1));
presses(
&window,
Point::new(two, 10.0),
&[1_000_000, 1_000_100],
true,
);
window.pointer_motion_at(Point::new(three, 10.0), 1_000_150);
assert_eq!(copied(&window).as_deref(), Some("two three"));
window.pointer_motion_at(Point::new(one, 10.0), 1_000_200);
assert_eq!(copied(&window).as_deref(), Some("one two"));
window.pointer_button_at(PointerButton::Left, false, 1_000_250);
window.pointer_motion_at(Point::new(two, 10.0), 2_000_000);
window.pointer_button_at(PointerButton::Left, true, 2_000_000);
window.pointer_motion_at(Point::new(three, 10.0), 2_000_050);
window.pointer_button_at(PointerButton::Left, false, 2_000_100);
assert_eq!(copied(&window).as_deref(), Some("wo th"));
}
#[test]
fn an_empty_line_inside_a_selection_shows_half_an_em() {
let mut h = Harness::new();
let window = label(
&mut h,
Text::new("one\n\ntwo").max_lines(3).selectable(true),
);
let text = rect_at(&window, &[0, 0]);
let line = text.height / 3.0;
drag(
&window,
Point::new(text.x, text.y + 2.0),
Point::new(text.x, text.y + line + 2.0),
0,
);
h.frame(&window);
assert_eq!(copied(&window).as_deref(), Some("one\n"));
assert_eq!(
selection(&window, &[0, 0]).len(),
1,
"its break isn't selected"
);
drag(
&window,
Point::new(text.x, text.y + 2.0),
Point::new(190.0, text.bottom() - 2.0),
1000,
);
h.frame(&window);
assert_eq!(copied(&window).as_deref(), Some("one\n\ntwo"));
let shown = selection(&window, &[0, 0]);
assert_eq!(shown.len(), 3, "{shown:?}");
let first = shown[0];
assert_eq!(
shown[1],
Rect::new(first.x, first.bottom(), 6.5, first.height)
);
}
#[test]
fn a_triple_click_ends_a_line_at_every_break_cosmic_text_makes() {
let mut h = Harness::new();
for (text, lines) in [
("one\r\ntwo", ["one", "two"]),
("one\rtwo", ["one", "two"]),
("one\n\rtwo", ["one", "two"]),
] {
let window = h.window(
h.panel().child(
Row::new()
.align(crate::Align::Start)
.child(Text::new(text).max_lines(2).selectable(true)),
),
200.0,
60.0,
);
window.keyboard_enter();
let rect = rect_at(&window, &[0, 0]);
let first = Point::new(rect.x + 3.0, rect.y + 4.0);
let second = Point::new(rect.x + 3.0, rect.bottom() - 4.0);
presses(&window, first, &[0, 100, 200], false);
assert_eq!(copied(&window).as_deref(), Some(lines[0]), "{text:?}");
presses(&window, second, &[1000, 1100, 1200], false);
assert_eq!(copied(&window).as_deref(), Some(lines[1]), "{text:?}");
}
}