From e1b9d42bf1443ae4958aa9303255ef19c635debb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Oct 2019 00:01:45 +0200 Subject: Start `iced_winit` and `iced_wgpu` --- wgpu/src/lib.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 wgpu/src/lib.rs (limited to 'wgpu/src/lib.rs') diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs new file mode 100644 index 00000000..7aa053bc --- /dev/null +++ b/wgpu/src/lib.rs @@ -0,0 +1,87 @@ +use iced_native::{ + button, checkbox, image, radio, renderer::Debugger, slider, text, Button, + Checkbox, Color, Image, Layout, MouseCursor, Node, Point, Radio, Slider, + Style, Text, +}; + +pub struct Renderer; + +impl text::Renderer for Renderer { + fn node(&self, _text: &Text) -> Node { + Node::new(Style::default()) + } + + fn draw(&mut self, _text: &Text, _layout: Layout<'_>) {} +} + +impl checkbox::Renderer for Renderer { + fn node(&mut self, _checkbox: &Checkbox) -> Node { + Node::new(Style::default()) + } + + fn draw( + &mut self, + _checkbox: &Checkbox, + _layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + MouseCursor::OutOfBounds + } +} + +impl radio::Renderer for Renderer { + fn node(&mut self, _checkbox: &Radio) -> Node { + Node::new(Style::default()) + } + + fn draw( + &mut self, + _radio: &Radio, + _layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + MouseCursor::OutOfBounds + } +} + +impl slider::Renderer for Renderer { + fn node(&self, _slider: &Slider) -> Node { + Node::new(Style::default()) + } + + fn draw( + &mut self, + _slider: &Slider, + _layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + MouseCursor::OutOfBounds + } +} + +impl image::Renderer<&str> for Renderer { + fn node(&mut self, _image: &Image<&str>) -> Node { + Node::new(Style::default()) + } + + fn draw(&mut self, _checkbox: &Image<&str>, _layout: Layout<'_>) {} +} + +impl button::Renderer for Renderer { + fn node(&self, _button: &Button) -> Node { + Node::new(Style::default()) + } + + fn draw( + &mut self, + _button: &Button, + _layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + MouseCursor::OutOfBounds + } +} + +impl Debugger for Renderer { + fn explain(&mut self, _layout: &Layout<'_>, _color: Color) {} +} -- cgit From 8bb33cd5a0b876a5e24108604be2cecd4efad3ef Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Oct 2019 00:23:08 +0200 Subject: Initialize `wgpu` We only enable the `vulkan` feature for now. --- wgpu/src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'wgpu/src/lib.rs') diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 7aa053bc..502a2614 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -4,7 +4,44 @@ use iced_native::{ Style, Text, }; -pub struct Renderer; +use raw_window_handle::HasRawWindowHandle; +use wgpu::{ + Adapter, Device, DeviceDescriptor, Extensions, Instance, Limits, + PowerPreference, RequestAdapterOptions, Surface, +}; + +pub struct Renderer { + instance: Instance, + surface: Surface, + adapter: Adapter, + device: Device, +} + +impl Renderer { + pub fn new(window: &W) -> Self { + let instance = Instance::new(); + + let adapter = instance.request_adapter(&RequestAdapterOptions { + power_preference: PowerPreference::LowPower, + }); + + let device = adapter.request_device(&DeviceDescriptor { + extensions: Extensions { + anisotropic_filtering: false, + }, + limits: Limits { max_bind_groups: 1 }, + }); + + let surface = instance.create_surface(window.raw_window_handle()); + + Self { + instance, + surface, + adapter, + device, + } + } +} impl text::Renderer for Renderer { fn node(&self, _text: &Text) -> Node { -- cgit From fc38119be3ffccc35c90971e956e8866e8b97e85 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 3 Oct 2019 00:34:15 +0200 Subject: Clear the window properly on redraw --- wgpu/src/lib.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) (limited to 'wgpu/src/lib.rs') diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 502a2614..1e11749b 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -6,8 +6,9 @@ use iced_native::{ use raw_window_handle::HasRawWindowHandle; use wgpu::{ - Adapter, Device, DeviceDescriptor, Extensions, Instance, Limits, - PowerPreference, RequestAdapterOptions, Surface, + Adapter, CommandEncoderDescriptor, Device, DeviceDescriptor, Extensions, + Instance, Limits, PowerPreference, RequestAdapterOptions, Surface, + SwapChain, SwapChainDescriptor, TextureFormat, TextureUsage, }; pub struct Renderer { @@ -15,10 +16,15 @@ pub struct Renderer { surface: Surface, adapter: Adapter, device: Device, + swap_chain: SwapChain, } impl Renderer { - pub fn new(window: &W) -> Self { + pub fn new( + window: &W, + width: u32, + height: u32, + ) -> Self { let instance = Instance::new(); let adapter = instance.request_adapter(&RequestAdapterOptions { @@ -34,13 +40,51 @@ impl Renderer { let surface = instance.create_surface(window.raw_window_handle()); + let swap_chain = device.create_swap_chain( + &surface, + &SwapChainDescriptor { + usage: TextureUsage::OUTPUT_ATTACHMENT, + format: TextureFormat::Bgra8UnormSrgb, + width, + height, + present_mode: wgpu::PresentMode::Vsync, + }, + ); + Self { instance, surface, adapter, device, + swap_chain, } } + + pub fn draw(&mut self) { + let frame = self.swap_chain.get_next_texture(); + + let mut encoder = self + .device + .create_command_encoder(&CommandEncoderDescriptor { todo: 0 }); + + let _ = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { + attachment: &frame.view, + resolve_target: None, + load_op: wgpu::LoadOp::Clear, + store_op: wgpu::StoreOp::Store, + clear_color: wgpu::Color { + r: 1.0, + g: 1.0, + b: 1.0, + a: 1.0, + }, + }], + depth_stencil_attachment: None, + }); + + self.device.get_queue().submit(&[encoder.finish()]); + } } impl text::Renderer for Renderer { -- cgit From a7d11944039a1b5ea5b72256e8d15367d99e6010 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 5 Oct 2019 03:56:18 +0200 Subject: Add `Renderer` and `Primitive` concepts --- wgpu/src/lib.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 14 deletions(-) (limited to 'wgpu/src/lib.rs') diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 1e11749b..28964858 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1,7 +1,7 @@ use iced_native::{ - button, checkbox, image, radio, renderer::Debugger, slider, text, Button, - Checkbox, Color, Image, Layout, MouseCursor, Node, Point, Radio, Slider, - Style, Text, + button, checkbox, column, image, radio, renderer::Debugger, row, slider, + text, Button, Checkbox, Color, Column, Image, Layout, Node, Point, Radio, + Row, Slider, Style, Text, Widget, }; use raw_window_handle::HasRawWindowHandle; @@ -87,12 +87,36 @@ impl Renderer { } } +impl column::Renderer for Renderer { + fn draw( + &mut self, + _column: &Column<'_, Message, Self>, + _layout: Layout<'_>, + _cursor_position: Point, + ) -> Self::Primitive { + () + } +} + +impl row::Renderer for Renderer { + fn draw( + &mut self, + _column: &Row<'_, Message, Self>, + _layout: Layout<'_>, + _cursor_position: Point, + ) -> Self::Primitive { + () + } +} + impl text::Renderer for Renderer { fn node(&self, _text: &Text) -> Node { Node::new(Style::default()) } - fn draw(&mut self, _text: &Text, _layout: Layout<'_>) {} + fn draw(&mut self, _text: &Text, _layout: Layout<'_>) -> Self::Primitive { + () + } } impl checkbox::Renderer for Renderer { @@ -105,8 +129,8 @@ impl checkbox::Renderer for Renderer { _checkbox: &Checkbox, _layout: Layout<'_>, _cursor_position: Point, - ) -> MouseCursor { - MouseCursor::OutOfBounds + ) -> Self::Primitive { + () } } @@ -120,8 +144,8 @@ impl radio::Renderer for Renderer { _radio: &Radio, _layout: Layout<'_>, _cursor_position: Point, - ) -> MouseCursor { - MouseCursor::OutOfBounds + ) -> Self::Primitive { + () } } @@ -135,8 +159,8 @@ impl slider::Renderer for Renderer { _slider: &Slider, _layout: Layout<'_>, _cursor_position: Point, - ) -> MouseCursor { - MouseCursor::OutOfBounds + ) -> Self::Primitive { + () } } @@ -145,7 +169,13 @@ impl image::Renderer<&str> for Renderer { Node::new(Style::default()) } - fn draw(&mut self, _checkbox: &Image<&str>, _layout: Layout<'_>) {} + fn draw( + &mut self, + _checkbox: &Image<&str>, + _layout: Layout<'_>, + ) -> Self::Primitive { + () + } } impl button::Renderer for Renderer { @@ -158,11 +188,23 @@ impl button::Renderer for Renderer { _button: &Button, _layout: Layout<'_>, _cursor_position: Point, - ) -> MouseCursor { - MouseCursor::OutOfBounds + ) -> Self::Primitive { + () } } +impl iced_native::Renderer for Renderer { + type Primitive = (); +} + impl Debugger for Renderer { - fn explain(&mut self, _layout: &Layout<'_>, _color: Color) {} + fn explain( + &mut self, + widget: &dyn Widget, + layout: Layout<'_>, + cursor_position: Point, + _color: Color, + ) -> Self::Primitive { + widget.draw(self, layout, cursor_position) + } } -- cgit From 0c3f78713d24b263e94cf6aebb8862926feaff23 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 5 Oct 2019 05:12:36 +0200 Subject: Draft basic text rendering using `wgpu_glyph` --- wgpu/src/lib.rs | 226 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 185 insertions(+), 41 deletions(-) (limited to 'wgpu/src/lib.rs') diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 28964858..d8f727cd 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1,7 +1,7 @@ use iced_native::{ button, checkbox, column, image, radio, renderer::Debugger, row, slider, text, Button, Checkbox, Color, Column, Image, Layout, Node, Point, Radio, - Row, Slider, Style, Text, Widget, + Rectangle, Row, Slider, Style, Text, Widget, }; use raw_window_handle::HasRawWindowHandle; @@ -10,28 +10,34 @@ use wgpu::{ Instance, Limits, PowerPreference, RequestAdapterOptions, Surface, SwapChain, SwapChainDescriptor, TextureFormat, TextureUsage, }; +use wgpu_glyph::{GlyphBrush, GlyphBrushBuilder, GlyphCruncher, Section}; + +use std::f32; +use std::{cell::RefCell, rc::Rc}; pub struct Renderer { instance: Instance, surface: Surface, adapter: Adapter, device: Device, + glyph_brush: Rc>>, +} + +pub struct Target { + width: u16, + height: u16, swap_chain: SwapChain, } impl Renderer { - pub fn new( - window: &W, - width: u32, - height: u32, - ) -> Self { + pub fn new(window: &W) -> Self { let instance = Instance::new(); let adapter = instance.request_adapter(&RequestAdapterOptions { power_preference: PowerPreference::LowPower, }); - let device = adapter.request_device(&DeviceDescriptor { + let mut device = adapter.request_device(&DeviceDescriptor { extensions: Extensions { anisotropic_filtering: false, }, @@ -40,28 +46,40 @@ impl Renderer { let surface = instance.create_surface(window.raw_window_handle()); - let swap_chain = device.create_swap_chain( - &surface, - &SwapChainDescriptor { - usage: TextureUsage::OUTPUT_ATTACHMENT, - format: TextureFormat::Bgra8UnormSrgb, - width, - height, - present_mode: wgpu::PresentMode::Vsync, - }, - ); + let font: &[u8] = + include_bytes!("../../examples/tour/resources/Roboto-Regular.ttf"); + + let glyph_brush = GlyphBrushBuilder::using_font_bytes(font) + .build(&mut device, TextureFormat::Bgra8UnormSrgb); Self { instance, surface, adapter, device, - swap_chain, + glyph_brush: Rc::new(RefCell::new(glyph_brush)), } } - pub fn draw(&mut self) { - let frame = self.swap_chain.get_next_texture(); + pub fn target(&self, width: u16, height: u16) -> Target { + Target { + width, + height, + swap_chain: self.device.create_swap_chain( + &self.surface, + &SwapChainDescriptor { + usage: TextureUsage::OUTPUT_ATTACHMENT, + format: TextureFormat::Bgra8UnormSrgb, + width: u32::from(width), + height: u32::from(height), + present_mode: wgpu::PresentMode::Vsync, + }, + ), + } + } + + pub fn draw(&mut self, target: &mut Target, primitive: &Primitive) { + let frame = target.swap_chain.get_next_texture(); let mut encoder = self .device @@ -83,39 +101,169 @@ impl Renderer { depth_stencil_attachment: None, }); + self.draw_primitive(primitive); + + self.glyph_brush + .borrow_mut() + .draw_queued( + &mut self.device, + &mut encoder, + &frame.view, + u32::from(target.width), + u32::from(target.height), + ) + .expect("Draw text"); + self.device.get_queue().submit(&[encoder.finish()]); } + + fn draw_primitive(&mut self, primitive: &Primitive) { + match primitive { + Primitive::None => {} + Primitive::Group { primitives } => { + for primitive in primitives { + self.draw_primitive(primitive) + } + } + Primitive::Text { + content, + bounds, + size, + } => self.glyph_brush.borrow_mut().queue(Section { + text: &content, + screen_position: (bounds.x, bounds.y), + bounds: (bounds.width, bounds.height), + scale: wgpu_glyph::Scale { x: *size, y: *size }, + ..Default::default() + }), + } + } +} + +#[derive(Debug, Clone)] +pub enum Primitive { + None, + Group { + primitives: Vec, + }, + Text { + content: String, + bounds: Rectangle, + size: f32, + }, +} + +impl iced_native::Renderer for Renderer { + type Primitive = Primitive; } impl column::Renderer for Renderer { fn draw( &mut self, - _column: &Column<'_, Message, Self>, - _layout: Layout<'_>, - _cursor_position: Point, + column: &Column<'_, Message, Self>, + layout: Layout<'_>, + cursor_position: Point, ) -> Self::Primitive { - () + Primitive::Group { + primitives: column + .children + .iter() + .zip(layout.children()) + .map(|(child, layout)| { + child.draw(self, layout, cursor_position) + }) + .collect(), + } } } impl row::Renderer for Renderer { fn draw( &mut self, - _column: &Row<'_, Message, Self>, - _layout: Layout<'_>, - _cursor_position: Point, + row: &Row<'_, Message, Self>, + layout: Layout<'_>, + cursor_position: Point, ) -> Self::Primitive { - () + Primitive::Group { + primitives: row + .children + .iter() + .zip(layout.children()) + .map(|(child, layout)| { + child.draw(self, layout, cursor_position) + }) + .collect(), + } } } impl text::Renderer for Renderer { - fn node(&self, _text: &Text) -> Node { - Node::new(Style::default()) + 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); + let size = text.size.map(f32::from).unwrap_or(20.0); + + 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(), bounds.height()) + } 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() + }) } - fn draw(&mut self, _text: &Text, _layout: Layout<'_>) -> Self::Primitive { - () + fn draw(&mut self, text: &Text, layout: Layout<'_>) -> Self::Primitive { + Primitive::Text { + content: text.content.clone(), + size: f32::from(text.size.unwrap_or(20)), + bounds: layout.bounds(), + } } } @@ -130,7 +278,7 @@ impl checkbox::Renderer for Renderer { _layout: Layout<'_>, _cursor_position: Point, ) -> Self::Primitive { - () + Primitive::None } } @@ -145,7 +293,7 @@ impl radio::Renderer for Renderer { _layout: Layout<'_>, _cursor_position: Point, ) -> Self::Primitive { - () + Primitive::None } } @@ -160,7 +308,7 @@ impl slider::Renderer for Renderer { _layout: Layout<'_>, _cursor_position: Point, ) -> Self::Primitive { - () + Primitive::None } } @@ -174,7 +322,7 @@ impl image::Renderer<&str> for Renderer { _checkbox: &Image<&str>, _layout: Layout<'_>, ) -> Self::Primitive { - () + Primitive::None } } @@ -189,14 +337,10 @@ impl button::Renderer for Renderer { _layout: Layout<'_>, _cursor_position: Point, ) -> Self::Primitive { - () + Primitive::None } } -impl iced_native::Renderer for Renderer { - type Primitive = (); -} - impl Debugger for Renderer { fn explain( &mut self, -- cgit From 5a5ca34b5fcab9266359d3f0885782969f8c058e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 5 Oct 2019 19:22:51 +0200 Subject: Modularize `iced_wgpu` --- wgpu/src/lib.rs | 359 +------------------------------------------------------- 1 file changed, 6 insertions(+), 353 deletions(-) (limited to 'wgpu/src/lib.rs') diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index d8f727cd..9a2a336f 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1,354 +1,7 @@ -use iced_native::{ - button, checkbox, column, image, radio, renderer::Debugger, row, slider, - text, Button, Checkbox, Color, Column, Image, Layout, Node, Point, Radio, - Rectangle, Row, Slider, Style, Text, Widget, -}; +mod mouse_cursor; +mod primitive; +mod renderer; -use raw_window_handle::HasRawWindowHandle; -use wgpu::{ - Adapter, CommandEncoderDescriptor, Device, DeviceDescriptor, Extensions, - Instance, Limits, PowerPreference, RequestAdapterOptions, Surface, - SwapChain, SwapChainDescriptor, TextureFormat, TextureUsage, -}; -use wgpu_glyph::{GlyphBrush, GlyphBrushBuilder, GlyphCruncher, Section}; - -use std::f32; -use std::{cell::RefCell, rc::Rc}; - -pub struct Renderer { - instance: Instance, - surface: Surface, - adapter: Adapter, - device: Device, - glyph_brush: Rc>>, -} - -pub struct Target { - width: u16, - height: u16, - swap_chain: SwapChain, -} - -impl Renderer { - pub fn new(window: &W) -> Self { - let instance = Instance::new(); - - let adapter = instance.request_adapter(&RequestAdapterOptions { - power_preference: PowerPreference::LowPower, - }); - - let mut device = adapter.request_device(&DeviceDescriptor { - extensions: Extensions { - anisotropic_filtering: false, - }, - limits: Limits { max_bind_groups: 1 }, - }); - - let surface = instance.create_surface(window.raw_window_handle()); - - let font: &[u8] = - include_bytes!("../../examples/tour/resources/Roboto-Regular.ttf"); - - let glyph_brush = GlyphBrushBuilder::using_font_bytes(font) - .build(&mut device, TextureFormat::Bgra8UnormSrgb); - - Self { - instance, - surface, - adapter, - device, - glyph_brush: Rc::new(RefCell::new(glyph_brush)), - } - } - - pub fn target(&self, width: u16, height: u16) -> Target { - Target { - width, - height, - swap_chain: self.device.create_swap_chain( - &self.surface, - &SwapChainDescriptor { - usage: TextureUsage::OUTPUT_ATTACHMENT, - format: TextureFormat::Bgra8UnormSrgb, - width: u32::from(width), - height: u32::from(height), - present_mode: wgpu::PresentMode::Vsync, - }, - ), - } - } - - pub fn draw(&mut self, target: &mut Target, primitive: &Primitive) { - let frame = target.swap_chain.get_next_texture(); - - let mut encoder = self - .device - .create_command_encoder(&CommandEncoderDescriptor { todo: 0 }); - - let _ = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - color_attachments: &[wgpu::RenderPassColorAttachmentDescriptor { - attachment: &frame.view, - resolve_target: None, - load_op: wgpu::LoadOp::Clear, - store_op: wgpu::StoreOp::Store, - clear_color: wgpu::Color { - r: 1.0, - g: 1.0, - b: 1.0, - a: 1.0, - }, - }], - depth_stencil_attachment: None, - }); - - self.draw_primitive(primitive); - - self.glyph_brush - .borrow_mut() - .draw_queued( - &mut self.device, - &mut encoder, - &frame.view, - u32::from(target.width), - u32::from(target.height), - ) - .expect("Draw text"); - - self.device.get_queue().submit(&[encoder.finish()]); - } - - fn draw_primitive(&mut self, primitive: &Primitive) { - match primitive { - Primitive::None => {} - Primitive::Group { primitives } => { - for primitive in primitives { - self.draw_primitive(primitive) - } - } - Primitive::Text { - content, - bounds, - size, - } => self.glyph_brush.borrow_mut().queue(Section { - text: &content, - screen_position: (bounds.x, bounds.y), - bounds: (bounds.width, bounds.height), - scale: wgpu_glyph::Scale { x: *size, y: *size }, - ..Default::default() - }), - } - } -} - -#[derive(Debug, Clone)] -pub enum Primitive { - None, - Group { - primitives: Vec, - }, - Text { - content: String, - bounds: Rectangle, - size: f32, - }, -} - -impl iced_native::Renderer for Renderer { - type Primitive = Primitive; -} - -impl column::Renderer for Renderer { - fn draw( - &mut self, - column: &Column<'_, Message, Self>, - layout: Layout<'_>, - cursor_position: Point, - ) -> Self::Primitive { - Primitive::Group { - primitives: column - .children - .iter() - .zip(layout.children()) - .map(|(child, layout)| { - child.draw(self, layout, cursor_position) - }) - .collect(), - } - } -} - -impl row::Renderer for Renderer { - fn draw( - &mut self, - row: &Row<'_, Message, Self>, - layout: Layout<'_>, - cursor_position: Point, - ) -> Self::Primitive { - Primitive::Group { - primitives: row - .children - .iter() - .zip(layout.children()) - .map(|(child, layout)| { - child.draw(self, layout, cursor_position) - }) - .collect(), - } - } -} - -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); - let size = text.size.map(f32::from).unwrap_or(20.0); - - 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(), bounds.height()) - } 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() - }) - } - - fn draw(&mut self, text: &Text, layout: Layout<'_>) -> Self::Primitive { - Primitive::Text { - content: text.content.clone(), - size: f32::from(text.size.unwrap_or(20)), - bounds: layout.bounds(), - } - } -} - -impl checkbox::Renderer for Renderer { - fn node(&mut self, _checkbox: &Checkbox) -> Node { - Node::new(Style::default()) - } - - fn draw( - &mut self, - _checkbox: &Checkbox, - _layout: Layout<'_>, - _cursor_position: Point, - ) -> Self::Primitive { - Primitive::None - } -} - -impl radio::Renderer for Renderer { - fn node(&mut self, _checkbox: &Radio) -> Node { - Node::new(Style::default()) - } - - fn draw( - &mut self, - _radio: &Radio, - _layout: Layout<'_>, - _cursor_position: Point, - ) -> Self::Primitive { - Primitive::None - } -} - -impl slider::Renderer for Renderer { - fn node(&self, _slider: &Slider) -> Node { - Node::new(Style::default()) - } - - fn draw( - &mut self, - _slider: &Slider, - _layout: Layout<'_>, - _cursor_position: Point, - ) -> Self::Primitive { - Primitive::None - } -} - -impl image::Renderer<&str> for Renderer { - fn node(&mut self, _image: &Image<&str>) -> Node { - Node::new(Style::default()) - } - - fn draw( - &mut self, - _checkbox: &Image<&str>, - _layout: Layout<'_>, - ) -> Self::Primitive { - Primitive::None - } -} - -impl button::Renderer for Renderer { - fn node(&self, _button: &Button) -> Node { - Node::new(Style::default()) - } - - fn draw( - &mut self, - _button: &Button, - _layout: Layout<'_>, - _cursor_position: Point, - ) -> Self::Primitive { - Primitive::None - } -} - -impl Debugger for Renderer { - fn explain( - &mut self, - widget: &dyn Widget, - layout: Layout<'_>, - cursor_position: Point, - _color: Color, - ) -> Self::Primitive { - widget.draw(self, layout, cursor_position) - } -} +pub use mouse_cursor::MouseCursor; +pub use primitive::Primitive; +pub use renderer::{Renderer, Target}; -- cgit From 7765e6da50a3448501ee1b639e580c94a4fbda8a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 6 Oct 2019 19:22:25 +0200 Subject: Draft `Box` primitive --- wgpu/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wgpu/src/lib.rs') diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 9a2a336f..d5cfee64 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -3,5 +3,5 @@ mod primitive; mod renderer; pub use mouse_cursor::MouseCursor; -pub use primitive::Primitive; +pub use primitive::{Background, Primitive}; pub use renderer::{Renderer, Target}; -- cgit From c9510db551893775d3233340dd114d971e24323a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 7 Oct 2019 03:56:16 +0200 Subject: Render colored quads --- wgpu/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'wgpu/src/lib.rs') diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index d5cfee64..33d8f5ed 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1,6 +1,11 @@ mod mouse_cursor; mod primitive; +mod quad; mod renderer; +mod transformation; + +pub(crate) use quad::Quad; +pub(crate) use transformation::Transformation; pub use mouse_cursor::MouseCursor; pub use primitive::{Background, Primitive}; -- cgit From 10e10e5e06841574425d2633f1c2916733f7b4ff Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 8 Oct 2019 03:13:41 +0200 Subject: Make `iced_core::Button` customizable Now it supports: - Any kind of content - Custom border radius - Custom background --- wgpu/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wgpu/src/lib.rs') diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 33d8f5ed..8f8d50e9 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -8,5 +8,5 @@ pub(crate) use quad::Quad; pub(crate) use transformation::Transformation; pub use mouse_cursor::MouseCursor; -pub use primitive::{Background, Primitive}; +pub use primitive::Primitive; pub use renderer::{Renderer, Target}; -- cgit From a92a0b73ed7ed935df762d06c4249894fd35b227 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 9 Oct 2019 05:36:49 +0200 Subject: Move `winit` logic from `iced` to `iced_winit` - Added new `renderer::Windowed` trait. This shoud allow users to easily try different renderers by simply changing one line. - Renamed `UserInterface` traits to `Application`, as the `run` method takes total control of the current thread. - Moved `MouseCursor` back to `iced_native`. The new `renderer::Windowed` trait returns one on `draw`. - Split `iced_native` renderer in multiple modules, for consistency. --- wgpu/src/lib.rs | 2 -- 1 file changed, 2 deletions(-) (limited to 'wgpu/src/lib.rs') diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 8f8d50e9..46849aab 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1,4 +1,3 @@ -mod mouse_cursor; mod primitive; mod quad; mod renderer; @@ -7,6 +6,5 @@ mod transformation; pub(crate) use quad::Quad; pub(crate) use transformation::Transformation; -pub use mouse_cursor::MouseCursor; pub use primitive::Primitive; pub use renderer::{Renderer, Target}; -- cgit From 38b6c84e7761c049b17d178deb9c866386a53946 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 23 Oct 2019 01:21:23 +0200 Subject: Implement basic image rendering in `iced_wgpu` --- wgpu/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'wgpu/src/lib.rs') diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 46849aab..01dc4c20 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1,8 +1,10 @@ +mod image; mod primitive; mod quad; mod renderer; mod transformation; +pub(crate) use crate::image::Image; pub(crate) use quad::Quad; pub(crate) use transformation::Transformation; -- cgit