diff options
Diffstat (limited to '')
| -rw-r--r-- | graphics/Cargo.toml | 1 | ||||
| -rw-r--r-- | graphics/src/layer.rs | 45 | ||||
| -rw-r--r-- | graphics/src/lib.rs | 1 | ||||
| -rw-r--r-- | graphics/src/viewport.rs | 47 | ||||
| -rw-r--r-- | graphics/src/window.rs | 3 | ||||
| -rw-r--r-- | graphics/src/window/compositor.rs (renamed from native/src/window/compositor.rs) | 10 | 
6 files changed, 54 insertions, 53 deletions
| diff --git a/graphics/Cargo.toml b/graphics/Cargo.toml index 61f1f6d4..12ad3f14 100644 --- a/graphics/Cargo.toml +++ b/graphics/Cargo.toml @@ -13,6 +13,7 @@ font-icons = []  [dependencies]  bytemuck = "1.2"  glam = "0.8" +raw-window-handle = "0.3"  [dependencies.iced_native]  version = "0.2" diff --git a/graphics/src/layer.rs b/graphics/src/layer.rs index ae9c6ce0..916b5c83 100644 --- a/graphics/src/layer.rs +++ b/graphics/src/layer.rs @@ -7,7 +7,7 @@ use crate::{  };  pub struct Layer<'a> { -    pub bounds: Rectangle<u32>, +    pub bounds: Rectangle,      pub quads: Vec<Quad>,      pub meshes: Vec<Mesh<'a>>,      pub text: Vec<Text<'a>>, @@ -15,7 +15,7 @@ pub struct Layer<'a> {  }  impl<'a> Layer<'a> { -    pub fn new(bounds: Rectangle<u32>) -> Self { +    pub fn new(bounds: Rectangle) -> Self {          Self {              bounds,              quads: Vec::new(), @@ -26,14 +26,8 @@ impl<'a> Layer<'a> {      }      pub fn overlay(lines: &'a [impl AsRef<str>], viewport: &Viewport) -> Self { -        let (width, height) = viewport.dimensions(); - -        let mut overlay = Layer::new(Rectangle { -            x: 0, -            y: 0, -            width, -            height, -        }); +        let mut overlay = +            Layer::new(Rectangle::with_size(viewport.logical_size()));          for (i, line) in lines.iter().enumerate() {              let text = Text { @@ -61,28 +55,14 @@ impl<'a> Layer<'a> {          overlay      } -    pub(crate) fn intersection( -        &self, -        rectangle: Rectangle, -    ) -> Option<Rectangle<u32>> { -        let layer_bounds: Rectangle<f32> = self.bounds.into(); - -        layer_bounds.intersection(&rectangle).map(Into::into) -    } -      pub fn generate(          primitive: &'a Primitive,          viewport: &Viewport,      ) -> Vec<Self> { -        let mut layers = Vec::new(); -        let (width, height) = viewport.dimensions(); +        let first_layer = +            Layer::new(Rectangle::with_size(viewport.logical_size())); -        layers.push(Layer::new(Rectangle { -            x: 0, -            y: 0, -            width, -            height, -        })); +        let mut layers = vec![first_layer];          Self::process_primitive(&mut layers, Vector::new(0.0, 0.0), primitive); @@ -156,11 +136,11 @@ impl<'a> Layer<'a> {                  );                  // Only draw visible content -                if let Some(clip_bounds) = layer.intersection(bounds) { +                if let Some(clip_bounds) = layer.bounds.intersection(&bounds) {                      layer.meshes.push(Mesh {                          origin: Point::new(translation.x, translation.y),                          buffers, -                        clip_bounds: clip_bounds.into(), +                        clip_bounds,                      });                  }              } @@ -170,12 +150,13 @@ impl<'a> Layer<'a> {                  content,              } => {                  let layer = layers.last_mut().unwrap(); +                let translated_bounds = *bounds + translation;                  // Only draw visible content                  if let Some(clip_bounds) = -                    layer.intersection(*bounds + translation) +                    layer.bounds.intersection(&translated_bounds)                  { -                    let clip_layer = Layer::new(clip_bounds.into()); +                    let clip_layer = Layer::new(clip_bounds);                      let new_layer = Layer::new(layer.bounds);                      layers.push(clip_layer); @@ -236,7 +217,7 @@ pub struct Quad {  pub struct Mesh<'a> {      pub origin: Point,      pub buffers: &'a triangle::Mesh2D, -    pub clip_bounds: Rectangle<u32>, +    pub clip_bounds: Rectangle<f32>,  }  #[derive(Debug, Clone, Copy)] diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs index 5ab333c7..2de9d21c 100644 --- a/graphics/src/lib.rs +++ b/graphics/src/lib.rs @@ -10,6 +10,7 @@ pub mod backend;  pub mod font;  pub mod layer;  pub mod triangle; +pub mod window;  #[doc(no_inline)]  pub use widget::*; diff --git a/graphics/src/viewport.rs b/graphics/src/viewport.rs index 48e5a249..745ef339 100644 --- a/graphics/src/viewport.rs +++ b/graphics/src/viewport.rs @@ -1,33 +1,48 @@ -use crate::Transformation; +use crate::{Size, Transformation};  /// A viewing region for displaying computer graphics.  #[derive(Debug)]  pub struct Viewport { -    width: u32, -    height: u32, -    transformation: Transformation, +    physical_size: Size<u32>, +    logical_size: Size<f32>, +    scale_factor: f64, +    projection: Transformation,  }  impl Viewport { -    /// Creates a new [`Viewport`] with the given dimensions. -    pub fn new(width: u32, height: u32) -> Viewport { +    /// Creates a new [`Viewport`] with the given physical dimensions and scale +    /// factor. +    /// +    /// [`Viewport`]: struct.Viewport.html +    pub fn with_physical_size(size: Size<u32>, scale_factor: f64) -> Viewport {          Viewport { -            width, -            height, -            transformation: Transformation::orthographic(width, height), +            physical_size: size, +            logical_size: Size::new( +                (size.width as f64 / scale_factor) as f32, +                (size.height as f64 / scale_factor) as f32, +            ), +            scale_factor, +            projection: Transformation::orthographic(size.width, size.height),          }      } -    pub fn height(&self) -> u32 { -        self.height +    pub fn physical_size(&self) -> Size<u32> { +        self.physical_size      } -    /// Returns the dimensions of the [`Viewport`]. -    pub fn dimensions(&self) -> (u32, u32) { -        (self.width, self.height) +    pub fn physical_height(&self) -> u32 { +        self.physical_size.height      } -    pub fn transformation(&self) -> Transformation { -        self.transformation +    pub fn logical_size(&self) -> Size<f32> { +        self.logical_size +    } + +    pub fn scale_factor(&self) -> f64 { +        self.scale_factor +    } + +    pub fn projection(&self) -> Transformation { +        self.projection      }  } diff --git a/graphics/src/window.rs b/graphics/src/window.rs new file mode 100644 index 00000000..a7c8911b --- /dev/null +++ b/graphics/src/window.rs @@ -0,0 +1,3 @@ +mod compositor; + +pub use compositor::Compositor; diff --git a/native/src/window/compositor.rs b/graphics/src/window/compositor.rs index ae010c89..82faa6e1 100644 --- a/native/src/window/compositor.rs +++ b/graphics/src/window/compositor.rs @@ -1,5 +1,5 @@ -use crate::mouse; - +use crate::Viewport; +use iced_native::mouse;  use raw_window_handle::HasRawWindowHandle;  /// A graphics compositor that can draw to windows. @@ -8,7 +8,7 @@ pub trait Compositor: Sized {      type Settings: Default + Clone;      /// The iced renderer of the backend. -    type Renderer: crate::Renderer; +    type Renderer: iced_native::Renderer;      /// The surface of the backend.      type Surface; @@ -53,8 +53,8 @@ pub trait Compositor: Sized {          &mut self,          renderer: &mut Self::Renderer,          swap_chain: &mut Self::SwapChain, -        output: &<Self::Renderer as crate::Renderer>::Output, -        scale_factor: f64, +        viewport: &Viewport, +        output: &<Self::Renderer as iced_native::Renderer>::Output,          overlay: &[T],      ) -> mouse::Interaction;  } | 
