Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TextInput widget hold horizontal scrolling position #2048

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 56 additions & 21 deletions widget/src/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub use cursor::Cursor;
pub use value::Value;

use editor::Editor;
use num_traits::clamp;
use std::cell::RefCell;

use crate::core::alignment;
use crate::core::event::{self, Event};
Expand Down Expand Up @@ -561,18 +563,23 @@ where
None
};

state.is_focused = if click_position.is_some() {
state.is_focused.or_else(|| {
let now = Instant::now();
if click_position.is_some() {
if !state.is_focused() {
state.cursor.move_to(0);

Some(Focus {
updated_at: now,
now,
is_window_focused: true,
})
})
state.is_focused = {
let now = Instant::now();

Some(Focus {
updated_at: now,
now,
is_window_focused: true,
horizontal_scroll_offset: RefCell::default(),
})
};
}
} else {
None
state.is_focused = None;
};

if let Some(cursor_position) = click_position {
Expand Down Expand Up @@ -1056,8 +1063,10 @@ pub fn draw<Renderer>(
value,
size,
position,
*focus.horizontal_scroll_offset.borrow(),
font,
);
*focus.horizontal_scroll_offset.borrow_mut() = offset;

let is_cursor_visible = ((focus.now - focus.updated_at)
.as_millis()
Expand Down Expand Up @@ -1097,6 +1106,7 @@ pub fn draw<Renderer>(
value,
size,
left,
*focus.horizontal_scroll_offset.borrow(),
font,
);

Expand All @@ -1107,9 +1117,17 @@ pub fn draw<Renderer>(
value,
size,
right,
*focus.horizontal_scroll_offset.borrow(),
font,
);

let offset = if end == right {
right_offset
} else {
left_offset
};
*focus.horizontal_scroll_offset.borrow_mut() = offset;

let width = right_position - left_position;

(
Expand All @@ -1127,11 +1145,7 @@ pub fn draw<Renderer>(
},
theme.selection_color(style),
)),
if end == right {
right_offset
} else {
left_offset
},
offset,
)
}
}
Expand Down Expand Up @@ -1211,14 +1225,14 @@ pub struct State {
last_click: Option<mouse::Click>,
cursor: Cursor,
keyboard_modifiers: keyboard::Modifiers,
// TODO: Add stateful horizontal scrolling offset
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone)]
struct Focus {
updated_at: Instant,
now: Instant,
is_window_focused: bool,
horizontal_scroll_offset: RefCell<f32>,
}

impl State {
Expand Down Expand Up @@ -1257,6 +1271,7 @@ impl State {
updated_at: now,
now,
is_window_focused: true,
horizontal_scroll_offset: RefCell::default(),
});

self.move_cursor_to_end();
Expand Down Expand Up @@ -1343,7 +1358,7 @@ fn offset<Renderer>(
where
Renderer: text::Renderer,
{
if state.is_focused() {
if let Some(focus) = state.is_focused.as_ref() {
let cursor = state.cursor();

let focus_position = match cursor.state(value) {
Expand All @@ -1357,6 +1372,7 @@ where
value,
size,
focus_position,
*focus.horizontal_scroll_offset.borrow(),
font,
);

Expand All @@ -1372,23 +1388,41 @@ fn measure_cursor_and_scroll_offset<Renderer>(
value: &Value,
size: f32,
cursor_index: usize,
horizontal_scroll_offset: f32,
font: Renderer::Font,
) -> (f32, f32)
where
Renderer: text::Renderer,
{
let text_before_cursor = value.until(cursor_index).to_string();

let text_value_width = renderer.measure_width(
let width_before_cursor = renderer.measure_width(
&text_before_cursor,
size,
font,
text::Shaping::Advanced,
);

let offset = ((text_value_width + 5.0) - text_bounds.width).max(0.0);
let width_all = renderer.measure_width(
&value.to_string(),
size,
font,
text::Shaping::Advanced,
);

let max = clamp(
width_before_cursor,
0.0,
(width_all - text_bounds.width + CURSOR_RIGHT_SCROLL_PADDING).max(0.0),
);

let min = (width_before_cursor - text_bounds.width
+ CURSOR_RIGHT_SCROLL_PADDING)
.max(0.0);

let offset = clamp(horizontal_scroll_offset, min, max);

(text_value_width, offset)
(width_before_cursor, offset)
}

/// Computes the position of the text cursor at the given X coordinate of
Expand Down Expand Up @@ -1435,3 +1469,4 @@ where
}

const CURSOR_BLINK_INTERVAL_MILLIS: u128 = 500;
const CURSOR_RIGHT_SCROLL_PADDING: f32 = 5.0;