diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/custom_widget/Cargo.toml | 1 | ||||
-rw-r--r-- | examples/custom_widget/src/main.rs | 42 | ||||
-rw-r--r-- | examples/geometry/src/main.rs | 130 | ||||
-rw-r--r-- | examples/integration_opengl/src/controls.rs | 8 | ||||
-rw-r--r-- | examples/integration_opengl/src/main.rs | 18 | ||||
-rw-r--r-- | examples/integration_wgpu/src/controls.rs | 7 | ||||
-rw-r--r-- | examples/integration_wgpu/src/main.rs | 31 | ||||
-rw-r--r-- | examples/pane_grid/src/main.rs | 34 | ||||
-rw-r--r-- | examples/scrollable/src/style.rs | 6 | ||||
-rw-r--r-- | examples/styling/src/main.rs | 14 | ||||
-rw-r--r-- | examples/todos/src/main.rs | 36 |
11 files changed, 165 insertions, 162 deletions
diff --git a/examples/custom_widget/Cargo.toml b/examples/custom_widget/Cargo.toml index 3942538d..86b0d2a9 100644 --- a/examples/custom_widget/Cargo.toml +++ b/examples/custom_widget/Cargo.toml @@ -8,4 +8,3 @@ publish = false [dependencies] iced = { path = "../.." } iced_native = { path = "../../native" } -iced_graphics = { path = "../../graphics" } diff --git a/examples/custom_widget/src/main.rs b/examples/custom_widget/src/main.rs index c9ad1905..78a4339e 100644 --- a/examples/custom_widget/src/main.rs +++ b/examples/custom_widget/src/main.rs @@ -9,10 +9,11 @@ mod circle { // Of course, you can choose to make the implementation renderer-agnostic, // if you wish to, by creating your own `Renderer` trait, which could be // implemented by `iced_wgpu` and other renderers. - use iced_graphics::{Backend, Defaults, Primitive, Renderer}; + use iced_native::layout::{self, Layout}; + use iced_native::renderer; use iced_native::{ - layout, mouse, Background, Color, Element, Hasher, Layout, Length, - Point, Rectangle, Size, Widget, + Background, Color, Element, Hasher, Length, Point, Rectangle, Size, + Widget, }; pub struct Circle { @@ -25,9 +26,9 @@ mod circle { } } - impl<Message, B> Widget<Message, Renderer<B>> for Circle + impl<Message, Renderer> Widget<Message, Renderer> for Circle where - B: Backend, + Renderer: renderer::Renderer, { fn width(&self) -> Length { Length::Shrink @@ -39,7 +40,7 @@ mod circle { fn layout( &self, - _renderer: &Renderer<B>, + _renderer: &Renderer, _limits: &layout::Limits, ) -> layout::Node { layout::Node::new(Size::new(self.radius * 2.0, self.radius * 2.0)) @@ -53,30 +54,27 @@ mod circle { fn draw( &self, - _renderer: &mut Renderer<B>, - _defaults: &Defaults, + renderer: &mut Renderer, + _style: &renderer::Style, layout: Layout<'_>, _cursor_position: Point, _viewport: &Rectangle, - ) -> (Primitive, mouse::Interaction) { - ( - Primitive::Quad { - bounds: layout.bounds(), - background: Background::Color(Color::BLACK), - border_radius: self.radius, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }, - mouse::Interaction::default(), - ) + ) { + renderer.fill_rectangle(renderer::Quad { + bounds: layout.bounds(), + background: Background::Color(Color::BLACK), + border_radius: self.radius, + border_width: 0.0, + border_color: Color::TRANSPARENT, + }); } } - impl<'a, Message, B> Into<Element<'a, Message, Renderer<B>>> for Circle + impl<'a, Message, Renderer> Into<Element<'a, Message, Renderer>> for Circle where - B: Backend, + Renderer: renderer::Renderer, { - fn into(self) -> Element<'a, Message, Renderer<B>> { + fn into(self) -> Element<'a, Message, Renderer> { Element::new(self) } } diff --git a/examples/geometry/src/main.rs b/examples/geometry/src/main.rs index e5115493..6ef12013 100644 --- a/examples/geometry/src/main.rs +++ b/examples/geometry/src/main.rs @@ -10,12 +10,11 @@ mod rainbow { // Of course, you can choose to make the implementation renderer-agnostic, // if you wish to, by creating your own `Renderer` trait, which could be // implemented by `iced_wgpu` and other renderers. - use iced_graphics::{ - triangle::{Mesh2D, Vertex2D}, - Backend, Defaults, Primitive, Renderer, - }; + use iced_graphics::renderer::{self, Renderer}; + use iced_graphics::{Backend, Primitive}; + use iced_native::{ - layout, mouse, Element, Hasher, Layout, Length, Point, Rectangle, Size, + layout, Element, Hasher, Layout, Length, Point, Rectangle, Size, Vector, Widget, }; @@ -53,12 +52,15 @@ mod rainbow { fn draw( &self, - _renderer: &mut Renderer<B>, - _defaults: &Defaults, + renderer: &mut Renderer<B>, + _style: &renderer::Style, layout: Layout<'_>, cursor_position: Point, _viewport: &Rectangle, - ) -> (Primitive, mouse::Interaction) { + ) { + use iced_graphics::triangle::{Mesh2D, Vertex2D}; + use iced_native::Renderer as _; + let b = layout.bounds(); // R O Y G B I V @@ -88,65 +90,63 @@ mod rainbow { let posn_bl = [0.0, b.height]; let posn_l = [0.0, b.height / 2.0]; - ( - Primitive::Translate { - translation: Vector::new(b.x, b.y), - content: Box::new(Primitive::Mesh2D { - size: b.size(), - buffers: Mesh2D { - vertices: vec![ - Vertex2D { - position: posn_center, - color: [1.0, 1.0, 1.0, 1.0], - }, - Vertex2D { - position: posn_tl, - color: color_r, - }, - Vertex2D { - position: posn_t, - color: color_o, - }, - Vertex2D { - position: posn_tr, - color: color_y, - }, - Vertex2D { - position: posn_r, - color: color_g, - }, - Vertex2D { - position: posn_br, - color: color_gb, - }, - Vertex2D { - position: posn_b, - color: color_b, - }, - Vertex2D { - position: posn_bl, - color: color_i, - }, - Vertex2D { - position: posn_l, - color: color_v, - }, - ], - indices: vec![ - 0, 1, 2, // TL - 0, 2, 3, // T - 0, 3, 4, // TR - 0, 4, 5, // R - 0, 5, 6, // BR - 0, 6, 7, // B - 0, 7, 8, // BL - 0, 8, 1, // L - ], + let mesh = Primitive::Mesh2D { + size: b.size(), + buffers: Mesh2D { + vertices: vec![ + Vertex2D { + position: posn_center, + color: [1.0, 1.0, 1.0, 1.0], + }, + Vertex2D { + position: posn_tl, + color: color_r, + }, + Vertex2D { + position: posn_t, + color: color_o, + }, + Vertex2D { + position: posn_tr, + color: color_y, + }, + Vertex2D { + position: posn_r, + color: color_g, }, - }), + Vertex2D { + position: posn_br, + color: color_gb, + }, + Vertex2D { + position: posn_b, + color: color_b, + }, + Vertex2D { + position: posn_bl, + color: color_i, + }, + Vertex2D { + position: posn_l, + color: color_v, + }, + ], + indices: vec![ + 0, 1, 2, // TL + 0, 2, 3, // T + 0, 3, 4, // TR + 0, 4, 5, // R + 0, 5, 6, // BR + 0, 6, 7, // B + 0, 7, 8, // BL + 0, 8, 1, // L + ], }, - mouse::Interaction::default(), - ) + }; + + renderer.with_translation(Vector::new(b.x, b.y), |renderer| { + renderer.draw_primitive(mesh); + }); } } diff --git a/examples/integration_opengl/src/controls.rs b/examples/integration_opengl/src/controls.rs index fa5aa91d..f387b4e5 100644 --- a/examples/integration_opengl/src/controls.rs +++ b/examples/integration_opengl/src/controls.rs @@ -1,9 +1,7 @@ use iced_glow::Renderer; -use iced_glutin::slider; -use iced_glutin::{ - Alignment, Color, Column, Command, Element, Length, Program, Row, Slider, - Text, -}; +use iced_glutin::widget::slider::{self, Slider}; +use iced_glutin::widget::{Column, Row, Text}; +use iced_glutin::{Alignment, Color, Command, Element, Length, Program}; pub struct Controls { background_color: Color, diff --git a/examples/integration_opengl/src/main.rs b/examples/integration_opengl/src/main.rs index f80915d2..f0ff2076 100644 --- a/examples/integration_opengl/src/main.rs +++ b/examples/integration_opengl/src/main.rs @@ -68,7 +68,6 @@ pub fn main() { let mut state = program::State::new( controls, viewport.logical_size(), - conversion::cursor_position(cursor_position, viewport.scale_factor()), &mut renderer, &mut debug, ); @@ -160,16 +159,19 @@ pub fn main() { } // And then iced on top - let mouse_interaction = renderer.backend_mut().draw( - &gl, - &viewport, - state.primitive(), - &debug.overlay(), - ); + renderer.present(|backend, primitive| { + backend.present( + &gl, + primitive, + &viewport, + &debug.overlay(), + ); + }); + // Update the mouse cursor windowed_context.window().set_cursor_icon( iced_winit::conversion::mouse_interaction( - mouse_interaction, + state.mouse_interaction(), ), ); diff --git a/examples/integration_wgpu/src/controls.rs b/examples/integration_wgpu/src/controls.rs index 414eb9ce..4f110bd2 100644 --- a/examples/integration_wgpu/src/controls.rs +++ b/examples/integration_wgpu/src/controls.rs @@ -1,8 +1,7 @@ use iced_wgpu::Renderer; -use iced_winit::{ - slider, Alignment, Color, Column, Command, Element, Length, Program, Row, - Slider, Text, -}; +use iced_winit::widget::slider::{self, Slider}; +use iced_winit::widget::{Column, Row, Text}; +use iced_winit::{Alignment, Color, Command, Element, Length, Program}; pub struct Controls { background_color: Color, diff --git a/examples/integration_wgpu/src/main.rs b/examples/integration_wgpu/src/main.rs index bf36c7a5..d1000748 100644 --- a/examples/integration_wgpu/src/main.rs +++ b/examples/integration_wgpu/src/main.rs @@ -94,7 +94,6 @@ pub fn main() { let mut state = program::State::new( controls, viewport.logical_size(), - conversion::cursor_position(cursor_position, viewport.scale_factor()), &mut renderer, &mut debug, ); @@ -196,15 +195,17 @@ pub fn main() { } // And then iced on top - let mouse_interaction = renderer.backend_mut().draw( - &mut device, - &mut staging_belt, - &mut encoder, - &view, - &viewport, - state.primitive(), - &debug.overlay(), - ); + renderer.present(|backend, primitive| { + backend.present( + &mut device, + &mut staging_belt, + &mut encoder, + &view, + primitive, + &viewport, + &debug.overlay(), + ); + }); // Then we submit the work staging_belt.finish(); @@ -212,11 +213,11 @@ pub fn main() { frame.present(); // Update the mouse cursor - window.set_cursor_icon( - iced_winit::conversion::mouse_interaction( - mouse_interaction, - ), - ); + window.set_cursor_icon( + iced_winit::conversion::mouse_interaction( + state.mouse_interaction(), + ), + ); // And recall staging buffers local_pool diff --git a/examples/pane_grid/src/main.rs b/examples/pane_grid/src/main.rs index 69872bad..8225e9e7 100644 --- a/examples/pane_grid/src/main.rs +++ b/examples/pane_grid/src/main.rs @@ -177,7 +177,11 @@ impl Application for Example { let title_bar = pane_grid::TitleBar::new(title) .controls(pane.controls.view(id, total_panes, pane.is_pinned)) .padding(10) - .style(style::TitleBar { is_focused }); + .style(if is_focused { + style::TitleBar::Focused + } else { + style::TitleBar::Active + }); pane_grid::Content::new(pane.content.view( id, @@ -185,7 +189,11 @@ impl Application for Example { pane.is_pinned, )) .title_bar(title_bar) - .style(style::Pane { is_focused }) + .style(if is_focused { + style::Pane::Focused + } else { + style::Pane::Active + }) }) .width(Length::Fill) .height(Length::Fill) @@ -387,14 +395,16 @@ mod style { 0xC4 as f32 / 255.0, ); - pub struct TitleBar { - pub is_focused: bool, + pub enum TitleBar { + Active, + Focused, } impl container::StyleSheet for TitleBar { fn style(&self) -> container::Style { - let pane = Pane { - is_focused: self.is_focused, + let pane = match self { + Self::Active => Pane::Active, + Self::Focused => Pane::Focused, } .style(); @@ -406,8 +416,9 @@ mod style { } } - pub struct Pane { - pub is_focused: bool, + pub enum Pane { + Active, + Focused, } impl container::StyleSheet for Pane { @@ -415,10 +426,9 @@ mod style { container::Style { background: Some(Background::Color(SURFACE)), border_width: 2.0, - border_color: if self.is_focused { - Color::BLACK - } else { - Color::from_rgb(0.7, 0.7, 0.7) + border_color: match self { + Self::Active => Color::from_rgb(0.7, 0.7, 0.7), + Self::Focused => Color::BLACK, }, ..Default::default() } diff --git a/examples/scrollable/src/style.rs b/examples/scrollable/src/style.rs index ae449141..ec1f13db 100644 --- a/examples/scrollable/src/style.rs +++ b/examples/scrollable/src/style.rs @@ -16,7 +16,7 @@ impl Default for Theme { } } -impl From<Theme> for Box<dyn container::StyleSheet> { +impl<'a> From<Theme> for Box<dyn container::StyleSheet + 'a> { fn from(theme: Theme) -> Self { match theme { Theme::Light => Default::default(), @@ -25,7 +25,7 @@ impl From<Theme> for Box<dyn container::StyleSheet> { } } -impl From<Theme> for Box<dyn radio::StyleSheet> { +impl<'a> From<Theme> for Box<dyn radio::StyleSheet + 'a> { fn from(theme: Theme) -> Self { match theme { Theme::Light => Default::default(), @@ -34,7 +34,7 @@ impl From<Theme> for Box<dyn radio::StyleSheet> { } } -impl From<Theme> for Box<dyn scrollable::StyleSheet> { +impl<'a> From<Theme> for Box<dyn scrollable::StyleSheet + 'a> { fn from(theme: Theme) -> Self { match theme { Theme::Light => Default::default(), diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 81d33ad3..262db03e 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -176,7 +176,7 @@ mod style { } } - impl From<Theme> for Box<dyn container::StyleSheet> { + impl<'a> From<Theme> for Box<dyn container::StyleSheet + 'a> { fn from(theme: Theme) -> Self { match theme { Theme::Light => Default::default(), @@ -185,7 +185,7 @@ mod style { } } - impl From<Theme> for Box<dyn radio::StyleSheet> { + impl<'a> From<Theme> for Box<dyn radio::StyleSheet + 'a> { fn from(theme: Theme) -> Self { match theme { Theme::Light => Default::default(), @@ -194,7 +194,7 @@ mod style { } } - impl From<Theme> for Box<dyn text_input::StyleSheet> { + impl<'a> From<Theme> for Box<dyn text_input::StyleSheet + 'a> { fn from(theme: Theme) -> Self { match theme { Theme::Light => Default::default(), @@ -203,7 +203,7 @@ mod style { } } - impl From<Theme> for Box<dyn button::StyleSheet> { + impl<'a> From<Theme> for Box<dyn button::StyleSheet + 'a> { fn from(theme: Theme) -> Self { match theme { Theme::Light => light::Button.into(), @@ -212,7 +212,7 @@ mod style { } } - impl From<Theme> for Box<dyn scrollable::StyleSheet> { + impl<'a> From<Theme> for Box<dyn scrollable::StyleSheet + 'a> { fn from(theme: Theme) -> Self { match theme { Theme::Light => Default::default(), @@ -221,7 +221,7 @@ mod style { } } - impl From<Theme> for Box<dyn slider::StyleSheet> { + impl<'a> From<Theme> for Box<dyn slider::StyleSheet + 'a> { fn from(theme: Theme) -> Self { match theme { Theme::Light => Default::default(), @@ -239,7 +239,7 @@ mod style { } } - impl From<Theme> for Box<dyn checkbox::StyleSheet> { + impl<'a> From<Theme> for Box<dyn checkbox::StyleSheet + 'a> { fn from(theme: Theme) -> Self { match theme { Theme::Light => Default::default(), diff --git a/examples/todos/src/main.rs b/examples/todos/src/main.rs index 11f23fd4..5ad8c208 100644 --- a/examples/todos/src/main.rs +++ b/examples/todos/src/main.rs @@ -363,8 +363,10 @@ impl Controls { let filter_button = |state, label, filter, current_filter| { let label = Text::new(label).size(16); let button = - Button::new(state, label).style(style::Button::Filter { - selected: filter == current_filter, + Button::new(state, label).style(if filter == current_filter { + style::Button::FilterSelected + } else { + style::Button::FilterActive }); button.on_press(Message::FilterChanged(filter)).padding(8) @@ -602,7 +604,8 @@ mod style { use iced::{button, Background, Color, Vector}; pub enum Button { - Filter { selected: bool }, + FilterActive, + FilterSelected, Icon, Destructive, } @@ -610,20 +613,15 @@ mod style { impl button::StyleSheet for Button { fn active(&self) -> button::Style { match self { - Button::Filter { selected } => { - if *selected { - button::Style { - background: Some(Background::Color( - Color::from_rgb(0.2, 0.2, 0.7), - )), - border_radius: 10.0, - text_color: Color::WHITE, - ..button::Style::default() - } - } else { - button::Style::default() - } - } + Button::FilterActive => button::Style::default(), + Button::FilterSelected => button::Style { + background: Some(Background::Color(Color::from_rgb( + 0.2, 0.2, 0.7, + ))), + border_radius: 10.0, + text_color: Color::WHITE, + ..button::Style::default() + }, Button::Icon => button::Style { text_color: Color::from_rgb(0.5, 0.5, 0.5), ..button::Style::default() @@ -646,9 +644,7 @@ mod style { button::Style { text_color: match self { Button::Icon => Color::from_rgb(0.2, 0.2, 0.7), - Button::Filter { selected } if !selected => { - Color::from_rgb(0.2, 0.2, 0.7) - } + Button::FilterActive => Color::from_rgb(0.2, 0.2, 0.7), _ => active.text_color, }, shadow_offset: active.shadow_offset + Vector::new(0.0, 1.0), |