gpui: Add closest_index_for_position
method (#23668)
Closes #ISSUE
Release Notes:
- N/A
------------
I just make a little change to improve `index_for_position` to support
return closest index for position.
I need this method to measure for position cursor in multi-line mode
TextInput.
https://github.com/longbridge/gpui-component/pull/583
https://github.com/user-attachments/assets/c69d098e-d2cb-4053-b739-6c7dd666e769
Before this change, GPUI have `LineLayout::closest_index_for_x` method
for unwrapped line case.
d1be419fff/crates/gpui/src/text_system/line_layout.rs (L58-L94)
This change is equivalent to making `index_for_position` have a
corresponding method to get the closest index like `index_for_x`.
This commit is contained in:
parent
cb15753694
commit
e1af35aa15
1 changed files with 34 additions and 4 deletions
|
@ -278,10 +278,34 @@ impl WrappedLineLayout {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The index corresponding to a given position in this layout for the given line height.
|
/// The index corresponding to a given position in this layout for the given line height.
|
||||||
|
///
|
||||||
|
/// See also [`Self::closest_index_for_position`].
|
||||||
pub fn index_for_position(
|
pub fn index_for_position(
|
||||||
|
&self,
|
||||||
|
position: Point<Pixels>,
|
||||||
|
line_height: Pixels,
|
||||||
|
) -> Result<usize, usize> {
|
||||||
|
self._index_for_position(position, line_height, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The closest index to a given position in this layout for the given line height.
|
||||||
|
///
|
||||||
|
/// Closest means the character boundary closest to the given position.
|
||||||
|
///
|
||||||
|
/// See also [`LineLayout::closest_index_for_x`].
|
||||||
|
pub fn closest_index_for_position(
|
||||||
|
&self,
|
||||||
|
position: Point<Pixels>,
|
||||||
|
line_height: Pixels,
|
||||||
|
) -> Result<usize, usize> {
|
||||||
|
self._index_for_position(position, line_height, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _index_for_position(
|
||||||
&self,
|
&self,
|
||||||
mut position: Point<Pixels>,
|
mut position: Point<Pixels>,
|
||||||
line_height: Pixels,
|
line_height: Pixels,
|
||||||
|
closest: bool,
|
||||||
) -> Result<usize, usize> {
|
) -> Result<usize, usize> {
|
||||||
let wrapped_line_ix = (position.y / line_height) as usize;
|
let wrapped_line_ix = (position.y / line_height) as usize;
|
||||||
|
|
||||||
|
@ -320,6 +344,11 @@ impl WrappedLineLayout {
|
||||||
Err(wrapped_line_start_index)
|
Err(wrapped_line_start_index)
|
||||||
} else if position_in_unwrapped_line.x >= wrapped_line_end_x {
|
} else if position_in_unwrapped_line.x >= wrapped_line_end_x {
|
||||||
Err(wrapped_line_end_index)
|
Err(wrapped_line_end_index)
|
||||||
|
} else {
|
||||||
|
if closest {
|
||||||
|
Ok(self
|
||||||
|
.unwrapped_layout
|
||||||
|
.closest_index_for_x(position_in_unwrapped_line.x))
|
||||||
} else {
|
} else {
|
||||||
Ok(self
|
Ok(self
|
||||||
.unwrapped_layout
|
.unwrapped_layout
|
||||||
|
@ -327,6 +356,7 @@ impl WrappedLineLayout {
|
||||||
.unwrap())
|
.unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the pixel position for the given byte index.
|
/// Returns the pixel position for the given byte index.
|
||||||
pub fn position_for_index(&self, index: usize, line_height: Pixels) -> Option<Point<Pixels>> {
|
pub fn position_for_index(&self, index: usize, line_height: Pixels) -> Option<Point<Pixels>> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue