From 897188317b5875cc00a0f1c797790df8ac13687f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 11 Feb 2022 17:50:12 +0700 Subject: Rename `iced_virtual` to `iced_pure` `virtual` is a reserved keyword in Rust :grimacing: --- Cargo.toml | 4 +- examples/pure/counter/Cargo.toml | 10 ++ examples/pure/counter/README.md | 18 +++ examples/pure/counter/index.html | 12 ++ examples/pure/counter/src/main.rs | 54 +++++++ examples/virtual_counter/Cargo.toml | 10 -- examples/virtual_counter/README.md | 18 --- examples/virtual_counter/index.html | 12 -- examples/virtual_counter/src/main.rs | 54 ------- pure/Cargo.toml | 8 ++ pure/src/application.rs | 1 + pure/src/flex.rs | 232 ++++++++++++++++++++++++++++++ pure/src/lib.rs | 146 +++++++++++++++++++ pure/src/widget.rs | 73 ++++++++++ pure/src/widget/button.rs | 272 +++++++++++++++++++++++++++++++++++ pure/src/widget/column.rs | 220 ++++++++++++++++++++++++++++ pure/src/widget/element.rs | 21 +++ pure/src/widget/text.rs | 185 ++++++++++++++++++++++++ pure/src/widget/tree.rs | 58 ++++++++ virtual/Cargo.toml | 8 -- virtual/src/flex.rs | 232 ------------------------------ virtual/src/lib.rs | 146 ------------------- virtual/src/widget.rs | 73 ---------- virtual/src/widget/button.rs | 272 ----------------------------------- virtual/src/widget/column.rs | 220 ---------------------------- virtual/src/widget/element.rs | 21 --- virtual/src/widget/text.rs | 185 ------------------------ virtual/src/widget/tree.rs | 58 -------- 28 files changed, 1312 insertions(+), 1311 deletions(-) create mode 100644 examples/pure/counter/Cargo.toml create mode 100644 examples/pure/counter/README.md create mode 100644 examples/pure/counter/index.html create mode 100644 examples/pure/counter/src/main.rs delete mode 100644 examples/virtual_counter/Cargo.toml delete mode 100644 examples/virtual_counter/README.md delete mode 100644 examples/virtual_counter/index.html delete mode 100644 examples/virtual_counter/src/main.rs create mode 100644 pure/Cargo.toml create mode 100644 pure/src/application.rs create mode 100644 pure/src/flex.rs create mode 100644 pure/src/lib.rs create mode 100644 pure/src/widget.rs create mode 100644 pure/src/widget/button.rs create mode 100644 pure/src/widget/column.rs create mode 100644 pure/src/widget/element.rs create mode 100644 pure/src/widget/text.rs create mode 100644 pure/src/widget/tree.rs delete mode 100644 virtual/Cargo.toml delete mode 100644 virtual/src/flex.rs delete mode 100644 virtual/src/lib.rs delete mode 100644 virtual/src/widget.rs delete mode 100644 virtual/src/widget/button.rs delete mode 100644 virtual/src/widget/column.rs delete mode 100644 virtual/src/widget/element.rs delete mode 100644 virtual/src/widget/text.rs delete mode 100644 virtual/src/widget/tree.rs diff --git a/Cargo.toml b/Cargo.toml index 1d4eb883..1918d8b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,8 +57,8 @@ members = [ "glutin", "lazy", "native", + "pure", "style", - "virtual", "wgpu", "winit", "examples/bezier_tool", @@ -88,7 +88,7 @@ members = [ "examples/tooltip", "examples/tour", "examples/url_handler", - "examples/virtual_counter", + "examples/pure/counter", "examples/websocket", ] diff --git a/examples/pure/counter/Cargo.toml b/examples/pure/counter/Cargo.toml new file mode 100644 index 00000000..1363bfd5 --- /dev/null +++ b/examples/pure/counter/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "pure_counter" +version = "0.1.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../../.." } +iced_pure = { path = "../../../pure" } diff --git a/examples/pure/counter/README.md b/examples/pure/counter/README.md new file mode 100644 index 00000000..4d9fc5b9 --- /dev/null +++ b/examples/pure/counter/README.md @@ -0,0 +1,18 @@ +## Counter + +The classic counter example explained in the [`README`](../../README.md). + +The __[`main`]__ file contains all the code of the example. + +
+ + + +
+ +You can run it with `cargo run`: +``` +cargo run --package counter +``` + +[`main`]: src/main.rs diff --git a/examples/pure/counter/index.html b/examples/pure/counter/index.html new file mode 100644 index 00000000..d2e368e4 --- /dev/null +++ b/examples/pure/counter/index.html @@ -0,0 +1,12 @@ + + + + + + Counter - Iced + + + + + + diff --git a/examples/pure/counter/src/main.rs b/examples/pure/counter/src/main.rs new file mode 100644 index 00000000..e2746d40 --- /dev/null +++ b/examples/pure/counter/src/main.rs @@ -0,0 +1,54 @@ +use iced::{Alignment, Element, Sandbox, Settings}; +use iced_pure::{Button, Column, Pure, State, Text}; + +pub fn main() -> iced::Result { + Counter::run(Settings::default()) +} + +struct Counter { + value: i32, + state: State, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + IncrementPressed, + DecrementPressed, +} + +impl Sandbox for Counter { + type Message = Message; + + fn new() -> Self { + Self { + value: 0, + state: State::new(), + } + } + + fn title(&self) -> String { + String::from("Counter - Iced") + } + + fn update(&mut self, message: Message) { + match message { + Message::IncrementPressed => { + self.value += 1; + } + Message::DecrementPressed => { + self.value -= 1; + } + } + } + + fn view(&mut self) -> Element<'_, Message> { + let content = Column::new() + .padding(20) + .align_items(Alignment::Center) + .push(Button::new("Increment").on_press(Message::IncrementPressed)) + .push(Text::new(self.value.to_string()).size(50)) + .push(Button::new("Decrement").on_press(Message::DecrementPressed)); + + Pure::new(&mut self.state, content).into() + } +} diff --git a/examples/virtual_counter/Cargo.toml b/examples/virtual_counter/Cargo.toml deleted file mode 100644 index 537d4174..00000000 --- a/examples/virtual_counter/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "virtual_counter" -version = "0.1.0" -authors = ["Héctor Ramón Jiménez "] -edition = "2021" -publish = false - -[dependencies] -iced = { path = "../.." } -iced_virtual = { path = "../../virtual" } diff --git a/examples/virtual_counter/README.md b/examples/virtual_counter/README.md deleted file mode 100644 index 4d9fc5b9..00000000 --- a/examples/virtual_counter/README.md +++ /dev/null @@ -1,18 +0,0 @@ -## Counter - -The classic counter example explained in the [`README`](../../README.md). - -The __[`main`]__ file contains all the code of the example. - -
- - - -
- -You can run it with `cargo run`: -``` -cargo run --package counter -``` - -[`main`]: src/main.rs diff --git a/examples/virtual_counter/index.html b/examples/virtual_counter/index.html deleted file mode 100644 index d2e368e4..00000000 --- a/examples/virtual_counter/index.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - Counter - Iced - - - - - - diff --git a/examples/virtual_counter/src/main.rs b/examples/virtual_counter/src/main.rs deleted file mode 100644 index 10d32112..00000000 --- a/examples/virtual_counter/src/main.rs +++ /dev/null @@ -1,54 +0,0 @@ -use iced::{Alignment, Element, Sandbox, Settings}; -use iced_virtual::{Button, Column, Text, Virtual}; - -pub fn main() -> iced::Result { - Counter::run(Settings::default()) -} - -struct Counter { - value: i32, - state: iced_virtual::State, -} - -#[derive(Debug, Clone, Copy)] -enum Message { - IncrementPressed, - DecrementPressed, -} - -impl Sandbox for Counter { - type Message = Message; - - fn new() -> Self { - Self { - value: 0, - state: iced_virtual::State::new(), - } - } - - fn title(&self) -> String { - String::from("Counter - Iced") - } - - fn update(&mut self, message: Message) { - match message { - Message::IncrementPressed => { - self.value += 1; - } - Message::DecrementPressed => { - self.value -= 1; - } - } - } - - fn view(&mut self) -> Element { - let content = Column::new() - .padding(20) - .align_items(Alignment::Center) - .push(Button::new("Increment").on_press(Message::IncrementPressed)) - .push(Text::new(self.value.to_string()).size(50)) - .push(Button::new("Decrement").on_press(Message::DecrementPressed)); - - Virtual::new(&mut self.state, content).into() - } -} diff --git a/pure/Cargo.toml b/pure/Cargo.toml new file mode 100644 index 00000000..bdfe43ae --- /dev/null +++ b/pure/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "iced_pure" +version = "0.1.0" +edition = "2021" + +[dependencies] +iced_native = { version = "0.4", path = "../native" } +iced_style = { version = "0.3", path = "../style" } diff --git a/pure/src/application.rs b/pure/src/application.rs new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/pure/src/application.rs @@ -0,0 +1 @@ + diff --git a/pure/src/flex.rs b/pure/src/flex.rs new file mode 100644 index 00000000..8d473f08 --- /dev/null +++ b/pure/src/flex.rs @@ -0,0 +1,232 @@ +//! Distribute elements using a flex-based layout. +// This code is heavily inspired by the [`druid`] codebase. +// +// [`druid`]: https://github.com/xi-editor/druid +// +// Copyright 2018 The xi-editor Authors, Héctor Ramón +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +use crate::Element; + +use iced_native::layout::{Limits, Node}; +use iced_native::{Alignment, Padding, Point, Size}; + +/// The main axis of a flex layout. +#[derive(Debug)] +pub enum Axis { + /// The horizontal axis + Horizontal, + + /// The vertical axis + Vertical, +} + +impl Axis { + fn main(&self, size: Size) -> f32 { + match self { + Axis::Horizontal => size.width, + Axis::Vertical => size.height, + } + } + + fn cross(&self, size: Size) -> f32 { + match self { + Axis::Horizontal => size.height, + Axis::Vertical => size.width, + } + } + + fn pack(&self, main: f32, cross: f32) -> (f32, f32) { + match self { + Axis::Horizontal => (main, cross), + Axis::Vertical => (cross, main), + } + } +} + +/// Computes the flex layout with the given axis and limits, applying spacing, +/// padding and alignment to the items as needed. +/// +/// It returns a new layout [`Node`]. +pub fn resolve( + axis: Axis, + renderer: &Renderer, + limits: &Limits, + padding: Padding, + spacing: f32, + align_items: Alignment, + items: &[Element], +) -> Node +where + Renderer: iced_native::Renderer, +{ + let limits = limits.pad(padding); + let total_spacing = spacing * items.len().saturating_sub(1) as f32; + let max_cross = axis.cross(limits.max()); + + let mut fill_sum = 0; + let mut cross = axis.cross(limits.min()).max(axis.cross(limits.fill())); + let mut available = axis.main(limits.max()) - total_spacing; + + let mut nodes: Vec = Vec::with_capacity(items.len()); + nodes.resize(items.len(), Node::default()); + + if align_items == Alignment::Fill { + let mut fill_cross = axis.cross(limits.min()); + + items.iter().for_each(|child| { + let cross_fill_factor = match axis { + Axis::Horizontal => child.as_widget().height(), + Axis::Vertical => child.as_widget().width(), + } + .fill_factor(); + + if cross_fill_factor == 0 { + let (max_width, max_height) = axis.pack(available, max_cross); + + let child_limits = + Limits::new(Size::ZERO, Size::new(max_width, max_height)); + + let layout = child.as_widget().layout(renderer, &child_limits); + let size = layout.size(); + + fill_cross = fill_cross.max(axis.cross(size)); + } + }); + + cross = fill_cross; + } + + for (i, child) in items.iter().enumerate() { + let fill_factor = match axis { + Axis::Horizontal => child.as_widget().width(), + Axis::Vertical => child.as_widget().height(), + } + .fill_factor(); + + if fill_factor == 0 { + let (min_width, min_height) = if align_items == Alignment::Fill { + axis.pack(0.0, cross) + } else { + axis.pack(0.0, 0.0) + }; + + let (max_width, max_height) = if align_items == Alignment::Fill { + axis.pack(available, cross) + } else { + axis.pack(available, max_cross) + }; + + let child_limits = Limits::new( + Size::new(min_width, min_height), + Size::new(max_width, max_height), + ); + + let layout = child.as_widget().layout(renderer, &child_limits); + let size = layout.size(); + + available -= axis.main(size); + + if align_items != Alignment::Fill { + cross = cross.max(axis.cross(size)); + } + + nodes[i] = layout; + } else { + fill_sum += fill_factor; + } + } + + let remaining = available.max(0.0); + + for (i, child) in items.iter().enumerate() { + let fill_factor = match axis { + Axis::Horizontal => child.as_widget().width(), + Axis::Vertical => child.as_widget().height(), + } + .fill_factor(); + + if fill_factor != 0 { + let max_main = remaining * fill_factor as f32 / fill_sum as f32; + let min_main = if max_main.is_infinite() { + 0.0 + } else { + max_main + }; + + let (min_width, min_height) = if align_items == Alignment::Fill { + axis.pack(min_main, cross) + } else { + axis.pack(min_main, axis.cross(limits.min())) + }; + + let (max_width, max_height) = if align_items == Alignment::Fill { + axis.pack(max_main, cross) + } else { + axis.pack(max_main, max_cross) + }; + + let child_limits = Limits::new( + Size::new(min_width, min_height), + Size::new(max_width, max_height), + ); + + let layout = child.as_widget().layout(renderer, &child_limits); + + if align_items != Alignment::Fill { + cross = cross.max(axis.cross(layout.size())); + } + + nodes[i] = layout; + } + } + + let pad = axis.pack(padding.left as f32, padding.top as f32); + let mut main = pad.0; + + for (i, node) in nodes.iter_mut().enumerate() { + if i > 0 { + main += spacing; + } + + let (x, y) = axis.pack(main, pad.1); + + node.move_to(Point::new(x, y)); + + match axis { + Axis::Horizontal => { + node.align( + Alignment::Start, + align_items, + Size::new(0.0, cross), + ); + } + Axis::Vertical => { + node.align( + align_items, + Alignment::Start, + Size::new(cross, 0.0), + ); + } + } + + let size = node.size(); + + main += axis.main(size); + } + + let (width, height) = axis.pack(main - pad.0, cross); + let size = limits.resolve(Size::new(width, height)); + + Node::with_children(size.pad(padding), nodes) +} diff --git a/pure/src/lib.rs b/pure/src/lib.rs new file mode 100644 index 00000000..4381bfc8 --- /dev/null +++ b/pure/src/lib.rs @@ -0,0 +1,146 @@ +pub mod widget; + +pub(crate) mod flex; + +pub use widget::*; + +use iced_native::event::{self, Event}; +use iced_native::layout::{self, Layout}; +use iced_native::mouse; +use iced_native::renderer; +use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell}; + +pub struct Pure<'a, Message, Renderer> { + state: &'a mut State, +} + +impl<'a, Message, Renderer> Pure<'a, Message, Renderer> +where + Message: 'static, + Renderer: iced_native::Renderer + 'static, +{ + pub fn new( + state: &'a mut State, + content: impl Into>, + ) -> Self { + let _ = state.diff(content.into()); + + Self { state } + } +} + +pub struct State { + state_tree: widget::Tree, + last_element: Element, +} + +impl State +where + Message: 'static, + Renderer: iced_native::Renderer + 'static, +{ + pub fn new() -> Self { + let last_element = Element::new(widget::Column::new()); + + Self { + state_tree: widget::Tree::new(&last_element), + last_element, + } + } + + fn diff(&mut self, new_element: Element) { + self.state_tree.diff(&self.last_element, &new_element); + + self.last_element = new_element; + } +} + +impl<'a, Message, Renderer> iced_native::Widget + for Pure<'a, Message, Renderer> +where + Renderer: iced_native::Renderer, +{ + fn width(&self) -> Length { + self.state.last_element.as_widget().width() + } + + fn height(&self) -> Length { + self.state.last_element.as_widget().height() + } + + fn hash_layout(&self, state: &mut Hasher) { + self.state.last_element.as_widget().hash_layout(state) + } + + fn layout( + &self, + renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + self.state.last_element.as_widget().layout(renderer, limits) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + renderer: &Renderer, + clipboard: &mut dyn Clipboard, + shell: &mut Shell<'_, Message>, + ) -> event::Status { + self.state.last_element.as_widget_mut().on_event( + &mut self.state.state_tree, + event, + layout, + cursor_position, + renderer, + clipboard, + shell, + ) + } + + fn draw( + &self, + renderer: &mut Renderer, + style: &renderer::Style, + layout: Layout<'_>, + cursor_position: Point, + viewport: &Rectangle, + ) { + self.state.last_element.as_widget().draw( + &self.state.state_tree, + renderer, + style, + layout, + cursor_position, + viewport, + ) + } + + fn mouse_interaction( + &self, + layout: Layout<'_>, + cursor_position: Point, + viewport: &Rectangle, + renderer: &Renderer, + ) -> mouse::Interaction { + self.state.last_element.as_widget().mouse_interaction( + &self.state.state_tree, + layout, + cursor_position, + viewport, + renderer, + ) + } +} + +impl<'a, Message, Renderer> Into> + for Pure<'a, Message, Renderer> +where + Renderer: iced_native::Renderer, +{ + fn into(self) -> iced_native::Element<'a, Message, Renderer> { + iced_native::Element::new(self) + } +} diff --git a/pure/src/widget.rs b/pure/src/widget.rs new file mode 100644 index 00000000..bf63b999 --- /dev/null +++ b/pure/src/widget.rs @@ -0,0 +1,73 @@ +mod button; +mod column; +mod element; +mod text; +mod tree; + +pub use button::Button; +pub use column::Column; +pub use element::Element; +pub use text::Text; +pub use tree::Tree; + +use iced_native::event::{self, Event}; +use iced_native::layout::{self, Layout}; +use iced_native::mouse; +use iced_native::renderer; +use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell}; + +use std::any::{self, Any}; + +pub trait Widget { + fn tag(&self) -> any::TypeId; + + fn state(&self) -> Box; + + fn children(&self) -> &[Element]; + + fn width(&self) -> Length; + + fn height(&self) -> Length; + + fn hash_layout(&self, state: &mut Hasher); + + fn layout( + &self, + renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node; + + fn draw( + &self, + state: &Tree, + renderer: &mut Renderer, + style: &renderer::Style, + layout: Layout<'_>, + cursor_position: Point, + viewport: &Rectangle, + ); + + fn mouse_interaction( + &self, + _state: &Tree, + _layout: Layout<'_>, + _cursor_position: Point, + _viewport: &Rectangle, + _renderer: &Renderer, + ) -> mouse::Interaction { + mouse::Interaction::Idle + } + + fn on_event( + &mut self, + _state: &mut Tree, + _event: Event, + _layout: Layout<'_>, + _cursor_position: Point, + _renderer: &Renderer, + _clipboard: &mut dyn Clipboard, + _shell: &mut Shell<'_, Message>, + ) -> event::Status { + event::Status::Ignored + } +} diff --git a/pure/src/widget/button.rs b/pure/src/widget/button.rs new file mode 100644 index 00000000..ece90811 --- /dev/null +++ b/pure/src/widget/button.rs @@ -0,0 +1,272 @@ +use crate::widget::{Element, Tree, Widget}; + +use iced_native::event::{self, Event}; +use iced_native::layout; +use iced_native::mouse; +use iced_native::renderer; +use iced_native::touch; +use iced_native::{ + Background, Clipboard, Color, Hasher, Layout, Length, Padding, Point, + Rectangle, Shell, Vector, +}; +use iced_style::button::StyleSheet; + +use std::any::Any; + +pub struct Button { + content: Element, + on_press: Option, + style_sheet: Box, + width: Length, + height: Length, + padding: Padding, +} + +impl Button { + pub fn new(content: impl Into>) -> Self { + Button { + content: content.into(), + on_press: None, + style_sheet: Default::default(), + width: Length::Shrink, + height: Length::Shrink, + padding: Padding::new(5), + } + } + + pub fn on_press(mut self, on_press: Message) -> Self { + self.on_press = Some(on_press); + self + } +} + +impl Widget for Button +where + Message: 'static + Clone, + Renderer: 'static + iced_native::Renderer, +{ + fn tag(&self) -> std::any::TypeId { + std::any::TypeId::of::() + } + + fn state(&self) -> Box { + Box::new(State { is_pressed: false }) + } + + fn children(&self) -> &[Element] { + std::slice::from_ref(&self.content) + } + + fn width(&self) -> Length { + self.width + } + + fn height(&self) -> Length { + self.height + } + + fn hash_layout(&self, state: &mut Hasher) { + use std::hash::Hash; + + self.tag().hash(state); + self.width.hash(state); + self.content.as_widget().hash_layout(state); + } + + fn layout( + &self, + renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + let limits = limits + .width(self.width) + .height(self.height) + .pad(self.padding); + + let mut content = self.content.as_widget().layout(renderer, &limits); + content.move_to(Point::new( + self.padding.left.into(), + self.padding.top.into(), + )); + + let size = limits.resolve(content.size()).pad(self.padding); + + layout::Node::with_children(size, vec![content]) + } + + fn on_event( + &mut self, + tree: &mut Tree, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + renderer: &Renderer, + clipboard: &mut dyn Clipboard, + shell: &mut Shell<'_, Message>, + ) -> event::Status { + let state = if let Some(state) = tree.state.downcast_mut::() { + state + } else { + return event::Status::Ignored; + }; + + if let event::Status::Captured = self.content.as_widget_mut().on_event( + &mut tree.children[0], + event.clone(), + layout.children().next().unwrap(), + cursor_position, + renderer, + clipboard, + shell, + ) { + return event::Status::Captured; + } + + match event { + Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) + | Event::Touch(touch::Event::FingerPressed { .. }) => { + if self.on_press.is_some() { + let bounds = layout.bounds(); + + if bounds.contains(cursor_position) { + state.is_pressed = true; + + return event::Status::Captured; + } + } + } + Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) + | Event::Touch(touch::Event::FingerLifted { .. }) => { + if let Some(on_press) = self.on_press.clone() { + let bounds = layout.bounds(); + + if state.is_pressed { + state.is_pressed = false; + + if bounds.contains(cursor_position) { + shell.publish(on_press); + } + + return event::Status::Captured; + } + } + } + Event::Touch(touch::Event::FingerLost { .. }) => { + state.is_pressed = false; + } + _ => {} + } + + event::Status::Ignored + } + + fn draw( + &self, + tree: &Tree, + renderer: &mut Renderer, + _style: &renderer::Style, + layout: Layout<'_>, + cursor_position: Point, + _viewport: &Rectangle, + ) { + let state = if let Some(state) = tree.state.downcast_ref::() { + state + } else { + return; + }; + + let bounds = layout.bounds(); + let content_layout = layout.children().next().unwrap(); + + let is_mouse_over = bounds.contains(cursor_position); + let is_disabled = self.on_press.is_none(); + + let styling = if is_disabled { + self.style_sheet.disabled() + } else if is_mouse_over { + if state.is_pressed { + self.style_sheet.pressed() + } else { + self.style_sheet.hovered() + } + } else { + self.style_sheet.active() + }; + + if styling.background.is_some() || styling.border_width > 0.0 { + if styling.shadow_offset != Vector::default() { + // TODO: Implement proper shadow support + renderer.fill_quad( + renderer::Quad { + bounds: Rectangle { + x: bounds.x + styling.shadow_offset.x, + y: bounds.y + styling.shadow_offset.y, + ..bounds + }, + border_radius: styling.border_radius, + border_width: 0.0, + border_color: Color::TRANSPARENT, + }, + Background::Color([0.0, 0.0, 0.0, 0.5].into()), + ); + } + + renderer.fill_quad( + renderer::Quad { + bounds, + border_radius: styling.border_radius, + border_width: styling.border_width, + border_color: styling.border_color, + }, + styling + .background + .unwrap_or(Background::Color(Color::TRANSPARENT)), + ); + } + + self.content.as_widget().draw( + &tree.children[0], + renderer, + &renderer::Style { + text_color: styling.text_color, + }, + content_layout, + cursor_position, + &bounds, + ); + } + + fn mouse_interaction( + &self, + _tree: &Tree, + layout: Layout<'_>, + cursor_position: Point, + _viewport: &Rectangle, + _renderer: &Renderer, + ) -> mouse::Interaction { + let is_mouse_over = layout.bounds().contains(cursor_position); + let is_disabled = self.on_press.is_none(); + + if is_mouse_over && !is_disabled { + mouse::Interaction::Pointer + } else { + mouse::Interaction::default() + } + } +} + +#[derive(Debug, Clone)] +struct State { + is_pressed: bool, +} + +impl Into> + for Button +where + Message: Clone + 'static, + Renderer: iced_native::Renderer + 'static, +{ + fn into(self) -> Element { + Element::new(self) + } +} diff --git a/pure/src/widget/column.rs b/pure/src/widget/column.rs new file mode 100644 index 00000000..2f70282a --- /dev/null +++ b/pure/src/widget/column.rs @@ -0,0 +1,220 @@ +use crate::flex; +use crate::widget::{Element, Tree, Widget}; + +use iced_native::event::{self, Event}; +use iced_native::layout::{self, Layout}; +use iced_native::mouse; +use iced_native::renderer; +use iced_native::{ + Alignment, Clipboard, Hasher, Length, Padding, Point, Rectangle, Shell, +}; + +use std::any::{self, Any}; + +pub struct Column { + spacing: u16, + padding: Padding, + width: Length, + height: Length, + align_items: Alignment, + children: Vec>, +} + +impl<'a, Message, Renderer> Column { + pub fn new() -> Self { + Self::with_children(Vec::new()) + } + + pub fn with_children(children: Vec>) -> Self { + Column { + spacing: 0, + padding: Padding::ZERO, + width: Length::Shrink, + height: Length::Shrink, + align_items: Alignment::Start, + children, + } + } + + pub fn spacing(mut self, units: u16) -> Self { + self.spacing = units; + self + } + + pub fn padding>(mut self, padding: P) -> Self { + self.padding = padding.into(); + self + } + + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } + + pub fn height(mut self, height: Length) -> Self { + self.height = height; + self + } + + pub fn align_items(mut self, align: Alignment) -> Self { + self.align_items = align; + self + } + + pub fn push( + mut self, + child: impl Into>, + ) -> Self { + self.children.push(child.into()); + self + } +} + +impl Widget for Column +where + Renderer: iced_native::Renderer, +{ + fn tag(&self) -> any::TypeId { + struct Marker; + any::TypeId::of::() + } + + fn state(&self) -> Box { + Box::new(()) + } + + fn children(&self) -> &[Element] { + &self.children + } + + fn width(&self) -> Length { + self.width + } + + fn height(&self) -> Length { + self.height + } + + fn layout( + &self, + renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + let limits = limits.width(self.width).height(self.height); + + flex::resolve( + flex::Axis::Vertical, + renderer, + &limits, + self.padding, + self.spacing as f32, + self.align_items, + &self.children, + ) + } + + fn on_event( + &mut self, + tree: &mut Tree, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + renderer: &Renderer, + clipboard: &mut dyn Clipboard, + shell: &mut Shell<'_, Message>, + ) -> event::Status { + self.children + .iter_mut() + .zip(&mut tree.children) + .zip(layout.children()) + .map(|((child, state), layout)| { + child.as_widget_mut().on_event( + state, + event.clone(), + layout, + cursor_position, + renderer, + clipboard, + shell, + ) + }) + .fold(event::Status::Ignored, event::Status::merge) + } + + fn mouse_interaction( + &self, + tree: &Tree, + layout: Layout<'_>, + cursor_position: Point, + viewport: &Rectangle, + renderer: &Renderer, + ) -> mouse::Interaction { + self.children + .iter() + .zip(&tree.children) + .zip(layout.children()) + .map(|((child, state), layout)| { + child.as_widget().mouse_interaction( + state, + layout, + cursor_position, + viewport, + renderer, + ) + }) + .max() + .unwrap_or_default() + } + + fn draw( + &self, + tree: &Tree, + renderer: &mut Renderer, + style: &renderer::Style, + layout: Layout<'_>, + cursor_position: Point, + viewport: &Rectangle, + ) { + for ((child, state), layout) in self + .children + .iter() + .zip(&tree.children) + .zip(layout.children()) + { + child.as_widget().draw( + state, + renderer, + style, + layout, + cursor_position, + viewport, + ); + } + } + + fn hash_layout(&self, state: &mut Hasher) { + use std::hash::Hash; + + self.tag().hash(state); + self.width.hash(state); + self.height.hash(state); + self.align_items.hash(state); + self.spacing.hash(state); + self.padding.hash(state); + + for child in &self.children { + child.as_widget().hash_layout(state); + } + } +} + +impl Into> + for Column +where + Message: 'static, + Renderer: iced_native::Renderer + 'static, +{ + fn into(self) -> Element { + Element::new(self) + } +} diff --git a/pure/src/widget/element.rs b/pure/src/widget/element.rs new file mode 100644 index 00000000..5e40d488 --- /dev/null +++ b/pure/src/widget/element.rs @@ -0,0 +1,21 @@ +use crate::Widget; + +pub struct Element { + widget: Box>, +} + +impl Element { + pub fn new(widget: impl Widget + 'static) -> Self { + Self { + widget: Box::new(widget), + } + } + + pub fn as_widget(&self) -> &dyn Widget { + self.widget.as_ref() + } + + pub fn as_widget_mut(&mut self) -> &mut dyn Widget { + self.widget.as_mut() + } +} diff --git a/pure/src/widget/text.rs b/pure/src/widget/text.rs new file mode 100644 index 00000000..e3a7d299 --- /dev/null +++ b/pure/src/widget/text.rs @@ -0,0 +1,185 @@ +use crate::{Element, Tree, Widget}; + +use iced_native::alignment; +use iced_native::layout::{self, Layout}; +use iced_native::renderer; +use iced_native::text; +use iced_native::{Color, Hasher, Length, Point, Rectangle, Size}; + +use std::any::{self, Any}; + +pub struct Text +where + Renderer: text::Renderer, +{ + content: String, + size: Option, + color: Option, + font: Renderer::Font, + width: Length, + height: Length, + horizontal_alignment: alignment::Horizontal, + vertical_alignment: alignment::Vertical, +} + +impl Text { + /// Create a new fragment of [`Text`] with the given contents. + pub fn new>(label: T) -> Self { + Text { + content: label.into(), + size: None, + color: None, + font: Default::default(), + width: Length::Shrink, + height: Length::Shrink, + horizontal_alignment: alignment::Horizontal::Left, + vertical_alignment: alignment::Vertical::Top, + } + } + + /// Sets the size of the [`Text`]. + pub fn size(mut self, size: u16) -> Self { + self.size = Some(size); + self + } + + /// Sets the [`Color`] of the [`Text`]. + pub fn color>(mut self, color: C) -> Self { + self.color = Some(color.into()); + self + } + + /// Sets the [`Font`] of the [`Text`]. + /// + /// [`Font`]: Renderer::Font + pub fn font(mut self, font: impl Into) -> Self { + self.font = font.into(); + self + } + + /// Sets the width of the [`Text`] boundaries. + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } + + /// Sets the height of the [`Text`] boundaries. + pub fn height(mut self, height: Length) -> Self { + self.height = height; + self + } + + /// Sets the [`HorizontalAlignment`] of the [`Text`]. + pub fn horizontal_alignment( + mut self, + alignment: alignment::Horizontal, + ) -> Self { + self.horizontal_alignment = alignment; + self + } + + /// Sets the [`VerticalAlignment`] of the [`Text`]. + pub fn vertical_alignment( + mut self, + alignment: alignment::Vertical, + ) -> Self { + self.vertical_alignment = alignment; + self + } +} + +impl Widget for Text +where + Renderer: text::Renderer, +{ + fn tag(&self) -> any::TypeId { + any::TypeId::of::<()>() + } + + fn state(&self) -> Box { + Box::new(()) + } + + fn children(&self) -> &[Element] { + &[] + } + + fn width(&self) -> Length { + self.width + } + + fn height(&self) -> Length { + self.height + } + + fn layout( + &self, + renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + let limits = limits.width(self.width).height(self.height); + + let size = self.size.unwrap_or(renderer.default_size()); + + let bounds = limits.max(); + + let (width, height) = + renderer.measure(&self.content, size, self.font.clone(), bounds); + + let size = limits.resolve(Size::new(width, height)); + + layout::Node::new(size) + } + + fn draw( + &self, + _tree: &Tree, + renderer: &mut Renderer, + style: &renderer::Style, + layout: Layout<'_>, + _cursor_position: Point, + _viewport: &Rectangle, + ) { + iced_native::widget::text::draw( + renderer, + style, + layout, + &self.content, + self.font.clone(), + self.size, + self.color, + self.horizontal_alignment, + self.vertical_alignment, + ); + } + + fn hash_layout(&self, state: &mut Hasher) { + use std::hash::Hash; + + struct Marker; + std::any::TypeId::of::().hash(state); + + self.content.hash(state); + self.size.hash(state); + self.width.hash(state); + self.height.hash(state); + } +} + +impl Into> for Text +where + Renderer: text::Renderer + 'static, +{ + fn into(self) -> Element { + Element::new(self) + } +} + +impl Into> for &'static str +where + Renderer: text::Renderer + 'static, +{ + fn into(self) -> Element { + Text::new(self).into() + } +} diff --git a/pure/src/widget/tree.rs b/pure/src/widget/tree.rs new file mode 100644 index 00000000..75f50a2f --- /dev/null +++ b/pure/src/widget/tree.rs @@ -0,0 +1,58 @@ +use crate::widget::Element; + +use std::any::Any; +use std::marker::PhantomData; + +pub struct Tree { + pub state: Box, + pub children: Vec>, + types_: PhantomData<(Message, Renderer)>, +} + +impl Tree { + pub fn new(element: &Element) -> Self { + Self { + state: element.as_widget().state(), + children: element + .as_widget() + .children() + .iter() + .map(Self::new) + .collect(), + types_: PhantomData, + } + } + + pub fn diff( + &mut self, + current: &Element, + new: &Element, + ) { + if current.as_widget().tag() == new.as_widget().tag() { + let current_children = current.as_widget().children(); + let new_children = new.as_widget().children(); + + if current_children.len() > new_children.len() { + self.children.truncate(new_children.len()); + } + + for (child_state, (current, new)) in self + .children + .iter_mut() + .zip(current_children.iter().zip(new_children.iter())) + { + child_state.diff(current, new); + } + + if current_children.len() < new_children.len() { + self.children.extend( + new_children[current_children.len()..] + .iter() + .map(Self::new), + ); + } + } else { + *self = Self::new(new); + } + } +} diff --git a/virtual/Cargo.toml b/virtual/Cargo.toml deleted file mode 100644 index 74960b80..00000000 --- a/virtual/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "iced_virtual" -version = "0.1.0" -edition = "2021" - -[dependencies] -iced_native = { version = "0.4", path = "../native" } -iced_style = { version = "0.3", path = "../style" } diff --git a/virtual/src/flex.rs b/virtual/src/flex.rs deleted file mode 100644 index 8d473f08..00000000 --- a/virtual/src/flex.rs +++ /dev/null @@ -1,232 +0,0 @@ -//! Distribute elements using a flex-based layout. -// This code is heavily inspired by the [`druid`] codebase. -// -// [`druid`]: https://github.com/xi-editor/druid -// -// Copyright 2018 The xi-editor Authors, Héctor Ramón -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -use crate::Element; - -use iced_native::layout::{Limits, Node}; -use iced_native::{Alignment, Padding, Point, Size}; - -/// The main axis of a flex layout. -#[derive(Debug)] -pub enum Axis { - /// The horizontal axis - Horizontal, - - /// The vertical axis - Vertical, -} - -impl Axis { - fn main(&self, size: Size) -> f32 { - match self { - Axis::Horizontal => size.width, - Axis::Vertical => size.height, - } - } - - fn cross(&self, size: Size) -> f32 { - match self { - Axis::Horizontal => size.height, - Axis::Vertical => size.width, - } - } - - fn pack(&self, main: f32, cross: f32) -> (f32, f32) { - match self { - Axis::Horizontal => (main, cross), - Axis::Vertical => (cross, main), - } - } -} - -/// Computes the flex layout with the given axis and limits, applying spacing, -/// padding and alignment to the items as needed. -/// -/// It returns a new layout [`Node`]. -pub fn resolve( - axis: Axis, - renderer: &Renderer, - limits: &Limits, - padding: Padding, - spacing: f32, - align_items: Alignment, - items: &[Element], -) -> Node -where - Renderer: iced_native::Renderer, -{ - let limits = limits.pad(padding); - let total_spacing = spacing * items.len().saturating_sub(1) as f32; - let max_cross = axis.cross(limits.max()); - - let mut fill_sum = 0; - let mut cross = axis.cross(limits.min()).max(axis.cross(limits.fill())); - let mut available = axis.main(limits.max()) - total_spacing; - - let mut nodes: Vec = Vec::with_capacity(items.len()); - nodes.resize(items.len(), Node::default()); - - if align_items == Alignment::Fill { - let mut fill_cross = axis.cross(limits.min()); - - items.iter().for_each(|child| { - let cross_fill_factor = match axis { - Axis::Horizontal => child.as_widget().height(), - Axis::Vertical => child.as_widget().width(), - } - .fill_factor(); - - if cross_fill_factor == 0 { - let (max_width, max_height) = axis.pack(available, max_cross); - - let child_limits = - Limits::new(Size::ZERO, Size::new(max_width, max_height)); - - let layout = child.as_widget().layout(renderer, &child_limits); - let size = layout.size(); - - fill_cross = fill_cross.max(axis.cross(size)); - } - }); - - cross = fill_cross; - } - - for (i, child) in items.iter().enumerate() { - let fill_factor = match axis { - Axis::Horizontal => child.as_widget().width(), - Axis::Vertical => child.as_widget().height(), - } - .fill_factor(); - - if fill_factor == 0 { - let (min_width, min_height) = if align_items == Alignment::Fill { - axis.pack(0.0, cross) - } else { - axis.pack(0.0, 0.0) - }; - - let (max_width, max_height) = if align_items == Alignment::Fill { - axis.pack(available, cross) - } else { - axis.pack(available, max_cross) - }; - - let child_limits = Limits::new( - Size::new(min_width, min_height), - Size::new(max_width, max_height), - ); - - let layout = child.as_widget().layout(renderer, &child_limits); - let size = layout.size(); - - available -= axis.main(size); - - if align_items != Alignment::Fill { - cross = cross.max(axis.cross(size)); - } - - nodes[i] = layout; - } else { - fill_sum += fill_factor; - } - } - - let remaining = available.max(0.0); - - for (i, child) in items.iter().enumerate() { - let fill_factor = match axis { - Axis::Horizontal => child.as_widget().width(), - Axis::Vertical => child.as_widget().height(), - } - .fill_factor(); - - if fill_factor != 0 { - let max_main = remaining * fill_factor as f32 / fill_sum as f32; - let min_main = if max_main.is_infinite() { - 0.0 - } else { - max_main - }; - - let (min_width, min_height) = if align_items == Alignment::Fill { - axis.pack(min_main, cross) - } else { - axis.pack(min_main, axis.cross(limits.min())) - }; - - let (max_width, max_height) = if align_items == Alignment::Fill { - axis.pack(max_main, cross) - } else { - axis.pack(max_main, max_cross) - }; - - let child_limits = Limits::new( - Size::new(min_width, min_height), - Size::new(max_width, max_height), - ); - - let layout = child.as_widget().layout(renderer, &child_limits); - - if align_items != Alignment::Fill { - cross = cross.max(axis.cross(layout.size())); - } - - nodes[i] = layout; - } - } - - let pad = axis.pack(padding.left as f32, padding.top as f32); - let mut main = pad.0; - - for (i, node) in nodes.iter_mut().enumerate() { - if i > 0 { - main += spacing; - } - - let (x, y) = axis.pack(main, pad.1); - - node.move_to(Point::new(x, y)); - - match axis { - Axis::Horizontal => { - node.align( - Alignment::Start, - align_items, - Size::new(0.0, cross), - ); - } - Axis::Vertical => { - node.align( - align_items, - Alignment::Start, - Size::new(cross, 0.0), - ); - } - } - - let size = node.size(); - - main += axis.main(size); - } - - let (width, height) = axis.pack(main - pad.0, cross); - let size = limits.resolve(Size::new(width, height)); - - Node::with_children(size.pad(padding), nodes) -} diff --git a/virtual/src/lib.rs b/virtual/src/lib.rs deleted file mode 100644 index 8ba4c328..00000000 --- a/virtual/src/lib.rs +++ /dev/null @@ -1,146 +0,0 @@ -pub mod widget; - -pub(crate) mod flex; - -pub use widget::*; - -use iced_native::event::{self, Event}; -use iced_native::layout::{self, Layout}; -use iced_native::mouse; -use iced_native::renderer; -use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell}; - -pub struct Virtual<'a, Message, Renderer> { - state: &'a mut State, -} - -impl<'a, Message, Renderer> Virtual<'a, Message, Renderer> -where - Message: 'static, - Renderer: iced_native::Renderer + 'static, -{ - pub fn new( - state: &'a mut State, - content: impl Into>, - ) -> Self { - let _ = state.diff(content.into()); - - Self { state } - } -} - -pub struct State { - state_tree: widget::Tree, - last_element: Element, -} - -impl State -where - Message: 'static, - Renderer: iced_native::Renderer + 'static, -{ - pub fn new() -> Self { - let last_element = Element::new(widget::Column::new()); - - Self { - state_tree: widget::Tree::new(&last_element), - last_element, - } - } - - fn diff(&mut self, new_element: Element) { - self.state_tree.diff(&self.last_element, &new_element); - - self.last_element = new_element; - } -} - -impl<'a, Message, Renderer> iced_native::Widget - for Virtual<'a, Message, Renderer> -where - Renderer: iced_native::Renderer, -{ - fn width(&self) -> Length { - self.state.last_element.as_widget().width() - } - - fn height(&self) -> Length { - self.state.last_element.as_widget().height() - } - - fn hash_layout(&self, state: &mut Hasher) { - self.state.last_element.as_widget().hash_layout(state) - } - - fn layout( - &self, - renderer: &Renderer, - limits: &layout::Limits, - ) -> layout::Node { - self.state.last_element.as_widget().layout(renderer, limits) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - renderer: &Renderer, - clipboard: &mut dyn Clipboard, - shell: &mut Shell<'_, Message>, - ) -> event::Status { - self.state.last_element.as_widget_mut().on_event( - &mut self.state.state_tree, - event, - layout, - cursor_position, - renderer, - clipboard, - shell, - ) - } - - fn draw( - &self, - renderer: &mut Renderer, - style: &renderer::Style, - layout: Layout<'_>, - cursor_position: Point, - viewport: &Rectangle, - ) { - self.state.last_element.as_widget().draw( - &self.state.state_tree, - renderer, - style, - layout, - cursor_position, - viewport, - ) - } - - fn mouse_interaction( - &self, - layout: Layout<'_>, - cursor_position: Point, - viewport: &Rectangle, - renderer: &Renderer, - ) -> mouse::Interaction { - self.state.last_element.as_widget().mouse_interaction( - &self.state.state_tree, - layout, - cursor_position, - viewport, - renderer, - ) - } -} - -impl<'a, Message, Renderer> Into> - for Virtual<'a, Message, Renderer> -where - Renderer: iced_native::Renderer, -{ - fn into(self) -> iced_native::Element<'a, Message, Renderer> { - iced_native::Element::new(self) - } -} diff --git a/virtual/src/widget.rs b/virtual/src/widget.rs deleted file mode 100644 index bf63b999..00000000 --- a/virtual/src/widget.rs +++ /dev/null @@ -1,73 +0,0 @@ -mod button; -mod column; -mod element; -mod text; -mod tree; - -pub use button::Button; -pub use column::Column; -pub use element::Element; -pub use text::Text; -pub use tree::Tree; - -use iced_native::event::{self, Event}; -use iced_native::layout::{self, Layout}; -use iced_native::mouse; -use iced_native::renderer; -use iced_native::{Clipboard, Hasher, Length, Point, Rectangle, Shell}; - -use std::any::{self, Any}; - -pub trait Widget { - fn tag(&self) -> any::TypeId; - - fn state(&self) -> Box; - - fn children(&self) -> &[Element]; - - fn width(&self) -> Length; - - fn height(&self) -> Length; - - fn hash_layout(&self, state: &mut Hasher); - - fn layout( - &self, - renderer: &Renderer, - limits: &layout::Limits, - ) -> layout::Node; - - fn draw( - &self, - state: &Tree, - renderer: &mut Renderer, - style: &renderer::Style, - layout: Layout<'_>, - cursor_position: Point, - viewport: &Rectangle, - ); - - fn mouse_interaction( - &self, - _state: &Tree, - _layout: Layout<'_>, - _cursor_position: Point, - _viewport: &Rectangle, - _renderer: &Renderer, - ) -> mouse::Interaction { - mouse::Interaction::Idle - } - - fn on_event( - &mut self, - _state: &mut Tree, - _event: Event, - _layout: Layout<'_>, - _cursor_position: Point, - _renderer: &Renderer, - _clipboard: &mut dyn Clipboard, - _shell: &mut Shell<'_, Message>, - ) -> event::Status { - event::Status::Ignored - } -} diff --git a/virtual/src/widget/button.rs b/virtual/src/widget/button.rs deleted file mode 100644 index ece90811..00000000 --- a/virtual/src/widget/button.rs +++ /dev/null @@ -1,272 +0,0 @@ -use crate::widget::{Element, Tree, Widget}; - -use iced_native::event::{self, Event}; -use iced_native::layout; -use iced_native::mouse; -use iced_native::renderer; -use iced_native::touch; -use iced_native::{ - Background, Clipboard, Color, Hasher, Layout, Length, Padding, Point, - Rectangle, Shell, Vector, -}; -use iced_style::button::StyleSheet; - -use std::any::Any; - -pub struct Button { - content: Element, - on_press: Option, - style_sheet: Box, - width: Length, - height: Length, - padding: Padding, -} - -impl Button { - pub fn new(content: impl Into>) -> Self { - Button { - content: content.into(), - on_press: None, - style_sheet: Default::default(), - width: Length::Shrink, - height: Length::Shrink, - padding: Padding::new(5), - } - } - - pub fn on_press(mut self, on_press: Message) -> Self { - self.on_press = Some(on_press); - self - } -} - -impl Widget for Button -where - Message: 'static + Clone, - Renderer: 'static + iced_native::Renderer, -{ - fn tag(&self) -> std::any::TypeId { - std::any::TypeId::of::() - } - - fn state(&self) -> Box { - Box::new(State { is_pressed: false }) - } - - fn children(&self) -> &[Element] { - std::slice::from_ref(&self.content) - } - - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height - } - - fn hash_layout(&self, state: &mut Hasher) { - use std::hash::Hash; - - self.tag().hash(state); - self.width.hash(state); - self.content.as_widget().hash_layout(state); - } - - fn layout( - &self, - renderer: &Renderer, - limits: &layout::Limits, - ) -> layout::Node { - let limits = limits - .width(self.width) - .height(self.height) - .pad(self.padding); - - let mut content = self.content.as_widget().layout(renderer, &limits); - content.move_to(Point::new( - self.padding.left.into(), - self.padding.top.into(), - )); - - let size = limits.resolve(content.size()).pad(self.padding); - - layout::Node::with_children(size, vec![content]) - } - - fn on_event( - &mut self, - tree: &mut Tree, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - renderer: &Renderer, - clipboard: &mut dyn Clipboard, - shell: &mut Shell<'_, Message>, - ) -> event::Status { - let state = if let Some(state) = tree.state.downcast_mut::() { - state - } else { - return event::Status::Ignored; - }; - - if let event::Status::Captured = self.content.as_widget_mut().on_event( - &mut tree.children[0], - event.clone(), - layout.children().next().unwrap(), - cursor_position, - renderer, - clipboard, - shell, - ) { - return event::Status::Captured; - } - - match event { - Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) - | Event::Touch(touch::Event::FingerPressed { .. }) => { - if self.on_press.is_some() { - let bounds = layout.bounds(); - - if bounds.contains(cursor_position) { - state.is_pressed = true; - - return event::Status::Captured; - } - } - } - Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) - | Event::Touch(touch::Event::FingerLifted { .. }) => { - if let Some(on_press) = self.on_press.clone() { - let bounds = layout.bounds(); - - if state.is_pressed { - state.is_pressed = false; - - if bounds.contains(cursor_position) { - shell.publish(on_press); - } - - return event::Status::Captured; - } - } - } - Event::Touch(touch::Event::FingerLost { .. }) => { - state.is_pressed = false; - } - _ => {} - } - - event::Status::Ignored - } - - fn draw( - &self, - tree: &Tree, - renderer: &mut Renderer, - _style: &renderer::Style, - layout: Layout<'_>, - cursor_position: Point, - _viewport: &Rectangle, - ) { - let state = if let Some(state) = tree.state.downcast_ref::() { - state - } else { - return; - }; - - let bounds = layout.bounds(); - let content_layout = layout.children().next().unwrap(); - - let is_mouse_over = bounds.contains(cursor_position); - let is_disabled = self.on_press.is_none(); - - let styling = if is_disabled { - self.style_sheet.disabled() - } else if is_mouse_over { - if state.is_pressed { - self.style_sheet.pressed() - } else { - self.style_sheet.hovered() - } - } else { - self.style_sheet.active() - }; - - if styling.background.is_some() || styling.border_width > 0.0 { - if styling.shadow_offset != Vector::default() { - // TODO: Implement proper shadow support - renderer.fill_quad( - renderer::Quad { - bounds: Rectangle { - x: bounds.x + styling.shadow_offset.x, - y: bounds.y + styling.shadow_offset.y, - ..bounds - }, - border_radius: styling.border_radius, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }, - Background::Color([0.0, 0.0, 0.0, 0.5].into()), - ); - } - - renderer.fill_quad( - renderer::Quad { - bounds, - border_radius: styling.border_radius, - border_width: styling.border_width, - border_color: styling.border_color, - }, - styling - .background - .unwrap_or(Background::Color(Color::TRANSPARENT)), - ); - } - - self.content.as_widget().draw( - &tree.children[0], - renderer, - &renderer::Style { - text_color: styling.text_color, - }, - content_layout, - cursor_position, - &bounds, - ); - } - - fn mouse_interaction( - &self, - _tree: &Tree, - layout: Layout<'_>, - cursor_position: Point, - _viewport: &Rectangle, - _renderer: &Renderer, - ) -> mouse::Interaction { - let is_mouse_over = layout.bounds().contains(cursor_position); - let is_disabled = self.on_press.is_none(); - - if is_mouse_over && !is_disabled { - mouse::Interaction::Pointer - } else { - mouse::Interaction::default() - } - } -} - -#[derive(Debug, Clone)] -struct State { - is_pressed: bool, -} - -impl Into> - for Button -where - Message: Clone + 'static, - Renderer: iced_native::Renderer + 'static, -{ - fn into(self) -> Element { - Element::new(self) - } -} diff --git a/virtual/src/widget/column.rs b/virtual/src/widget/column.rs deleted file mode 100644 index 2f70282a..00000000 --- a/virtual/src/widget/column.rs +++ /dev/null @@ -1,220 +0,0 @@ -use crate::flex; -use crate::widget::{Element, Tree, Widget}; - -use iced_native::event::{self, Event}; -use iced_native::layout::{self, Layout}; -use iced_native::mouse; -use iced_native::renderer; -use iced_native::{ - Alignment, Clipboard, Hasher, Length, Padding, Point, Rectangle, Shell, -}; - -use std::any::{self, Any}; - -pub struct Column { - spacing: u16, - padding: Padding, - width: Length, - height: Length, - align_items: Alignment, - children: Vec>, -} - -impl<'a, Message, Renderer> Column { - pub fn new() -> Self { - Self::with_children(Vec::new()) - } - - pub fn with_children(children: Vec>) -> Self { - Column { - spacing: 0, - padding: Padding::ZERO, - width: Length::Shrink, - height: Length::Shrink, - align_items: Alignment::Start, - children, - } - } - - pub fn spacing(mut self, units: u16) -> Self { - self.spacing = units; - self - } - - pub fn padding>(mut self, padding: P) -> Self { - self.padding = padding.into(); - self - } - - pub fn width(mut self, width: Length) -> Self { - self.width = width; - self - } - - pub fn height(mut self, height: Length) -> Self { - self.height = height; - self - } - - pub fn align_items(mut self, align: Alignment) -> Self { - self.align_items = align; - self - } - - pub fn push( - mut self, - child: impl Into>, - ) -> Self { - self.children.push(child.into()); - self - } -} - -impl Widget for Column -where - Renderer: iced_native::Renderer, -{ - fn tag(&self) -> any::TypeId { - struct Marker; - any::TypeId::of::() - } - - fn state(&self) -> Box { - Box::new(()) - } - - fn children(&self) -> &[Element] { - &self.children - } - - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height - } - - fn layout( - &self, - renderer: &Renderer, - limits: &layout::Limits, - ) -> layout::Node { - let limits = limits.width(self.width).height(self.height); - - flex::resolve( - flex::Axis::Vertical, - renderer, - &limits, - self.padding, - self.spacing as f32, - self.align_items, - &self.children, - ) - } - - fn on_event( - &mut self, - tree: &mut Tree, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - renderer: &Renderer, - clipboard: &mut dyn Clipboard, - shell: &mut Shell<'_, Message>, - ) -> event::Status { - self.children - .iter_mut() - .zip(&mut tree.children) - .zip(layout.children()) - .map(|((child, state), layout)| { - child.as_widget_mut().on_event( - state, - event.clone(), - layout, - cursor_position, - renderer, - clipboard, - shell, - ) - }) - .fold(event::Status::Ignored, event::Status::merge) - } - - fn mouse_interaction( - &self, - tree: &Tree, - layout: Layout<'_>, - cursor_position: Point, - viewport: &Rectangle, - renderer: &Renderer, - ) -> mouse::Interaction { - self.children - .iter() - .zip(&tree.children) - .zip(layout.children()) - .map(|((child, state), layout)| { - child.as_widget().mouse_interaction( - state, - layout, - cursor_position, - viewport, - renderer, - ) - }) - .max() - .unwrap_or_default() - } - - fn draw( - &self, - tree: &Tree, - renderer: &mut Renderer, - style: &renderer::Style, - layout: Layout<'_>, - cursor_position: Point, - viewport: &Rectangle, - ) { - for ((child, state), layout) in self - .children - .iter() - .zip(&tree.children) - .zip(layout.children()) - { - child.as_widget().draw( - state, - renderer, - style, - layout, - cursor_position, - viewport, - ); - } - } - - fn hash_layout(&self, state: &mut Hasher) { - use std::hash::Hash; - - self.tag().hash(state); - self.width.hash(state); - self.height.hash(state); - self.align_items.hash(state); - self.spacing.hash(state); - self.padding.hash(state); - - for child in &self.children { - child.as_widget().hash_layout(state); - } - } -} - -impl Into> - for Column -where - Message: 'static, - Renderer: iced_native::Renderer + 'static, -{ - fn into(self) -> Element { - Element::new(self) - } -} diff --git a/virtual/src/widget/element.rs b/virtual/src/widget/element.rs deleted file mode 100644 index 5e40d488..00000000 --- a/virtual/src/widget/element.rs +++ /dev/null @@ -1,21 +0,0 @@ -use crate::Widget; - -pub struct Element { - widget: Box>, -} - -impl Element { - pub fn new(widget: impl Widget + 'static) -> Self { - Self { - widget: Box::new(widget), - } - } - - pub fn as_widget(&self) -> &dyn Widget { - self.widget.as_ref() - } - - pub fn as_widget_mut(&mut self) -> &mut dyn Widget { - self.widget.as_mut() - } -} diff --git a/virtual/src/widget/text.rs b/virtual/src/widget/text.rs deleted file mode 100644 index e3a7d299..00000000 --- a/virtual/src/widget/text.rs +++ /dev/null @@ -1,185 +0,0 @@ -use crate::{Element, Tree, Widget}; - -use iced_native::alignment; -use iced_native::layout::{self, Layout}; -use iced_native::renderer; -use iced_native::text; -use iced_native::{Color, Hasher, Length, Point, Rectangle, Size}; - -use std::any::{self, Any}; - -pub struct Text -where - Renderer: text::Renderer, -{ - content: String, - size: Option, - color: Option, - font: Renderer::Font, - width: Length, - height: Length, - horizontal_alignment: alignment::Horizontal, - vertical_alignment: alignment::Vertical, -} - -impl Text { - /// Create a new fragment of [`Text`] with the given contents. - pub fn new>(label: T) -> Self { - Text { - content: label.into(), - size: None, - color: None, - font: Default::default(), - width: Length::Shrink, - height: Length::Shrink, - horizontal_alignment: alignment::Horizontal::Left, - vertical_alignment: alignment::Vertical::Top, - } - } - - /// Sets the size of the [`Text`]. - pub fn size(mut self, size: u16) -> Self { - self.size = Some(size); - self - } - - /// Sets the [`Color`] of the [`Text`]. - pub fn color>(mut self, color: C) -> Self { - self.color = Some(color.into()); - self - } - - /// Sets the [`Font`] of the [`Text`]. - /// - /// [`Font`]: Renderer::Font - pub fn font(mut self, font: impl Into) -> Self { - self.font = font.into(); - self - } - - /// Sets the width of the [`Text`] boundaries. - pub fn width(mut self, width: Length) -> Self { - self.width = width; - self - } - - /// Sets the height of the [`Text`] boundaries. - pub fn height(mut self, height: Length) -> Self { - self.height = height; - self - } - - /// Sets the [`HorizontalAlignment`] of the [`Text`]. - pub fn horizontal_alignment( - mut self, - alignment: alignment::Horizontal, - ) -> Self { - self.horizontal_alignment = alignment; - self - } - - /// Sets the [`VerticalAlignment`] of the [`Text`]. - pub fn vertical_alignment( - mut self, - alignment: alignment::Vertical, - ) -> Self { - self.vertical_alignment = alignment; - self - } -} - -impl Widget for Text -where - Renderer: text::Renderer, -{ - fn tag(&self) -> any::TypeId { - any::TypeId::of::<()>() - } - - fn state(&self) -> Box { - Box::new(()) - } - - fn children(&self) -> &[Element] { - &[] - } - - fn width(&self) -> Length { - self.width - } - - fn height(&self) -> Length { - self.height - } - - fn layout( - &self, - renderer: &Renderer, - limits: &layout::Limits, - ) -> layout::Node { - let limits = limits.width(self.width).height(self.height); - - let size = self.size.unwrap_or(renderer.default_size()); - - let bounds = limits.max(); - - let (width, height) = - renderer.measure(&self.content, size, self.font.clone(), bounds); - - let size = limits.resolve(Size::new(width, height)); - - layout::Node::new(size) - } - - fn draw( - &self, - _tree: &Tree, - renderer: &mut Renderer, - style: &renderer::Style, - layout: Layout<'_>, - _cursor_position: Point, - _viewport: &Rectangle, - ) { - iced_native::widget::text::draw( - renderer, - style, - layout, - &self.content, - self.font.clone(), - self.size, - self.color, - self.horizontal_alignment, - self.vertical_alignment, - ); - } - - fn hash_layout(&self, state: &mut Hasher) { - use std::hash::Hash; - - struct Marker; - std::any::TypeId::of::().hash(state); - - self.content.hash(state); - self.size.hash(state); - self.width.hash(state); - self.height.hash(state); - } -} - -impl Into> for Text -where - Renderer: text::Renderer + 'static, -{ - fn into(self) -> Element { - Element::new(self) - } -} - -impl Into> for &'static str -where - Renderer: text::Renderer + 'static, -{ - fn into(self) -> Element { - Text::new(self).into() - } -} diff --git a/virtual/src/widget/tree.rs b/virtual/src/widget/tree.rs deleted file mode 100644 index 75f50a2f..00000000 --- a/virtual/src/widget/tree.rs +++ /dev/null @@ -1,58 +0,0 @@ -use crate::widget::Element; - -use std::any::Any; -use std::marker::PhantomData; - -pub struct Tree { - pub state: Box, - pub children: Vec>, - types_: PhantomData<(Message, Renderer)>, -} - -impl Tree { - pub fn new(element: &Element) -> Self { - Self { - state: element.as_widget().state(), - children: element - .as_widget() - .children() - .iter() - .map(Self::new) - .collect(), - types_: PhantomData, - } - } - - pub fn diff( - &mut self, - current: &Element, - new: &Element, - ) { - if current.as_widget().tag() == new.as_widget().tag() { - let current_children = current.as_widget().children(); - let new_children = new.as_widget().children(); - - if current_children.len() > new_children.len() { - self.children.truncate(new_children.len()); - } - - for (child_state, (current, new)) in self - .children - .iter_mut() - .zip(current_children.iter().zip(new_children.iter())) - { - child_state.diff(current, new); - } - - if current_children.len() < new_children.len() { - self.children.extend( - new_children[current_children.len()..] - .iter() - .map(Self::new), - ); - } - } else { - *self = Self::new(new); - } - } -} -- cgit