From 85c6872e6898eb9e8bddb0fed25495b041cde061 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 3 Feb 2025 15:03:31 +0800 Subject: [PATCH] input: Use `closest_index_for_position` from GPUI. (#593) --- Cargo.lock | 55 ++++++++------------ crates/ui/src/input/element.rs | 4 +- crates/ui/src/input/input.rs | 93 +--------------------------------- 3 files changed, 26 insertions(+), 126 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f0a5ae1f..ad6e458c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -474,7 +474,7 @@ dependencies = [ "bitflags 2.6.0", "cexpr", "clang-sys", - "itertools 0.13.0", + "itertools 0.11.0", "log", "prettyplease", "proc-macro2", @@ -952,7 +952,7 @@ dependencies = [ [[package]] name = "collections" version = "0.1.0" -source = "git+https://github.com/huacnlee/zed.git?branch=webview#7071d1a9c22776da144c051907c7d3210d12d29c" +source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046" dependencies = [ "indexmap 2.7.1", "rustc-hash 2.1.0", @@ -1316,7 +1316,7 @@ dependencies = [ [[package]] name = "derive_refineable" version = "0.1.0" -source = "git+https://github.com/huacnlee/zed.git?branch=webview#7071d1a9c22776da144c051907c7d3210d12d29c" +source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046" dependencies = [ "proc-macro2", "quote", @@ -2275,7 +2275,7 @@ dependencies = [ [[package]] name = "gpui" version = "0.1.0" -source = "git+https://github.com/huacnlee/zed.git?branch=webview#7071d1a9c22776da144c051907c7d3210d12d29c" +source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046" dependencies = [ "anyhow", "as-raw-xcb-connection", @@ -2309,8 +2309,8 @@ dependencies = [ "gpui_macros", "http_client", "image", + "inventory", "itertools 0.14.0", - "linkme", "log", "lyon", "media", @@ -2363,7 +2363,7 @@ dependencies = [ [[package]] name = "gpui_macros" version = "0.1.0" -source = "git+https://github.com/huacnlee/zed.git?branch=webview#7071d1a9c22776da144c051907c7d3210d12d29c" +source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046" dependencies = [ "proc-macro2", "quote", @@ -2553,7 +2553,7 @@ dependencies = [ [[package]] name = "http_client" version = "0.1.0" -source = "git+https://github.com/huacnlee/zed.git?branch=webview#7071d1a9c22776da144c051907c7d3210d12d29c" +source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046" dependencies = [ "anyhow", "bytes", @@ -2717,6 +2717,15 @@ dependencies = [ "syn 2.0.96", ] +[[package]] +name = "inventory" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54b12ebb6799019b044deaf431eadfe23245b259bba5a2c0796acec3943a3cdb" +dependencies = [ + "rustversion", +] + [[package]] name = "io-lifetimes" version = "1.0.11" @@ -2961,26 +2970,6 @@ version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64804cc6a5042d4f05379909ba25b503ec04e2c082151d62122d5dcaa274b961" -[[package]] -name = "linkme" -version = "0.3.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70fe496a7af8c406f877635cbf3cd6a9fac9d6f443f58691cd8afe6ce0971af4" -dependencies = [ - "linkme-impl", -] - -[[package]] -name = "linkme-impl" -version = "0.3.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b01f197a15988fb5b2ec0a5a9800c97e70771499c456ad757d63b3c5e9b96e75" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.96", -] - [[package]] name = "linux-raw-sys" version = "0.3.8" @@ -3131,7 +3120,7 @@ dependencies = [ [[package]] name = "media" version = "0.1.0" -source = "git+https://github.com/huacnlee/zed.git?branch=webview#7071d1a9c22776da144c051907c7d3210d12d29c" +source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046" dependencies = [ "anyhow", "bindgen", @@ -3491,7 +3480,7 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56" dependencies = [ - "proc-macro-crate 1.3.1", + "proc-macro-crate 3.2.0", "proc-macro2", "quote", "syn 2.0.96", @@ -4486,7 +4475,7 @@ dependencies = [ [[package]] name = "refineable" version = "0.1.0" -source = "git+https://github.com/huacnlee/zed.git?branch=webview#7071d1a9c22776da144c051907c7d3210d12d29c" +source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046" dependencies = [ "derive_refineable", ] @@ -4814,7 +4803,7 @@ checksum = "d369a96f978623eb3dc28807c4852d6cc617fed53da5d3c400feff1ef34a714a" [[package]] name = "semantic_version" version = "0.1.0" -source = "git+https://github.com/huacnlee/zed.git?branch=webview#7071d1a9c22776da144c051907c7d3210d12d29c" +source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046" dependencies = [ "anyhow", "serde", @@ -5253,7 +5242,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "sum_tree" version = "0.1.0" -source = "git+https://github.com/huacnlee/zed.git?branch=webview#7071d1a9c22776da144c051907c7d3210d12d29c" +source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046" dependencies = [ "arrayvec", "log", @@ -5938,7 +5927,7 @@ checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" [[package]] name = "util" version = "0.1.0" -source = "git+https://github.com/huacnlee/zed.git?branch=webview#7071d1a9c22776da144c051907c7d3210d12d29c" +source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046" dependencies = [ "anyhow", "async-fs 2.1.2", diff --git a/crates/ui/src/input/element.rs b/crates/ui/src/input/element.rs index baf78cec..bb0853c7 100644 --- a/crates/ui/src/input/element.rs +++ b/crates/ui/src/input/element.rs @@ -1,7 +1,7 @@ use gpui::{ fill, point, px, relative, size, App, Bounds, Corners, Element, ElementId, ElementInputHandler, Entity, GlobalElementId, IntoElement, LayoutId, MouseButton, MouseMoveEvent, PaintQuad, Path, - Pixels, Point, Style, TextRun, UnderlineStyle, Window, WrappedLine, + Pixels, Point, Style, TextAlign, TextRun, UnderlineStyle, Window, WrappedLine, }; use smallvec::SmallVec; @@ -514,7 +514,7 @@ impl Element for TextElement { } for line in prepaint.lines.iter() { let p = point(origin.x, origin.y + offset_y); - _ = line.paint(p, line_height, window, cx); + _ = line.paint(p, line_height, TextAlign::Left, window, cx); offset_y += line.size(line_height).height; } diff --git a/crates/ui/src/input/input.rs b/crates/ui/src/input/input.rs index 11211cbd..2138fef6 100644 --- a/crates/ui/src/input/input.rs +++ b/crates/ui/src/input/input.rs @@ -19,7 +19,6 @@ use gpui::{ }; // TODO: -// - Press Up,Down to move cursor up, down line if multi-line // - Move cursor to skip line eof empty chars. use super::blink_cursor::BlinkCursor; @@ -1081,94 +1080,6 @@ impl TextInput { } } - // Fork of `closest_index_for_x` method from `gpui::text::Layout` - // https://github.com/zed-industries/zed/blob/d1be419fff415329b38f26aff90488700702c82a/crates/gpui/src/text_system/line_layout.rs#L74 - fn closest_index_for_x(&self, line: &WrappedLine, x: Pixels) -> usize { - let mut prev_index = 0; - let mut prev_x = px(0.); - - for run in line.unwrapped_layout.runs.iter() { - for glyph in run.glyphs.iter() { - if glyph.position.x >= x { - if glyph.position.x - x < x - prev_x { - return glyph.index; - } else { - return prev_index; - } - } - prev_index = glyph.index; - prev_x = glyph.position.x; - } - } - - // HOTFIX: - // - // Wait https://github.com/zed-industries/zed/pull/23603 - if line.unwrapped_layout.len == 1 { - if x > line.width() / 2.0 { - return 1; - } - return 0; - } - - line.unwrapped_layout.len - } - - /// Get the closest index for position - /// - /// Fork: - /// https://github.com/zed-industries/zed/blob/1c322c0f2daa50f46644832a6a1cdf15bcf921ea/crates/gpui/src/text_system/line_layout.rs#L272 - /// - /// Wait PR https://github.com/zed-industries/zed/pull/23668 - pub fn closest_index_for_position( - &self, - line: &WrappedLine, - position: Point, - line_height: Pixels, - ) -> Result { - let wrapped_line_ix = (position.y / line_height) as usize; - - let wrapped_line_start_index; - let wrapped_line_start_x; - if wrapped_line_ix > 0 { - let Some(line_start_boundary) = line.wrap_boundaries.get(wrapped_line_ix - 1) else { - return Err(0); - }; - let run = &line.unwrapped_layout.runs[line_start_boundary.run_ix]; - let glyph = &run.glyphs[line_start_boundary.glyph_ix]; - wrapped_line_start_index = glyph.index; - wrapped_line_start_x = glyph.position.x; - } else { - wrapped_line_start_index = 0; - wrapped_line_start_x = Pixels::ZERO; - }; - - let wrapped_line_end_index; - let wrapped_line_end_x; - if wrapped_line_ix < line.wrap_boundaries.len() { - let next_wrap_boundary_ix = wrapped_line_ix; - let next_wrap_boundary = line.wrap_boundaries[next_wrap_boundary_ix]; - let run = &line.unwrapped_layout.runs[next_wrap_boundary.run_ix]; - let glyph = &run.glyphs[next_wrap_boundary.glyph_ix]; - wrapped_line_end_index = glyph.index; - wrapped_line_end_x = glyph.position.x; - } else { - wrapped_line_end_index = line.unwrapped_layout.len; - wrapped_line_end_x = line.unwrapped_layout.width; - }; - - let mut position_in_unwrapped_line = position; - position_in_unwrapped_line.x += wrapped_line_start_x; - if position_in_unwrapped_line.x < wrapped_line_start_x { - Err(wrapped_line_start_index) - } else if position_in_unwrapped_line.x >= wrapped_line_end_x { - Err(wrapped_line_end_index) - } else { - // HOTFIX - Ok(self.closest_index_for_x(&line, position_in_unwrapped_line.x)) - } - } - fn index_for_mouse_position( &self, position: Point, @@ -1205,14 +1116,14 @@ impl TextInput { for line in lines.iter() { let line_origin = self.line_origin_with_y_offset(&mut y_offset, &line, line_height); let pos = inner_position - line_origin; - let closest_index = self.closest_index_for_x(line, pos.x); + let closest_index = line.unwrapped_layout.closest_index_for_x(pos.x); // Return offset by use closest_index_for_x if is single line mode. if self.is_single_line() { return closest_index; } - let index_result = self.closest_index_for_position(line, pos, line_height); + let index_result = line.closest_index_for_position(pos, line_height); if let Ok(v) = index_result { index += v; break;