Rename HitTestResult to Hit

... and also move it to a new `text` module in `iced_core`
This commit is contained in:
Héctor Ramón Jiménez 2021-08-26 14:41:33 +07:00
parent aa63841e2c
commit 7614127d36
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
14 changed files with 48 additions and 48 deletions

View file

@ -1,4 +1,4 @@
//! Reuse basic keyboard types. //! Listen to keyboard events.
mod event; mod event;
mod hotkey; mod hotkey;
mod key_code; mod key_code;

View file

@ -17,12 +17,12 @@
pub mod keyboard; pub mod keyboard;
pub mod menu; pub mod menu;
pub mod mouse; pub mod mouse;
pub mod text;
mod align; mod align;
mod background; mod background;
mod color; mod color;
mod font; mod font;
mod hit_test;
mod length; mod length;
mod padding; mod padding;
mod point; mod point;
@ -34,7 +34,6 @@ pub use align::{Align, HorizontalAlignment, VerticalAlignment};
pub use background::Background; pub use background::Background;
pub use color::Color; pub use color::Color;
pub use font::Font; pub use font::Font;
pub use hit_test::HitTestResult;
pub use length::Length; pub use length::Length;
pub use menu::Menu; pub use menu::Menu;
pub use padding::Padding; pub use padding::Padding;

View file

@ -1,4 +1,4 @@
//! Reuse basic mouse types. //! Handle mouse events.
mod button; mod button;
mod event; mod event;
mod interaction; mod interaction;

View file

