diff options
| author | 2019-11-14 06:46:50 +0100 | |
|---|---|---|
| committer | 2019-11-14 06:46:50 +0100 | |
| commit | bc8d347736ec997ec0e0c401289e2bc09e212b8a (patch) | |
| tree | b98798c09a3aa914b7d0869fba0cfd3efff7754f /wgpu/src/renderer | |
| parent | 839e039dbf2fb89dcb8c141503740777d2af2eb3 (diff) | |
| parent | 73f3c900071f950ea914652ca3f0002c1e173f61 (diff) | |
| download | iced-bc8d347736ec997ec0e0c401289e2bc09e212b8a.tar.gz iced-bc8d347736ec997ec0e0c401289e2bc09e212b8a.tar.bz2 iced-bc8d347736ec997ec0e0c401289e2bc09e212b8a.zip | |
Merge pull request #52 from hecrj/custom-layout-engine
Custom layout engine
Diffstat (limited to '')
| -rw-r--r-- | wgpu/src/renderer.rs | 68 | ||||
| -rw-r--r-- | wgpu/src/renderer/widget/button.rs | 29 | ||||
| -rw-r--r-- | wgpu/src/renderer/widget/checkbox.rs | 15 | ||||
| -rw-r--r-- | wgpu/src/renderer/widget/image.rs | 23 | ||||
| -rw-r--r-- | wgpu/src/renderer/widget/radio.rs | 12 | ||||
| -rw-r--r-- | wgpu/src/renderer/widget/scrollable.rs | 4 | ||||
| -rw-r--r-- | wgpu/src/renderer/widget/slider.rs | 18 | ||||
| -rw-r--r-- | wgpu/src/renderer/widget/text.rs | 82 | ||||
| -rw-r--r-- | wgpu/src/renderer/widget/text_input.rs | 4 | 
9 files changed, 132 insertions, 123 deletions
| diff --git a/wgpu/src/renderer.rs b/wgpu/src/renderer.rs index 235eefc6..e9df3550 100644 --- a/wgpu/src/renderer.rs +++ b/wgpu/src/renderer.rs @@ -9,9 +9,8 @@ use wgpu::{      Extensions, Limits, PowerPreference, Queue, RequestAdapterOptions,      TextureFormat,  }; -use wgpu_glyph::{GlyphBrush, GlyphBrushBuilder, Section}; -use std::{cell::RefCell, rc::Rc}; +use std::cell::RefCell;  mod target;  mod widget; @@ -23,8 +22,8 @@ pub struct Renderer {      queue: Queue,      quad_pipeline: quad::Pipeline,      image_pipeline: crate::image::Pipeline, - -    glyph_brush: Rc<RefCell<GlyphBrush<'static, ()>>>, +    text_pipeline: wgpu_glyph::GlyphBrush<'static, ()>, +    text_measurements: RefCell<glyph_brush::GlyphBrush<'static, ()>>,  }  pub struct Layer<'a> { @@ -72,10 +71,17 @@ impl Renderer {              .load(&[font::Family::Monospace])              .expect("Find monospace font"); -        let glyph_brush = -            GlyphBrushBuilder::using_fonts_bytes(vec![default_font, mono_font]) -                .initial_cache_size((2048, 2048)) -                .build(&mut device, TextureFormat::Bgra8UnormSrgb); +        let text_pipeline = +            wgpu_glyph::GlyphBrushBuilder::using_fonts_bytes(vec![ +                default_font.clone(), +                mono_font, +            ]) +            .initial_cache_size((2048, 2048)) +            .build(&mut device, TextureFormat::Bgra8UnormSrgb); + +        let text_measurements = +            glyph_brush::GlyphBrushBuilder::using_font_bytes(default_font) +                .build();          let quad_pipeline = quad::Pipeline::new(&mut device);          let image_pipeline = crate::image::Pipeline::new(&mut device); @@ -85,8 +91,8 @@ impl Renderer {              queue,              quad_pipeline,              image_pipeline, - -            glyph_brush: Rc::new(RefCell::new(glyph_brush)), +            text_pipeline, +            text_measurements: RefCell::new(text_measurements),          }      } @@ -190,7 +196,7 @@ impl Renderer {                      }                  }; -                layer.text.push(Section { +                layer.text.push(wgpu_glyph::Section {                      text: &content,                      screen_position: (                          x - layer.offset.x as f32, @@ -292,27 +298,26 @@ impl Renderer {          let first = layers.first().unwrap();          let mut overlay = Layer::new(first.bounds, Vector::new(0, 0)); -        let font_id = -            wgpu_glyph::FontId(self.glyph_brush.borrow().fonts().len() - 1); +        let font_id = wgpu_glyph::FontId(self.text_pipeline.fonts().len() - 1);          let scale = wgpu_glyph::Scale { x: 20.0, y: 20.0 };          for (i, line) in lines.iter().enumerate() { -            overlay.text.push(Section { +            overlay.text.push(wgpu_glyph::Section {                  text: line.as_ref(),                  screen_position: (11.0, 11.0 + 25.0 * i as f32),                  color: [0.9, 0.9, 0.9, 1.0],                  scale,                  font_id, -                ..Section::default() +                ..wgpu_glyph::Section::default()              }); -            overlay.text.push(Section { +            overlay.text.push(wgpu_glyph::Section {                  text: line.as_ref(),                  screen_position: (10.0, 10.0 + 25.0 * i as f32),                  color: [0.0, 0.0, 0.0, 1.0],                  scale,                  font_id, -                ..Section::default() +                ..wgpu_glyph::Section::default()              });          } @@ -360,11 +365,9 @@ impl Renderer {          }          if layer.text.len() > 0 { -            let mut glyph_brush = self.glyph_brush.borrow_mut(); -              for text in layer.text.iter() {                  // Target physical coordinates directly to avoid blurry text -                let text = Section { +                let text = wgpu_glyph::Section {                      screen_position: (                          (text.screen_position.0 * dpi).round(),                          (text.screen_position.1 * dpi).round(), @@ -377,10 +380,10 @@ impl Renderer {                      ..*text                  }; -                glyph_brush.queue(text); +                self.text_pipeline.queue(text);              } -            glyph_brush +            self.text_pipeline                  .draw_queued_with_transform_and_scissoring(                      &mut self.device,                      encoder, @@ -400,6 +403,25 @@ impl Renderer {  impl iced_native::Renderer for Renderer {      type Output = (Primitive, MouseCursor); + +    fn layout<'a, Message>( +        &mut self, +        element: &iced_native::Element<'a, Message, Self>, +    ) -> iced_native::layout::Node { +        let node = element.layout(self, &iced_native::layout::Limits::NONE); + +        // Trim measurements cache +        // TODO: We should probably use a `GlyphCalculator` for this. However, +        // it uses a lifetimed `GlyphCalculatorGuard` with side-effects on drop. +        // This makes stuff quite inconvenient. A manual method for trimming the +        // cache would make our lives easier. +        self.text_measurements +            .borrow_mut() +            .process_queued(|_, _| {}, |_| {}) +            .expect("Trim text measurements"); + +        node +    }  }  impl Windowed for Renderer { @@ -438,7 +460,7 @@ impl Debugger for Renderer {  }  fn explain_layout( -    layout: Layout, +    layout: Layout<'_>,      color: Color,      primitives: &mut Vec<Primitive>,  ) { diff --git a/wgpu/src/renderer/widget/button.rs b/wgpu/src/renderer/widget/button.rs index 0ac1c0a6..3d5e42ba 100644 --- a/wgpu/src/renderer/widget/button.rs +++ b/wgpu/src/renderer/widget/button.rs @@ -1,19 +1,30 @@  use crate::{Primitive, Renderer};  use iced_native::{ -    button, Align, Background, Button, Layout, Length, MouseCursor, -    Node, Point, Rectangle, Style, +    button, layout, Background, Button, Layout, Length, MouseCursor, Point, +    Rectangle,  };  impl button::Renderer for Renderer { -    fn node<Message>(&self, button: &Button<Message, Self>) -> Node { -        let style = Style::default() +    fn layout<Message>( +        &self, +        button: &Button<Message, Self>, +        limits: &layout::Limits, +    ) -> layout::Node { +        let padding = f32::from(button.padding); +        let limits = limits +            .min_width(100)              .width(button.width) -            .padding(button.padding) -            .min_width(Length::Units(100)) -            .align_self(button.align_self) -            .align_items(Align::Stretch); +            .height(Length::Shrink) +            .pad(padding); + +        let mut content = button.content.layout(self, &limits); + +        content.bounds.x = padding; +        content.bounds.y = padding; + +        let size = limits.resolve(content.size()).pad(padding); -        Node::with_children(style, vec![button.content.node(self)]) +        layout::Node::with_children(size, vec![content])      }      fn draw<Message>( diff --git a/wgpu/src/renderer/widget/checkbox.rs b/wgpu/src/renderer/widget/checkbox.rs index 1594c769..c2d7911c 100644 --- a/wgpu/src/renderer/widget/checkbox.rs +++ b/wgpu/src/renderer/widget/checkbox.rs @@ -1,16 +1,19 @@  use crate::{Primitive, Renderer};  use iced_native::{ -    checkbox, text, text::HorizontalAlignment, text::VerticalAlignment, Align, -    Background, Checkbox, Column, Layout, Length, MouseCursor, Node, -    Point, Rectangle, Row, Text, Widget, +    checkbox, layout, text, text::HorizontalAlignment, text::VerticalAlignment, +    Align, Background, Checkbox, Column, Layout, Length, MouseCursor, Point, +    Rectangle, Row, Text, Widget,  };  const SIZE: f32 = 28.0;  impl checkbox::Renderer for Renderer { -    fn node<Message>(&self, checkbox: &Checkbox<Message>) -> Node { +    fn layout<Message>( +        &self, +        checkbox: &Checkbox<Message>, +        limits: &layout::Limits, +    ) -> layout::Node {          Row::<(), Self>::new() -            .width(Length::Fill)              .spacing(15)              .align_items(Align::Center)              .push( @@ -19,7 +22,7 @@ impl checkbox::Renderer for Renderer {                      .height(Length::Units(SIZE as u16)),              )              .push(Text::new(&checkbox.label)) -            .node(self) +            .layout(self, limits)      }      fn draw<Message>( diff --git a/wgpu/src/renderer/widget/image.rs b/wgpu/src/renderer/widget/image.rs index 0e312706..0afb11e3 100644 --- a/wgpu/src/renderer/widget/image.rs +++ b/wgpu/src/renderer/widget/image.rs @@ -1,25 +1,28 @@  use crate::{Primitive, Renderer}; -use iced_native::{image, Image, Layout, Length, MouseCursor, Node, Style}; +use iced_native::{image, layout, Image, Layout, Length, MouseCursor, Size};  impl image::Renderer for Renderer { -    fn node(&self, image: &Image) -> Node { +    fn layout(&self, image: &Image, limits: &layout::Limits) -> layout::Node {          let (width, height) = self.image_pipeline.dimensions(&image.path);          let aspect_ratio = width as f32 / height as f32; -        let mut style = Style::default().align_self(image.align_self); -          // TODO: Deal with additional cases -        style = match (image.width, image.height) { -            (Length::Units(width), _) => style.width(image.width).height( +        let (width, height) = match (image.width, image.height) { +            (Length::Units(width), _) => ( +                image.width,                  Length::Units((width as f32 / aspect_ratio).round() as u16),              ), -            (_, _) => style -                .width(Length::Units(width as u16)) -                .height(Length::Units(height as u16)), +            (_, _) => { +                (Length::Units(width as u16), Length::Units(height as u16)) +            }          }; -        Node::new(style) +        let mut size = limits.width(width).height(height).resolve(Size::ZERO); + +        size.height = size.width / aspect_ratio; + +        layout::Node::new(size)      }      fn draw(&mut self, image: &Image, layout: Layout<'_>) -> Self::Output { diff --git a/wgpu/src/renderer/widget/radio.rs b/wgpu/src/renderer/widget/radio.rs index 61f5ce47..1f17ba28 100644 --- a/wgpu/src/renderer/widget/radio.rs +++ b/wgpu/src/renderer/widget/radio.rs @@ -1,14 +1,18 @@  use crate::{Primitive, Renderer};  use iced_native::{ -    radio, text, Align, Background, Column, Layout, Length, MouseCursor, -    Node, Point, Radio, Rectangle, Row, Text, Widget, +    layout, radio, text, Align, Background, Column, Layout, Length, +    MouseCursor, Point, Radio, Rectangle, Row, Text, Widget,  };  const SIZE: f32 = 28.0;  const DOT_SIZE: f32 = SIZE / 2.0;  impl radio::Renderer for Renderer { -    fn node<Message>(&self, radio: &Radio<Message>) -> Node { +    fn layout<Message>( +        &self, +        radio: &Radio<Message>, +        limits: &layout::Limits, +    ) -> layout::Node {          Row::<(), Self>::new()              .spacing(15)              .align_items(Align::Center) @@ -18,7 +22,7 @@ impl radio::Renderer for Renderer {                      .height(Length::Units(SIZE as u16)),              )              .push(Text::new(&radio.label)) -            .node(self) +            .layout(self, limits)      }      fn draw<Message>( diff --git a/wgpu/src/renderer/widget/scrollable.rs b/wgpu/src/renderer/widget/scrollable.rs index 5eadf275..dd6ebcc1 100644 --- a/wgpu/src/renderer/widget/scrollable.rs +++ b/wgpu/src/renderer/widget/scrollable.rs @@ -1,7 +1,7 @@  use crate::{Primitive, Renderer};  use iced_native::{ -    scrollable, Background, Layout, MouseCursor, Point, Rectangle, -    Scrollable, Vector, Widget, +    scrollable, Background, Layout, MouseCursor, Point, Rectangle, Scrollable, +    Vector, Widget,  };  const SCROLLBAR_WIDTH: u16 = 10; diff --git a/wgpu/src/renderer/widget/slider.rs b/wgpu/src/renderer/widget/slider.rs index 789e7bd4..98065bc9 100644 --- a/wgpu/src/renderer/widget/slider.rs +++ b/wgpu/src/renderer/widget/slider.rs @@ -1,20 +1,22 @@  use crate::{Primitive, Renderer};  use iced_native::{ -    slider, Background, Color, Layout, Length, MouseCursor, Node, Point, -    Rectangle, Slider, Style, +    layout, slider, Background, Color, Layout, Length, MouseCursor, Point, +    Rectangle, Size, Slider,  };  const HANDLE_WIDTH: f32 = 8.0;  const HANDLE_HEIGHT: f32 = 22.0;  impl slider::Renderer for Renderer { -    fn node<Message>(&self, slider: &Slider<Message>) -> Node { -        let style = Style::default() -            .width(slider.width) -            .height(Length::Units(HANDLE_HEIGHT as u16)) -            .min_width(Length::Units(100)); +    fn layout<Message>( +        &self, +        slider: &Slider<Message>, +        limits: &layout::Limits, +    ) -> layout::Node { +        let limits = limits.width(slider.width).height(Length::Units(30)); +        let size = limits.resolve(Size::ZERO); -        Node::new(style) +        layout::Node::new(size)      }      fn draw<Message>( diff --git a/wgpu/src/renderer/widget/text.rs b/wgpu/src/renderer/widget/text.rs index 29e07ff7..b9ccd787 100644 --- a/wgpu/src/renderer/widget/text.rs +++ b/wgpu/src/renderer/widget/text.rs @@ -1,73 +1,37 @@  use crate::{Primitive, Renderer}; -use iced_native::{text, Color, Layout, MouseCursor, Node, Style, Text}; +use iced_native::{layout, text, Color, Layout, MouseCursor, Size, Text};  use wgpu_glyph::{GlyphCruncher, Section}; -use std::cell::RefCell;  use std::f32;  // TODO: Obtain from renderer configuration  const DEFAULT_TEXT_SIZE: f32 = 20.0;  impl text::Renderer for Renderer { -    fn node(&self, text: &Text) -> Node { -        let glyph_brush = self.glyph_brush.clone(); -        let content = text.content.clone(); - -        // TODO: Investigate why stretch tries to measure this MANY times -        // with every ancestor's bounds. -        // Bug? Using the library wrong? I should probably open an issue on -        // the stretch repository. -        // I noticed that the first measure is the one that matters in -        // practice. Here, we use a RefCell to store the cached measurement. -        let measure = RefCell::new(None); +    fn layout(&self, text: &Text, limits: &layout::Limits) -> layout::Node { +        let limits = limits.width(text.width).height(text.height);          let size = text.size.map(f32::from).unwrap_or(DEFAULT_TEXT_SIZE); - -        let style = Style::default().width(text.width); - -        iced_native::Node::with_measure(style, move |bounds| { -            let mut measure = measure.borrow_mut(); - -            if measure.is_none() { -                let bounds = ( -                    match bounds.width { -                        iced_native::Number::Undefined => f32::INFINITY, -                        iced_native::Number::Defined(w) => w, -                    }, -                    match bounds.height { -                        iced_native::Number::Undefined => f32::INFINITY, -                        iced_native::Number::Defined(h) => h, -                    }, -                ); - -                let text = Section { -                    text: &content, -                    scale: wgpu_glyph::Scale { x: size, y: size }, -                    bounds, -                    ..Default::default() -                }; - -                let (width, height) = if let Some(bounds) = -                    glyph_brush.borrow_mut().glyph_bounds(&text) -                { -                    (bounds.width().ceil(), bounds.height().ceil()) -                } else { -                    (0.0, 0.0) -                }; - -                let size = iced_native::Size { width, height }; - -                // If the text has no width boundary we avoid caching as the -                // layout engine may just be measuring text in a row. -                if bounds.0 == f32::INFINITY { -                    return size; -                } else { -                    *measure = Some(size); -                } -            } - -            measure.unwrap() -        }) +        let bounds = limits.max(); + +        let section = Section { +            text: &text.content, +            scale: wgpu_glyph::Scale { x: size, y: size }, +            bounds: (bounds.width, bounds.height), +            ..Default::default() +        }; + +        let (width, height) = if let Some(bounds) = +            self.text_measurements.borrow_mut().glyph_bounds(§ion) +        { +            (bounds.width().ceil(), bounds.height().ceil()) +        } else { +            (0.0, 0.0) +        }; + +        let size = limits.resolve(Size::new(width, height)); + +        layout::Node::new(size)      }      fn draw(&mut self, text: &Text, layout: Layout<'_>) -> Self::Output { diff --git a/wgpu/src/renderer/widget/text_input.rs b/wgpu/src/renderer/widget/text_input.rs index b5f6c5f6..75eb20f7 100644 --- a/wgpu/src/renderer/widget/text_input.rs +++ b/wgpu/src/renderer/widget/text_input.rs @@ -78,7 +78,7 @@ impl text_input::Renderer for Renderer {                  .to_string();              let mut text_value_width = self -                .glyph_brush +                .text_measurements                  .borrow_mut()                  .glyph_bounds(Section {                      text: text_before_cursor, @@ -94,7 +94,7 @@ impl text_input::Renderer for Renderer {              if spaces_at_the_end > 0 {                  let space_width = { -                    let glyph_brush = self.glyph_brush.borrow(); +                    let glyph_brush = self.text_measurements.borrow();                      // TODO: Select appropriate font                      let font = &glyph_brush.fonts()[0]; | 
