diff --git a/Cargo.toml b/Cargo.toml index 5f8df9a07..dbb829f5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -175,7 +175,7 @@ resolver = "2" [patch.crates-io.kas-text] git = "https://github.com/kas-gui/kas-text.git" -rev = "367ba7e0db5f1b890e3563773cf96c0dd49aac2c" +rev = "9797e78ad4664ae5055e8dab6067e40b215f6b3e" [patch.crates-io.impl-tools-lib] git = "https://github.com/kas-gui/impl-tools.git" diff --git a/crates/kas-core/src/text/display.rs b/crates/kas-core/src/text/display.rs index 052a825ef..2ba497861 100644 --- a/crates/kas-core/src/text/display.rs +++ b/crates/kas-core/src/text/display.rs @@ -311,24 +311,15 @@ impl ConfiguredDisplay { /// Get the base directionality of the text /// - /// This does not require that the text is prepared. + /// This returns the direction inferred during run-breaking if available, + /// falling back to [`Direction::text_is_rtl`]. + #[inline] pub fn text_is_rtl(&self, text: &str) -> bool { - let cached_is_rtl = match self.line_is_rtl(0) { - Ok(None) => Some(self.direction == Direction::Rtl), - Ok(Some(is_rtl)) => Some(is_rtl), - Err(NotReady) => None, - }; - - #[cfg(not(debug_assertions))] - if let Some(cached) = cached_is_rtl { - return cached; - } - - let is_rtl = self.unchecked_display().text_is_rtl(text, self.direction()); - if let Some(cached) = cached_is_rtl { - debug_assert_eq!(cached, is_rtl); + if self.status >= Status::ResizeLevelRuns { + self.display.text_is_rtl() + } else { + self.direction.text_is_rtl(text) } - is_rtl } /// Get the status of preparation diff --git a/crates/kas-widgets/src/edit/edit_box.rs b/crates/kas-widgets/src/edit/edit_box.rs index fed9c33d5..1a1f00431 100644 --- a/crates/kas-widgets/src/edit/edit_box.rs +++ b/crates/kas-widgets/src/edit/edit_box.rs @@ -393,6 +393,10 @@ impl EditBox> { impl EditBox { /// Set the base text direction (inline) + /// + /// If [`Direction::Auto`] or [`Direction::AutoRtl`] is used, the direction + /// will be updated on edit to persist the last used text direction to + /// non-directional content. #[inline] pub fn with_direction(mut self, direction: Direction) -> Self { self.inner.set_direction(direction); diff --git a/crates/kas-widgets/src/edit/edit_field.rs b/crates/kas-widgets/src/edit/edit_field.rs index a2f9b9ee2..c9c00c03a 100644 --- a/crates/kas-widgets/src/edit/edit_field.rs +++ b/crates/kas-widgets/src/edit/edit_field.rs @@ -305,6 +305,10 @@ impl EditBoxCore> { impl EditBoxCore { /// Set the base text direction + /// + /// If [`Direction::Auto`] or [`Direction::AutoRtl`] is used, the direction + /// will be updated on edit to persist the last used text direction to + /// non-directional content. #[inline] pub fn set_direction(&mut self, direction: Direction) { self.editor.set_direction(direction); diff --git a/crates/kas-widgets/src/edit/editor.rs b/crates/kas-widgets/src/edit/editor.rs index bfbe135fa..47ba178f0 100644 --- a/crates/kas-widgets/src/edit/editor.rs +++ b/crates/kas-widgets/src/edit/editor.rs @@ -222,6 +222,10 @@ impl Component { } /// Set the base text direction + /// + /// If [`Direction::Auto`] or [`Direction::AutoRtl`] is used, the direction + /// will be updated on edit to persist the last used text direction to + /// non-directional content. #[inline] pub fn set_direction(&mut self, direction: Direction) { self.0.part.set_direction(direction); @@ -370,6 +374,10 @@ impl Part { } /// Set the base text direction + /// + /// If [`Direction::Auto`] or [`Direction::AutoRtl`] is used, the direction + /// will be updated on edit to persist the last used text direction to + /// non-directional content. #[inline] pub fn set_direction(&mut self, direction: Direction) { self.direction = direction; @@ -398,28 +406,11 @@ impl Part { /// Get the base directionality of the text /// - /// This does not require that the text is prepared. + /// [`Self::configure`] should be called before this method. #[inline] pub fn text_is_rtl(&self) -> bool { - let mut cached_is_rtl = None; - if self.status >= Status::Wrapped { - cached_is_rtl = match self.display.line_is_rtl(0) { - None => Some(self.direction == Direction::Rtl), - Some(is_rtl) => Some(is_rtl), - }; - }; - - #[cfg(not(debug_assertions))] - if let Some(cached) = cached_is_rtl { - return cached; - } - - let text = self.as_str(); - let is_rtl = self.display.text_is_rtl(text, self.direction); - if let Some(cached) = cached_is_rtl { - debug_assert_eq!(cached, is_rtl); - } - is_rtl + debug_assert!(self.status >= Status::ResizeLevelRuns); + self.display.text_is_rtl() } /// Access the cursor index / selection range @@ -484,6 +475,14 @@ impl Part { } part.status = Status::LevelRuns; + + if part.direction.is_auto() { + part.direction = if dbg!(part.display.text_is_rtl()) { + Direction::AutoRtl + } else { + Direction::Auto + }; + } } if self.status < Status::LevelRuns {