@ -1,8 +1,9 @@
//! Draw and interact with text.
use crate::Vector; use crate::Vector;
/// The result of hit testing on text. /// The result of hit testing on text.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum HitTestResult { pub enum Hit {
/// The point was within the bounds of the returned character index. /// The point was within the bounds of the returned character index.
CharOffset(usize), CharOffset(usize),
/// The provided point was not within the bounds of a glyph. The index /// The provided point was not within the bounds of a glyph. The index
@ -11,12 +12,12 @@ pub enum HitTestResult {
NearestCharOffset(usize, Vector), NearestCharOffset(usize, Vector),
} }
impl HitTestResult { impl Hit {
/// Computes the cursor position corresponding to this [`HitTestResult`] . /// Computes the cursor position corresponding to this [`HitTestResult`] .
pub fn cursor(&self) -> usize { pub fn cursor(&self) -> usize {
match self { match self {
HitTestResult::CharOffset(i) => *i, Self::CharOffset(i) => *i,
HitTestResult::NearestCharOffset(i, delta) => { Self::NearestCharOffset(i, delta) => {
if delta.x > f32::EPSILON { if delta.x > f32::EPSILON {
i + 1 i + 1
} else { } else {

View file

@ -2,14 +2,13 @@ use crate::quad;
use crate::text; use crate::text;
use crate::triangle; use crate::triangle;
use crate::{Settings, Transformation, Viewport}; use crate::{Settings, Transformation, Viewport};
use iced_graphics::backend; use iced_graphics::backend;
use iced_graphics::font; use iced_graphics::font;
use iced_graphics::Layer; use iced_graphics::Layer;
use iced_graphics::Primitive; use iced_graphics::Primitive;
use iced_native::mouse; use iced_native::mouse;
use iced_native::{ use iced_native::{Font, HorizontalAlignment, Size, VerticalAlignment};
Font, HitTestResult, HorizontalAlignment, Size, VerticalAlignment,
};
/// A [`glow`] graphics backend for [`iced`]. /// A [`glow`] graphics backend for [`iced`].
/// ///
@ -222,7 +221,7 @@ impl backend::Text for Backend {
bounds: Size, bounds: Size,
point: iced_native::Point, point: iced_native::Point,
nearest_only: bool, nearest_only: bool,
) -> HitTestResult { ) -> text::Hit {
self.text_pipeline.hit_test( self.text_pipeline.hit_test(
contents, contents,
size, size,

View file

@ -1,9 +1,12 @@
use crate::Transformation; use crate::Transformation;
use glow_glyph::ab_glyph;
use iced_graphics::font; use iced_graphics::font;
use iced_native::HitTestResult;
use glow_glyph::ab_glyph;
use std::{cell::RefCell, collections::HashMap}; use std::{cell::RefCell, collections::HashMap};
pub use iced_native::text::Hit;
#[derive(Debug)] #[derive(Debug)]
pub struct Pipeline { pub struct Pipeline {
draw_brush: RefCell<glow_glyph::GlyphBrush>, draw_brush: RefCell<glow_glyph::GlyphBrush>,
@ -118,7 +121,7 @@ impl Pipeline {
bounds: iced_native::Size, bounds: iced_native::Size,
point: iced_native::Point, point: iced_native::Point,
nearest_only: bool, nearest_only: bool,
) -> HitTestResult { ) -> Hit {
use glow_glyph::GlyphCruncher; use glow_glyph::GlyphCruncher;
let glow_glyph::FontId(font_id) = self.find_font(font); let glow_glyph::FontId(font_id) = self.find_font(font);
@ -179,7 +182,7 @@ impl Pipeline {
if !nearest_only { if !nearest_only {
for (idx, bounds) in bounds.clone() { for (idx, bounds) in bounds.clone() {
if bounds.contains(point) { if bounds.contains(point) {
return HitTestResult::CharOffset(char_index(idx)); return Hit::CharOffset(char_index(idx));
} }
} }
} }
@ -195,10 +198,7 @@ impl Pipeline {
}, },
); );
HitTestResult::NearestCharOffset( Hit::NearestCharOffset(char_index(idx), (point - nearest).into())
char_index(idx),
(point - nearest).into(),
)
} }
pub fn trim_measurement_cache(&mut self) { pub fn trim_measurement_cache(&mut self) {

View file

@ -1,7 +1,8 @@
//! Write a graphics backend. //! Write a graphics backend.
use iced_native::image; use iced_native::image;
use iced_native::svg; 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`]. /// The graphics backend of a [`Renderer`].
/// ///
@ -59,7 +60,7 @@ pub trait Text {
bounds: Size, bounds: Size,
point: Point, point: Point,
nearest_only: bool, nearest_only: bool,
) -> HitTestResult; ) -> text::Hit;
} }
/// A graphics backend that supports image rendering. /// A graphics backend that supports image rendering.

View file

@ -40,6 +40,6 @@ pub use transformation::Transformation;
pub use viewport::Viewport; pub use viewport::Viewport;
pub use iced_native::{ pub use iced_native::{
Background, Color, Font, HitTestResult, HorizontalAlignment, Point, Background, Color, Font, HorizontalAlignment, Point, Rectangle, Size,
Rectangle, Size, Vector, VerticalAlignment, Vector, VerticalAlignment,
}; };

View file

@ -4,8 +4,7 @@ use crate::{Primitive, Renderer};
use iced_native::mouse; use iced_native::mouse;
use iced_native::text; use iced_native::text;
use iced_native::{ use iced_native::{
Color, Font, HitTestResult, HorizontalAlignment, Point, Rectangle, Size, Color, Font, HorizontalAlignment, Point, Rectangle, Size, VerticalAlignment,
VerticalAlignment,
}; };
/// A paragraph of text. /// A paragraph of text.
@ -44,7 +43,7 @@ where
bounds: Size, bounds: Size,
point: Point, point: Point,
nearest_only: bool, nearest_only: bool,
) -> HitTestResult { ) -> text::Hit {
self.backend().hit_test( self.backend().hit_test(
content, content,
size, size,

View file

@ -61,8 +61,8 @@ mod debug;
mod debug; mod debug;
pub use iced_core::{ pub use iced_core::{
menu, Align, Background, Color, Font, HitTestResult, HorizontalAlignment, menu, Align, Background, Color, Font, HorizontalAlignment, Length, Menu,
Length, Menu, Padding, Point, Rectangle, Size, Vector, VerticalAlignment, Padding, Point, Rectangle, Size, Vector, VerticalAlignment,
}; };
pub use iced_futures::{executor, futures, Command}; pub use iced_futures::{executor, futures, Command};

View file

@ -1,8 +1,8 @@
use crate::{ use crate::{
button, checkbox, column, container, pane_grid, progress_bar, radio, row, button, checkbox, column, container, pane_grid, progress_bar, radio, row,
scrollable, slider, text, text_input, toggler, Color, Element, Font, scrollable, slider, text, text_input, toggler, Color, Element, Font,
HitTestResult, HorizontalAlignment, Layout, Padding, Point, Rectangle, HorizontalAlignment, Layout, Padding, Point, Rectangle, Renderer, Size,
Renderer, Size, Vector, VerticalAlignment, Vector, VerticalAlignment,
}; };
/// A renderer that does nothing. /// A renderer that does nothing.
@ -75,8 +75,8 @@ impl text::Renderer for Null {
_bounds: Size, _bounds: Size,
_point: Point, _point: Point,
_nearest_only: bool, _nearest_only: bool,
) -> HitTestResult { ) -> text::Hit {
HitTestResult::NearestCharOffset(0, Vector::new(0., 0.)) text::Hit::NearestCharOffset(0, Vector::new(0., 0.))
} }
fn draw( fn draw(

View file

@ -1,9 +1,11 @@
//! Write some text for your users to read. //! Write some text for your users to read.
use crate::{ use crate::{
layout, Color, Element, Hasher, HitTestResult, HorizontalAlignment, Layout, layout, Color, Element, Hasher, HorizontalAlignment, Layout, Length, Point,
Length, Point, Rectangle, Size, VerticalAlignment, Widget, Rectangle, Size, VerticalAlignment, Widget,
}; };
pub use iced_core::text::Hit;
use std::hash::Hash; use std::hash::Hash;
/// A paragraph of text. /// A paragraph of text.
@ -183,7 +185,7 @@ pub trait Renderer: crate::Renderer {
/// laid out with the given parameters, returning information about /// laid out with the given parameters, returning information about
/// the nearest character. /// 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 /// the point is interior to any glyph bounds, returning only the character
/// with the nearest centeroid. /// with the nearest centeroid.
fn hit_test( fn hit_test(
@ -194,7 +196,7 @@ pub trait Renderer: crate::Renderer {
bounds: Size, bounds: Size,
point: Point, point: Point,
nearest_only: bool, nearest_only: bool,
) -> HitTestResult; ) -> Hit;
/// Draws a [`Text`] fragment. /// Draws a [`Text`] fragment.
/// ///

View file

@ -2,14 +2,13 @@ use crate::quad;
use crate::text; use crate::text;
use crate::triangle; use crate::triangle;
use crate::{Settings, Transformation}; use crate::{Settings, Transformation};
use iced_graphics::backend; use iced_graphics::backend;
use iced_graphics::font; use iced_graphics::font;
use iced_graphics::layer::Layer; use iced_graphics::layer::Layer;
use iced_graphics::{Primitive, Viewport}; use iced_graphics::{Primitive, Viewport};
use iced_native::mouse; use iced_native::mouse;
use iced_native::{ use iced_native::{Font, HorizontalAlignment, Size, VerticalAlignment};
Font, HitTestResult, HorizontalAlignment, Size, VerticalAlignment,
};
#[cfg(any(feature = "image_rs", feature = "svg"))] #[cfg(any(feature = "image_rs", feature = "svg"))]
use crate::image; use crate::image;
@ -285,7 +284,7 @@ impl backend::Text for Backend {
bounds: Size, bounds: Size,
point: iced_native::Point, point: iced_native::Point,
nearest_only: bool, nearest_only: bool,
) -> HitTestResult { ) -> text::Hit {
self.text_pipeline.hit_test( self.text_pipeline.hit_test(
contents, contents,
size, size,

View file

@ -1,9 +1,12 @@
use crate::Transformation; use crate::Transformation;
use iced_graphics::font; use iced_graphics::font;
use iced_native::HitTestResult;
use std::{cell::RefCell, collections::HashMap}; use std::{cell::RefCell, collections::HashMap};
use wgpu_glyph::ab_glyph; use wgpu_glyph::ab_glyph;
pub use iced_native::text::Hit;
#[derive(Debug)] #[derive(Debug)]
pub struct Pipeline { pub struct Pipeline {
draw_brush: RefCell<wgpu_glyph::GlyphBrush<()>>, draw_brush: RefCell<wgpu_glyph::GlyphBrush<()>>,
@ -126,7 +129,7 @@ impl Pipeline {
bounds: iced_native::Size, bounds: iced_native::Size,
point: iced_native::Point, point: iced_native::Point,
nearest_only: bool, nearest_only: bool,
) -> HitTestResult { ) -> Hit {
use wgpu_glyph::GlyphCruncher; use wgpu_glyph::GlyphCruncher;
let wgpu_glyph::FontId(font_id) = self.find_font(font); let wgpu_glyph::FontId(font_id) = self.find_font(font);
@ -187,7 +190,7 @@ impl Pipeline {
if !nearest_only { if !nearest_only {
for (idx, bounds) in bounds.clone() { for (idx, bounds) in bounds.clone() {
if bounds.contains(point) { if bounds.contains(point) {
return HitTestResult::CharOffset(char_index(idx)); return Hit::CharOffset(char_index(idx));
} }
} }
} }
@ -203,10 +206,7 @@ impl Pipeline {
}, },
); );
HitTestResult::NearestCharOffset( Hit::NearestCharOffset(char_index(idx), (point - nearest).into())
char_index(idx),
(point - nearest).into(),
)
} }
pub fn trim_measurement_cache(&mut self) { pub fn trim_measurement_cache(&mut self) {