From f6ce981e5885f5b199dd765edb3f601447fcf77e Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Mon, 5 Feb 2024 10:17:16 -0500 Subject: [PATCH] chore: fix pong example --- Cargo.toml | 3 --- examples/pong/src/layer.rs | 42 ++++++++++++++++---------------------- examples/pong/src/main.rs | 26 +++++++++++++---------- examples/pong/src/theme.rs | 10 ++++++--- 4 files changed, 40 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 15185fe..85f9007 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,9 +22,6 @@ libcosmic = ["dep:libcosmic"] members = [ "examples/*" ] -exclude = [ - "examples/stopwatch" -] [dependencies] iced = { git = "https://github.com/iced-rs/iced", rev = "5540ac0", features = ["tokio"], optional = true } diff --git a/examples/pong/src/layer.rs b/examples/pong/src/layer.rs index 30a4e0a..fc92b62 100644 --- a/examples/pong/src/layer.rs +++ b/examples/pong/src/layer.rs @@ -12,17 +12,19 @@ use iced_core::{ Point, Rectangle, Shell, Size, Widget, }; +use crate::theme::Theme; + /// A simple widget that layers one above another. pub struct Layer<'a, Message, Renderer> { - base: Element<'a, Message, Renderer>, - layer: Element<'a, Message, Renderer>, + base: Element<'a, Message, Theme, Renderer>, + layer: Element<'a, Message, Theme, Renderer>, } impl<'a, Message, Renderer> Layer<'a, Message, Renderer> { /// Returns a new [`Layer`] pub fn new( - base: impl Into>, - layer: impl Into>, + base: impl Into>, + layer: impl Into>, ) -> Self { Self { base: base.into(), @@ -31,7 +33,7 @@ impl<'a, Message, Renderer> Layer<'a, Message, Renderer> { } } -impl<'a, Message, Renderer> Widget for Layer<'a, Message, Renderer> +impl<'a, Message, Renderer> Widget for Layer<'a, Message, Renderer> where Renderer: iced_core::Renderer, Message: Clone, @@ -44,12 +46,8 @@ where tree.diff_children(&[&self.base, &self.layer]); } - fn width(&self) -> Length { - self.base.as_widget().width() - } - - fn height(&self) -> Length { - self.base.as_widget().height() + fn size(&self) -> Size { + self.base.as_widget().size() } fn layout( @@ -90,7 +88,7 @@ where &self, state: &Tree, renderer: &mut Renderer, - theme: &::Theme, + theme: &Theme, style: &renderer::Style, layout: Layout<'_>, cursor_position: mouse::Cursor, @@ -112,7 +110,7 @@ where state: &'b mut Tree, layout: Layout<'_>, _renderer: &Renderer, - ) -> Option> { + ) -> Option> { Some(overlay::Element::new( layout.position(), Box::new(Overlay { @@ -154,12 +152,12 @@ where } struct Overlay<'a, 'b, Message, Renderer> { - content: &'b mut Element<'a, Message, Renderer>, + content: &'b mut Element<'a, Message, Theme, Renderer>, tree: &'b mut Tree, size: Size, } -impl<'a, 'b, Message, Renderer> overlay::Overlay +impl<'a, 'b, Message, Renderer> overlay::Overlay for Overlay<'a, 'b, Message, Renderer> where Renderer: iced_core::Renderer, @@ -180,10 +178,7 @@ where .content .as_widget() .layout(&mut self.tree.children[0], renderer, &limits); - let mut node = layout::Node::with_children(self.size, vec![child]); - node.move_to(position); - - node + layout::Node::with_children(self.size, vec![child]).move_to(position) } fn on_event( @@ -210,7 +205,7 @@ where fn draw( &self, renderer: &mut Renderer, - theme: &Renderer::Theme, + theme: &Theme, style: &renderer::Style, layout: Layout<'_>, cursor_position: mouse::Cursor, @@ -218,9 +213,7 @@ where renderer.fill_quad( renderer::Quad { bounds: layout.bounds(), - border_radius: iced_core::BorderRadius::from(0.0), - border_width: 0.0, - border_color: Color::TRANSPARENT, + ..Default::default() }, Color { a: 0., @@ -270,7 +263,8 @@ where } } -impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> +impl<'a, Message, Renderer> From> + for Element<'a, Message, Theme, Renderer> where Renderer: 'a + iced_core::Renderer, Message: 'a + Clone, diff --git a/examples/pong/src/main.rs b/examples/pong/src/main.rs index 9f13fc5..e8f7c57 100644 --- a/examples/pong/src/main.rs +++ b/examples/pong/src/main.rs @@ -1,6 +1,7 @@ +use cosmic_time::reexports::iced_core::keyboard::key::Named; use cosmic_time::reexports::iced_futures::event::listen_raw; use iced::event; -use iced::keyboard::{self, KeyCode}; +use iced::keyboard; use iced::widget::{column, container, row, Space}; use iced::{executor, Application, Command, Event, Length, Settings, Subscription}; @@ -96,20 +97,23 @@ impl Application for Pong { self.timeline.as_subscription::().map(Message::Tick), listen_raw(|event, status| match (event, status) { ( - Event::Keyboard(keyboard::Event::KeyPressed { - key_code, - modifiers: _, - }), + Event::Keyboard(keyboard::Event::KeyPressed { key, .. }), event::Status::Ignored, - ) => match key_code { - KeyCode::W => Some(Message::Paddle(Paddle::LeftUp)), - KeyCode::S => Some(Message::Paddle(Paddle::LeftDown)), - KeyCode::Up => Some(Message::Paddle(Paddle::RightUp)), - KeyCode::Down => Some(Message::Paddle(Paddle::RightDown)), + ) => match key { + keyboard::Key::Character(c) if c == "w" => { + Some(Message::Paddle(Paddle::LeftUp)) + } + keyboard::Key::Character(c) if c == "s" => { + Some(Message::Paddle(Paddle::LeftDown)) + } + keyboard::Key::Named(Named::ArrowUp) => Some(Message::Paddle(Paddle::RightUp)), + keyboard::Key::Named(Named::ArrowDown) => { + Some(Message::Paddle(Paddle::RightDown)) + } _ => None, }, ( - Event::Window(window::Event::Resized { width, height }), + Event::Window(_, window::Event::Resized { width, height }), event::Status::Ignored, ) => Some(Message::WindowResized(width, height)), _ => None, diff --git a/examples/pong/src/theme.rs b/examples/pong/src/theme.rs index 5e6afa9..f0e8ec6 100644 --- a/examples/pong/src/theme.rs +++ b/examples/pong/src/theme.rs @@ -1,4 +1,5 @@ use cosmic_time::reexports::iced; +use cosmic_time::reexports::iced_core::Border; use iced::widget::{container, text}; use iced::{application, color}; @@ -49,7 +50,10 @@ impl container::StyleSheet for Theme { }, Container::Ball => container::Appearance { background: Some(color!(0xff, 0xff, 0xff).into()), - border_radius: 100000.0.into(), + border: Border { + radius: 100000.0.into(), + ..Default::default() + }, ..Default::default() }, } @@ -62,7 +66,7 @@ pub mod widget { use crate::theme::Theme; - pub type Renderer = iced::Renderer; - pub type Element<'a, Message> = iced::Element<'a, Message, Renderer>; + pub type Renderer = iced::Renderer; + pub type Element<'a, Message> = iced::Element<'a, Message, Theme, Renderer>; pub type Container<'a, Message> = iced::widget::Container<'a, Message, Renderer>; }