From 7614127d3641cf3224798c2f0ff07b6ae57d9a53 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 26 Aug 2021 14:41:33 +0700 Subject: Rename `HitTestResult` to `Hit` ... and also move it to a new `text` module in `iced_core` --- core/src/hit_test.rs | 28 ---------------------------- core/src/keyboard.rs | 2 +- core/src/lib.rs | 3 +-- core/src/mouse.rs | 2 +- core/src/text.rs | 29 +++++++++++++++++++++++++++++ glow/src/backend.rs | 7 +++---- glow/src/text.rs | 16 ++++++++-------- graphics/src/backend.rs | 5 +++-- graphics/src/lib.rs | 4 ++-- graphics/src/widget/text.rs | 5 ++--- native/src/lib.rs | 4 ++-- native/src/renderer/null.rs | 8 ++++---- native/src/widget/text.rs | 10 ++++++---- wgpu/src/backend.rs | 7 +++---- wgpu/src/text.rs | 14 +++++++------- 15 files changed, 72 insertions(+), 72 deletions(-) delete mode 100644 core/src/hit_test.rs create mode 100644 core/src/text.rs diff --git a/core/src/hit_test.rs b/core/src/hit_test.rs deleted file mode 100644 index a4a7b610..00000000 --- a/core/src/hit_test.rs +++ /dev/null @@ -1,28 +0,0 @@ -use crate::Vector; - -/// The result of hit testing on text. -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum HitTestResult { - /// The point was within the bounds of the returned character index. - CharOffset(usize), - /// The provided point was not within the bounds of a glyph. The index - /// of the character with the closest centeroid position is returned, - /// as well as its delta. - NearestCharOffset(usize, Vector), -} - -impl HitTestResult { - /// Computes the cursor position corresponding to this [`HitTestResult`] . - pub fn cursor(&self) -> usize { - match self { - HitTestResult::CharOffset(i) => *i, - HitTestResult::NearestCharOffset(i, delta) => { - if delta.x > f32::EPSILON { - i + 1 - } else { - *i - } - } - } - } -} diff --git a/core/src/keyboard.rs b/core/src/keyboard.rs index cb64701a..6827a4db 100644 --- a/core/src/keyboard.rs +++ b/core/src/keyboard.rs @@ -1,4 +1,4 @@ -//! Reuse basic keyboard types. +//! Listen to keyboard events. mod event; mod hotkey; mod key_code; diff --git a/core/src/lib.rs b/core/src/lib.rs index 7c4c3c0c..a0decdab 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -17,12 +17,12 @@ pub mod keyboard; pub mod menu; pub mod mouse; +pub mod text; mod align; mod background; mod color; mod font; -mod hit_test; mod length; mod padding; mod point; @@ -34,7 +34,6 @@ pub use align::{Align, HorizontalAlignment, VerticalAlignment}; pub use background::Background; pub use color::Color; pub use font::Font; -pub use hit_test::HitTestResult; pub use length::Length; pub use menu::Menu; pub use padding::Padding; diff --git a/core/src/mouse.rs b/core/src/mouse.rs index 25ce6ac3..48214f65 100644 --- a/core/src/mouse.rs +++ b/core/src/mouse.rs @@ -1,4 +1,4 @@ -//! Reuse basic mouse types. +//! Handle mouse events. mod button; mod event; mod interaction; diff --git a/core/src/text.rs b/core/src/text.rs new file mode 100644 index 00000000..ded22eef --- /dev/null +++ b/core/src/text.rs @@ -0,0 +1,29 @@ +//! Draw and interact with text. +use crate::Vector; + +/// The result of hit testing on text. +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Hit { + /// The point was within the bounds of the returned character index. + CharOffset(usize), + /// The provided point was not within the bounds of a glyph. The index + /// of the character with the closest centeroid position is returned, + /// as well as its delta. + NearestCharOffset(usize, Vector), +} + +impl Hit { + /// Computes the cursor position corresponding to this [`HitTestResult`] . + pub fn cursor(&self) -> usize { + match self { + Self::CharOffset(i) => *i, + Self::NearestCharOffset(i, delta) => { + if delta.x > f32::EPSILON { + i + 1 + } else { + *i + } + } + } + } +} diff --git a/glow/src/backend.rs b/glow/src/backend.rs index d97cbceb..37c0ac9d 100644 --- a/glow/src/backend.rs +++ b/glow/src/backend.rs @@ -2,14 +2,13 @@ use crate::quad; use crate::text; use crate::triangle; use crate::{Settings, Transformation, Viewport}; + use iced_graphics::backend; use iced_graphics::font; use iced_graphics::Layer; use iced_graphics::Primitive; use iced_native::mouse; -use iced_native::{ - Font, HitTestResult, HorizontalAlignment, Size, VerticalAlignment, -}; +use iced_native::{Font, HorizontalAlignment, Size, VerticalAlignment}; /// A [`glow`] graphics backend for [`iced`]. /// @@ -222,7 +221,7 @@ impl backend::Text for Backend { bounds: Size, point: iced_native::Point, nearest_only: bool, - ) -> HitTestResult { + ) -> text::Hit { self.text_pipeline.hit_test( contents, size, diff --git a/glow/src/text.rs b/glow/src/text.rs index bb882594..d6915d92 100644 --- a/glow/src/text.rs +++ b/glow/src/text.rs @@ -1,9 +1,12 @@ use crate::Transformation; -use glow_glyph::ab_glyph; + use iced_graphics::font; -use iced_native::HitTestResult; + +use glow_glyph::ab_glyph; use std::{cell::RefCell, collections::HashMap}; +pub use iced_native::text::Hit; + #[derive(Debug)] pub struct Pipeline { draw_brush: RefCell, @@ -118,7 +121,7 @@ impl Pipeline { bounds: iced_native::Size, point: iced_native::Point, nearest_only: bool, - ) -> HitTestResult { + ) -> Hit { use glow_glyph::GlyphCruncher; let glow_glyph::FontId(font_id) = self.find_font(font); @@ -179,7 +182,7 @@ impl Pipeline { if !nearest_only { for (idx, bounds) in bounds.clone() { if bounds.contains(point) { - return HitTestResult::CharOffset(char_index(idx)); + return Hit::CharOffset(char_index(idx)); } } } @@ -195,10 +198,7 @@ impl Pipeline { }, ); - HitTestResult::NearestCharOffset( - char_index(idx), - (point - nearest).into(), - ) + Hit::NearestCharOffset(char_index(idx), (point - nearest).into()) } pub fn trim_measurement_cache(&mut self) { diff --git a/graphics/src/backend.rs b/graphics/src/backend.rs index 94124509..656949c5 100644 --- a/graphics/src/backend.rs +++ b/graphics/src/backend.rs @@ -1,7 +1,8 @@ //! Write a graphics backend. use iced_native::image; use iced_native::svg; -use iced_native::{Font, HitTestResult, Point, Size}; +use iced_native::text; +use iced_native::{Font, Point, Size}; /// The graphics backend of a [`Renderer`]. /// @@ -59,7 +60,7 @@ pub trait Text { bounds: Size, point: Point, nearest_only: bool, - ) -> HitTestResult; + ) -> text::Hit; } /// A graphics backend that supports image rendering. diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index 687294fc..14388653 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -40,6 +40,6 @@ pub use transformation::Transformation; pub use viewport::Viewport; pub use iced_native::{ - Background, Color, Font, HitTestResult, HorizontalAlignment, Point, - Rectangle, Size, Vector, VerticalAlignment, + Background, Color, Font, HorizontalAlignment, Point, Rectangle, Size, + Vector, VerticalAlignment, }; diff --git a/graphics/src/widget/text.rs b/graphics/src/widget/text.rs index e1b4e4e8..c235f254 100644 --- a/graphics/src/widget/text.rs +++ b/graphics/src/widget/text.rs @@ -4,8 +4,7 @@ use crate::{Primitive, Renderer}; use iced_native::mouse; use iced_native::text; use iced_native::{ - Color, Font, HitTestResult, HorizontalAlignment, Point, Rectangle, Size, - VerticalAlignment, + Color, Font, HorizontalAlignment, Point, Rectangle, Size, VerticalAlignment, }; /// A paragraph of text. @@ -44,7 +43,7 @@ where bounds: Size, point: Point, nearest_only: bool, - ) -> HitTestResult { + ) -> text::Hit { self.backend().hit_test( content, size, diff --git a/native/src/lib.rs b/native/src/lib.rs index 06bfd3c5..cbb02506 100644 --- a/native/src/lib.rs +++ b/native/src/lib.rs @@ -61,8 +61,8 @@ mod debug; mod debug; pub use iced_core::{ - menu, Align, Background, Color, Font, HitTestResult, HorizontalAlignment, - Length, Menu, Padding, Point, Rectangle, Size, Vector, VerticalAlignment, + menu, Align, Background, Color, Font, HorizontalAlignment, Length, Menu, + Padding, Point, Rectangle, Size, Vector, VerticalAlignment, }; pub use iced_futures::{executor, futures, Command}; diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index 3ae05f10..b1a26c41 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -1,8 +1,8 @@ use crate::{ button, checkbox, column, container, pane_grid, progress_bar, radio, row, scrollable, slider, text, text_input, toggler, Color, Element, Font, - HitTestResult, HorizontalAlignment, Layout, Padding, Point, Rectangle, - Renderer, Size, Vector, VerticalAlignment, + HorizontalAlignment, Layout, Padding, Point, Rectangle, Renderer, Size, + Vector, VerticalAlignment, }; /// A renderer that does nothing. @@ -75,8 +75,8 @@ impl text::Renderer for Null { _bounds: Size, _point: Point, _nearest_only: bool, - ) -> HitTestResult { - HitTestResult::NearestCharOffset(0, Vector::new(0., 0.)) + ) -> text::Hit { + text::Hit::NearestCharOffset(0, Vector::new(0., 0.)) } fn draw( diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index f37f90a9..adf6a74f 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -1,9 +1,11 @@ //! Write some text for your users to read. use crate::{ - layout, Color, Element, Hasher, HitTestResult, HorizontalAlignment, Layout, - Length, Point, Rectangle, Size, VerticalAlignment, Widget, + layout, Color, Element, Hasher, HorizontalAlignment, Layout, Length, Point, + Rectangle, Size, VerticalAlignment, Widget, }; +pub use iced_core::text::Hit; + use std::hash::Hash; /// A paragraph of text. @@ -183,7 +185,7 @@ pub trait Renderer: crate::Renderer { /// laid out with the given parameters, returning information about /// the nearest character. /// - /// If nearest_only is true, the hit test does not consider whether the + /// If `nearest_only` is true, the hit test does not consider whether the /// the point is interior to any glyph bounds, returning only the character /// with the nearest centeroid. fn hit_test( @@ -194,7 +196,7 @@ pub trait Renderer: crate::Renderer { bounds: Size, point: Point, nearest_only: bool, - ) -> HitTestResult; + ) -> Hit; /// Draws a [`Text`] fragment. /// diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs index fe8ed255..b31bf92c 100644 --- a/wgpu/src/backend.rs +++ b/wgpu/src/backend.rs @@ -2,14 +2,13 @@ use crate::quad; use crate::text; use crate::triangle; use crate::{Settings, Transformation}; + use iced_graphics::backend; use iced_graphics::font; use iced_graphics::layer::Layer; use iced_graphics::{Primitive, Viewport}; use iced_native::mouse; -use iced_native::{ - Font, HitTestResult, HorizontalAlignment, Size, VerticalAlignment, -}; +use iced_native::{Font, HorizontalAlignment, Size, VerticalAlignment}; #[cfg(any(feature = "image_rs", feature = "svg"))] use crate::image; @@ -285,7 +284,7 @@ impl backend::Text for Backend { bounds: Size, point: iced_native::Point, nearest_only: bool, - ) -> HitTestResult { + ) -> text::Hit { self.text_pipeline.hit_test( contents, size, diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs index f227cb6f..ee49ee4b 100644 --- a/wgpu/src/text.rs +++ b/wgpu/src/text.rs @@ -1,9 +1,12 @@ use crate::Transformation; + use iced_graphics::font; -use iced_native::HitTestResult; + use std::{cell::RefCell, collections::HashMap}; use wgpu_glyph::ab_glyph; +pub use iced_native::text::Hit; + #[derive(Debug)] pub struct Pipeline { draw_brush: RefCell>, @@ -126,7 +129,7 @@ impl Pipeline { bounds: iced_native::Size, point: iced_native::Point, nearest_only: bool, - ) -> HitTestResult { + ) -> Hit { use wgpu_glyph::GlyphCruncher; let wgpu_glyph::FontId(font_id) = self.find_font(font); @@ -187,7 +190,7 @@ impl Pipeline { if !nearest_only { for (idx, bounds) in bounds.clone() { if bounds.contains(point) { - return HitTestResult::CharOffset(char_index(idx)); + return Hit::CharOffset(char_index(idx)); } } } @@ -203,10 +206,7 @@ impl Pipeline { }, ); - HitTestResult::NearestCharOffset( - char_index(idx), - (point - nearest).into(), - ) + Hit::NearestCharOffset(char_index(idx), (point - nearest).into()) } pub fn trim_measurement_cache(&mut self) { -- cgit