From 8b8f7563ad33dafeadf6238e377748cdec17d67a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 10 Sep 2019 19:41:49 +0200 Subject: Switch to workspace layout --- Cargo.toml | 35 +--- core/Cargo.toml | 25 +++ core/src/element.rs | 370 +++++++++++++++++++++++++++++++++++ core/src/event.rs | 16 ++ core/src/hasher.rs | 19 ++ core/src/input.rs | 7 + core/src/input/button_state.rs | 24 +++ core/src/input/keyboard.rs | 6 + core/src/input/keyboard/event.rs | 23 +++ core/src/input/keyboard/key_code.rs | 374 ++++++++++++++++++++++++++++++++++++ core/src/input/mouse.rs | 6 + core/src/input/mouse/button.rs | 32 +++ core/src/input/mouse/event.rs | 44 +++++ core/src/layout.rs | 62 ++++++ core/src/lib.rs | 229 ++++++++++++++++++++++ core/src/mouse_cursor.rs | 35 ++++ core/src/node.rs | 60 ++++++ core/src/point.rs | 31 +++ core/src/rectangle.rs | 30 +++ core/src/renderer.rs | 45 +++++ core/src/style.rs | 262 +++++++++++++++++++++++++ core/src/user_interface.rs | 323 +++++++++++++++++++++++++++++++ core/src/vector.rs | 15 ++ core/src/widget.rs | 114 +++++++++++ core/src/widget/button.rs | 282 +++++++++++++++++++++++++++ core/src/widget/checkbox.rs | 203 +++++++++++++++++++ core/src/widget/column.rs | 224 +++++++++++++++++++++ core/src/widget/image.rs | 178 +++++++++++++++++ core/src/widget/panel.rs | 94 +++++++++ core/src/widget/progress_bar.rs | 106 ++++++++++ core/src/widget/radio.rs | 211 ++++++++++++++++++++ core/src/widget/row.rs | 219 +++++++++++++++++++++ core/src/widget/slider.rs | 241 +++++++++++++++++++++++ core/src/widget/text.rs | 224 +++++++++++++++++++++ examples/Cargo.toml | 17 ++ examples/tour/main.rs | 2 +- src/element.rs | 370 ----------------------------------- src/event.rs | 16 -- src/hasher.rs | 19 -- src/input.rs | 7 - src/input/button_state.rs | 24 --- src/input/keyboard.rs | 6 - src/input/keyboard/event.rs | 23 --- src/input/keyboard/key_code.rs | 374 ------------------------------------ src/input/mouse.rs | 6 - src/input/mouse/button.rs | 32 --- src/input/mouse/event.rs | 44 ----- src/layout.rs | 62 ------ src/lib.rs | 229 ---------------------- src/mouse_cursor.rs | 35 ---- src/node.rs | 60 ------ src/point.rs | 31 --- src/rectangle.rs | 30 --- src/renderer.rs | 45 ----- src/style.rs | 262 ------------------------- src/user_interface.rs | 323 ------------------------------- src/vector.rs | 15 -- src/widget.rs | 114 ----------- src/widget/button.rs | 282 --------------------------- src/widget/checkbox.rs | 203 ------------------- src/widget/column.rs | 224 --------------------- src/widget/image.rs | 178 ----------------- src/widget/panel.rs | 94 --------- src/widget/progress_bar.rs | 106 ---------- src/widget/radio.rs | 211 -------------------- src/widget/row.rs | 219 --------------------- src/widget/slider.rs | 241 ----------------------- src/widget/text.rs | 224 --------------------- 68 files changed, 4157 insertions(+), 4140 deletions(-) create mode 100644 core/Cargo.toml create mode 100644 core/src/element.rs create mode 100644 core/src/event.rs create mode 100644 core/src/hasher.rs create mode 100644 core/src/input.rs create mode 100644 core/src/input/button_state.rs create mode 100644 core/src/input/keyboard.rs create mode 100644 core/src/input/keyboard/event.rs create mode 100644 core/src/input/keyboard/key_code.rs create mode 100644 core/src/input/mouse.rs create mode 100644 core/src/input/mouse/button.rs create mode 100644 core/src/input/mouse/event.rs create mode 100644 core/src/layout.rs create mode 100644 core/src/lib.rs create mode 100644 core/src/mouse_cursor.rs create mode 100644 core/src/node.rs create mode 100644 core/src/point.rs create mode 100644 core/src/rectangle.rs create mode 100644 core/src/renderer.rs create mode 100644 core/src/style.rs create mode 100644 core/src/user_interface.rs create mode 100644 core/src/vector.rs create mode 100644 core/src/widget.rs create mode 100644 core/src/widget/button.rs create mode 100644 core/src/widget/checkbox.rs create mode 100644 core/src/widget/column.rs create mode 100644 core/src/widget/image.rs create mode 100644 core/src/widget/panel.rs create mode 100644 core/src/widget/progress_bar.rs create mode 100644 core/src/widget/radio.rs create mode 100644 core/src/widget/row.rs create mode 100644 core/src/widget/slider.rs create mode 100644 core/src/widget/text.rs create mode 100644 examples/Cargo.toml delete mode 100644 src/element.rs delete mode 100644 src/event.rs delete mode 100644 src/hasher.rs delete mode 100644 src/input.rs delete mode 100644 src/input/button_state.rs delete mode 100644 src/input/keyboard.rs delete mode 100644 src/input/keyboard/event.rs delete mode 100644 src/input/keyboard/key_code.rs delete mode 100644 src/input/mouse.rs delete mode 100644 src/input/mouse/button.rs delete mode 100644 src/input/mouse/event.rs delete mode 100644 src/layout.rs delete mode 100644 src/lib.rs delete mode 100644 src/mouse_cursor.rs delete mode 100644 src/node.rs delete mode 100644 src/point.rs delete mode 100644 src/rectangle.rs delete mode 100644 src/renderer.rs delete mode 100644 src/style.rs delete mode 100644 src/user_interface.rs delete mode 100644 src/vector.rs delete mode 100644 src/widget.rs delete mode 100644 src/widget/button.rs delete mode 100644 src/widget/checkbox.rs delete mode 100644 src/widget/column.rs delete mode 100644 src/widget/image.rs delete mode 100644 src/widget/panel.rs delete mode 100644 src/widget/progress_bar.rs delete mode 100644 src/widget/radio.rs delete mode 100644 src/widget/row.rs delete mode 100644 src/widget/slider.rs delete mode 100644 src/widget/text.rs diff --git a/Cargo.toml b/Cargo.toml index 1e806b22..43556cbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,30 +1,5 @@ -[package] -name = "iced" -version = "0.1.0-alpha" -authors = ["Héctor Ramón Jiménez "] -edition = "2018" -description = "A GUI runtime, heavily inspired by Elm." -license = "MIT" -repository = "https://github.com/hecrj/iced" -documentation = "https://docs.rs/iced" -readme = "README.md" -keywords = ["gui", "ui", "graphics", "interface", "widgets"] -categories = ["gui"] - -[badges] -maintenance = { status = "actively-developed" } - -[package.metadata.docs.rs] -features = ["winit"] - -[dependencies] -stretch = "0.2" -twox-hash = "1.5" - -# Enable to obtain conversion traits -winit = { version = "0.20.0-alpha3", optional = true } - -[dev-dependencies] -# A personal `ggez` fork that introduces a `FontCache` type to measure text -# efficiently and fixes HiDPI issues. -ggez = { version = "0.5", git = "https://github.com/hecrj/ggez.git" } +[workspace] +members = [ + "core", + "examples", +] diff --git a/core/Cargo.toml b/core/Cargo.toml new file mode 100644 index 00000000..cd84d03e --- /dev/null +++ b/core/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "iced" +version = "0.1.0-alpha" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +description = "A GUI runtime, heavily inspired by Elm." +license = "MIT" +repository = "https://github.com/hecrj/iced" +documentation = "https://docs.rs/iced" +readme = "README.md" +keywords = ["gui", "ui", "graphics", "interface", "widgets"] +categories = ["gui"] + +[badges] +maintenance = { status = "actively-developed" } + +[package.metadata.docs.rs] +features = ["winit"] + +[dependencies] +stretch = "0.2" +twox-hash = "1.5" + +# Enable to obtain conversion traits +winit = { version = "0.20.0-alpha3", optional = true } diff --git a/core/src/element.rs b/core/src/element.rs new file mode 100644 index 00000000..70d06f42 --- /dev/null +++ b/core/src/element.rs @@ -0,0 +1,370 @@ +use stretch::{geometry, result}; + +use crate::{ + renderer, Event, Hasher, Layout, MouseCursor, Node, Point, Widget, +}; + +/// A generic [`Widget`]. +/// +/// It is useful to build composable user interfaces that do not leak +/// implementation details in their __view logic__. +/// +/// If you have a [built-in widget], you should be able to use `Into` +/// to turn it into an [`Element`]. +/// +/// [built-in widget]: widget/index.html#built-in-widgets +/// [`Widget`]: widget/trait.Widget.html +/// [`Element`]: struct.Element.html +pub struct Element<'a, Message, Renderer> { + pub(crate) widget: Box + 'a>, +} + +impl<'a, Message, Renderer> std::fmt::Debug for Element<'a, Message, Renderer> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Element") + .field("widget", &self.widget) + .finish() + } +} + +impl<'a, Message, Renderer> Element<'a, Message, Renderer> { + /// Create a new [`Element`] containing the given [`Widget`]. + /// + /// [`Element`]: struct.Element.html + /// [`Widget`]: widget/trait.Widget.html + pub fn new( + widget: impl Widget + 'a, + ) -> Element<'a, Message, Renderer> { + Element { + widget: Box::new(widget), + } + } + + /// Applies a transformation to the produced message of the [`Element`]. + /// + /// This method is useful when you want to decouple different parts of your + /// UI and make them __composable__. + /// + /// [`Element`]: struct.Element.html + /// + /// # Example + /// Imagine we want to use [our counter](index.html#usage). But instead of + /// showing a single counter, we want to display many of them. We can reuse + /// the `Counter` type as it is! + /// + /// We use composition to model the __state__ of our new application: + /// + /// ``` + /// # mod counter { + /// # pub struct Counter; + /// # } + /// use counter::Counter; + /// + /// struct ManyCounters { + /// counters: Vec, + /// } + /// ``` + /// + /// We can store the state of multiple counters now. However, the + /// __messages__ we implemented before describe the user interactions + /// of a __single__ counter. Right now, we need to also identify which + /// counter is receiving user interactions. Can we use composition again? + /// Yes. + /// + /// ``` + /// # mod counter { + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message {} + /// # } + /// #[derive(Debug, Clone, Copy)] + /// pub enum Message { + /// Counter(usize, counter::Message) + /// } + /// ``` + /// + /// We compose the previous __messages__ with the index of the counter + /// producing them. Let's implement our __view logic__ now: + /// + /// ``` + /// # mod counter { + /// # use iced::{button, Button}; + /// # + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message {} + /// # pub struct Counter(button::State); + /// # + /// # impl Counter { + /// # pub fn view(&mut self) -> Button { + /// # Button::new(&mut self.0, "_") + /// # } + /// # } + /// # } + /// # + /// # mod iced_wgpu { + /// # use iced::{ + /// # button, MouseCursor, Node, Point, Rectangle, Style, + /// # }; + /// # pub struct Renderer; + /// # + /// # impl button::Renderer for Renderer { + /// # fn draw( + /// # &mut self, + /// # _cursor_position: Point, + /// # _bounds: Rectangle, + /// # _state: &button::State, + /// # _label: &str, + /// # _class: button::Class, + /// # ) -> MouseCursor { + /// # MouseCursor::OutOfBounds + /// # } + /// # } + /// # } + /// # + /// # use counter::Counter; + /// # + /// # struct ManyCounters { + /// # counters: Vec, + /// # } + /// # + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message { + /// # Counter(usize, counter::Message) + /// # } + /// use iced::{Element, Row}; + /// use iced_wgpu::Renderer; + /// + /// impl ManyCounters { + /// pub fn view(&mut self) -> Row { + /// // We can quickly populate a `Row` by folding over our counters + /// self.counters.iter_mut().enumerate().fold( + /// Row::new().spacing(20), + /// |row, (index, counter)| { + /// // We display the counter + /// let element: Element = + /// counter.view().into(); + /// + /// row.push( + /// // Here we turn our `Element` into + /// // an `Element` by combining the `index` and the + /// // message of the `element`. + /// element.map(move |message| Message::Counter(index, message)) + /// ) + /// } + /// ) + /// } + /// } + /// ``` + /// + /// Finally, our __update logic__ is pretty straightforward: simple + /// delegation. + /// + /// ``` + /// # mod counter { + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message {} + /// # pub struct Counter; + /// # + /// # impl Counter { + /// # pub fn update(&mut self, _message: Message) {} + /// # } + /// # } + /// # + /// # use counter::Counter; + /// # + /// # struct ManyCounters { + /// # counters: Vec, + /// # } + /// # + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message { + /// # Counter(usize, counter::Message) + /// # } + /// impl ManyCounters { + /// pub fn update(&mut self, message: Message) { + /// match message { + /// Message::Counter(index, counter_msg) => { + /// if let Some(counter) = self.counters.get_mut(index) { + /// counter.update(counter_msg); + /// } + /// } + /// } + /// } + /// } + /// ``` + pub fn map(self, f: F) -> Element<'a, B, Renderer> + where + Message: 'static + Copy, + Renderer: 'a, + B: 'static, + F: 'static + Fn(Message) -> B, + { + Element { + widget: Box::new(Map::new(self.widget, f)), + } + } + + /// Marks the [`Element`] as _to-be-explained_. + /// + /// The [`Renderer`] will explain the layout of the [`Element`] graphically. + /// This can be very useful for debugging your layout! + /// + /// [`Element`]: struct.Element.html + /// [`Renderer`]: trait.Renderer.html + pub fn explain( + self, + color: Renderer::Color, + ) -> Element<'a, Message, Renderer> + where + Message: 'static, + Renderer: 'a + renderer::Debugger, + { + Element { + widget: Box::new(Explain::new(self, color)), + } + } + + pub(crate) fn compute_layout(&self, renderer: &Renderer) -> result::Layout { + let node = self.widget.node(renderer); + + node.0.compute_layout(geometry::Size::undefined()).unwrap() + } + + pub(crate) fn hash_layout(&self, state: &mut Hasher) { + self.widget.hash_layout(state); + } +} + +struct Map<'a, A, B, Renderer> { + widget: Box + 'a>, + mapper: Box B>, +} + +impl<'a, A, B, Renderer> std::fmt::Debug for Map<'a, A, B, Renderer> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Map").field("widget", &self.widget).finish() + } +} + +impl<'a, A, B, Renderer> Map<'a, A, B, Renderer> { + pub fn new( + widget: Box + 'a>, + mapper: F, + ) -> Map<'a, A, B, Renderer> + where + F: 'static + Fn(A) -> B, + { + Map { + widget, + mapper: Box::new(mapper), + } + } +} + +impl<'a, A, B, Renderer> Widget for Map<'a, A, B, Renderer> +where + A: Copy, +{ + fn node(&self, renderer: &Renderer) -> Node { + self.widget.node(renderer) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + let mut original_messages = Vec::new(); + + self.widget.on_event( + event, + layout, + cursor_position, + &mut original_messages, + ); + + original_messages + .iter() + .cloned() + .for_each(|message| messages.push((self.mapper)(message))); + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + self.widget.draw(renderer, layout, cursor_position) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.widget.hash_layout(state); + } +} + +struct Explain<'a, Message, Renderer: renderer::Debugger> { + element: Element<'a, Message, Renderer>, + color: Renderer::Color, +} + +impl<'a, Message, Renderer> std::fmt::Debug for Explain<'a, Message, Renderer> +where + Renderer: renderer::Debugger, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Explain") + .field("element", &self.element) + .finish() + } +} + +impl<'a, Message, Renderer> Explain<'a, Message, Renderer> +where + Renderer: renderer::Debugger, +{ + fn new( + element: Element<'a, Message, Renderer>, + color: Renderer::Color, + ) -> Self { + Explain { element, color } + } +} + +impl<'a, Message, Renderer> Widget + for Explain<'a, Message, Renderer> +where + Renderer: renderer::Debugger, +{ + fn node(&self, renderer: &Renderer) -> Node { + self.element.widget.node(renderer) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + self.element + .widget + .on_event(event, layout, cursor_position, messages) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + renderer.explain(&layout, self.color); + + self.element.widget.draw(renderer, layout, cursor_position) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.element.widget.hash_layout(state); + } +} diff --git a/core/src/event.rs b/core/src/event.rs new file mode 100644 index 00000000..71f06006 --- /dev/null +++ b/core/src/event.rs @@ -0,0 +1,16 @@ +use crate::input::{keyboard, mouse}; + +/// A user interface event. +/// +/// _**Note:** This type is largely incomplete! If you need to track +/// additional events, feel free to [open an issue] and share your use case!_ +/// +/// [open an issue]: https://github.com/hecrj/iced/issues +#[derive(PartialEq, Clone, Copy, Debug)] +pub enum Event { + /// A keyboard event + Keyboard(keyboard::Event), + + /// A mouse event + Mouse(mouse::Event), +} diff --git a/core/src/hasher.rs b/core/src/hasher.rs new file mode 100644 index 00000000..9f6aacce --- /dev/null +++ b/core/src/hasher.rs @@ -0,0 +1,19 @@ +/// The hasher used to compare layouts. +#[derive(Debug)] +pub struct Hasher(twox_hash::XxHash64); + +impl Default for Hasher { + fn default() -> Self { + Hasher(twox_hash::XxHash64::default()) + } +} + +impl core::hash::Hasher for Hasher { + fn write(&mut self, bytes: &[u8]) { + self.0.write(bytes) + } + + fn finish(&self) -> u64 { + self.0.finish() + } +} diff --git a/core/src/input.rs b/core/src/input.rs new file mode 100644 index 00000000..097fa730 --- /dev/null +++ b/core/src/input.rs @@ -0,0 +1,7 @@ +//! Map your system events into input events that the runtime can understand. +pub mod keyboard; +pub mod mouse; + +mod button_state; + +pub use button_state::ButtonState; diff --git a/core/src/input/button_state.rs b/core/src/input/button_state.rs new file mode 100644 index 00000000..e9dc05d7 --- /dev/null +++ b/core/src/input/button_state.rs @@ -0,0 +1,24 @@ +/// The state of a button. +/// +/// If you are using [`winit`], consider enabling the `winit` feature to get +/// conversion implementations for free! +/// +/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ +#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] +pub enum ButtonState { + /// The button is pressed. + Pressed, + + /// The button is __not__ pressed. + Released, +} + +#[cfg(feature = "winit")] +impl From for ButtonState { + fn from(element_state: winit::event::ElementState) -> Self { + match element_state { + winit::event::ElementState::Pressed => ButtonState::Pressed, + winit::event::ElementState::Released => ButtonState::Released, + } + } +} diff --git a/core/src/input/keyboard.rs b/core/src/input/keyboard.rs new file mode 100644 index 00000000..57c24484 --- /dev/null +++ b/core/src/input/keyboard.rs @@ -0,0 +1,6 @@ +//! Build keyboard events. +mod event; +mod key_code; + +pub use event::Event; +pub use key_code::KeyCode; diff --git a/core/src/input/keyboard/event.rs b/core/src/input/keyboard/event.rs new file mode 100644 index 00000000..8118f112 --- /dev/null +++ b/core/src/input/keyboard/event.rs @@ -0,0 +1,23 @@ +use super::KeyCode; +use crate::input::ButtonState; + +#[derive(Debug, Clone, Copy, PartialEq)] +/// A keyboard event. +/// +/// _**Note:** This type is largely incomplete! If you need to track +/// additional events, feel free to [open an issue] and share your use case!_ +/// +/// [open an issue]: https://github.com/hecrj/iced/issues +pub enum Event { + /// A keyboard key was pressed or released. + Input { + /// The state of the key + state: ButtonState, + + /// The key identifier + key_code: KeyCode, + }, + + /// A unicode character was received. + CharacterReceived(char), +} diff --git a/core/src/input/keyboard/key_code.rs b/core/src/input/keyboard/key_code.rs new file mode 100644 index 00000000..207ddeac --- /dev/null +++ b/core/src/input/keyboard/key_code.rs @@ -0,0 +1,374 @@ +/// The symbolic name of a keyboard key. +/// +/// This is mostly the `KeyCode` type found in [`winit`]. +/// +/// If you are using [`winit`], consider enabling the `winit` feature to get +/// conversion implementations for free! +/// +/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ +#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] +#[repr(u32)] +#[allow(missing_docs)] +pub enum KeyCode { + /// The '1' key over the letters. + Key1, + /// The '2' key over the letters. + Key2, + /// The '3' key over the letters. + Key3, + /// The '4' key over the letters. + Key4, + /// The '5' key over the letters. + Key5, + /// The '6' key over the letters. + Key6, + /// The '7' key over the letters. + Key7, + /// The '8' key over the letters. + Key8, + /// The '9' key over the letters. + Key9, + /// The '0' key over the 'O' and 'P' keys. + Key0, + + A, + B, + C, + D, + E, + F, + G, + H, + I, + J, + K, + L, + M, + N, + O, + P, + Q, + R, + S, + T, + U, + V, + W, + X, + Y, + Z, + + /// The Escape key, next to F1 + Escape, + + F1, + F2, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + F10, + F11, + F12, + F13, + F14, + F15, + F16, + F17, + F18, + F19, + F20, + F21, + F22, + F23, + F24, + + /// Print Screen/SysRq + Snapshot, + /// Scroll Lock + Scroll, + /// Pause/Break key, next to Scroll lock + Pause, + + /// `Insert`, next to Backspace + Insert, + Home, + Delete, + End, + PageDown, + PageUp, + + Left, + Up, + Right, + Down, + + Backspace, + Enter, + Space, + + /// The "Compose" key on Linux + Compose, + + Caret, + + Numlock, + Numpad0, + Numpad1, + Numpad2, + Numpad3, + Numpad4, + Numpad5, + Numpad6, + Numpad7, + Numpad8, + Numpad9, + + AbntC1, + AbntC2, + Add, + Apostrophe, + Apps, + At, + Ax, + Backslash, + Calculator, + Capital, + Colon, + Comma, + Convert, + Decimal, + Divide, + Equals, + Grave, + Kana, + Kanji, + LAlt, + LBracket, + LControl, + LShift, + LWin, + Mail, + MediaSelect, + MediaStop, + Minus, + Multiply, + Mute, + MyComputer, + NavigateForward, // also called "Prior" + NavigateBackward, // also called "Next" + NextTrack, + NoConvert, + NumpadComma, + NumpadEnter, + NumpadEquals, + OEM102, + Period, + PlayPause, + Power, + PrevTrack, + RAlt, + RBracket, + RControl, + RShift, + RWin, + Semicolon, + Slash, + Sleep, + Stop, + Subtract, + Sysrq, + Tab, + Underline, + Unlabeled, + VolumeDown, + VolumeUp, + Wake, + WebBack, + WebFavorites, + WebForward, + WebHome, + WebRefresh, + WebSearch, + WebStop, + Yen, + Copy, + Paste, + Cut, +} + +#[cfg(feature = "winit")] +impl From for KeyCode { + fn from(virtual_keycode: winit::event::VirtualKeyCode) -> Self { + match virtual_keycode { + winit::event::VirtualKeyCode::Key1 => KeyCode::Key1, + winit::event::VirtualKeyCode::Key2 => KeyCode::Key2, + winit::event::VirtualKeyCode::Key3 => KeyCode::Key3, + winit::event::VirtualKeyCode::Key4 => KeyCode::Key4, + winit::event::VirtualKeyCode::Key5 => KeyCode::Key5, + winit::event::VirtualKeyCode::Key6 => KeyCode::Key6, + winit::event::VirtualKeyCode::Key7 => KeyCode::Key7, + winit::event::VirtualKeyCode::Key8 => KeyCode::Key8, + winit::event::VirtualKeyCode::Key9 => KeyCode::Key9, + winit::event::VirtualKeyCode::Key0 => KeyCode::Key0, + winit::event::VirtualKeyCode::A => KeyCode::A, + winit::event::VirtualKeyCode::B => KeyCode::B, + winit::event::VirtualKeyCode::C => KeyCode::C, + winit::event::VirtualKeyCode::D => KeyCode::D, + winit::event::VirtualKeyCode::E => KeyCode::E, + winit::event::VirtualKeyCode::F => KeyCode::F, + winit::event::VirtualKeyCode::G => KeyCode::G, + winit::event::VirtualKeyCode::H => KeyCode::H, + winit::event::VirtualKeyCode::I => KeyCode::I, + winit::event::VirtualKeyCode::J => KeyCode::J, + winit::event::VirtualKeyCode::K => KeyCode::K, + winit::event::VirtualKeyCode::L => KeyCode::L, + winit::event::VirtualKeyCode::M => KeyCode::M, + winit::event::VirtualKeyCode::N => KeyCode::N, + winit::event::VirtualKeyCode::O => KeyCode::O, + winit::event::VirtualKeyCode::P => KeyCode::P, + winit::event::VirtualKeyCode::Q => KeyCode::Q, + winit::event::VirtualKeyCode::R => KeyCode::R, + winit::event::VirtualKeyCode::S => KeyCode::S, + winit::event::VirtualKeyCode::T => KeyCode::T, + winit::event::VirtualKeyCode::U => KeyCode::U, + winit::event::VirtualKeyCode::V => KeyCode::V, + winit::event::VirtualKeyCode::W => KeyCode::W, + winit::event::VirtualKeyCode::X => KeyCode::X, + winit::event::VirtualKeyCode::Y => KeyCode::Y, + winit::event::VirtualKeyCode::Z => KeyCode::Z, + winit::event::VirtualKeyCode::Escape => KeyCode::Escape, + winit::event::VirtualKeyCode::F1 => KeyCode::F1, + winit::event::VirtualKeyCode::F2 => KeyCode::F2, + winit::event::VirtualKeyCode::F3 => KeyCode::F3, + winit::event::VirtualKeyCode::F4 => KeyCode::F4, + winit::event::VirtualKeyCode::F5 => KeyCode::F5, + winit::event::VirtualKeyCode::F6 => KeyCode::F6, + winit::event::VirtualKeyCode::F7 => KeyCode::F7, + winit::event::VirtualKeyCode::F8 => KeyCode::F8, + winit::event::VirtualKeyCode::F9 => KeyCode::F9, + winit::event::VirtualKeyCode::F10 => KeyCode::F10, + winit::event::VirtualKeyCode::F11 => KeyCode::F11, + winit::event::VirtualKeyCode::F12 => KeyCode::F12, + winit::event::VirtualKeyCode::F13 => KeyCode::F13, + winit::event::VirtualKeyCode::F14 => KeyCode::F14, + winit::event::VirtualKeyCode::F15 => KeyCode::F15, + winit::event::VirtualKeyCode::F16 => KeyCode::F16, + winit::event::VirtualKeyCode::F17 => KeyCode::F17, + winit::event::VirtualKeyCode::F18 => KeyCode::F18, + winit::event::VirtualKeyCode::F19 => KeyCode::F19, + winit::event::VirtualKeyCode::F20 => KeyCode::F20, + winit::event::VirtualKeyCode::F21 => KeyCode::F21, + winit::event::VirtualKeyCode::F22 => KeyCode::F22, + winit::event::VirtualKeyCode::F23 => KeyCode::F23, + winit::event::VirtualKeyCode::F24 => KeyCode::F24, + winit::event::VirtualKeyCode::Snapshot => KeyCode::Snapshot, + winit::event::VirtualKeyCode::Scroll => KeyCode::Scroll, + winit::event::VirtualKeyCode::Pause => KeyCode::Pause, + winit::event::VirtualKeyCode::Insert => KeyCode::Insert, + winit::event::VirtualKeyCode::Home => KeyCode::Home, + winit::event::VirtualKeyCode::Delete => KeyCode::Delete, + winit::event::VirtualKeyCode::End => KeyCode::End, + winit::event::VirtualKeyCode::PageDown => KeyCode::PageDown, + winit::event::VirtualKeyCode::PageUp => KeyCode::PageUp, + winit::event::VirtualKeyCode::Left => KeyCode::Left, + winit::event::VirtualKeyCode::Up => KeyCode::Up, + winit::event::VirtualKeyCode::Right => KeyCode::Right, + winit::event::VirtualKeyCode::Down => KeyCode::Down, + winit::event::VirtualKeyCode::Back => KeyCode::Backspace, + winit::event::VirtualKeyCode::Return => KeyCode::Enter, + winit::event::VirtualKeyCode::Space => KeyCode::Space, + winit::event::VirtualKeyCode::Compose => KeyCode::Compose, + winit::event::VirtualKeyCode::Caret => KeyCode::Caret, + winit::event::VirtualKeyCode::Numlock => KeyCode::Numlock, + winit::event::VirtualKeyCode::Numpad0 => KeyCode::Numpad0, + winit::event::VirtualKeyCode::Numpad1 => KeyCode::Numpad1, + winit::event::VirtualKeyCode::Numpad2 => KeyCode::Numpad2, + winit::event::VirtualKeyCode::Numpad3 => KeyCode::Numpad3, + winit::event::VirtualKeyCode::Numpad4 => KeyCode::Numpad4, + winit::event::VirtualKeyCode::Numpad5 => KeyCode::Numpad5, + winit::event::VirtualKeyCode::Numpad6 => KeyCode::Numpad6, + winit::event::VirtualKeyCode::Numpad7 => KeyCode::Numpad7, + winit::event::VirtualKeyCode::Numpad8 => KeyCode::Numpad8, + winit::event::VirtualKeyCode::Numpad9 => KeyCode::Numpad9, + winit::event::VirtualKeyCode::AbntC1 => KeyCode::AbntC1, + winit::event::VirtualKeyCode::AbntC2 => KeyCode::AbntC2, + winit::event::VirtualKeyCode::Add => KeyCode::Add, + winit::event::VirtualKeyCode::Apostrophe => KeyCode::Apostrophe, + winit::event::VirtualKeyCode::Apps => KeyCode::Apps, + winit::event::VirtualKeyCode::At => KeyCode::At, + winit::event::VirtualKeyCode::Ax => KeyCode::Ax, + winit::event::VirtualKeyCode::Backslash => KeyCode::Backslash, + winit::event::VirtualKeyCode::Calculator => KeyCode::Calculator, + winit::event::VirtualKeyCode::Capital => KeyCode::Capital, + winit::event::VirtualKeyCode::Colon => KeyCode::Colon, + winit::event::VirtualKeyCode::Comma => KeyCode::Comma, + winit::event::VirtualKeyCode::Convert => KeyCode::Convert, + winit::event::VirtualKeyCode::Decimal => KeyCode::Decimal, + winit::event::VirtualKeyCode::Divide => KeyCode::Divide, + winit::event::VirtualKeyCode::Equals => KeyCode::Equals, + winit::event::VirtualKeyCode::Grave => KeyCode::Grave, + winit::event::VirtualKeyCode::Kana => KeyCode::Kana, + winit::event::VirtualKeyCode::Kanji => KeyCode::Kanji, + winit::event::VirtualKeyCode::LAlt => KeyCode::LAlt, + winit::event::VirtualKeyCode::LBracket => KeyCode::LBracket, + winit::event::VirtualKeyCode::LControl => KeyCode::LControl, + winit::event::VirtualKeyCode::LShift => KeyCode::LShift, + winit::event::VirtualKeyCode::LWin => KeyCode::LWin, + winit::event::VirtualKeyCode::Mail => KeyCode::Mail, + winit::event::VirtualKeyCode::MediaSelect => KeyCode::MediaSelect, + winit::event::VirtualKeyCode::MediaStop => KeyCode::MediaStop, + winit::event::VirtualKeyCode::Minus => KeyCode::Minus, + winit::event::VirtualKeyCode::Multiply => KeyCode::Multiply, + winit::event::VirtualKeyCode::Mute => KeyCode::Mute, + winit::event::VirtualKeyCode::MyComputer => KeyCode::MyComputer, + winit::event::VirtualKeyCode::NavigateForward => { + KeyCode::NavigateForward + } + winit::event::VirtualKeyCode::NavigateBackward => { + KeyCode::NavigateBackward + } + winit::event::VirtualKeyCode::NextTrack => KeyCode::NextTrack, + winit::event::VirtualKeyCode::NoConvert => KeyCode::NoConvert, + winit::event::VirtualKeyCode::NumpadComma => KeyCode::NumpadComma, + winit::event::VirtualKeyCode::NumpadEnter => KeyCode::NumpadEnter, + winit::event::VirtualKeyCode::NumpadEquals => KeyCode::NumpadEquals, + winit::event::VirtualKeyCode::OEM102 => KeyCode::OEM102, + winit::event::VirtualKeyCode::Period => KeyCode::Period, + winit::event::VirtualKeyCode::PlayPause => KeyCode::PlayPause, + winit::event::VirtualKeyCode::Power => KeyCode::Power, + winit::event::VirtualKeyCode::PrevTrack => KeyCode::PrevTrack, + winit::event::VirtualKeyCode::RAlt => KeyCode::RAlt, + winit::event::VirtualKeyCode::RBracket => KeyCode::RBracket, + winit::event::VirtualKeyCode::RControl => KeyCode::RControl, + winit::event::VirtualKeyCode::RShift => KeyCode::RShift, + winit::event::VirtualKeyCode::RWin => KeyCode::RWin, + winit::event::VirtualKeyCode::Semicolon => KeyCode::Semicolon, + winit::event::VirtualKeyCode::Slash => KeyCode::Slash, + winit::event::VirtualKeyCode::Sleep => KeyCode::Sleep, + winit::event::VirtualKeyCode::Stop => KeyCode::Stop, + winit::event::VirtualKeyCode::Subtract => KeyCode::Subtract, + winit::event::VirtualKeyCode::Sysrq => KeyCode::Sysrq, + winit::event::VirtualKeyCode::Tab => KeyCode::Tab, + winit::event::VirtualKeyCode::Underline => KeyCode::Underline, + winit::event::VirtualKeyCode::Unlabeled => KeyCode::Unlabeled, + winit::event::VirtualKeyCode::VolumeDown => KeyCode::VolumeDown, + winit::event::VirtualKeyCode::VolumeUp => KeyCode::VolumeUp, + winit::event::VirtualKeyCode::Wake => KeyCode::Wake, + winit::event::VirtualKeyCode::WebBack => KeyCode::WebBack, + winit::event::VirtualKeyCode::WebFavorites => KeyCode::WebFavorites, + winit::event::VirtualKeyCode::WebForward => KeyCode::WebForward, + winit::event::VirtualKeyCode::WebHome => KeyCode::WebHome, + winit::event::VirtualKeyCode::WebRefresh => KeyCode::WebRefresh, + winit::event::VirtualKeyCode::WebSearch => KeyCode::WebSearch, + winit::event::VirtualKeyCode::WebStop => KeyCode::WebStop, + winit::event::VirtualKeyCode::Yen => KeyCode::Yen, + winit::event::VirtualKeyCode::Copy => KeyCode::Copy, + winit::event::VirtualKeyCode::Paste => KeyCode::Paste, + winit::event::VirtualKeyCode::Cut => KeyCode::Cut, + } + } +} diff --git a/core/src/input/mouse.rs b/core/src/input/mouse.rs new file mode 100644 index 00000000..d37f5b96 --- /dev/null +++ b/core/src/input/mouse.rs @@ -0,0 +1,6 @@ +//! Build mouse events. +mod button; +mod event; + +pub use button::Button; +pub use event::Event; diff --git a/core/src/input/mouse/button.rs b/core/src/input/mouse/button.rs new file mode 100644 index 00000000..6320d701 --- /dev/null +++ b/core/src/input/mouse/button.rs @@ -0,0 +1,32 @@ +/// The button of a mouse. +/// +/// If you are using [`winit`], consider enabling the `winit` feature to get +/// conversion implementations for free! +/// +/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ +#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] +pub enum Button { + /// The left mouse button. + Left, + + /// The right mouse button. + Right, + + /// The middle (wheel) button. + Middle, + + /// Some other button. + Other(u8), +} + +#[cfg(feature = "winit")] +impl From for super::Button { + fn from(mouse_button: winit::event::MouseButton) -> Self { + match mouse_button { + winit::event::MouseButton::Left => Button::Left, + winit::event::MouseButton::Right => Button::Right, + winit::event::MouseButton::Middle => Button::Middle, + winit::event::MouseButton::Other(other) => Button::Other(other), + } + } +} diff --git a/core/src/input/mouse/event.rs b/core/src/input/mouse/event.rs new file mode 100644 index 00000000..7b68208f --- /dev/null +++ b/core/src/input/mouse/event.rs @@ -0,0 +1,44 @@ +use super::Button; +use crate::input::ButtonState; + +/// A mouse event. +/// +/// _**Note:** This type is largely incomplete! If you need to track +/// additional events, feel free to [open an issue] and share your use case!_ +/// +/// [open an issue]: https://github.com/hecrj/iced/issues +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Event { + /// The mouse cursor entered the window. + CursorEntered, + + /// The mouse cursor left the window. + CursorLeft, + + /// The mouse cursor was moved + CursorMoved { + /// The X coordinate of the mouse position + x: f32, + + /// The Y coordinate of the mouse position + y: f32, + }, + + /// A mouse button was pressed or released. + Input { + /// The state of the button + state: ButtonState, + + /// The button identifier + button: Button, + }, + + /// The mouse wheel was scrolled. + WheelScrolled { + /// The number of horizontal lines scrolled + delta_x: f32, + + /// The number of vertical lines scrolled + delta_y: f32, + }, +} diff --git a/core/src/layout.rs b/core/src/layout.rs new file mode 100644 index 00000000..de284a43 --- /dev/null +++ b/core/src/layout.rs @@ -0,0 +1,62 @@ +use stretch::result; + +use crate::{Point, Rectangle, Vector}; + +/// The computed bounds of a [`Node`] and its children. +/// +/// This type is provided by the GUI runtime to [`Widget::on_event`] and +/// [`Widget::draw`], describing the layout of the [`Node`] produced by +/// [`Widget::node`]. +/// +/// [`Node`]: struct.Node.html +/// [`Widget::on_event`]: widget/trait.Widget.html#method.on_event +/// [`Widget::draw`]: widget/trait.Widget.html#tymethod.draw +/// [`Widget::node`]: widget/trait.Widget.html#tymethod.node +#[derive(Debug)] +pub struct Layout<'a> { + layout: &'a result::Layout, + position: Point, +} + +impl<'a> Layout<'a> { + pub(crate) fn new(layout: &'a result::Layout) -> Self { + Self::with_parent_position(layout, Point::new(0.0, 0.0)) + } + + fn with_parent_position( + layout: &'a result::Layout, + parent_position: Point, + ) -> Self { + let position = + parent_position + Vector::new(layout.location.x, layout.location.y); + + Layout { layout, position } + } + + /// Gets the bounds of the [`Layout`]. + /// + /// The returned [`Rectangle`] describes the position and size of a + /// [`Node`]. + /// + /// [`Layout`]: struct.Layout.html + /// [`Rectangle`]: struct.Rectangle.html + /// [`Node`]: struct.Node.html + pub fn bounds(&self) -> Rectangle { + Rectangle { + x: self.position.x, + y: self.position.y, + width: self.layout.size.width, + height: self.layout.size.height, + } + } + + /// Returns an iterator over the [`Layout`] of the children of a [`Node`]. + /// + /// [`Layout`]: struct.Layout.html + /// [`Node`]: struct.Node.html + pub fn children(&'a self) -> impl Iterator> { + self.layout.children.iter().map(move |layout| { + Layout::with_parent_position(layout, self.position) + }) + } +} diff --git a/core/src/lib.rs b/core/src/lib.rs new file mode 100644 index 00000000..c1c18b41 --- /dev/null +++ b/core/src/lib.rs @@ -0,0 +1,229 @@ +//! Iced is a renderer-agnostic GUI library focused on simplicity and +//! type-safety. Inspired by [Elm]. +//! +//! # Features +//! * Simple, easy-to-use, renderer-agnostic API +//! * Responsive, flexbox-based layouting +//! * Type-safe, reactive programming model +//! * Built-in widgets +//! * Custom widget support +//! +//! Check out the [repository] and the [examples] for more details! +//! +//! [examples]: https://github.com/hecrj/iced/tree/0.1.0/examples +//! [repository]: https://github.com/hecrj/iced +//! +//! # Usage +//! Inspired by [The Elm Architecture], Iced expects you to split user interfaces +//! into four different concepts: +//! +//! * __State__ — the state of your application +//! * __Messages__ — user interactions or meaningful events that you care +//! about +//! * __View logic__ — a way to display your __state__ as widgets that +//! may produce __messages__ on user interaction +//! * __Update logic__ — a way to react to __messages__ and update your +//! __state__ +//! +//! We can build something to see how this works! Let's say we want a simple counter +//! that can be incremented and decremented using two buttons. +//! +//! We start by modelling the __state__ of our application: +//! +//! ``` +//! use iced::button; +//! +//! struct Counter { +//! // The counter value +//! value: i32, +//! +//! // The local state of the two buttons +//! increment_button: button::State, +//! decrement_button: button::State, +//! } +//! ``` +//! +//! Next, we need to define the possible user interactions of our counter: +//! the button presses. These interactions are our __messages__: +//! +//! ``` +//! #[derive(Debug, Clone, Copy)] +//! pub enum Message { +//! IncrementPressed, +//! DecrementPressed, +//! } +//! ``` +//! +//! Now, let's show the actual counter by putting it all together in our +//! __view logic__: +//! +//! ``` +//! # use iced::button; +//! # +//! # struct Counter { +//! # // The counter value +//! # value: i32, +//! # +//! # // The local state of the two buttons +//! # increment_button: button::State, +//! # decrement_button: button::State, +//! # } +//! # +//! # #[derive(Debug, Clone, Copy)] +//! # pub enum Message { +//! # IncrementPressed, +//! # DecrementPressed, +//! # } +//! # +//! # mod iced_wgpu { +//! # use iced::{ +//! # button, text, text::HorizontalAlignment, text::VerticalAlignment, +//! # MouseCursor, Node, Point, Rectangle, Style, +//! # }; +//! # +//! # pub struct Renderer {} +//! # +//! # impl button::Renderer for Renderer { +//! # fn draw( +//! # &mut self, +//! # _cursor_position: Point, +//! # _bounds: Rectangle, +//! # _state: &button::State, +//! # _label: &str, +//! # _class: button::Class, +//! # ) -> MouseCursor { +//! # MouseCursor::OutOfBounds +//! # } +//! # } +//! # +//! # impl text::Renderer<[f32; 4]> for Renderer { +//! # fn node(&self, style: Style, _content: &str, _size: Option) -> Node { +//! # Node::new(style) +//! # } +//! # +//! # fn draw( +//! # &mut self, +//! # _bounds: Rectangle, +//! # _content: &str, +//! # _size: Option, +//! # _color: Option<[f32; 4]>, +//! # _horizontal_alignment: HorizontalAlignment, +//! # _vertical_alignment: VerticalAlignment, +//! # ) { +//! # } +//! # } +//! # } +//! use iced::{Button, Column, Text}; +//! use iced_wgpu::Renderer; // Iced does not include a renderer! We need to bring our own! +//! +//! impl Counter { +//! pub fn view(&mut self) -> Column { +//! // We use a column: a simple vertical layout +//! Column::new() +//! .push( +//! // The increment button. We tell it to produce an +//! // `IncrementPressed` message when pressed +//! Button::new(&mut self.increment_button, "+") +//! .on_press(Message::IncrementPressed), +//! ) +//! .push( +//! // We show the value of the counter here +//! Text::new(&self.value.to_string()).size(50), +//! ) +//! .push( +//! // The decrement button. We tell it to produce a +//! // `DecrementPressed` message when pressed +//! Button::new(&mut self.decrement_button, "-") +//! .on_press(Message::DecrementPressed), +//! ) +//! } +//! } +//! ``` +//! +//! Finally, we need to be able to react to any produced __messages__ and change +//! our __state__ accordingly in our __update logic__: +//! +//! ``` +//! # use iced::button; +//! # +//! # struct Counter { +//! # // The counter value +//! # value: i32, +//! # +//! # // The local state of the two buttons +//! # increment_button: button::State, +//! # decrement_button: button::State, +//! # } +//! # +//! # #[derive(Debug, Clone, Copy)] +//! # pub enum Message { +//! # IncrementPressed, +//! # DecrementPressed, +//! # } +//! impl Counter { +//! // ... +//! +//! pub fn update(&mut self, message: Message) { +//! match message { +//! Message::IncrementPressed => { +//! self.value += 1; +//! } +//! Message::DecrementPressed => { +//! self.value -= 1; +//! } +//! } +//! } +//! } +//! ``` +//! +//! And that's everything! We just wrote a whole user interface. Iced is now able +//! to: +//! +//! 1. Take the result of our __view logic__ and layout its widgets. +//! 1. Process events from our system and produce __messages__ for our +//! __update logic__. +//! 1. Draw the resulting user interface using our chosen __renderer__. +//! +//! Check out the [`UserInterface`] type to learn how to wire everything up! +//! +//! [Elm]: https://elm-lang.org/ +//! [The Elm Architecture]: https://guide.elm-lang.org/architecture/ +//! [documentation]: https://docs.rs/iced +//! [examples]: https://github.com/hecrj/iced/tree/master/examples +//! [`UserInterface`]: struct.UserInterface.html +#![deny(missing_docs)] +#![deny(missing_debug_implementations)] +#![deny(unused_results)] +#![deny(unsafe_code)] +#![deny(rust_2018_idioms)] +pub mod input; +pub mod renderer; +pub mod widget; + +mod element; +mod event; +mod hasher; +mod layout; +mod mouse_cursor; +mod node; +mod point; +mod rectangle; +mod style; +mod user_interface; +mod vector; + +#[doc(no_inline)] +pub use stretch::{geometry::Size, number::Number}; + +pub use element::Element; +pub use event::Event; +pub use hasher::Hasher; +pub use layout::Layout; +pub use mouse_cursor::MouseCursor; +pub use node::Node; +pub use point::Point; +pub use rectangle::Rectangle; +pub use style::{Align, Justify, Style}; +pub use user_interface::{Cache, UserInterface}; +pub(crate) use vector::Vector; +pub use widget::*; diff --git a/core/src/mouse_cursor.rs b/core/src/mouse_cursor.rs new file mode 100644 index 00000000..4ef6361a --- /dev/null +++ b/core/src/mouse_cursor.rs @@ -0,0 +1,35 @@ +/// The state of the mouse cursor. +#[derive(Debug, Eq, PartialEq, Clone, Copy)] +pub enum MouseCursor { + /// The cursor is out of the bounds of the user interface. + OutOfBounds, + + /// The cursor is over a non-interactive widget. + Idle, + + /// The cursor is over a clickable widget. + Pointer, + + /// The cursor is over a busy widget. + Working, + + /// The cursor is over a grabbable widget. + Grab, + + /// The cursor is grabbing a widget. + Grabbing, +} + +#[cfg(feature = "winit")] +impl From for winit::window::CursorIcon { + fn from(mouse_cursor: MouseCursor) -> winit::window::CursorIcon { + match mouse_cursor { + MouseCursor::OutOfBounds => winit::window::CursorIcon::Default, + MouseCursor::Idle => winit::window::CursorIcon::Default, + MouseCursor::Pointer => winit::window::CursorIcon::Hand, + MouseCursor::Working => winit::window::CursorIcon::Progress, + MouseCursor::Grab => winit::window::CursorIcon::Grab, + MouseCursor::Grabbing => winit::window::CursorIcon::Grabbing, + } + } +} diff --git a/core/src/node.rs b/core/src/node.rs new file mode 100644 index 00000000..1db10d7f --- /dev/null +++ b/core/src/node.rs @@ -0,0 +1,60 @@ +use stretch::node; + +use crate::{Number, Size, Style}; + +/// The visual requirements of a [`Widget`] and its children. +/// +/// When there have been changes and the [`Layout`] needs to be recomputed, the +/// runtime obtains a [`Node`] by calling [`Widget::node`]. +/// +/// [`Style`]: struct.Style.html +/// [`Widget`]: widget/trait.Widget.html +/// [`Node`]: struct.Node.html +/// [`Widget::node`]: widget/trait.Widget.html#tymethod.node +/// [`Layout`]: struct.Layout.html +#[derive(Debug)] +pub struct Node(pub(crate) node::Node); + +impl Node { + /// Creates a new [`Node`] with the given [`Style`]. + /// + /// [`Node`]: struct.Node.html + /// [`Style`]: struct.Style.html + pub fn new(style: Style) -> Node { + Self::with_children(style, Vec::new()) + } + + /// Creates a new [`Node`] with the given [`Style`] and a measure function. + /// + /// This type of node cannot have any children. + /// + /// You should use this when your [`Widget`] can adapt its contents to the + /// size of its container. The measure function will receive the container + /// size as a parameter and must compute the size of the [`Node`] inside + /// the given bounds (if the `Number` for a dimension is `Undefined` it + /// means that it has no boundary). + /// + /// [`Node`]: struct.Node.html + /// [`Style`]: struct.Style.html + /// [`Widget`]: widget/trait.Widget.html + pub fn with_measure(style: Style, measure: F) -> Node + where + F: 'static + Fn(Size) -> Size, + { + Node(node::Node::new_leaf( + style.0, + Box::new(move |size| Ok(measure(size))), + )) + } + + /// Creates a new [`Node`] with the given [`Style`] and children. + /// + /// [`Node`]: struct.Node.html + /// [`Style`]: struct.Style.html + pub fn with_children(style: Style, children: Vec) -> Node { + Node(node::Node::new( + style.0, + children.iter().map(|c| &c.0).collect(), + )) + } +} diff --git a/core/src/point.rs b/core/src/point.rs new file mode 100644 index 00000000..183998dd --- /dev/null +++ b/core/src/point.rs @@ -0,0 +1,31 @@ +use crate::Vector; + +/// A 2D point. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Point { + /// The X coordinate. + pub x: f32, + + /// The Y coordinate. + pub y: f32, +} + +impl Point { + /// Creates a new [`Point`] with the given coordinates. + /// + /// [`Point`]: struct.Point.html + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} + +impl std::ops::Add for Point { + type Output = Self; + + fn add(self, vector: Vector) -> Self { + Self { + x: self.x + vector.x, + y: self.y + vector.y, + } + } +} diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs new file mode 100644 index 00000000..95c2570c --- /dev/null +++ b/core/src/rectangle.rs @@ -0,0 +1,30 @@ +use crate::Point; + +/// A rectangle. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Rectangle { + /// X coordinate of the top-left corner. + pub x: T, + + /// Y coordinate of the top-left corner. + pub y: T, + + /// Width of the rectangle. + pub width: T, + + /// Height of the rectangle. + pub height: T, +} + +impl Rectangle { + /// Returns true if the given [`Point`] is contained in the [`Rectangle`]. + /// + /// [`Point`]: struct.Point.html + /// [`Rectangle`]: struct.Rectangle.html + pub fn contains(&self, point: Point) -> bool { + self.x <= point.x + && point.x <= self.x + self.width + && self.y <= point.y + && point.y <= self.y + self.height + } +} diff --git a/core/src/renderer.rs b/core/src/renderer.rs new file mode 100644 index 00000000..b445190b --- /dev/null +++ b/core/src/renderer.rs @@ -0,0 +1,45 @@ +//! Write your own renderer. +//! +//! There is not a common entrypoint or trait for a __renderer__ in Iced. +//! Instead, every [`Widget`] constrains its generic `Renderer` type as +//! necessary. +//! +//! This approach is flexible and composable. For instance, the +//! [`Text`] widget only needs a [`text::Renderer`] while a [`Checkbox`] widget +//! needs both a [`text::Renderer`] and a [`checkbox::Renderer`], reusing logic. +//! +//! In the end, a __renderer__ satisfying all the constraints is +//! needed to build a [`UserInterface`]. +//! +//! [`Widget`]: ../widget/trait.Widget.html +//! [`UserInterface`]: ../struct.UserInterface.html +//! [`Text`]: ../widget/text/struct.Text.html +//! [`text::Renderer`]: ../widget/text/trait.Renderer.html +//! [`Checkbox`]: ../widget/checkbox/struct.Checkbox.html +//! [`checkbox::Renderer`]: ../widget/checkbox/trait.Renderer.html +use crate::Layout; + +/// A renderer able to graphically explain a [`Layout`]. +/// +/// [`Layout`]: ../struct.Layout.html +pub trait Debugger { + /// The color type that will be used to configure the _explanation_. + /// + /// This is the type that will be asked in [`Element::explain`]. + /// + /// [`Element::explain`]: ../struct.Element.html#method.explain + type Color: Copy; + + /// Explains the [`Layout`] of an [`Element`] for debugging purposes. + /// + /// This will be called when [`Element::explain`] has been used. It should + /// _explain_ the given [`Layout`] graphically. + /// + /// A common approach consists in recursively rendering the bounds of the + /// [`Layout`] and its children. + /// + /// [`Layout`]: struct.Layout.html + /// [`Element`]: struct.Element.html + /// [`Element::explain`]: struct.Element.html#method.explain + fn explain(&mut self, layout: &Layout<'_>, color: Self::Color); +} diff --git a/core/src/style.rs b/core/src/style.rs new file mode 100644 index 00000000..575ea366 --- /dev/null +++ b/core/src/style.rs @@ -0,0 +1,262 @@ +use std::hash::{Hash, Hasher}; +use stretch::{geometry, style}; + +/// The appearance of a [`Node`]. +/// +/// [`Node`]: struct.Node.html +#[derive(Debug, Clone, Copy)] +pub struct Style(pub(crate) style::Style); + +impl Style { + /// Defines the width of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn width(mut self, width: u16) -> Self { + self.0.size.width = style::Dimension::Points(width as f32); + self + } + + /// Defines the height of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn height(mut self, height: u16) -> Self { + self.0.size.height = style::Dimension::Points(height as f32); + self + } + + /// Defines the minimum width of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn min_width(mut self, min_width: u16) -> Self { + self.0.min_size.width = style::Dimension::Points(min_width as f32); + self + } + + /// Defines the maximum width of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn max_width(mut self, max_width: u16) -> Self { + self.0.max_size.width = style::Dimension::Points(max_width as f32); + self.fill_width() + } + + /// Defines the minimum height of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn min_height(mut self, min_height: u16) -> Self { + self.0.min_size.height = + style::Dimension::Points(f32::from(min_height)); + self + } + + /// Defines the maximum height of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn max_height(mut self, max_height: u16) -> Self { + self.0.max_size.height = + style::Dimension::Points(f32::from(max_height)); + self.fill_height() + } + + /// Makes a [`Node`] fill all the horizontal available space. + /// + /// [`Node`]: struct.Node.html + pub fn fill_width(mut self) -> Self { + self.0.size.width = stretch::style::Dimension::Percent(1.0); + self + } + + /// Makes a [`Node`] fill all the vertical available space. + /// + /// [`Node`]: struct.Node.html + pub fn fill_height(mut self) -> Self { + self.0.size.height = stretch::style::Dimension::Percent(1.0); + self + } + + pub(crate) fn align_items(mut self, align: Align) -> Self { + self.0.align_items = align.into(); + self + } + + pub(crate) fn justify_content(mut self, justify: Justify) -> Self { + self.0.justify_content = justify.into(); + self + } + + /// Sets the alignment of a [`Node`]. + /// + /// If the [`Node`] is inside a... + /// + /// * [`Column`], this setting will affect its __horizontal__ alignment. + /// * [`Row`], this setting will affect its __vertical__ alignment. + /// + /// [`Node`]: struct.Node.html + /// [`Column`]: widget/struct.Column.html + /// [`Row`]: widget/struct.Row.html + pub fn align_self(mut self, align: Align) -> Self { + self.0.align_self = align.into(); + self + } + + /// Sets the padding of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn padding(mut self, px: u16) -> Self { + self.0.padding = stretch::geometry::Rect { + start: style::Dimension::Points(px as f32), + end: style::Dimension::Points(px as f32), + top: style::Dimension::Points(px as f32), + bottom: style::Dimension::Points(px as f32), + }; + + self + } +} + +impl Default for Style { + fn default() -> Style { + Style(style::Style { + align_items: style::AlignItems::FlexStart, + justify_content: style::JustifyContent::FlexStart, + ..style::Style::default() + }) + } +} + +impl Hash for Style { + fn hash(&self, state: &mut H) { + hash_size(&self.0.size, state); + hash_size(&self.0.min_size, state); + hash_size(&self.0.max_size, state); + + hash_rect(&self.0.margin, state); + + (self.0.flex_direction as u8).hash(state); + (self.0.align_items as u8).hash(state); + (self.0.justify_content as u8).hash(state); + (self.0.align_self as u8).hash(state); + (self.0.flex_grow as u32).hash(state); + } +} + +fn hash_size( + size: &geometry::Size, + state: &mut H, +) { + hash_dimension(size.width, state); + hash_dimension(size.height, state); +} + +fn hash_rect( + rect: &geometry::Rect, + state: &mut H, +) { + hash_dimension(rect.start, state); + hash_dimension(rect.end, state); + hash_dimension(rect.top, state); + hash_dimension(rect.bottom, state); +} + +fn hash_dimension(dimension: style::Dimension, state: &mut H) { + match dimension { + style::Dimension::Undefined => state.write_u8(0), + style::Dimension::Auto => state.write_u8(1), + style::Dimension::Points(points) => { + state.write_u8(2); + (points as u32).hash(state); + } + style::Dimension::Percent(percent) => { + state.write_u8(3); + (percent as u32).hash(state); + } + } +} + +/// Alignment on the cross axis of a container. +/// +/// * On a [`Column`], it describes __horizontal__ alignment. +/// * On a [`Row`], it describes __vertical__ alignment. +/// +/// [`Column`]: widget/struct.Column.html +/// [`Row`]: widget/struct.Row.html +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Align { + /// Align at the start of the cross axis. + Start, + + /// Align at the center of the cross axis. + Center, + + /// Align at the end of the cross axis. + End, + + /// Stretch over the cross axis. + Stretch, +} + +#[doc(hidden)] +impl From for style::AlignItems { + fn from(align: Align) -> Self { + match align { + Align::Start => style::AlignItems::FlexStart, + Align::Center => style::AlignItems::Center, + Align::End => style::AlignItems::FlexEnd, + Align::Stretch => style::AlignItems::Stretch, + } + } +} + +#[doc(hidden)] +impl From for style::AlignSelf { + fn from(align: Align) -> Self { + match align { + Align::Start => style::AlignSelf::FlexStart, + Align::Center => style::AlignSelf::Center, + Align::End => style::AlignSelf::FlexEnd, + Align::Stretch => style::AlignSelf::Stretch, + } + } +} + +/// Distribution on the main axis of a container. +/// +/// * On a [`Column`], it describes __vertical__ distribution. +/// * On a [`Row`], it describes __horizontal__ distribution. +/// +/// [`Column`]: widget/struct.Column.html +/// [`Row`]: widget/struct.Row.html +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Justify { + /// Place items at the start of the main axis. + Start, + + /// Place items at the center of the main axis. + Center, + + /// Place items at the end of the main axis. + End, + + /// Place items with space between. + SpaceBetween, + + /// Place items with space around. + SpaceAround, + + /// Place items with evenly distributed space. + SpaceEvenly, +} + +#[doc(hidden)] +impl From for style::JustifyContent { + fn from(justify: Justify) -> Self { + match justify { + Justify::Start => style::JustifyContent::FlexStart, + Justify::Center => style::JustifyContent::Center, + Justify::End => style::JustifyContent::FlexEnd, + Justify::SpaceBetween => style::JustifyContent::SpaceBetween, + Justify::SpaceAround => style::JustifyContent::SpaceAround, + Justify::SpaceEvenly => style::JustifyContent::SpaceEvenly, + } + } +} diff --git a/core/src/user_interface.rs b/core/src/user_interface.rs new file mode 100644 index 00000000..2c7cbf82 --- /dev/null +++ b/core/src/user_interface.rs @@ -0,0 +1,323 @@ +use crate::{input::mouse, Column, Element, Event, Layout, MouseCursor, Point}; + +use std::hash::Hasher; +use stretch::result; + +/// A set of interactive graphical elements with a specific [`Layout`]. +/// +/// It can be updated and drawn. +/// +/// Iced tries to avoid dictating how to write your event loop. You are in +/// charge of using this type in your system in any way you want. +/// +/// [`Layout`]: struct.Layout.html +#[derive(Debug)] +pub struct UserInterface<'a, Message, Renderer> { + hash: u64, + root: Element<'a, Message, Renderer>, + layout: result::Layout, + cursor_position: Point, +} + +impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { + /// Builds a user interface for an [`Element`]. + /// + /// It is able to avoid expensive computations when using a [`Cache`] + /// obtained from a previous instance of a [`UserInterface`]. + /// + /// [`Element`]: struct.Element.html + /// [`Cache`]: struct.Cache.html + /// [`UserInterface`]: struct.UserInterface.html + /// + /// # Example + /// Imagine we want to build a [`UserInterface`] for + /// [the counter example that we previously wrote](index.html#usage). Here + /// is naive way to set up our application loop: + /// + /// ```no_run + /// use iced::{UserInterface, Cache}; + /// use iced_wgpu::Renderer; + /// + /// # mod iced_wgpu { + /// # pub struct Renderer; + /// # + /// # impl Renderer { + /// # pub fn new() -> Self { Renderer } + /// # } + /// # } + /// # + /// # use iced::Column; + /// # + /// # pub struct Counter; + /// # + /// # impl Counter { + /// # pub fn new() -> Self { Counter } + /// # pub fn view(&self) -> Column<(), Renderer> { + /// # Column::new() + /// # } + /// # } + /// // Initialization + /// let mut counter = Counter::new(); + /// let mut cache = Cache::new(); + /// let mut renderer = Renderer::new(); + /// + /// // Application loop + /// loop { + /// // Process system events here... + /// + /// // Build the user interface + /// let user_interface = UserInterface::build( + /// counter.view(), + /// cache, + /// &renderer, + /// ); + /// + /// // Update and draw the user interface here... + /// // ... + /// + /// // Obtain the cache for the next iteration + /// cache = user_interface.into_cache(); + /// } + /// ``` + pub fn build>>( + root: E, + cache: Cache, + renderer: &Renderer, + ) -> Self { + let root = root.into(); + + let hasher = &mut crate::Hasher::default(); + root.hash_layout(hasher); + + let hash = hasher.finish(); + + let layout = if hash == cache.hash { + cache.layout + } else { + root.compute_layout(renderer) + }; + + UserInterface { + hash, + root, + layout, + cursor_position: cache.cursor_position, + } + } + + /// Updates the [`UserInterface`] by processing each provided [`Event`]. + /// + /// It returns __messages__ that may have been produced as a result of user + /// interactions. You should feed these to your __update logic__. + /// + /// [`UserInterface`]: struct.UserInterface.html + /// [`Event`]: enum.Event.html + /// + /// # Example + /// Let's allow our [counter](index.html#usage) to change state by completing + /// [the previous example](#example): + /// + /// ```no_run + /// use iced::{UserInterface, Cache}; + /// use iced_wgpu::Renderer; + /// + /// # mod iced_wgpu { + /// # pub struct Renderer; + /// # + /// # impl Renderer { + /// # pub fn new() -> Self { Renderer } + /// # } + /// # } + /// # + /// # use iced::Column; + /// # + /// # pub struct Counter; + /// # + /// # impl Counter { + /// # pub fn new() -> Self { Counter } + /// # pub fn view(&self) -> Column<(), Renderer> { + /// # Column::new() + /// # } + /// # pub fn update(&mut self, message: ()) {} + /// # } + /// let mut counter = Counter::new(); + /// let mut cache = Cache::new(); + /// let mut renderer = Renderer::new(); + /// + /// // Initialize our event storage + /// let mut events = Vec::new(); + /// + /// loop { + /// // Process system events... + /// + /// let mut user_interface = UserInterface::build( + /// counter.view(), + /// cache, + /// &renderer, + /// ); + /// + /// // Update the user interface + /// let messages = user_interface.update(events.drain(..)); + /// + /// cache = user_interface.into_cache(); + /// + /// // Process the produced messages + /// for message in messages { + /// counter.update(message); + /// } + /// } + /// ``` + pub fn update( + &mut self, + events: impl Iterator, + ) -> Vec { + let mut messages = Vec::new(); + + for event in events { + if let Event::Mouse(mouse::Event::CursorMoved { x, y }) = event { + self.cursor_position = Point::new(x, y); + } + + self.root.widget.on_event( + event, + Layout::new(&self.layout), + self.cursor_position, + &mut messages, + ); + } + + messages + } + + /// Draws the [`UserInterface`] with the provided [`Renderer`]. + /// + /// It returns the current state of the [`MouseCursor`]. You should update + /// the icon of the mouse cursor accordingly in your system. + /// + /// [`UserInterface`]: struct.UserInterface.html + /// [`Renderer`]: trait.Renderer.html + /// [`MouseCursor`]: enum.MouseCursor.html + /// + /// # Example + /// We can finally draw our [counter](index.html#usage) by + /// [completing the last example](#example-1): + /// + /// ```no_run + /// use iced::{UserInterface, Cache}; + /// use iced_wgpu::Renderer; + /// + /// # mod iced_wgpu { + /// # pub struct Renderer; + /// # + /// # impl Renderer { + /// # pub fn new() -> Self { Renderer } + /// # } + /// # } + /// # + /// # use iced::Column; + /// # + /// # pub struct Counter; + /// # + /// # impl Counter { + /// # pub fn new() -> Self { Counter } + /// # pub fn view(&self) -> Column<(), Renderer> { + /// # Column::new() + /// # } + /// # pub fn update(&mut self, message: ()) {} + /// # } + /// let mut counter = Counter::new(); + /// let mut cache = Cache::new(); + /// let mut renderer = Renderer::new(); + /// let mut events = Vec::new(); + /// + /// loop { + /// // Process system events... + /// + /// let mut user_interface = UserInterface::build( + /// counter.view(), + /// cache, + /// &renderer, + /// ); + /// + /// let messages = user_interface.update(events.drain(..)); + /// + /// // Draw the user interface + /// let mouse_cursor = user_interface.draw(&mut renderer); + /// + /// cache = user_interface.into_cache(); + /// + /// for message in messages { + /// counter.update(message); + /// } + /// + /// // Update mouse cursor icon... + /// // Flush rendering operations... + /// } + /// ``` + pub fn draw(&self, renderer: &mut Renderer) -> MouseCursor { + self.root.widget.draw( + renderer, + Layout::new(&self.layout), + self.cursor_position, + ) + } + + /// Extract the [`Cache`] of the [`UserInterface`], consuming it in the + /// process. + /// + /// [`Cache`]: struct.Cache.html + /// [`UserInterface`]: struct.UserInterface.html + pub fn into_cache(self) -> Cache { + Cache { + hash: self.hash, + layout: self.layout, + cursor_position: self.cursor_position, + } + } +} + +/// Reusable data of a specific [`UserInterface`]. +/// +/// [`UserInterface`]: struct.UserInterface.html +#[derive(Debug, Clone)] +pub struct Cache { + hash: u64, + layout: result::Layout, + cursor_position: Point, +} + +impl Cache { + /// Creates an empty [`Cache`]. + /// + /// You should use this to initialize a [`Cache`] before building your first + /// [`UserInterface`]. + /// + /// [`Cache`]: struct.Cache.html + /// [`UserInterface`]: struct.UserInterface.html + pub fn new() -> Cache { + let root: Element<'_, (), ()> = Column::new().into(); + + let hasher = &mut crate::Hasher::default(); + root.hash_layout(hasher); + + Cache { + hash: hasher.finish(), + layout: root.compute_layout(&()), + cursor_position: Point::new(0.0, 0.0), + } + } +} + +impl Default for Cache { + fn default() -> Cache { + Cache::new() + } +} + +impl PartialEq for Cache { + fn eq(&self, other: &Cache) -> bool { + self.hash == other.hash && self.cursor_position == other.cursor_position + } +} + +impl Eq for Cache {} diff --git a/core/src/vector.rs b/core/src/vector.rs new file mode 100644 index 00000000..f45daab9 --- /dev/null +++ b/core/src/vector.rs @@ -0,0 +1,15 @@ +/// A 2D vector. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Vector { + pub x: f32, + pub y: f32, +} + +impl Vector { + /// Creates a new [`Vector`] with the given components. + /// + /// [`Vector`]: struct.Vector.html + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} diff --git a/core/src/widget.rs b/core/src/widget.rs new file mode 100644 index 00000000..30606934 --- /dev/null +++ b/core/src/widget.rs @@ -0,0 +1,114 @@ +//! Use the built-in widgets or create your own. +//! +//! # Built-in widgets +//! Every built-in drawable widget has its own module with a `Renderer` trait +//! that must be implemented by a [renderer] before being able to use it as +//! a [`Widget`]. +//! +//! # Custom widgets +//! If you want to implement a custom widget, you simply need to implement the +//! [`Widget`] trait. You can use the API of the built-in widgets as a guide or +//! source of inspiration. +//! +//! # Re-exports +//! For convenience, the contents of this module are available at the root +//! module. Therefore, you can directly type: +//! +//! ``` +//! use iced::{button, Button, Widget}; +//! ``` +//! +//! [`Widget`]: trait.Widget.html +//! [renderer]: ../renderer/index.html +mod column; +mod row; + +pub mod button; +pub mod checkbox; +pub mod image; +//pub mod progress_bar; +pub mod radio; +pub mod slider; +pub mod text; + +pub use button::Button; +pub use checkbox::Checkbox; +pub use column::Column; +pub use image::Image; +//pub use progress_bar::ProgressBar; +pub use radio::Radio; +pub use row::Row; +pub use slider::Slider; +pub use text::Text; + +use crate::{Event, Hasher, Layout, MouseCursor, Node, Point}; + +/// A component that displays information and allows interaction. +/// +/// If you want to build your own widgets, you will need to implement this +/// trait. +/// +/// [`Widget`]: trait.Widget.html +/// [`Element`]: ../struct.Element.html +pub trait Widget: std::fmt::Debug { + /// Returns the [`Node`] of the [`Widget`]. + /// + /// This [`Node`] is used by the runtime to compute the [`Layout`] of the + /// user interface. + /// + /// [`Node`]: ../struct.Node.html + /// [`Widget`]: trait.Widget.html + /// [`Layout`]: ../struct.Layout.html + fn node(&self, renderer: &Renderer) -> Node; + + /// Draws the [`Widget`] using the associated `Renderer`. + /// + /// It must return the [`MouseCursor`] state for the [`Widget`]. + /// + /// [`Widget`]: trait.Widget.html + /// [`MouseCursor`]: ../enum.MouseCursor.html + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor; + + /// Computes the _layout_ hash of the [`Widget`]. + /// + /// The produced hash is used by the runtime to decide if the [`Layout`] + /// needs to be recomputed between frames. Therefore, to ensure maximum + /// efficiency, the hash should only be affected by the properties of the + /// [`Widget`] that can affect layouting. + /// + /// For example, the [`Text`] widget does not hash its color property, as + /// its value cannot affect the overall [`Layout`] of the user interface. + /// + /// [`Widget`]: trait.Widget.html + /// [`Layout`]: ../struct.Layout.html + /// [`Text`]: text/struct.Text.html + fn hash_layout(&self, state: &mut Hasher); + + /// Processes a runtime [`Event`]. + /// + /// It receives: + /// * an [`Event`] describing user interaction + /// * the computed [`Layout`] of the [`Widget`] + /// * the current cursor position + /// * a mutable `Message` list, allowing the [`Widget`] to produce + /// new messages based on user interaction. + /// + /// By default, it does nothing. + /// + /// [`Event`]: ../enum.Event.html + /// [`Widget`]: trait.Widget.html + /// [`Layout`]: ../struct.Layout.html + fn on_event( + &mut self, + _event: Event, + _layout: Layout<'_>, + _cursor_position: Point, + _messages: &mut Vec, + ) { + } +} diff --git a/core/src/widget/button.rs b/core/src/widget/button.rs new file mode 100644 index 00000000..abcdbfeb --- /dev/null +++ b/core/src/widget/button.rs @@ -0,0 +1,282 @@ +//! Allow your users to perform actions by pressing a button. +//! +//! A [`Button`] has some local [`State`] and a [`Class`]. +//! +//! [`Button`]: struct.Button.html +//! [`State`]: struct.State.html +//! [`Class`]: enum.Class.html + +use crate::input::{mouse, ButtonState}; +use crate::{ + Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, + Style, Widget, +}; + +use std::hash::Hash; + +/// A generic widget that produces a message when clicked. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`button::Renderer`] trait. +/// +/// [`Widget`]: ../trait.Widget.html +/// [`button::Renderer`]: trait.Renderer.html +/// +/// # Example +/// +/// ``` +/// use iced::{button, Button}; +/// +/// pub enum Message { +/// ButtonClicked, +/// } +/// +/// let state = &mut button::State::new(); +/// +/// Button::new(state, "Click me!") +/// .on_press(Message::ButtonClicked); +/// ``` +/// +/// ![Button drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/button.png?raw=true) +pub struct Button<'a, Message> { + state: &'a mut State, + label: String, + class: Class, + on_press: Option, + style: Style, +} + +impl<'a, Message> std::fmt::Debug for Button<'a, Message> +where + Message: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Button") + .field("state", &self.state) + .field("label", &self.label) + .field("class", &self.class) + .field("on_press", &self.on_press) + .field("style", &self.style) + .finish() + } +} + +impl<'a, Message> Button<'a, Message> { + /// Creates a new [`Button`] with some local [`State`] and the given label. + /// + /// The default [`Class`] of a new [`Button`] is [`Class::Primary`]. + /// + /// [`Button`]: struct.Button.html + /// [`State`]: struct.State.html + /// [`Class`]: enum.Class.html + /// [`Class::Primary`]: enum.Class.html#variant.Primary + pub fn new(state: &'a mut State, label: &str) -> Self { + Button { + state, + label: String::from(label), + class: Class::Primary, + on_press: None, + style: Style::default().min_width(100), + } + } + + /// Sets the width of the [`Button`] in pixels. + /// + /// [`Button`]: struct.Button.html + pub fn width(mut self, width: u16) -> Self { + self.style = self.style.width(width); + self + } + + /// Makes the [`Button`] fill the horizontal space of its container. + /// + /// [`Button`]: struct.Button.html + pub fn fill_width(mut self) -> Self { + self.style = self.style.fill_width(); + self + } + + /// Sets the alignment of the [`Button`] itself. + /// + /// This is useful if you want to override the default alignment given by + /// the parent container. + /// + /// [`Button`]: struct.Button.html + pub fn align_self(mut self, align: Align) -> Self { + self.style = self.style.align_self(align); + self + } + + /// Sets the [`Class`] of the [`Button`]. + /// + /// + /// [`Button`]: struct.Button.html + /// [`Class`]: enum.Class.html + pub fn class(mut self, class: Class) -> Self { + self.class = class; + self + } + + /// Sets the message that will be produced when the [`Button`] is pressed. + /// + /// [`Button`]: struct.Button.html + pub fn on_press(mut self, msg: Message) -> Self { + self.on_press = Some(msg); + self + } +} + +impl<'a, Message, Renderer> Widget for Button<'a, Message> +where + Renderer: self::Renderer, + Message: Copy + std::fmt::Debug, +{ + fn node(&self, _renderer: &Renderer) -> Node { + Node::new(self.style.height(50)) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + match event { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state, + }) => { + if let Some(on_press) = self.on_press { + let bounds = layout.bounds(); + + match state { + ButtonState::Pressed => { + self.state.is_pressed = + bounds.contains(cursor_position); + } + ButtonState::Released => { + let is_clicked = self.state.is_pressed + && bounds.contains(cursor_position); + + self.state.is_pressed = false; + + if is_clicked { + messages.push(on_press); + } + } + } + } + } + _ => {} + } + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + renderer.draw( + cursor_position, + layout.bounds(), + self.state, + &self.label, + self.class, + ) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + } +} + +/// The local state of a [`Button`]. +/// +/// [`Button`]: struct.Button.html +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct State { + is_pressed: bool, +} + +impl State { + /// Creates a new [`State`]. + /// + /// [`State`]: struct.State.html + pub fn new() -> State { + State::default() + } + + /// Returns whether the associated [`Button`] is currently being pressed or + /// not. + /// + /// [`Button`]: struct.Button.html + pub fn is_pressed(&self) -> bool { + self.is_pressed + } +} + +/// The type of a [`Button`]. +/// +/// ![Different buttons drawn by the built-in renderer in Coffee](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/button_classes.png?raw=true) +/// +/// [`Button`]: struct.Button.html +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Class { + /// The [`Button`] performs the main action. + /// + /// [`Button`]: struct.Button.html + Primary, + + /// The [`Button`] performs an alternative action. + /// + /// [`Button`]: struct.Button.html + Secondary, + + /// The [`Button`] performs a productive action. + /// + /// [`Button`]: struct.Button.html + Positive, +} + +/// The renderer of a [`Button`]. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`Button`] in your user interface. +/// +/// [`Button`]: struct.Button.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Draws a [`Button`]. + /// + /// It receives: + /// * the current cursor position + /// * the bounds of the [`Button`] + /// * the local state of the [`Button`] + /// * the label of the [`Button`] + /// * the [`Class`] of the [`Button`] + /// + /// [`Button`]: struct.Button.html + /// [`State`]: struct.State.html + /// [`Class`]: enum.Class.html + fn draw( + &mut self, + cursor_position: Point, + bounds: Rectangle, + state: &State, + label: &str, + class: Class, + ) -> MouseCursor; +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + Message: 'static + Copy + std::fmt::Debug, +{ + fn from(button: Button<'a, Message>) -> Element<'a, Message, Renderer> { + Element::new(button) + } +} diff --git a/core/src/widget/checkbox.rs b/core/src/widget/checkbox.rs new file mode 100644 index 00000000..c60807fd --- /dev/null +++ b/core/src/widget/checkbox.rs @@ -0,0 +1,203 @@ +//! Show toggle controls using checkboxes. +use std::hash::Hash; + +use crate::input::{mouse, ButtonState}; +use crate::widget::{text, Column, Row, Text}; +use crate::{ + Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, + Widget, +}; + +/// A box that can be checked, with a generic text `Color`. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`checkbox::Renderer`] trait. +/// +/// [`Widget`]: ../trait.Widget.html +/// [`checkbox::Renderer`]: trait.Renderer.html +/// +/// # Example +/// +/// ``` +/// use iced::Checkbox; +/// +/// #[derive(Debug, Clone, Copy)] +/// pub enum Color { +/// Black, +/// } +/// +/// pub enum Message { +/// CheckboxToggled(bool), +/// } +/// +/// let is_checked = true; +/// +/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled) +/// .label_color(Color::Black); +/// ``` +/// +/// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true) +pub struct Checkbox { + is_checked: bool, + on_toggle: Box Message>, + label: String, + label_color: Option, +} + +impl std::fmt::Debug for Checkbox +where + Color: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Checkbox") + .field("is_checked", &self.is_checked) + .field("label", &self.label) + .field("label_color", &self.label_color) + .finish() + } +} + +impl Checkbox { + /// Creates a new [`Checkbox`]. + /// + /// It expects: + /// * a boolean describing whether the [`Checkbox`] is checked or not + /// * the label of the [`Checkbox`] + /// * a function that will be called when the [`Checkbox`] is toggled. + /// It will receive the new state of the [`Checkbox`] and must produce + /// a `Message`. + /// + /// [`Checkbox`]: struct.Checkbox.html + pub fn new(is_checked: bool, label: &str, f: F) -> Self + where + F: 'static + Fn(bool) -> Message, + { + Checkbox { + is_checked, + on_toggle: Box::new(f), + label: String::from(label), + label_color: None, + } + } + + /// Sets the `Color` of the label of the [`Checkbox`]. + /// + /// [`Checkbox`]: struct.Checkbox.html + pub fn label_color(mut self, color: Color) -> Self { + self.label_color = Some(color); + self + } +} + +impl Widget + for Checkbox +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer + text::Renderer, +{ + fn node(&self, renderer: &Renderer) -> Node { + Row::<(), Renderer>::new() + .spacing(15) + .align_items(Align::Center) + .push(Column::new().width(28).height(28)) + .push(Text::new(&self.label)) + .node(renderer) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + match event { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state: ButtonState::Pressed, + }) => { + let mouse_over = layout + .children() + .any(|child| child.bounds().contains(cursor_position)); + + if mouse_over { + messages.push((self.on_toggle)(!self.is_checked)); + } + } + _ => {} + } + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + let children: Vec<_> = layout.children().collect(); + + let text_bounds = children[1].bounds(); + + text::Renderer::draw( + renderer, + text_bounds, + &self.label, + None, + self.label_color, + text::HorizontalAlignment::Left, + text::VerticalAlignment::Top, + ); + + self::Renderer::draw( + renderer, + cursor_position, + children[0].bounds(), + text_bounds, + self.is_checked, + ) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.label.hash(state); + } +} + +/// The renderer of a [`Checkbox`]. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`Checkbox`] in your user interface. +/// +/// [`Checkbox`]: struct.Checkbox.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Draws a [`Checkbox`]. + /// + /// It receives: + /// * the current cursor position + /// * the bounds of the [`Checkbox`] + /// * the bounds of the label of the [`Checkbox`] + /// * whether the [`Checkbox`] is checked or not + /// + /// [`Checkbox`]: struct.Checkbox.html + fn draw( + &mut self, + cursor_position: Point, + bounds: Rectangle, + label_bounds: Rectangle, + is_checked: bool, + ) -> MouseCursor; +} + +impl<'a, Color, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer + text::Renderer, + Message: 'static, +{ + fn from( + checkbox: Checkbox, + ) -> Element<'a, Message, Renderer> { + Element::new(checkbox) + } +} diff --git a/core/src/widget/column.rs b/core/src/widget/column.rs new file mode 100644 index 00000000..ff754e98 --- /dev/null +++ b/core/src/widget/column.rs @@ -0,0 +1,224 @@ +use std::hash::Hash; + +use crate::{ + Align, Element, Event, Hasher, Justify, Layout, MouseCursor, Node, Point, + Style, Widget, +}; + +/// A container that distributes its contents vertically. +/// +/// A [`Column`] will try to fill the horizontal space of its container. +/// +/// [`Column`]: struct.Column.html +#[derive(Default)] +pub struct Column<'a, Message, Renderer> { + style: Style, + spacing: u16, + children: Vec>, +} + +impl<'a, Message, Renderer> std::fmt::Debug for Column<'a, Message, Renderer> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Column") + .field("style", &self.style) + .field("spacing", &self.spacing) + .field("children", &self.children) + .finish() + } +} + +impl<'a, Message, Renderer> Column<'a, Message, Renderer> { + /// Creates an empty [`Column`]. + /// + /// [`Column`]: struct.Column.html + pub fn new() -> Self { + let mut style = Style::default().fill_width(); + style.0.flex_direction = stretch::style::FlexDirection::Column; + + Column { + style, + spacing: 0, + children: Vec::new(), + } + } + + /// Sets the vertical spacing _between_ elements in pixels. + /// + /// Custom margins per element do not exist in Iced. You should use this + /// method instead! While less flexible, it helps you keep spacing between + /// elements consistent. + pub fn spacing(mut self, px: u16) -> Self { + self.spacing = px; + self + } + + /// Sets the padding of the [`Column`] in pixels. + /// + /// [`Column`]: struct.Column.html + pub fn padding(mut self, px: u16) -> Self { + self.style = self.style.padding(px); + self + } + + /// Sets the width of the [`Column`] in pixels. + /// + /// [`Column`]: struct.Column.html + pub fn width(mut self, width: u16) -> Self { + self.style = self.style.width(width); + self + } + + /// Sets the height of the [`Column`] in pixels. + /// + /// [`Column`]: struct.Column.html + pub fn height(mut self, height: u16) -> Self { + self.style = self.style.height(height); + self + } + + /// Sets the maximum width of the [`Column`] in pixels. + /// + /// [`Column`]: struct.Column.html + pub fn max_width(mut self, max_width: u16) -> Self { + self.style = self.style.max_width(max_width); + self + } + + /// Sets the maximum height of the [`Column`] in pixels. + /// + /// [`Column`]: struct.Column.html + pub fn max_height(mut self, max_height: u16) -> Self { + self.style = self.style.max_height(max_height); + self + } + + /// Sets the alignment of the [`Column`] itself. + /// + /// This is useful if you want to override the default alignment given by + /// the parent container. + /// + /// [`Column`]: struct.Column.html + pub fn align_self(mut self, align: Align) -> Self { + self.style = self.style.align_self(align); + self + } + + /// Sets the horizontal alignment of the contents of the [`Column`] . + /// + /// [`Column`]: struct.Column.html + pub fn align_items(mut self, align: Align) -> Self { + self.style = self.style.align_items(align); + self + } + + /// Sets the vertical distribution strategy for the contents of the + /// [`Column`] . + /// + /// [`Column`]: struct.Column.html + pub fn justify_content(mut self, justify: Justify) -> Self { + self.style = self.style.justify_content(justify); + self + } + + /// Adds an [`Element`] to the [`Column`]. + /// + /// [`Element`]: ../struct.Element.html + /// [`Column`]: struct.Column.html + pub fn push(mut self, child: E) -> Column<'a, Message, Renderer> + where + E: Into>, + { + self.children.push(child.into()); + self + } +} + +impl<'a, Message, Renderer> Widget + for Column<'a, Message, Renderer> +{ + fn node(&self, renderer: &Renderer) -> Node { + let mut children: Vec = self + .children + .iter() + .map(|child| { + let mut node = child.widget.node(renderer); + + let mut style = node.0.style(); + style.margin.bottom = + stretch::style::Dimension::Points(f32::from(self.spacing)); + + node.0.set_style(style); + node + }) + .collect(); + + if let Some(node) = children.last_mut() { + let mut style = node.0.style(); + style.margin.bottom = stretch::style::Dimension::Undefined; + + node.0.set_style(style); + } + + Node::with_children(self.style, children) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + self.children.iter_mut().zip(layout.children()).for_each( + |(child, layout)| { + child + .widget + .on_event(event, layout, cursor_position, messages) + }, + ); + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + let mut cursor = MouseCursor::OutOfBounds; + + self.children.iter().zip(layout.children()).for_each( + |(child, layout)| { + let new_cursor = + child.widget.draw(renderer, layout, cursor_position); + + if new_cursor != MouseCursor::OutOfBounds { + cursor = new_cursor; + } + }, + ); + + cursor + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + self.spacing.hash(state); + + for child in &self.children { + child.widget.hash_layout(state); + } + } +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: 'a, + Message: 'static, +{ + fn from( + column: Column<'a, Message, Renderer>, + ) -> Element<'a, Message, Renderer> { + Element::new(column) + } +} diff --git a/core/src/widget/image.rs b/core/src/widget/image.rs new file mode 100644 index 00000000..d94bfea5 --- /dev/null +++ b/core/src/widget/image.rs @@ -0,0 +1,178 @@ +//! Display images in your user interface. + +use crate::{ + Align, Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, + Widget, +}; + +use std::hash::Hash; + +/// A frame that displays an image while keeping aspect ratio. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`image::Renderer`] trait. +/// +/// [`Widget`]: ../../core/trait.Widget.html +/// [`image::Renderer`]: trait.Renderer.html +/// +/// # Example +/// +/// ``` +/// use iced::Image; +/// +/// # let my_handle = String::from("some_handle"); +/// let image = Image::new(my_handle); +/// ``` +pub struct Image { + image: I, + source: Option>, + width: Option, + height: Option, + style: Style, +} + +impl std::fmt::Debug for Image { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Image") + .field("source", &self.source) + .field("width", &self.width) + .field("height", &self.height) + .field("style", &self.style) + .finish() + } +} + +impl Image { + /// Creates a new [`Image`] with given image handle. + /// + /// [`Image`]: struct.Image.html + pub fn new(image: I) -> Self { + Image { + image, + source: None, + width: None, + height: None, + style: Style::default(), + } + } + + /// Sets the portion of the [`Image`] to draw. + /// + /// [`Image`]: struct.Image.html + pub fn clip(mut self, source: Rectangle) -> Self { + self.source = Some(source); + self + } + + /// Sets the width of the [`Image`] boundaries in pixels. + /// + /// [`Image`]: struct.Image.html + pub fn width(mut self, width: u16) -> Self { + self.width = Some(width); + self + } + + /// Sets the height of the [`Image`] boundaries in pixels. + /// + /// [`Image`]: struct.Image.html + pub fn height(mut self, height: u16) -> Self { + self.height = Some(height); + self + } + + /// Sets the alignment of the [`Image`] itself. + /// + /// This is useful if you want to override the default alignment given by + /// the parent container. + /// + /// [`Image`]: struct.Image.html + pub fn align_self(mut self, align: Align) -> Self { + self.style = self.style.align_self(align); + self + } +} + +impl Widget for Image +where + Renderer: self::Renderer, + I: Clone, +{ + fn node(&self, renderer: &Renderer) -> Node { + renderer.node( + self.style, + &self.image, + self.width, + self.height, + self.source, + ) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + renderer.draw(&self.image, layout.bounds(), self.source); + + MouseCursor::OutOfBounds + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + self.width.hash(state); + self.height.hash(state); + } +} + +/// The renderer of an [`Image`]. +/// +/// Your [renderer] will need to implement this trait before being able to use +/// an [`Image`] in your user interface. +/// +/// [`Image`]: struct.Image.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Creates a [`Node`] with the given [`Style`] for the provided [`Image`] + /// and its size. + /// + /// You should probably keep the original aspect ratio, if possible. + /// + /// [`Node`]: ../../struct.Node.html + /// [`Style`]: ../../struct.Style.html + /// [`Image`]: struct.Image.html + fn node( + &self, + style: Style, + image: &I, + width: Option, + height: Option, + source: Option>, + ) -> Node; + + /// Draws an [`Image`]. + /// + /// It receives: + /// * the bounds of the [`Image`] + /// * the handle of the loaded [`Image`] + /// * the portion of the image to draw. If not specified, the entire image + /// should be drawn. + /// + /// [`Image`]: struct.Image.html + fn draw( + &mut self, + image: &I, + bounds: Rectangle, + source: Option>, + ); +} + +impl<'a, I, Message, Renderer> From> for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + I: Clone + 'a, +{ + fn from(image: Image) -> Element<'a, Message, Renderer> { + Element::new(image) + } +} diff --git a/core/src/widget/panel.rs b/core/src/widget/panel.rs new file mode 100644 index 00000000..d43d6fb6 --- /dev/null +++ b/core/src/widget/panel.rs @@ -0,0 +1,94 @@ +use std::hash::Hash; + +use crate::graphics::{Point, Rectangle}; +use crate::ui::core::{ + Event, Hasher, Layout, MouseCursor, Node, Style, Widget, +}; + +pub struct Panel<'a, Message, Renderer> { + style: Style, + content: Box + 'a>, +} + +impl<'a, Message, Renderer> Panel<'a, Message, Renderer> { + pub fn new(content: impl Widget + 'a) -> Self { + Panel { + style: Style::default().padding(20), + content: Box::new(content), + } + } + + pub fn width(mut self, width: u32) -> Self { + self.style = self.style.width(width); + self + } + + pub fn max_width(mut self, max_width: u32) -> Self { + self.style = self.style.max_width(max_width); + self + } +} + +impl<'a, Message, Renderer> Widget + for Panel<'a, Message, Renderer> +where + Renderer: self::Renderer, +{ + fn node(&self, renderer: &Renderer) -> Node { + Node::with_children(self.style, vec![self.content.node(renderer)]) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout, + cursor_position: Point, + messages: &mut Vec, + ) { + [&mut self.content] + .iter_mut() + .zip(layout.children()) + .for_each(|(child, layout)| { + child.on_event(event, layout, cursor_position, messages) + }); + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout, + cursor_position: Point, + ) -> MouseCursor { + let bounds = layout.bounds(); + let mut cursor = MouseCursor::OutOfBounds; + renderer.draw(bounds); + + [&self.content].iter().zip(layout.children()).for_each( + |(child, layout)| { + let new_cursor = child.draw(renderer, layout, cursor_position); + + if new_cursor != MouseCursor::OutOfBounds { + cursor = new_cursor; + } + }, + ); + + if cursor == MouseCursor::OutOfBounds { + if bounds.contains(cursor_position) { + MouseCursor::Idle + } else { + MouseCursor::OutOfBounds + } + } else { + cursor + } + } + + fn hash(&self, state: &mut Hasher) { + self.style.hash(state); + } +} + +pub trait Renderer { + fn draw(&mut self, bounds: Rectangle); +} diff --git a/core/src/widget/progress_bar.rs b/core/src/widget/progress_bar.rs new file mode 100644 index 00000000..d4499160 --- /dev/null +++ b/core/src/widget/progress_bar.rs @@ -0,0 +1,106 @@ +//! Provide visual feedback to your users when performing a slow task. + +use crate::{ + Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, +}; + +use std::hash::Hash; + +/// A bar that is filled based on an amount of progress. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`progress_bar::Renderer`] trait. +/// +/// [`Widget`]: ../trait.Widget.html +/// [`progress_bar::Renderer`]: trait.Renderer.html +/// +/// # Example +/// +/// ``` +/// use iced::ProgressBar; +/// +/// let progress = 0.75; +/// +/// ProgressBar::new(progress); +/// ``` +#[derive(Debug)] +pub struct ProgressBar { + progress: f32, + style: Style, +} + +impl ProgressBar { + /// Creates a new [`ProgressBar`] filled based on the given amount of + /// progress. + /// + /// The progress should be in the `0.0..=1.0` range. `0` meaning no work + /// done, and `1` meaning work finished. + /// + /// [`ProgressBar`]: struct.ProgressBar.html + pub fn new(progress: f32) -> Self { + ProgressBar { + progress, + style: Style::default().fill_width(), + } + } + + /// Sets the width of the [`ProgressBar`] in pixels. + /// + /// [`ProgressBar`]: struct.ProgressBar.html + pub fn width(mut self, width: u16) -> Self { + self.style = self.style.width(width); + self + } +} + +impl Widget for ProgressBar +where + Renderer: self::Renderer, +{ + fn node(&self, _renderer: &Renderer) -> Node { + Node::new(self.style.height(50)) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + renderer.draw(layout.bounds(), self.progress); + + MouseCursor::OutOfBounds + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + } +} + +/// The renderer of a [`ProgressBar`]. +/// +/// Your [renderer] will need to implement this trait before being able to use +/// a [`ProgressBar`] in your user interface. +/// +/// [`ProgressBar`]: struct.ProgressBar.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Draws a [`ProgressBar`]. + /// + /// It receives: + /// * the bounds of the [`ProgressBar`] + /// * the current progress of the [`ProgressBar`], in the `0.0..=1.0` + /// range. + /// + /// [`ProgressBar`]: struct.ProgressBar.html + fn draw(&mut self, bounds: Rectangle, progress: f32); +} + +impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, +{ + fn from(progress_bar: ProgressBar) -> Element<'a, Message, Renderer> { + Element::new(progress_bar) + } +} diff --git a/core/src/widget/radio.rs b/core/src/widget/radio.rs new file mode 100644 index 00000000..28353ef4 --- /dev/null +++ b/core/src/widget/radio.rs @@ -0,0 +1,211 @@ +//! Create choices using radio buttons. +use crate::input::{mouse, ButtonState}; +use crate::widget::{text, Column, Row, Text}; +use crate::{ + Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, + Widget, +}; + +use std::hash::Hash; + +/// A circular button representing a choice, with a generic text `Color`. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`radio::Renderer`] trait. +/// +/// [`Widget`]: ../trait.Widget.html +/// [`radio::Renderer`]: trait.Renderer.html +/// +/// # Example +/// ``` +/// use iced::{Column, Radio}; +/// +/// #[derive(Debug, Clone, Copy)] +/// pub enum Color { +/// Black, +/// } +/// +/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] +/// pub enum Choice { +/// A, +/// B, +/// } +/// +/// #[derive(Debug, Clone, Copy)] +/// pub enum Message { +/// RadioSelected(Choice), +/// } +/// +/// let selected_choice = Some(Choice::A); +/// +/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected) +/// .label_color(Color::Black); +/// +/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected) +/// .label_color(Color::Black); +/// ``` +/// +/// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true) +pub struct Radio { + is_selected: bool, + on_click: Message, + label: String, + label_color: Option, +} + +impl std::fmt::Debug for Radio +where + Color: std::fmt::Debug, + Message: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Radio") + .field("is_selected", &self.is_selected) + .field("on_click", &self.on_click) + .field("label", &self.label) + .field("label_color", &self.label_color) + .finish() + } +} + +impl Radio { + /// Creates a new [`Radio`] button. + /// + /// It expects: + /// * the value related to the [`Radio`] button + /// * the label of the [`Radio`] button + /// * the current selected value + /// * a function that will be called when the [`Radio`] is selected. It + /// receives the value of the radio and must produce a `Message`. + /// + /// [`Radio`]: struct.Radio.html + pub fn new(value: V, label: &str, selected: Option, f: F) -> Self + where + V: Eq + Copy, + F: 'static + Fn(V) -> Message, + { + Radio { + is_selected: Some(value) == selected, + on_click: f(value), + label: String::from(label), + label_color: None, + } + } + + /// Sets the `Color` of the label of the [`Radio`]. + /// + /// [`Radio`]: struct.Radio.html + pub fn label_color(mut self, color: Color) -> Self { + self.label_color = Some(color); + self + } +} + +impl Widget + for Radio +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer + text::Renderer, + Message: Copy + std::fmt::Debug, +{ + fn node(&self, renderer: &Renderer) -> Node { + Row::<(), Renderer>::new() + .spacing(15) + .align_items(Align::Center) + .push(Column::new().width(28).height(28)) + .push(Text::new(&self.label)) + .node(renderer) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + match event { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state: ButtonState::Pressed, + }) => { + if layout.bounds().contains(cursor_position) { + messages.push(self.on_click); + } + } + _ => {} + } + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + let children: Vec<_> = layout.children().collect(); + + let mut text_bounds = children[1].bounds(); + text_bounds.y -= 2.0; + + text::Renderer::draw( + renderer, + text_bounds, + &self.label, + None, + self.label_color, + text::HorizontalAlignment::Left, + text::VerticalAlignment::Top, + ); + + self::Renderer::draw( + renderer, + cursor_position, + children[0].bounds(), + layout.bounds(), + self.is_selected, + ) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.label.hash(state); + } +} + +/// The renderer of a [`Radio`] button. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`Radio`] button in your user interface. +/// +/// [`Radio`]: struct.Radio.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Draws a [`Radio`] button. + /// + /// It receives: + /// * the current cursor position + /// * the bounds of the [`Radio`] + /// * the bounds of the label of the [`Radio`] + /// * whether the [`Radio`] is selected or not + /// + /// [`Radio`]: struct.Radio.html + fn draw( + &mut self, + cursor_position: Point, + bounds: Rectangle, + label_bounds: Rectangle, + is_selected: bool, + ) -> MouseCursor; +} + +impl<'a, Color, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer + text::Renderer, + Message: 'static + Copy + std::fmt::Debug, +{ + fn from(checkbox: Radio) -> Element<'a, Message, Renderer> { + Element::new(checkbox) + } +} diff --git a/core/src/widget/row.rs b/core/src/widget/row.rs new file mode 100644 index 00000000..959528dc --- /dev/null +++ b/core/src/widget/row.rs @@ -0,0 +1,219 @@ +use std::hash::Hash; + +use crate::{ + Align, Element, Event, Hasher, Justify, Layout, MouseCursor, Node, Point, + Style, Widget, +}; + +/// A container that distributes its contents horizontally. +/// +/// A [`Row`] will try to fill the horizontal space of its container. +/// +/// [`Row`]: struct.Row.html +#[derive(Default)] +pub struct Row<'a, Message, Renderer> { + style: Style, + spacing: u16, + children: Vec>, +} + +impl<'a, Message, Renderer> std::fmt::Debug for Row<'a, Message, Renderer> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Row") + .field("style", &self.style) + .field("spacing", &self.spacing) + .field("children", &self.children) + .finish() + } +} + +impl<'a, Message, Renderer> Row<'a, Message, Renderer> { + /// Creates an empty [`Row`]. + /// + /// [`Row`]: struct.Row.html + pub fn new() -> Self { + Row { + style: Style::default().fill_width(), + spacing: 0, + children: Vec::new(), + } + } + + /// Sets the horizontal spacing _between_ elements in pixels. + /// + /// Custom margins per element do not exist in Iced. You should use this + /// method instead! While less flexible, it helps you keep spacing between + /// elements consistent. + pub fn spacing(mut self, px: u16) -> Self { + self.spacing = px; + self + } + + /// Sets the padding of the [`Row`] in pixels. + /// + /// [`Row`]: struct.Row.html + pub fn padding(mut self, px: u16) -> Self { + self.style = self.style.padding(px); + self + } + + /// Sets the width of the [`Row`] in pixels. + /// + /// [`Row`]: struct.Row.html + pub fn width(mut self, width: u16) -> Self { + self.style = self.style.width(width); + self + } + + /// Sets the height of the [`Row`] in pixels. + /// + /// [`Row`]: struct.Row.html + pub fn height(mut self, height: u16) -> Self { + self.style = self.style.height(height); + self + } + + /// Sets the maximum width of the [`Row`] in pixels. + /// + /// [`Row`]: struct.Row.html + pub fn max_width(mut self, max_width: u16) -> Self { + self.style = self.style.max_width(max_width); + self + } + + /// Sets the maximum height of the [`Row`] in pixels. + /// + /// [`Row`]: struct.Row.html + pub fn max_height(mut self, max_height: u16) -> Self { + self.style = self.style.max_height(max_height); + self + } + + /// Sets the alignment of the [`Row`] itself. + /// + /// This is useful if you want to override the default alignment given by + /// the parent container. + /// + /// [`Row`]: struct.Row.html + pub fn align_self(mut self, align: Align) -> Self { + self.style = self.style.align_self(align); + self + } + + /// Sets the vertical alignment of the contents of the [`Row`] . + /// + /// [`Row`]: struct.Row.html + pub fn align_items(mut self, align: Align) -> Self { + self.style = self.style.align_items(align); + self + } + + /// Sets the horizontal distribution strategy for the contents of the + /// [`Row`] . + /// + /// [`Row`]: struct.Row.html + pub fn justify_content(mut self, justify: Justify) -> Self { + self.style = self.style.justify_content(justify); + self + } + + /// Adds an [`Element`] to the [`Row`]. + /// + /// [`Element`]: ../struct.Element.html + /// [`Row`]: struct.Row.html + pub fn push(mut self, child: E) -> Row<'a, Message, Renderer> + where + E: Into>, + { + self.children.push(child.into()); + self + } +} + +impl<'a, Message, Renderer> Widget + for Row<'a, Message, Renderer> +{ + fn node(&self, renderer: &Renderer) -> Node { + let mut children: Vec = self + .children + .iter() + .map(|child| { + let mut node = child.widget.node(renderer); + + let mut style = node.0.style(); + style.margin.end = + stretch::style::Dimension::Points(f32::from(self.spacing)); + + node.0.set_style(style); + node + }) + .collect(); + + if let Some(node) = children.last_mut() { + let mut style = node.0.style(); + style.margin.end = stretch::style::Dimension::Undefined; + + node.0.set_style(style); + } + + Node::with_children(self.style, children) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + self.children.iter_mut().zip(layout.children()).for_each( + |(child, layout)| { + child + .widget + .on_event(event, layout, cursor_position, messages) + }, + ); + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + let mut cursor = MouseCursor::OutOfBounds; + + self.children.iter().zip(layout.children()).for_each( + |(child, layout)| { + let new_cursor = + child.widget.draw(renderer, layout, cursor_position); + + if new_cursor != MouseCursor::OutOfBounds { + cursor = new_cursor; + } + }, + ); + + cursor + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + self.spacing.hash(state); + + for child in &self.children { + child.widget.hash_layout(state); + } + } +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: 'a, + Message: 'static, +{ + fn from(row: Row<'a, Message, Renderer>) -> Element<'a, Message, Renderer> { + Element::new(row) + } +} diff --git a/core/src/widget/slider.rs b/core/src/widget/slider.rs new file mode 100644 index 00000000..cdec9ec4 --- /dev/null +++ b/core/src/widget/slider.rs @@ -0,0 +1,241 @@ +//! Display an interactive selector of a single value from a range of values. +//! +//! A [`Slider`] has some local [`State`]. +//! +//! [`Slider`]: struct.Slider.html +//! [`State`]: struct.State.html +use std::hash::Hash; +use std::ops::RangeInclusive; + +use crate::input::{mouse, ButtonState}; +use crate::{ + Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, + Widget, +}; + +/// An horizontal bar and a handle that selects a single value from a range of +/// values. +/// +/// A [`Slider`] will try to fill the horizontal space of its container. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`slider::Renderer`] trait. +/// +/// [`Slider`]: struct.Slider.html +/// [`Widget`]: ../trait.Widget.html +/// [`slider::Renderer`]: trait.Renderer.html +/// +/// # Example +/// ``` +/// use iced::{slider, Slider}; +/// +/// pub enum Message { +/// SliderChanged(f32), +/// } +/// +/// let state = &mut slider::State::new(); +/// let value = 50.0; +/// +/// Slider::new(state, 0.0..=100.0, value, Message::SliderChanged); +/// ``` +/// +/// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true) +pub struct Slider<'a, Message> { + state: &'a mut State, + range: RangeInclusive, + value: f32, + on_change: Box Message>, + style: Style, +} + +impl<'a, Message> std::fmt::Debug for Slider<'a, Message> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Slider") + .field("state", &self.state) + .field("range", &self.range) + .field("value", &self.value) + .field("style", &self.style) + .finish() + } +} + +impl<'a, Message> Slider<'a, Message> { + /// Creates a new [`Slider`]. + /// + /// It expects: + /// * the local [`State`] of the [`Slider`] + /// * an inclusive range of possible values + /// * the current value of the [`Slider`] + /// * a function that will be called when the [`Slider`] is dragged. + /// It receives the new value of the [`Slider`] and must produce a + /// `Message`. + /// + /// [`Slider`]: struct.Slider.html + /// [`State`]: struct.State.html + pub fn new( + state: &'a mut State, + range: RangeInclusive, + value: f32, + on_change: F, + ) -> Self + where + F: 'static + Fn(f32) -> Message, + { + Slider { + state, + value: value.max(*range.start()).min(*range.end()), + range, + on_change: Box::new(on_change), + style: Style::default().min_width(100).fill_width(), + } + } + + /// Sets the width of the [`Slider`] in pixels. + /// + /// [`Slider`]: struct.Slider.html + pub fn width(mut self, width: u16) -> Self { + self.style = self.style.width(width); + self + } +} + +impl<'a, Message, Renderer> Widget for Slider<'a, Message> +where + Renderer: self::Renderer, +{ + fn node(&self, _renderer: &Renderer) -> Node { + Node::new(self.style.height(25)) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + let mut change = || { + let bounds = layout.bounds(); + + if cursor_position.x <= bounds.x { + messages.push((self.on_change)(*self.range.start())); + } else if cursor_position.x >= bounds.x + bounds.width { + messages.push((self.on_change)(*self.range.end())); + } else { + let percent = (cursor_position.x - bounds.x) / bounds.width; + let value = (self.range.end() - self.range.start()) * percent + + self.range.start(); + + messages.push((self.on_change)(value)); + } + }; + + match event { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state, + }) => match state { + ButtonState::Pressed => { + if layout.bounds().contains(cursor_position) { + change(); + self.state.is_dragging = true; + } + } + ButtonState::Released => { + self.state.is_dragging = false; + } + }, + Event::Mouse(mouse::Event::CursorMoved { .. }) => { + if self.state.is_dragging { + change(); + } + } + _ => {} + } + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + renderer.draw( + cursor_position, + layout.bounds(), + self.state, + self.range.clone(), + self.value, + ) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + } +} + +/// The local state of a [`Slider`]. +/// +/// [`Slider`]: struct.Slider.html +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct State { + is_dragging: bool, +} + +impl State { + /// Creates a new [`State`]. + /// + /// [`State`]: struct.State.html + pub fn new() -> State { + State::default() + } + + /// Returns whether the associated [`Slider`] is currently being dragged or + /// not. + /// + /// [`Slider`]: struct.Slider.html + pub fn is_dragging(&self) -> bool { + self.is_dragging + } +} + +/// The renderer of a [`Slider`]. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`Slider`] in your user interface. +/// +/// [`Slider`]: struct.Slider.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Draws a [`Slider`]. + /// + /// It receives: + /// * the current cursor position + /// * the bounds of the [`Slider`] + /// * the local state of the [`Slider`] + /// * the range of values of the [`Slider`] + /// * the current value of the [`Slider`] + /// + /// [`Slider`]: struct.Slider.html + /// [`State`]: struct.State.html + /// [`Class`]: enum.Class.html + fn draw( + &mut self, + cursor_position: Point, + bounds: Rectangle, + state: &State, + range: RangeInclusive, + value: f32, + ) -> MouseCursor; +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + Message: 'static, +{ + fn from(slider: Slider<'a, Message>) -> Element<'a, Message, Renderer> { + Element::new(slider) + } +} diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs new file mode 100644 index 00000000..59b599bb --- /dev/null +++ b/core/src/widget/text.rs @@ -0,0 +1,224 @@ +//! Write some text for your users to read. +use crate::{ + Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, +}; + +use std::hash::Hash; + +/// A fragment of text with a generic `Color`. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`text::Renderer`] trait. +/// +/// [`Widget`]: ../trait.Widget.html +/// [`text::Renderer`]: trait.Renderer.html +/// +/// # Example +/// +/// ``` +/// use iced::Text; +/// +/// #[derive(Debug, Clone, Copy)] +/// pub enum Color { +/// Black, +/// } +/// +/// Text::new("I <3 iced!") +/// .size(40) +/// .color(Color::Black); +/// ``` +#[derive(Debug, Clone)] +pub struct Text { + content: String, + size: Option, + color: Option, + style: Style, + horizontal_alignment: HorizontalAlignment, + vertical_alignment: VerticalAlignment, +} + +impl Text { + /// Create a new fragment of [`Text`] with the given contents. + /// + /// [`Text`]: struct.Text.html + pub fn new(label: &str) -> Self { + Text { + content: String::from(label), + size: None, + color: None, + style: Style::default().fill_width(), + horizontal_alignment: HorizontalAlignment::Left, + vertical_alignment: VerticalAlignment::Top, + } + } + + /// Sets the size of the [`Text`] in pixels. + /// + /// [`Text`]: struct.Text.html + pub fn size(mut self, size: u16) -> Self { + self.size = Some(size); + self + } + + /// Sets the `Color` of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + pub fn color(mut self, color: Color) -> Self { + self.color = Some(color); + self + } + + /// Sets the width of the [`Text`] boundaries in pixels. + /// + /// [`Text`]: struct.Text.html + pub fn width(mut self, width: u16) -> Self { + self.style = self.style.width(width); + self + } + + /// Sets the height of the [`Text`] boundaries in pixels. + /// + /// [`Text`]: struct.Text.html + pub fn height(mut self, height: u16) -> Self { + self.style = self.style.height(height); + self + } + + /// Sets the [`HorizontalAlignment`] of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html + pub fn horizontal_alignment( + mut self, + alignment: HorizontalAlignment, + ) -> Self { + self.horizontal_alignment = alignment; + self + } + + /// Sets the [`VerticalAlignment`] of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + /// [`VerticalAlignment`]: enum.VerticalAlignment.html + pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self { + self.vertical_alignment = alignment; + self + } +} + +impl Widget for Text +where + Color: Copy + std::fmt::Debug, + Renderer: self::Renderer, +{ + fn node(&self, renderer: &Renderer) -> Node { + renderer.node(self.style, &self.content, self.size) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + renderer.draw( + layout.bounds(), + &self.content, + self.size, + self.color, + self.horizontal_alignment, + self.vertical_alignment, + ); + + MouseCursor::OutOfBounds + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + + self.content.hash(state); + self.size.hash(state); + } +} + +/// The renderer of a [`Text`] fragment with a generic `Color`. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use [`Text`] in your [`UserInterface`]. +/// +/// [`Text`]: struct.Text.html +/// [renderer]: ../../renderer/index.html +/// [`UserInterface`]: ../../struct.UserInterface.html +pub trait Renderer { + /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] + /// contents and size. + /// + /// You should probably use [`Node::with_measure`] to allow [`Text`] to + /// adapt to the dimensions of its container. + /// + /// [`Node`]: ../../struct.Node.html + /// [`Style`]: ../../struct.Style.html + /// [`Text`]: struct.Text.html + /// [`Node::with_measure`]: ../../struct.Node.html#method.with_measure + fn node(&self, style: Style, content: &str, size: Option) -> Node; + + /// Draws a [`Text`] fragment. + /// + /// It receives: + /// * the bounds of the [`Text`] + /// * the contents of the [`Text`] + /// * the size of the [`Text`] + /// * the color of the [`Text`] + /// * the [`HorizontalAlignment`] of the [`Text`] + /// * the [`VerticalAlignment`] of the [`Text`] + /// + /// [`Text`]: struct.Text.html + /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html + /// [`VerticalAlignment`]: enum.VerticalAlignment.html + fn draw( + &mut self, + bounds: Rectangle, + content: &str, + size: Option, + color: Option, + horizontal_alignment: HorizontalAlignment, + vertical_alignment: VerticalAlignment, + ); +} + +impl<'a, Message, Renderer, Color> From> + for Element<'a, Message, Renderer> +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer, +{ + fn from(text: Text) -> Element<'a, Message, Renderer> { + Element::new(text) + } +} + +/// The horizontal alignment of some resource. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum HorizontalAlignment { + /// Align left + Left, + + /// Horizontally centered + Center, + + /// Align right + Right, +} + +/// The vertical alignment of some resource. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum VerticalAlignment { + /// Align top + Top, + + /// Vertically centered + Center, + + /// Align bottom + Bottom, +} diff --git a/examples/Cargo.toml b/examples/Cargo.toml new file mode 100644 index 00000000..fdf854cd --- /dev/null +++ b/examples/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "iced_examples" +version = "0.0.0" +authors = ["Héctor Ramón Jiménez "] +publish = false +edition = "2018" + +[[bin]] +name = "tour" +path = "tour/main.rs" + +[dependencies] +iced = { version = "0.1.0-alpha", path = "../core" } + +# A personal `ggez` fork that introduces a `FontCache` type to measure text +# efficiently and fixes HiDPI issues. +ggez = { version = "0.5", git = "https://github.com/hecrj/ggez.git" } diff --git a/examples/tour/main.rs b/examples/tour/main.rs index 571bc2e2..ce477fd9 100644 --- a/examples/tour/main.rs +++ b/examples/tour/main.rs @@ -27,7 +27,7 @@ pub fn main() -> ggez::GameResult { filesystem::mount( context, std::path::Path::new(&format!( - "{}/examples/resources", + "{}/resources", env!("CARGO_MANIFEST_DIR") )), true, diff --git a/src/element.rs b/src/element.rs deleted file mode 100644 index 70d06f42..00000000 --- a/src/element.rs +++ /dev/null @@ -1,370 +0,0 @@ -use stretch::{geometry, result}; - -use crate::{ - renderer, Event, Hasher, Layout, MouseCursor, Node, Point, Widget, -}; - -/// A generic [`Widget`]. -/// -/// It is useful to build composable user interfaces that do not leak -/// implementation details in their __view logic__. -/// -/// If you have a [built-in widget], you should be able to use `Into` -/// to turn it into an [`Element`]. -/// -/// [built-in widget]: widget/index.html#built-in-widgets -/// [`Widget`]: widget/trait.Widget.html -/// [`Element`]: struct.Element.html -pub struct Element<'a, Message, Renderer> { - pub(crate) widget: Box + 'a>, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Element<'a, Message, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Element") - .field("widget", &self.widget) - .finish() - } -} - -impl<'a, Message, Renderer> Element<'a, Message, Renderer> { - /// Create a new [`Element`] containing the given [`Widget`]. - /// - /// [`Element`]: struct.Element.html - /// [`Widget`]: widget/trait.Widget.html - pub fn new( - widget: impl Widget + 'a, - ) -> Element<'a, Message, Renderer> { - Element { - widget: Box::new(widget), - } - } - - /// Applies a transformation to the produced message of the [`Element`]. - /// - /// This method is useful when you want to decouple different parts of your - /// UI and make them __composable__. - /// - /// [`Element`]: struct.Element.html - /// - /// # Example - /// Imagine we want to use [our counter](index.html#usage). But instead of - /// showing a single counter, we want to display many of them. We can reuse - /// the `Counter` type as it is! - /// - /// We use composition to model the __state__ of our new application: - /// - /// ``` - /// # mod counter { - /// # pub struct Counter; - /// # } - /// use counter::Counter; - /// - /// struct ManyCounters { - /// counters: Vec, - /// } - /// ``` - /// - /// We can store the state of multiple counters now. However, the - /// __messages__ we implemented before describe the user interactions - /// of a __single__ counter. Right now, we need to also identify which - /// counter is receiving user interactions. Can we use composition again? - /// Yes. - /// - /// ``` - /// # mod counter { - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message {} - /// # } - /// #[derive(Debug, Clone, Copy)] - /// pub enum Message { - /// Counter(usize, counter::Message) - /// } - /// ``` - /// - /// We compose the previous __messages__ with the index of the counter - /// producing them. Let's implement our __view logic__ now: - /// - /// ``` - /// # mod counter { - /// # use iced::{button, Button}; - /// # - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message {} - /// # pub struct Counter(button::State); - /// # - /// # impl Counter { - /// # pub fn view(&mut self) -> Button { - /// # Button::new(&mut self.0, "_") - /// # } - /// # } - /// # } - /// # - /// # mod iced_wgpu { - /// # use iced::{ - /// # button, MouseCursor, Node, Point, Rectangle, Style, - /// # }; - /// # pub struct Renderer; - /// # - /// # impl button::Renderer for Renderer { - /// # fn draw( - /// # &mut self, - /// # _cursor_position: Point, - /// # _bounds: Rectangle, - /// # _state: &button::State, - /// # _label: &str, - /// # _class: button::Class, - /// # ) -> MouseCursor { - /// # MouseCursor::OutOfBounds - /// # } - /// # } - /// # } - /// # - /// # use counter::Counter; - /// # - /// # struct ManyCounters { - /// # counters: Vec, - /// # } - /// # - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message { - /// # Counter(usize, counter::Message) - /// # } - /// use iced::{Element, Row}; - /// use iced_wgpu::Renderer; - /// - /// impl ManyCounters { - /// pub fn view(&mut self) -> Row { - /// // We can quickly populate a `Row` by folding over our counters - /// self.counters.iter_mut().enumerate().fold( - /// Row::new().spacing(20), - /// |row, (index, counter)| { - /// // We display the counter - /// let element: Element = - /// counter.view().into(); - /// - /// row.push( - /// // Here we turn our `Element` into - /// // an `Element` by combining the `index` and the - /// // message of the `element`. - /// element.map(move |message| Message::Counter(index, message)) - /// ) - /// } - /// ) - /// } - /// } - /// ``` - /// - /// Finally, our __update logic__ is pretty straightforward: simple - /// delegation. - /// - /// ``` - /// # mod counter { - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message {} - /// # pub struct Counter; - /// # - /// # impl Counter { - /// # pub fn update(&mut self, _message: Message) {} - /// # } - /// # } - /// # - /// # use counter::Counter; - /// # - /// # struct ManyCounters { - /// # counters: Vec, - /// # } - /// # - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message { - /// # Counter(usize, counter::Message) - /// # } - /// impl ManyCounters { - /// pub fn update(&mut self, message: Message) { - /// match message { - /// Message::Counter(index, counter_msg) => { - /// if let Some(counter) = self.counters.get_mut(index) { - /// counter.update(counter_msg); - /// } - /// } - /// } - /// } - /// } - /// ``` - pub fn map(self, f: F) -> Element<'a, B, Renderer> - where - Message: 'static + Copy, - Renderer: 'a, - B: 'static, - F: 'static + Fn(Message) -> B, - { - Element { - widget: Box::new(Map::new(self.widget, f)), - } - } - - /// Marks the [`Element`] as _to-be-explained_. - /// - /// The [`Renderer`] will explain the layout of the [`Element`] graphically. - /// This can be very useful for debugging your layout! - /// - /// [`Element`]: struct.Element.html - /// [`Renderer`]: trait.Renderer.html - pub fn explain( - self, - color: Renderer::Color, - ) -> Element<'a, Message, Renderer> - where - Message: 'static, - Renderer: 'a + renderer::Debugger, - { - Element { - widget: Box::new(Explain::new(self, color)), - } - } - - pub(crate) fn compute_layout(&self, renderer: &Renderer) -> result::Layout { - let node = self.widget.node(renderer); - - node.0.compute_layout(geometry::Size::undefined()).unwrap() - } - - pub(crate) fn hash_layout(&self, state: &mut Hasher) { - self.widget.hash_layout(state); - } -} - -struct Map<'a, A, B, Renderer> { - widget: Box + 'a>, - mapper: Box B>, -} - -impl<'a, A, B, Renderer> std::fmt::Debug for Map<'a, A, B, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Map").field("widget", &self.widget).finish() - } -} - -impl<'a, A, B, Renderer> Map<'a, A, B, Renderer> { - pub fn new( - widget: Box + 'a>, - mapper: F, - ) -> Map<'a, A, B, Renderer> - where - F: 'static + Fn(A) -> B, - { - Map { - widget, - mapper: Box::new(mapper), - } - } -} - -impl<'a, A, B, Renderer> Widget for Map<'a, A, B, Renderer> -where - A: Copy, -{ - fn node(&self, renderer: &Renderer) -> Node { - self.widget.node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - let mut original_messages = Vec::new(); - - self.widget.on_event( - event, - layout, - cursor_position, - &mut original_messages, - ); - - original_messages - .iter() - .cloned() - .for_each(|message| messages.push((self.mapper)(message))); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - self.widget.draw(renderer, layout, cursor_position) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.widget.hash_layout(state); - } -} - -struct Explain<'a, Message, Renderer: renderer::Debugger> { - element: Element<'a, Message, Renderer>, - color: Renderer::Color, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Explain<'a, Message, Renderer> -where - Renderer: renderer::Debugger, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Explain") - .field("element", &self.element) - .finish() - } -} - -impl<'a, Message, Renderer> Explain<'a, Message, Renderer> -where - Renderer: renderer::Debugger, -{ - fn new( - element: Element<'a, Message, Renderer>, - color: Renderer::Color, - ) -> Self { - Explain { element, color } - } -} - -impl<'a, Message, Renderer> Widget - for Explain<'a, Message, Renderer> -where - Renderer: renderer::Debugger, -{ - fn node(&self, renderer: &Renderer) -> Node { - self.element.widget.node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - self.element - .widget - .on_event(event, layout, cursor_position, messages) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - renderer.explain(&layout, self.color); - - self.element.widget.draw(renderer, layout, cursor_position) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.element.widget.hash_layout(state); - } -} diff --git a/src/event.rs b/src/event.rs deleted file mode 100644 index 71f06006..00000000 --- a/src/event.rs +++ /dev/null @@ -1,16 +0,0 @@ -use crate::input::{keyboard, mouse}; - -/// A user interface event. -/// -/// _**Note:** This type is largely incomplete! If you need to track -/// additional events, feel free to [open an issue] and share your use case!_ -/// -/// [open an issue]: https://github.com/hecrj/iced/issues -#[derive(PartialEq, Clone, Copy, Debug)] -pub enum Event { - /// A keyboard event - Keyboard(keyboard::Event), - - /// A mouse event - Mouse(mouse::Event), -} diff --git a/src/hasher.rs b/src/hasher.rs deleted file mode 100644 index 9f6aacce..00000000 --- a/src/hasher.rs +++ /dev/null @@ -1,19 +0,0 @@ -/// The hasher used to compare layouts. -#[derive(Debug)] -pub struct Hasher(twox_hash::XxHash64); - -impl Default for Hasher { - fn default() -> Self { - Hasher(twox_hash::XxHash64::default()) - } -} - -impl core::hash::Hasher for Hasher { - fn write(&mut self, bytes: &[u8]) { - self.0.write(bytes) - } - - fn finish(&self) -> u64 { - self.0.finish() - } -} diff --git a/src/input.rs b/src/input.rs deleted file mode 100644 index 097fa730..00000000 --- a/src/input.rs +++ /dev/null @@ -1,7 +0,0 @@ -//! Map your system events into input events that the runtime can understand. -pub mod keyboard; -pub mod mouse; - -mod button_state; - -pub use button_state::ButtonState; diff --git a/src/input/button_state.rs b/src/input/button_state.rs deleted file mode 100644 index e9dc05d7..00000000 --- a/src/input/button_state.rs +++ /dev/null @@ -1,24 +0,0 @@ -/// The state of a button. -/// -/// If you are using [`winit`], consider enabling the `winit` feature to get -/// conversion implementations for free! -/// -/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ -#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] -pub enum ButtonState { - /// The button is pressed. - Pressed, - - /// The button is __not__ pressed. - Released, -} - -#[cfg(feature = "winit")] -impl From for ButtonState { - fn from(element_state: winit::event::ElementState) -> Self { - match element_state { - winit::event::ElementState::Pressed => ButtonState::Pressed, - winit::event::ElementState::Released => ButtonState::Released, - } - } -} diff --git a/src/input/keyboard.rs b/src/input/keyboard.rs deleted file mode 100644 index 57c24484..00000000 --- a/src/input/keyboard.rs +++ /dev/null @@ -1,6 +0,0 @@ -//! Build keyboard events. -mod event; -mod key_code; - -pub use event::Event; -pub use key_code::KeyCode; diff --git a/src/input/keyboard/event.rs b/src/input/keyboard/event.rs deleted file mode 100644 index 8118f112..00000000 --- a/src/input/keyboard/event.rs +++ /dev/null @@ -1,23 +0,0 @@ -use super::KeyCode; -use crate::input::ButtonState; - -#[derive(Debug, Clone, Copy, PartialEq)] -/// A keyboard event. -/// -/// _**Note:** This type is largely incomplete! If you need to track -/// additional events, feel free to [open an issue] and share your use case!_ -/// -/// [open an issue]: https://github.com/hecrj/iced/issues -pub enum Event { - /// A keyboard key was pressed or released. - Input { - /// The state of the key - state: ButtonState, - - /// The key identifier - key_code: KeyCode, - }, - - /// A unicode character was received. - CharacterReceived(char), -} diff --git a/src/input/keyboard/key_code.rs b/src/input/keyboard/key_code.rs deleted file mode 100644 index 207ddeac..00000000 --- a/src/input/keyboard/key_code.rs +++ /dev/null @@ -1,374 +0,0 @@ -/// The symbolic name of a keyboard key. -/// -/// This is mostly the `KeyCode` type found in [`winit`]. -/// -/// If you are using [`winit`], consider enabling the `winit` feature to get -/// conversion implementations for free! -/// -/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ -#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] -#[repr(u32)] -#[allow(missing_docs)] -pub enum KeyCode { - /// The '1' key over the letters. - Key1, - /// The '2' key over the letters. - Key2, - /// The '3' key over the letters. - Key3, - /// The '4' key over the letters. - Key4, - /// The '5' key over the letters. - Key5, - /// The '6' key over the letters. - Key6, - /// The '7' key over the letters. - Key7, - /// The '8' key over the letters. - Key8, - /// The '9' key over the letters. - Key9, - /// The '0' key over the 'O' and 'P' keys. - Key0, - - A, - B, - C, - D, - E, - F, - G, - H, - I, - J, - K, - L, - M, - N, - O, - P, - Q, - R, - S, - T, - U, - V, - W, - X, - Y, - Z, - - /// The Escape key, next to F1 - Escape, - - F1, - F2, - F3, - F4, - F5, - F6, - F7, - F8, - F9, - F10, - F11, - F12, - F13, - F14, - F15, - F16, - F17, - F18, - F19, - F20, - F21, - F22, - F23, - F24, - - /// Print Screen/SysRq - Snapshot, - /// Scroll Lock - Scroll, - /// Pause/Break key, next to Scroll lock - Pause, - - /// `Insert`, next to Backspace - Insert, - Home, - Delete, - End, - PageDown, - PageUp, - - Left, - Up, - Right, - Down, - - Backspace, - Enter, - Space, - - /// The "Compose" key on Linux - Compose, - - Caret, - - Numlock, - Numpad0, - Numpad1, - Numpad2, - Numpad3, - Numpad4, - Numpad5, - Numpad6, - Numpad7, - Numpad8, - Numpad9, - - AbntC1, - AbntC2, - Add, - Apostrophe, - Apps, - At, - Ax, - Backslash, - Calculator, - Capital, - Colon, - Comma, - Convert, - Decimal, - Divide, - Equals, - Grave, - Kana, - Kanji, - LAlt, - LBracket, - LControl, - LShift, - LWin, - Mail, - MediaSelect, - MediaStop, - Minus, - Multiply, - Mute, - MyComputer, - NavigateForward, // also called "Prior" - NavigateBackward, // also called "Next" - NextTrack, - NoConvert, - NumpadComma, - NumpadEnter, - NumpadEquals, - OEM102, - Period, - PlayPause, - Power, - PrevTrack, - RAlt, - RBracket, - RControl, - RShift, - RWin, - Semicolon, - Slash, - Sleep, - Stop, - Subtract, - Sysrq, - Tab, - Underline, - Unlabeled, - VolumeDown, - VolumeUp, - Wake, - WebBack, - WebFavorites, - WebForward, - WebHome, - WebRefresh, - WebSearch, - WebStop, - Yen, - Copy, - Paste, - Cut, -} - -#[cfg(feature = "winit")] -impl From for KeyCode { - fn from(virtual_keycode: winit::event::VirtualKeyCode) -> Self { - match virtual_keycode { - winit::event::VirtualKeyCode::Key1 => KeyCode::Key1, - winit::event::VirtualKeyCode::Key2 => KeyCode::Key2, - winit::event::VirtualKeyCode::Key3 => KeyCode::Key3, - winit::event::VirtualKeyCode::Key4 => KeyCode::Key4, - winit::event::VirtualKeyCode::Key5 => KeyCode::Key5, - winit::event::VirtualKeyCode::Key6 => KeyCode::Key6, - winit::event::VirtualKeyCode::Key7 => KeyCode::Key7, - winit::event::VirtualKeyCode::Key8 => KeyCode::Key8, - winit::event::VirtualKeyCode::Key9 => KeyCode::Key9, - winit::event::VirtualKeyCode::Key0 => KeyCode::Key0, - winit::event::VirtualKeyCode::A => KeyCode::A, - winit::event::VirtualKeyCode::B => KeyCode::B, - winit::event::VirtualKeyCode::C => KeyCode::C, - winit::event::VirtualKeyCode::D => KeyCode::D, - winit::event::VirtualKeyCode::E => KeyCode::E, - winit::event::VirtualKeyCode::F => KeyCode::F, - winit::event::VirtualKeyCode::G => KeyCode::G, - winit::event::VirtualKeyCode::H => KeyCode::H, - winit::event::VirtualKeyCode::I => KeyCode::I, - winit::event::VirtualKeyCode::J => KeyCode::J, - winit::event::VirtualKeyCode::K => KeyCode::K, - winit::event::VirtualKeyCode::L => KeyCode::L, - winit::event::VirtualKeyCode::M => KeyCode::M, - winit::event::VirtualKeyCode::N => KeyCode::N, - winit::event::VirtualKeyCode::O => KeyCode::O, - winit::event::VirtualKeyCode::P => KeyCode::P, - winit::event::VirtualKeyCode::Q => KeyCode::Q, - winit::event::VirtualKeyCode::R => KeyCode::R, - winit::event::VirtualKeyCode::S => KeyCode::S, - winit::event::VirtualKeyCode::T => KeyCode::T, - winit::event::VirtualKeyCode::U => KeyCode::U, - winit::event::VirtualKeyCode::V => KeyCode::V, - winit::event::VirtualKeyCode::W => KeyCode::W, - winit::event::VirtualKeyCode::X => KeyCode::X, - winit::event::VirtualKeyCode::Y => KeyCode::Y, - winit::event::VirtualKeyCode::Z => KeyCode::Z, - winit::event::VirtualKeyCode::Escape => KeyCode::Escape, - winit::event::VirtualKeyCode::F1 => KeyCode::F1, - winit::event::VirtualKeyCode::F2 => KeyCode::F2, - winit::event::VirtualKeyCode::F3 => KeyCode::F3, - winit::event::VirtualKeyCode::F4 => KeyCode::F4, - winit::event::VirtualKeyCode::F5 => KeyCode::F5, - winit::event::VirtualKeyCode::F6 => KeyCode::F6, - winit::event::VirtualKeyCode::F7 => KeyCode::F7, - winit::event::VirtualKeyCode::F8 => KeyCode::F8, - winit::event::VirtualKeyCode::F9 => KeyCode::F9, - winit::event::VirtualKeyCode::F10 => KeyCode::F10, - winit::event::VirtualKeyCode::F11 => KeyCode::F11, - winit::event::VirtualKeyCode::F12 => KeyCode::F12, - winit::event::VirtualKeyCode::F13 => KeyCode::F13, - winit::event::VirtualKeyCode::F14 => KeyCode::F14, - winit::event::VirtualKeyCode::F15 => KeyCode::F15, - winit::event::VirtualKeyCode::F16 => KeyCode::F16, - winit::event::VirtualKeyCode::F17 => KeyCode::F17, - winit::event::VirtualKeyCode::F18 => KeyCode::F18, - winit::event::VirtualKeyCode::F19 => KeyCode::F19, - winit::event::VirtualKeyCode::F20 => KeyCode::F20, - winit::event::VirtualKeyCode::F21 => KeyCode::F21, - winit::event::VirtualKeyCode::F22 => KeyCode::F22, - winit::event::VirtualKeyCode::F23 => KeyCode::F23, - winit::event::VirtualKeyCode::F24 => KeyCode::F24, - winit::event::VirtualKeyCode::Snapshot => KeyCode::Snapshot, - winit::event::VirtualKeyCode::Scroll => KeyCode::Scroll, - winit::event::VirtualKeyCode::Pause => KeyCode::Pause, - winit::event::VirtualKeyCode::Insert => KeyCode::Insert, - winit::event::VirtualKeyCode::Home => KeyCode::Home, - winit::event::VirtualKeyCode::Delete => KeyCode::Delete, - winit::event::VirtualKeyCode::End => KeyCode::End, - winit::event::VirtualKeyCode::PageDown => KeyCode::PageDown, - winit::event::VirtualKeyCode::PageUp => KeyCode::PageUp, - winit::event::VirtualKeyCode::Left => KeyCode::Left, - winit::event::VirtualKeyCode::Up => KeyCode::Up, - winit::event::VirtualKeyCode::Right => KeyCode::Right, - winit::event::VirtualKeyCode::Down => KeyCode::Down, - winit::event::VirtualKeyCode::Back => KeyCode::Backspace, - winit::event::VirtualKeyCode::Return => KeyCode::Enter, - winit::event::VirtualKeyCode::Space => KeyCode::Space, - winit::event::VirtualKeyCode::Compose => KeyCode::Compose, - winit::event::VirtualKeyCode::Caret => KeyCode::Caret, - winit::event::VirtualKeyCode::Numlock => KeyCode::Numlock, - winit::event::VirtualKeyCode::Numpad0 => KeyCode::Numpad0, - winit::event::VirtualKeyCode::Numpad1 => KeyCode::Numpad1, - winit::event::VirtualKeyCode::Numpad2 => KeyCode::Numpad2, - winit::event::VirtualKeyCode::Numpad3 => KeyCode::Numpad3, - winit::event::VirtualKeyCode::Numpad4 => KeyCode::Numpad4, - winit::event::VirtualKeyCode::Numpad5 => KeyCode::Numpad5, - winit::event::VirtualKeyCode::Numpad6 => KeyCode::Numpad6, - winit::event::VirtualKeyCode::Numpad7 => KeyCode::Numpad7, - winit::event::VirtualKeyCode::Numpad8 => KeyCode::Numpad8, - winit::event::VirtualKeyCode::Numpad9 => KeyCode::Numpad9, - winit::event::VirtualKeyCode::AbntC1 => KeyCode::AbntC1, - winit::event::VirtualKeyCode::AbntC2 => KeyCode::AbntC2, - winit::event::VirtualKeyCode::Add => KeyCode::Add, - winit::event::VirtualKeyCode::Apostrophe => KeyCode::Apostrophe, - winit::event::VirtualKeyCode::Apps => KeyCode::Apps, - winit::event::VirtualKeyCode::At => KeyCode::At, - winit::event::VirtualKeyCode::Ax => KeyCode::Ax, - winit::event::VirtualKeyCode::Backslash => KeyCode::Backslash, - winit::event::VirtualKeyCode::Calculator => KeyCode::Calculator, - winit::event::VirtualKeyCode::Capital => KeyCode::Capital, - winit::event::VirtualKeyCode::Colon => KeyCode::Colon, - winit::event::VirtualKeyCode::Comma => KeyCode::Comma, - winit::event::VirtualKeyCode::Convert => KeyCode::Convert, - winit::event::VirtualKeyCode::Decimal => KeyCode::Decimal, - winit::event::VirtualKeyCode::Divide => KeyCode::Divide, - winit::event::VirtualKeyCode::Equals => KeyCode::Equals, - winit::event::VirtualKeyCode::Grave => KeyCode::Grave, - winit::event::VirtualKeyCode::Kana => KeyCode::Kana, - winit::event::VirtualKeyCode::Kanji => KeyCode::Kanji, - winit::event::VirtualKeyCode::LAlt => KeyCode::LAlt, - winit::event::VirtualKeyCode::LBracket => KeyCode::LBracket, - winit::event::VirtualKeyCode::LControl => KeyCode::LControl, - winit::event::VirtualKeyCode::LShift => KeyCode::LShift, - winit::event::VirtualKeyCode::LWin => KeyCode::LWin, - winit::event::VirtualKeyCode::Mail => KeyCode::Mail, - winit::event::VirtualKeyCode::MediaSelect => KeyCode::MediaSelect, - winit::event::VirtualKeyCode::MediaStop => KeyCode::MediaStop, - winit::event::VirtualKeyCode::Minus => KeyCode::Minus, - winit::event::VirtualKeyCode::Multiply => KeyCode::Multiply, - winit::event::VirtualKeyCode::Mute => KeyCode::Mute, - winit::event::VirtualKeyCode::MyComputer => KeyCode::MyComputer, - winit::event::VirtualKeyCode::NavigateForward => { - KeyCode::NavigateForward - } - winit::event::VirtualKeyCode::NavigateBackward => { - KeyCode::NavigateBackward - } - winit::event::VirtualKeyCode::NextTrack => KeyCode::NextTrack, - winit::event::VirtualKeyCode::NoConvert => KeyCode::NoConvert, - winit::event::VirtualKeyCode::NumpadComma => KeyCode::NumpadComma, - winit::event::VirtualKeyCode::NumpadEnter => KeyCode::NumpadEnter, - winit::event::VirtualKeyCode::NumpadEquals => KeyCode::NumpadEquals, - winit::event::VirtualKeyCode::OEM102 => KeyCode::OEM102, - winit::event::VirtualKeyCode::Period => KeyCode::Period, - winit::event::VirtualKeyCode::PlayPause => KeyCode::PlayPause, - winit::event::VirtualKeyCode::Power => KeyCode::Power, - winit::event::VirtualKeyCode::PrevTrack => KeyCode::PrevTrack, - winit::event::VirtualKeyCode::RAlt => KeyCode::RAlt, - winit::event::VirtualKeyCode::RBracket => KeyCode::RBracket, - winit::event::VirtualKeyCode::RControl => KeyCode::RControl, - winit::event::VirtualKeyCode::RShift => KeyCode::RShift, - winit::event::VirtualKeyCode::RWin => KeyCode::RWin, - winit::event::VirtualKeyCode::Semicolon => KeyCode::Semicolon, - winit::event::VirtualKeyCode::Slash => KeyCode::Slash, - winit::event::VirtualKeyCode::Sleep => KeyCode::Sleep, - winit::event::VirtualKeyCode::Stop => KeyCode::Stop, - winit::event::VirtualKeyCode::Subtract => KeyCode::Subtract, - winit::event::VirtualKeyCode::Sysrq => KeyCode::Sysrq, - winit::event::VirtualKeyCode::Tab => KeyCode::Tab, - winit::event::VirtualKeyCode::Underline => KeyCode::Underline, - winit::event::VirtualKeyCode::Unlabeled => KeyCode::Unlabeled, - winit::event::VirtualKeyCode::VolumeDown => KeyCode::VolumeDown, - winit::event::VirtualKeyCode::VolumeUp => KeyCode::VolumeUp, - winit::event::VirtualKeyCode::Wake => KeyCode::Wake, - winit::event::VirtualKeyCode::WebBack => KeyCode::WebBack, - winit::event::VirtualKeyCode::WebFavorites => KeyCode::WebFavorites, - winit::event::VirtualKeyCode::WebForward => KeyCode::WebForward, - winit::event::VirtualKeyCode::WebHome => KeyCode::WebHome, - winit::event::VirtualKeyCode::WebRefresh => KeyCode::WebRefresh, - winit::event::VirtualKeyCode::WebSearch => KeyCode::WebSearch, - winit::event::VirtualKeyCode::WebStop => KeyCode::WebStop, - winit::event::VirtualKeyCode::Yen => KeyCode::Yen, - winit::event::VirtualKeyCode::Copy => KeyCode::Copy, - winit::event::VirtualKeyCode::Paste => KeyCode::Paste, - winit::event::VirtualKeyCode::Cut => KeyCode::Cut, - } - } -} diff --git a/src/input/mouse.rs b/src/input/mouse.rs deleted file mode 100644 index d37f5b96..00000000 --- a/src/input/mouse.rs +++ /dev/null @@ -1,6 +0,0 @@ -//! Build mouse events. -mod button; -mod event; - -pub use button::Button; -pub use event::Event; diff --git a/src/input/mouse/button.rs b/src/input/mouse/button.rs deleted file mode 100644 index 6320d701..00000000 --- a/src/input/mouse/button.rs +++ /dev/null @@ -1,32 +0,0 @@ -/// The button of a mouse. -/// -/// If you are using [`winit`], consider enabling the `winit` feature to get -/// conversion implementations for free! -/// -/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ -#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] -pub enum Button { - /// The left mouse button. - Left, - - /// The right mouse button. - Right, - - /// The middle (wheel) button. - Middle, - - /// Some other button. - Other(u8), -} - -#[cfg(feature = "winit")] -impl From for super::Button { - fn from(mouse_button: winit::event::MouseButton) -> Self { - match mouse_button { - winit::event::MouseButton::Left => Button::Left, - winit::event::MouseButton::Right => Button::Right, - winit::event::MouseButton::Middle => Button::Middle, - winit::event::MouseButton::Other(other) => Button::Other(other), - } - } -} diff --git a/src/input/mouse/event.rs b/src/input/mouse/event.rs deleted file mode 100644 index 7b68208f..00000000 --- a/src/input/mouse/event.rs +++ /dev/null @@ -1,44 +0,0 @@ -use super::Button; -use crate::input::ButtonState; - -/// A mouse event. -/// -/// _**Note:** This type is largely incomplete! If you need to track -/// additional events, feel free to [open an issue] and share your use case!_ -/// -/// [open an issue]: https://github.com/hecrj/iced/issues -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum Event { - /// The mouse cursor entered the window. - CursorEntered, - - /// The mouse cursor left the window. - CursorLeft, - - /// The mouse cursor was moved - CursorMoved { - /// The X coordinate of the mouse position - x: f32, - - /// The Y coordinate of the mouse position - y: f32, - }, - - /// A mouse button was pressed or released. - Input { - /// The state of the button - state: ButtonState, - - /// The button identifier - button: Button, - }, - - /// The mouse wheel was scrolled. - WheelScrolled { - /// The number of horizontal lines scrolled - delta_x: f32, - - /// The number of vertical lines scrolled - delta_y: f32, - }, -} diff --git a/src/layout.rs b/src/layout.rs deleted file mode 100644 index de284a43..00000000 --- a/src/layout.rs +++ /dev/null @@ -1,62 +0,0 @@ -use stretch::result; - -use crate::{Point, Rectangle, Vector}; - -/// The computed bounds of a [`Node`] and its children. -/// -/// This type is provided by the GUI runtime to [`Widget::on_event`] and -/// [`Widget::draw`], describing the layout of the [`Node`] produced by -/// [`Widget::node`]. -/// -/// [`Node`]: struct.Node.html -/// [`Widget::on_event`]: widget/trait.Widget.html#method.on_event -/// [`Widget::draw`]: widget/trait.Widget.html#tymethod.draw -/// [`Widget::node`]: widget/trait.Widget.html#tymethod.node -#[derive(Debug)] -pub struct Layout<'a> { - layout: &'a result::Layout, - position: Point, -} - -impl<'a> Layout<'a> { - pub(crate) fn new(layout: &'a result::Layout) -> Self { - Self::with_parent_position(layout, Point::new(0.0, 0.0)) - } - - fn with_parent_position( - layout: &'a result::Layout, - parent_position: Point, - ) -> Self { - let position = - parent_position + Vector::new(layout.location.x, layout.location.y); - - Layout { layout, position } - } - - /// Gets the bounds of the [`Layout`]. - /// - /// The returned [`Rectangle`] describes the position and size of a - /// [`Node`]. - /// - /// [`Layout`]: struct.Layout.html - /// [`Rectangle`]: struct.Rectangle.html - /// [`Node`]: struct.Node.html - pub fn bounds(&self) -> Rectangle { - Rectangle { - x: self.position.x, - y: self.position.y, - width: self.layout.size.width, - height: self.layout.size.height, - } - } - - /// Returns an iterator over the [`Layout`] of the children of a [`Node`]. - /// - /// [`Layout`]: struct.Layout.html - /// [`Node`]: struct.Node.html - pub fn children(&'a self) -> impl Iterator> { - self.layout.children.iter().map(move |layout| { - Layout::with_parent_position(layout, self.position) - }) - } -} diff --git a/src/lib.rs b/src/lib.rs deleted file mode 100644 index c1c18b41..00000000 --- a/src/lib.rs +++ /dev/null @@ -1,229 +0,0 @@ -//! Iced is a renderer-agnostic GUI library focused on simplicity and -//! type-safety. Inspired by [Elm]. -//! -//! # Features -//! * Simple, easy-to-use, renderer-agnostic API -//! * Responsive, flexbox-based layouting -//! * Type-safe, reactive programming model -//! * Built-in widgets -//! * Custom widget support -//! -//! Check out the [repository] and the [examples] for more details! -//! -//! [examples]: https://github.com/hecrj/iced/tree/0.1.0/examples -//! [repository]: https://github.com/hecrj/iced -//! -//! # Usage -//! Inspired by [The Elm Architecture], Iced expects you to split user interfaces -//! into four different concepts: -//! -//! * __State__ — the state of your application -//! * __Messages__ — user interactions or meaningful events that you care -//! about -//! * __View logic__ — a way to display your __state__ as widgets that -//! may produce __messages__ on user interaction -//! * __Update logic__ — a way to react to __messages__ and update your -//! __state__ -//! -//! We can build something to see how this works! Let's say we want a simple counter -//! that can be incremented and decremented using two buttons. -//! -//! We start by modelling the __state__ of our application: -//! -//! ``` -//! use iced::button; -//! -//! struct Counter { -//! // The counter value -//! value: i32, -//! -//! // The local state of the two buttons -//! increment_button: button::State, -//! decrement_button: button::State, -//! } -//! ``` -//! -//! Next, we need to define the possible user interactions of our counter: -//! the button presses. These interactions are our __messages__: -//! -//! ``` -//! #[derive(Debug, Clone, Copy)] -//! pub enum Message { -//! IncrementPressed, -//! DecrementPressed, -//! } -//! ``` -//! -//! Now, let's show the actual counter by putting it all together in our -//! __view logic__: -//! -//! ``` -//! # use iced::button; -//! # -//! # struct Counter { -//! # // The counter value -//! # value: i32, -//! # -//! # // The local state of the two buttons -//! # increment_button: button::State, -//! # decrement_button: button::State, -//! # } -//! # -//! # #[derive(Debug, Clone, Copy)] -//! # pub enum Message { -//! # IncrementPressed, -//! # DecrementPressed, -//! # } -//! # -//! # mod iced_wgpu { -//! # use iced::{ -//! # button, text, text::HorizontalAlignment, text::VerticalAlignment, -//! # MouseCursor, Node, Point, Rectangle, Style, -//! # }; -//! # -//! # pub struct Renderer {} -//! # -//! # impl button::Renderer for Renderer { -//! # fn draw( -//! # &mut self, -//! # _cursor_position: Point, -//! # _bounds: Rectangle, -//! # _state: &button::State, -//! # _label: &str, -//! # _class: button::Class, -//! # ) -> MouseCursor { -//! # MouseCursor::OutOfBounds -//! # } -//! # } -//! # -//! # impl text::Renderer<[f32; 4]> for Renderer { -//! # fn node(&self, style: Style, _content: &str, _size: Option) -> Node { -//! # Node::new(style) -//! # } -//! # -//! # fn draw( -//! # &mut self, -//! # _bounds: Rectangle, -//! # _content: &str, -//! # _size: Option, -//! # _color: Option<[f32; 4]>, -//! # _horizontal_alignment: HorizontalAlignment, -//! # _vertical_alignment: VerticalAlignment, -//! # ) { -//! # } -//! # } -//! # } -//! use iced::{Button, Column, Text}; -//! use iced_wgpu::Renderer; // Iced does not include a renderer! We need to bring our own! -//! -//! impl Counter { -//! pub fn view(&mut self) -> Column { -//! // We use a column: a simple vertical layout -//! Column::new() -//! .push( -//! // The increment button. We tell it to produce an -//! // `IncrementPressed` message when pressed -//! Button::new(&mut self.increment_button, "+") -//! .on_press(Message::IncrementPressed), -//! ) -//! .push( -//! // We show the value of the counter here -//! Text::new(&self.value.to_string()).size(50), -//! ) -//! .push( -//! // The decrement button. We tell it to produce a -//! // `DecrementPressed` message when pressed -//! Button::new(&mut self.decrement_button, "-") -//! .on_press(Message::DecrementPressed), -//! ) -//! } -//! } -//! ``` -//! -//! Finally, we need to be able to react to any produced __messages__ and change -//! our __state__ accordingly in our __update logic__: -//! -//! ``` -//! # use iced::button; -//! # -//! # struct Counter { -//! # // The counter value -//! # value: i32, -//! # -//! # // The local state of the two buttons -//! # increment_button: button::State, -//! # decrement_button: button::State, -//! # } -//! # -//! # #[derive(Debug, Clone, Copy)] -//! # pub enum Message { -//! # IncrementPressed, -//! # DecrementPressed, -//! # } -//! impl Counter { -//! // ... -//! -//! pub fn update(&mut self, message: Message) { -//! match message { -//! Message::IncrementPressed => { -//! self.value += 1; -//! } -//! Message::DecrementPressed => { -//! self.value -= 1; -//! } -//! } -//! } -//! } -//! ``` -//! -//! And that's everything! We just wrote a whole user interface. Iced is now able -//! to: -//! -//! 1. Take the result of our __view logic__ and layout its widgets. -//! 1. Process events from our system and produce __messages__ for our -//! __update logic__. -//! 1. Draw the resulting user interface using our chosen __renderer__. -//! -//! Check out the [`UserInterface`] type to learn how to wire everything up! -//! -//! [Elm]: https://elm-lang.org/ -//! [The Elm Architecture]: https://guide.elm-lang.org/architecture/ -//! [documentation]: https://docs.rs/iced -//! [examples]: https://github.com/hecrj/iced/tree/master/examples -//! [`UserInterface`]: struct.UserInterface.html -#![deny(missing_docs)] -#![deny(missing_debug_implementations)] -#![deny(unused_results)] -#![deny(unsafe_code)] -#![deny(rust_2018_idioms)] -pub mod input; -pub mod renderer; -pub mod widget; - -mod element; -mod event; -mod hasher; -mod layout; -mod mouse_cursor; -mod node; -mod point; -mod rectangle; -mod style; -mod user_interface; -mod vector; - -#[doc(no_inline)] -pub use stretch::{geometry::Size, number::Number}; - -pub use element::Element; -pub use event::Event; -pub use hasher::Hasher; -pub use layout::Layout; -pub use mouse_cursor::MouseCursor; -pub use node::Node; -pub use point::Point; -pub use rectangle::Rectangle; -pub use style::{Align, Justify, Style}; -pub use user_interface::{Cache, UserInterface}; -pub(crate) use vector::Vector; -pub use widget::*; diff --git a/src/mouse_cursor.rs b/src/mouse_cursor.rs deleted file mode 100644 index 4ef6361a..00000000 --- a/src/mouse_cursor.rs +++ /dev/null @@ -1,35 +0,0 @@ -/// The state of the mouse cursor. -#[derive(Debug, Eq, PartialEq, Clone, Copy)] -pub enum MouseCursor { - /// The cursor is out of the bounds of the user interface. - OutOfBounds, - - /// The cursor is over a non-interactive widget. - Idle, - - /// The cursor is over a clickable widget. - Pointer, - - /// The cursor is over a busy widget. - Working, - - /// The cursor is over a grabbable widget. - Grab, - - /// The cursor is grabbing a widget. - Grabbing, -} - -#[cfg(feature = "winit")] -impl From for winit::window::CursorIcon { - fn from(mouse_cursor: MouseCursor) -> winit::window::CursorIcon { - match mouse_cursor { - MouseCursor::OutOfBounds => winit::window::CursorIcon::Default, - MouseCursor::Idle => winit::window::CursorIcon::Default, - MouseCursor::Pointer => winit::window::CursorIcon::Hand, - MouseCursor::Working => winit::window::CursorIcon::Progress, - MouseCursor::Grab => winit::window::CursorIcon::Grab, - MouseCursor::Grabbing => winit::window::CursorIcon::Grabbing, - } - } -} diff --git a/src/node.rs b/src/node.rs deleted file mode 100644 index 1db10d7f..00000000 --- a/src/node.rs +++ /dev/null @@ -1,60 +0,0 @@ -use stretch::node; - -use crate::{Number, Size, Style}; - -/// The visual requirements of a [`Widget`] and its children. -/// -/// When there have been changes and the [`Layout`] needs to be recomputed, the -/// runtime obtains a [`Node`] by calling [`Widget::node`]. -/// -/// [`Style`]: struct.Style.html -/// [`Widget`]: widget/trait.Widget.html -/// [`Node`]: struct.Node.html -/// [`Widget::node`]: widget/trait.Widget.html#tymethod.node -/// [`Layout`]: struct.Layout.html -#[derive(Debug)] -pub struct Node(pub(crate) node::Node); - -impl Node { - /// Creates a new [`Node`] with the given [`Style`]. - /// - /// [`Node`]: struct.Node.html - /// [`Style`]: struct.Style.html - pub fn new(style: Style) -> Node { - Self::with_children(style, Vec::new()) - } - - /// Creates a new [`Node`] with the given [`Style`] and a measure function. - /// - /// This type of node cannot have any children. - /// - /// You should use this when your [`Widget`] can adapt its contents to the - /// size of its container. The measure function will receive the container - /// size as a parameter and must compute the size of the [`Node`] inside - /// the given bounds (if the `Number` for a dimension is `Undefined` it - /// means that it has no boundary). - /// - /// [`Node`]: struct.Node.html - /// [`Style`]: struct.Style.html - /// [`Widget`]: widget/trait.Widget.html - pub fn with_measure(style: Style, measure: F) -> Node - where - F: 'static + Fn(Size) -> Size, - { - Node(node::Node::new_leaf( - style.0, - Box::new(move |size| Ok(measure(size))), - )) - } - - /// Creates a new [`Node`] with the given [`Style`] and children. - /// - /// [`Node`]: struct.Node.html - /// [`Style`]: struct.Style.html - pub fn with_children(style: Style, children: Vec) -> Node { - Node(node::Node::new( - style.0, - children.iter().map(|c| &c.0).collect(), - )) - } -} diff --git a/src/point.rs b/src/point.rs deleted file mode 100644 index 183998dd..00000000 --- a/src/point.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::Vector; - -/// A 2D point. -#[derive(Debug, Clone, Copy, PartialEq)] -pub struct Point { - /// The X coordinate. - pub x: f32, - - /// The Y coordinate. - pub y: f32, -} - -impl Point { - /// Creates a new [`Point`] with the given coordinates. - /// - /// [`Point`]: struct.Point.html - pub fn new(x: f32, y: f32) -> Self { - Self { x, y } - } -} - -impl std::ops::Add for Point { - type Output = Self; - - fn add(self, vector: Vector) -> Self { - Self { - x: self.x + vector.x, - y: self.y + vector.y, - } - } -} diff --git a/src/rectangle.rs b/src/rectangle.rs deleted file mode 100644 index 95c2570c..00000000 --- a/src/rectangle.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::Point; - -/// A rectangle. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Rectangle { - /// X coordinate of the top-left corner. - pub x: T, - - /// Y coordinate of the top-left corner. - pub y: T, - - /// Width of the rectangle. - pub width: T, - - /// Height of the rectangle. - pub height: T, -} - -impl Rectangle { - /// Returns true if the given [`Point`] is contained in the [`Rectangle`]. - /// - /// [`Point`]: struct.Point.html - /// [`Rectangle`]: struct.Rectangle.html - pub fn contains(&self, point: Point) -> bool { - self.x <= point.x - && point.x <= self.x + self.width - && self.y <= point.y - && point.y <= self.y + self.height - } -} diff --git a/src/renderer.rs b/src/renderer.rs deleted file mode 100644 index b445190b..00000000 --- a/src/renderer.rs +++ /dev/null @@ -1,45 +0,0 @@ -//! Write your own renderer. -//! -//! There is not a common entrypoint or trait for a __renderer__ in Iced. -//! Instead, every [`Widget`] constrains its generic `Renderer` type as -//! necessary. -//! -//! This approach is flexible and composable. For instance, the -//! [`Text`] widget only needs a [`text::Renderer`] while a [`Checkbox`] widget -//! needs both a [`text::Renderer`] and a [`checkbox::Renderer`], reusing logic. -//! -//! In the end, a __renderer__ satisfying all the constraints is -//! needed to build a [`UserInterface`]. -//! -//! [`Widget`]: ../widget/trait.Widget.html -//! [`UserInterface`]: ../struct.UserInterface.html -//! [`Text`]: ../widget/text/struct.Text.html -//! [`text::Renderer`]: ../widget/text/trait.Renderer.html -//! [`Checkbox`]: ../widget/checkbox/struct.Checkbox.html -//! [`checkbox::Renderer`]: ../widget/checkbox/trait.Renderer.html -use crate::Layout; - -/// A renderer able to graphically explain a [`Layout`]. -/// -/// [`Layout`]: ../struct.Layout.html -pub trait Debugger { - /// The color type that will be used to configure the _explanation_. - /// - /// This is the type that will be asked in [`Element::explain`]. - /// - /// [`Element::explain`]: ../struct.Element.html#method.explain - type Color: Copy; - - /// Explains the [`Layout`] of an [`Element`] for debugging purposes. - /// - /// This will be called when [`Element::explain`] has been used. It should - /// _explain_ the given [`Layout`] graphically. - /// - /// A common approach consists in recursively rendering the bounds of the - /// [`Layout`] and its children. - /// - /// [`Layout`]: struct.Layout.html - /// [`Element`]: struct.Element.html - /// [`Element::explain`]: struct.Element.html#method.explain - fn explain(&mut self, layout: &Layout<'_>, color: Self::Color); -} diff --git a/src/style.rs b/src/style.rs deleted file mode 100644 index 575ea366..00000000 --- a/src/style.rs +++ /dev/null @@ -1,262 +0,0 @@ -use std::hash::{Hash, Hasher}; -use stretch::{geometry, style}; - -/// The appearance of a [`Node`]. -/// -/// [`Node`]: struct.Node.html -#[derive(Debug, Clone, Copy)] -pub struct Style(pub(crate) style::Style); - -impl Style { - /// Defines the width of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn width(mut self, width: u16) -> Self { - self.0.size.width = style::Dimension::Points(width as f32); - self - } - - /// Defines the height of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn height(mut self, height: u16) -> Self { - self.0.size.height = style::Dimension::Points(height as f32); - self - } - - /// Defines the minimum width of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn min_width(mut self, min_width: u16) -> Self { - self.0.min_size.width = style::Dimension::Points(min_width as f32); - self - } - - /// Defines the maximum width of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn max_width(mut self, max_width: u16) -> Self { - self.0.max_size.width = style::Dimension::Points(max_width as f32); - self.fill_width() - } - - /// Defines the minimum height of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn min_height(mut self, min_height: u16) -> Self { - self.0.min_size.height = - style::Dimension::Points(f32::from(min_height)); - self - } - - /// Defines the maximum height of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn max_height(mut self, max_height: u16) -> Self { - self.0.max_size.height = - style::Dimension::Points(f32::from(max_height)); - self.fill_height() - } - - /// Makes a [`Node`] fill all the horizontal available space. - /// - /// [`Node`]: struct.Node.html - pub fn fill_width(mut self) -> Self { - self.0.size.width = stretch::style::Dimension::Percent(1.0); - self - } - - /// Makes a [`Node`] fill all the vertical available space. - /// - /// [`Node`]: struct.Node.html - pub fn fill_height(mut self) -> Self { - self.0.size.height = stretch::style::Dimension::Percent(1.0); - self - } - - pub(crate) fn align_items(mut self, align: Align) -> Self { - self.0.align_items = align.into(); - self - } - - pub(crate) fn justify_content(mut self, justify: Justify) -> Self { - self.0.justify_content = justify.into(); - self - } - - /// Sets the alignment of a [`Node`]. - /// - /// If the [`Node`] is inside a... - /// - /// * [`Column`], this setting will affect its __horizontal__ alignment. - /// * [`Row`], this setting will affect its __vertical__ alignment. - /// - /// [`Node`]: struct.Node.html - /// [`Column`]: widget/struct.Column.html - /// [`Row`]: widget/struct.Row.html - pub fn align_self(mut self, align: Align) -> Self { - self.0.align_self = align.into(); - self - } - - /// Sets the padding of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn padding(mut self, px: u16) -> Self { - self.0.padding = stretch::geometry::Rect { - start: style::Dimension::Points(px as f32), - end: style::Dimension::Points(px as f32), - top: style::Dimension::Points(px as f32), - bottom: style::Dimension::Points(px as f32), - }; - - self - } -} - -impl Default for Style { - fn default() -> Style { - Style(style::Style { - align_items: style::AlignItems::FlexStart, - justify_content: style::JustifyContent::FlexStart, - ..style::Style::default() - }) - } -} - -impl Hash for Style { - fn hash(&self, state: &mut H) { - hash_size(&self.0.size, state); - hash_size(&self.0.min_size, state); - hash_size(&self.0.max_size, state); - - hash_rect(&self.0.margin, state); - - (self.0.flex_direction as u8).hash(state); - (self.0.align_items as u8).hash(state); - (self.0.justify_content as u8).hash(state); - (self.0.align_self as u8).hash(state); - (self.0.flex_grow as u32).hash(state); - } -} - -fn hash_size( - size: &geometry::Size, - state: &mut H, -) { - hash_dimension(size.width, state); - hash_dimension(size.height, state); -} - -fn hash_rect( - rect: &geometry::Rect, - state: &mut H, -) { - hash_dimension(rect.start, state); - hash_dimension(rect.end, state); - hash_dimension(rect.top, state); - hash_dimension(rect.bottom, state); -} - -fn hash_dimension(dimension: style::Dimension, state: &mut H) { - match dimension { - style::Dimension::Undefined => state.write_u8(0), - style::Dimension::Auto => state.write_u8(1), - style::Dimension::Points(points) => { - state.write_u8(2); - (points as u32).hash(state); - } - style::Dimension::Percent(percent) => { - state.write_u8(3); - (percent as u32).hash(state); - } - } -} - -/// Alignment on the cross axis of a container. -/// -/// * On a [`Column`], it describes __horizontal__ alignment. -/// * On a [`Row`], it describes __vertical__ alignment. -/// -/// [`Column`]: widget/struct.Column.html -/// [`Row`]: widget/struct.Row.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Align { - /// Align at the start of the cross axis. - Start, - - /// Align at the center of the cross axis. - Center, - - /// Align at the end of the cross axis. - End, - - /// Stretch over the cross axis. - Stretch, -} - -#[doc(hidden)] -impl From for style::AlignItems { - fn from(align: Align) -> Self { - match align { - Align::Start => style::AlignItems::FlexStart, - Align::Center => style::AlignItems::Center, - Align::End => style::AlignItems::FlexEnd, - Align::Stretch => style::AlignItems::Stretch, - } - } -} - -#[doc(hidden)] -impl From for style::AlignSelf { - fn from(align: Align) -> Self { - match align { - Align::Start => style::AlignSelf::FlexStart, - Align::Center => style::AlignSelf::Center, - Align::End => style::AlignSelf::FlexEnd, - Align::Stretch => style::AlignSelf::Stretch, - } - } -} - -/// Distribution on the main axis of a container. -/// -/// * On a [`Column`], it describes __vertical__ distribution. -/// * On a [`Row`], it describes __horizontal__ distribution. -/// -/// [`Column`]: widget/struct.Column.html -/// [`Row`]: widget/struct.Row.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Justify { - /// Place items at the start of the main axis. - Start, - - /// Place items at the center of the main axis. - Center, - - /// Place items at the end of the main axis. - End, - - /// Place items with space between. - SpaceBetween, - - /// Place items with space around. - SpaceAround, - - /// Place items with evenly distributed space. - SpaceEvenly, -} - -#[doc(hidden)] -impl From for style::JustifyContent { - fn from(justify: Justify) -> Self { - match justify { - Justify::Start => style::JustifyContent::FlexStart, - Justify::Center => style::JustifyContent::Center, - Justify::End => style::JustifyContent::FlexEnd, - Justify::SpaceBetween => style::JustifyContent::SpaceBetween, - Justify::SpaceAround => style::JustifyContent::SpaceAround, - Justify::SpaceEvenly => style::JustifyContent::SpaceEvenly, - } - } -} diff --git a/src/user_interface.rs b/src/user_interface.rs deleted file mode 100644 index 2c7cbf82..00000000 --- a/src/user_interface.rs +++ /dev/null @@ -1,323 +0,0 @@ -use crate::{input::mouse, Column, Element, Event, Layout, MouseCursor, Point}; - -use std::hash::Hasher; -use stretch::result; - -/// A set of interactive graphical elements with a specific [`Layout`]. -/// -/// It can be updated and drawn. -/// -/// Iced tries to avoid dictating how to write your event loop. You are in -/// charge of using this type in your system in any way you want. -/// -/// [`Layout`]: struct.Layout.html -#[derive(Debug)] -pub struct UserInterface<'a, Message, Renderer> { - hash: u64, - root: Element<'a, Message, Renderer>, - layout: result::Layout, - cursor_position: Point, -} - -impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { - /// Builds a user interface for an [`Element`]. - /// - /// It is able to avoid expensive computations when using a [`Cache`] - /// obtained from a previous instance of a [`UserInterface`]. - /// - /// [`Element`]: struct.Element.html - /// [`Cache`]: struct.Cache.html - /// [`UserInterface`]: struct.UserInterface.html - /// - /// # Example - /// Imagine we want to build a [`UserInterface`] for - /// [the counter example that we previously wrote](index.html#usage). Here - /// is naive way to set up our application loop: - /// - /// ```no_run - /// use iced::{UserInterface, Cache}; - /// use iced_wgpu::Renderer; - /// - /// # mod iced_wgpu { - /// # pub struct Renderer; - /// # - /// # impl Renderer { - /// # pub fn new() -> Self { Renderer } - /// # } - /// # } - /// # - /// # use iced::Column; - /// # - /// # pub struct Counter; - /// # - /// # impl Counter { - /// # pub fn new() -> Self { Counter } - /// # pub fn view(&self) -> Column<(), Renderer> { - /// # Column::new() - /// # } - /// # } - /// // Initialization - /// let mut counter = Counter::new(); - /// let mut cache = Cache::new(); - /// let mut renderer = Renderer::new(); - /// - /// // Application loop - /// loop { - /// // Process system events here... - /// - /// // Build the user interface - /// let user_interface = UserInterface::build( - /// counter.view(), - /// cache, - /// &renderer, - /// ); - /// - /// // Update and draw the user interface here... - /// // ... - /// - /// // Obtain the cache for the next iteration - /// cache = user_interface.into_cache(); - /// } - /// ``` - pub fn build>>( - root: E, - cache: Cache, - renderer: &Renderer, - ) -> Self { - let root = root.into(); - - let hasher = &mut crate::Hasher::default(); - root.hash_layout(hasher); - - let hash = hasher.finish(); - - let layout = if hash == cache.hash { - cache.layout - } else { - root.compute_layout(renderer) - }; - - UserInterface { - hash, - root, - layout, - cursor_position: cache.cursor_position, - } - } - - /// Updates the [`UserInterface`] by processing each provided [`Event`]. - /// - /// It returns __messages__ that may have been produced as a result of user - /// interactions. You should feed these to your __update logic__. - /// - /// [`UserInterface`]: struct.UserInterface.html - /// [`Event`]: enum.Event.html - /// - /// # Example - /// Let's allow our [counter](index.html#usage) to change state by completing - /// [the previous example](#example): - /// - /// ```no_run - /// use iced::{UserInterface, Cache}; - /// use iced_wgpu::Renderer; - /// - /// # mod iced_wgpu { - /// # pub struct Renderer; - /// # - /// # impl Renderer { - /// # pub fn new() -> Self { Renderer } - /// # } - /// # } - /// # - /// # use iced::Column; - /// # - /// # pub struct Counter; - /// # - /// # impl Counter { - /// # pub fn new() -> Self { Counter } - /// # pub fn view(&self) -> Column<(), Renderer> { - /// # Column::new() - /// # } - /// # pub fn update(&mut self, message: ()) {} - /// # } - /// let mut counter = Counter::new(); - /// let mut cache = Cache::new(); - /// let mut renderer = Renderer::new(); - /// - /// // Initialize our event storage - /// let mut events = Vec::new(); - /// - /// loop { - /// // Process system events... - /// - /// let mut user_interface = UserInterface::build( - /// counter.view(), - /// cache, - /// &renderer, - /// ); - /// - /// // Update the user interface - /// let messages = user_interface.update(events.drain(..)); - /// - /// cache = user_interface.into_cache(); - /// - /// // Process the produced messages - /// for message in messages { - /// counter.update(message); - /// } - /// } - /// ``` - pub fn update( - &mut self, - events: impl Iterator, - ) -> Vec { - let mut messages = Vec::new(); - - for event in events { - if let Event::Mouse(mouse::Event::CursorMoved { x, y }) = event { - self.cursor_position = Point::new(x, y); - } - - self.root.widget.on_event( - event, - Layout::new(&self.layout), - self.cursor_position, - &mut messages, - ); - } - - messages - } - - /// Draws the [`UserInterface`] with the provided [`Renderer`]. - /// - /// It returns the current state of the [`MouseCursor`]. You should update - /// the icon of the mouse cursor accordingly in your system. - /// - /// [`UserInterface`]: struct.UserInterface.html - /// [`Renderer`]: trait.Renderer.html - /// [`MouseCursor`]: enum.MouseCursor.html - /// - /// # Example - /// We can finally draw our [counter](index.html#usage) by - /// [completing the last example](#example-1): - /// - /// ```no_run - /// use iced::{UserInterface, Cache}; - /// use iced_wgpu::Renderer; - /// - /// # mod iced_wgpu { - /// # pub struct Renderer; - /// # - /// # impl Renderer { - /// # pub fn new() -> Self { Renderer } - /// # } - /// # } - /// # - /// # use iced::Column; - /// # - /// # pub struct Counter; - /// # - /// # impl Counter { - /// # pub fn new() -> Self { Counter } - /// # pub fn view(&self) -> Column<(), Renderer> { - /// # Column::new() - /// # } - /// # pub fn update(&mut self, message: ()) {} - /// # } - /// let mut counter = Counter::new(); - /// let mut cache = Cache::new(); - /// let mut renderer = Renderer::new(); - /// let mut events = Vec::new(); - /// - /// loop { - /// // Process system events... - /// - /// let mut user_interface = UserInterface::build( - /// counter.view(), - /// cache, - /// &renderer, - /// ); - /// - /// let messages = user_interface.update(events.drain(..)); - /// - /// // Draw the user interface - /// let mouse_cursor = user_interface.draw(&mut renderer); - /// - /// cache = user_interface.into_cache(); - /// - /// for message in messages { - /// counter.update(message); - /// } - /// - /// // Update mouse cursor icon... - /// // Flush rendering operations... - /// } - /// ``` - pub fn draw(&self, renderer: &mut Renderer) -> MouseCursor { - self.root.widget.draw( - renderer, - Layout::new(&self.layout), - self.cursor_position, - ) - } - - /// Extract the [`Cache`] of the [`UserInterface`], consuming it in the - /// process. - /// - /// [`Cache`]: struct.Cache.html - /// [`UserInterface`]: struct.UserInterface.html - pub fn into_cache(self) -> Cache { - Cache { - hash: self.hash, - layout: self.layout, - cursor_position: self.cursor_position, - } - } -} - -/// Reusable data of a specific [`UserInterface`]. -/// -/// [`UserInterface`]: struct.UserInterface.html -#[derive(Debug, Clone)] -pub struct Cache { - hash: u64, - layout: result::Layout, - cursor_position: Point, -} - -impl Cache { - /// Creates an empty [`Cache`]. - /// - /// You should use this to initialize a [`Cache`] before building your first - /// [`UserInterface`]. - /// - /// [`Cache`]: struct.Cache.html - /// [`UserInterface`]: struct.UserInterface.html - pub fn new() -> Cache { - let root: Element<'_, (), ()> = Column::new().into(); - - let hasher = &mut crate::Hasher::default(); - root.hash_layout(hasher); - - Cache { - hash: hasher.finish(), - layout: root.compute_layout(&()), - cursor_position: Point::new(0.0, 0.0), - } - } -} - -impl Default for Cache { - fn default() -> Cache { - Cache::new() - } -} - -impl PartialEq for Cache { - fn eq(&self, other: &Cache) -> bool { - self.hash == other.hash && self.cursor_position == other.cursor_position - } -} - -impl Eq for Cache {} diff --git a/src/vector.rs b/src/vector.rs deleted file mode 100644 index f45daab9..00000000 --- a/src/vector.rs +++ /dev/null @@ -1,15 +0,0 @@ -/// A 2D vector. -#[derive(Debug, Clone, Copy, PartialEq)] -pub struct Vector { - pub x: f32, - pub y: f32, -} - -impl Vector { - /// Creates a new [`Vector`] with the given components. - /// - /// [`Vector`]: struct.Vector.html - pub fn new(x: f32, y: f32) -> Self { - Self { x, y } - } -} diff --git a/src/widget.rs b/src/widget.rs deleted file mode 100644 index 30606934..00000000 --- a/src/widget.rs +++ /dev/null @@ -1,114 +0,0 @@ -//! Use the built-in widgets or create your own. -//! -//! # Built-in widgets -//! Every built-in drawable widget has its own module with a `Renderer` trait -//! that must be implemented by a [renderer] before being able to use it as -//! a [`Widget`]. -//! -//! # Custom widgets -//! If you want to implement a custom widget, you simply need to implement the -//! [`Widget`] trait. You can use the API of the built-in widgets as a guide or -//! source of inspiration. -//! -//! # Re-exports -//! For convenience, the contents of this module are available at the root -//! module. Therefore, you can directly type: -//! -//! ``` -//! use iced::{button, Button, Widget}; -//! ``` -//! -//! [`Widget`]: trait.Widget.html -//! [renderer]: ../renderer/index.html -mod column; -mod row; - -pub mod button; -pub mod checkbox; -pub mod image; -//pub mod progress_bar; -pub mod radio; -pub mod slider; -pub mod text; - -pub use button::Button; -pub use checkbox::Checkbox; -pub use column::Column; -pub use image::Image; -//pub use progress_bar::ProgressBar; -pub use radio::Radio; -pub use row::Row; -pub use slider::Slider; -pub use text::Text; - -use crate::{Event, Hasher, Layout, MouseCursor, Node, Point}; - -/// A component that displays information and allows interaction. -/// -/// If you want to build your own widgets, you will need to implement this -/// trait. -/// -/// [`Widget`]: trait.Widget.html -/// [`Element`]: ../struct.Element.html -pub trait Widget: std::fmt::Debug { - /// Returns the [`Node`] of the [`Widget`]. - /// - /// This [`Node`] is used by the runtime to compute the [`Layout`] of the - /// user interface. - /// - /// [`Node`]: ../struct.Node.html - /// [`Widget`]: trait.Widget.html - /// [`Layout`]: ../struct.Layout.html - fn node(&self, renderer: &Renderer) -> Node; - - /// Draws the [`Widget`] using the associated `Renderer`. - /// - /// It must return the [`MouseCursor`] state for the [`Widget`]. - /// - /// [`Widget`]: trait.Widget.html - /// [`MouseCursor`]: ../enum.MouseCursor.html - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor; - - /// Computes the _layout_ hash of the [`Widget`]. - /// - /// The produced hash is used by the runtime to decide if the [`Layout`] - /// needs to be recomputed between frames. Therefore, to ensure maximum - /// efficiency, the hash should only be affected by the properties of the - /// [`Widget`] that can affect layouting. - /// - /// For example, the [`Text`] widget does not hash its color property, as - /// its value cannot affect the overall [`Layout`] of the user interface. - /// - /// [`Widget`]: trait.Widget.html - /// [`Layout`]: ../struct.Layout.html - /// [`Text`]: text/struct.Text.html - fn hash_layout(&self, state: &mut Hasher); - - /// Processes a runtime [`Event`]. - /// - /// It receives: - /// * an [`Event`] describing user interaction - /// * the computed [`Layout`] of the [`Widget`] - /// * the current cursor position - /// * a mutable `Message` list, allowing the [`Widget`] to produce - /// new messages based on user interaction. - /// - /// By default, it does nothing. - /// - /// [`Event`]: ../enum.Event.html - /// [`Widget`]: trait.Widget.html - /// [`Layout`]: ../struct.Layout.html - fn on_event( - &mut self, - _event: Event, - _layout: Layout<'_>, - _cursor_position: Point, - _messages: &mut Vec, - ) { - } -} diff --git a/src/widget/button.rs b/src/widget/button.rs deleted file mode 100644 index abcdbfeb..00000000 --- a/src/widget/button.rs +++ /dev/null @@ -1,282 +0,0 @@ -//! Allow your users to perform actions by pressing a button. -//! -//! A [`Button`] has some local [`State`] and a [`Class`]. -//! -//! [`Button`]: struct.Button.html -//! [`State`]: struct.State.html -//! [`Class`]: enum.Class.html - -use crate::input::{mouse, ButtonState}; -use crate::{ - Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, - Style, Widget, -}; - -use std::hash::Hash; - -/// A generic widget that produces a message when clicked. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`button::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`button::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::{button, Button}; -/// -/// pub enum Message { -/// ButtonClicked, -/// } -/// -/// let state = &mut button::State::new(); -/// -/// Button::new(state, "Click me!") -/// .on_press(Message::ButtonClicked); -/// ``` -/// -/// ![Button drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/button.png?raw=true) -pub struct Button<'a, Message> { - state: &'a mut State, - label: String, - class: Class, - on_press: Option, - style: Style, -} - -impl<'a, Message> std::fmt::Debug for Button<'a, Message> -where - Message: std::fmt::Debug, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Button") - .field("state", &self.state) - .field("label", &self.label) - .field("class", &self.class) - .field("on_press", &self.on_press) - .field("style", &self.style) - .finish() - } -} - -impl<'a, Message> Button<'a, Message> { - /// Creates a new [`Button`] with some local [`State`] and the given label. - /// - /// The default [`Class`] of a new [`Button`] is [`Class::Primary`]. - /// - /// [`Button`]: struct.Button.html - /// [`State`]: struct.State.html - /// [`Class`]: enum.Class.html - /// [`Class::Primary`]: enum.Class.html#variant.Primary - pub fn new(state: &'a mut State, label: &str) -> Self { - Button { - state, - label: String::from(label), - class: Class::Primary, - on_press: None, - style: Style::default().min_width(100), - } - } - - /// Sets the width of the [`Button`] in pixels. - /// - /// [`Button`]: struct.Button.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Makes the [`Button`] fill the horizontal space of its container. - /// - /// [`Button`]: struct.Button.html - pub fn fill_width(mut self) -> Self { - self.style = self.style.fill_width(); - self - } - - /// Sets the alignment of the [`Button`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Button`]: struct.Button.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } - - /// Sets the [`Class`] of the [`Button`]. - /// - /// - /// [`Button`]: struct.Button.html - /// [`Class`]: enum.Class.html - pub fn class(mut self, class: Class) -> Self { - self.class = class; - self - } - - /// Sets the message that will be produced when the [`Button`] is pressed. - /// - /// [`Button`]: struct.Button.html - pub fn on_press(mut self, msg: Message) -> Self { - self.on_press = Some(msg); - self - } -} - -impl<'a, Message, Renderer> Widget for Button<'a, Message> -where - Renderer: self::Renderer, - Message: Copy + std::fmt::Debug, -{ - fn node(&self, _renderer: &Renderer) -> Node { - Node::new(self.style.height(50)) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state, - }) => { - if let Some(on_press) = self.on_press { - let bounds = layout.bounds(); - - match state { - ButtonState::Pressed => { - self.state.is_pressed = - bounds.contains(cursor_position); - } - ButtonState::Released => { - let is_clicked = self.state.is_pressed - && bounds.contains(cursor_position); - - self.state.is_pressed = false; - - if is_clicked { - messages.push(on_press); - } - } - } - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - cursor_position, - layout.bounds(), - self.state, - &self.label, - self.class, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -/// The local state of a [`Button`]. -/// -/// [`Button`]: struct.Button.html -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub struct State { - is_pressed: bool, -} - -impl State { - /// Creates a new [`State`]. - /// - /// [`State`]: struct.State.html - pub fn new() -> State { - State::default() - } - - /// Returns whether the associated [`Button`] is currently being pressed or - /// not. - /// - /// [`Button`]: struct.Button.html - pub fn is_pressed(&self) -> bool { - self.is_pressed - } -} - -/// The type of a [`Button`]. -/// -/// ![Different buttons drawn by the built-in renderer in Coffee](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/button_classes.png?raw=true) -/// -/// [`Button`]: struct.Button.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Class { - /// The [`Button`] performs the main action. - /// - /// [`Button`]: struct.Button.html - Primary, - - /// The [`Button`] performs an alternative action. - /// - /// [`Button`]: struct.Button.html - Secondary, - - /// The [`Button`] performs a productive action. - /// - /// [`Button`]: struct.Button.html - Positive, -} - -/// The renderer of a [`Button`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Button`] in your user interface. -/// -/// [`Button`]: struct.Button.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Button`]. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Button`] - /// * the local state of the [`Button`] - /// * the label of the [`Button`] - /// * the [`Class`] of the [`Button`] - /// - /// [`Button`]: struct.Button.html - /// [`State`]: struct.State.html - /// [`Class`]: enum.Class.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - state: &State, - label: &str, - class: Class, - ) -> MouseCursor; -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, - Message: 'static + Copy + std::fmt::Debug, -{ - fn from(button: Button<'a, Message>) -> Element<'a, Message, Renderer> { - Element::new(button) - } -} diff --git a/src/widget/checkbox.rs b/src/widget/checkbox.rs deleted file mode 100644 index c60807fd..00000000 --- a/src/widget/checkbox.rs +++ /dev/null @@ -1,203 +0,0 @@ -//! Show toggle controls using checkboxes. -use std::hash::Hash; - -use crate::input::{mouse, ButtonState}; -use crate::widget::{text, Column, Row, Text}; -use crate::{ - Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, - Widget, -}; - -/// A box that can be checked, with a generic text `Color`. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`checkbox::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`checkbox::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::Checkbox; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } -/// -/// pub enum Message { -/// CheckboxToggled(bool), -/// } -/// -/// let is_checked = true; -/// -/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled) -/// .label_color(Color::Black); -/// ``` -/// -/// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true) -pub struct Checkbox { - is_checked: bool, - on_toggle: Box Message>, - label: String, - label_color: Option, -} - -impl std::fmt::Debug for Checkbox -where - Color: std::fmt::Debug, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Checkbox") - .field("is_checked", &self.is_checked) - .field("label", &self.label) - .field("label_color", &self.label_color) - .finish() - } -} - -impl Checkbox { - /// Creates a new [`Checkbox`]. - /// - /// It expects: - /// * a boolean describing whether the [`Checkbox`] is checked or not - /// * the label of the [`Checkbox`] - /// * a function that will be called when the [`Checkbox`] is toggled. - /// It will receive the new state of the [`Checkbox`] and must produce - /// a `Message`. - /// - /// [`Checkbox`]: struct.Checkbox.html - pub fn new(is_checked: bool, label: &str, f: F) -> Self - where - F: 'static + Fn(bool) -> Message, - { - Checkbox { - is_checked, - on_toggle: Box::new(f), - label: String::from(label), - label_color: None, - } - } - - /// Sets the `Color` of the label of the [`Checkbox`]. - /// - /// [`Checkbox`]: struct.Checkbox.html - pub fn label_color(mut self, color: Color) -> Self { - self.label_color = Some(color); - self - } -} - -impl Widget - for Checkbox -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, -{ - fn node(&self, renderer: &Renderer) -> Node { - Row::<(), Renderer>::new() - .spacing(15) - .align_items(Align::Center) - .push(Column::new().width(28).height(28)) - .push(Text::new(&self.label)) - .node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Pressed, - }) => { - let mouse_over = layout - .children() - .any(|child| child.bounds().contains(cursor_position)); - - if mouse_over { - messages.push((self.on_toggle)(!self.is_checked)); - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let children: Vec<_> = layout.children().collect(); - - let text_bounds = children[1].bounds(); - - text::Renderer::draw( - renderer, - text_bounds, - &self.label, - None, - self.label_color, - text::HorizontalAlignment::Left, - text::VerticalAlignment::Top, - ); - - self::Renderer::draw( - renderer, - cursor_position, - children[0].bounds(), - text_bounds, - self.is_checked, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.label.hash(state); - } -} - -/// The renderer of a [`Checkbox`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Checkbox`] in your user interface. -/// -/// [`Checkbox`]: struct.Checkbox.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Checkbox`]. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Checkbox`] - /// * the bounds of the label of the [`Checkbox`] - /// * whether the [`Checkbox`] is checked or not - /// - /// [`Checkbox`]: struct.Checkbox.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - label_bounds: Rectangle, - is_checked: bool, - ) -> MouseCursor; -} - -impl<'a, Color, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, - Message: 'static, -{ - fn from( - checkbox: Checkbox, - ) -> Element<'a, Message, Renderer> { - Element::new(checkbox) - } -} diff --git a/src/widget/column.rs b/src/widget/column.rs deleted file mode 100644 index ff754e98..00000000 --- a/src/widget/column.rs +++ /dev/null @@ -1,224 +0,0 @@ -use std::hash::Hash; - -use crate::{ - Align, Element, Event, Hasher, Justify, Layout, MouseCursor, Node, Point, - Style, Widget, -}; - -/// A container that distributes its contents vertically. -/// -/// A [`Column`] will try to fill the horizontal space of its container. -/// -/// [`Column`]: struct.Column.html -#[derive(Default)] -pub struct Column<'a, Message, Renderer> { - style: Style, - spacing: u16, - children: Vec>, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Column<'a, Message, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Column") - .field("style", &self.style) - .field("spacing", &self.spacing) - .field("children", &self.children) - .finish() - } -} - -impl<'a, Message, Renderer> Column<'a, Message, Renderer> { - /// Creates an empty [`Column`]. - /// - /// [`Column`]: struct.Column.html - pub fn new() -> Self { - let mut style = Style::default().fill_width(); - style.0.flex_direction = stretch::style::FlexDirection::Column; - - Column { - style, - spacing: 0, - children: Vec::new(), - } - } - - /// Sets the vertical spacing _between_ elements in pixels. - /// - /// Custom margins per element do not exist in Iced. You should use this - /// method instead! While less flexible, it helps you keep spacing between - /// elements consistent. - pub fn spacing(mut self, px: u16) -> Self { - self.spacing = px; - self - } - - /// Sets the padding of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn padding(mut self, px: u16) -> Self { - self.style = self.style.padding(px); - self - } - - /// Sets the width of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the maximum width of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn max_width(mut self, max_width: u16) -> Self { - self.style = self.style.max_width(max_width); - self - } - - /// Sets the maximum height of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn max_height(mut self, max_height: u16) -> Self { - self.style = self.style.max_height(max_height); - self - } - - /// Sets the alignment of the [`Column`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Column`]: struct.Column.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } - - /// Sets the horizontal alignment of the contents of the [`Column`] . - /// - /// [`Column`]: struct.Column.html - pub fn align_items(mut self, align: Align) -> Self { - self.style = self.style.align_items(align); - self - } - - /// Sets the vertical distribution strategy for the contents of the - /// [`Column`] . - /// - /// [`Column`]: struct.Column.html - pub fn justify_content(mut self, justify: Justify) -> Self { - self.style = self.style.justify_content(justify); - self - } - - /// Adds an [`Element`] to the [`Column`]. - /// - /// [`Element`]: ../struct.Element.html - /// [`Column`]: struct.Column.html - pub fn push(mut self, child: E) -> Column<'a, Message, Renderer> - where - E: Into>, - { - self.children.push(child.into()); - self - } -} - -impl<'a, Message, Renderer> Widget - for Column<'a, Message, Renderer> -{ - fn node(&self, renderer: &Renderer) -> Node { - let mut children: Vec = self - .children - .iter() - .map(|child| { - let mut node = child.widget.node(renderer); - - let mut style = node.0.style(); - style.margin.bottom = - stretch::style::Dimension::Points(f32::from(self.spacing)); - - node.0.set_style(style); - node - }) - .collect(); - - if let Some(node) = children.last_mut() { - let mut style = node.0.style(); - style.margin.bottom = stretch::style::Dimension::Undefined; - - node.0.set_style(style); - } - - Node::with_children(self.style, children) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - self.children.iter_mut().zip(layout.children()).for_each( - |(child, layout)| { - child - .widget - .on_event(event, layout, cursor_position, messages) - }, - ); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let mut cursor = MouseCursor::OutOfBounds; - - self.children.iter().zip(layout.children()).for_each( - |(child, layout)| { - let new_cursor = - child.widget.draw(renderer, layout, cursor_position); - - if new_cursor != MouseCursor::OutOfBounds { - cursor = new_cursor; - } - }, - ); - - cursor - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - self.spacing.hash(state); - - for child in &self.children { - child.widget.hash_layout(state); - } - } -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: 'a, - Message: 'static, -{ - fn from( - column: Column<'a, Message, Renderer>, - ) -> Element<'a, Message, Renderer> { - Element::new(column) - } -} diff --git a/src/widget/image.rs b/src/widget/image.rs deleted file mode 100644 index d94bfea5..00000000 --- a/src/widget/image.rs +++ /dev/null @@ -1,178 +0,0 @@ -//! Display images in your user interface. - -use crate::{ - Align, Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, - Widget, -}; - -use std::hash::Hash; - -/// A frame that displays an image while keeping aspect ratio. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`image::Renderer`] trait. -/// -/// [`Widget`]: ../../core/trait.Widget.html -/// [`image::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::Image; -/// -/// # let my_handle = String::from("some_handle"); -/// let image = Image::new(my_handle); -/// ``` -pub struct Image { - image: I, - source: Option>, - width: Option, - height: Option, - style: Style, -} - -impl std::fmt::Debug for Image { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Image") - .field("source", &self.source) - .field("width", &self.width) - .field("height", &self.height) - .field("style", &self.style) - .finish() - } -} - -impl Image { - /// Creates a new [`Image`] with given image handle. - /// - /// [`Image`]: struct.Image.html - pub fn new(image: I) -> Self { - Image { - image, - source: None, - width: None, - height: None, - style: Style::default(), - } - } - - /// Sets the portion of the [`Image`] to draw. - /// - /// [`Image`]: struct.Image.html - pub fn clip(mut self, source: Rectangle) -> Self { - self.source = Some(source); - self - } - - /// Sets the width of the [`Image`] boundaries in pixels. - /// - /// [`Image`]: struct.Image.html - pub fn width(mut self, width: u16) -> Self { - self.width = Some(width); - self - } - - /// Sets the height of the [`Image`] boundaries in pixels. - /// - /// [`Image`]: struct.Image.html - pub fn height(mut self, height: u16) -> Self { - self.height = Some(height); - self - } - - /// Sets the alignment of the [`Image`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Image`]: struct.Image.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } -} - -impl Widget for Image -where - Renderer: self::Renderer, - I: Clone, -{ - fn node(&self, renderer: &Renderer) -> Node { - renderer.node( - self.style, - &self.image, - self.width, - self.height, - self.source, - ) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw(&self.image, layout.bounds(), self.source); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - self.width.hash(state); - self.height.hash(state); - } -} - -/// The renderer of an [`Image`]. -/// -/// Your [renderer] will need to implement this trait before being able to use -/// an [`Image`] in your user interface. -/// -/// [`Image`]: struct.Image.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Creates a [`Node`] with the given [`Style`] for the provided [`Image`] - /// and its size. - /// - /// You should probably keep the original aspect ratio, if possible. - /// - /// [`Node`]: ../../struct.Node.html - /// [`Style`]: ../../struct.Style.html - /// [`Image`]: struct.Image.html - fn node( - &self, - style: Style, - image: &I, - width: Option, - height: Option, - source: Option>, - ) -> Node; - - /// Draws an [`Image`]. - /// - /// It receives: - /// * the bounds of the [`Image`] - /// * the handle of the loaded [`Image`] - /// * the portion of the image to draw. If not specified, the entire image - /// should be drawn. - /// - /// [`Image`]: struct.Image.html - fn draw( - &mut self, - image: &I, - bounds: Rectangle, - source: Option>, - ); -} - -impl<'a, I, Message, Renderer> From> for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, - I: Clone + 'a, -{ - fn from(image: Image) -> Element<'a, Message, Renderer> { - Element::new(image) - } -} diff --git a/src/widget/panel.rs b/src/widget/panel.rs deleted file mode 100644 index d43d6fb6..00000000 --- a/src/widget/panel.rs +++ /dev/null @@ -1,94 +0,0 @@ -use std::hash::Hash; - -use crate::graphics::{Point, Rectangle}; -use crate::ui::core::{ - Event, Hasher, Layout, MouseCursor, Node, Style, Widget, -}; - -pub struct Panel<'a, Message, Renderer> { - style: Style, - content: Box + 'a>, -} - -impl<'a, Message, Renderer> Panel<'a, Message, Renderer> { - pub fn new(content: impl Widget + 'a) -> Self { - Panel { - style: Style::default().padding(20), - content: Box::new(content), - } - } - - pub fn width(mut self, width: u32) -> Self { - self.style = self.style.width(width); - self - } - - pub fn max_width(mut self, max_width: u32) -> Self { - self.style = self.style.max_width(max_width); - self - } -} - -impl<'a, Message, Renderer> Widget - for Panel<'a, Message, Renderer> -where - Renderer: self::Renderer, -{ - fn node(&self, renderer: &Renderer) -> Node { - Node::with_children(self.style, vec![self.content.node(renderer)]) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout, - cursor_position: Point, - messages: &mut Vec, - ) { - [&mut self.content] - .iter_mut() - .zip(layout.children()) - .for_each(|(child, layout)| { - child.on_event(event, layout, cursor_position, messages) - }); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout, - cursor_position: Point, - ) -> MouseCursor { - let bounds = layout.bounds(); - let mut cursor = MouseCursor::OutOfBounds; - renderer.draw(bounds); - - [&self.content].iter().zip(layout.children()).for_each( - |(child, layout)| { - let new_cursor = child.draw(renderer, layout, cursor_position); - - if new_cursor != MouseCursor::OutOfBounds { - cursor = new_cursor; - } - }, - ); - - if cursor == MouseCursor::OutOfBounds { - if bounds.contains(cursor_position) { - MouseCursor::Idle - } else { - MouseCursor::OutOfBounds - } - } else { - cursor - } - } - - fn hash(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -pub trait Renderer { - fn draw(&mut self, bounds: Rectangle); -} diff --git a/src/widget/progress_bar.rs b/src/widget/progress_bar.rs deleted file mode 100644 index d4499160..00000000 --- a/src/widget/progress_bar.rs +++ /dev/null @@ -1,106 +0,0 @@ -//! Provide visual feedback to your users when performing a slow task. - -use crate::{ - Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, -}; - -use std::hash::Hash; - -/// A bar that is filled based on an amount of progress. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`progress_bar::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`progress_bar::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::ProgressBar; -/// -/// let progress = 0.75; -/// -/// ProgressBar::new(progress); -/// ``` -#[derive(Debug)] -pub struct ProgressBar { - progress: f32, - style: Style, -} - -impl ProgressBar { - /// Creates a new [`ProgressBar`] filled based on the given amount of - /// progress. - /// - /// The progress should be in the `0.0..=1.0` range. `0` meaning no work - /// done, and `1` meaning work finished. - /// - /// [`ProgressBar`]: struct.ProgressBar.html - pub fn new(progress: f32) -> Self { - ProgressBar { - progress, - style: Style::default().fill_width(), - } - } - - /// Sets the width of the [`ProgressBar`] in pixels. - /// - /// [`ProgressBar`]: struct.ProgressBar.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } -} - -impl Widget for ProgressBar -where - Renderer: self::Renderer, -{ - fn node(&self, _renderer: &Renderer) -> Node { - Node::new(self.style.height(50)) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw(layout.bounds(), self.progress); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -/// The renderer of a [`ProgressBar`]. -/// -/// Your [renderer] will need to implement this trait before being able to use -/// a [`ProgressBar`] in your user interface. -/// -/// [`ProgressBar`]: struct.ProgressBar.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`ProgressBar`]. - /// - /// It receives: - /// * the bounds of the [`ProgressBar`] - /// * the current progress of the [`ProgressBar`], in the `0.0..=1.0` - /// range. - /// - /// [`ProgressBar`]: struct.ProgressBar.html - fn draw(&mut self, bounds: Rectangle, progress: f32); -} - -impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, -{ - fn from(progress_bar: ProgressBar) -> Element<'a, Message, Renderer> { - Element::new(progress_bar) - } -} diff --git a/src/widget/radio.rs b/src/widget/radio.rs deleted file mode 100644 index 28353ef4..00000000 --- a/src/widget/radio.rs +++ /dev/null @@ -1,211 +0,0 @@ -//! Create choices using radio buttons. -use crate::input::{mouse, ButtonState}; -use crate::widget::{text, Column, Row, Text}; -use crate::{ - Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, - Widget, -}; - -use std::hash::Hash; - -/// A circular button representing a choice, with a generic text `Color`. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`radio::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`radio::Renderer`]: trait.Renderer.html -/// -/// # Example -/// ``` -/// use iced::{Column, Radio}; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } -/// -/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] -/// pub enum Choice { -/// A, -/// B, -/// } -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Message { -/// RadioSelected(Choice), -/// } -/// -/// let selected_choice = Some(Choice::A); -/// -/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected) -/// .label_color(Color::Black); -/// -/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected) -/// .label_color(Color::Black); -/// ``` -/// -/// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true) -pub struct Radio { - is_selected: bool, - on_click: Message, - label: String, - label_color: Option, -} - -impl std::fmt::Debug for Radio -where - Color: std::fmt::Debug, - Message: std::fmt::Debug, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Radio") - .field("is_selected", &self.is_selected) - .field("on_click", &self.on_click) - .field("label", &self.label) - .field("label_color", &self.label_color) - .finish() - } -} - -impl Radio { - /// Creates a new [`Radio`] button. - /// - /// It expects: - /// * the value related to the [`Radio`] button - /// * the label of the [`Radio`] button - /// * the current selected value - /// * a function that will be called when the [`Radio`] is selected. It - /// receives the value of the radio and must produce a `Message`. - /// - /// [`Radio`]: struct.Radio.html - pub fn new(value: V, label: &str, selected: Option, f: F) -> Self - where - V: Eq + Copy, - F: 'static + Fn(V) -> Message, - { - Radio { - is_selected: Some(value) == selected, - on_click: f(value), - label: String::from(label), - label_color: None, - } - } - - /// Sets the `Color` of the label of the [`Radio`]. - /// - /// [`Radio`]: struct.Radio.html - pub fn label_color(mut self, color: Color) -> Self { - self.label_color = Some(color); - self - } -} - -impl Widget - for Radio -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, - Message: Copy + std::fmt::Debug, -{ - fn node(&self, renderer: &Renderer) -> Node { - Row::<(), Renderer>::new() - .spacing(15) - .align_items(Align::Center) - .push(Column::new().width(28).height(28)) - .push(Text::new(&self.label)) - .node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Pressed, - }) => { - if layout.bounds().contains(cursor_position) { - messages.push(self.on_click); - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let children: Vec<_> = layout.children().collect(); - - let mut text_bounds = children[1].bounds(); - text_bounds.y -= 2.0; - - text::Renderer::draw( - renderer, - text_bounds, - &self.label, - None, - self.label_color, - text::HorizontalAlignment::Left, - text::VerticalAlignment::Top, - ); - - self::Renderer::draw( - renderer, - cursor_position, - children[0].bounds(), - layout.bounds(), - self.is_selected, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.label.hash(state); - } -} - -/// The renderer of a [`Radio`] button. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Radio`] button in your user interface. -/// -/// [`Radio`]: struct.Radio.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Radio`] button. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Radio`] - /// * the bounds of the label of the [`Radio`] - /// * whether the [`Radio`] is selected or not - /// - /// [`Radio`]: struct.Radio.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - label_bounds: Rectangle, - is_selected: bool, - ) -> MouseCursor; -} - -impl<'a, Color, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, - Message: 'static + Copy + std::fmt::Debug, -{ - fn from(checkbox: Radio) -> Element<'a, Message, Renderer> { - Element::new(checkbox) - } -} diff --git a/src/widget/row.rs b/src/widget/row.rs deleted file mode 100644 index 959528dc..00000000 --- a/src/widget/row.rs +++ /dev/null @@ -1,219 +0,0 @@ -use std::hash::Hash; - -use crate::{ - Align, Element, Event, Hasher, Justify, Layout, MouseCursor, Node, Point, - Style, Widget, -}; - -/// A container that distributes its contents horizontally. -/// -/// A [`Row`] will try to fill the horizontal space of its container. -/// -/// [`Row`]: struct.Row.html -#[derive(Default)] -pub struct Row<'a, Message, Renderer> { - style: Style, - spacing: u16, - children: Vec>, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Row<'a, Message, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Row") - .field("style", &self.style) - .field("spacing", &self.spacing) - .field("children", &self.children) - .finish() - } -} - -impl<'a, Message, Renderer> Row<'a, Message, Renderer> { - /// Creates an empty [`Row`]. - /// - /// [`Row`]: struct.Row.html - pub fn new() -> Self { - Row { - style: Style::default().fill_width(), - spacing: 0, - children: Vec::new(), - } - } - - /// Sets the horizontal spacing _between_ elements in pixels. - /// - /// Custom margins per element do not exist in Iced. You should use this - /// method instead! While less flexible, it helps you keep spacing between - /// elements consistent. - pub fn spacing(mut self, px: u16) -> Self { - self.spacing = px; - self - } - - /// Sets the padding of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn padding(mut self, px: u16) -> Self { - self.style = self.style.padding(px); - self - } - - /// Sets the width of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the maximum width of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn max_width(mut self, max_width: u16) -> Self { - self.style = self.style.max_width(max_width); - self - } - - /// Sets the maximum height of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn max_height(mut self, max_height: u16) -> Self { - self.style = self.style.max_height(max_height); - self - } - - /// Sets the alignment of the [`Row`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Row`]: struct.Row.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } - - /// Sets the vertical alignment of the contents of the [`Row`] . - /// - /// [`Row`]: struct.Row.html - pub fn align_items(mut self, align: Align) -> Self { - self.style = self.style.align_items(align); - self - } - - /// Sets the horizontal distribution strategy for the contents of the - /// [`Row`] . - /// - /// [`Row`]: struct.Row.html - pub fn justify_content(mut self, justify: Justify) -> Self { - self.style = self.style.justify_content(justify); - self - } - - /// Adds an [`Element`] to the [`Row`]. - /// - /// [`Element`]: ../struct.Element.html - /// [`Row`]: struct.Row.html - pub fn push(mut self, child: E) -> Row<'a, Message, Renderer> - where - E: Into>, - { - self.children.push(child.into()); - self - } -} - -impl<'a, Message, Renderer> Widget - for Row<'a, Message, Renderer> -{ - fn node(&self, renderer: &Renderer) -> Node { - let mut children: Vec = self - .children - .iter() - .map(|child| { - let mut node = child.widget.node(renderer); - - let mut style = node.0.style(); - style.margin.end = - stretch::style::Dimension::Points(f32::from(self.spacing)); - - node.0.set_style(style); - node - }) - .collect(); - - if let Some(node) = children.last_mut() { - let mut style = node.0.style(); - style.margin.end = stretch::style::Dimension::Undefined; - - node.0.set_style(style); - } - - Node::with_children(self.style, children) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - self.children.iter_mut().zip(layout.children()).for_each( - |(child, layout)| { - child - .widget - .on_event(event, layout, cursor_position, messages) - }, - ); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let mut cursor = MouseCursor::OutOfBounds; - - self.children.iter().zip(layout.children()).for_each( - |(child, layout)| { - let new_cursor = - child.widget.draw(renderer, layout, cursor_position); - - if new_cursor != MouseCursor::OutOfBounds { - cursor = new_cursor; - } - }, - ); - - cursor - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - self.spacing.hash(state); - - for child in &self.children { - child.widget.hash_layout(state); - } - } -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: 'a, - Message: 'static, -{ - fn from(row: Row<'a, Message, Renderer>) -> Element<'a, Message, Renderer> { - Element::new(row) - } -} diff --git a/src/widget/slider.rs b/src/widget/slider.rs deleted file mode 100644 index cdec9ec4..00000000 --- a/src/widget/slider.rs +++ /dev/null @@ -1,241 +0,0 @@ -//! Display an interactive selector of a single value from a range of values. -//! -//! A [`Slider`] has some local [`State`]. -//! -//! [`Slider`]: struct.Slider.html -//! [`State`]: struct.State.html -use std::hash::Hash; -use std::ops::RangeInclusive; - -use crate::input::{mouse, ButtonState}; -use crate::{ - Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, - Widget, -}; - -/// An horizontal bar and a handle that selects a single value from a range of -/// values. -/// -/// A [`Slider`] will try to fill the horizontal space of its container. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`slider::Renderer`] trait. -/// -/// [`Slider`]: struct.Slider.html -/// [`Widget`]: ../trait.Widget.html -/// [`slider::Renderer`]: trait.Renderer.html -/// -/// # Example -/// ``` -/// use iced::{slider, Slider}; -/// -/// pub enum Message { -/// SliderChanged(f32), -/// } -/// -/// let state = &mut slider::State::new(); -/// let value = 50.0; -/// -/// Slider::new(state, 0.0..=100.0, value, Message::SliderChanged); -/// ``` -/// -/// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true) -pub struct Slider<'a, Message> { - state: &'a mut State, - range: RangeInclusive, - value: f32, - on_change: Box Message>, - style: Style, -} - -impl<'a, Message> std::fmt::Debug for Slider<'a, Message> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Slider") - .field("state", &self.state) - .field("range", &self.range) - .field("value", &self.value) - .field("style", &self.style) - .finish() - } -} - -impl<'a, Message> Slider<'a, Message> { - /// Creates a new [`Slider`]. - /// - /// It expects: - /// * the local [`State`] of the [`Slider`] - /// * an inclusive range of possible values - /// * the current value of the [`Slider`] - /// * a function that will be called when the [`Slider`] is dragged. - /// It receives the new value of the [`Slider`] and must produce a - /// `Message`. - /// - /// [`Slider`]: struct.Slider.html - /// [`State`]: struct.State.html - pub fn new( - state: &'a mut State, - range: RangeInclusive, - value: f32, - on_change: F, - ) -> Self - where - F: 'static + Fn(f32) -> Message, - { - Slider { - state, - value: value.max(*range.start()).min(*range.end()), - range, - on_change: Box::new(on_change), - style: Style::default().min_width(100).fill_width(), - } - } - - /// Sets the width of the [`Slider`] in pixels. - /// - /// [`Slider`]: struct.Slider.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } -} - -impl<'a, Message, Renderer> Widget for Slider<'a, Message> -where - Renderer: self::Renderer, -{ - fn node(&self, _renderer: &Renderer) -> Node { - Node::new(self.style.height(25)) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - let mut change = || { - let bounds = layout.bounds(); - - if cursor_position.x <= bounds.x { - messages.push((self.on_change)(*self.range.start())); - } else if cursor_position.x >= bounds.x + bounds.width { - messages.push((self.on_change)(*self.range.end())); - } else { - let percent = (cursor_position.x - bounds.x) / bounds.width; - let value = (self.range.end() - self.range.start()) * percent - + self.range.start(); - - messages.push((self.on_change)(value)); - } - }; - - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state, - }) => match state { - ButtonState::Pressed => { - if layout.bounds().contains(cursor_position) { - change(); - self.state.is_dragging = true; - } - } - ButtonState::Released => { - self.state.is_dragging = false; - } - }, - Event::Mouse(mouse::Event::CursorMoved { .. }) => { - if self.state.is_dragging { - change(); - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - cursor_position, - layout.bounds(), - self.state, - self.range.clone(), - self.value, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -/// The local state of a [`Slider`]. -/// -/// [`Slider`]: struct.Slider.html -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub struct State { - is_dragging: bool, -} - -impl State { - /// Creates a new [`State`]. - /// - /// [`State`]: struct.State.html - pub fn new() -> State { - State::default() - } - - /// Returns whether the associated [`Slider`] is currently being dragged or - /// not. - /// - /// [`Slider`]: struct.Slider.html - pub fn is_dragging(&self) -> bool { - self.is_dragging - } -} - -/// The renderer of a [`Slider`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Slider`] in your user interface. -/// -/// [`Slider`]: struct.Slider.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Slider`]. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Slider`] - /// * the local state of the [`Slider`] - /// * the range of values of the [`Slider`] - /// * the current value of the [`Slider`] - /// - /// [`Slider`]: struct.Slider.html - /// [`State`]: struct.State.html - /// [`Class`]: enum.Class.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - state: &State, - range: RangeInclusive, - value: f32, - ) -> MouseCursor; -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, - Message: 'static, -{ - fn from(slider: Slider<'a, Message>) -> Element<'a, Message, Renderer> { - Element::new(slider) - } -} diff --git a/src/widget/text.rs b/src/widget/text.rs deleted file mode 100644 index 59b599bb..00000000 --- a/src/widget/text.rs +++ /dev/null @@ -1,224 +0,0 @@ -//! Write some text for your users to read. -use crate::{ - Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, -}; - -use std::hash::Hash; - -/// A fragment of text with a generic `Color`. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`text::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`text::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::Text; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } -/// -/// Text::new("I <3 iced!") -/// .size(40) -/// .color(Color::Black); -/// ``` -#[derive(Debug, Clone)] -pub struct Text { - content: String, - size: Option, - color: Option, - style: Style, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, -} - -impl Text { - /// Create a new fragment of [`Text`] with the given contents. - /// - /// [`Text`]: struct.Text.html - pub fn new(label: &str) -> Self { - Text { - content: String::from(label), - size: None, - color: None, - style: Style::default().fill_width(), - horizontal_alignment: HorizontalAlignment::Left, - vertical_alignment: VerticalAlignment::Top, - } - } - - /// Sets the size of the [`Text`] in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn size(mut self, size: u16) -> Self { - self.size = Some(size); - self - } - - /// Sets the `Color` of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - pub fn color(mut self, color: Color) -> Self { - self.color = Some(color); - self - } - - /// Sets the width of the [`Text`] boundaries in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Text`] boundaries in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the [`HorizontalAlignment`] of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html - pub fn horizontal_alignment( - mut self, - alignment: HorizontalAlignment, - ) -> Self { - self.horizontal_alignment = alignment; - self - } - - /// Sets the [`VerticalAlignment`] of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - /// [`VerticalAlignment`]: enum.VerticalAlignment.html - pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self { - self.vertical_alignment = alignment; - self - } -} - -impl Widget for Text -where - Color: Copy + std::fmt::Debug, - Renderer: self::Renderer, -{ - fn node(&self, renderer: &Renderer) -> Node { - renderer.node(self.style, &self.content, self.size) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - layout.bounds(), - &self.content, - self.size, - self.color, - self.horizontal_alignment, - self.vertical_alignment, - ); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - - self.content.hash(state); - self.size.hash(state); - } -} - -/// The renderer of a [`Text`] fragment with a generic `Color`. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use [`Text`] in your [`UserInterface`]. -/// -/// [`Text`]: struct.Text.html -/// [renderer]: ../../renderer/index.html -/// [`UserInterface`]: ../../struct.UserInterface.html -pub trait Renderer { - /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] - /// contents and size. - /// - /// You should probably use [`Node::with_measure`] to allow [`Text`] to - /// adapt to the dimensions of its container. - /// - /// [`Node`]: ../../struct.Node.html - /// [`Style`]: ../../struct.Style.html - /// [`Text`]: struct.Text.html - /// [`Node::with_measure`]: ../../struct.Node.html#method.with_measure - fn node(&self, style: Style, content: &str, size: Option) -> Node; - - /// Draws a [`Text`] fragment. - /// - /// It receives: - /// * the bounds of the [`Text`] - /// * the contents of the [`Text`] - /// * the size of the [`Text`] - /// * the color of the [`Text`] - /// * the [`HorizontalAlignment`] of the [`Text`] - /// * the [`VerticalAlignment`] of the [`Text`] - /// - /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html - /// [`VerticalAlignment`]: enum.VerticalAlignment.html - fn draw( - &mut self, - bounds: Rectangle, - content: &str, - size: Option, - color: Option, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, - ); -} - -impl<'a, Message, Renderer, Color> From> - for Element<'a, Message, Renderer> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer, -{ - fn from(text: Text) -> Element<'a, Message, Renderer> { - Element::new(text) - } -} - -/// The horizontal alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum HorizontalAlignment { - /// Align left - Left, - - /// Horizontally centered - Center, - - /// Align right - Right, -} - -/// The vertical alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum VerticalAlignment { - /// Align top - Top, - - /// Vertically centered - Center, - - /// Align bottom - Bottom, -} -- cgit From a97401aed2a173260a4abfdb65a77975ce6c0f01 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Sep 2019 19:16:06 +0200 Subject: Rethink workspace structure --- .gitignore | 3 +- Cargo.toml | 35 ++- core/Cargo.toml | 25 -- core/src/element.rs | 370 ----------------------- core/src/event.rs | 16 - core/src/hasher.rs | 19 -- core/src/input.rs | 7 - core/src/input/button_state.rs | 24 -- core/src/input/keyboard.rs | 6 - core/src/input/keyboard/event.rs | 23 -- core/src/input/keyboard/key_code.rs | 374 ----------------------- core/src/input/mouse.rs | 6 - core/src/input/mouse/button.rs | 32 -- core/src/input/mouse/event.rs | 44 --- core/src/layout.rs | 62 ---- core/src/lib.rs | 229 -------------- core/src/mouse_cursor.rs | 35 --- core/src/node.rs | 60 ---- core/src/point.rs | 31 -- core/src/rectangle.rs | 30 -- core/src/renderer.rs | 45 --- core/src/style.rs | 262 ---------------- core/src/user_interface.rs | 323 -------------------- core/src/vector.rs | 15 - core/src/widget.rs | 114 ------- core/src/widget/button.rs | 282 ------------------ core/src/widget/checkbox.rs | 203 ------------- core/src/widget/column.rs | 224 -------------- core/src/widget/image.rs | 178 ----------- core/src/widget/panel.rs | 94 ------ core/src/widget/progress_bar.rs | 106 ------- core/src/widget/radio.rs | 211 ------------- core/src/widget/row.rs | 219 -------------- core/src/widget/slider.rs | 241 --------------- core/src/widget/text.rs | 224 -------------- examples/Cargo.toml | 17 -- examples/README.md | 10 +- examples/tour/main.rs | 2 +- src/element.rs | 370 +++++++++++++++++++++++ src/event.rs | 16 + src/hasher.rs | 19 ++ src/input.rs | 7 + src/input/button_state.rs | 24 ++ src/input/keyboard.rs | 6 + src/input/keyboard/event.rs | 23 ++ src/input/keyboard/key_code.rs | 374 +++++++++++++++++++++++ src/input/mouse.rs | 6 + src/input/mouse/button.rs | 32 ++ src/input/mouse/event.rs | 44 +++ src/layout.rs | 62 ++++ src/lib.rs | 229 ++++++++++++++ src/mouse_cursor.rs | 35 +++ src/node.rs | 60 ++++ src/point.rs | 31 ++ src/rectangle.rs | 30 ++ src/renderer.rs | 45 +++ src/style.rs | 262 ++++++++++++++++ src/user_interface.rs | 323 ++++++++++++++++++++ src/vector.rs | 15 + src/widget.rs | 114 +++++++ src/widget/button.rs | 282 ++++++++++++++++++ src/widget/checkbox.rs | 203 +++++++++++++ src/widget/column.rs | 224 ++++++++++++++ src/widget/image.rs | 178 +++++++++++ src/widget/panel.rs | 94 ++++++ src/widget/progress_bar.rs | 106 +++++++ src/widget/radio.rs | 211 +++++++++++++ src/widget/row.rs | 219 ++++++++++++++ src/widget/slider.rs | 241 +++++++++++++++ src/widget/text.rs | 224 ++++++++++++++ web/Cargo.toml | 27 ++ web/examples/tour/Cargo.toml | 16 + web/examples/tour/index.html | 13 + web/examples/tour/src/lib.rs | 8 + web/examples/tour/src/tour.rs | 578 ++++++++++++++++++++++++++++++++++++ web/src/lib.rs | 1 + 76 files changed, 4793 insertions(+), 4160 deletions(-) delete mode 100644 core/Cargo.toml delete mode 100644 core/src/element.rs delete mode 100644 core/src/event.rs delete mode 100644 core/src/hasher.rs delete mode 100644 core/src/input.rs delete mode 100644 core/src/input/button_state.rs delete mode 100644 core/src/input/keyboard.rs delete mode 100644 core/src/input/keyboard/event.rs delete mode 100644 core/src/input/keyboard/key_code.rs delete mode 100644 core/src/input/mouse.rs delete mode 100644 core/src/input/mouse/button.rs delete mode 100644 core/src/input/mouse/event.rs delete mode 100644 core/src/layout.rs delete mode 100644 core/src/lib.rs delete mode 100644 core/src/mouse_cursor.rs delete mode 100644 core/src/node.rs delete mode 100644 core/src/point.rs delete mode 100644 core/src/rectangle.rs delete mode 100644 core/src/renderer.rs delete mode 100644 core/src/style.rs delete mode 100644 core/src/user_interface.rs delete mode 100644 core/src/vector.rs delete mode 100644 core/src/widget.rs delete mode 100644 core/src/widget/button.rs delete mode 100644 core/src/widget/checkbox.rs delete mode 100644 core/src/widget/column.rs delete mode 100644 core/src/widget/image.rs delete mode 100644 core/src/widget/panel.rs delete mode 100644 core/src/widget/progress_bar.rs delete mode 100644 core/src/widget/radio.rs delete mode 100644 core/src/widget/row.rs delete mode 100644 core/src/widget/slider.rs delete mode 100644 core/src/widget/text.rs delete mode 100644 examples/Cargo.toml create mode 100644 src/element.rs create mode 100644 src/event.rs create mode 100644 src/hasher.rs create mode 100644 src/input.rs create mode 100644 src/input/button_state.rs create mode 100644 src/input/keyboard.rs create mode 100644 src/input/keyboard/event.rs create mode 100644 src/input/keyboard/key_code.rs create mode 100644 src/input/mouse.rs create mode 100644 src/input/mouse/button.rs create mode 100644 src/input/mouse/event.rs create mode 100644 src/layout.rs create mode 100644 src/lib.rs create mode 100644 src/mouse_cursor.rs create mode 100644 src/node.rs create mode 100644 src/point.rs create mode 100644 src/rectangle.rs create mode 100644 src/renderer.rs create mode 100644 src/style.rs create mode 100644 src/user_interface.rs create mode 100644 src/vector.rs create mode 100644 src/widget.rs create mode 100644 src/widget/button.rs create mode 100644 src/widget/checkbox.rs create mode 100644 src/widget/column.rs create mode 100644 src/widget/image.rs create mode 100644 src/widget/panel.rs create mode 100644 src/widget/progress_bar.rs create mode 100644 src/widget/radio.rs create mode 100644 src/widget/row.rs create mode 100644 src/widget/slider.rs create mode 100644 src/widget/text.rs create mode 100644 web/Cargo.toml create mode 100644 web/examples/tour/Cargo.toml create mode 100644 web/examples/tour/index.html create mode 100644 web/examples/tour/src/lib.rs create mode 100644 web/examples/tour/src/tour.rs create mode 100644 web/src/lib.rs diff --git a/.gitignore b/.gitignore index 2f88dbac..99cebb8a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target +pkg/ **/*.rs.bk -Cargo.lock \ No newline at end of file +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml index 43556cbe..07a4e9ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,36 @@ +[package] +name = "iced" +version = "0.1.0-alpha" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +description = "A GUI runtime, heavily inspired by Elm." +license = "MIT" +repository = "https://github.com/hecrj/iced" +documentation = "https://docs.rs/iced" +readme = "README.md" +keywords = ["gui", "ui", "graphics", "interface", "widgets"] +categories = ["gui"] + +[badges] +maintenance = { status = "actively-developed" } + +[package.metadata.docs.rs] +features = ["winit"] + +[dependencies] +stretch = "0.2" +twox-hash = "1.5" + +# Enable to obtain conversion traits +winit = { version = "0.20.0-alpha3", optional = true } + +[dev-dependencies] +# A personal `ggez` fork that introduces a `FontCache` type to measure text +# efficiently and fixes HiDPI issues. +ggez = { version = "0.5", git = "https://github.com/hecrj/ggez.git" } + [workspace] members = [ - "core", - "examples", + "web", + "web/examples/tour", ] diff --git a/core/Cargo.toml b/core/Cargo.toml deleted file mode 100644 index cd84d03e..00000000 --- a/core/Cargo.toml +++ /dev/null @@ -1,25 +0,0 @@ -[package] -name = "iced" -version = "0.1.0-alpha" -authors = ["Héctor Ramón Jiménez "] -edition = "2018" -description = "A GUI runtime, heavily inspired by Elm." -license = "MIT" -repository = "https://github.com/hecrj/iced" -documentation = "https://docs.rs/iced" -readme = "README.md" -keywords = ["gui", "ui", "graphics", "interface", "widgets"] -categories = ["gui"] - -[badges] -maintenance = { status = "actively-developed" } - -[package.metadata.docs.rs] -features = ["winit"] - -[dependencies] -stretch = "0.2" -twox-hash = "1.5" - -# Enable to obtain conversion traits -winit = { version = "0.20.0-alpha3", optional = true } diff --git a/core/src/element.rs b/core/src/element.rs deleted file mode 100644 index 70d06f42..00000000 --- a/core/src/element.rs +++ /dev/null @@ -1,370 +0,0 @@ -use stretch::{geometry, result}; - -use crate::{ - renderer, Event, Hasher, Layout, MouseCursor, Node, Point, Widget, -}; - -/// A generic [`Widget`]. -/// -/// It is useful to build composable user interfaces that do not leak -/// implementation details in their __view logic__. -/// -/// If you have a [built-in widget], you should be able to use `Into` -/// to turn it into an [`Element`]. -/// -/// [built-in widget]: widget/index.html#built-in-widgets -/// [`Widget`]: widget/trait.Widget.html -/// [`Element`]: struct.Element.html -pub struct Element<'a, Message, Renderer> { - pub(crate) widget: Box + 'a>, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Element<'a, Message, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Element") - .field("widget", &self.widget) - .finish() - } -} - -impl<'a, Message, Renderer> Element<'a, Message, Renderer> { - /// Create a new [`Element`] containing the given [`Widget`]. - /// - /// [`Element`]: struct.Element.html - /// [`Widget`]: widget/trait.Widget.html - pub fn new( - widget: impl Widget + 'a, - ) -> Element<'a, Message, Renderer> { - Element { - widget: Box::new(widget), - } - } - - /// Applies a transformation to the produced message of the [`Element`]. - /// - /// This method is useful when you want to decouple different parts of your - /// UI and make them __composable__. - /// - /// [`Element`]: struct.Element.html - /// - /// # Example - /// Imagine we want to use [our counter](index.html#usage). But instead of - /// showing a single counter, we want to display many of them. We can reuse - /// the `Counter` type as it is! - /// - /// We use composition to model the __state__ of our new application: - /// - /// ``` - /// # mod counter { - /// # pub struct Counter; - /// # } - /// use counter::Counter; - /// - /// struct ManyCounters { - /// counters: Vec, - /// } - /// ``` - /// - /// We can store the state of multiple counters now. However, the - /// __messages__ we implemented before describe the user interactions - /// of a __single__ counter. Right now, we need to also identify which - /// counter is receiving user interactions. Can we use composition again? - /// Yes. - /// - /// ``` - /// # mod counter { - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message {} - /// # } - /// #[derive(Debug, Clone, Copy)] - /// pub enum Message { - /// Counter(usize, counter::Message) - /// } - /// ``` - /// - /// We compose the previous __messages__ with the index of the counter - /// producing them. Let's implement our __view logic__ now: - /// - /// ``` - /// # mod counter { - /// # use iced::{button, Button}; - /// # - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message {} - /// # pub struct Counter(button::State); - /// # - /// # impl Counter { - /// # pub fn view(&mut self) -> Button { - /// # Button::new(&mut self.0, "_") - /// # } - /// # } - /// # } - /// # - /// # mod iced_wgpu { - /// # use iced::{ - /// # button, MouseCursor, Node, Point, Rectangle, Style, - /// # }; - /// # pub struct Renderer; - /// # - /// # impl button::Renderer for Renderer { - /// # fn draw( - /// # &mut self, - /// # _cursor_position: Point, - /// # _bounds: Rectangle, - /// # _state: &button::State, - /// # _label: &str, - /// # _class: button::Class, - /// # ) -> MouseCursor { - /// # MouseCursor::OutOfBounds - /// # } - /// # } - /// # } - /// # - /// # use counter::Counter; - /// # - /// # struct ManyCounters { - /// # counters: Vec, - /// # } - /// # - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message { - /// # Counter(usize, counter::Message) - /// # } - /// use iced::{Element, Row}; - /// use iced_wgpu::Renderer; - /// - /// impl ManyCounters { - /// pub fn view(&mut self) -> Row { - /// // We can quickly populate a `Row` by folding over our counters - /// self.counters.iter_mut().enumerate().fold( - /// Row::new().spacing(20), - /// |row, (index, counter)| { - /// // We display the counter - /// let element: Element = - /// counter.view().into(); - /// - /// row.push( - /// // Here we turn our `Element` into - /// // an `Element` by combining the `index` and the - /// // message of the `element`. - /// element.map(move |message| Message::Counter(index, message)) - /// ) - /// } - /// ) - /// } - /// } - /// ``` - /// - /// Finally, our __update logic__ is pretty straightforward: simple - /// delegation. - /// - /// ``` - /// # mod counter { - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message {} - /// # pub struct Counter; - /// # - /// # impl Counter { - /// # pub fn update(&mut self, _message: Message) {} - /// # } - /// # } - /// # - /// # use counter::Counter; - /// # - /// # struct ManyCounters { - /// # counters: Vec, - /// # } - /// # - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message { - /// # Counter(usize, counter::Message) - /// # } - /// impl ManyCounters { - /// pub fn update(&mut self, message: Message) { - /// match message { - /// Message::Counter(index, counter_msg) => { - /// if let Some(counter) = self.counters.get_mut(index) { - /// counter.update(counter_msg); - /// } - /// } - /// } - /// } - /// } - /// ``` - pub fn map(self, f: F) -> Element<'a, B, Renderer> - where - Message: 'static + Copy, - Renderer: 'a, - B: 'static, - F: 'static + Fn(Message) -> B, - { - Element { - widget: Box::new(Map::new(self.widget, f)), - } - } - - /// Marks the [`Element`] as _to-be-explained_. - /// - /// The [`Renderer`] will explain the layout of the [`Element`] graphically. - /// This can be very useful for debugging your layout! - /// - /// [`Element`]: struct.Element.html - /// [`Renderer`]: trait.Renderer.html - pub fn explain( - self, - color: Renderer::Color, - ) -> Element<'a, Message, Renderer> - where - Message: 'static, - Renderer: 'a + renderer::Debugger, - { - Element { - widget: Box::new(Explain::new(self, color)), - } - } - - pub(crate) fn compute_layout(&self, renderer: &Renderer) -> result::Layout { - let node = self.widget.node(renderer); - - node.0.compute_layout(geometry::Size::undefined()).unwrap() - } - - pub(crate) fn hash_layout(&self, state: &mut Hasher) { - self.widget.hash_layout(state); - } -} - -struct Map<'a, A, B, Renderer> { - widget: Box + 'a>, - mapper: Box B>, -} - -impl<'a, A, B, Renderer> std::fmt::Debug for Map<'a, A, B, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Map").field("widget", &self.widget).finish() - } -} - -impl<'a, A, B, Renderer> Map<'a, A, B, Renderer> { - pub fn new( - widget: Box + 'a>, - mapper: F, - ) -> Map<'a, A, B, Renderer> - where - F: 'static + Fn(A) -> B, - { - Map { - widget, - mapper: Box::new(mapper), - } - } -} - -impl<'a, A, B, Renderer> Widget for Map<'a, A, B, Renderer> -where - A: Copy, -{ - fn node(&self, renderer: &Renderer) -> Node { - self.widget.node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - let mut original_messages = Vec::new(); - - self.widget.on_event( - event, - layout, - cursor_position, - &mut original_messages, - ); - - original_messages - .iter() - .cloned() - .for_each(|message| messages.push((self.mapper)(message))); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - self.widget.draw(renderer, layout, cursor_position) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.widget.hash_layout(state); - } -} - -struct Explain<'a, Message, Renderer: renderer::Debugger> { - element: Element<'a, Message, Renderer>, - color: Renderer::Color, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Explain<'a, Message, Renderer> -where - Renderer: renderer::Debugger, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Explain") - .field("element", &self.element) - .finish() - } -} - -impl<'a, Message, Renderer> Explain<'a, Message, Renderer> -where - Renderer: renderer::Debugger, -{ - fn new( - element: Element<'a, Message, Renderer>, - color: Renderer::Color, - ) -> Self { - Explain { element, color } - } -} - -impl<'a, Message, Renderer> Widget - for Explain<'a, Message, Renderer> -where - Renderer: renderer::Debugger, -{ - fn node(&self, renderer: &Renderer) -> Node { - self.element.widget.node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - self.element - .widget - .on_event(event, layout, cursor_position, messages) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - renderer.explain(&layout, self.color); - - self.element.widget.draw(renderer, layout, cursor_position) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.element.widget.hash_layout(state); - } -} diff --git a/core/src/event.rs b/core/src/event.rs deleted file mode 100644 index 71f06006..00000000 --- a/core/src/event.rs +++ /dev/null @@ -1,16 +0,0 @@ -use crate::input::{keyboard, mouse}; - -/// A user interface event. -/// -/// _**Note:** This type is largely incomplete! If you need to track -/// additional events, feel free to [open an issue] and share your use case!_ -/// -/// [open an issue]: https://github.com/hecrj/iced/issues -#[derive(PartialEq, Clone, Copy, Debug)] -pub enum Event { - /// A keyboard event - Keyboard(keyboard::Event), - - /// A mouse event - Mouse(mouse::Event), -} diff --git a/core/src/hasher.rs b/core/src/hasher.rs deleted file mode 100644 index 9f6aacce..00000000 --- a/core/src/hasher.rs +++ /dev/null @@ -1,19 +0,0 @@ -/// The hasher used to compare layouts. -#[derive(Debug)] -pub struct Hasher(twox_hash::XxHash64); - -impl Default for Hasher { - fn default() -> Self { - Hasher(twox_hash::XxHash64::default()) - } -} - -impl core::hash::Hasher for Hasher { - fn write(&mut self, bytes: &[u8]) { - self.0.write(bytes) - } - - fn finish(&self) -> u64 { - self.0.finish() - } -} diff --git a/core/src/input.rs b/core/src/input.rs deleted file mode 100644 index 097fa730..00000000 --- a/core/src/input.rs +++ /dev/null @@ -1,7 +0,0 @@ -//! Map your system events into input events that the runtime can understand. -pub mod keyboard; -pub mod mouse; - -mod button_state; - -pub use button_state::ButtonState; diff --git a/core/src/input/button_state.rs b/core/src/input/button_state.rs deleted file mode 100644 index e9dc05d7..00000000 --- a/core/src/input/button_state.rs +++ /dev/null @@ -1,24 +0,0 @@ -/// The state of a button. -/// -/// If you are using [`winit`], consider enabling the `winit` feature to get -/// conversion implementations for free! -/// -/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ -#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] -pub enum ButtonState { - /// The button is pressed. - Pressed, - - /// The button is __not__ pressed. - Released, -} - -#[cfg(feature = "winit")] -impl From for ButtonState { - fn from(element_state: winit::event::ElementState) -> Self { - match element_state { - winit::event::ElementState::Pressed => ButtonState::Pressed, - winit::event::ElementState::Released => ButtonState::Released, - } - } -} diff --git a/core/src/input/keyboard.rs b/core/src/input/keyboard.rs deleted file mode 100644 index 57c24484..00000000 --- a/core/src/input/keyboard.rs +++ /dev/null @@ -1,6 +0,0 @@ -//! Build keyboard events. -mod event; -mod key_code; - -pub use event::Event; -pub use key_code::KeyCode; diff --git a/core/src/input/keyboard/event.rs b/core/src/input/keyboard/event.rs deleted file mode 100644 index 8118f112..00000000 --- a/core/src/input/keyboard/event.rs +++ /dev/null @@ -1,23 +0,0 @@ -use super::KeyCode; -use crate::input::ButtonState; - -#[derive(Debug, Clone, Copy, PartialEq)] -/// A keyboard event. -/// -/// _**Note:** This type is largely incomplete! If you need to track -/// additional events, feel free to [open an issue] and share your use case!_ -/// -/// [open an issue]: https://github.com/hecrj/iced/issues -pub enum Event { - /// A keyboard key was pressed or released. - Input { - /// The state of the key - state: ButtonState, - - /// The key identifier - key_code: KeyCode, - }, - - /// A unicode character was received. - CharacterReceived(char), -} diff --git a/core/src/input/keyboard/key_code.rs b/core/src/input/keyboard/key_code.rs deleted file mode 100644 index 207ddeac..00000000 --- a/core/src/input/keyboard/key_code.rs +++ /dev/null @@ -1,374 +0,0 @@ -/// The symbolic name of a keyboard key. -/// -/// This is mostly the `KeyCode` type found in [`winit`]. -/// -/// If you are using [`winit`], consider enabling the `winit` feature to get -/// conversion implementations for free! -/// -/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ -#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] -#[repr(u32)] -#[allow(missing_docs)] -pub enum KeyCode { - /// The '1' key over the letters. - Key1, - /// The '2' key over the letters. - Key2, - /// The '3' key over the letters. - Key3, - /// The '4' key over the letters. - Key4, - /// The '5' key over the letters. - Key5, - /// The '6' key over the letters. - Key6, - /// The '7' key over the letters. - Key7, - /// The '8' key over the letters. - Key8, - /// The '9' key over the letters. - Key9, - /// The '0' key over the 'O' and 'P' keys. - Key0, - - A, - B, - C, - D, - E, - F, - G, - H, - I, - J, - K, - L, - M, - N, - O, - P, - Q, - R, - S, - T, - U, - V, - W, - X, - Y, - Z, - - /// The Escape key, next to F1 - Escape, - - F1, - F2, - F3, - F4, - F5, - F6, - F7, - F8, - F9, - F10, - F11, - F12, - F13, - F14, - F15, - F16, - F17, - F18, - F19, - F20, - F21, - F22, - F23, - F24, - - /// Print Screen/SysRq - Snapshot, - /// Scroll Lock - Scroll, - /// Pause/Break key, next to Scroll lock - Pause, - - /// `Insert`, next to Backspace - Insert, - Home, - Delete, - End, - PageDown, - PageUp, - - Left, - Up, - Right, - Down, - - Backspace, - Enter, - Space, - - /// The "Compose" key on Linux - Compose, - - Caret, - - Numlock, - Numpad0, - Numpad1, - Numpad2, - Numpad3, - Numpad4, - Numpad5, - Numpad6, - Numpad7, - Numpad8, - Numpad9, - - AbntC1, - AbntC2, - Add, - Apostrophe, - Apps, - At, - Ax, - Backslash, - Calculator, - Capital, - Colon, - Comma, - Convert, - Decimal, - Divide, - Equals, - Grave, - Kana, - Kanji, - LAlt, - LBracket, - LControl, - LShift, - LWin, - Mail, - MediaSelect, - MediaStop, - Minus, - Multiply, - Mute, - MyComputer, - NavigateForward, // also called "Prior" - NavigateBackward, // also called "Next" - NextTrack, - NoConvert, - NumpadComma, - NumpadEnter, - NumpadEquals, - OEM102, - Period, - PlayPause, - Power, - PrevTrack, - RAlt, - RBracket, - RControl, - RShift, - RWin, - Semicolon, - Slash, - Sleep, - Stop, - Subtract, - Sysrq, - Tab, - Underline, - Unlabeled, - VolumeDown, - VolumeUp, - Wake, - WebBack, - WebFavorites, - WebForward, - WebHome, - WebRefresh, - WebSearch, - WebStop, - Yen, - Copy, - Paste, - Cut, -} - -#[cfg(feature = "winit")] -impl From for KeyCode { - fn from(virtual_keycode: winit::event::VirtualKeyCode) -> Self { - match virtual_keycode { - winit::event::VirtualKeyCode::Key1 => KeyCode::Key1, - winit::event::VirtualKeyCode::Key2 => KeyCode::Key2, - winit::event::VirtualKeyCode::Key3 => KeyCode::Key3, - winit::event::VirtualKeyCode::Key4 => KeyCode::Key4, - winit::event::VirtualKeyCode::Key5 => KeyCode::Key5, - winit::event::VirtualKeyCode::Key6 => KeyCode::Key6, - winit::event::VirtualKeyCode::Key7 => KeyCode::Key7, - winit::event::VirtualKeyCode::Key8 => KeyCode::Key8, - winit::event::VirtualKeyCode::Key9 => KeyCode::Key9, - winit::event::VirtualKeyCode::Key0 => KeyCode::Key0, - winit::event::VirtualKeyCode::A => KeyCode::A, - winit::event::VirtualKeyCode::B => KeyCode::B, - winit::event::VirtualKeyCode::C => KeyCode::C, - winit::event::VirtualKeyCode::D => KeyCode::D, - winit::event::VirtualKeyCode::E => KeyCode::E, - winit::event::VirtualKeyCode::F => KeyCode::F, - winit::event::VirtualKeyCode::G => KeyCode::G, - winit::event::VirtualKeyCode::H => KeyCode::H, - winit::event::VirtualKeyCode::I => KeyCode::I, - winit::event::VirtualKeyCode::J => KeyCode::J, - winit::event::VirtualKeyCode::K => KeyCode::K, - winit::event::VirtualKeyCode::L => KeyCode::L, - winit::event::VirtualKeyCode::M => KeyCode::M, - winit::event::VirtualKeyCode::N => KeyCode::N, - winit::event::VirtualKeyCode::O => KeyCode::O, - winit::event::VirtualKeyCode::P => KeyCode::P, - winit::event::VirtualKeyCode::Q => KeyCode::Q, - winit::event::VirtualKeyCode::R => KeyCode::R, - winit::event::VirtualKeyCode::S => KeyCode::S, - winit::event::VirtualKeyCode::T => KeyCode::T, - winit::event::VirtualKeyCode::U => KeyCode::U, - winit::event::VirtualKeyCode::V => KeyCode::V, - winit::event::VirtualKeyCode::W => KeyCode::W, - winit::event::VirtualKeyCode::X => KeyCode::X, - winit::event::VirtualKeyCode::Y => KeyCode::Y, - winit::event::VirtualKeyCode::Z => KeyCode::Z, - winit::event::VirtualKeyCode::Escape => KeyCode::Escape, - winit::event::VirtualKeyCode::F1 => KeyCode::F1, - winit::event::VirtualKeyCode::F2 => KeyCode::F2, - winit::event::VirtualKeyCode::F3 => KeyCode::F3, - winit::event::VirtualKeyCode::F4 => KeyCode::F4, - winit::event::VirtualKeyCode::F5 => KeyCode::F5, - winit::event::VirtualKeyCode::F6 => KeyCode::F6, - winit::event::VirtualKeyCode::F7 => KeyCode::F7, - winit::event::VirtualKeyCode::F8 => KeyCode::F8, - winit::event::VirtualKeyCode::F9 => KeyCode::F9, - winit::event::VirtualKeyCode::F10 => KeyCode::F10, - winit::event::VirtualKeyCode::F11 => KeyCode::F11, - winit::event::VirtualKeyCode::F12 => KeyCode::F12, - winit::event::VirtualKeyCode::F13 => KeyCode::F13, - winit::event::VirtualKeyCode::F14 => KeyCode::F14, - winit::event::VirtualKeyCode::F15 => KeyCode::F15, - winit::event::VirtualKeyCode::F16 => KeyCode::F16, - winit::event::VirtualKeyCode::F17 => KeyCode::F17, - winit::event::VirtualKeyCode::F18 => KeyCode::F18, - winit::event::VirtualKeyCode::F19 => KeyCode::F19, - winit::event::VirtualKeyCode::F20 => KeyCode::F20, - winit::event::VirtualKeyCode::F21 => KeyCode::F21, - winit::event::VirtualKeyCode::F22 => KeyCode::F22, - winit::event::VirtualKeyCode::F23 => KeyCode::F23, - winit::event::VirtualKeyCode::F24 => KeyCode::F24, - winit::event::VirtualKeyCode::Snapshot => KeyCode::Snapshot, - winit::event::VirtualKeyCode::Scroll => KeyCode::Scroll, - winit::event::VirtualKeyCode::Pause => KeyCode::Pause, - winit::event::VirtualKeyCode::Insert => KeyCode::Insert, - winit::event::VirtualKeyCode::Home => KeyCode::Home, - winit::event::VirtualKeyCode::Delete => KeyCode::Delete, - winit::event::VirtualKeyCode::End => KeyCode::End, - winit::event::VirtualKeyCode::PageDown => KeyCode::PageDown, - winit::event::VirtualKeyCode::PageUp => KeyCode::PageUp, - winit::event::VirtualKeyCode::Left => KeyCode::Left, - winit::event::VirtualKeyCode::Up => KeyCode::Up, - winit::event::VirtualKeyCode::Right => KeyCode::Right, - winit::event::VirtualKeyCode::Down => KeyCode::Down, - winit::event::VirtualKeyCode::Back => KeyCode::Backspace, - winit::event::VirtualKeyCode::Return => KeyCode::Enter, - winit::event::VirtualKeyCode::Space => KeyCode::Space, - winit::event::VirtualKeyCode::Compose => KeyCode::Compose, - winit::event::VirtualKeyCode::Caret => KeyCode::Caret, - winit::event::VirtualKeyCode::Numlock => KeyCode::Numlock, - winit::event::VirtualKeyCode::Numpad0 => KeyCode::Numpad0, - winit::event::VirtualKeyCode::Numpad1 => KeyCode::Numpad1, - winit::event::VirtualKeyCode::Numpad2 => KeyCode::Numpad2, - winit::event::VirtualKeyCode::Numpad3 => KeyCode::Numpad3, - winit::event::VirtualKeyCode::Numpad4 => KeyCode::Numpad4, - winit::event::VirtualKeyCode::Numpad5 => KeyCode::Numpad5, - winit::event::VirtualKeyCode::Numpad6 => KeyCode::Numpad6, - winit::event::VirtualKeyCode::Numpad7 => KeyCode::Numpad7, - winit::event::VirtualKeyCode::Numpad8 => KeyCode::Numpad8, - winit::event::VirtualKeyCode::Numpad9 => KeyCode::Numpad9, - winit::event::VirtualKeyCode::AbntC1 => KeyCode::AbntC1, - winit::event::VirtualKeyCode::AbntC2 => KeyCode::AbntC2, - winit::event::VirtualKeyCode::Add => KeyCode::Add, - winit::event::VirtualKeyCode::Apostrophe => KeyCode::Apostrophe, - winit::event::VirtualKeyCode::Apps => KeyCode::Apps, - winit::event::VirtualKeyCode::At => KeyCode::At, - winit::event::VirtualKeyCode::Ax => KeyCode::Ax, - winit::event::VirtualKeyCode::Backslash => KeyCode::Backslash, - winit::event::VirtualKeyCode::Calculator => KeyCode::Calculator, - winit::event::VirtualKeyCode::Capital => KeyCode::Capital, - winit::event::VirtualKeyCode::Colon => KeyCode::Colon, - winit::event::VirtualKeyCode::Comma => KeyCode::Comma, - winit::event::VirtualKeyCode::Convert => KeyCode::Convert, - winit::event::VirtualKeyCode::Decimal => KeyCode::Decimal, - winit::event::VirtualKeyCode::Divide => KeyCode::Divide, - winit::event::VirtualKeyCode::Equals => KeyCode::Equals, - winit::event::VirtualKeyCode::Grave => KeyCode::Grave, - winit::event::VirtualKeyCode::Kana => KeyCode::Kana, - winit::event::VirtualKeyCode::Kanji => KeyCode::Kanji, - winit::event::VirtualKeyCode::LAlt => KeyCode::LAlt, - winit::event::VirtualKeyCode::LBracket => KeyCode::LBracket, - winit::event::VirtualKeyCode::LControl => KeyCode::LControl, - winit::event::VirtualKeyCode::LShift => KeyCode::LShift, - winit::event::VirtualKeyCode::LWin => KeyCode::LWin, - winit::event::VirtualKeyCode::Mail => KeyCode::Mail, - winit::event::VirtualKeyCode::MediaSelect => KeyCode::MediaSelect, - winit::event::VirtualKeyCode::MediaStop => KeyCode::MediaStop, - winit::event::VirtualKeyCode::Minus => KeyCode::Minus, - winit::event::VirtualKeyCode::Multiply => KeyCode::Multiply, - winit::event::VirtualKeyCode::Mute => KeyCode::Mute, - winit::event::VirtualKeyCode::MyComputer => KeyCode::MyComputer, - winit::event::VirtualKeyCode::NavigateForward => { - KeyCode::NavigateForward - } - winit::event::VirtualKeyCode::NavigateBackward => { - KeyCode::NavigateBackward - } - winit::event::VirtualKeyCode::NextTrack => KeyCode::NextTrack, - winit::event::VirtualKeyCode::NoConvert => KeyCode::NoConvert, - winit::event::VirtualKeyCode::NumpadComma => KeyCode::NumpadComma, - winit::event::VirtualKeyCode::NumpadEnter => KeyCode::NumpadEnter, - winit::event::VirtualKeyCode::NumpadEquals => KeyCode::NumpadEquals, - winit::event::VirtualKeyCode::OEM102 => KeyCode::OEM102, - winit::event::VirtualKeyCode::Period => KeyCode::Period, - winit::event::VirtualKeyCode::PlayPause => KeyCode::PlayPause, - winit::event::VirtualKeyCode::Power => KeyCode::Power, - winit::event::VirtualKeyCode::PrevTrack => KeyCode::PrevTrack, - winit::event::VirtualKeyCode::RAlt => KeyCode::RAlt, - winit::event::VirtualKeyCode::RBracket => KeyCode::RBracket, - winit::event::VirtualKeyCode::RControl => KeyCode::RControl, - winit::event::VirtualKeyCode::RShift => KeyCode::RShift, - winit::event::VirtualKeyCode::RWin => KeyCode::RWin, - winit::event::VirtualKeyCode::Semicolon => KeyCode::Semicolon, - winit::event::VirtualKeyCode::Slash => KeyCode::Slash, - winit::event::VirtualKeyCode::Sleep => KeyCode::Sleep, - winit::event::VirtualKeyCode::Stop => KeyCode::Stop, - winit::event::VirtualKeyCode::Subtract => KeyCode::Subtract, - winit::event::VirtualKeyCode::Sysrq => KeyCode::Sysrq, - winit::event::VirtualKeyCode::Tab => KeyCode::Tab, - winit::event::VirtualKeyCode::Underline => KeyCode::Underline, - winit::event::VirtualKeyCode::Unlabeled => KeyCode::Unlabeled, - winit::event::VirtualKeyCode::VolumeDown => KeyCode::VolumeDown, - winit::event::VirtualKeyCode::VolumeUp => KeyCode::VolumeUp, - winit::event::VirtualKeyCode::Wake => KeyCode::Wake, - winit::event::VirtualKeyCode::WebBack => KeyCode::WebBack, - winit::event::VirtualKeyCode::WebFavorites => KeyCode::WebFavorites, - winit::event::VirtualKeyCode::WebForward => KeyCode::WebForward, - winit::event::VirtualKeyCode::WebHome => KeyCode::WebHome, - winit::event::VirtualKeyCode::WebRefresh => KeyCode::WebRefresh, - winit::event::VirtualKeyCode::WebSearch => KeyCode::WebSearch, - winit::event::VirtualKeyCode::WebStop => KeyCode::WebStop, - winit::event::VirtualKeyCode::Yen => KeyCode::Yen, - winit::event::VirtualKeyCode::Copy => KeyCode::Copy, - winit::event::VirtualKeyCode::Paste => KeyCode::Paste, - winit::event::VirtualKeyCode::Cut => KeyCode::Cut, - } - } -} diff --git a/core/src/input/mouse.rs b/core/src/input/mouse.rs deleted file mode 100644 index d37f5b96..00000000 --- a/core/src/input/mouse.rs +++ /dev/null @@ -1,6 +0,0 @@ -//! Build mouse events. -mod button; -mod event; - -pub use button::Button; -pub use event::Event; diff --git a/core/src/input/mouse/button.rs b/core/src/input/mouse/button.rs deleted file mode 100644 index 6320d701..00000000 --- a/core/src/input/mouse/button.rs +++ /dev/null @@ -1,32 +0,0 @@ -/// The button of a mouse. -/// -/// If you are using [`winit`], consider enabling the `winit` feature to get -/// conversion implementations for free! -/// -/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ -#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] -pub enum Button { - /// The left mouse button. - Left, - - /// The right mouse button. - Right, - - /// The middle (wheel) button. - Middle, - - /// Some other button. - Other(u8), -} - -#[cfg(feature = "winit")] -impl From for super::Button { - fn from(mouse_button: winit::event::MouseButton) -> Self { - match mouse_button { - winit::event::MouseButton::Left => Button::Left, - winit::event::MouseButton::Right => Button::Right, - winit::event::MouseButton::Middle => Button::Middle, - winit::event::MouseButton::Other(other) => Button::Other(other), - } - } -} diff --git a/core/src/input/mouse/event.rs b/core/src/input/mouse/event.rs deleted file mode 100644 index 7b68208f..00000000 --- a/core/src/input/mouse/event.rs +++ /dev/null @@ -1,44 +0,0 @@ -use super::Button; -use crate::input::ButtonState; - -/// A mouse event. -/// -/// _**Note:** This type is largely incomplete! If you need to track -/// additional events, feel free to [open an issue] and share your use case!_ -/// -/// [open an issue]: https://github.com/hecrj/iced/issues -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum Event { - /// The mouse cursor entered the window. - CursorEntered, - - /// The mouse cursor left the window. - CursorLeft, - - /// The mouse cursor was moved - CursorMoved { - /// The X coordinate of the mouse position - x: f32, - - /// The Y coordinate of the mouse position - y: f32, - }, - - /// A mouse button was pressed or released. - Input { - /// The state of the button - state: ButtonState, - - /// The button identifier - button: Button, - }, - - /// The mouse wheel was scrolled. - WheelScrolled { - /// The number of horizontal lines scrolled - delta_x: f32, - - /// The number of vertical lines scrolled - delta_y: f32, - }, -} diff --git a/core/src/layout.rs b/core/src/layout.rs deleted file mode 100644 index de284a43..00000000 --- a/core/src/layout.rs +++ /dev/null @@ -1,62 +0,0 @@ -use stretch::result; - -use crate::{Point, Rectangle, Vector}; - -/// The computed bounds of a [`Node`] and its children. -/// -/// This type is provided by the GUI runtime to [`Widget::on_event`] and -/// [`Widget::draw`], describing the layout of the [`Node`] produced by -/// [`Widget::node`]. -/// -/// [`Node`]: struct.Node.html -/// [`Widget::on_event`]: widget/trait.Widget.html#method.on_event -/// [`Widget::draw`]: widget/trait.Widget.html#tymethod.draw -/// [`Widget::node`]: widget/trait.Widget.html#tymethod.node -#[derive(Debug)] -pub struct Layout<'a> { - layout: &'a result::Layout, - position: Point, -} - -impl<'a> Layout<'a> { - pub(crate) fn new(layout: &'a result::Layout) -> Self { - Self::with_parent_position(layout, Point::new(0.0, 0.0)) - } - - fn with_parent_position( - layout: &'a result::Layout, - parent_position: Point, - ) -> Self { - let position = - parent_position + Vector::new(layout.location.x, layout.location.y); - - Layout { layout, position } - } - - /// Gets the bounds of the [`Layout`]. - /// - /// The returned [`Rectangle`] describes the position and size of a - /// [`Node`]. - /// - /// [`Layout`]: struct.Layout.html - /// [`Rectangle`]: struct.Rectangle.html - /// [`Node`]: struct.Node.html - pub fn bounds(&self) -> Rectangle { - Rectangle { - x: self.position.x, - y: self.position.y, - width: self.layout.size.width, - height: self.layout.size.height, - } - } - - /// Returns an iterator over the [`Layout`] of the children of a [`Node`]. - /// - /// [`Layout`]: struct.Layout.html - /// [`Node`]: struct.Node.html - pub fn children(&'a self) -> impl Iterator> { - self.layout.children.iter().map(move |layout| { - Layout::with_parent_position(layout, self.position) - }) - } -} diff --git a/core/src/lib.rs b/core/src/lib.rs deleted file mode 100644 index c1c18b41..00000000 --- a/core/src/lib.rs +++ /dev/null @@ -1,229 +0,0 @@ -//! Iced is a renderer-agnostic GUI library focused on simplicity and -//! type-safety. Inspired by [Elm]. -//! -//! # Features -//! * Simple, easy-to-use, renderer-agnostic API -//! * Responsive, flexbox-based layouting -//! * Type-safe, reactive programming model -//! * Built-in widgets -//! * Custom widget support -//! -//! Check out the [repository] and the [examples] for more details! -//! -//! [examples]: https://github.com/hecrj/iced/tree/0.1.0/examples -//! [repository]: https://github.com/hecrj/iced -//! -//! # Usage -//! Inspired by [The Elm Architecture], Iced expects you to split user interfaces -//! into four different concepts: -//! -//! * __State__ — the state of your application -//! * __Messages__ — user interactions or meaningful events that you care -//! about -//! * __View logic__ — a way to display your __state__ as widgets that -//! may produce __messages__ on user interaction -//! * __Update logic__ — a way to react to __messages__ and update your -//! __state__ -//! -//! We can build something to see how this works! Let's say we want a simple counter -//! that can be incremented and decremented using two buttons. -//! -//! We start by modelling the __state__ of our application: -//! -//! ``` -//! use iced::button; -//! -//! struct Counter { -//! // The counter value -//! value: i32, -//! -//! // The local state of the two buttons -//! increment_button: button::State, -//! decrement_button: button::State, -//! } -//! ``` -//! -//! Next, we need to define the possible user interactions of our counter: -//! the button presses. These interactions are our __messages__: -//! -//! ``` -//! #[derive(Debug, Clone, Copy)] -//! pub enum Message { -//! IncrementPressed, -//! DecrementPressed, -//! } -//! ``` -//! -//! Now, let's show the actual counter by putting it all together in our -//! __view logic__: -//! -//! ``` -//! # use iced::button; -//! # -//! # struct Counter { -//! # // The counter value -//! # value: i32, -//! # -//! # // The local state of the two buttons -//! # increment_button: button::State, -//! # decrement_button: button::State, -//! # } -//! # -//! # #[derive(Debug, Clone, Copy)] -//! # pub enum Message { -//! # IncrementPressed, -//! # DecrementPressed, -//! # } -//! # -//! # mod iced_wgpu { -//! # use iced::{ -//! # button, text, text::HorizontalAlignment, text::VerticalAlignment, -//! # MouseCursor, Node, Point, Rectangle, Style, -//! # }; -//! # -//! # pub struct Renderer {} -//! # -//! # impl button::Renderer for Renderer { -//! # fn draw( -//! # &mut self, -//! # _cursor_position: Point, -//! # _bounds: Rectangle, -//! # _state: &button::State, -//! # _label: &str, -//! # _class: button::Class, -//! # ) -> MouseCursor { -//! # MouseCursor::OutOfBounds -//! # } -//! # } -//! # -//! # impl text::Renderer<[f32; 4]> for Renderer { -//! # fn node(&self, style: Style, _content: &str, _size: Option) -> Node { -//! # Node::new(style) -//! # } -//! # -//! # fn draw( -//! # &mut self, -//! # _bounds: Rectangle, -//! # _content: &str, -//! # _size: Option, -//! # _color: Option<[f32; 4]>, -//! # _horizontal_alignment: HorizontalAlignment, -//! # _vertical_alignment: VerticalAlignment, -//! # ) { -//! # } -//! # } -//! # } -//! use iced::{Button, Column, Text}; -//! use iced_wgpu::Renderer; // Iced does not include a renderer! We need to bring our own! -//! -//! impl Counter { -//! pub fn view(&mut self) -> Column { -//! // We use a column: a simple vertical layout -//! Column::new() -//! .push( -//! // The increment button. We tell it to produce an -//! // `IncrementPressed` message when pressed -//! Button::new(&mut self.increment_button, "+") -//! .on_press(Message::IncrementPressed), -//! ) -//! .push( -//! // We show the value of the counter here -//! Text::new(&self.value.to_string()).size(50), -//! ) -//! .push( -//! // The decrement button. We tell it to produce a -//! // `DecrementPressed` message when pressed -//! Button::new(&mut self.decrement_button, "-") -//! .on_press(Message::DecrementPressed), -//! ) -//! } -//! } -//! ``` -//! -//! Finally, we need to be able to react to any produced __messages__ and change -//! our __state__ accordingly in our __update logic__: -//! -//! ``` -//! # use iced::button; -//! # -//! # struct Counter { -//! # // The counter value -//! # value: i32, -//! # -//! # // The local state of the two buttons -//! # increment_button: button::State, -//! # decrement_button: button::State, -//! # } -//! # -//! # #[derive(Debug, Clone, Copy)] -//! # pub enum Message { -//! # IncrementPressed, -//! # DecrementPressed, -//! # } -//! impl Counter { -//! // ... -//! -//! pub fn update(&mut self, message: Message) { -//! match message { -//! Message::IncrementPressed => { -//! self.value += 1; -//! } -//! Message::DecrementPressed => { -//! self.value -= 1; -//! } -//! } -//! } -//! } -//! ``` -//! -//! And that's everything! We just wrote a whole user interface. Iced is now able -//! to: -//! -//! 1. Take the result of our __view logic__ and layout its widgets. -//! 1. Process events from our system and produce __messages__ for our -//! __update logic__. -//! 1. Draw the resulting user interface using our chosen __renderer__. -//! -//! Check out the [`UserInterface`] type to learn how to wire everything up! -//! -//! [Elm]: https://elm-lang.org/ -//! [The Elm Architecture]: https://guide.elm-lang.org/architecture/ -//! [documentation]: https://docs.rs/iced -//! [examples]: https://github.com/hecrj/iced/tree/master/examples -//! [`UserInterface`]: struct.UserInterface.html -#![deny(missing_docs)] -#![deny(missing_debug_implementations)] -#![deny(unused_results)] -#![deny(unsafe_code)] -#![deny(rust_2018_idioms)] -pub mod input; -pub mod renderer; -pub mod widget; - -mod element; -mod event; -mod hasher; -mod layout; -mod mouse_cursor; -mod node; -mod point; -mod rectangle; -mod style; -mod user_interface; -mod vector; - -#[doc(no_inline)] -pub use stretch::{geometry::Size, number::Number}; - -pub use element::Element; -pub use event::Event; -pub use hasher::Hasher; -pub use layout::Layout; -pub use mouse_cursor::MouseCursor; -pub use node::Node; -pub use point::Point; -pub use rectangle::Rectangle; -pub use style::{Align, Justify, Style}; -pub use user_interface::{Cache, UserInterface}; -pub(crate) use vector::Vector; -pub use widget::*; diff --git a/core/src/mouse_cursor.rs b/core/src/mouse_cursor.rs deleted file mode 100644 index 4ef6361a..00000000 --- a/core/src/mouse_cursor.rs +++ /dev/null @@ -1,35 +0,0 @@ -/// The state of the mouse cursor. -#[derive(Debug, Eq, PartialEq, Clone, Copy)] -pub enum MouseCursor { - /// The cursor is out of the bounds of the user interface. - OutOfBounds, - - /// The cursor is over a non-interactive widget. - Idle, - - /// The cursor is over a clickable widget. - Pointer, - - /// The cursor is over a busy widget. - Working, - - /// The cursor is over a grabbable widget. - Grab, - - /// The cursor is grabbing a widget. - Grabbing, -} - -#[cfg(feature = "winit")] -impl From for winit::window::CursorIcon { - fn from(mouse_cursor: MouseCursor) -> winit::window::CursorIcon { - match mouse_cursor { - MouseCursor::OutOfBounds => winit::window::CursorIcon::Default, - MouseCursor::Idle => winit::window::CursorIcon::Default, - MouseCursor::Pointer => winit::window::CursorIcon::Hand, - MouseCursor::Working => winit::window::CursorIcon::Progress, - MouseCursor::Grab => winit::window::CursorIcon::Grab, - MouseCursor::Grabbing => winit::window::CursorIcon::Grabbing, - } - } -} diff --git a/core/src/node.rs b/core/src/node.rs deleted file mode 100644 index 1db10d7f..00000000 --- a/core/src/node.rs +++ /dev/null @@ -1,60 +0,0 @@ -use stretch::node; - -use crate::{Number, Size, Style}; - -/// The visual requirements of a [`Widget`] and its children. -/// -/// When there have been changes and the [`Layout`] needs to be recomputed, the -/// runtime obtains a [`Node`] by calling [`Widget::node`]. -/// -/// [`Style`]: struct.Style.html -/// [`Widget`]: widget/trait.Widget.html -/// [`Node`]: struct.Node.html -/// [`Widget::node`]: widget/trait.Widget.html#tymethod.node -/// [`Layout`]: struct.Layout.html -#[derive(Debug)] -pub struct Node(pub(crate) node::Node); - -impl Node { - /// Creates a new [`Node`] with the given [`Style`]. - /// - /// [`Node`]: struct.Node.html - /// [`Style`]: struct.Style.html - pub fn new(style: Style) -> Node { - Self::with_children(style, Vec::new()) - } - - /// Creates a new [`Node`] with the given [`Style`] and a measure function. - /// - /// This type of node cannot have any children. - /// - /// You should use this when your [`Widget`] can adapt its contents to the - /// size of its container. The measure function will receive the container - /// size as a parameter and must compute the size of the [`Node`] inside - /// the given bounds (if the `Number` for a dimension is `Undefined` it - /// means that it has no boundary). - /// - /// [`Node`]: struct.Node.html - /// [`Style`]: struct.Style.html - /// [`Widget`]: widget/trait.Widget.html - pub fn with_measure(style: Style, measure: F) -> Node - where - F: 'static + Fn(Size) -> Size, - { - Node(node::Node::new_leaf( - style.0, - Box::new(move |size| Ok(measure(size))), - )) - } - - /// Creates a new [`Node`] with the given [`Style`] and children. - /// - /// [`Node`]: struct.Node.html - /// [`Style`]: struct.Style.html - pub fn with_children(style: Style, children: Vec) -> Node { - Node(node::Node::new( - style.0, - children.iter().map(|c| &c.0).collect(), - )) - } -} diff --git a/core/src/point.rs b/core/src/point.rs deleted file mode 100644 index 183998dd..00000000 --- a/core/src/point.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::Vector; - -/// A 2D point. -#[derive(Debug, Clone, Copy, PartialEq)] -pub struct Point { - /// The X coordinate. - pub x: f32, - - /// The Y coordinate. - pub y: f32, -} - -impl Point { - /// Creates a new [`Point`] with the given coordinates. - /// - /// [`Point`]: struct.Point.html - pub fn new(x: f32, y: f32) -> Self { - Self { x, y } - } -} - -impl std::ops::Add for Point { - type Output = Self; - - fn add(self, vector: Vector) -> Self { - Self { - x: self.x + vector.x, - y: self.y + vector.y, - } - } -} diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs deleted file mode 100644 index 95c2570c..00000000 --- a/core/src/rectangle.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::Point; - -/// A rectangle. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Rectangle { - /// X coordinate of the top-left corner. - pub x: T, - - /// Y coordinate of the top-left corner. - pub y: T, - - /// Width of the rectangle. - pub width: T, - - /// Height of the rectangle. - pub height: T, -} - -impl Rectangle { - /// Returns true if the given [`Point`] is contained in the [`Rectangle`]. - /// - /// [`Point`]: struct.Point.html - /// [`Rectangle`]: struct.Rectangle.html - pub fn contains(&self, point: Point) -> bool { - self.x <= point.x - && point.x <= self.x + self.width - && self.y <= point.y - && point.y <= self.y + self.height - } -} diff --git a/core/src/renderer.rs b/core/src/renderer.rs deleted file mode 100644 index b445190b..00000000 --- a/core/src/renderer.rs +++ /dev/null @@ -1,45 +0,0 @@ -//! Write your own renderer. -//! -//! There is not a common entrypoint or trait for a __renderer__ in Iced. -//! Instead, every [`Widget`] constrains its generic `Renderer` type as -//! necessary. -//! -//! This approach is flexible and composable. For instance, the -//! [`Text`] widget only needs a [`text::Renderer`] while a [`Checkbox`] widget -//! needs both a [`text::Renderer`] and a [`checkbox::Renderer`], reusing logic. -//! -//! In the end, a __renderer__ satisfying all the constraints is -//! needed to build a [`UserInterface`]. -//! -//! [`Widget`]: ../widget/trait.Widget.html -//! [`UserInterface`]: ../struct.UserInterface.html -//! [`Text`]: ../widget/text/struct.Text.html -//! [`text::Renderer`]: ../widget/text/trait.Renderer.html -//! [`Checkbox`]: ../widget/checkbox/struct.Checkbox.html -//! [`checkbox::Renderer`]: ../widget/checkbox/trait.Renderer.html -use crate::Layout; - -/// A renderer able to graphically explain a [`Layout`]. -/// -/// [`Layout`]: ../struct.Layout.html -pub trait Debugger { - /// The color type that will be used to configure the _explanation_. - /// - /// This is the type that will be asked in [`Element::explain`]. - /// - /// [`Element::explain`]: ../struct.Element.html#method.explain - type Color: Copy; - - /// Explains the [`Layout`] of an [`Element`] for debugging purposes. - /// - /// This will be called when [`Element::explain`] has been used. It should - /// _explain_ the given [`Layout`] graphically. - /// - /// A common approach consists in recursively rendering the bounds of the - /// [`Layout`] and its children. - /// - /// [`Layout`]: struct.Layout.html - /// [`Element`]: struct.Element.html - /// [`Element::explain`]: struct.Element.html#method.explain - fn explain(&mut self, layout: &Layout<'_>, color: Self::Color); -} diff --git a/core/src/style.rs b/core/src/style.rs deleted file mode 100644 index 575ea366..00000000 --- a/core/src/style.rs +++ /dev/null @@ -1,262 +0,0 @@ -use std::hash::{Hash, Hasher}; -use stretch::{geometry, style}; - -/// The appearance of a [`Node`]. -/// -/// [`Node`]: struct.Node.html -#[derive(Debug, Clone, Copy)] -pub struct Style(pub(crate) style::Style); - -impl Style { - /// Defines the width of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn width(mut self, width: u16) -> Self { - self.0.size.width = style::Dimension::Points(width as f32); - self - } - - /// Defines the height of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn height(mut self, height: u16) -> Self { - self.0.size.height = style::Dimension::Points(height as f32); - self - } - - /// Defines the minimum width of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn min_width(mut self, min_width: u16) -> Self { - self.0.min_size.width = style::Dimension::Points(min_width as f32); - self - } - - /// Defines the maximum width of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn max_width(mut self, max_width: u16) -> Self { - self.0.max_size.width = style::Dimension::Points(max_width as f32); - self.fill_width() - } - - /// Defines the minimum height of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn min_height(mut self, min_height: u16) -> Self { - self.0.min_size.height = - style::Dimension::Points(f32::from(min_height)); - self - } - - /// Defines the maximum height of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn max_height(mut self, max_height: u16) -> Self { - self.0.max_size.height = - style::Dimension::Points(f32::from(max_height)); - self.fill_height() - } - - /// Makes a [`Node`] fill all the horizontal available space. - /// - /// [`Node`]: struct.Node.html - pub fn fill_width(mut self) -> Self { - self.0.size.width = stretch::style::Dimension::Percent(1.0); - self - } - - /// Makes a [`Node`] fill all the vertical available space. - /// - /// [`Node`]: struct.Node.html - pub fn fill_height(mut self) -> Self { - self.0.size.height = stretch::style::Dimension::Percent(1.0); - self - } - - pub(crate) fn align_items(mut self, align: Align) -> Self { - self.0.align_items = align.into(); - self - } - - pub(crate) fn justify_content(mut self, justify: Justify) -> Self { - self.0.justify_content = justify.into(); - self - } - - /// Sets the alignment of a [`Node`]. - /// - /// If the [`Node`] is inside a... - /// - /// * [`Column`], this setting will affect its __horizontal__ alignment. - /// * [`Row`], this setting will affect its __vertical__ alignment. - /// - /// [`Node`]: struct.Node.html - /// [`Column`]: widget/struct.Column.html - /// [`Row`]: widget/struct.Row.html - pub fn align_self(mut self, align: Align) -> Self { - self.0.align_self = align.into(); - self - } - - /// Sets the padding of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn padding(mut self, px: u16) -> Self { - self.0.padding = stretch::geometry::Rect { - start: style::Dimension::Points(px as f32), - end: style::Dimension::Points(px as f32), - top: style::Dimension::Points(px as f32), - bottom: style::Dimension::Points(px as f32), - }; - - self - } -} - -impl Default for Style { - fn default() -> Style { - Style(style::Style { - align_items: style::AlignItems::FlexStart, - justify_content: style::JustifyContent::FlexStart, - ..style::Style::default() - }) - } -} - -impl Hash for Style { - fn hash(&self, state: &mut H) { - hash_size(&self.0.size, state); - hash_size(&self.0.min_size, state); - hash_size(&self.0.max_size, state); - - hash_rect(&self.0.margin, state); - - (self.0.flex_direction as u8).hash(state); - (self.0.align_items as u8).hash(state); - (self.0.justify_content as u8).hash(state); - (self.0.align_self as u8).hash(state); - (self.0.flex_grow as u32).hash(state); - } -} - -fn hash_size( - size: &geometry::Size, - state: &mut H, -) { - hash_dimension(size.width, state); - hash_dimension(size.height, state); -} - -fn hash_rect( - rect: &geometry::Rect, - state: &mut H, -) { - hash_dimension(rect.start, state); - hash_dimension(rect.end, state); - hash_dimension(rect.top, state); - hash_dimension(rect.bottom, state); -} - -fn hash_dimension(dimension: style::Dimension, state: &mut H) { - match dimension { - style::Dimension::Undefined => state.write_u8(0), - style::Dimension::Auto => state.write_u8(1), - style::Dimension::Points(points) => { - state.write_u8(2); - (points as u32).hash(state); - } - style::Dimension::Percent(percent) => { - state.write_u8(3); - (percent as u32).hash(state); - } - } -} - -/// Alignment on the cross axis of a container. -/// -/// * On a [`Column`], it describes __horizontal__ alignment. -/// * On a [`Row`], it describes __vertical__ alignment. -/// -/// [`Column`]: widget/struct.Column.html -/// [`Row`]: widget/struct.Row.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Align { - /// Align at the start of the cross axis. - Start, - - /// Align at the center of the cross axis. - Center, - - /// Align at the end of the cross axis. - End, - - /// Stretch over the cross axis. - Stretch, -} - -#[doc(hidden)] -impl From for style::AlignItems { - fn from(align: Align) -> Self { - match align { - Align::Start => style::AlignItems::FlexStart, - Align::Center => style::AlignItems::Center, - Align::End => style::AlignItems::FlexEnd, - Align::Stretch => style::AlignItems::Stretch, - } - } -} - -#[doc(hidden)] -impl From for style::AlignSelf { - fn from(align: Align) -> Self { - match align { - Align::Start => style::AlignSelf::FlexStart, - Align::Center => style::AlignSelf::Center, - Align::End => style::AlignSelf::FlexEnd, - Align::Stretch => style::AlignSelf::Stretch, - } - } -} - -/// Distribution on the main axis of a container. -/// -/// * On a [`Column`], it describes __vertical__ distribution. -/// * On a [`Row`], it describes __horizontal__ distribution. -/// -/// [`Column`]: widget/struct.Column.html -/// [`Row`]: widget/struct.Row.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Justify { - /// Place items at the start of the main axis. - Start, - - /// Place items at the center of the main axis. - Center, - - /// Place items at the end of the main axis. - End, - - /// Place items with space between. - SpaceBetween, - - /// Place items with space around. - SpaceAround, - - /// Place items with evenly distributed space. - SpaceEvenly, -} - -#[doc(hidden)] -impl From for style::JustifyContent { - fn from(justify: Justify) -> Self { - match justify { - Justify::Start => style::JustifyContent::FlexStart, - Justify::Center => style::JustifyContent::Center, - Justify::End => style::JustifyContent::FlexEnd, - Justify::SpaceBetween => style::JustifyContent::SpaceBetween, - Justify::SpaceAround => style::JustifyContent::SpaceAround, - Justify::SpaceEvenly => style::JustifyContent::SpaceEvenly, - } - } -} diff --git a/core/src/user_interface.rs b/core/src/user_interface.rs deleted file mode 100644 index 2c7cbf82..00000000 --- a/core/src/user_interface.rs +++ /dev/null @@ -1,323 +0,0 @@ -use crate::{input::mouse, Column, Element, Event, Layout, MouseCursor, Point}; - -use std::hash::Hasher; -use stretch::result; - -/// A set of interactive graphical elements with a specific [`Layout`]. -/// -/// It can be updated and drawn. -/// -/// Iced tries to avoid dictating how to write your event loop. You are in -/// charge of using this type in your system in any way you want. -/// -/// [`Layout`]: struct.Layout.html -#[derive(Debug)] -pub struct UserInterface<'a, Message, Renderer> { - hash: u64, - root: Element<'a, Message, Renderer>, - layout: result::Layout, - cursor_position: Point, -} - -impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { - /// Builds a user interface for an [`Element`]. - /// - /// It is able to avoid expensive computations when using a [`Cache`] - /// obtained from a previous instance of a [`UserInterface`]. - /// - /// [`Element`]: struct.Element.html - /// [`Cache`]: struct.Cache.html - /// [`UserInterface`]: struct.UserInterface.html - /// - /// # Example - /// Imagine we want to build a [`UserInterface`] for - /// [the counter example that we previously wrote](index.html#usage). Here - /// is naive way to set up our application loop: - /// - /// ```no_run - /// use iced::{UserInterface, Cache}; - /// use iced_wgpu::Renderer; - /// - /// # mod iced_wgpu { - /// # pub struct Renderer; - /// # - /// # impl Renderer { - /// # pub fn new() -> Self { Renderer } - /// # } - /// # } - /// # - /// # use iced::Column; - /// # - /// # pub struct Counter; - /// # - /// # impl Counter { - /// # pub fn new() -> Self { Counter } - /// # pub fn view(&self) -> Column<(), Renderer> { - /// # Column::new() - /// # } - /// # } - /// // Initialization - /// let mut counter = Counter::new(); - /// let mut cache = Cache::new(); - /// let mut renderer = Renderer::new(); - /// - /// // Application loop - /// loop { - /// // Process system events here... - /// - /// // Build the user interface - /// let user_interface = UserInterface::build( - /// counter.view(), - /// cache, - /// &renderer, - /// ); - /// - /// // Update and draw the user interface here... - /// // ... - /// - /// // Obtain the cache for the next iteration - /// cache = user_interface.into_cache(); - /// } - /// ``` - pub fn build>>( - root: E, - cache: Cache, - renderer: &Renderer, - ) -> Self { - let root = root.into(); - - let hasher = &mut crate::Hasher::default(); - root.hash_layout(hasher); - - let hash = hasher.finish(); - - let layout = if hash == cache.hash { - cache.layout - } else { - root.compute_layout(renderer) - }; - - UserInterface { - hash, - root, - layout, - cursor_position: cache.cursor_position, - } - } - - /// Updates the [`UserInterface`] by processing each provided [`Event`]. - /// - /// It returns __messages__ that may have been produced as a result of user - /// interactions. You should feed these to your __update logic__. - /// - /// [`UserInterface`]: struct.UserInterface.html - /// [`Event`]: enum.Event.html - /// - /// # Example - /// Let's allow our [counter](index.html#usage) to change state by completing - /// [the previous example](#example): - /// - /// ```no_run - /// use iced::{UserInterface, Cache}; - /// use iced_wgpu::Renderer; - /// - /// # mod iced_wgpu { - /// # pub struct Renderer; - /// # - /// # impl Renderer { - /// # pub fn new() -> Self { Renderer } - /// # } - /// # } - /// # - /// # use iced::Column; - /// # - /// # pub struct Counter; - /// # - /// # impl Counter { - /// # pub fn new() -> Self { Counter } - /// # pub fn view(&self) -> Column<(), Renderer> { - /// # Column::new() - /// # } - /// # pub fn update(&mut self, message: ()) {} - /// # } - /// let mut counter = Counter::new(); - /// let mut cache = Cache::new(); - /// let mut renderer = Renderer::new(); - /// - /// // Initialize our event storage - /// let mut events = Vec::new(); - /// - /// loop { - /// // Process system events... - /// - /// let mut user_interface = UserInterface::build( - /// counter.view(), - /// cache, - /// &renderer, - /// ); - /// - /// // Update the user interface - /// let messages = user_interface.update(events.drain(..)); - /// - /// cache = user_interface.into_cache(); - /// - /// // Process the produced messages - /// for message in messages { - /// counter.update(message); - /// } - /// } - /// ``` - pub fn update( - &mut self, - events: impl Iterator, - ) -> Vec { - let mut messages = Vec::new(); - - for event in events { - if let Event::Mouse(mouse::Event::CursorMoved { x, y }) = event { - self.cursor_position = Point::new(x, y); - } - - self.root.widget.on_event( - event, - Layout::new(&self.layout), - self.cursor_position, - &mut messages, - ); - } - - messages - } - - /// Draws the [`UserInterface`] with the provided [`Renderer`]. - /// - /// It returns the current state of the [`MouseCursor`]. You should update - /// the icon of the mouse cursor accordingly in your system. - /// - /// [`UserInterface`]: struct.UserInterface.html - /// [`Renderer`]: trait.Renderer.html - /// [`MouseCursor`]: enum.MouseCursor.html - /// - /// # Example - /// We can finally draw our [counter](index.html#usage) by - /// [completing the last example](#example-1): - /// - /// ```no_run - /// use iced::{UserInterface, Cache}; - /// use iced_wgpu::Renderer; - /// - /// # mod iced_wgpu { - /// # pub struct Renderer; - /// # - /// # impl Renderer { - /// # pub fn new() -> Self { Renderer } - /// # } - /// # } - /// # - /// # use iced::Column; - /// # - /// # pub struct Counter; - /// # - /// # impl Counter { - /// # pub fn new() -> Self { Counter } - /// # pub fn view(&self) -> Column<(), Renderer> { - /// # Column::new() - /// # } - /// # pub fn update(&mut self, message: ()) {} - /// # } - /// let mut counter = Counter::new(); - /// let mut cache = Cache::new(); - /// let mut renderer = Renderer::new(); - /// let mut events = Vec::new(); - /// - /// loop { - /// // Process system events... - /// - /// let mut user_interface = UserInterface::build( - /// counter.view(), - /// cache, - /// &renderer, - /// ); - /// - /// let messages = user_interface.update(events.drain(..)); - /// - /// // Draw the user interface - /// let mouse_cursor = user_interface.draw(&mut renderer); - /// - /// cache = user_interface.into_cache(); - /// - /// for message in messages { - /// counter.update(message); - /// } - /// - /// // Update mouse cursor icon... - /// // Flush rendering operations... - /// } - /// ``` - pub fn draw(&self, renderer: &mut Renderer) -> MouseCursor { - self.root.widget.draw( - renderer, - Layout::new(&self.layout), - self.cursor_position, - ) - } - - /// Extract the [`Cache`] of the [`UserInterface`], consuming it in the - /// process. - /// - /// [`Cache`]: struct.Cache.html - /// [`UserInterface`]: struct.UserInterface.html - pub fn into_cache(self) -> Cache { - Cache { - hash: self.hash, - layout: self.layout, - cursor_position: self.cursor_position, - } - } -} - -/// Reusable data of a specific [`UserInterface`]. -/// -/// [`UserInterface`]: struct.UserInterface.html -#[derive(Debug, Clone)] -pub struct Cache { - hash: u64, - layout: result::Layout, - cursor_position: Point, -} - -impl Cache { - /// Creates an empty [`Cache`]. - /// - /// You should use this to initialize a [`Cache`] before building your first - /// [`UserInterface`]. - /// - /// [`Cache`]: struct.Cache.html - /// [`UserInterface`]: struct.UserInterface.html - pub fn new() -> Cache { - let root: Element<'_, (), ()> = Column::new().into(); - - let hasher = &mut crate::Hasher::default(); - root.hash_layout(hasher); - - Cache { - hash: hasher.finish(), - layout: root.compute_layout(&()), - cursor_position: Point::new(0.0, 0.0), - } - } -} - -impl Default for Cache { - fn default() -> Cache { - Cache::new() - } -} - -impl PartialEq for Cache { - fn eq(&self, other: &Cache) -> bool { - self.hash == other.hash && self.cursor_position == other.cursor_position - } -} - -impl Eq for Cache {} diff --git a/core/src/vector.rs b/core/src/vector.rs deleted file mode 100644 index f45daab9..00000000 --- a/core/src/vector.rs +++ /dev/null @@ -1,15 +0,0 @@ -/// A 2D vector. -#[derive(Debug, Clone, Copy, PartialEq)] -pub struct Vector { - pub x: f32, - pub y: f32, -} - -impl Vector { - /// Creates a new [`Vector`] with the given components. - /// - /// [`Vector`]: struct.Vector.html - pub fn new(x: f32, y: f32) -> Self { - Self { x, y } - } -} diff --git a/core/src/widget.rs b/core/src/widget.rs deleted file mode 100644 index 30606934..00000000 --- a/core/src/widget.rs +++ /dev/null @@ -1,114 +0,0 @@ -//! Use the built-in widgets or create your own. -//! -//! # Built-in widgets -//! Every built-in drawable widget has its own module with a `Renderer` trait -//! that must be implemented by a [renderer] before being able to use it as -//! a [`Widget`]. -//! -//! # Custom widgets -//! If you want to implement a custom widget, you simply need to implement the -//! [`Widget`] trait. You can use the API of the built-in widgets as a guide or -//! source of inspiration. -//! -//! # Re-exports -//! For convenience, the contents of this module are available at the root -//! module. Therefore, you can directly type: -//! -//! ``` -//! use iced::{button, Button, Widget}; -//! ``` -//! -//! [`Widget`]: trait.Widget.html -//! [renderer]: ../renderer/index.html -mod column; -mod row; - -pub mod button; -pub mod checkbox; -pub mod image; -//pub mod progress_bar; -pub mod radio; -pub mod slider; -pub mod text; - -pub use button::Button; -pub use checkbox::Checkbox; -pub use column::Column; -pub use image::Image; -//pub use progress_bar::ProgressBar; -pub use radio::Radio; -pub use row::Row; -pub use slider::Slider; -pub use text::Text; - -use crate::{Event, Hasher, Layout, MouseCursor, Node, Point}; - -/// A component that displays information and allows interaction. -/// -/// If you want to build your own widgets, you will need to implement this -/// trait. -/// -/// [`Widget`]: trait.Widget.html -/// [`Element`]: ../struct.Element.html -pub trait Widget: std::fmt::Debug { - /// Returns the [`Node`] of the [`Widget`]. - /// - /// This [`Node`] is used by the runtime to compute the [`Layout`] of the - /// user interface. - /// - /// [`Node`]: ../struct.Node.html - /// [`Widget`]: trait.Widget.html - /// [`Layout`]: ../struct.Layout.html - fn node(&self, renderer: &Renderer) -> Node; - - /// Draws the [`Widget`] using the associated `Renderer`. - /// - /// It must return the [`MouseCursor`] state for the [`Widget`]. - /// - /// [`Widget`]: trait.Widget.html - /// [`MouseCursor`]: ../enum.MouseCursor.html - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor; - - /// Computes the _layout_ hash of the [`Widget`]. - /// - /// The produced hash is used by the runtime to decide if the [`Layout`] - /// needs to be recomputed between frames. Therefore, to ensure maximum - /// efficiency, the hash should only be affected by the properties of the - /// [`Widget`] that can affect layouting. - /// - /// For example, the [`Text`] widget does not hash its color property, as - /// its value cannot affect the overall [`Layout`] of the user interface. - /// - /// [`Widget`]: trait.Widget.html - /// [`Layout`]: ../struct.Layout.html - /// [`Text`]: text/struct.Text.html - fn hash_layout(&self, state: &mut Hasher); - - /// Processes a runtime [`Event`]. - /// - /// It receives: - /// * an [`Event`] describing user interaction - /// * the computed [`Layout`] of the [`Widget`] - /// * the current cursor position - /// * a mutable `Message` list, allowing the [`Widget`] to produce - /// new messages based on user interaction. - /// - /// By default, it does nothing. - /// - /// [`Event`]: ../enum.Event.html - /// [`Widget`]: trait.Widget.html - /// [`Layout`]: ../struct.Layout.html - fn on_event( - &mut self, - _event: Event, - _layout: Layout<'_>, - _cursor_position: Point, - _messages: &mut Vec, - ) { - } -} diff --git a/core/src/widget/button.rs b/core/src/widget/button.rs deleted file mode 100644 index abcdbfeb..00000000 --- a/core/src/widget/button.rs +++ /dev/null @@ -1,282 +0,0 @@ -//! Allow your users to perform actions by pressing a button. -//! -//! A [`Button`] has some local [`State`] and a [`Class`]. -//! -//! [`Button`]: struct.Button.html -//! [`State`]: struct.State.html -//! [`Class`]: enum.Class.html - -use crate::input::{mouse, ButtonState}; -use crate::{ - Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, - Style, Widget, -}; - -use std::hash::Hash; - -/// A generic widget that produces a message when clicked. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`button::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`button::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::{button, Button}; -/// -/// pub enum Message { -/// ButtonClicked, -/// } -/// -/// let state = &mut button::State::new(); -/// -/// Button::new(state, "Click me!") -/// .on_press(Message::ButtonClicked); -/// ``` -/// -/// ![Button drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/button.png?raw=true) -pub struct Button<'a, Message> { - state: &'a mut State, - label: String, - class: Class, - on_press: Option, - style: Style, -} - -impl<'a, Message> std::fmt::Debug for Button<'a, Message> -where - Message: std::fmt::Debug, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Button") - .field("state", &self.state) - .field("label", &self.label) - .field("class", &self.class) - .field("on_press", &self.on_press) - .field("style", &self.style) - .finish() - } -} - -impl<'a, Message> Button<'a, Message> { - /// Creates a new [`Button`] with some local [`State`] and the given label. - /// - /// The default [`Class`] of a new [`Button`] is [`Class::Primary`]. - /// - /// [`Button`]: struct.Button.html - /// [`State`]: struct.State.html - /// [`Class`]: enum.Class.html - /// [`Class::Primary`]: enum.Class.html#variant.Primary - pub fn new(state: &'a mut State, label: &str) -> Self { - Button { - state, - label: String::from(label), - class: Class::Primary, - on_press: None, - style: Style::default().min_width(100), - } - } - - /// Sets the width of the [`Button`] in pixels. - /// - /// [`Button`]: struct.Button.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Makes the [`Button`] fill the horizontal space of its container. - /// - /// [`Button`]: struct.Button.html - pub fn fill_width(mut self) -> Self { - self.style = self.style.fill_width(); - self - } - - /// Sets the alignment of the [`Button`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Button`]: struct.Button.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } - - /// Sets the [`Class`] of the [`Button`]. - /// - /// - /// [`Button`]: struct.Button.html - /// [`Class`]: enum.Class.html - pub fn class(mut self, class: Class) -> Self { - self.class = class; - self - } - - /// Sets the message that will be produced when the [`Button`] is pressed. - /// - /// [`Button`]: struct.Button.html - pub fn on_press(mut self, msg: Message) -> Self { - self.on_press = Some(msg); - self - } -} - -impl<'a, Message, Renderer> Widget for Button<'a, Message> -where - Renderer: self::Renderer, - Message: Copy + std::fmt::Debug, -{ - fn node(&self, _renderer: &Renderer) -> Node { - Node::new(self.style.height(50)) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state, - }) => { - if let Some(on_press) = self.on_press { - let bounds = layout.bounds(); - - match state { - ButtonState::Pressed => { - self.state.is_pressed = - bounds.contains(cursor_position); - } - ButtonState::Released => { - let is_clicked = self.state.is_pressed - && bounds.contains(cursor_position); - - self.state.is_pressed = false; - - if is_clicked { - messages.push(on_press); - } - } - } - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - cursor_position, - layout.bounds(), - self.state, - &self.label, - self.class, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -/// The local state of a [`Button`]. -/// -/// [`Button`]: struct.Button.html -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub struct State { - is_pressed: bool, -} - -impl State { - /// Creates a new [`State`]. - /// - /// [`State`]: struct.State.html - pub fn new() -> State { - State::default() - } - - /// Returns whether the associated [`Button`] is currently being pressed or - /// not. - /// - /// [`Button`]: struct.Button.html - pub fn is_pressed(&self) -> bool { - self.is_pressed - } -} - -/// The type of a [`Button`]. -/// -/// ![Different buttons drawn by the built-in renderer in Coffee](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/button_classes.png?raw=true) -/// -/// [`Button`]: struct.Button.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Class { - /// The [`Button`] performs the main action. - /// - /// [`Button`]: struct.Button.html - Primary, - - /// The [`Button`] performs an alternative action. - /// - /// [`Button`]: struct.Button.html - Secondary, - - /// The [`Button`] performs a productive action. - /// - /// [`Button`]: struct.Button.html - Positive, -} - -/// The renderer of a [`Button`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Button`] in your user interface. -/// -/// [`Button`]: struct.Button.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Button`]. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Button`] - /// * the local state of the [`Button`] - /// * the label of the [`Button`] - /// * the [`Class`] of the [`Button`] - /// - /// [`Button`]: struct.Button.html - /// [`State`]: struct.State.html - /// [`Class`]: enum.Class.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - state: &State, - label: &str, - class: Class, - ) -> MouseCursor; -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, - Message: 'static + Copy + std::fmt::Debug, -{ - fn from(button: Button<'a, Message>) -> Element<'a, Message, Renderer> { - Element::new(button) - } -} diff --git a/core/src/widget/checkbox.rs b/core/src/widget/checkbox.rs deleted file mode 100644 index c60807fd..00000000 --- a/core/src/widget/checkbox.rs +++ /dev/null @@ -1,203 +0,0 @@ -//! Show toggle controls using checkboxes. -use std::hash::Hash; - -use crate::input::{mouse, ButtonState}; -use crate::widget::{text, Column, Row, Text}; -use crate::{ - Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, - Widget, -}; - -/// A box that can be checked, with a generic text `Color`. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`checkbox::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`checkbox::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::Checkbox; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } -/// -/// pub enum Message { -/// CheckboxToggled(bool), -/// } -/// -/// let is_checked = true; -/// -/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled) -/// .label_color(Color::Black); -/// ``` -/// -/// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true) -pub struct Checkbox { - is_checked: bool, - on_toggle: Box Message>, - label: String, - label_color: Option, -} - -impl std::fmt::Debug for Checkbox -where - Color: std::fmt::Debug, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Checkbox") - .field("is_checked", &self.is_checked) - .field("label", &self.label) - .field("label_color", &self.label_color) - .finish() - } -} - -impl Checkbox { - /// Creates a new [`Checkbox`]. - /// - /// It expects: - /// * a boolean describing whether the [`Checkbox`] is checked or not - /// * the label of the [`Checkbox`] - /// * a function that will be called when the [`Checkbox`] is toggled. - /// It will receive the new state of the [`Checkbox`] and must produce - /// a `Message`. - /// - /// [`Checkbox`]: struct.Checkbox.html - pub fn new(is_checked: bool, label: &str, f: F) -> Self - where - F: 'static + Fn(bool) -> Message, - { - Checkbox { - is_checked, - on_toggle: Box::new(f), - label: String::from(label), - label_color: None, - } - } - - /// Sets the `Color` of the label of the [`Checkbox`]. - /// - /// [`Checkbox`]: struct.Checkbox.html - pub fn label_color(mut self, color: Color) -> Self { - self.label_color = Some(color); - self - } -} - -impl Widget - for Checkbox -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, -{ - fn node(&self, renderer: &Renderer) -> Node { - Row::<(), Renderer>::new() - .spacing(15) - .align_items(Align::Center) - .push(Column::new().width(28).height(28)) - .push(Text::new(&self.label)) - .node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Pressed, - }) => { - let mouse_over = layout - .children() - .any(|child| child.bounds().contains(cursor_position)); - - if mouse_over { - messages.push((self.on_toggle)(!self.is_checked)); - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let children: Vec<_> = layout.children().collect(); - - let text_bounds = children[1].bounds(); - - text::Renderer::draw( - renderer, - text_bounds, - &self.label, - None, - self.label_color, - text::HorizontalAlignment::Left, - text::VerticalAlignment::Top, - ); - - self::Renderer::draw( - renderer, - cursor_position, - children[0].bounds(), - text_bounds, - self.is_checked, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.label.hash(state); - } -} - -/// The renderer of a [`Checkbox`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Checkbox`] in your user interface. -/// -/// [`Checkbox`]: struct.Checkbox.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Checkbox`]. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Checkbox`] - /// * the bounds of the label of the [`Checkbox`] - /// * whether the [`Checkbox`] is checked or not - /// - /// [`Checkbox`]: struct.Checkbox.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - label_bounds: Rectangle, - is_checked: bool, - ) -> MouseCursor; -} - -impl<'a, Color, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, - Message: 'static, -{ - fn from( - checkbox: Checkbox, - ) -> Element<'a, Message, Renderer> { - Element::new(checkbox) - } -} diff --git a/core/src/widget/column.rs b/core/src/widget/column.rs deleted file mode 100644 index ff754e98..00000000 --- a/core/src/widget/column.rs +++ /dev/null @@ -1,224 +0,0 @@ -use std::hash::Hash; - -use crate::{ - Align, Element, Event, Hasher, Justify, Layout, MouseCursor, Node, Point, - Style, Widget, -}; - -/// A container that distributes its contents vertically. -/// -/// A [`Column`] will try to fill the horizontal space of its container. -/// -/// [`Column`]: struct.Column.html -#[derive(Default)] -pub struct Column<'a, Message, Renderer> { - style: Style, - spacing: u16, - children: Vec>, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Column<'a, Message, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Column") - .field("style", &self.style) - .field("spacing", &self.spacing) - .field("children", &self.children) - .finish() - } -} - -impl<'a, Message, Renderer> Column<'a, Message, Renderer> { - /// Creates an empty [`Column`]. - /// - /// [`Column`]: struct.Column.html - pub fn new() -> Self { - let mut style = Style::default().fill_width(); - style.0.flex_direction = stretch::style::FlexDirection::Column; - - Column { - style, - spacing: 0, - children: Vec::new(), - } - } - - /// Sets the vertical spacing _between_ elements in pixels. - /// - /// Custom margins per element do not exist in Iced. You should use this - /// method instead! While less flexible, it helps you keep spacing between - /// elements consistent. - pub fn spacing(mut self, px: u16) -> Self { - self.spacing = px; - self - } - - /// Sets the padding of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn padding(mut self, px: u16) -> Self { - self.style = self.style.padding(px); - self - } - - /// Sets the width of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the maximum width of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn max_width(mut self, max_width: u16) -> Self { - self.style = self.style.max_width(max_width); - self - } - - /// Sets the maximum height of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn max_height(mut self, max_height: u16) -> Self { - self.style = self.style.max_height(max_height); - self - } - - /// Sets the alignment of the [`Column`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Column`]: struct.Column.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } - - /// Sets the horizontal alignment of the contents of the [`Column`] . - /// - /// [`Column`]: struct.Column.html - pub fn align_items(mut self, align: Align) -> Self { - self.style = self.style.align_items(align); - self - } - - /// Sets the vertical distribution strategy for the contents of the - /// [`Column`] . - /// - /// [`Column`]: struct.Column.html - pub fn justify_content(mut self, justify: Justify) -> Self { - self.style = self.style.justify_content(justify); - self - } - - /// Adds an [`Element`] to the [`Column`]. - /// - /// [`Element`]: ../struct.Element.html - /// [`Column`]: struct.Column.html - pub fn push(mut self, child: E) -> Column<'a, Message, Renderer> - where - E: Into>, - { - self.children.push(child.into()); - self - } -} - -impl<'a, Message, Renderer> Widget - for Column<'a, Message, Renderer> -{ - fn node(&self, renderer: &Renderer) -> Node { - let mut children: Vec = self - .children - .iter() - .map(|child| { - let mut node = child.widget.node(renderer); - - let mut style = node.0.style(); - style.margin.bottom = - stretch::style::Dimension::Points(f32::from(self.spacing)); - - node.0.set_style(style); - node - }) - .collect(); - - if let Some(node) = children.last_mut() { - let mut style = node.0.style(); - style.margin.bottom = stretch::style::Dimension::Undefined; - - node.0.set_style(style); - } - - Node::with_children(self.style, children) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - self.children.iter_mut().zip(layout.children()).for_each( - |(child, layout)| { - child - .widget - .on_event(event, layout, cursor_position, messages) - }, - ); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let mut cursor = MouseCursor::OutOfBounds; - - self.children.iter().zip(layout.children()).for_each( - |(child, layout)| { - let new_cursor = - child.widget.draw(renderer, layout, cursor_position); - - if new_cursor != MouseCursor::OutOfBounds { - cursor = new_cursor; - } - }, - ); - - cursor - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - self.spacing.hash(state); - - for child in &self.children { - child.widget.hash_layout(state); - } - } -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: 'a, - Message: 'static, -{ - fn from( - column: Column<'a, Message, Renderer>, - ) -> Element<'a, Message, Renderer> { - Element::new(column) - } -} diff --git a/core/src/widget/image.rs b/core/src/widget/image.rs deleted file mode 100644 index d94bfea5..00000000 --- a/core/src/widget/image.rs +++ /dev/null @@ -1,178 +0,0 @@ -//! Display images in your user interface. - -use crate::{ - Align, Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, - Widget, -}; - -use std::hash::Hash; - -/// A frame that displays an image while keeping aspect ratio. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`image::Renderer`] trait. -/// -/// [`Widget`]: ../../core/trait.Widget.html -/// [`image::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::Image; -/// -/// # let my_handle = String::from("some_handle"); -/// let image = Image::new(my_handle); -/// ``` -pub struct Image { - image: I, - source: Option>, - width: Option, - height: Option, - style: Style, -} - -impl std::fmt::Debug for Image { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Image") - .field("source", &self.source) - .field("width", &self.width) - .field("height", &self.height) - .field("style", &self.style) - .finish() - } -} - -impl Image { - /// Creates a new [`Image`] with given image handle. - /// - /// [`Image`]: struct.Image.html - pub fn new(image: I) -> Self { - Image { - image, - source: None, - width: None, - height: None, - style: Style::default(), - } - } - - /// Sets the portion of the [`Image`] to draw. - /// - /// [`Image`]: struct.Image.html - pub fn clip(mut self, source: Rectangle) -> Self { - self.source = Some(source); - self - } - - /// Sets the width of the [`Image`] boundaries in pixels. - /// - /// [`Image`]: struct.Image.html - pub fn width(mut self, width: u16) -> Self { - self.width = Some(width); - self - } - - /// Sets the height of the [`Image`] boundaries in pixels. - /// - /// [`Image`]: struct.Image.html - pub fn height(mut self, height: u16) -> Self { - self.height = Some(height); - self - } - - /// Sets the alignment of the [`Image`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Image`]: struct.Image.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } -} - -impl Widget for Image -where - Renderer: self::Renderer, - I: Clone, -{ - fn node(&self, renderer: &Renderer) -> Node { - renderer.node( - self.style, - &self.image, - self.width, - self.height, - self.source, - ) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw(&self.image, layout.bounds(), self.source); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - self.width.hash(state); - self.height.hash(state); - } -} - -/// The renderer of an [`Image`]. -/// -/// Your [renderer] will need to implement this trait before being able to use -/// an [`Image`] in your user interface. -/// -/// [`Image`]: struct.Image.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Creates a [`Node`] with the given [`Style`] for the provided [`Image`] - /// and its size. - /// - /// You should probably keep the original aspect ratio, if possible. - /// - /// [`Node`]: ../../struct.Node.html - /// [`Style`]: ../../struct.Style.html - /// [`Image`]: struct.Image.html - fn node( - &self, - style: Style, - image: &I, - width: Option, - height: Option, - source: Option>, - ) -> Node; - - /// Draws an [`Image`]. - /// - /// It receives: - /// * the bounds of the [`Image`] - /// * the handle of the loaded [`Image`] - /// * the portion of the image to draw. If not specified, the entire image - /// should be drawn. - /// - /// [`Image`]: struct.Image.html - fn draw( - &mut self, - image: &I, - bounds: Rectangle, - source: Option>, - ); -} - -impl<'a, I, Message, Renderer> From> for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, - I: Clone + 'a, -{ - fn from(image: Image) -> Element<'a, Message, Renderer> { - Element::new(image) - } -} diff --git a/core/src/widget/panel.rs b/core/src/widget/panel.rs deleted file mode 100644 index d43d6fb6..00000000 --- a/core/src/widget/panel.rs +++ /dev/null @@ -1,94 +0,0 @@ -use std::hash::Hash; - -use crate::graphics::{Point, Rectangle}; -use crate::ui::core::{ - Event, Hasher, Layout, MouseCursor, Node, Style, Widget, -}; - -pub struct Panel<'a, Message, Renderer> { - style: Style, - content: Box + 'a>, -} - -impl<'a, Message, Renderer> Panel<'a, Message, Renderer> { - pub fn new(content: impl Widget + 'a) -> Self { - Panel { - style: Style::default().padding(20), - content: Box::new(content), - } - } - - pub fn width(mut self, width: u32) -> Self { - self.style = self.style.width(width); - self - } - - pub fn max_width(mut self, max_width: u32) -> Self { - self.style = self.style.max_width(max_width); - self - } -} - -impl<'a, Message, Renderer> Widget - for Panel<'a, Message, Renderer> -where - Renderer: self::Renderer, -{ - fn node(&self, renderer: &Renderer) -> Node { - Node::with_children(self.style, vec![self.content.node(renderer)]) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout, - cursor_position: Point, - messages: &mut Vec, - ) { - [&mut self.content] - .iter_mut() - .zip(layout.children()) - .for_each(|(child, layout)| { - child.on_event(event, layout, cursor_position, messages) - }); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout, - cursor_position: Point, - ) -> MouseCursor { - let bounds = layout.bounds(); - let mut cursor = MouseCursor::OutOfBounds; - renderer.draw(bounds); - - [&self.content].iter().zip(layout.children()).for_each( - |(child, layout)| { - let new_cursor = child.draw(renderer, layout, cursor_position); - - if new_cursor != MouseCursor::OutOfBounds { - cursor = new_cursor; - } - }, - ); - - if cursor == MouseCursor::OutOfBounds { - if bounds.contains(cursor_position) { - MouseCursor::Idle - } else { - MouseCursor::OutOfBounds - } - } else { - cursor - } - } - - fn hash(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -pub trait Renderer { - fn draw(&mut self, bounds: Rectangle); -} diff --git a/core/src/widget/progress_bar.rs b/core/src/widget/progress_bar.rs deleted file mode 100644 index d4499160..00000000 --- a/core/src/widget/progress_bar.rs +++ /dev/null @@ -1,106 +0,0 @@ -//! Provide visual feedback to your users when performing a slow task. - -use crate::{ - Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, -}; - -use std::hash::Hash; - -/// A bar that is filled based on an amount of progress. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`progress_bar::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`progress_bar::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::ProgressBar; -/// -/// let progress = 0.75; -/// -/// ProgressBar::new(progress); -/// ``` -#[derive(Debug)] -pub struct ProgressBar { - progress: f32, - style: Style, -} - -impl ProgressBar { - /// Creates a new [`ProgressBar`] filled based on the given amount of - /// progress. - /// - /// The progress should be in the `0.0..=1.0` range. `0` meaning no work - /// done, and `1` meaning work finished. - /// - /// [`ProgressBar`]: struct.ProgressBar.html - pub fn new(progress: f32) -> Self { - ProgressBar { - progress, - style: Style::default().fill_width(), - } - } - - /// Sets the width of the [`ProgressBar`] in pixels. - /// - /// [`ProgressBar`]: struct.ProgressBar.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } -} - -impl Widget for ProgressBar -where - Renderer: self::Renderer, -{ - fn node(&self, _renderer: &Renderer) -> Node { - Node::new(self.style.height(50)) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw(layout.bounds(), self.progress); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -/// The renderer of a [`ProgressBar`]. -/// -/// Your [renderer] will need to implement this trait before being able to use -/// a [`ProgressBar`] in your user interface. -/// -/// [`ProgressBar`]: struct.ProgressBar.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`ProgressBar`]. - /// - /// It receives: - /// * the bounds of the [`ProgressBar`] - /// * the current progress of the [`ProgressBar`], in the `0.0..=1.0` - /// range. - /// - /// [`ProgressBar`]: struct.ProgressBar.html - fn draw(&mut self, bounds: Rectangle, progress: f32); -} - -impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, -{ - fn from(progress_bar: ProgressBar) -> Element<'a, Message, Renderer> { - Element::new(progress_bar) - } -} diff --git a/core/src/widget/radio.rs b/core/src/widget/radio.rs deleted file mode 100644 index 28353ef4..00000000 --- a/core/src/widget/radio.rs +++ /dev/null @@ -1,211 +0,0 @@ -//! Create choices using radio buttons. -use crate::input::{mouse, ButtonState}; -use crate::widget::{text, Column, Row, Text}; -use crate::{ - Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, - Widget, -}; - -use std::hash::Hash; - -/// A circular button representing a choice, with a generic text `Color`. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`radio::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`radio::Renderer`]: trait.Renderer.html -/// -/// # Example -/// ``` -/// use iced::{Column, Radio}; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } -/// -/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] -/// pub enum Choice { -/// A, -/// B, -/// } -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Message { -/// RadioSelected(Choice), -/// } -/// -/// let selected_choice = Some(Choice::A); -/// -/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected) -/// .label_color(Color::Black); -/// -/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected) -/// .label_color(Color::Black); -/// ``` -/// -/// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true) -pub struct Radio { - is_selected: bool, - on_click: Message, - label: String, - label_color: Option, -} - -impl std::fmt::Debug for Radio -where - Color: std::fmt::Debug, - Message: std::fmt::Debug, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Radio") - .field("is_selected", &self.is_selected) - .field("on_click", &self.on_click) - .field("label", &self.label) - .field("label_color", &self.label_color) - .finish() - } -} - -impl Radio { - /// Creates a new [`Radio`] button. - /// - /// It expects: - /// * the value related to the [`Radio`] button - /// * the label of the [`Radio`] button - /// * the current selected value - /// * a function that will be called when the [`Radio`] is selected. It - /// receives the value of the radio and must produce a `Message`. - /// - /// [`Radio`]: struct.Radio.html - pub fn new(value: V, label: &str, selected: Option, f: F) -> Self - where - V: Eq + Copy, - F: 'static + Fn(V) -> Message, - { - Radio { - is_selected: Some(value) == selected, - on_click: f(value), - label: String::from(label), - label_color: None, - } - } - - /// Sets the `Color` of the label of the [`Radio`]. - /// - /// [`Radio`]: struct.Radio.html - pub fn label_color(mut self, color: Color) -> Self { - self.label_color = Some(color); - self - } -} - -impl Widget - for Radio -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, - Message: Copy + std::fmt::Debug, -{ - fn node(&self, renderer: &Renderer) -> Node { - Row::<(), Renderer>::new() - .spacing(15) - .align_items(Align::Center) - .push(Column::new().width(28).height(28)) - .push(Text::new(&self.label)) - .node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Pressed, - }) => { - if layout.bounds().contains(cursor_position) { - messages.push(self.on_click); - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let children: Vec<_> = layout.children().collect(); - - let mut text_bounds = children[1].bounds(); - text_bounds.y -= 2.0; - - text::Renderer::draw( - renderer, - text_bounds, - &self.label, - None, - self.label_color, - text::HorizontalAlignment::Left, - text::VerticalAlignment::Top, - ); - - self::Renderer::draw( - renderer, - cursor_position, - children[0].bounds(), - layout.bounds(), - self.is_selected, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.label.hash(state); - } -} - -/// The renderer of a [`Radio`] button. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Radio`] button in your user interface. -/// -/// [`Radio`]: struct.Radio.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Radio`] button. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Radio`] - /// * the bounds of the label of the [`Radio`] - /// * whether the [`Radio`] is selected or not - /// - /// [`Radio`]: struct.Radio.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - label_bounds: Rectangle, - is_selected: bool, - ) -> MouseCursor; -} - -impl<'a, Color, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, - Message: 'static + Copy + std::fmt::Debug, -{ - fn from(checkbox: Radio) -> Element<'a, Message, Renderer> { - Element::new(checkbox) - } -} diff --git a/core/src/widget/row.rs b/core/src/widget/row.rs deleted file mode 100644 index 959528dc..00000000 --- a/core/src/widget/row.rs +++ /dev/null @@ -1,219 +0,0 @@ -use std::hash::Hash; - -use crate::{ - Align, Element, Event, Hasher, Justify, Layout, MouseCursor, Node, Point, - Style, Widget, -}; - -/// A container that distributes its contents horizontally. -/// -/// A [`Row`] will try to fill the horizontal space of its container. -/// -/// [`Row`]: struct.Row.html -#[derive(Default)] -pub struct Row<'a, Message, Renderer> { - style: Style, - spacing: u16, - children: Vec>, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Row<'a, Message, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Row") - .field("style", &self.style) - .field("spacing", &self.spacing) - .field("children", &self.children) - .finish() - } -} - -impl<'a, Message, Renderer> Row<'a, Message, Renderer> { - /// Creates an empty [`Row`]. - /// - /// [`Row`]: struct.Row.html - pub fn new() -> Self { - Row { - style: Style::default().fill_width(), - spacing: 0, - children: Vec::new(), - } - } - - /// Sets the horizontal spacing _between_ elements in pixels. - /// - /// Custom margins per element do not exist in Iced. You should use this - /// method instead! While less flexible, it helps you keep spacing between - /// elements consistent. - pub fn spacing(mut self, px: u16) -> Self { - self.spacing = px; - self - } - - /// Sets the padding of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn padding(mut self, px: u16) -> Self { - self.style = self.style.padding(px); - self - } - - /// Sets the width of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the maximum width of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn max_width(mut self, max_width: u16) -> Self { - self.style = self.style.max_width(max_width); - self - } - - /// Sets the maximum height of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn max_height(mut self, max_height: u16) -> Self { - self.style = self.style.max_height(max_height); - self - } - - /// Sets the alignment of the [`Row`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Row`]: struct.Row.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } - - /// Sets the vertical alignment of the contents of the [`Row`] . - /// - /// [`Row`]: struct.Row.html - pub fn align_items(mut self, align: Align) -> Self { - self.style = self.style.align_items(align); - self - } - - /// Sets the horizontal distribution strategy for the contents of the - /// [`Row`] . - /// - /// [`Row`]: struct.Row.html - pub fn justify_content(mut self, justify: Justify) -> Self { - self.style = self.style.justify_content(justify); - self - } - - /// Adds an [`Element`] to the [`Row`]. - /// - /// [`Element`]: ../struct.Element.html - /// [`Row`]: struct.Row.html - pub fn push(mut self, child: E) -> Row<'a, Message, Renderer> - where - E: Into>, - { - self.children.push(child.into()); - self - } -} - -impl<'a, Message, Renderer> Widget - for Row<'a, Message, Renderer> -{ - fn node(&self, renderer: &Renderer) -> Node { - let mut children: Vec = self - .children - .iter() - .map(|child| { - let mut node = child.widget.node(renderer); - - let mut style = node.0.style(); - style.margin.end = - stretch::style::Dimension::Points(f32::from(self.spacing)); - - node.0.set_style(style); - node - }) - .collect(); - - if let Some(node) = children.last_mut() { - let mut style = node.0.style(); - style.margin.end = stretch::style::Dimension::Undefined; - - node.0.set_style(style); - } - - Node::with_children(self.style, children) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - self.children.iter_mut().zip(layout.children()).for_each( - |(child, layout)| { - child - .widget - .on_event(event, layout, cursor_position, messages) - }, - ); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let mut cursor = MouseCursor::OutOfBounds; - - self.children.iter().zip(layout.children()).for_each( - |(child, layout)| { - let new_cursor = - child.widget.draw(renderer, layout, cursor_position); - - if new_cursor != MouseCursor::OutOfBounds { - cursor = new_cursor; - } - }, - ); - - cursor - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - self.spacing.hash(state); - - for child in &self.children { - child.widget.hash_layout(state); - } - } -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: 'a, - Message: 'static, -{ - fn from(row: Row<'a, Message, Renderer>) -> Element<'a, Message, Renderer> { - Element::new(row) - } -} diff --git a/core/src/widget/slider.rs b/core/src/widget/slider.rs deleted file mode 100644 index cdec9ec4..00000000 --- a/core/src/widget/slider.rs +++ /dev/null @@ -1,241 +0,0 @@ -//! Display an interactive selector of a single value from a range of values. -//! -//! A [`Slider`] has some local [`State`]. -//! -//! [`Slider`]: struct.Slider.html -//! [`State`]: struct.State.html -use std::hash::Hash; -use std::ops::RangeInclusive; - -use crate::input::{mouse, ButtonState}; -use crate::{ - Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, - Widget, -}; - -/// An horizontal bar and a handle that selects a single value from a range of -/// values. -/// -/// A [`Slider`] will try to fill the horizontal space of its container. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`slider::Renderer`] trait. -/// -/// [`Slider`]: struct.Slider.html -/// [`Widget`]: ../trait.Widget.html -/// [`slider::Renderer`]: trait.Renderer.html -/// -/// # Example -/// ``` -/// use iced::{slider, Slider}; -/// -/// pub enum Message { -/// SliderChanged(f32), -/// } -/// -/// let state = &mut slider::State::new(); -/// let value = 50.0; -/// -/// Slider::new(state, 0.0..=100.0, value, Message::SliderChanged); -/// ``` -/// -/// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true) -pub struct Slider<'a, Message> { - state: &'a mut State, - range: RangeInclusive, - value: f32, - on_change: Box Message>, - style: Style, -} - -impl<'a, Message> std::fmt::Debug for Slider<'a, Message> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Slider") - .field("state", &self.state) - .field("range", &self.range) - .field("value", &self.value) - .field("style", &self.style) - .finish() - } -} - -impl<'a, Message> Slider<'a, Message> { - /// Creates a new [`Slider`]. - /// - /// It expects: - /// * the local [`State`] of the [`Slider`] - /// * an inclusive range of possible values - /// * the current value of the [`Slider`] - /// * a function that will be called when the [`Slider`] is dragged. - /// It receives the new value of the [`Slider`] and must produce a - /// `Message`. - /// - /// [`Slider`]: struct.Slider.html - /// [`State`]: struct.State.html - pub fn new( - state: &'a mut State, - range: RangeInclusive, - value: f32, - on_change: F, - ) -> Self - where - F: 'static + Fn(f32) -> Message, - { - Slider { - state, - value: value.max(*range.start()).min(*range.end()), - range, - on_change: Box::new(on_change), - style: Style::default().min_width(100).fill_width(), - } - } - - /// Sets the width of the [`Slider`] in pixels. - /// - /// [`Slider`]: struct.Slider.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } -} - -impl<'a, Message, Renderer> Widget for Slider<'a, Message> -where - Renderer: self::Renderer, -{ - fn node(&self, _renderer: &Renderer) -> Node { - Node::new(self.style.height(25)) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - let mut change = || { - let bounds = layout.bounds(); - - if cursor_position.x <= bounds.x { - messages.push((self.on_change)(*self.range.start())); - } else if cursor_position.x >= bounds.x + bounds.width { - messages.push((self.on_change)(*self.range.end())); - } else { - let percent = (cursor_position.x - bounds.x) / bounds.width; - let value = (self.range.end() - self.range.start()) * percent - + self.range.start(); - - messages.push((self.on_change)(value)); - } - }; - - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state, - }) => match state { - ButtonState::Pressed => { - if layout.bounds().contains(cursor_position) { - change(); - self.state.is_dragging = true; - } - } - ButtonState::Released => { - self.state.is_dragging = false; - } - }, - Event::Mouse(mouse::Event::CursorMoved { .. }) => { - if self.state.is_dragging { - change(); - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - cursor_position, - layout.bounds(), - self.state, - self.range.clone(), - self.value, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -/// The local state of a [`Slider`]. -/// -/// [`Slider`]: struct.Slider.html -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub struct State { - is_dragging: bool, -} - -impl State { - /// Creates a new [`State`]. - /// - /// [`State`]: struct.State.html - pub fn new() -> State { - State::default() - } - - /// Returns whether the associated [`Slider`] is currently being dragged or - /// not. - /// - /// [`Slider`]: struct.Slider.html - pub fn is_dragging(&self) -> bool { - self.is_dragging - } -} - -/// The renderer of a [`Slider`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Slider`] in your user interface. -/// -/// [`Slider`]: struct.Slider.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Slider`]. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Slider`] - /// * the local state of the [`Slider`] - /// * the range of values of the [`Slider`] - /// * the current value of the [`Slider`] - /// - /// [`Slider`]: struct.Slider.html - /// [`State`]: struct.State.html - /// [`Class`]: enum.Class.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - state: &State, - range: RangeInclusive, - value: f32, - ) -> MouseCursor; -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, - Message: 'static, -{ - fn from(slider: Slider<'a, Message>) -> Element<'a, Message, Renderer> { - Element::new(slider) - } -} diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs deleted file mode 100644 index 59b599bb..00000000 --- a/core/src/widget/text.rs +++ /dev/null @@ -1,224 +0,0 @@ -//! Write some text for your users to read. -use crate::{ - Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, -}; - -use std::hash::Hash; - -/// A fragment of text with a generic `Color`. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`text::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`text::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::Text; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } -/// -/// Text::new("I <3 iced!") -/// .size(40) -/// .color(Color::Black); -/// ``` -#[derive(Debug, Clone)] -pub struct Text { - content: String, - size: Option, - color: Option, - style: Style, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, -} - -impl Text { - /// Create a new fragment of [`Text`] with the given contents. - /// - /// [`Text`]: struct.Text.html - pub fn new(label: &str) -> Self { - Text { - content: String::from(label), - size: None, - color: None, - style: Style::default().fill_width(), - horizontal_alignment: HorizontalAlignment::Left, - vertical_alignment: VerticalAlignment::Top, - } - } - - /// Sets the size of the [`Text`] in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn size(mut self, size: u16) -> Self { - self.size = Some(size); - self - } - - /// Sets the `Color` of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - pub fn color(mut self, color: Color) -> Self { - self.color = Some(color); - self - } - - /// Sets the width of the [`Text`] boundaries in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Text`] boundaries in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the [`HorizontalAlignment`] of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html - pub fn horizontal_alignment( - mut self, - alignment: HorizontalAlignment, - ) -> Self { - self.horizontal_alignment = alignment; - self - } - - /// Sets the [`VerticalAlignment`] of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - /// [`VerticalAlignment`]: enum.VerticalAlignment.html - pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self { - self.vertical_alignment = alignment; - self - } -} - -impl Widget for Text -where - Color: Copy + std::fmt::Debug, - Renderer: self::Renderer, -{ - fn node(&self, renderer: &Renderer) -> Node { - renderer.node(self.style, &self.content, self.size) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - layout.bounds(), - &self.content, - self.size, - self.color, - self.horizontal_alignment, - self.vertical_alignment, - ); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - - self.content.hash(state); - self.size.hash(state); - } -} - -/// The renderer of a [`Text`] fragment with a generic `Color`. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use [`Text`] in your [`UserInterface`]. -/// -/// [`Text`]: struct.Text.html -/// [renderer]: ../../renderer/index.html -/// [`UserInterface`]: ../../struct.UserInterface.html -pub trait Renderer { - /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] - /// contents and size. - /// - /// You should probably use [`Node::with_measure`] to allow [`Text`] to - /// adapt to the dimensions of its container. - /// - /// [`Node`]: ../../struct.Node.html - /// [`Style`]: ../../struct.Style.html - /// [`Text`]: struct.Text.html - /// [`Node::with_measure`]: ../../struct.Node.html#method.with_measure - fn node(&self, style: Style, content: &str, size: Option) -> Node; - - /// Draws a [`Text`] fragment. - /// - /// It receives: - /// * the bounds of the [`Text`] - /// * the contents of the [`Text`] - /// * the size of the [`Text`] - /// * the color of the [`Text`] - /// * the [`HorizontalAlignment`] of the [`Text`] - /// * the [`VerticalAlignment`] of the [`Text`] - /// - /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html - /// [`VerticalAlignment`]: enum.VerticalAlignment.html - fn draw( - &mut self, - bounds: Rectangle, - content: &str, - size: Option, - color: Option, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, - ); -} - -impl<'a, Message, Renderer, Color> From> - for Element<'a, Message, Renderer> -where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer, -{ - fn from(text: Text) -> Element<'a, Message, Renderer> { - Element::new(text) - } -} - -/// The horizontal alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum HorizontalAlignment { - /// Align left - Left, - - /// Horizontally centered - Center, - - /// Align right - Right, -} - -/// The vertical alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum VerticalAlignment { - /// Align top - Top, - - /// Vertically centered - Center, - - /// Align bottom - Bottom, -} diff --git a/examples/Cargo.toml b/examples/Cargo.toml deleted file mode 100644 index fdf854cd..00000000 --- a/examples/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "iced_examples" -version = "0.0.0" -authors = ["Héctor Ramón Jiménez "] -publish = false -edition = "2018" - -[[bin]] -name = "tour" -path = "tour/main.rs" - -[dependencies] -iced = { version = "0.1.0-alpha", path = "../core" } - -# A personal `ggez` fork that introduces a `FontCache` type to measure text -# efficiently and fixes HiDPI issues. -ggez = { version = "0.5", git = "https://github.com/hecrj/ggez.git" } diff --git a/examples/README.md b/examples/README.md index 0a8a126e..2032df49 100644 --- a/examples/README.md +++ b/examples/README.md @@ -26,16 +26,16 @@ The implementation consists of different modules: the [`renderer`]. ``` -cargo run --example tour +cargo run --package iced_ggez_tour ``` [![Tour - Iced][gui_gif]][gui_gfycat] [`ggez`]: https://github.com/ggez/ggez -[`tour`]: tour/tour.rs -[`renderer`]: tour/renderer -[`widget`]: tour/widget.rs -[`main`]: tour/main.rs +[`tour`]: tour/src/tour.rs +[`renderer`]: tour/src/renderer +[`widget`]: tour/src/widget.rs +[`main`]: tour/src/main.rs [personal fork]: https://github.com/hecrj/ggez [add a `FontCache` type]: https://github.com/ggez/ggez/pull/679 [fix some issues with HiDPI]: https://github.com/hecrj/ggez/commit/dfe2fd2423c51a6daf42c75f66dfaeaacd439fb1 diff --git a/examples/tour/main.rs b/examples/tour/main.rs index ce477fd9..571bc2e2 100644 --- a/examples/tour/main.rs +++ b/examples/tour/main.rs @@ -27,7 +27,7 @@ pub fn main() -> ggez::GameResult { filesystem::mount( context, std::path::Path::new(&format!( - "{}/resources", + "{}/examples/resources", env!("CARGO_MANIFEST_DIR") )), true, diff --git a/src/element.rs b/src/element.rs new file mode 100644 index 00000000..70d06f42 --- /dev/null +++ b/src/element.rs @@ -0,0 +1,370 @@ +use stretch::{geometry, result}; + +use crate::{ + renderer, Event, Hasher, Layout, MouseCursor, Node, Point, Widget, +}; + +/// A generic [`Widget`]. +/// +/// It is useful to build composable user interfaces that do not leak +/// implementation details in their __view logic__. +/// +/// If you have a [built-in widget], you should be able to use `Into` +/// to turn it into an [`Element`]. +/// +/// [built-in widget]: widget/index.html#built-in-widgets +/// [`Widget`]: widget/trait.Widget.html +/// [`Element`]: struct.Element.html +pub struct Element<'a, Message, Renderer> { + pub(crate) widget: Box + 'a>, +} + +impl<'a, Message, Renderer> std::fmt::Debug for Element<'a, Message, Renderer> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Element") + .field("widget", &self.widget) + .finish() + } +} + +impl<'a, Message, Renderer> Element<'a, Message, Renderer> { + /// Create a new [`Element`] containing the given [`Widget`]. + /// + /// [`Element`]: struct.Element.html + /// [`Widget`]: widget/trait.Widget.html + pub fn new( + widget: impl Widget + 'a, + ) -> Element<'a, Message, Renderer> { + Element { + widget: Box::new(widget), + } + } + + /// Applies a transformation to the produced message of the [`Element`]. + /// + /// This method is useful when you want to decouple different parts of your + /// UI and make them __composable__. + /// + /// [`Element`]: struct.Element.html + /// + /// # Example + /// Imagine we want to use [our counter](index.html#usage). But instead of + /// showing a single counter, we want to display many of them. We can reuse + /// the `Counter` type as it is! + /// + /// We use composition to model the __state__ of our new application: + /// + /// ``` + /// # mod counter { + /// # pub struct Counter; + /// # } + /// use counter::Counter; + /// + /// struct ManyCounters { + /// counters: Vec, + /// } + /// ``` + /// + /// We can store the state of multiple counters now. However, the + /// __messages__ we implemented before describe the user interactions + /// of a __single__ counter. Right now, we need to also identify which + /// counter is receiving user interactions. Can we use composition again? + /// Yes. + /// + /// ``` + /// # mod counter { + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message {} + /// # } + /// #[derive(Debug, Clone, Copy)] + /// pub enum Message { + /// Counter(usize, counter::Message) + /// } + /// ``` + /// + /// We compose the previous __messages__ with the index of the counter + /// producing them. Let's implement our __view logic__ now: + /// + /// ``` + /// # mod counter { + /// # use iced::{button, Button}; + /// # + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message {} + /// # pub struct Counter(button::State); + /// # + /// # impl Counter { + /// # pub fn view(&mut self) -> Button { + /// # Button::new(&mut self.0, "_") + /// # } + /// # } + /// # } + /// # + /// # mod iced_wgpu { + /// # use iced::{ + /// # button, MouseCursor, Node, Point, Rectangle, Style, + /// # }; + /// # pub struct Renderer; + /// # + /// # impl button::Renderer for Renderer { + /// # fn draw( + /// # &mut self, + /// # _cursor_position: Point, + /// # _bounds: Rectangle, + /// # _state: &button::State, + /// # _label: &str, + /// # _class: button::Class, + /// # ) -> MouseCursor { + /// # MouseCursor::OutOfBounds + /// # } + /// # } + /// # } + /// # + /// # use counter::Counter; + /// # + /// # struct ManyCounters { + /// # counters: Vec, + /// # } + /// # + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message { + /// # Counter(usize, counter::Message) + /// # } + /// use iced::{Element, Row}; + /// use iced_wgpu::Renderer; + /// + /// impl ManyCounters { + /// pub fn view(&mut self) -> Row { + /// // We can quickly populate a `Row` by folding over our counters + /// self.counters.iter_mut().enumerate().fold( + /// Row::new().spacing(20), + /// |row, (index, counter)| { + /// // We display the counter + /// let element: Element = + /// counter.view().into(); + /// + /// row.push( + /// // Here we turn our `Element` into + /// // an `Element` by combining the `index` and the + /// // message of the `element`. + /// element.map(move |message| Message::Counter(index, message)) + /// ) + /// } + /// ) + /// } + /// } + /// ``` + /// + /// Finally, our __update logic__ is pretty straightforward: simple + /// delegation. + /// + /// ``` + /// # mod counter { + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message {} + /// # pub struct Counter; + /// # + /// # impl Counter { + /// # pub fn update(&mut self, _message: Message) {} + /// # } + /// # } + /// # + /// # use counter::Counter; + /// # + /// # struct ManyCounters { + /// # counters: Vec, + /// # } + /// # + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message { + /// # Counter(usize, counter::Message) + /// # } + /// impl ManyCounters { + /// pub fn update(&mut self, message: Message) { + /// match message { + /// Message::Counter(index, counter_msg) => { + /// if let Some(counter) = self.counters.get_mut(index) { + /// counter.update(counter_msg); + /// } + /// } + /// } + /// } + /// } + /// ``` + pub fn map(self, f: F) -> Element<'a, B, Renderer> + where + Message: 'static + Copy, + Renderer: 'a, + B: 'static, + F: 'static + Fn(Message) -> B, + { + Element { + widget: Box::new(Map::new(self.widget, f)), + } + } + + /// Marks the [`Element`] as _to-be-explained_. + /// + /// The [`Renderer`] will explain the layout of the [`Element`] graphically. + /// This can be very useful for debugging your layout! + /// + /// [`Element`]: struct.Element.html + /// [`Renderer`]: trait.Renderer.html + pub fn explain( + self, + color: Renderer::Color, + ) -> Element<'a, Message, Renderer> + where + Message: 'static, + Renderer: 'a + renderer::Debugger, + { + Element { + widget: Box::new(Explain::new(self, color)), + } + } + + pub(crate) fn compute_layout(&self, renderer: &Renderer) -> result::Layout { + let node = self.widget.node(renderer); + + node.0.compute_layout(geometry::Size::undefined()).unwrap() + } + + pub(crate) fn hash_layout(&self, state: &mut Hasher) { + self.widget.hash_layout(state); + } +} + +struct Map<'a, A, B, Renderer> { + widget: Box + 'a>, + mapper: Box B>, +} + +impl<'a, A, B, Renderer> std::fmt::Debug for Map<'a, A, B, Renderer> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Map").field("widget", &self.widget).finish() + } +} + +impl<'a, A, B, Renderer> Map<'a, A, B, Renderer> { + pub fn new( + widget: Box + 'a>, + mapper: F, + ) -> Map<'a, A, B, Renderer> + where + F: 'static + Fn(A) -> B, + { + Map { + widget, + mapper: Box::new(mapper), + } + } +} + +impl<'a, A, B, Renderer> Widget for Map<'a, A, B, Renderer> +where + A: Copy, +{ + fn node(&self, renderer: &Renderer) -> Node { + self.widget.node(renderer) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + let mut original_messages = Vec::new(); + + self.widget.on_event( + event, + layout, + cursor_position, + &mut original_messages, + ); + + original_messages + .iter() + .cloned() + .for_each(|message| messages.push((self.mapper)(message))); + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + self.widget.draw(renderer, layout, cursor_position) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.widget.hash_layout(state); + } +} + +struct Explain<'a, Message, Renderer: renderer::Debugger> { + element: Element<'a, Message, Renderer>, + color: Renderer::Color, +} + +impl<'a, Message, Renderer> std::fmt::Debug for Explain<'a, Message, Renderer> +where + Renderer: renderer::Debugger, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Explain") + .field("element", &self.element) + .finish() + } +} + +impl<'a, Message, Renderer> Explain<'a, Message, Renderer> +where + Renderer: renderer::Debugger, +{ + fn new( + element: Element<'a, Message, Renderer>, + color: Renderer::Color, + ) -> Self { + Explain { element, color } + } +} + +impl<'a, Message, Renderer> Widget + for Explain<'a, Message, Renderer> +where + Renderer: renderer::Debugger, +{ + fn node(&self, renderer: &Renderer) -> Node { + self.element.widget.node(renderer) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + self.element + .widget + .on_event(event, layout, cursor_position, messages) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + renderer.explain(&layout, self.color); + + self.element.widget.draw(renderer, layout, cursor_position) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.element.widget.hash_layout(state); + } +} diff --git a/src/event.rs b/src/event.rs new file mode 100644 index 00000000..71f06006 --- /dev/null +++ b/src/event.rs @@ -0,0 +1,16 @@ +use crate::input::{keyboard, mouse}; + +/// A user interface event. +/// +/// _**Note:** This type is largely incomplete! If you need to track +/// additional events, feel free to [open an issue] and share your use case!_ +/// +/// [open an issue]: https://github.com/hecrj/iced/issues +#[derive(PartialEq, Clone, Copy, Debug)] +pub enum Event { + /// A keyboard event + Keyboard(keyboard::Event), + + /// A mouse event + Mouse(mouse::Event), +} diff --git a/src/hasher.rs b/src/hasher.rs new file mode 100644 index 00000000..9f6aacce --- /dev/null +++ b/src/hasher.rs @@ -0,0 +1,19 @@ +/// The hasher used to compare layouts. +#[derive(Debug)] +pub struct Hasher(twox_hash::XxHash64); + +impl Default for Hasher { + fn default() -> Self { + Hasher(twox_hash::XxHash64::default()) + } +} + +impl core::hash::Hasher for Hasher { + fn write(&mut self, bytes: &[u8]) { + self.0.write(bytes) + } + + fn finish(&self) -> u64 { + self.0.finish() + } +} diff --git a/src/input.rs b/src/input.rs new file mode 100644 index 00000000..097fa730 --- /dev/null +++ b/src/input.rs @@ -0,0 +1,7 @@ +//! Map your system events into input events that the runtime can understand. +pub mod keyboard; +pub mod mouse; + +mod button_state; + +pub use button_state::ButtonState; diff --git a/src/input/button_state.rs b/src/input/button_state.rs new file mode 100644 index 00000000..e9dc05d7 --- /dev/null +++ b/src/input/button_state.rs @@ -0,0 +1,24 @@ +/// The state of a button. +/// +/// If you are using [`winit`], consider enabling the `winit` feature to get +/// conversion implementations for free! +/// +/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ +#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] +pub enum ButtonState { + /// The button is pressed. + Pressed, + + /// The button is __not__ pressed. + Released, +} + +#[cfg(feature = "winit")] +impl From for ButtonState { + fn from(element_state: winit::event::ElementState) -> Self { + match element_state { + winit::event::ElementState::Pressed => ButtonState::Pressed, + winit::event::ElementState::Released => ButtonState::Released, + } + } +} diff --git a/src/input/keyboard.rs b/src/input/keyboard.rs new file mode 100644 index 00000000..57c24484 --- /dev/null +++ b/src/input/keyboard.rs @@ -0,0 +1,6 @@ +//! Build keyboard events. +mod event; +mod key_code; + +pub use event::Event; +pub use key_code::KeyCode; diff --git a/src/input/keyboard/event.rs b/src/input/keyboard/event.rs new file mode 100644 index 00000000..8118f112 --- /dev/null +++ b/src/input/keyboard/event.rs @@ -0,0 +1,23 @@ +use super::KeyCode; +use crate::input::ButtonState; + +#[derive(Debug, Clone, Copy, PartialEq)] +/// A keyboard event. +/// +/// _**Note:** This type is largely incomplete! If you need to track +/// additional events, feel free to [open an issue] and share your use case!_ +/// +/// [open an issue]: https://github.com/hecrj/iced/issues +pub enum Event { + /// A keyboard key was pressed or released. + Input { + /// The state of the key + state: ButtonState, + + /// The key identifier + key_code: KeyCode, + }, + + /// A unicode character was received. + CharacterReceived(char), +} diff --git a/src/input/keyboard/key_code.rs b/src/input/keyboard/key_code.rs new file mode 100644 index 00000000..207ddeac --- /dev/null +++ b/src/input/keyboard/key_code.rs @@ -0,0 +1,374 @@ +/// The symbolic name of a keyboard key. +/// +/// This is mostly the `KeyCode` type found in [`winit`]. +/// +/// If you are using [`winit`], consider enabling the `winit` feature to get +/// conversion implementations for free! +/// +/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ +#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] +#[repr(u32)] +#[allow(missing_docs)] +pub enum KeyCode { + /// The '1' key over the letters. + Key1, + /// The '2' key over the letters. + Key2, + /// The '3' key over the letters. + Key3, + /// The '4' key over the letters. + Key4, + /// The '5' key over the letters. + Key5, + /// The '6' key over the letters. + Key6, + /// The '7' key over the letters. + Key7, + /// The '8' key over the letters. + Key8, + /// The '9' key over the letters. + Key9, + /// The '0' key over the 'O' and 'P' keys. + Key0, + + A, + B, + C, + D, + E, + F, + G, + H, + I, + J, + K, + L, + M, + N, + O, + P, + Q, + R, + S, + T, + U, + V, + W, + X, + Y, + Z, + + /// The Escape key, next to F1 + Escape, + + F1, + F2, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + F10, + F11, + F12, + F13, + F14, + F15, + F16, + F17, + F18, + F19, + F20, + F21, + F22, + F23, + F24, + + /// Print Screen/SysRq + Snapshot, + /// Scroll Lock + Scroll, + /// Pause/Break key, next to Scroll lock + Pause, + + /// `Insert`, next to Backspace + Insert, + Home, + Delete, + End, + PageDown, + PageUp, + + Left, + Up, + Right, + Down, + + Backspace, + Enter, + Space, + + /// The "Compose" key on Linux + Compose, + + Caret, + + Numlock, + Numpad0, + Numpad1, + Numpad2, + Numpad3, + Numpad4, + Numpad5, + Numpad6, + Numpad7, + Numpad8, + Numpad9, + + AbntC1, + AbntC2, + Add, + Apostrophe, + Apps, + At, + Ax, + Backslash, + Calculator, + Capital, + Colon, + Comma, + Convert, + Decimal, + Divide, + Equals, + Grave, + Kana, + Kanji, + LAlt, + LBracket, + LControl, + LShift, + LWin, + Mail, + MediaSelect, + MediaStop, + Minus, + Multiply, + Mute, + MyComputer, + NavigateForward, // also called "Prior" + NavigateBackward, // also called "Next" + NextTrack, + NoConvert, + NumpadComma, + NumpadEnter, + NumpadEquals, + OEM102, + Period, + PlayPause, + Power, + PrevTrack, + RAlt, + RBracket, + RControl, + RShift, + RWin, + Semicolon, + Slash, + Sleep, + Stop, + Subtract, + Sysrq, + Tab, + Underline, + Unlabeled, + VolumeDown, + VolumeUp, + Wake, + WebBack, + WebFavorites, + WebForward, + WebHome, + WebRefresh, + WebSearch, + WebStop, + Yen, + Copy, + Paste, + Cut, +} + +#[cfg(feature = "winit")] +impl From for KeyCode { + fn from(virtual_keycode: winit::event::VirtualKeyCode) -> Self { + match virtual_keycode { + winit::event::VirtualKeyCode::Key1 => KeyCode::Key1, + winit::event::VirtualKeyCode::Key2 => KeyCode::Key2, + winit::event::VirtualKeyCode::Key3 => KeyCode::Key3, + winit::event::VirtualKeyCode::Key4 => KeyCode::Key4, + winit::event::VirtualKeyCode::Key5 => KeyCode::Key5, + winit::event::VirtualKeyCode::Key6 => KeyCode::Key6, + winit::event::VirtualKeyCode::Key7 => KeyCode::Key7, + winit::event::VirtualKeyCode::Key8 => KeyCode::Key8, + winit::event::VirtualKeyCode::Key9 => KeyCode::Key9, + winit::event::VirtualKeyCode::Key0 => KeyCode::Key0, + winit::event::VirtualKeyCode::A => KeyCode::A, + winit::event::VirtualKeyCode::B => KeyCode::B, + winit::event::VirtualKeyCode::C => KeyCode::C, + winit::event::VirtualKeyCode::D => KeyCode::D, + winit::event::VirtualKeyCode::E => KeyCode::E, + winit::event::VirtualKeyCode::F => KeyCode::F, + winit::event::VirtualKeyCode::G => KeyCode::G, + winit::event::VirtualKeyCode::H => KeyCode::H, + winit::event::VirtualKeyCode::I => KeyCode::I, + winit::event::VirtualKeyCode::J => KeyCode::J, + winit::event::VirtualKeyCode::K => KeyCode::K, + winit::event::VirtualKeyCode::L => KeyCode::L, + winit::event::VirtualKeyCode::M => KeyCode::M, + winit::event::VirtualKeyCode::N => KeyCode::N, + winit::event::VirtualKeyCode::O => KeyCode::O, + winit::event::VirtualKeyCode::P => KeyCode::P, + winit::event::VirtualKeyCode::Q => KeyCode::Q, + winit::event::VirtualKeyCode::R => KeyCode::R, + winit::event::VirtualKeyCode::S => KeyCode::S, + winit::event::VirtualKeyCode::T => KeyCode::T, + winit::event::VirtualKeyCode::U => KeyCode::U, + winit::event::VirtualKeyCode::V => KeyCode::V, + winit::event::VirtualKeyCode::W => KeyCode::W, + winit::event::VirtualKeyCode::X => KeyCode::X, + winit::event::VirtualKeyCode::Y => KeyCode::Y, + winit::event::VirtualKeyCode::Z => KeyCode::Z, + winit::event::VirtualKeyCode::Escape => KeyCode::Escape, + winit::event::VirtualKeyCode::F1 => KeyCode::F1, + winit::event::VirtualKeyCode::F2 => KeyCode::F2, + winit::event::VirtualKeyCode::F3 => KeyCode::F3, + winit::event::VirtualKeyCode::F4 => KeyCode::F4, + winit::event::VirtualKeyCode::F5 => KeyCode::F5, + winit::event::VirtualKeyCode::F6 => KeyCode::F6, + winit::event::VirtualKeyCode::F7 => KeyCode::F7, + winit::event::VirtualKeyCode::F8 => KeyCode::F8, + winit::event::VirtualKeyCode::F9 => KeyCode::F9, + winit::event::VirtualKeyCode::F10 => KeyCode::F10, + winit::event::VirtualKeyCode::F11 => KeyCode::F11, + winit::event::VirtualKeyCode::F12 => KeyCode::F12, + winit::event::VirtualKeyCode::F13 => KeyCode::F13, + winit::event::VirtualKeyCode::F14 => KeyCode::F14, + winit::event::VirtualKeyCode::F15 => KeyCode::F15, + winit::event::VirtualKeyCode::F16 => KeyCode::F16, + winit::event::VirtualKeyCode::F17 => KeyCode::F17, + winit::event::VirtualKeyCode::F18 => KeyCode::F18, + winit::event::VirtualKeyCode::F19 => KeyCode::F19, + winit::event::VirtualKeyCode::F20 => KeyCode::F20, + winit::event::VirtualKeyCode::F21 => KeyCode::F21, + winit::event::VirtualKeyCode::F22 => KeyCode::F22, + winit::event::VirtualKeyCode::F23 => KeyCode::F23, + winit::event::VirtualKeyCode::F24 => KeyCode::F24, + winit::event::VirtualKeyCode::Snapshot => KeyCode::Snapshot, + winit::event::VirtualKeyCode::Scroll => KeyCode::Scroll, + winit::event::VirtualKeyCode::Pause => KeyCode::Pause, + winit::event::VirtualKeyCode::Insert => KeyCode::Insert, + winit::event::VirtualKeyCode::Home => KeyCode::Home, + winit::event::VirtualKeyCode::Delete => KeyCode::Delete, + winit::event::VirtualKeyCode::End => KeyCode::End, + winit::event::VirtualKeyCode::PageDown => KeyCode::PageDown, + winit::event::VirtualKeyCode::PageUp => KeyCode::PageUp, + winit::event::VirtualKeyCode::Left => KeyCode::Left, + winit::event::VirtualKeyCode::Up => KeyCode::Up, + winit::event::VirtualKeyCode::Right => KeyCode::Right, + winit::event::VirtualKeyCode::Down => KeyCode::Down, + winit::event::VirtualKeyCode::Back => KeyCode::Backspace, + winit::event::VirtualKeyCode::Return => KeyCode::Enter, + winit::event::VirtualKeyCode::Space => KeyCode::Space, + winit::event::VirtualKeyCode::Compose => KeyCode::Compose, + winit::event::VirtualKeyCode::Caret => KeyCode::Caret, + winit::event::VirtualKeyCode::Numlock => KeyCode::Numlock, + winit::event::VirtualKeyCode::Numpad0 => KeyCode::Numpad0, + winit::event::VirtualKeyCode::Numpad1 => KeyCode::Numpad1, + winit::event::VirtualKeyCode::Numpad2 => KeyCode::Numpad2, + winit::event::VirtualKeyCode::Numpad3 => KeyCode::Numpad3, + winit::event::VirtualKeyCode::Numpad4 => KeyCode::Numpad4, + winit::event::VirtualKeyCode::Numpad5 => KeyCode::Numpad5, + winit::event::VirtualKeyCode::Numpad6 => KeyCode::Numpad6, + winit::event::VirtualKeyCode::Numpad7 => KeyCode::Numpad7, + winit::event::VirtualKeyCode::Numpad8 => KeyCode::Numpad8, + winit::event::VirtualKeyCode::Numpad9 => KeyCode::Numpad9, + winit::event::VirtualKeyCode::AbntC1 => KeyCode::AbntC1, + winit::event::VirtualKeyCode::AbntC2 => KeyCode::AbntC2, + winit::event::VirtualKeyCode::Add => KeyCode::Add, + winit::event::VirtualKeyCode::Apostrophe => KeyCode::Apostrophe, + winit::event::VirtualKeyCode::Apps => KeyCode::Apps, + winit::event::VirtualKeyCode::At => KeyCode::At, + winit::event::VirtualKeyCode::Ax => KeyCode::Ax, + winit::event::VirtualKeyCode::Backslash => KeyCode::Backslash, + winit::event::VirtualKeyCode::Calculator => KeyCode::Calculator, + winit::event::VirtualKeyCode::Capital => KeyCode::Capital, + winit::event::VirtualKeyCode::Colon => KeyCode::Colon, + winit::event::VirtualKeyCode::Comma => KeyCode::Comma, + winit::event::VirtualKeyCode::Convert => KeyCode::Convert, + winit::event::VirtualKeyCode::Decimal => KeyCode::Decimal, + winit::event::VirtualKeyCode::Divide => KeyCode::Divide, + winit::event::VirtualKeyCode::Equals => KeyCode::Equals, + winit::event::VirtualKeyCode::Grave => KeyCode::Grave, + winit::event::VirtualKeyCode::Kana => KeyCode::Kana, + winit::event::VirtualKeyCode::Kanji => KeyCode::Kanji, + winit::event::VirtualKeyCode::LAlt => KeyCode::LAlt, + winit::event::VirtualKeyCode::LBracket => KeyCode::LBracket, + winit::event::VirtualKeyCode::LControl => KeyCode::LControl, + winit::event::VirtualKeyCode::LShift => KeyCode::LShift, + winit::event::VirtualKeyCode::LWin => KeyCode::LWin, + winit::event::VirtualKeyCode::Mail => KeyCode::Mail, + winit::event::VirtualKeyCode::MediaSelect => KeyCode::MediaSelect, + winit::event::VirtualKeyCode::MediaStop => KeyCode::MediaStop, + winit::event::VirtualKeyCode::Minus => KeyCode::Minus, + winit::event::VirtualKeyCode::Multiply => KeyCode::Multiply, + winit::event::VirtualKeyCode::Mute => KeyCode::Mute, + winit::event::VirtualKeyCode::MyComputer => KeyCode::MyComputer, + winit::event::VirtualKeyCode::NavigateForward => { + KeyCode::NavigateForward + } + winit::event::VirtualKeyCode::NavigateBackward => { + KeyCode::NavigateBackward + } + winit::event::VirtualKeyCode::NextTrack => KeyCode::NextTrack, + winit::event::VirtualKeyCode::NoConvert => KeyCode::NoConvert, + winit::event::VirtualKeyCode::NumpadComma => KeyCode::NumpadComma, + winit::event::VirtualKeyCode::NumpadEnter => KeyCode::NumpadEnter, + winit::event::VirtualKeyCode::NumpadEquals => KeyCode::NumpadEquals, + winit::event::VirtualKeyCode::OEM102 => KeyCode::OEM102, + winit::event::VirtualKeyCode::Period => KeyCode::Period, + winit::event::VirtualKeyCode::PlayPause => KeyCode::PlayPause, + winit::event::VirtualKeyCode::Power => KeyCode::Power, + winit::event::VirtualKeyCode::PrevTrack => KeyCode::PrevTrack, + winit::event::VirtualKeyCode::RAlt => KeyCode::RAlt, + winit::event::VirtualKeyCode::RBracket => KeyCode::RBracket, + winit::event::VirtualKeyCode::RControl => KeyCode::RControl, + winit::event::VirtualKeyCode::RShift => KeyCode::RShift, + winit::event::VirtualKeyCode::RWin => KeyCode::RWin, + winit::event::VirtualKeyCode::Semicolon => KeyCode::Semicolon, + winit::event::VirtualKeyCode::Slash => KeyCode::Slash, + winit::event::VirtualKeyCode::Sleep => KeyCode::Sleep, + winit::event::VirtualKeyCode::Stop => KeyCode::Stop, + winit::event::VirtualKeyCode::Subtract => KeyCode::Subtract, + winit::event::VirtualKeyCode::Sysrq => KeyCode::Sysrq, + winit::event::VirtualKeyCode::Tab => KeyCode::Tab, + winit::event::VirtualKeyCode::Underline => KeyCode::Underline, + winit::event::VirtualKeyCode::Unlabeled => KeyCode::Unlabeled, + winit::event::VirtualKeyCode::VolumeDown => KeyCode::VolumeDown, + winit::event::VirtualKeyCode::VolumeUp => KeyCode::VolumeUp, + winit::event::VirtualKeyCode::Wake => KeyCode::Wake, + winit::event::VirtualKeyCode::WebBack => KeyCode::WebBack, + winit::event::VirtualKeyCode::WebFavorites => KeyCode::WebFavorites, + winit::event::VirtualKeyCode::WebForward => KeyCode::WebForward, + winit::event::VirtualKeyCode::WebHome => KeyCode::WebHome, + winit::event::VirtualKeyCode::WebRefresh => KeyCode::WebRefresh, + winit::event::VirtualKeyCode::WebSearch => KeyCode::WebSearch, + winit::event::VirtualKeyCode::WebStop => KeyCode::WebStop, + winit::event::VirtualKeyCode::Yen => KeyCode::Yen, + winit::event::VirtualKeyCode::Copy => KeyCode::Copy, + winit::event::VirtualKeyCode::Paste => KeyCode::Paste, + winit::event::VirtualKeyCode::Cut => KeyCode::Cut, + } + } +} diff --git a/src/input/mouse.rs b/src/input/mouse.rs new file mode 100644 index 00000000..d37f5b96 --- /dev/null +++ b/src/input/mouse.rs @@ -0,0 +1,6 @@ +//! Build mouse events. +mod button; +mod event; + +pub use button::Button; +pub use event::Event; diff --git a/src/input/mouse/button.rs b/src/input/mouse/button.rs new file mode 100644 index 00000000..6320d701 --- /dev/null +++ b/src/input/mouse/button.rs @@ -0,0 +1,32 @@ +/// The button of a mouse. +/// +/// If you are using [`winit`], consider enabling the `winit` feature to get +/// conversion implementations for free! +/// +/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ +#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] +pub enum Button { + /// The left mouse button. + Left, + + /// The right mouse button. + Right, + + /// The middle (wheel) button. + Middle, + + /// Some other button. + Other(u8), +} + +#[cfg(feature = "winit")] +impl From for super::Button { + fn from(mouse_button: winit::event::MouseButton) -> Self { + match mouse_button { + winit::event::MouseButton::Left => Button::Left, + winit::event::MouseButton::Right => Button::Right, + winit::event::MouseButton::Middle => Button::Middle, + winit::event::MouseButton::Other(other) => Button::Other(other), + } + } +} diff --git a/src/input/mouse/event.rs b/src/input/mouse/event.rs new file mode 100644 index 00000000..7b68208f --- /dev/null +++ b/src/input/mouse/event.rs @@ -0,0 +1,44 @@ +use super::Button; +use crate::input::ButtonState; + +/// A mouse event. +/// +/// _**Note:** This type is largely incomplete! If you need to track +/// additional events, feel free to [open an issue] and share your use case!_ +/// +/// [open an issue]: https://github.com/hecrj/iced/issues +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Event { + /// The mouse cursor entered the window. + CursorEntered, + + /// The mouse cursor left the window. + CursorLeft, + + /// The mouse cursor was moved + CursorMoved { + /// The X coordinate of the mouse position + x: f32, + + /// The Y coordinate of the mouse position + y: f32, + }, + + /// A mouse button was pressed or released. + Input { + /// The state of the button + state: ButtonState, + + /// The button identifier + button: Button, + }, + + /// The mouse wheel was scrolled. + WheelScrolled { + /// The number of horizontal lines scrolled + delta_x: f32, + + /// The number of vertical lines scrolled + delta_y: f32, + }, +} diff --git a/src/layout.rs b/src/layout.rs new file mode 100644 index 00000000..de284a43 --- /dev/null +++ b/src/layout.rs @@ -0,0 +1,62 @@ +use stretch::result; + +use crate::{Point, Rectangle, Vector}; + +/// The computed bounds of a [`Node`] and its children. +/// +/// This type is provided by the GUI runtime to [`Widget::on_event`] and +/// [`Widget::draw`], describing the layout of the [`Node`] produced by +/// [`Widget::node`]. +/// +/// [`Node`]: struct.Node.html +/// [`Widget::on_event`]: widget/trait.Widget.html#method.on_event +/// [`Widget::draw`]: widget/trait.Widget.html#tymethod.draw +/// [`Widget::node`]: widget/trait.Widget.html#tymethod.node +#[derive(Debug)] +pub struct Layout<'a> { + layout: &'a result::Layout, + position: Point, +} + +impl<'a> Layout<'a> { + pub(crate) fn new(layout: &'a result::Layout) -> Self { + Self::with_parent_position(layout, Point::new(0.0, 0.0)) + } + + fn with_parent_position( + layout: &'a result::Layout, + parent_position: Point, + ) -> Self { + let position = + parent_position + Vector::new(layout.location.x, layout.location.y); + + Layout { layout, position } + } + + /// Gets the bounds of the [`Layout`]. + /// + /// The returned [`Rectangle`] describes the position and size of a + /// [`Node`]. + /// + /// [`Layout`]: struct.Layout.html + /// [`Rectangle`]: struct.Rectangle.html + /// [`Node`]: struct.Node.html + pub fn bounds(&self) -> Rectangle { + Rectangle { + x: self.position.x, + y: self.position.y, + width: self.layout.size.width, + height: self.layout.size.height, + } + } + + /// Returns an iterator over the [`Layout`] of the children of a [`Node`]. + /// + /// [`Layout`]: struct.Layout.html + /// [`Node`]: struct.Node.html + pub fn children(&'a self) -> impl Iterator> { + self.layout.children.iter().map(move |layout| { + Layout::with_parent_position(layout, self.position) + }) + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 00000000..c1c18b41 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,229 @@ +//! Iced is a renderer-agnostic GUI library focused on simplicity and +//! type-safety. Inspired by [Elm]. +//! +//! # Features +//! * Simple, easy-to-use, renderer-agnostic API +//! * Responsive, flexbox-based layouting +//! * Type-safe, reactive programming model +//! * Built-in widgets +//! * Custom widget support +//! +//! Check out the [repository] and the [examples] for more details! +//! +//! [examples]: https://github.com/hecrj/iced/tree/0.1.0/examples +//! [repository]: https://github.com/hecrj/iced +//! +//! # Usage +//! Inspired by [The Elm Architecture], Iced expects you to split user interfaces +//! into four different concepts: +//! +//! * __State__ — the state of your application +//! * __Messages__ — user interactions or meaningful events that you care +//! about +//! * __View logic__ — a way to display your __state__ as widgets that +//! may produce __messages__ on user interaction +//! * __Update logic__ — a way to react to __messages__ and update your +//! __state__ +//! +//! We can build something to see how this works! Let's say we want a simple counter +//! that can be incremented and decremented using two buttons. +//! +//! We start by modelling the __state__ of our application: +//! +//! ``` +//! use iced::button; +//! +//! struct Counter { +//! // The counter value +//! value: i32, +//! +//! // The local state of the two buttons +//! increment_button: button::State, +//! decrement_button: button::State, +//! } +//! ``` +//! +//! Next, we need to define the possible user interactions of our counter: +//! the button presses. These interactions are our __messages__: +//! +//! ``` +//! #[derive(Debug, Clone, Copy)] +//! pub enum Message { +//! IncrementPressed, +//! DecrementPressed, +//! } +//! ``` +//! +//! Now, let's show the actual counter by putting it all together in our +//! __view logic__: +//! +//! ``` +//! # use iced::button; +//! # +//! # struct Counter { +//! # // The counter value +//! # value: i32, +//! # +//! # // The local state of the two buttons +//! # increment_button: button::State, +//! # decrement_button: button::State, +//! # } +//! # +//! # #[derive(Debug, Clone, Copy)] +//! # pub enum Message { +//! # IncrementPressed, +//! # DecrementPressed, +//! # } +//! # +//! # mod iced_wgpu { +//! # use iced::{ +//! # button, text, text::HorizontalAlignment, text::VerticalAlignment, +//! # MouseCursor, Node, Point, Rectangle, Style, +//! # }; +//! # +//! # pub struct Renderer {} +//! # +//! # impl button::Renderer for Renderer { +//! # fn draw( +//! # &mut self, +//! # _cursor_position: Point, +//! # _bounds: Rectangle, +//! # _state: &button::State, +//! # _label: &str, +//! # _class: button::Class, +//! # ) -> MouseCursor { +//! # MouseCursor::OutOfBounds +//! # } +//! # } +//! # +//! # impl text::Renderer<[f32; 4]> for Renderer { +//! # fn node(&self, style: Style, _content: &str, _size: Option) -> Node { +//! # Node::new(style) +//! # } +//! # +//! # fn draw( +//! # &mut self, +//! # _bounds: Rectangle, +//! # _content: &str, +//! # _size: Option, +//! # _color: Option<[f32; 4]>, +//! # _horizontal_alignment: HorizontalAlignment, +//! # _vertical_alignment: VerticalAlignment, +//! # ) { +//! # } +//! # } +//! # } +//! use iced::{Button, Column, Text}; +//! use iced_wgpu::Renderer; // Iced does not include a renderer! We need to bring our own! +//! +//! impl Counter { +//! pub fn view(&mut self) -> Column { +//! // We use a column: a simple vertical layout +//! Column::new() +//! .push( +//! // The increment button. We tell it to produce an +//! // `IncrementPressed` message when pressed +//! Button::new(&mut self.increment_button, "+") +//! .on_press(Message::IncrementPressed), +//! ) +//! .push( +//! // We show the value of the counter here +//! Text::new(&self.value.to_string()).size(50), +//! ) +//! .push( +//! // The decrement button. We tell it to produce a +//! // `DecrementPressed` message when pressed +//! Button::new(&mut self.decrement_button, "-") +//! .on_press(Message::DecrementPressed), +//! ) +//! } +//! } +//! ``` +//! +//! Finally, we need to be able to react to any produced __messages__ and change +//! our __state__ accordingly in our __update logic__: +//! +//! ``` +//! # use iced::button; +//! # +//! # struct Counter { +//! # // The counter value +//! # value: i32, +//! # +//! # // The local state of the two buttons +//! # increment_button: button::State, +//! # decrement_button: button::State, +//! # } +//! # +//! # #[derive(Debug, Clone, Copy)] +//! # pub enum Message { +//! # IncrementPressed, +//! # DecrementPressed, +//! # } +//! impl Counter { +//! // ... +//! +//! pub fn update(&mut self, message: Message) { +//! match message { +//! Message::IncrementPressed => { +//! self.value += 1; +//! } +//! Message::DecrementPressed => { +//! self.value -= 1; +//! } +//! } +//! } +//! } +//! ``` +//! +//! And that's everything! We just wrote a whole user interface. Iced is now able +//! to: +//! +//! 1. Take the result of our __view logic__ and layout its widgets. +//! 1. Process events from our system and produce __messages__ for our +//! __update logic__. +//! 1. Draw the resulting user interface using our chosen __renderer__. +//! +//! Check out the [`UserInterface`] type to learn how to wire everything up! +//! +//! [Elm]: https://elm-lang.org/ +//! [The Elm Architecture]: https://guide.elm-lang.org/architecture/ +//! [documentation]: https://docs.rs/iced +//! [examples]: https://github.com/hecrj/iced/tree/master/examples +//! [`UserInterface`]: struct.UserInterface.html +#![deny(missing_docs)] +#![deny(missing_debug_implementations)] +#![deny(unused_results)] +#![deny(unsafe_code)] +#![deny(rust_2018_idioms)] +pub mod input; +pub mod renderer; +pub mod widget; + +mod element; +mod event; +mod hasher; +mod layout; +mod mouse_cursor; +mod node; +mod point; +mod rectangle; +mod style; +mod user_interface; +mod vector; + +#[doc(no_inline)] +pub use stretch::{geometry::Size, number::Number}; + +pub use element::Element; +pub use event::Event; +pub use hasher::Hasher; +pub use layout::Layout; +pub use mouse_cursor::MouseCursor; +pub use node::Node; +pub use point::Point; +pub use rectangle::Rectangle; +pub use style::{Align, Justify, Style}; +pub use user_interface::{Cache, UserInterface}; +pub(crate) use vector::Vector; +pub use widget::*; diff --git a/src/mouse_cursor.rs b/src/mouse_cursor.rs new file mode 100644 index 00000000..4ef6361a --- /dev/null +++ b/src/mouse_cursor.rs @@ -0,0 +1,35 @@ +/// The state of the mouse cursor. +#[derive(Debug, Eq, PartialEq, Clone, Copy)] +pub enum MouseCursor { + /// The cursor is out of the bounds of the user interface. + OutOfBounds, + + /// The cursor is over a non-interactive widget. + Idle, + + /// The cursor is over a clickable widget. + Pointer, + + /// The cursor is over a busy widget. + Working, + + /// The cursor is over a grabbable widget. + Grab, + + /// The cursor is grabbing a widget. + Grabbing, +} + +#[cfg(feature = "winit")] +impl From for winit::window::CursorIcon { + fn from(mouse_cursor: MouseCursor) -> winit::window::CursorIcon { + match mouse_cursor { + MouseCursor::OutOfBounds => winit::window::CursorIcon::Default, + MouseCursor::Idle => winit::window::CursorIcon::Default, + MouseCursor::Pointer => winit::window::CursorIcon::Hand, + MouseCursor::Working => winit::window::CursorIcon::Progress, + MouseCursor::Grab => winit::window::CursorIcon::Grab, + MouseCursor::Grabbing => winit::window::CursorIcon::Grabbing, + } + } +} diff --git a/src/node.rs b/src/node.rs new file mode 100644 index 00000000..1db10d7f --- /dev/null +++ b/src/node.rs @@ -0,0 +1,60 @@ +use stretch::node; + +use crate::{Number, Size, Style}; + +/// The visual requirements of a [`Widget`] and its children. +/// +/// When there have been changes and the [`Layout`] needs to be recomputed, the +/// runtime obtains a [`Node`] by calling [`Widget::node`]. +/// +/// [`Style`]: struct.Style.html +/// [`Widget`]: widget/trait.Widget.html +/// [`Node`]: struct.Node.html +/// [`Widget::node`]: widget/trait.Widget.html#tymethod.node +/// [`Layout`]: struct.Layout.html +#[derive(Debug)] +pub struct Node(pub(crate) node::Node); + +impl Node { + /// Creates a new [`Node`] with the given [`Style`]. + /// + /// [`Node`]: struct.Node.html + /// [`Style`]: struct.Style.html + pub fn new(style: Style) -> Node { + Self::with_children(style, Vec::new()) + } + + /// Creates a new [`Node`] with the given [`Style`] and a measure function. + /// + /// This type of node cannot have any children. + /// + /// You should use this when your [`Widget`] can adapt its contents to the + /// size of its container. The measure function will receive the container + /// size as a parameter and must compute the size of the [`Node`] inside + /// the given bounds (if the `Number` for a dimension is `Undefined` it + /// means that it has no boundary). + /// + /// [`Node`]: struct.Node.html + /// [`Style`]: struct.Style.html + /// [`Widget`]: widget/trait.Widget.html + pub fn with_measure(style: Style, measure: F) -> Node + where + F: 'static + Fn(Size) -> Size, + { + Node(node::Node::new_leaf( + style.0, + Box::new(move |size| Ok(measure(size))), + )) + } + + /// Creates a new [`Node`] with the given [`Style`] and children. + /// + /// [`Node`]: struct.Node.html + /// [`Style`]: struct.Style.html + pub fn with_children(style: Style, children: Vec) -> Node { + Node(node::Node::new( + style.0, + children.iter().map(|c| &c.0).collect(), + )) + } +} diff --git a/src/point.rs b/src/point.rs new file mode 100644 index 00000000..183998dd --- /dev/null +++ b/src/point.rs @@ -0,0 +1,31 @@ +use crate::Vector; + +/// A 2D point. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Point { + /// The X coordinate. + pub x: f32, + + /// The Y coordinate. + pub y: f32, +} + +impl Point { + /// Creates a new [`Point`] with the given coordinates. + /// + /// [`Point`]: struct.Point.html + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} + +impl std::ops::Add for Point { + type Output = Self; + + fn add(self, vector: Vector) -> Self { + Self { + x: self.x + vector.x, + y: self.y + vector.y, + } + } +} diff --git a/src/rectangle.rs b/src/rectangle.rs new file mode 100644 index 00000000..95c2570c --- /dev/null +++ b/src/rectangle.rs @@ -0,0 +1,30 @@ +use crate::Point; + +/// A rectangle. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Rectangle { + /// X coordinate of the top-left corner. + pub x: T, + + /// Y coordinate of the top-left corner. + pub y: T, + + /// Width of the rectangle. + pub width: T, + + /// Height of the rectangle. + pub height: T, +} + +impl Rectangle { + /// Returns true if the given [`Point`] is contained in the [`Rectangle`]. + /// + /// [`Point`]: struct.Point.html + /// [`Rectangle`]: struct.Rectangle.html + pub fn contains(&self, point: Point) -> bool { + self.x <= point.x + && point.x <= self.x + self.width + && self.y <= point.y + && point.y <= self.y + self.height + } +} diff --git a/src/renderer.rs b/src/renderer.rs new file mode 100644 index 00000000..b445190b --- /dev/null +++ b/src/renderer.rs @@ -0,0 +1,45 @@ +//! Write your own renderer. +//! +//! There is not a common entrypoint or trait for a __renderer__ in Iced. +//! Instead, every [`Widget`] constrains its generic `Renderer` type as +//! necessary. +//! +//! This approach is flexible and composable. For instance, the +//! [`Text`] widget only needs a [`text::Renderer`] while a [`Checkbox`] widget +//! needs both a [`text::Renderer`] and a [`checkbox::Renderer`], reusing logic. +//! +//! In the end, a __renderer__ satisfying all the constraints is +//! needed to build a [`UserInterface`]. +//! +//! [`Widget`]: ../widget/trait.Widget.html +//! [`UserInterface`]: ../struct.UserInterface.html +//! [`Text`]: ../widget/text/struct.Text.html +//! [`text::Renderer`]: ../widget/text/trait.Renderer.html +//! [`Checkbox`]: ../widget/checkbox/struct.Checkbox.html +//! [`checkbox::Renderer`]: ../widget/checkbox/trait.Renderer.html +use crate::Layout; + +/// A renderer able to graphically explain a [`Layout`]. +/// +/// [`Layout`]: ../struct.Layout.html +pub trait Debugger { + /// The color type that will be used to configure the _explanation_. + /// + /// This is the type that will be asked in [`Element::explain`]. + /// + /// [`Element::explain`]: ../struct.Element.html#method.explain + type Color: Copy; + + /// Explains the [`Layout`] of an [`Element`] for debugging purposes. + /// + /// This will be called when [`Element::explain`] has been used. It should + /// _explain_ the given [`Layout`] graphically. + /// + /// A common approach consists in recursively rendering the bounds of the + /// [`Layout`] and its children. + /// + /// [`Layout`]: struct.Layout.html + /// [`Element`]: struct.Element.html + /// [`Element::explain`]: struct.Element.html#method.explain + fn explain(&mut self, layout: &Layout<'_>, color: Self::Color); +} diff --git a/src/style.rs b/src/style.rs new file mode 100644 index 00000000..575ea366 --- /dev/null +++ b/src/style.rs @@ -0,0 +1,262 @@ +use std::hash::{Hash, Hasher}; +use stretch::{geometry, style}; + +/// The appearance of a [`Node`]. +/// +/// [`Node`]: struct.Node.html +#[derive(Debug, Clone, Copy)] +pub struct Style(pub(crate) style::Style); + +impl Style { + /// Defines the width of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn width(mut self, width: u16) -> Self { + self.0.size.width = style::Dimension::Points(width as f32); + self + } + + /// Defines the height of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn height(mut self, height: u16) -> Self { + self.0.size.height = style::Dimension::Points(height as f32); + self + } + + /// Defines the minimum width of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn min_width(mut self, min_width: u16) -> Self { + self.0.min_size.width = style::Dimension::Points(min_width as f32); + self + } + + /// Defines the maximum width of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn max_width(mut self, max_width: u16) -> Self { + self.0.max_size.width = style::Dimension::Points(max_width as f32); + self.fill_width() + } + + /// Defines the minimum height of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn min_height(mut self, min_height: u16) -> Self { + self.0.min_size.height = + style::Dimension::Points(f32::from(min_height)); + self + } + + /// Defines the maximum height of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn max_height(mut self, max_height: u16) -> Self { + self.0.max_size.height = + style::Dimension::Points(f32::from(max_height)); + self.fill_height() + } + + /// Makes a [`Node`] fill all the horizontal available space. + /// + /// [`Node`]: struct.Node.html + pub fn fill_width(mut self) -> Self { + self.0.size.width = stretch::style::Dimension::Percent(1.0); + self + } + + /// Makes a [`Node`] fill all the vertical available space. + /// + /// [`Node`]: struct.Node.html + pub fn fill_height(mut self) -> Self { + self.0.size.height = stretch::style::Dimension::Percent(1.0); + self + } + + pub(crate) fn align_items(mut self, align: Align) -> Self { + self.0.align_items = align.into(); + self + } + + pub(crate) fn justify_content(mut self, justify: Justify) -> Self { + self.0.justify_content = justify.into(); + self + } + + /// Sets the alignment of a [`Node`]. + /// + /// If the [`Node`] is inside a... + /// + /// * [`Column`], this setting will affect its __horizontal__ alignment. + /// * [`Row`], this setting will affect its __vertical__ alignment. + /// + /// [`Node`]: struct.Node.html + /// [`Column`]: widget/struct.Column.html + /// [`Row`]: widget/struct.Row.html + pub fn align_self(mut self, align: Align) -> Self { + self.0.align_self = align.into(); + self + } + + /// Sets the padding of a [`Node`] in pixels. + /// + /// [`Node`]: struct.Node.html + pub fn padding(mut self, px: u16) -> Self { + self.0.padding = stretch::geometry::Rect { + start: style::Dimension::Points(px as f32), + end: style::Dimension::Points(px as f32), + top: style::Dimension::Points(px as f32), + bottom: style::Dimension::Points(px as f32), + }; + + self + } +} + +impl Default for Style { + fn default() -> Style { + Style(style::Style { + align_items: style::AlignItems::FlexStart, + justify_content: style::JustifyContent::FlexStart, + ..style::Style::default() + }) + } +} + +impl Hash for Style { + fn hash(&self, state: &mut H) { + hash_size(&self.0.size, state); + hash_size(&self.0.min_size, state); + hash_size(&self.0.max_size, state); + + hash_rect(&self.0.margin, state); + + (self.0.flex_direction as u8).hash(state); + (self.0.align_items as u8).hash(state); + (self.0.justify_content as u8).hash(state); + (self.0.align_self as u8).hash(state); + (self.0.flex_grow as u32).hash(state); + } +} + +fn hash_size( + size: &geometry::Size, + state: &mut H, +) { + hash_dimension(size.width, state); + hash_dimension(size.height, state); +} + +fn hash_rect( + rect: &geometry::Rect, + state: &mut H, +) { + hash_dimension(rect.start, state); + hash_dimension(rect.end, state); + hash_dimension(rect.top, state); + hash_dimension(rect.bottom, state); +} + +fn hash_dimension(dimension: style::Dimension, state: &mut H) { + match dimension { + style::Dimension::Undefined => state.write_u8(0), + style::Dimension::Auto => state.write_u8(1), + style::Dimension::Points(points) => { + state.write_u8(2); + (points as u32).hash(state); + } + style::Dimension::Percent(percent) => { + state.write_u8(3); + (percent as u32).hash(state); + } + } +} + +/// Alignment on the cross axis of a container. +/// +/// * On a [`Column`], it describes __horizontal__ alignment. +/// * On a [`Row`], it describes __vertical__ alignment. +/// +/// [`Column`]: widget/struct.Column.html +/// [`Row`]: widget/struct.Row.html +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Align { + /// Align at the start of the cross axis. + Start, + + /// Align at the center of the cross axis. + Center, + + /// Align at the end of the cross axis. + End, + + /// Stretch over the cross axis. + Stretch, +} + +#[doc(hidden)] +impl From for style::AlignItems { + fn from(align: Align) -> Self { + match align { + Align::Start => style::AlignItems::FlexStart, + Align::Center => style::AlignItems::Center, + Align::End => style::AlignItems::FlexEnd, + Align::Stretch => style::AlignItems::Stretch, + } + } +} + +#[doc(hidden)] +impl From for style::AlignSelf { + fn from(align: Align) -> Self { + match align { + Align::Start => style::AlignSelf::FlexStart, + Align::Center => style::AlignSelf::Center, + Align::End => style::AlignSelf::FlexEnd, + Align::Stretch => style::AlignSelf::Stretch, + } + } +} + +/// Distribution on the main axis of a container. +/// +/// * On a [`Column`], it describes __vertical__ distribution. +/// * On a [`Row`], it describes __horizontal__ distribution. +/// +/// [`Column`]: widget/struct.Column.html +/// [`Row`]: widget/struct.Row.html +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Justify { + /// Place items at the start of the main axis. + Start, + + /// Place items at the center of the main axis. + Center, + + /// Place items at the end of the main axis. + End, + + /// Place items with space between. + SpaceBetween, + + /// Place items with space around. + SpaceAround, + + /// Place items with evenly distributed space. + SpaceEvenly, +} + +#[doc(hidden)] +impl From for style::JustifyContent { + fn from(justify: Justify) -> Self { + match justify { + Justify::Start => style::JustifyContent::FlexStart, + Justify::Center => style::JustifyContent::Center, + Justify::End => style::JustifyContent::FlexEnd, + Justify::SpaceBetween => style::JustifyContent::SpaceBetween, + Justify::SpaceAround => style::JustifyContent::SpaceAround, + Justify::SpaceEvenly => style::JustifyContent::SpaceEvenly, + } + } +} diff --git a/src/user_interface.rs b/src/user_interface.rs new file mode 100644 index 00000000..2c7cbf82 --- /dev/null +++ b/src/user_interface.rs @@ -0,0 +1,323 @@ +use crate::{input::mouse, Column, Element, Event, Layout, MouseCursor, Point}; + +use std::hash::Hasher; +use stretch::result; + +/// A set of interactive graphical elements with a specific [`Layout`]. +/// +/// It can be updated and drawn. +/// +/// Iced tries to avoid dictating how to write your event loop. You are in +/// charge of using this type in your system in any way you want. +/// +/// [`Layout`]: struct.Layout.html +#[derive(Debug)] +pub struct UserInterface<'a, Message, Renderer> { + hash: u64, + root: Element<'a, Message, Renderer>, + layout: result::Layout, + cursor_position: Point, +} + +impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { + /// Builds a user interface for an [`Element`]. + /// + /// It is able to avoid expensive computations when using a [`Cache`] + /// obtained from a previous instance of a [`UserInterface`]. + /// + /// [`Element`]: struct.Element.html + /// [`Cache`]: struct.Cache.html + /// [`UserInterface`]: struct.UserInterface.html + /// + /// # Example + /// Imagine we want to build a [`UserInterface`] for + /// [the counter example that we previously wrote](index.html#usage). Here + /// is naive way to set up our application loop: + /// + /// ```no_run + /// use iced::{UserInterface, Cache}; + /// use iced_wgpu::Renderer; + /// + /// # mod iced_wgpu { + /// # pub struct Renderer; + /// # + /// # impl Renderer { + /// # pub fn new() -> Self { Renderer } + /// # } + /// # } + /// # + /// # use iced::Column; + /// # + /// # pub struct Counter; + /// # + /// # impl Counter { + /// # pub fn new() -> Self { Counter } + /// # pub fn view(&self) -> Column<(), Renderer> { + /// # Column::new() + /// # } + /// # } + /// // Initialization + /// let mut counter = Counter::new(); + /// let mut cache = Cache::new(); + /// let mut renderer = Renderer::new(); + /// + /// // Application loop + /// loop { + /// // Process system events here... + /// + /// // Build the user interface + /// let user_interface = UserInterface::build( + /// counter.view(), + /// cache, + /// &renderer, + /// ); + /// + /// // Update and draw the user interface here... + /// // ... + /// + /// // Obtain the cache for the next iteration + /// cache = user_interface.into_cache(); + /// } + /// ``` + pub fn build>>( + root: E, + cache: Cache, + renderer: &Renderer, + ) -> Self { + let root = root.into(); + + let hasher = &mut crate::Hasher::default(); + root.hash_layout(hasher); + + let hash = hasher.finish(); + + let layout = if hash == cache.hash { + cache.layout + } else { + root.compute_layout(renderer) + }; + + UserInterface { + hash, + root, + layout, + cursor_position: cache.cursor_position, + } + } + + /// Updates the [`UserInterface`] by processing each provided [`Event`]. + /// + /// It returns __messages__ that may have been produced as a result of user + /// interactions. You should feed these to your __update logic__. + /// + /// [`UserInterface`]: struct.UserInterface.html + /// [`Event`]: enum.Event.html + /// + /// # Example + /// Let's allow our [counter](index.html#usage) to change state by completing + /// [the previous example](#example): + /// + /// ```no_run + /// use iced::{UserInterface, Cache}; + /// use iced_wgpu::Renderer; + /// + /// # mod iced_wgpu { + /// # pub struct Renderer; + /// # + /// # impl Renderer { + /// # pub fn new() -> Self { Renderer } + /// # } + /// # } + /// # + /// # use iced::Column; + /// # + /// # pub struct Counter; + /// # + /// # impl Counter { + /// # pub fn new() -> Self { Counter } + /// # pub fn view(&self) -> Column<(), Renderer> { + /// # Column::new() + /// # } + /// # pub fn update(&mut self, message: ()) {} + /// # } + /// let mut counter = Counter::new(); + /// let mut cache = Cache::new(); + /// let mut renderer = Renderer::new(); + /// + /// // Initialize our event storage + /// let mut events = Vec::new(); + /// + /// loop { + /// // Process system events... + /// + /// let mut user_interface = UserInterface::build( + /// counter.view(), + /// cache, + /// &renderer, + /// ); + /// + /// // Update the user interface + /// let messages = user_interface.update(events.drain(..)); + /// + /// cache = user_interface.into_cache(); + /// + /// // Process the produced messages + /// for message in messages { + /// counter.update(message); + /// } + /// } + /// ``` + pub fn update( + &mut self, + events: impl Iterator, + ) -> Vec { + let mut messages = Vec::new(); + + for event in events { + if let Event::Mouse(mouse::Event::CursorMoved { x, y }) = event { + self.cursor_position = Point::new(x, y); + } + + self.root.widget.on_event( + event, + Layout::new(&self.layout), + self.cursor_position, + &mut messages, + ); + } + + messages + } + + /// Draws the [`UserInterface`] with the provided [`Renderer`]. + /// + /// It returns the current state of the [`MouseCursor`]. You should update + /// the icon of the mouse cursor accordingly in your system. + /// + /// [`UserInterface`]: struct.UserInterface.html + /// [`Renderer`]: trait.Renderer.html + /// [`MouseCursor`]: enum.MouseCursor.html + /// + /// # Example + /// We can finally draw our [counter](index.html#usage) by + /// [completing the last example](#example-1): + /// + /// ```no_run + /// use iced::{UserInterface, Cache}; + /// use iced_wgpu::Renderer; + /// + /// # mod iced_wgpu { + /// # pub struct Renderer; + /// # + /// # impl Renderer { + /// # pub fn new() -> Self { Renderer } + /// # } + /// # } + /// # + /// # use iced::Column; + /// # + /// # pub struct Counter; + /// # + /// # impl Counter { + /// # pub fn new() -> Self { Counter } + /// # pub fn view(&self) -> Column<(), Renderer> { + /// # Column::new() + /// # } + /// # pub fn update(&mut self, message: ()) {} + /// # } + /// let mut counter = Counter::new(); + /// let mut cache = Cache::new(); + /// let mut renderer = Renderer::new(); + /// let mut events = Vec::new(); + /// + /// loop { + /// // Process system events... + /// + /// let mut user_interface = UserInterface::build( + /// counter.view(), + /// cache, + /// &renderer, + /// ); + /// + /// let messages = user_interface.update(events.drain(..)); + /// + /// // Draw the user interface + /// let mouse_cursor = user_interface.draw(&mut renderer); + /// + /// cache = user_interface.into_cache(); + /// + /// for message in messages { + /// counter.update(message); + /// } + /// + /// // Update mouse cursor icon... + /// // Flush rendering operations... + /// } + /// ``` + pub fn draw(&self, renderer: &mut Renderer) -> MouseCursor { + self.root.widget.draw( + renderer, + Layout::new(&self.layout), + self.cursor_position, + ) + } + + /// Extract the [`Cache`] of the [`UserInterface`], consuming it in the + /// process. + /// + /// [`Cache`]: struct.Cache.html + /// [`UserInterface`]: struct.UserInterface.html + pub fn into_cache(self) -> Cache { + Cache { + hash: self.hash, + layout: self.layout, + cursor_position: self.cursor_position, + } + } +} + +/// Reusable data of a specific [`UserInterface`]. +/// +/// [`UserInterface`]: struct.UserInterface.html +#[derive(Debug, Clone)] +pub struct Cache { + hash: u64, + layout: result::Layout, + cursor_position: Point, +} + +impl Cache { + /// Creates an empty [`Cache`]. + /// + /// You should use this to initialize a [`Cache`] before building your first + /// [`UserInterface`]. + /// + /// [`Cache`]: struct.Cache.html + /// [`UserInterface`]: struct.UserInterface.html + pub fn new() -> Cache { + let root: Element<'_, (), ()> = Column::new().into(); + + let hasher = &mut crate::Hasher::default(); + root.hash_layout(hasher); + + Cache { + hash: hasher.finish(), + layout: root.compute_layout(&()), + cursor_position: Point::new(0.0, 0.0), + } + } +} + +impl Default for Cache { + fn default() -> Cache { + Cache::new() + } +} + +impl PartialEq for Cache { + fn eq(&self, other: &Cache) -> bool { + self.hash == other.hash && self.cursor_position == other.cursor_position + } +} + +impl Eq for Cache {} diff --git a/src/vector.rs b/src/vector.rs new file mode 100644 index 00000000..f45daab9 --- /dev/null +++ b/src/vector.rs @@ -0,0 +1,15 @@ +/// A 2D vector. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Vector { + pub x: f32, + pub y: f32, +} + +impl Vector { + /// Creates a new [`Vector`] with the given components. + /// + /// [`Vector`]: struct.Vector.html + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} diff --git a/src/widget.rs b/src/widget.rs new file mode 100644 index 00000000..30606934 --- /dev/null +++ b/src/widget.rs @@ -0,0 +1,114 @@ +//! Use the built-in widgets or create your own. +//! +//! # Built-in widgets +//! Every built-in drawable widget has its own module with a `Renderer` trait +//! that must be implemented by a [renderer] before being able to use it as +//! a [`Widget`]. +//! +//! # Custom widgets +//! If you want to implement a custom widget, you simply need to implement the +//! [`Widget`] trait. You can use the API of the built-in widgets as a guide or +//! source of inspiration. +//! +//! # Re-exports +//! For convenience, the contents of this module are available at the root +//! module. Therefore, you can directly type: +//! +//! ``` +//! use iced::{button, Button, Widget}; +//! ``` +//! +//! [`Widget`]: trait.Widget.html +//! [renderer]: ../renderer/index.html +mod column; +mod row; + +pub mod button; +pub mod checkbox; +pub mod image; +//pub mod progress_bar; +pub mod radio; +pub mod slider; +pub mod text; + +pub use button::Button; +pub use checkbox::Checkbox; +pub use column::Column; +pub use image::Image; +//pub use progress_bar::ProgressBar; +pub use radio::Radio; +pub use row::Row; +pub use slider::Slider; +pub use text::Text; + +use crate::{Event, Hasher, Layout, MouseCursor, Node, Point}; + +/// A component that displays information and allows interaction. +/// +/// If you want to build your own widgets, you will need to implement this +/// trait. +/// +/// [`Widget`]: trait.Widget.html +/// [`Element`]: ../struct.Element.html +pub trait Widget: std::fmt::Debug { + /// Returns the [`Node`] of the [`Widget`]. + /// + /// This [`Node`] is used by the runtime to compute the [`Layout`] of the + /// user interface. + /// + /// [`Node`]: ../struct.Node.html + /// [`Widget`]: trait.Widget.html + /// [`Layout`]: ../struct.Layout.html + fn node(&self, renderer: &Renderer) -> Node; + + /// Draws the [`Widget`] using the associated `Renderer`. + /// + /// It must return the [`MouseCursor`] state for the [`Widget`]. + /// + /// [`Widget`]: trait.Widget.html + /// [`MouseCursor`]: ../enum.MouseCursor.html + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor; + + /// Computes the _layout_ hash of the [`Widget`]. + /// + /// The produced hash is used by the runtime to decide if the [`Layout`] + /// needs to be recomputed between frames. Therefore, to ensure maximum + /// efficiency, the hash should only be affected by the properties of the + /// [`Widget`] that can affect layouting. + /// + /// For example, the [`Text`] widget does not hash its color property, as + /// its value cannot affect the overall [`Layout`] of the user interface. + /// + /// [`Widget`]: trait.Widget.html + /// [`Layout`]: ../struct.Layout.html + /// [`Text`]: text/struct.Text.html + fn hash_layout(&self, state: &mut Hasher); + + /// Processes a runtime [`Event`]. + /// + /// It receives: + /// * an [`Event`] describing user interaction + /// * the computed [`Layout`] of the [`Widget`] + /// * the current cursor position + /// * a mutable `Message` list, allowing the [`Widget`] to produce + /// new messages based on user interaction. + /// + /// By default, it does nothing. + /// + /// [`Event`]: ../enum.Event.html + /// [`Widget`]: trait.Widget.html + /// [`Layout`]: ../struct.Layout.html + fn on_event( + &mut self, + _event: Event, + _layout: Layout<'_>, + _cursor_position: Point, + _messages: &mut Vec, + ) { + } +} diff --git a/src/widget/button.rs b/src/widget/button.rs new file mode 100644 index 00000000..abcdbfeb --- /dev/null +++ b/src/widget/button.rs @@ -0,0 +1,282 @@ +//! Allow your users to perform actions by pressing a button. +//! +//! A [`Button`] has some local [`State`] and a [`Class`]. +//! +//! [`Button`]: struct.Button.html +//! [`State`]: struct.State.html +//! [`Class`]: enum.Class.html + +use crate::input::{mouse, ButtonState}; +use crate::{ + Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, + Style, Widget, +}; + +use std::hash::Hash; + +/// A generic widget that produces a message when clicked. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`button::Renderer`] trait. +/// +/// [`Widget`]: ../trait.Widget.html +/// [`button::Renderer`]: trait.Renderer.html +/// +/// # Example +/// +/// ``` +/// use iced::{button, Button}; +/// +/// pub enum Message { +/// ButtonClicked, +/// } +/// +/// let state = &mut button::State::new(); +/// +/// Button::new(state, "Click me!") +/// .on_press(Message::ButtonClicked); +/// ``` +/// +/// ![Button drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/button.png?raw=true) +pub struct Button<'a, Message> { + state: &'a mut State, + label: String, + class: Class, + on_press: Option, + style: Style, +} + +impl<'a, Message> std::fmt::Debug for Button<'a, Message> +where + Message: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Button") + .field("state", &self.state) + .field("label", &self.label) + .field("class", &self.class) + .field("on_press", &self.on_press) + .field("style", &self.style) + .finish() + } +} + +impl<'a, Message> Button<'a, Message> { + /// Creates a new [`Button`] with some local [`State`] and the given label. + /// + /// The default [`Class`] of a new [`Button`] is [`Class::Primary`]. + /// + /// [`Button`]: struct.Button.html + /// [`State`]: struct.State.html + /// [`Class`]: enum.Class.html + /// [`Class::Primary`]: enum.Class.html#variant.Primary + pub fn new(state: &'a mut State, label: &str) -> Self { + Button { + state, + label: String::from(label), + class: Class::Primary, + on_press: None, + style: Style::default().min_width(100), + } + } + + /// Sets the width of the [`Button`] in pixels. + /// + /// [`Button`]: struct.Button.html + pub fn width(mut self, width: u16) -> Self { + self.style = self.style.width(width); + self + } + + /// Makes the [`Button`] fill the horizontal space of its container. + /// + /// [`Button`]: struct.Button.html + pub fn fill_width(mut self) -> Self { + self.style = self.style.fill_width(); + self + } + + /// Sets the alignment of the [`Button`] itself. + /// + /// This is useful if you want to override the default alignment given by + /// the parent container. + /// + /// [`Button`]: struct.Button.html + pub fn align_self(mut self, align: Align) -> Self { + self.style = self.style.align_self(align); + self + } + + /// Sets the [`Class`] of the [`Button`]. + /// + /// + /// [`Button`]: struct.Button.html + /// [`Class`]: enum.Class.html + pub fn class(mut self, class: Class) -> Self { + self.class = class; + self + } + + /// Sets the message that will be produced when the [`Button`] is pressed. + /// + /// [`Button`]: struct.Button.html + pub fn on_press(mut self, msg: Message) -> Self { + self.on_press = Some(msg); + self + } +} + +impl<'a, Message, Renderer> Widget for Button<'a, Message> +where + Renderer: self::Renderer, + Message: Copy + std::fmt::Debug, +{ + fn node(&self, _renderer: &Renderer) -> Node { + Node::new(self.style.height(50)) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + match event { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state, + }) => { + if let Some(on_press) = self.on_press { + let bounds = layout.bounds(); + + match state { + ButtonState::Pressed => { + self.state.is_pressed = + bounds.contains(cursor_position); + } + ButtonState::Released => { + let is_clicked = self.state.is_pressed + && bounds.contains(cursor_position); + + self.state.is_pressed = false; + + if is_clicked { + messages.push(on_press); + } + } + } + } + } + _ => {} + } + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + renderer.draw( + cursor_position, + layout.bounds(), + self.state, + &self.label, + self.class, + ) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + } +} + +/// The local state of a [`Button`]. +/// +/// [`Button`]: struct.Button.html +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct State { + is_pressed: bool, +} + +impl State { + /// Creates a new [`State`]. + /// + /// [`State`]: struct.State.html + pub fn new() -> State { + State::default() + } + + /// Returns whether the associated [`Button`] is currently being pressed or + /// not. + /// + /// [`Button`]: struct.Button.html + pub fn is_pressed(&self) -> bool { + self.is_pressed + } +} + +/// The type of a [`Button`]. +/// +/// ![Different buttons drawn by the built-in renderer in Coffee](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/button_classes.png?raw=true) +/// +/// [`Button`]: struct.Button.html +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Class { + /// The [`Button`] performs the main action. + /// + /// [`Button`]: struct.Button.html + Primary, + + /// The [`Button`] performs an alternative action. + /// + /// [`Button`]: struct.Button.html + Secondary, + + /// The [`Button`] performs a productive action. + /// + /// [`Button`]: struct.Button.html + Positive, +} + +/// The renderer of a [`Button`]. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`Button`] in your user interface. +/// +/// [`Button`]: struct.Button.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Draws a [`Button`]. + /// + /// It receives: + /// * the current cursor position + /// * the bounds of the [`Button`] + /// * the local state of the [`Button`] + /// * the label of the [`Button`] + /// * the [`Class`] of the [`Button`] + /// + /// [`Button`]: struct.Button.html + /// [`State`]: struct.State.html + /// [`Class`]: enum.Class.html + fn draw( + &mut self, + cursor_position: Point, + bounds: Rectangle, + state: &State, + label: &str, + class: Class, + ) -> MouseCursor; +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + Message: 'static + Copy + std::fmt::Debug, +{ + fn from(button: Button<'a, Message>) -> Element<'a, Message, Renderer> { + Element::new(button) + } +} diff --git a/src/widget/checkbox.rs b/src/widget/checkbox.rs new file mode 100644 index 00000000..c60807fd --- /dev/null +++ b/src/widget/checkbox.rs @@ -0,0 +1,203 @@ +//! Show toggle controls using checkboxes. +use std::hash::Hash; + +use crate::input::{mouse, ButtonState}; +use crate::widget::{text, Column, Row, Text}; +use crate::{ + Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, + Widget, +}; + +/// A box that can be checked, with a generic text `Color`. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`checkbox::Renderer`] trait. +/// +/// [`Widget`]: ../trait.Widget.html +/// [`checkbox::Renderer`]: trait.Renderer.html +/// +/// # Example +/// +/// ``` +/// use iced::Checkbox; +/// +/// #[derive(Debug, Clone, Copy)] +/// pub enum Color { +/// Black, +/// } +/// +/// pub enum Message { +/// CheckboxToggled(bool), +/// } +/// +/// let is_checked = true; +/// +/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled) +/// .label_color(Color::Black); +/// ``` +/// +/// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true) +pub struct Checkbox { + is_checked: bool, + on_toggle: Box Message>, + label: String, + label_color: Option, +} + +impl std::fmt::Debug for Checkbox +where + Color: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Checkbox") + .field("is_checked", &self.is_checked) + .field("label", &self.label) + .field("label_color", &self.label_color) + .finish() + } +} + +impl Checkbox { + /// Creates a new [`Checkbox`]. + /// + /// It expects: + /// * a boolean describing whether the [`Checkbox`] is checked or not + /// * the label of the [`Checkbox`] + /// * a function that will be called when the [`Checkbox`] is toggled. + /// It will receive the new state of the [`Checkbox`] and must produce + /// a `Message`. + /// + /// [`Checkbox`]: struct.Checkbox.html + pub fn new(is_checked: bool, label: &str, f: F) -> Self + where + F: 'static + Fn(bool) -> Message, + { + Checkbox { + is_checked, + on_toggle: Box::new(f), + label: String::from(label), + label_color: None, + } + } + + /// Sets the `Color` of the label of the [`Checkbox`]. + /// + /// [`Checkbox`]: struct.Checkbox.html + pub fn label_color(mut self, color: Color) -> Self { + self.label_color = Some(color); + self + } +} + +impl Widget + for Checkbox +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer + text::Renderer, +{ + fn node(&self, renderer: &Renderer) -> Node { + Row::<(), Renderer>::new() + .spacing(15) + .align_items(Align::Center) + .push(Column::new().width(28).height(28)) + .push(Text::new(&self.label)) + .node(renderer) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + match event { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state: ButtonState::Pressed, + }) => { + let mouse_over = layout + .children() + .any(|child| child.bounds().contains(cursor_position)); + + if mouse_over { + messages.push((self.on_toggle)(!self.is_checked)); + } + } + _ => {} + } + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + let children: Vec<_> = layout.children().collect(); + + let text_bounds = children[1].bounds(); + + text::Renderer::draw( + renderer, + text_bounds, + &self.label, + None, + self.label_color, + text::HorizontalAlignment::Left, + text::VerticalAlignment::Top, + ); + + self::Renderer::draw( + renderer, + cursor_position, + children[0].bounds(), + text_bounds, + self.is_checked, + ) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.label.hash(state); + } +} + +/// The renderer of a [`Checkbox`]. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`Checkbox`] in your user interface. +/// +/// [`Checkbox`]: struct.Checkbox.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Draws a [`Checkbox`]. + /// + /// It receives: + /// * the current cursor position + /// * the bounds of the [`Checkbox`] + /// * the bounds of the label of the [`Checkbox`] + /// * whether the [`Checkbox`] is checked or not + /// + /// [`Checkbox`]: struct.Checkbox.html + fn draw( + &mut self, + cursor_position: Point, + bounds: Rectangle, + label_bounds: Rectangle, + is_checked: bool, + ) -> MouseCursor; +} + +impl<'a, Color, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer + text::Renderer, + Message: 'static, +{ + fn from( + checkbox: Checkbox, + ) -> Element<'a, Message, Renderer> { + Element::new(checkbox) + } +} diff --git a/src/widget/column.rs b/src/widget/column.rs new file mode 100644 index 00000000..ff754e98 --- /dev/null +++ b/src/widget/column.rs @@ -0,0 +1,224 @@ +use std::hash::Hash; + +use crate::{ + Align, Element, Event, Hasher, Justify, Layout, MouseCursor, Node, Point, + Style, Widget, +}; + +/// A container that distributes its contents vertically. +/// +/// A [`Column`] will try to fill the horizontal space of its container. +/// +/// [`Column`]: struct.Column.html +#[derive(Default)] +pub struct Column<'a, Message, Renderer> { + style: Style, + spacing: u16, + children: Vec>, +} + +impl<'a, Message, Renderer> std::fmt::Debug for Column<'a, Message, Renderer> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Column") + .field("style", &self.style) + .field("spacing", &self.spacing) + .field("children", &self.children) + .finish() + } +} + +impl<'a, Message, Renderer> Column<'a, Message, Renderer> { + /// Creates an empty [`Column`]. + /// + /// [`Column`]: struct.Column.html + pub fn new() -> Self { + let mut style = Style::default().fill_width(); + style.0.flex_direction = stretch::style::FlexDirection::Column; + + Column { + style, + spacing: 0, + children: Vec::new(), + } + } + + /// Sets the vertical spacing _between_ elements in pixels. + /// + /// Custom margins per element do not exist in Iced. You should use this + /// method instead! While less flexible, it helps you keep spacing between + /// elements consistent. + pub fn spacing(mut self, px: u16) -> Self { + self.spacing = px; + self + } + + /// Sets the padding of the [`Column`] in pixels. + /// + /// [`Column`]: struct.Column.html + pub fn padding(mut self, px: u16) -> Self { + self.style = self.style.padding(px); + self + } + + /// Sets the width of the [`Column`] in pixels. + /// + /// [`Column`]: struct.Column.html + pub fn width(mut self, width: u16) -> Self { + self.style = self.style.width(width); + self + } + + /// Sets the height of the [`Column`] in pixels. + /// + /// [`Column`]: struct.Column.html + pub fn height(mut self, height: u16) -> Self { + self.style = self.style.height(height); + self + } + + /// Sets the maximum width of the [`Column`] in pixels. + /// + /// [`Column`]: struct.Column.html + pub fn max_width(mut self, max_width: u16) -> Self { + self.style = self.style.max_width(max_width); + self + } + + /// Sets the maximum height of the [`Column`] in pixels. + /// + /// [`Column`]: struct.Column.html + pub fn max_height(mut self, max_height: u16) -> Self { + self.style = self.style.max_height(max_height); + self + } + + /// Sets the alignment of the [`Column`] itself. + /// + /// This is useful if you want to override the default alignment given by + /// the parent container. + /// + /// [`Column`]: struct.Column.html + pub fn align_self(mut self, align: Align) -> Self { + self.style = self.style.align_self(align); + self + } + + /// Sets the horizontal alignment of the contents of the [`Column`] . + /// + /// [`Column`]: struct.Column.html + pub fn align_items(mut self, align: Align) -> Self { + self.style = self.style.align_items(align); + self + } + + /// Sets the vertical distribution strategy for the contents of the + /// [`Column`] . + /// + /// [`Column`]: struct.Column.html + pub fn justify_content(mut self, justify: Justify) -> Self { + self.style = self.style.justify_content(justify); + self + } + + /// Adds an [`Element`] to the [`Column`]. + /// + /// [`Element`]: ../struct.Element.html + /// [`Column`]: struct.Column.html + pub fn push(mut self, child: E) -> Column<'a, Message, Renderer> + where + E: Into>, + { + self.children.push(child.into()); + self + } +} + +impl<'a, Message, Renderer> Widget + for Column<'a, Message, Renderer> +{ + fn node(&self, renderer: &Renderer) -> Node { + let mut children: Vec = self + .children + .iter() + .map(|child| { + let mut node = child.widget.node(renderer); + + let mut style = node.0.style(); + style.margin.bottom = + stretch::style::Dimension::Points(f32::from(self.spacing)); + + node.0.set_style(style); + node + }) + .collect(); + + if let Some(node) = children.last_mut() { + let mut style = node.0.style(); + style.margin.bottom = stretch::style::Dimension::Undefined; + + node.0.set_style(style); + } + + Node::with_children(self.style, children) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + self.children.iter_mut().zip(layout.children()).for_each( + |(child, layout)| { + child + .widget + .on_event(event, layout, cursor_position, messages) + }, + ); + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + let mut cursor = MouseCursor::OutOfBounds; + + self.children.iter().zip(layout.children()).for_each( + |(child, layout)| { + let new_cursor = + child.widget.draw(renderer, layout, cursor_position); + + if new_cursor != MouseCursor::OutOfBounds { + cursor = new_cursor; + } + }, + ); + + cursor + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + self.spacing.hash(state); + + for child in &self.children { + child.widget.hash_layout(state); + } + } +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: 'a, + Message: 'static, +{ + fn from( + column: Column<'a, Message, Renderer>, + ) -> Element<'a, Message, Renderer> { + Element::new(column) + } +} diff --git a/src/widget/image.rs b/src/widget/image.rs new file mode 100644 index 00000000..d94bfea5 --- /dev/null +++ b/src/widget/image.rs @@ -0,0 +1,178 @@ +//! Display images in your user interface. + +use crate::{ + Align, Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, + Widget, +}; + +use std::hash::Hash; + +/// A frame that displays an image while keeping aspect ratio. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`image::Renderer`] trait. +/// +/// [`Widget`]: ../../core/trait.Widget.html +/// [`image::Renderer`]: trait.Renderer.html +/// +/// # Example +/// +/// ``` +/// use iced::Image; +/// +/// # let my_handle = String::from("some_handle"); +/// let image = Image::new(my_handle); +/// ``` +pub struct Image { + image: I, + source: Option>, + width: Option, + height: Option, + style: Style, +} + +impl std::fmt::Debug for Image { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Image") + .field("source", &self.source) + .field("width", &self.width) + .field("height", &self.height) + .field("style", &self.style) + .finish() + } +} + +impl Image { + /// Creates a new [`Image`] with given image handle. + /// + /// [`Image`]: struct.Image.html + pub fn new(image: I) -> Self { + Image { + image, + source: None, + width: None, + height: None, + style: Style::default(), + } + } + + /// Sets the portion of the [`Image`] to draw. + /// + /// [`Image`]: struct.Image.html + pub fn clip(mut self, source: Rectangle) -> Self { + self.source = Some(source); + self + } + + /// Sets the width of the [`Image`] boundaries in pixels. + /// + /// [`Image`]: struct.Image.html + pub fn width(mut self, width: u16) -> Self { + self.width = Some(width); + self + } + + /// Sets the height of the [`Image`] boundaries in pixels. + /// + /// [`Image`]: struct.Image.html + pub fn height(mut self, height: u16) -> Self { + self.height = Some(height); + self + } + + /// Sets the alignment of the [`Image`] itself. + /// + /// This is useful if you want to override the default alignment given by + /// the parent container. + /// + /// [`Image`]: struct.Image.html + pub fn align_self(mut self, align: Align) -> Self { + self.style = self.style.align_self(align); + self + } +} + +impl Widget for Image +where + Renderer: self::Renderer, + I: Clone, +{ + fn node(&self, renderer: &Renderer) -> Node { + renderer.node( + self.style, + &self.image, + self.width, + self.height, + self.source, + ) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + renderer.draw(&self.image, layout.bounds(), self.source); + + MouseCursor::OutOfBounds + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + self.width.hash(state); + self.height.hash(state); + } +} + +/// The renderer of an [`Image`]. +/// +/// Your [renderer] will need to implement this trait before being able to use +/// an [`Image`] in your user interface. +/// +/// [`Image`]: struct.Image.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Creates a [`Node`] with the given [`Style`] for the provided [`Image`] + /// and its size. + /// + /// You should probably keep the original aspect ratio, if possible. + /// + /// [`Node`]: ../../struct.Node.html + /// [`Style`]: ../../struct.Style.html + /// [`Image`]: struct.Image.html + fn node( + &self, + style: Style, + image: &I, + width: Option, + height: Option, + source: Option>, + ) -> Node; + + /// Draws an [`Image`]. + /// + /// It receives: + /// * the bounds of the [`Image`] + /// * the handle of the loaded [`Image`] + /// * the portion of the image to draw. If not specified, the entire image + /// should be drawn. + /// + /// [`Image`]: struct.Image.html + fn draw( + &mut self, + image: &I, + bounds: Rectangle, + source: Option>, + ); +} + +impl<'a, I, Message, Renderer> From> for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + I: Clone + 'a, +{ + fn from(image: Image) -> Element<'a, Message, Renderer> { + Element::new(image) + } +} diff --git a/src/widget/panel.rs b/src/widget/panel.rs new file mode 100644 index 00000000..d43d6fb6 --- /dev/null +++ b/src/widget/panel.rs @@ -0,0 +1,94 @@ +use std::hash::Hash; + +use crate::graphics::{Point, Rectangle}; +use crate::ui::core::{ + Event, Hasher, Layout, MouseCursor, Node, Style, Widget, +}; + +pub struct Panel<'a, Message, Renderer> { + style: Style, + content: Box + 'a>, +} + +impl<'a, Message, Renderer> Panel<'a, Message, Renderer> { + pub fn new(content: impl Widget + 'a) -> Self { + Panel { + style: Style::default().padding(20), + content: Box::new(content), + } + } + + pub fn width(mut self, width: u32) -> Self { + self.style = self.style.width(width); + self + } + + pub fn max_width(mut self, max_width: u32) -> Self { + self.style = self.style.max_width(max_width); + self + } +} + +impl<'a, Message, Renderer> Widget + for Panel<'a, Message, Renderer> +where + Renderer: self::Renderer, +{ + fn node(&self, renderer: &Renderer) -> Node { + Node::with_children(self.style, vec![self.content.node(renderer)]) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout, + cursor_position: Point, + messages: &mut Vec, + ) { + [&mut self.content] + .iter_mut() + .zip(layout.children()) + .for_each(|(child, layout)| { + child.on_event(event, layout, cursor_position, messages) + }); + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout, + cursor_position: Point, + ) -> MouseCursor { + let bounds = layout.bounds(); + let mut cursor = MouseCursor::OutOfBounds; + renderer.draw(bounds); + + [&self.content].iter().zip(layout.children()).for_each( + |(child, layout)| { + let new_cursor = child.draw(renderer, layout, cursor_position); + + if new_cursor != MouseCursor::OutOfBounds { + cursor = new_cursor; + } + }, + ); + + if cursor == MouseCursor::OutOfBounds { + if bounds.contains(cursor_position) { + MouseCursor::Idle + } else { + MouseCursor::OutOfBounds + } + } else { + cursor + } + } + + fn hash(&self, state: &mut Hasher) { + self.style.hash(state); + } +} + +pub trait Renderer { + fn draw(&mut self, bounds: Rectangle); +} diff --git a/src/widget/progress_bar.rs b/src/widget/progress_bar.rs new file mode 100644 index 00000000..d4499160 --- /dev/null +++ b/src/widget/progress_bar.rs @@ -0,0 +1,106 @@ +//! Provide visual feedback to your users when performing a slow task. + +use crate::{ + Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, +}; + +use std::hash::Hash; + +/// A bar that is filled based on an amount of progress. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`progress_bar::Renderer`] trait. +/// +/// [`Widget`]: ../trait.Widget.html +/// [`progress_bar::Renderer`]: trait.Renderer.html +/// +/// # Example +/// +/// ``` +/// use iced::ProgressBar; +/// +/// let progress = 0.75; +/// +/// ProgressBar::new(progress); +/// ``` +#[derive(Debug)] +pub struct ProgressBar { + progress: f32, + style: Style, +} + +impl ProgressBar { + /// Creates a new [`ProgressBar`] filled based on the given amount of + /// progress. + /// + /// The progress should be in the `0.0..=1.0` range. `0` meaning no work + /// done, and `1` meaning work finished. + /// + /// [`ProgressBar`]: struct.ProgressBar.html + pub fn new(progress: f32) -> Self { + ProgressBar { + progress, + style: Style::default().fill_width(), + } + } + + /// Sets the width of the [`ProgressBar`] in pixels. + /// + /// [`ProgressBar`]: struct.ProgressBar.html + pub fn width(mut self, width: u16) -> Self { + self.style = self.style.width(width); + self + } +} + +impl Widget for ProgressBar +where + Renderer: self::Renderer, +{ + fn node(&self, _renderer: &Renderer) -> Node { + Node::new(self.style.height(50)) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + renderer.draw(layout.bounds(), self.progress); + + MouseCursor::OutOfBounds + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + } +} + +/// The renderer of a [`ProgressBar`]. +/// +/// Your [renderer] will need to implement this trait before being able to use +/// a [`ProgressBar`] in your user interface. +/// +/// [`ProgressBar`]: struct.ProgressBar.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Draws a [`ProgressBar`]. + /// + /// It receives: + /// * the bounds of the [`ProgressBar`] + /// * the current progress of the [`ProgressBar`], in the `0.0..=1.0` + /// range. + /// + /// [`ProgressBar`]: struct.ProgressBar.html + fn draw(&mut self, bounds: Rectangle, progress: f32); +} + +impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, +{ + fn from(progress_bar: ProgressBar) -> Element<'a, Message, Renderer> { + Element::new(progress_bar) + } +} diff --git a/src/widget/radio.rs b/src/widget/radio.rs new file mode 100644 index 00000000..28353ef4 --- /dev/null +++ b/src/widget/radio.rs @@ -0,0 +1,211 @@ +//! Create choices using radio buttons. +use crate::input::{mouse, ButtonState}; +use crate::widget::{text, Column, Row, Text}; +use crate::{ + Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, + Widget, +}; + +use std::hash::Hash; + +/// A circular button representing a choice, with a generic text `Color`. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`radio::Renderer`] trait. +/// +/// [`Widget`]: ../trait.Widget.html +/// [`radio::Renderer`]: trait.Renderer.html +/// +/// # Example +/// ``` +/// use iced::{Column, Radio}; +/// +/// #[derive(Debug, Clone, Copy)] +/// pub enum Color { +/// Black, +/// } +/// +/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] +/// pub enum Choice { +/// A, +/// B, +/// } +/// +/// #[derive(Debug, Clone, Copy)] +/// pub enum Message { +/// RadioSelected(Choice), +/// } +/// +/// let selected_choice = Some(Choice::A); +/// +/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected) +/// .label_color(Color::Black); +/// +/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected) +/// .label_color(Color::Black); +/// ``` +/// +/// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true) +pub struct Radio { + is_selected: bool, + on_click: Message, + label: String, + label_color: Option, +} + +impl std::fmt::Debug for Radio +where + Color: std::fmt::Debug, + Message: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Radio") + .field("is_selected", &self.is_selected) + .field("on_click", &self.on_click) + .field("label", &self.label) + .field("label_color", &self.label_color) + .finish() + } +} + +impl Radio { + /// Creates a new [`Radio`] button. + /// + /// It expects: + /// * the value related to the [`Radio`] button + /// * the label of the [`Radio`] button + /// * the current selected value + /// * a function that will be called when the [`Radio`] is selected. It + /// receives the value of the radio and must produce a `Message`. + /// + /// [`Radio`]: struct.Radio.html + pub fn new(value: V, label: &str, selected: Option, f: F) -> Self + where + V: Eq + Copy, + F: 'static + Fn(V) -> Message, + { + Radio { + is_selected: Some(value) == selected, + on_click: f(value), + label: String::from(label), + label_color: None, + } + } + + /// Sets the `Color` of the label of the [`Radio`]. + /// + /// [`Radio`]: struct.Radio.html + pub fn label_color(mut self, color: Color) -> Self { + self.label_color = Some(color); + self + } +} + +impl Widget + for Radio +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer + text::Renderer, + Message: Copy + std::fmt::Debug, +{ + fn node(&self, renderer: &Renderer) -> Node { + Row::<(), Renderer>::new() + .spacing(15) + .align_items(Align::Center) + .push(Column::new().width(28).height(28)) + .push(Text::new(&self.label)) + .node(renderer) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + match event { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state: ButtonState::Pressed, + }) => { + if layout.bounds().contains(cursor_position) { + messages.push(self.on_click); + } + } + _ => {} + } + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + let children: Vec<_> = layout.children().collect(); + + let mut text_bounds = children[1].bounds(); + text_bounds.y -= 2.0; + + text::Renderer::draw( + renderer, + text_bounds, + &self.label, + None, + self.label_color, + text::HorizontalAlignment::Left, + text::VerticalAlignment::Top, + ); + + self::Renderer::draw( + renderer, + cursor_position, + children[0].bounds(), + layout.bounds(), + self.is_selected, + ) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.label.hash(state); + } +} + +/// The renderer of a [`Radio`] button. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`Radio`] button in your user interface. +/// +/// [`Radio`]: struct.Radio.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Draws a [`Radio`] button. + /// + /// It receives: + /// * the current cursor position + /// * the bounds of the [`Radio`] + /// * the bounds of the label of the [`Radio`] + /// * whether the [`Radio`] is selected or not + /// + /// [`Radio`]: struct.Radio.html + fn draw( + &mut self, + cursor_position: Point, + bounds: Rectangle, + label_bounds: Rectangle, + is_selected: bool, + ) -> MouseCursor; +} + +impl<'a, Color, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer + text::Renderer, + Message: 'static + Copy + std::fmt::Debug, +{ + fn from(checkbox: Radio) -> Element<'a, Message, Renderer> { + Element::new(checkbox) + } +} diff --git a/src/widget/row.rs b/src/widget/row.rs new file mode 100644 index 00000000..959528dc --- /dev/null +++ b/src/widget/row.rs @@ -0,0 +1,219 @@ +use std::hash::Hash; + +use crate::{ + Align, Element, Event, Hasher, Justify, Layout, MouseCursor, Node, Point, + Style, Widget, +}; + +/// A container that distributes its contents horizontally. +/// +/// A [`Row`] will try to fill the horizontal space of its container. +/// +/// [`Row`]: struct.Row.html +#[derive(Default)] +pub struct Row<'a, Message, Renderer> { + style: Style, + spacing: u16, + children: Vec>, +} + +impl<'a, Message, Renderer> std::fmt::Debug for Row<'a, Message, Renderer> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Row") + .field("style", &self.style) + .field("spacing", &self.spacing) + .field("children", &self.children) + .finish() + } +} + +impl<'a, Message, Renderer> Row<'a, Message, Renderer> { + /// Creates an empty [`Row`]. + /// + /// [`Row`]: struct.Row.html + pub fn new() -> Self { + Row { + style: Style::default().fill_width(), + spacing: 0, + children: Vec::new(), + } + } + + /// Sets the horizontal spacing _between_ elements in pixels. + /// + /// Custom margins per element do not exist in Iced. You should use this + /// method instead! While less flexible, it helps you keep spacing between + /// elements consistent. + pub fn spacing(mut self, px: u16) -> Self { + self.spacing = px; + self + } + + /// Sets the padding of the [`Row`] in pixels. + /// + /// [`Row`]: struct.Row.html + pub fn padding(mut self, px: u16) -> Self { + self.style = self.style.padding(px); + self + } + + /// Sets the width of the [`Row`] in pixels. + /// + /// [`Row`]: struct.Row.html + pub fn width(mut self, width: u16) -> Self { + self.style = self.style.width(width); + self + } + + /// Sets the height of the [`Row`] in pixels. + /// + /// [`Row`]: struct.Row.html + pub fn height(mut self, height: u16) -> Self { + self.style = self.style.height(height); + self + } + + /// Sets the maximum width of the [`Row`] in pixels. + /// + /// [`Row`]: struct.Row.html + pub fn max_width(mut self, max_width: u16) -> Self { + self.style = self.style.max_width(max_width); + self + } + + /// Sets the maximum height of the [`Row`] in pixels. + /// + /// [`Row`]: struct.Row.html + pub fn max_height(mut self, max_height: u16) -> Self { + self.style = self.style.max_height(max_height); + self + } + + /// Sets the alignment of the [`Row`] itself. + /// + /// This is useful if you want to override the default alignment given by + /// the parent container. + /// + /// [`Row`]: struct.Row.html + pub fn align_self(mut self, align: Align) -> Self { + self.style = self.style.align_self(align); + self + } + + /// Sets the vertical alignment of the contents of the [`Row`] . + /// + /// [`Row`]: struct.Row.html + pub fn align_items(mut self, align: Align) -> Self { + self.style = self.style.align_items(align); + self + } + + /// Sets the horizontal distribution strategy for the contents of the + /// [`Row`] . + /// + /// [`Row`]: struct.Row.html + pub fn justify_content(mut self, justify: Justify) -> Self { + self.style = self.style.justify_content(justify); + self + } + + /// Adds an [`Element`] to the [`Row`]. + /// + /// [`Element`]: ../struct.Element.html + /// [`Row`]: struct.Row.html + pub fn push(mut self, child: E) -> Row<'a, Message, Renderer> + where + E: Into>, + { + self.children.push(child.into()); + self + } +} + +impl<'a, Message, Renderer> Widget + for Row<'a, Message, Renderer> +{ + fn node(&self, renderer: &Renderer) -> Node { + let mut children: Vec = self + .children + .iter() + .map(|child| { + let mut node = child.widget.node(renderer); + + let mut style = node.0.style(); + style.margin.end = + stretch::style::Dimension::Points(f32::from(self.spacing)); + + node.0.set_style(style); + node + }) + .collect(); + + if let Some(node) = children.last_mut() { + let mut style = node.0.style(); + style.margin.end = stretch::style::Dimension::Undefined; + + node.0.set_style(style); + } + + Node::with_children(self.style, children) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + self.children.iter_mut().zip(layout.children()).for_each( + |(child, layout)| { + child + .widget + .on_event(event, layout, cursor_position, messages) + }, + ); + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + let mut cursor = MouseCursor::OutOfBounds; + + self.children.iter().zip(layout.children()).for_each( + |(child, layout)| { + let new_cursor = + child.widget.draw(renderer, layout, cursor_position); + + if new_cursor != MouseCursor::OutOfBounds { + cursor = new_cursor; + } + }, + ); + + cursor + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + self.spacing.hash(state); + + for child in &self.children { + child.widget.hash_layout(state); + } + } +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: 'a, + Message: 'static, +{ + fn from(row: Row<'a, Message, Renderer>) -> Element<'a, Message, Renderer> { + Element::new(row) + } +} diff --git a/src/widget/slider.rs b/src/widget/slider.rs new file mode 100644 index 00000000..cdec9ec4 --- /dev/null +++ b/src/widget/slider.rs @@ -0,0 +1,241 @@ +//! Display an interactive selector of a single value from a range of values. +//! +//! A [`Slider`] has some local [`State`]. +//! +//! [`Slider`]: struct.Slider.html +//! [`State`]: struct.State.html +use std::hash::Hash; +use std::ops::RangeInclusive; + +use crate::input::{mouse, ButtonState}; +use crate::{ + Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, + Widget, +}; + +/// An horizontal bar and a handle that selects a single value from a range of +/// values. +/// +/// A [`Slider`] will try to fill the horizontal space of its container. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`slider::Renderer`] trait. +/// +/// [`Slider`]: struct.Slider.html +/// [`Widget`]: ../trait.Widget.html +/// [`slider::Renderer`]: trait.Renderer.html +/// +/// # Example +/// ``` +/// use iced::{slider, Slider}; +/// +/// pub enum Message { +/// SliderChanged(f32), +/// } +/// +/// let state = &mut slider::State::new(); +/// let value = 50.0; +/// +/// Slider::new(state, 0.0..=100.0, value, Message::SliderChanged); +/// ``` +/// +/// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true) +pub struct Slider<'a, Message> { + state: &'a mut State, + range: RangeInclusive, + value: f32, + on_change: Box Message>, + style: Style, +} + +impl<'a, Message> std::fmt::Debug for Slider<'a, Message> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Slider") + .field("state", &self.state) + .field("range", &self.range) + .field("value", &self.value) + .field("style", &self.style) + .finish() + } +} + +impl<'a, Message> Slider<'a, Message> { + /// Creates a new [`Slider`]. + /// + /// It expects: + /// * the local [`State`] of the [`Slider`] + /// * an inclusive range of possible values + /// * the current value of the [`Slider`] + /// * a function that will be called when the [`Slider`] is dragged. + /// It receives the new value of the [`Slider`] and must produce a + /// `Message`. + /// + /// [`Slider`]: struct.Slider.html + /// [`State`]: struct.State.html + pub fn new( + state: &'a mut State, + range: RangeInclusive, + value: f32, + on_change: F, + ) -> Self + where + F: 'static + Fn(f32) -> Message, + { + Slider { + state, + value: value.max(*range.start()).min(*range.end()), + range, + on_change: Box::new(on_change), + style: Style::default().min_width(100).fill_width(), + } + } + + /// Sets the width of the [`Slider`] in pixels. + /// + /// [`Slider`]: struct.Slider.html + pub fn width(mut self, width: u16) -> Self { + self.style = self.style.width(width); + self + } +} + +impl<'a, Message, Renderer> Widget for Slider<'a, Message> +where + Renderer: self::Renderer, +{ + fn node(&self, _renderer: &Renderer) -> Node { + Node::new(self.style.height(25)) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + let mut change = || { + let bounds = layout.bounds(); + + if cursor_position.x <= bounds.x { + messages.push((self.on_change)(*self.range.start())); + } else if cursor_position.x >= bounds.x + bounds.width { + messages.push((self.on_change)(*self.range.end())); + } else { + let percent = (cursor_position.x - bounds.x) / bounds.width; + let value = (self.range.end() - self.range.start()) * percent + + self.range.start(); + + messages.push((self.on_change)(value)); + } + }; + + match event { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state, + }) => match state { + ButtonState::Pressed => { + if layout.bounds().contains(cursor_position) { + change(); + self.state.is_dragging = true; + } + } + ButtonState::Released => { + self.state.is_dragging = false; + } + }, + Event::Mouse(mouse::Event::CursorMoved { .. }) => { + if self.state.is_dragging { + change(); + } + } + _ => {} + } + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + renderer.draw( + cursor_position, + layout.bounds(), + self.state, + self.range.clone(), + self.value, + ) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + } +} + +/// The local state of a [`Slider`]. +/// +/// [`Slider`]: struct.Slider.html +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct State { + is_dragging: bool, +} + +impl State { + /// Creates a new [`State`]. + /// + /// [`State`]: struct.State.html + pub fn new() -> State { + State::default() + } + + /// Returns whether the associated [`Slider`] is currently being dragged or + /// not. + /// + /// [`Slider`]: struct.Slider.html + pub fn is_dragging(&self) -> bool { + self.is_dragging + } +} + +/// The renderer of a [`Slider`]. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`Slider`] in your user interface. +/// +/// [`Slider`]: struct.Slider.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Draws a [`Slider`]. + /// + /// It receives: + /// * the current cursor position + /// * the bounds of the [`Slider`] + /// * the local state of the [`Slider`] + /// * the range of values of the [`Slider`] + /// * the current value of the [`Slider`] + /// + /// [`Slider`]: struct.Slider.html + /// [`State`]: struct.State.html + /// [`Class`]: enum.Class.html + fn draw( + &mut self, + cursor_position: Point, + bounds: Rectangle, + state: &State, + range: RangeInclusive, + value: f32, + ) -> MouseCursor; +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + Message: 'static, +{ + fn from(slider: Slider<'a, Message>) -> Element<'a, Message, Renderer> { + Element::new(slider) + } +} diff --git a/src/widget/text.rs b/src/widget/text.rs new file mode 100644 index 00000000..59b599bb --- /dev/null +++ b/src/widget/text.rs @@ -0,0 +1,224 @@ +//! Write some text for your users to read. +use crate::{ + Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, +}; + +use std::hash::Hash; + +/// A fragment of text with a generic `Color`. +/// +/// It implements [`Widget`] when the associated `Renderer` implements the +/// [`text::Renderer`] trait. +/// +/// [`Widget`]: ../trait.Widget.html +/// [`text::Renderer`]: trait.Renderer.html +/// +/// # Example +/// +/// ``` +/// use iced::Text; +/// +/// #[derive(Debug, Clone, Copy)] +/// pub enum Color { +/// Black, +/// } +/// +/// Text::new("I <3 iced!") +/// .size(40) +/// .color(Color::Black); +/// ``` +#[derive(Debug, Clone)] +pub struct Text { + content: String, + size: Option, + color: Option, + style: Style, + horizontal_alignment: HorizontalAlignment, + vertical_alignment: VerticalAlignment, +} + +impl Text { + /// Create a new fragment of [`Text`] with the given contents. + /// + /// [`Text`]: struct.Text.html + pub fn new(label: &str) -> Self { + Text { + content: String::from(label), + size: None, + color: None, + style: Style::default().fill_width(), + horizontal_alignment: HorizontalAlignment::Left, + vertical_alignment: VerticalAlignment::Top, + } + } + + /// Sets the size of the [`Text`] in pixels. + /// + /// [`Text`]: struct.Text.html + pub fn size(mut self, size: u16) -> Self { + self.size = Some(size); + self + } + + /// Sets the `Color` of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + pub fn color(mut self, color: Color) -> Self { + self.color = Some(color); + self + } + + /// Sets the width of the [`Text`] boundaries in pixels. + /// + /// [`Text`]: struct.Text.html + pub fn width(mut self, width: u16) -> Self { + self.style = self.style.width(width); + self + } + + /// Sets the height of the [`Text`] boundaries in pixels. + /// + /// [`Text`]: struct.Text.html + pub fn height(mut self, height: u16) -> Self { + self.style = self.style.height(height); + self + } + + /// Sets the [`HorizontalAlignment`] of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html + pub fn horizontal_alignment( + mut self, + alignment: HorizontalAlignment, + ) -> Self { + self.horizontal_alignment = alignment; + self + } + + /// Sets the [`VerticalAlignment`] of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + /// [`VerticalAlignment`]: enum.VerticalAlignment.html + pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self { + self.vertical_alignment = alignment; + self + } +} + +impl Widget for Text +where + Color: Copy + std::fmt::Debug, + Renderer: self::Renderer, +{ + fn node(&self, renderer: &Renderer) -> Node { + renderer.node(self.style, &self.content, self.size) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + renderer.draw( + layout.bounds(), + &self.content, + self.size, + self.color, + self.horizontal_alignment, + self.vertical_alignment, + ); + + MouseCursor::OutOfBounds + } + + fn hash_layout(&self, state: &mut Hasher) { + self.style.hash(state); + + self.content.hash(state); + self.size.hash(state); + } +} + +/// The renderer of a [`Text`] fragment with a generic `Color`. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use [`Text`] in your [`UserInterface`]. +/// +/// [`Text`]: struct.Text.html +/// [renderer]: ../../renderer/index.html +/// [`UserInterface`]: ../../struct.UserInterface.html +pub trait Renderer { + /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] + /// contents and size. + /// + /// You should probably use [`Node::with_measure`] to allow [`Text`] to + /// adapt to the dimensions of its container. + /// + /// [`Node`]: ../../struct.Node.html + /// [`Style`]: ../../struct.Style.html + /// [`Text`]: struct.Text.html + /// [`Node::with_measure`]: ../../struct.Node.html#method.with_measure + fn node(&self, style: Style, content: &str, size: Option) -> Node; + + /// Draws a [`Text`] fragment. + /// + /// It receives: + /// * the bounds of the [`Text`] + /// * the contents of the [`Text`] + /// * the size of the [`Text`] + /// * the color of the [`Text`] + /// * the [`HorizontalAlignment`] of the [`Text`] + /// * the [`VerticalAlignment`] of the [`Text`] + /// + /// [`Text`]: struct.Text.html + /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html + /// [`VerticalAlignment`]: enum.VerticalAlignment.html + fn draw( + &mut self, + bounds: Rectangle, + content: &str, + size: Option, + color: Option, + horizontal_alignment: HorizontalAlignment, + vertical_alignment: VerticalAlignment, + ); +} + +impl<'a, Message, Renderer, Color> From> + for Element<'a, Message, Renderer> +where + Color: 'static + Copy + std::fmt::Debug, + Renderer: self::Renderer, +{ + fn from(text: Text) -> Element<'a, Message, Renderer> { + Element::new(text) + } +} + +/// The horizontal alignment of some resource. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum HorizontalAlignment { + /// Align left + Left, + + /// Horizontally centered + Center, + + /// Align right + Right, +} + +/// The vertical alignment of some resource. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum VerticalAlignment { + /// Align top + Top, + + /// Vertically centered + Center, + + /// Align bottom + Bottom, +} diff --git a/web/Cargo.toml b/web/Cargo.toml new file mode 100644 index 00000000..acc5f18b --- /dev/null +++ b/web/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "iced_web" +version = "0.1.0-alpha" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +description = "A web backend for Iced" +license = "MIT" +repository = "https://github.com/hecrj/iced" +documentation = "https://docs.rs/iced_web" +readme = "README.md" +keywords = ["gui", "ui", "web", "interface", "widgets"] +categories = ["web-programming"] + +[badges] +maintenance = { status = "actively-developed" } + +[dependencies] +iced = { version = "0.1.0-alpha", path = ".." } +dodrio = "0.1.0" + +[dependencies.web-sys] +version = "0.3.27" +features = [ + "console", + "Document", + "HtmlElement", +] diff --git a/web/examples/tour/Cargo.toml b/web/examples/tour/Cargo.toml new file mode 100644 index 00000000..15f38fa7 --- /dev/null +++ b/web/examples/tour/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "iced_web_tour" +version = "0.0.0" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +publish = false + +[lib] +crate-type = ["cdylib"] + +[dependencies] +iced_web = { path = "../.." } +wasm-bindgen = "0.2.50" +log = "0.4" +console_error_panic_hook = "0.1.6" +console_log = "0.1.2" diff --git a/web/examples/tour/index.html b/web/examples/tour/index.html new file mode 100644 index 00000000..a639a6cb --- /dev/null +++ b/web/examples/tour/index.html @@ -0,0 +1,13 @@ + + + + + Tour - Iced Web + + + + + diff --git a/web/examples/tour/src/lib.rs b/web/examples/tour/src/lib.rs new file mode 100644 index 00000000..e747a193 --- /dev/null +++ b/web/examples/tour/src/lib.rs @@ -0,0 +1,8 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen(start)] +pub fn run() { + console_error_panic_hook::set_once(); + console_log::init_with_level(log::Level::Trace) + .expect("Initialize logging"); +} diff --git a/web/examples/tour/src/tour.rs b/web/examples/tour/src/tour.rs new file mode 100644 index 00000000..d0be99b0 --- /dev/null +++ b/web/examples/tour/src/tour.rs @@ -0,0 +1,578 @@ +use super::widget::{ + button, slider, Button, Checkbox, Column, Element, Image, Radio, Row, + Slider, Text, +}; + +use ggez::graphics::{self, Color, FilterMode, BLACK}; +use ggez::Context; +use iced::{text::HorizontalAlignment, Align}; + +pub struct Tour { + steps: Steps, + back_button: button::State, + next_button: button::State, + debug: bool, +} + +impl Tour { + pub fn new(context: &mut Context) -> Tour { + Tour { + steps: Steps::new(context), + back_button: button::State::new(), + next_button: button::State::new(), + debug: false, + } + } + + pub fn update(&mut self, event: Message) { + match event { + Message::BackPressed => { + self.steps.go_back(); + } + Message::NextPressed => { + self.steps.advance(); + } + Message::StepMessage(step_msg) => { + self.steps.update(step_msg, &mut self.debug); + } + } + } + + pub fn view(&mut self) -> Element { + let Tour { + steps, + back_button, + next_button, + .. + } = self; + + let mut controls = Row::new(); + + if steps.has_previous() { + controls = controls.push( + Button::new(back_button, "Back") + .on_press(Message::BackPressed) + .class(button::Class::Secondary), + ); + } + + controls = controls.push(Column::new()); + + if steps.can_continue() { + controls = controls.push( + Button::new(next_button, "Next").on_press(Message::NextPressed), + ); + } + + let element: Element<_> = Column::new() + .max_width(500) + .spacing(20) + .push(steps.view(self.debug).map(Message::StepMessage)) + .push(controls) + .into(); + + if self.debug { + element.explain(BLACK) + } else { + element + } + } +} + +#[derive(Debug, Clone, Copy)] +pub enum Message { + BackPressed, + NextPressed, + StepMessage(StepMessage), +} + +struct Steps { + steps: Vec, + current: usize, +} + +impl Steps { + fn new(context: &mut Context) -> Steps { + Steps { + steps: vec![ + Step::Welcome, + Step::Slider { + state: slider::State::new(), + value: 50, + }, + Step::RowsAndColumns { + layout: Layout::Row, + spacing_slider: slider::State::new(), + spacing: 20, + }, + Step::Text { + size_slider: slider::State::new(), + size: 30, + color_sliders: [slider::State::new(); 3], + color: BLACK, + }, + Step::Radio { selection: None }, + Step::Image { + ferris: { + let mut image = + graphics::Image::new(context, "/ferris.png") + .expect("Load ferris image"); + + image.set_filter(FilterMode::Linear); + + image + }, + width: 300, + slider: slider::State::new(), + }, + Step::Debugger, + Step::End, + ], + current: 0, + } + } + + fn update(&mut self, msg: StepMessage, debug: &mut bool) { + self.steps[self.current].update(msg, debug); + } + + fn view(&mut self, debug: bool) -> Element { + self.steps[self.current].view(debug) + } + + fn advance(&mut self) { + if self.can_continue() { + self.current += 1; + } + } + + fn go_back(&mut self) { + if self.has_previous() { + self.current -= 1; + } + } + + fn has_previous(&self) -> bool { + self.current > 0 + } + + fn can_continue(&self) -> bool { + self.current + 1 < self.steps.len() + && self.steps[self.current].can_continue() + } +} + +enum Step { + Welcome, + Slider { + state: slider::State, + value: u16, + }, + RowsAndColumns { + layout: Layout, + spacing_slider: slider::State, + spacing: u16, + }, + Text { + size_slider: slider::State, + size: u16, + color_sliders: [slider::State; 3], + color: Color, + }, + Radio { + selection: Option, + }, + Image { + ferris: graphics::Image, + width: u16, + slider: slider::State, + }, + Debugger, + End, +} + +#[derive(Debug, Clone, Copy)] +pub enum StepMessage { + SliderChanged(f32), + LayoutChanged(Layout), + SpacingChanged(f32), + TextSizeChanged(f32), + TextColorChanged(Color), + LanguageSelected(Language), + ImageWidthChanged(f32), + DebugToggled(bool), +} + +impl<'a> Step { + fn update(&mut self, msg: StepMessage, debug: &mut bool) { + match msg { + StepMessage::DebugToggled(value) => { + if let Step::Debugger = self { + *debug = value; + } + } + StepMessage::LanguageSelected(language) => { + if let Step::Radio { selection } = self { + *selection = Some(language); + } + } + StepMessage::SliderChanged(new_value) => { + if let Step::Slider { value, .. } = self { + *value = new_value.round() as u16; + } + } + StepMessage::TextSizeChanged(new_size) => { + if let Step::Text { size, .. } = self { + *size = new_size.round() as u16; + } + } + StepMessage::TextColorChanged(new_color) => { + if let Step::Text { color, .. } = self { + *color = new_color; + } + } + StepMessage::LayoutChanged(new_layout) => { + if let Step::RowsAndColumns { layout, .. } = self { + *layout = new_layout; + } + } + StepMessage::SpacingChanged(new_spacing) => { + if let Step::RowsAndColumns { spacing, .. } = self { + *spacing = new_spacing.round() as u16; + } + } + StepMessage::ImageWidthChanged(new_width) => { + if let Step::Image { width, .. } = self { + *width = new_width.round() as u16; + } + } + }; + } + + fn can_continue(&self) -> bool { + match self { + Step::Welcome => true, + Step::Radio { selection } => *selection == Some(Language::Rust), + Step::Slider { .. } => true, + Step::Text { .. } => true, + Step::Image { .. } => true, + Step::RowsAndColumns { .. } => true, + Step::Debugger => true, + Step::End => false, + } + } + + fn view(&mut self, debug: bool) -> Element { + match self { + Step::Welcome => Self::welcome().into(), + Step::Radio { selection } => Self::radio(*selection).into(), + Step::Slider { state, value } => Self::slider(state, *value).into(), + Step::Text { + size_slider, + size, + color_sliders, + color, + } => Self::text(size_slider, *size, color_sliders, *color).into(), + Step::Image { + ferris, + width, + slider, + } => Self::image(ferris.clone(), *width, slider).into(), + Step::RowsAndColumns { + layout, + spacing_slider, + spacing, + } => { + Self::rows_and_columns(*layout, spacing_slider, *spacing).into() + } + Step::Debugger => Self::debugger(debug).into(), + Step::End => Self::end().into(), + } + } + + fn container(title: &str) -> Column<'a, StepMessage> { + Column::new() + .spacing(20) + .align_items(Align::Stretch) + .push(Text::new(title).size(50)) + } + + fn welcome() -> Column<'a, StepMessage> { + Self::container("Welcome!") + .push(Text::new( + "This a simple tour meant to showcase a bunch of widgets that \ + can be easily implemented on top of Iced.", + )) + .push(Text::new( + "Iced is a renderer-agnostic GUI library for Rust focused on \ + simplicity and type-safety. It is heavily inspired by Elm.", + )) + .push(Text::new( + "It was originally born as part of Coffee, an opinionated \ + 2D game engine for Rust.", + )) + .push(Text::new( + "Iced does not provide a built-in renderer. This example runs \ + on a fairly simple renderer built on top of ggez, another \ + game library.", + )) + .push(Text::new( + "You will need to interact with the UI in order to reach the \ + end!", + )) + } + + fn slider( + state: &'a mut slider::State, + value: u16, + ) -> Column<'a, StepMessage> { + Self::container("Slider") + .push(Text::new( + "A slider allows you to smoothly select a value from a range \ + of values.", + )) + .push(Text::new( + "The following slider lets you choose an integer from \ + 0 to 100:", + )) + .push(Slider::new( + state, + 0.0..=100.0, + value as f32, + StepMessage::SliderChanged, + )) + .push( + Text::new(&value.to_string()) + .horizontal_alignment(HorizontalAlignment::Center), + ) + } + + fn rows_and_columns( + layout: Layout, + spacing_slider: &'a mut slider::State, + spacing: u16, + ) -> Column<'a, StepMessage> { + let row_radio = Radio::new( + Layout::Row, + "Row", + Some(layout), + StepMessage::LayoutChanged, + ); + + let column_radio = Radio::new( + Layout::Column, + "Column", + Some(layout), + StepMessage::LayoutChanged, + ); + + let layout_section: Element<_> = match layout { + Layout::Row => Row::new() + .spacing(spacing) + .push(row_radio) + .push(column_radio) + .into(), + Layout::Column => Column::new() + .spacing(spacing) + .push(row_radio) + .push(column_radio) + .into(), + }; + + let spacing_section = Column::new() + .spacing(10) + .push(Slider::new( + spacing_slider, + 0.0..=80.0, + spacing as f32, + StepMessage::SpacingChanged, + )) + .push( + Text::new(&format!("{} px", spacing)) + .horizontal_alignment(HorizontalAlignment::Center), + ); + + Self::container("Rows and columns") + .spacing(spacing) + .push(Text::new( + "Iced uses a layout model based on flexbox to position UI \ + elements.", + )) + .push(Text::new( + "Rows and columns can be used to distribute content \ + horizontally or vertically, respectively.", + )) + .push(layout_section) + .push(Text::new( + "You can also easily change the spacing between elements:", + )) + .push(spacing_section) + } + + fn text( + size_slider: &'a mut slider::State, + size: u16, + color_sliders: &'a mut [slider::State; 3], + color: Color, + ) -> Column<'a, StepMessage> { + let size_section = Column::new() + .padding(20) + .spacing(20) + .push(Text::new("You can change its size:")) + .push( + Text::new(&format!("This text is {} pixels", size)).size(size), + ) + .push(Slider::new( + size_slider, + 10.0..=70.0, + size as f32, + StepMessage::TextSizeChanged, + )); + + let [red, green, blue] = color_sliders; + let color_section = Column::new() + .padding(20) + .spacing(20) + .push(Text::new("And its color:")) + .push(Text::new(&format!("{:?}", color)).color(color)) + .push( + Row::new() + .spacing(10) + .push(Slider::new(red, 0.0..=1.0, color.r, move |r| { + StepMessage::TextColorChanged(Color { r, ..color }) + })) + .push(Slider::new(green, 0.0..=1.0, color.g, move |g| { + StepMessage::TextColorChanged(Color { g, ..color }) + })) + .push(Slider::new(blue, 0.0..=1.0, color.b, move |b| { + StepMessage::TextColorChanged(Color { b, ..color }) + })), + ); + + Self::container("Text") + .push(Text::new( + "Text is probably the most essential widget for your UI. \ + It will try to adapt to the dimensions of its container.", + )) + .push(size_section) + .push(color_section) + } + + fn radio(selection: Option) -> Column<'a, StepMessage> { + let question = Column::new() + .padding(20) + .spacing(10) + .push(Text::new("Iced is written in...").size(24)) + .push(Language::all().iter().cloned().fold( + Column::new().padding(10).spacing(20), + |choices, language| { + choices.push(Radio::new( + language, + language.into(), + selection, + StepMessage::LanguageSelected, + )) + }, + )); + + Self::container("Radio button") + .push(Text::new( + "A radio button is normally used to represent a choice... \ + Surprise test!", + )) + .push(question) + .push(Text::new( + "Iced works very well with iterators! The list above is \ + basically created by folding a column over the different \ + choices, creating a radio button for each one of them!", + )) + } + + fn image( + ferris: graphics::Image, + width: u16, + slider: &'a mut slider::State, + ) -> Column<'a, StepMessage> { + Self::container("Image") + .push(Text::new("An image that tries to keep its aspect ratio.")) + .push(Image::new(ferris).width(width).align_self(Align::Center)) + .push(Slider::new( + slider, + 100.0..=500.0, + width as f32, + StepMessage::ImageWidthChanged, + )) + .push( + Text::new(&format!("Width: {} px", width.to_string())) + .horizontal_alignment(HorizontalAlignment::Center), + ) + } + + fn debugger(debug: bool) -> Column<'a, StepMessage> { + Self::container("Debugger") + .push(Text::new( + "You can ask Iced to visually explain the layouting of the \ + different elements comprising your UI!", + )) + .push(Text::new( + "Give it a shot! Check the following checkbox to be able to \ + see element boundaries.", + )) + .push(Checkbox::new( + debug, + "Explain layout", + StepMessage::DebugToggled, + )) + .push(Text::new("Feel free to go back and take a look.")) + } + + fn end() -> Column<'a, StepMessage> { + Self::container("You reached the end!") + .push(Text::new( + "This tour will be updated as more features are added.", + )) + .push(Text::new("Make sure to keep an eye on it!")) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Language { + Rust, + Elm, + Ruby, + Haskell, + C, + Other, +} + +impl Language { + fn all() -> [Language; 6] { + [ + Language::C, + Language::Elm, + Language::Ruby, + Language::Haskell, + Language::Rust, + Language::Other, + ] + } +} + +impl From for &str { + fn from(language: Language) -> &'static str { + match language { + Language::Rust => "Rust", + Language::Elm => "Elm", + Language::Ruby => "Ruby", + Language::Haskell => "Haskell", + Language::C => "C", + Language::Other => "Other", + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Layout { + Row, + Column, +} diff --git a/web/src/lib.rs b/web/src/lib.rs new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/web/src/lib.rs @@ -0,0 +1 @@ + -- cgit From 27ac85a9d98474904c422a891e54888376dec00a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Sep 2019 20:54:50 +0200 Subject: Draft web runtime and widgets --- web/Cargo.toml | 1 + web/examples/tour/Cargo.toml | 1 + web/examples/tour/src/lib.rs | 27 ++++++++++++++++++++++++ web/examples/tour/src/tour.rs | 43 +++++++++++++------------------------- web/src/color.rs | 16 +++++++++++++++ web/src/element.rs | 47 ++++++++++++++++++++++++++++++++++++++++++ web/src/lib.rs | 27 ++++++++++++++++++++++++ web/src/widget.rs | 20 ++++++++++++++++++ web/src/widget/button.rs | 16 +++++++++++++++ web/src/widget/checkbox.rs | 14 +++++++++++++ web/src/widget/column.rs | 48 +++++++++++++++++++++++++++++++++++++++++++ web/src/widget/image.rs | 11 ++++++++++ web/src/widget/radio.rs | 14 +++++++++++++ web/src/widget/row.rs | 36 ++++++++++++++++++++++++++++++++ web/src/widget/slider.rs | 16 +++++++++++++++ web/src/widget/text.rs | 13 ++++++++++++ 16 files changed, 321 insertions(+), 29 deletions(-) create mode 100644 web/src/color.rs create mode 100644 web/src/element.rs create mode 100644 web/src/widget.rs create mode 100644 web/src/widget/button.rs create mode 100644 web/src/widget/checkbox.rs create mode 100644 web/src/widget/column.rs create mode 100644 web/src/widget/image.rs create mode 100644 web/src/widget/radio.rs create mode 100644 web/src/widget/row.rs create mode 100644 web/src/widget/slider.rs create mode 100644 web/src/widget/text.rs diff --git a/web/Cargo.toml b/web/Cargo.toml index acc5f18b..6d8c37b1 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -17,6 +17,7 @@ maintenance = { status = "actively-developed" } [dependencies] iced = { version = "0.1.0-alpha", path = ".." } dodrio = "0.1.0" +futures = "0.1" [dependencies.web-sys] version = "0.3.27" diff --git a/web/examples/tour/Cargo.toml b/web/examples/tour/Cargo.toml index 15f38fa7..c2db46ec 100644 --- a/web/examples/tour/Cargo.toml +++ b/web/examples/tour/Cargo.toml @@ -11,6 +11,7 @@ crate-type = ["cdylib"] [dependencies] iced_web = { path = "../.." } wasm-bindgen = "0.2.50" +futures = "0.1" log = "0.4" console_error_panic_hook = "0.1.6" console_log = "0.1.2" diff --git a/web/examples/tour/src/lib.rs b/web/examples/tour/src/lib.rs index e747a193..30855e8b 100644 --- a/web/examples/tour/src/lib.rs +++ b/web/examples/tour/src/lib.rs @@ -1,8 +1,35 @@ +use futures::{future, Future}; +use iced_web::UserInterface; use wasm_bindgen::prelude::*; +mod tour; + +use tour::Tour; + #[wasm_bindgen(start)] pub fn run() { console_error_panic_hook::set_once(); console_log::init_with_level(log::Level::Trace) .expect("Initialize logging"); + + let tour = Tour::new(); + + tour.run(); +} + +impl iced_web::UserInterface for Tour { + type Message = tour::Message; + + fn update( + &mut self, + message: tour::Message, + ) -> Box> { + self.update(message); + + Box::new(future::err(())) + } + + fn view(&mut self) -> iced_web::Element { + self.view() + } } diff --git a/web/examples/tour/src/tour.rs b/web/examples/tour/src/tour.rs index d0be99b0..6c24622e 100644 --- a/web/examples/tour/src/tour.rs +++ b/web/examples/tour/src/tour.rs @@ -1,12 +1,8 @@ -use super::widget::{ - button, slider, Button, Checkbox, Column, Element, Image, Radio, Row, - Slider, Text, +use iced_web::{ + button, slider, text::HorizontalAlignment, Align, Button, Checkbox, Color, + Column, Element, Image, Radio, Row, Slider, Text, }; -use ggez::graphics::{self, Color, FilterMode, BLACK}; -use ggez::Context; -use iced::{text::HorizontalAlignment, Align}; - pub struct Tour { steps: Steps, back_button: button::State, @@ -15,9 +11,9 @@ pub struct Tour { } impl Tour { - pub fn new(context: &mut Context) -> Tour { + pub fn new() -> Tour { Tour { - steps: Steps::new(context), + steps: Steps::new(), back_button: button::State::new(), next_button: button::State::new(), debug: false, @@ -72,7 +68,7 @@ impl Tour { .into(); if self.debug { - element.explain(BLACK) + element.explain(Color::BLACK) } else { element } @@ -92,7 +88,7 @@ struct Steps { } impl Steps { - fn new(context: &mut Context) -> Steps { + fn new() -> Steps { Steps { steps: vec![ Step::Welcome, @@ -109,19 +105,10 @@ impl Steps { size_slider: slider::State::new(), size: 30, color_sliders: [slider::State::new(); 3], - color: BLACK, + color: Color::BLACK, }, Step::Radio { selection: None }, Step::Image { - ferris: { - let mut image = - graphics::Image::new(context, "/ferris.png") - .expect("Load ferris image"); - - image.set_filter(FilterMode::Linear); - - image - }, width: 300, slider: slider::State::new(), }, @@ -183,7 +170,6 @@ enum Step { selection: Option, }, Image { - ferris: graphics::Image, width: u16, slider: slider::State, }, @@ -273,11 +259,7 @@ impl<'a> Step { color_sliders, color, } => Self::text(size_slider, *size, color_sliders, *color).into(), - Step::Image { - ferris, - width, - slider, - } => Self::image(ferris.clone(), *width, slider).into(), + Step::Image { width, slider } => Self::image(*width, slider).into(), Step::RowsAndColumns { layout, spacing_slider, @@ -489,13 +471,16 @@ impl<'a> Step { } fn image( - ferris: graphics::Image, width: u16, slider: &'a mut slider::State, ) -> Column<'a, StepMessage> { Self::container("Image") .push(Text::new("An image that tries to keep its aspect ratio.")) - .push(Image::new(ferris).width(width).align_self(Align::Center)) + .push( + Image::new("resources/ferris.png") + .width(width) + .align_self(Align::Center), + ) .push(Slider::new( slider, 100.0..=500.0, diff --git a/web/src/color.rs b/web/src/color.rs new file mode 100644 index 00000000..2624c3c9 --- /dev/null +++ b/web/src/color.rs @@ -0,0 +1,16 @@ +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Color { + pub r: f32, + pub g: f32, + pub b: f32, + pub a: f32, +} + +impl Color { + pub const BLACK: Color = Color { + r: 0.0, + g: 0.0, + b: 0.0, + a: 1.0, + }; +} diff --git a/web/src/element.rs b/web/src/element.rs new file mode 100644 index 00000000..c0c9ce5d --- /dev/null +++ b/web/src/element.rs @@ -0,0 +1,47 @@ +use crate::{Color, Widget}; + +pub struct Element<'a, Message> { + pub(crate) widget: Box + 'a>, +} + +impl<'a, Message> Element<'a, Message> { + pub fn new(widget: impl Widget + 'a) -> Self { + Self { + widget: Box::new(widget), + } + } + + pub fn explain(self, color: Color) -> Element<'a, Message> { + self + } + + pub fn map(self, f: F) -> Element<'a, B> + where + Message: 'static, + B: 'static, + F: 'static + Fn(Message) -> B, + { + Element { + widget: Box::new(Map::new(self.widget, f)), + } + } +} + +struct Map<'a, A, B> { + widget: Box + 'a>, + mapper: Box B>, +} + +impl<'a, A, B> Map<'a, A, B> { + pub fn new(widget: Box + 'a>, mapper: F) -> Map<'a, A, B> + where + F: 'static + Fn(A) -> B, + { + Map { + widget, + mapper: Box::new(mapper), + } + } +} + +impl<'a, A, B> Widget for Map<'a, A, B> {} diff --git a/web/src/lib.rs b/web/src/lib.rs index 8b137891..187e8ad9 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -1 +1,28 @@ +use futures::Future; +mod color; +mod element; +mod widget; + +pub use color::Color; +pub use element::Element; +pub use iced::Align; +pub use widget::*; + +pub trait UserInterface { + type Message; + + fn update( + &mut self, + message: Self::Message, + ) -> Box>; + + fn view(&mut self) -> Element; + + fn run(mut self) + where + Self: Sized, + { + let element = self.view(); + } +} diff --git a/web/src/widget.rs b/web/src/widget.rs new file mode 100644 index 00000000..5cb89a60 --- /dev/null +++ b/web/src/widget.rs @@ -0,0 +1,20 @@ +pub mod button; +pub mod slider; +pub mod text; + +mod checkbox; +mod column; +mod image; +mod radio; +mod row; + +pub use button::Button; +pub use checkbox::Checkbox; +pub use column::Column; +pub use image::Image; +pub use radio::Radio; +pub use row::Row; +pub use slider::Slider; +pub use text::Text; + +pub trait Widget {} diff --git a/web/src/widget/button.rs b/web/src/widget/button.rs new file mode 100644 index 00000000..1f117d82 --- /dev/null +++ b/web/src/widget/button.rs @@ -0,0 +1,16 @@ +use crate::{Element, Widget}; + +pub use iced::button::{Class, State}; + +pub type Button<'a, Message> = iced::Button<'a, Message>; + +impl<'a, Message> Widget for Button<'a, Message> {} + +impl<'a, Message> From> for Element<'a, Message> +where + Message: 'static, +{ + fn from(button: Button<'a, Message>) -> Element<'a, Message> { + Element::new(button) + } +} diff --git a/web/src/widget/checkbox.rs b/web/src/widget/checkbox.rs new file mode 100644 index 00000000..a231d801 --- /dev/null +++ b/web/src/widget/checkbox.rs @@ -0,0 +1,14 @@ +use crate::{Color, Element, Widget}; + +pub type Checkbox = iced::Checkbox; + +impl Widget for Checkbox {} + +impl<'a, Message> From> for Element<'a, Message> +where + Message: 'static, +{ + fn from(checkbox: Checkbox) -> Element<'a, Message> { + Element::new(checkbox) + } +} diff --git a/web/src/widget/column.rs b/web/src/widget/column.rs new file mode 100644 index 00000000..84068ce6 --- /dev/null +++ b/web/src/widget/column.rs @@ -0,0 +1,48 @@ +use crate::{Align, Element, Widget}; + +pub struct Column<'a, Message> { + children: Vec>, +} + +impl<'a, Message> Column<'a, Message> { + pub fn new() -> Self { + Self { + children: Vec::new(), + } + } + + pub fn spacing(self, _spacing: u16) -> Self { + self + } + + pub fn padding(self, _padding: u16) -> Self { + self + } + + pub fn max_width(self, _max_width: u16) -> Self { + self + } + + pub fn align_items(self, _align: Align) -> Self { + self + } + + pub fn push(mut self, element: E) -> Self + where + E: Into>, + { + self.children.push(element.into()); + self + } +} + +impl<'a, Message> Widget for Column<'a, Message> {} + +impl<'a, Message> From> for Element<'a, Message> +where + Message: 'static, +{ + fn from(column: Column<'a, Message>) -> Element<'a, Message> { + Element::new(column) + } +} diff --git a/web/src/widget/image.rs b/web/src/widget/image.rs new file mode 100644 index 00000000..ac144fd8 --- /dev/null +++ b/web/src/widget/image.rs @@ -0,0 +1,11 @@ +use crate::{Element, Widget}; + +pub type Image<'a> = iced::Image<&'a str>; + +impl<'a, Message> Widget for Image<'a> {} + +impl<'a, Message> From> for Element<'a, Message> { + fn from(image: Image<'a>) -> Element<'a, Message> { + Element::new(image) + } +} diff --git a/web/src/widget/radio.rs b/web/src/widget/radio.rs new file mode 100644 index 00000000..0c28b46f --- /dev/null +++ b/web/src/widget/radio.rs @@ -0,0 +1,14 @@ +use crate::{Color, Element, Widget}; + +pub type Radio = iced::Radio; + +impl Widget for Radio {} + +impl<'a, Message> From> for Element<'a, Message> +where + Message: 'static, +{ + fn from(radio: Radio) -> Element<'a, Message> { + Element::new(radio) + } +} diff --git a/web/src/widget/row.rs b/web/src/widget/row.rs new file mode 100644 index 00000000..fc474ec3 --- /dev/null +++ b/web/src/widget/row.rs @@ -0,0 +1,36 @@ +use crate::{Element, Widget}; + +pub struct Row<'a, Message> { + children: Vec>, +} + +impl<'a, Message> Row<'a, Message> { + pub fn new() -> Self { + Self { + children: Vec::new(), + } + } + + pub fn spacing(self, _spacing: u16) -> Self { + self + } + + pub fn push(mut self, element: E) -> Self + where + E: Into>, + { + self.children.push(element.into()); + self + } +} + +impl<'a, Message> Widget for Row<'a, Message> {} + +impl<'a, Message> From> for Element<'a, Message> +where + Message: 'static, +{ + fn from(column: Row<'a, Message>) -> Element<'a, Message> { + Element::new(column) + } +} diff --git a/web/src/widget/slider.rs b/web/src/widget/slider.rs new file mode 100644 index 00000000..9c83befb --- /dev/null +++ b/web/src/widget/slider.rs @@ -0,0 +1,16 @@ +use crate::{Element, Widget}; + +pub use iced::slider::State; + +pub type Slider<'a, Message> = iced::Slider<'a, Message>; + +impl<'a, Message> Widget for Slider<'a, Message> {} + +impl<'a, Message> From> for Element<'a, Message> +where + Message: 'static, +{ + fn from(slider: Slider<'a, Message>) -> Element<'a, Message> { + Element::new(slider) + } +} diff --git a/web/src/widget/text.rs b/web/src/widget/text.rs new file mode 100644 index 00000000..0c1f75b6 --- /dev/null +++ b/web/src/widget/text.rs @@ -0,0 +1,13 @@ +use crate::{Color, Element, Widget}; + +pub use iced::text::HorizontalAlignment; + +pub type Text = iced::Text; + +impl<'a, Message> Widget for Text {} + +impl<'a, Message> From for Element<'a, Message> { + fn from(text: Text) -> Element<'a, Message> { + Element::new(text) + } +} -- cgit From 8834772fa70850559f7bd82cc8432394e3fd9db7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 15 Sep 2019 17:43:15 +0200 Subject: Draft widget nodes and wire interaction --- src/widget/button.rs | 6 ++++-- src/widget/text.rs | 6 ++++-- web/src/bus.rs | 40 +++++++++++++++++++++++++++++++++++ web/src/element.rs | 25 +++++++++++++++++----- web/src/lib.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++--- web/src/widget.rs | 15 +++++++++++++- web/src/widget/button.rs | 34 +++++++++++++++++++++++++++--- web/src/widget/column.rs | 22 ++++++++++++++++++-- web/src/widget/row.rs | 22 ++++++++++++++++++-- web/src/widget/text.rs | 21 +++++++++++++++++-- 10 files changed, 223 insertions(+), 22 deletions(-) create mode 100644 web/src/bus.rs diff --git a/src/widget/button.rs b/src/widget/button.rs index abcdbfeb..6f5d9908 100644 --- a/src/widget/button.rs +++ b/src/widget/button.rs @@ -40,9 +40,11 @@ use std::hash::Hash; /// ![Button drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/button.png?raw=true) pub struct Button<'a, Message> { state: &'a mut State, - label: String, + /// The label of the button. + pub label: String, class: Class, - on_press: Option, + /// The message to produce when the button is pressed + pub on_press: Option, style: Style, } diff --git a/src/widget/text.rs b/src/widget/text.rs index 59b599bb..b529cfd2 100644 --- a/src/widget/text.rs +++ b/src/widget/text.rs @@ -29,8 +29,10 @@ use std::hash::Hash; /// ``` #[derive(Debug, Clone)] pub struct Text { - content: String, - size: Option, + /// The text contents + pub content: String, + /// The text size + pub size: Option, color: Option, style: Style, horizontal_alignment: HorizontalAlignment, diff --git a/web/src/bus.rs b/web/src/bus.rs new file mode 100644 index 00000000..d76466f5 --- /dev/null +++ b/web/src/bus.rs @@ -0,0 +1,40 @@ +use crate::Application; + +use std::rc::Rc; + +#[derive(Clone)] +pub struct Bus { + publish: Rc>, +} + +impl Bus +where + Message: 'static, +{ + pub fn new() -> Self { + Self { + publish: Rc::new(Box::new(|message, root| { + let app = root.unwrap_mut::>(); + + app.update(message) + })), + } + } + + pub fn publish(&self, message: Message, root: &mut dyn dodrio::RootRender) { + (self.publish)(message, root); + } + + pub fn map(&self, mapper: Rc Message>>) -> Bus + where + B: 'static, + { + let publish = self.publish.clone(); + + Bus { + publish: Rc::new(Box::new(move |message, root| { + publish(mapper(message), root) + })), + } + } +} diff --git a/web/src/element.rs b/web/src/element.rs index c0c9ce5d..8270d8db 100644 --- a/web/src/element.rs +++ b/web/src/element.rs @@ -1,4 +1,7 @@ -use crate::{Color, Widget}; +use crate::{Bus, Color, Widget}; + +use dodrio::bumpalo; +use std::rc::Rc; pub struct Element<'a, Message> { pub(crate) widget: Box + 'a>, @@ -11,7 +14,7 @@ impl<'a, Message> Element<'a, Message> { } } - pub fn explain(self, color: Color) -> Element<'a, Message> { + pub fn explain(self, _color: Color) -> Element<'a, Message> { self } @@ -29,7 +32,7 @@ impl<'a, Message> Element<'a, Message> { struct Map<'a, A, B> { widget: Box + 'a>, - mapper: Box B>, + mapper: Rc B>>, } impl<'a, A, B> Map<'a, A, B> { @@ -39,9 +42,21 @@ impl<'a, A, B> Map<'a, A, B> { { Map { widget, - mapper: Box::new(mapper), + mapper: Rc::new(Box::new(mapper)), } } } -impl<'a, A, B> Widget for Map<'a, A, B> {} +impl<'a, A, B> Widget for Map<'a, A, B> +where + A: 'static, + B: 'static, +{ + fn node<'b>( + &self, + bump: &'b bumpalo::Bump, + bus: &Bus, + ) -> dodrio::Node<'b> { + self.widget.node(bump, &bus.map(self.mapper.clone())) + } +} diff --git a/web/src/lib.rs b/web/src/lib.rs index 187e8ad9..3173e736 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -1,9 +1,13 @@ +use dodrio::bumpalo; use futures::Future; +use std::cell::RefCell; +mod bus; mod color; mod element; mod widget; +pub use bus::Bus; pub use color::Color; pub use element::Element; pub use iced::Align; @@ -19,10 +23,54 @@ pub trait UserInterface { fn view(&mut self) -> Element; - fn run(mut self) + fn run(self) where - Self: Sized, + Self: 'static + Sized, { - let element = self.view(); + let window = web_sys::window().unwrap(); + let document = window.document().unwrap(); + let body = document.body().unwrap(); + + let app = Application::new(self); + + let vdom = dodrio::Vdom::new(&body, app); + vdom.forget(); + } +} + +struct Application { + ui: RefCell>>, +} + +impl Application { + fn new(ui: impl UserInterface + 'static) -> Self { + Self { + ui: RefCell::new(Box::new(ui)), + } + } + + fn update(&mut self, message: Message) { + let mut ui = self.ui.borrow_mut(); + + // TODO: Resolve futures and publish resulting messages + let _ = ui.update(message); + } +} + +impl dodrio::Render for Application +where + Message: 'static, +{ + fn render<'a, 'bump>( + &'a self, + bump: &'bump bumpalo::Bump, + ) -> dodrio::Node<'bump> + where + 'a: 'bump, + { + let mut ui = self.ui.borrow_mut(); + let element = ui.view(); + + element.widget.node(bump, &Bus::new()) } } diff --git a/web/src/widget.rs b/web/src/widget.rs index 5cb89a60..7af592e1 100644 --- a/web/src/widget.rs +++ b/web/src/widget.rs @@ -1,3 +1,6 @@ +use crate::Bus; +use dodrio::bumpalo; + pub mod button; pub mod slider; pub mod text; @@ -17,4 +20,14 @@ pub use row::Row; pub use slider::Slider; pub use text::Text; -pub trait Widget {} +pub trait Widget { + fn node<'b>( + &self, + bump: &'b bumpalo::Bump, + _bus: &Bus, + ) -> dodrio::Node<'b> { + use dodrio::builder::*; + + div(bump).children(vec![text("WIP")]).finish() + } +} diff --git a/web/src/widget/button.rs b/web/src/widget/button.rs index 1f117d82..8ccda107 100644 --- a/web/src/widget/button.rs +++ b/web/src/widget/button.rs @@ -1,14 +1,42 @@ -use crate::{Element, Widget}; +use crate::{Bus, Element, Widget}; +use dodrio::bumpalo; pub use iced::button::{Class, State}; pub type Button<'a, Message> = iced::Button<'a, Message>; -impl<'a, Message> Widget for Button<'a, Message> {} +impl<'a, Message> Widget for Button<'a, Message> +where + Message: 'static + Copy, +{ + fn node<'b>( + &self, + bump: &'b bumpalo::Bump, + bus: &Bus, + ) -> dodrio::Node<'b> { + use dodrio::builder::*; + + let label = bumpalo::format!(in bump, "{}", self.label); + + let mut node = button(bump).children(vec![text(label.into_bump_str())]); + + if let Some(on_press) = self.on_press { + let event_bus = bus.clone(); + + node = node.on("click", move |root, vdom, _event| { + event_bus.publish(on_press, root); + + vdom.schedule_render(); + }); + } + + node.finish() + } +} impl<'a, Message> From> for Element<'a, Message> where - Message: 'static, + Message: 'static + Copy, { fn from(button: Button<'a, Message>) -> Element<'a, Message> { Element::new(button) diff --git a/web/src/widget/column.rs b/web/src/widget/column.rs index 84068ce6..b3131f5e 100644 --- a/web/src/widget/column.rs +++ b/web/src/widget/column.rs @@ -1,4 +1,6 @@ -use crate::{Align, Element, Widget}; +use crate::{Align, Bus, Element, Widget}; + +use dodrio::bumpalo; pub struct Column<'a, Message> { children: Vec>, @@ -36,7 +38,23 @@ impl<'a, Message> Column<'a, Message> { } } -impl<'a, Message> Widget for Column<'a, Message> {} +impl<'a, Message> Widget for Column<'a, Message> { + fn node<'b>( + &self, + bump: &'b bumpalo::Bump, + publish: &Bus, + ) -> dodrio::Node<'b> { + use dodrio::builder::*; + + let children: Vec<_> = self + .children + .iter() + .map(|element| element.widget.node(bump, publish)) + .collect(); + + div(bump).children(children).finish() + } +} impl<'a, Message> From> for Element<'a, Message> where diff --git a/web/src/widget/row.rs b/web/src/widget/row.rs index fc474ec3..40fc68e3 100644 --- a/web/src/widget/row.rs +++ b/web/src/widget/row.rs @@ -1,4 +1,6 @@ -use crate::{Element, Widget}; +use crate::{Bus, Element, Widget}; + +use dodrio::bumpalo; pub struct Row<'a, Message> { children: Vec>, @@ -24,7 +26,23 @@ impl<'a, Message> Row<'a, Message> { } } -impl<'a, Message> Widget for Row<'a, Message> {} +impl<'a, Message> Widget for Row<'a, Message> { + fn node<'b>( + &self, + bump: &'b bumpalo::Bump, + publish: &Bus, + ) -> dodrio::Node<'b> { + use dodrio::builder::*; + + let children: Vec<_> = self + .children + .iter() + .map(|element| element.widget.node(bump, publish)) + .collect(); + + div(bump).children(children).finish() + } +} impl<'a, Message> From> for Element<'a, Message> where diff --git a/web/src/widget/text.rs b/web/src/widget/text.rs index 0c1f75b6..b8fe9565 100644 --- a/web/src/widget/text.rs +++ b/web/src/widget/text.rs @@ -1,10 +1,27 @@ -use crate::{Color, Element, Widget}; +use crate::{Bus, Color, Element, Widget}; +use dodrio::bumpalo; pub use iced::text::HorizontalAlignment; pub type Text = iced::Text; -impl<'a, Message> Widget for Text {} +impl<'a, Message> Widget for Text { + fn node<'b>( + &self, + bump: &'b bumpalo::Bump, + _publish: &Bus, + ) -> dodrio::Node<'b> { + use dodrio::builder::*; + + let content = bumpalo::format!(in bump, "{}", self.content); + let size = bumpalo::format!(in bump, "font-size: {}px", self.size.unwrap_or(20)); + + p(bump) + .attr("style", size.into_bump_str()) + .children(vec![text(content.into_bump_str())]) + .finish() + } +} impl<'a, Message> From for Element<'a, Message> { fn from(text: Text) -> Element<'a, Message> { -- cgit From 655978f480c32bc696f0d5fe2fff834bfbf238ea Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 15 Sep 2019 18:53:13 +0200 Subject: Draft nodes for missing widgets --- src/widget/checkbox.rs | 9 ++++-- src/widget/image.rs | 6 ++-- src/widget/radio.rs | 9 ++++-- src/widget/slider.rs | 12 +++++--- web/Cargo.toml | 5 ++++ web/examples/tour/index.html | 2 +- web/examples/tour/resources/ferris.png | 1 + web/examples/tour/src/tour.rs | 4 +-- web/src/widget.rs | 6 +--- web/src/widget/button.rs | 4 +-- web/src/widget/checkbox.rs | 39 ++++++++++++++++++++++-- web/src/widget/column.rs | 5 +++- web/src/widget/image.rs | 25 ++++++++++++++-- web/src/widget/radio.rs | 40 +++++++++++++++++++++++-- web/src/widget/row.rs | 5 +++- web/src/widget/slider.rs | 55 +++++++++++++++++++++++++++++++--- 16 files changed, 191 insertions(+), 36 deletions(-) create mode 120000 web/examples/tour/resources/ferris.png diff --git a/src/widget/checkbox.rs b/src/widget/checkbox.rs index c60807fd..4ae167ad 100644 --- a/src/widget/checkbox.rs +++ b/src/widget/checkbox.rs @@ -38,9 +38,12 @@ use crate::{ /// /// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true) pub struct Checkbox { - is_checked: bool, - on_toggle: Box Message>, - label: String, + /// Whether the checkbox is checked or not + pub is_checked: bool, + /// Toggle message to fire + pub on_toggle: Box Message>, + /// The label of the checkbox + pub label: String, label_color: Option, } diff --git a/src/widget/image.rs b/src/widget/image.rs index d94bfea5..8c869ab8 100644 --- a/src/widget/image.rs +++ b/src/widget/image.rs @@ -24,9 +24,11 @@ use std::hash::Hash; /// let image = Image::new(my_handle); /// ``` pub struct Image { - image: I, + /// The image handle + pub image: I, source: Option>, - width: Option, + /// The width of the image + pub width: Option, height: Option, style: Style, } diff --git a/src/widget/radio.rs b/src/widget/radio.rs index 28353ef4..27c0ff17 100644 --- a/src/widget/radio.rs +++ b/src/widget/radio.rs @@ -47,9 +47,12 @@ use std::hash::Hash; /// /// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true) pub struct Radio { - is_selected: bool, - on_click: Message, - label: String, + /// Whether the radio button is selected or not + pub is_selected: bool, + /// The message to produce when the radio button is clicked + pub on_click: Message, + /// The label of the radio button + pub label: String, label_color: Option, } diff --git a/src/widget/slider.rs b/src/widget/slider.rs index cdec9ec4..8a0cea01 100644 --- a/src/widget/slider.rs +++ b/src/widget/slider.rs @@ -6,6 +6,7 @@ //! [`State`]: struct.State.html use std::hash::Hash; use std::ops::RangeInclusive; +use std::rc::Rc; use crate::input::{mouse, ButtonState}; use crate::{ @@ -42,9 +43,12 @@ use crate::{ /// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true) pub struct Slider<'a, Message> { state: &'a mut State, - range: RangeInclusive, - value: f32, - on_change: Box Message>, + /// The range of the slider + pub range: RangeInclusive, + /// The current value of the slider + pub value: f32, + /// The function to produce messages on change + pub on_change: Rc Message>>, style: Style, } @@ -85,7 +89,7 @@ impl<'a, Message> Slider<'a, Message> { state, value: value.max(*range.start()).min(*range.end()), range, - on_change: Box::new(on_change), + on_change: Rc::new(Box::new(on_change)), style: Style::default().min_width(100).fill_width(), } } diff --git a/web/Cargo.toml b/web/Cargo.toml index 6d8c37b1..0ab89570 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -18,6 +18,7 @@ maintenance = { status = "actively-developed" } iced = { version = "0.1.0-alpha", path = ".." } dodrio = "0.1.0" futures = "0.1" +wasm-bindgen = "0.2.50" [dependencies.web-sys] version = "0.3.27" @@ -25,4 +26,8 @@ features = [ "console", "Document", "HtmlElement", + "HtmlInputElement", + "Event", + "EventTarget", + "InputEvent", ] diff --git a/web/examples/tour/index.html b/web/examples/tour/index.html index a639a6cb..527cc54c 100644 --- a/web/examples/tour/index.html +++ b/web/examples/tour/index.html @@ -2,7 +2,7 @@ - Tour - Iced Web + Web Tour - Iced + + diff --git a/examples/tour/main.rs b/examples/tour/main.rs deleted file mode 100644 index 571bc2e2..00000000 --- a/examples/tour/main.rs +++ /dev/null @@ -1,193 +0,0 @@ -mod renderer; -mod tour; -mod widget; - -use renderer::Renderer; -use tour::Tour; -use widget::Column; - -use ggez; -use ggez::event; -use ggez::filesystem; -use ggez::graphics; -use ggez::input::mouse; - -pub fn main() -> ggez::GameResult { - let (context, event_loop) = { - &mut ggez::ContextBuilder::new("iced", "ggez") - .window_mode(ggez::conf::WindowMode { - width: 1280.0, - height: 1024.0, - resizable: true, - ..ggez::conf::WindowMode::default() - }) - .build()? - }; - - filesystem::mount( - context, - std::path::Path::new(&format!( - "{}/examples/resources", - env!("CARGO_MANIFEST_DIR") - )), - true, - ); - - let state = &mut Game::new(context)?; - - event::run(context, event_loop, state) -} - -struct Game { - spritesheet: graphics::Image, - font: graphics::Font, - tour: Tour, - - events: Vec, - cache: Option, -} - -impl Game { - fn new(context: &mut ggez::Context) -> ggez::GameResult { - graphics::set_default_filter(context, graphics::FilterMode::Nearest); - - Ok(Game { - spritesheet: graphics::Image::new(context, "/ui.png").unwrap(), - font: graphics::Font::new(context, "/Roboto-Regular.ttf").unwrap(), - tour: Tour::new(context), - - events: Vec::new(), - cache: Some(iced::Cache::default()), - }) - } -} - -impl event::EventHandler for Game { - fn update(&mut self, _ctx: &mut ggez::Context) -> ggez::GameResult { - Ok(()) - } - - fn mouse_button_down_event( - &mut self, - _context: &mut ggez::Context, - _button: mouse::MouseButton, - _x: f32, - _y: f32, - ) { - self.events.push(iced::Event::Mouse( - iced::input::mouse::Event::Input { - state: iced::input::ButtonState::Pressed, - button: iced::input::mouse::Button::Left, // TODO: Map `button` - }, - )); - } - - fn mouse_button_up_event( - &mut self, - _context: &mut ggez::Context, - _button: mouse::MouseButton, - _x: f32, - _y: f32, - ) { - self.events.push(iced::Event::Mouse( - iced::input::mouse::Event::Input { - state: iced::input::ButtonState::Released, - button: iced::input::mouse::Button::Left, // TODO: Map `button` - }, - )); - } - - fn mouse_motion_event( - &mut self, - _context: &mut ggez::Context, - x: f32, - y: f32, - _dx: f32, - _dy: f32, - ) { - self.events.push(iced::Event::Mouse( - iced::input::mouse::Event::CursorMoved { x, y }, - )); - } - - fn resize_event( - &mut self, - context: &mut ggez::Context, - width: f32, - height: f32, - ) { - graphics::set_screen_coordinates( - context, - graphics::Rect { - x: 0.0, - y: 0.0, - w: width, - h: height, - }, - ) - .expect("Set screen coordinates"); - } - - fn draw(&mut self, context: &mut ggez::Context) -> ggez::GameResult { - graphics::clear(context, graphics::WHITE); - - let screen = graphics::screen_coordinates(context); - - let (messages, cursor) = { - let view = self.tour.view(); - - let content = Column::new() - .width(screen.w as u16) - .height(screen.h as u16) - .padding(20) - .align_items(iced::Align::Center) - .justify_content(iced::Justify::Center) - .push(view); - - let renderer = &mut Renderer::new( - context, - self.spritesheet.clone(), - self.font, - ); - - let mut ui = iced::UserInterface::build( - content, - self.cache.take().unwrap(), - renderer, - ); - - let messages = ui.update(self.events.drain(..)); - let cursor = ui.draw(renderer); - - self.cache = Some(ui.into_cache()); - - renderer.flush(); - - (messages, cursor) - }; - - for message in messages { - self.tour.update(message); - } - - let cursor_type = into_cursor_type(cursor); - - if mouse::cursor_type(context) != cursor_type { - mouse::set_cursor_type(context, cursor_type); - } - - graphics::present(context)?; - Ok(()) - } -} - -fn into_cursor_type(cursor: iced::MouseCursor) -> mouse::MouseCursor { - match cursor { - iced::MouseCursor::OutOfBounds => mouse::MouseCursor::Default, - iced::MouseCursor::Idle => mouse::MouseCursor::Default, - iced::MouseCursor::Pointer => mouse::MouseCursor::Hand, - iced::MouseCursor::Working => mouse::MouseCursor::Progress, - iced::MouseCursor::Grab => mouse::MouseCursor::Grab, - iced::MouseCursor::Grabbing => mouse::MouseCursor::Grabbing, - } -} diff --git a/examples/tour/renderer.rs b/examples/tour/renderer.rs deleted file mode 100644 index 8746dd96..00000000 --- a/examples/tour/renderer.rs +++ /dev/null @@ -1,63 +0,0 @@ -mod button; -mod checkbox; -mod debugger; -mod image; -mod radio; -mod slider; -mod text; - -use ggez::graphics::{ - self, spritebatch::SpriteBatch, Font, Image, MeshBuilder, -}; -use ggez::Context; - -pub struct Renderer<'a> { - pub context: &'a mut Context, - pub sprites: SpriteBatch, - pub spritesheet: Image, - pub font: Font, - font_size: f32, - debug_mesh: Option, -} - -impl Renderer<'_> { - pub fn new( - context: &mut Context, - spritesheet: Image, - font: Font, - ) -> Renderer { - Renderer { - context, - sprites: SpriteBatch::new(spritesheet.clone()), - spritesheet, - font, - font_size: 20.0, - debug_mesh: None, - } - } - - pub fn flush(&mut self) { - graphics::draw( - self.context, - &self.sprites, - graphics::DrawParam::default(), - ) - .expect("Draw sprites"); - - graphics::draw_queued_text( - self.context, - graphics::DrawParam::default(), - Default::default(), - graphics::FilterMode::Linear, - ) - .expect("Draw text"); - - if let Some(debug_mesh) = self.debug_mesh.take() { - let mesh = - debug_mesh.build(self.context).expect("Build debug mesh"); - - graphics::draw(self.context, &mesh, graphics::DrawParam::default()) - .expect("Draw debug mesh"); - } - } -} diff --git a/examples/tour/renderer/button.rs b/examples/tour/renderer/button.rs deleted file mode 100644 index 486e07ed..00000000 --- a/examples/tour/renderer/button.rs +++ /dev/null @@ -1,145 +0,0 @@ -use super::Renderer; -use ggez::graphics::{ - self, Align, Color, DrawParam, Rect, Scale, Text, TextFragment, WHITE, -}; -use iced::{button, MouseCursor}; - -const LEFT: Rect = Rect { - x: 0.0, - y: 34.0, - w: 6.0, - h: 49.0, -}; - -const BACKGROUND: Rect = Rect { - x: LEFT.w, - y: LEFT.y, - w: 1.0, - h: LEFT.h, -}; - -const RIGHT: Rect = Rect { - x: LEFT.h - LEFT.w, - y: LEFT.y, - w: LEFT.w, - h: LEFT.h, -}; - -impl button::Renderer for Renderer<'_> { - fn draw( - &mut self, - cursor_position: iced::Point, - mut bounds: iced::Rectangle, - state: &button::State, - label: &str, - class: button::Class, - ) -> MouseCursor { - let mouse_over = bounds.contains(cursor_position); - - let mut state_offset = 0.0; - - if mouse_over { - if state.is_pressed() { - bounds.y += 4.0; - state_offset = RIGHT.x + RIGHT.w; - } else { - bounds.y -= 1.0; - } - } - - let class_index = match class { - button::Class::Primary => 0, - button::Class::Secondary => 1, - button::Class::Positive => 2, - }; - - let width = self.spritesheet.width() as f32; - let height = self.spritesheet.height() as f32; - - self.sprites.add(DrawParam { - src: Rect { - x: (LEFT.x + state_offset) / width, - y: (LEFT.y + class_index as f32 * LEFT.h) / height, - w: LEFT.w / width, - h: LEFT.h / height, - }, - dest: ggez::mint::Point2 { - x: bounds.x, - y: bounds.y, - }, - ..DrawParam::default() - }); - - self.sprites.add(DrawParam { - src: Rect { - x: (BACKGROUND.x + state_offset) / width, - y: (BACKGROUND.y + class_index as f32 * BACKGROUND.h) / height, - w: BACKGROUND.w / width, - h: BACKGROUND.h / height, - }, - dest: ggez::mint::Point2 { - x: bounds.x + LEFT.w, - y: bounds.y, - }, - scale: ggez::mint::Vector2 { - x: bounds.width - LEFT.w - RIGHT.w, - y: 1.0, - }, - ..DrawParam::default() - }); - - self.sprites.add(DrawParam { - src: Rect { - x: (RIGHT.x + state_offset) / width, - y: (RIGHT.y + class_index as f32 * RIGHT.h) / height, - w: RIGHT.w / width, - h: RIGHT.h / height, - }, - dest: ggez::mint::Point2 { - x: bounds.x + bounds.width - RIGHT.w, - y: bounds.y, - }, - ..DrawParam::default() - }); - - let mut text = Text::new(TextFragment { - text: String::from(label), - font: Some(self.font), - scale: Some(Scale { x: 20.0, y: 20.0 }), - ..Default::default() - }); - - text.set_bounds( - ggez::mint::Point2 { - x: bounds.width, - y: bounds.height, - }, - Align::Center, - ); - - graphics::queue_text( - self.context, - &text, - ggez::mint::Point2 { - x: bounds.x, - y: bounds.y + BACKGROUND.h / 4.0, - }, - Some(if mouse_over { - WHITE - } else { - Color { - r: 0.9, - g: 0.9, - b: 0.9, - a: 1.0, - } - }), - ); - - if mouse_over { - MouseCursor::Pointer - } else { - MouseCursor::OutOfBounds - } - } -} diff --git a/examples/tour/renderer/checkbox.rs b/examples/tour/renderer/checkbox.rs deleted file mode 100644 index 20a91be5..00000000 --- a/examples/tour/renderer/checkbox.rs +++ /dev/null @@ -1,64 +0,0 @@ -use super::Renderer; - -use ggez::graphics::{DrawParam, Rect}; -use iced::{checkbox, MouseCursor}; - -const SPRITE: Rect = Rect { - x: 98.0, - y: 0.0, - w: 28.0, - h: 28.0, -}; - -impl checkbox::Renderer for Renderer<'_> { - fn draw( - &mut self, - cursor_position: iced::Point, - bounds: iced::Rectangle, - text_bounds: iced::Rectangle, - is_checked: bool, - ) -> MouseCursor { - let mouse_over = bounds.contains(cursor_position) - || text_bounds.contains(cursor_position); - - let width = self.spritesheet.width() as f32; - let height = self.spritesheet.height() as f32; - - self.sprites.add(DrawParam { - src: Rect { - x: (SPRITE.x + (if mouse_over { SPRITE.w } else { 0.0 })) - / width, - y: SPRITE.y / height, - w: SPRITE.w / width, - h: SPRITE.h / height, - }, - dest: ggez::mint::Point2 { - x: bounds.x, - y: bounds.y, - }, - ..DrawParam::default() - }); - - if is_checked { - self.sprites.add(DrawParam { - src: Rect { - x: (SPRITE.x + SPRITE.w * 2.0) / width, - y: SPRITE.y / height, - w: SPRITE.w / width, - h: SPRITE.h / height, - }, - dest: ggez::mint::Point2 { - x: bounds.x, - y: bounds.y, - }, - ..DrawParam::default() - }); - } - - if mouse_over { - MouseCursor::Pointer - } else { - MouseCursor::OutOfBounds - } - } -} diff --git a/examples/tour/renderer/debugger.rs b/examples/tour/renderer/debugger.rs deleted file mode 100644 index 98124795..00000000 --- a/examples/tour/renderer/debugger.rs +++ /dev/null @@ -1,30 +0,0 @@ -use super::Renderer; -use ggez::graphics::{Color, DrawMode, MeshBuilder, Rect}; - -impl iced::renderer::Debugger for Renderer<'_> { - type Color = Color; - - fn explain(&mut self, layout: &iced::Layout<'_>, color: Color) { - let bounds = layout.bounds(); - - let mut debug_mesh = - self.debug_mesh.take().unwrap_or(MeshBuilder::new()); - - debug_mesh.rectangle( - DrawMode::stroke(1.0), - Rect { - x: bounds.x, - y: bounds.y, - w: bounds.width, - h: bounds.height, - }, - color, - ); - - self.debug_mesh = Some(debug_mesh); - - for child in layout.children() { - self.explain(&child, color); - } - } -} diff --git a/examples/tour/renderer/image.rs b/examples/tour/renderer/image.rs deleted file mode 100644 index c3ead5c9..00000000 --- a/examples/tour/renderer/image.rs +++ /dev/null @@ -1,51 +0,0 @@ -use super::Renderer; - -use ggez::{graphics, nalgebra}; -use iced::image; - -impl image::Renderer for Renderer<'_> { - fn node( - &self, - style: iced::Style, - image: &graphics::Image, - width: Option, - height: Option, - _source: Option>, - ) -> iced::Node { - let aspect_ratio = image.width() as f32 / image.height() as f32; - - let style = match (width, height) { - (Some(width), Some(height)) => style.width(width).height(height), - (Some(width), None) => style - .width(width) - .height((width as f32 / aspect_ratio).round() as u16), - (None, Some(height)) => style - .height(height) - .width((height as f32 * aspect_ratio).round() as u16), - (None, None) => style.width(image.width()).height(image.height()), - }; - - iced::Node::new(style) - } - - fn draw( - &mut self, - image: &graphics::Image, - bounds: iced::Rectangle, - _source: Option>, - ) { - // We should probably use batches to draw images efficiently and keep - // draw side-effect free, but this is good enough for the example. - graphics::draw( - self.context, - image, - graphics::DrawParam::new() - .dest(nalgebra::Point2::new(bounds.x, bounds.y)) - .scale(nalgebra::Vector2::new( - bounds.width / image.width() as f32, - bounds.height / image.height() as f32, - )), - ) - .expect("Draw image"); - } -} diff --git a/examples/tour/renderer/radio.rs b/examples/tour/renderer/radio.rs deleted file mode 100644 index 0f7815d6..00000000 --- a/examples/tour/renderer/radio.rs +++ /dev/null @@ -1,63 +0,0 @@ -use super::Renderer; - -use ggez::graphics::{DrawParam, Rect}; -use iced::{radio, MouseCursor, Point, Rectangle}; - -const SPRITE: Rect = Rect { - x: 98.0, - y: 28.0, - w: 28.0, - h: 28.0, -}; - -impl radio::Renderer for Renderer<'_> { - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - bounds_with_label: Rectangle, - is_selected: bool, - ) -> MouseCursor { - let mouse_over = bounds_with_label.contains(cursor_position); - - let width = self.spritesheet.width() as f32; - let height = self.spritesheet.height() as f32; - - self.sprites.add(DrawParam { - src: Rect { - x: (SPRITE.x + (if mouse_over { SPRITE.w } else { 0.0 })) - / width, - y: SPRITE.y / height, - w: SPRITE.w / width, - h: SPRITE.h / height, - }, - dest: ggez::mint::Point2 { - x: bounds.x, - y: bounds.y, - }, - ..DrawParam::default() - }); - - if is_selected { - self.sprites.add(DrawParam { - src: Rect { - x: (SPRITE.x + SPRITE.w * 2.0) / width, - y: SPRITE.y / height, - w: SPRITE.w / width, - h: SPRITE.h / height, - }, - dest: ggez::mint::Point2 { - x: bounds.x, - y: bounds.y, - }, - ..DrawParam::default() - }); - } - - if mouse_over { - MouseCursor::Pointer - } else { - MouseCursor::OutOfBounds - } - } -} diff --git a/examples/tour/renderer/slider.rs b/examples/tour/renderer/slider.rs deleted file mode 100644 index 146cee18..00000000 --- a/examples/tour/renderer/slider.rs +++ /dev/null @@ -1,82 +0,0 @@ -use super::Renderer; - -use ggez::graphics::{DrawParam, Rect}; -use iced::{slider, MouseCursor, Point, Rectangle}; -use std::ops::RangeInclusive; - -const RAIL: Rect = Rect { - x: 98.0, - y: 56.0, - w: 1.0, - h: 4.0, -}; - -const MARKER: Rect = Rect { - x: RAIL.x + 28.0, - y: RAIL.y, - w: 16.0, - h: 24.0, -}; - -impl slider::Renderer for Renderer<'_> { - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - state: &slider::State, - range: RangeInclusive, - value: f32, - ) -> MouseCursor { - let width = self.spritesheet.width() as f32; - let height = self.spritesheet.height() as f32; - - self.sprites.add(DrawParam { - src: Rect { - x: RAIL.x / width, - y: RAIL.y / height, - w: RAIL.w / width, - h: RAIL.h / height, - }, - dest: ggez::mint::Point2 { - x: bounds.x + MARKER.w as f32 / 2.0, - y: bounds.y + 12.5, - }, - scale: ggez::mint::Vector2 { - x: bounds.width - MARKER.w as f32, - y: 1.0, - }, - ..DrawParam::default() - }); - - let (range_start, range_end) = range.into_inner(); - - let marker_offset = (bounds.width - MARKER.w as f32) - * ((value - range_start) / (range_end - range_start).max(1.0)); - - let mouse_over = bounds.contains(cursor_position); - let is_active = state.is_dragging() || mouse_over; - - self.sprites.add(DrawParam { - src: Rect { - x: (MARKER.x + (if is_active { MARKER.w } else { 0.0 })) - / width, - y: MARKER.y / height, - w: MARKER.w / width, - h: MARKER.h / height, - }, - dest: ggez::mint::Point2 { - x: bounds.x + marker_offset.round(), - y: bounds.y + (if state.is_dragging() { 2.0 } else { 0.0 }), - }, - ..DrawParam::default() - }); - - if state.is_dragging() { - MouseCursor::Grabbing - } else if mouse_over { - MouseCursor::Grab - } else { - MouseCursor::OutOfBounds - } - } -} diff --git a/examples/tour/renderer/text.rs b/examples/tour/renderer/text.rs deleted file mode 100644 index ecf1481e..00000000 --- a/examples/tour/renderer/text.rs +++ /dev/null @@ -1,118 +0,0 @@ -use super::Renderer; -use ggez::graphics::{self, mint, Align, Color, Scale, Text, TextFragment}; - -use iced::text; -use std::cell::RefCell; -use std::f32; - -impl text::Renderer for Renderer<'_> { - fn node( - &self, - style: iced::Style, - content: &str, - size: Option, - ) -> iced::Node { - let font = self.font; - let font_cache = graphics::font_cache(self.context); - let content = String::from(content); - - // 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 = size.map(f32::from).unwrap_or(self.font_size); - - iced::Node::with_measure(style, move |bounds| { - let mut measure = measure.borrow_mut(); - - if measure.is_none() { - let bounds = ( - match bounds.width { - iced::Number::Undefined => f32::INFINITY, - iced::Number::Defined(w) => w, - }, - match bounds.height { - iced::Number::Undefined => f32::INFINITY, - iced::Number::Defined(h) => h, - }, - ); - - let mut text = Text::new(TextFragment { - text: content.clone(), - font: Some(font), - scale: Some(Scale { x: size, y: size }), - ..Default::default() - }); - - text.set_bounds( - mint::Point2 { - x: bounds.0, - y: bounds.1, - }, - Align::Left, - ); - - let (width, height) = font_cache.dimensions(&text); - - let size = iced::Size { - width: width as f32, - height: height as f32, - }; - - // 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, - bounds: iced::Rectangle, - content: &str, - size: Option, - color: Option, - horizontal_alignment: text::HorizontalAlignment, - _vertical_alignment: text::VerticalAlignment, - ) { - let size = size.map(f32::from).unwrap_or(self.font_size); - - let mut text = Text::new(TextFragment { - text: String::from(content), - font: Some(self.font), - scale: Some(Scale { x: size, y: size }), - ..Default::default() - }); - - text.set_bounds( - mint::Point2 { - x: bounds.width, - y: bounds.height, - }, - match horizontal_alignment { - text::HorizontalAlignment::Left => graphics::Align::Left, - text::HorizontalAlignment::Center => graphics::Align::Center, - text::HorizontalAlignment::Right => graphics::Align::Right, - }, - ); - - graphics::queue_text( - self.context, - &text, - mint::Point2 { - x: bounds.x, - y: bounds.y, - }, - color.or(Some(graphics::BLACK)), - ); - } -} diff --git a/examples/tour/resources/Roboto-LICENSE b/examples/tour/resources/Roboto-LICENSE new file mode 100644 index 00000000..75b52484 --- /dev/null +++ b/examples/tour/resources/Roboto-LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. diff --git a/examples/tour/resources/Roboto-Regular.ttf b/examples/tour/resources/Roboto-Regular.ttf new file mode 100644 index 00000000..2b6392ff Binary files /dev/null and b/examples/tour/resources/Roboto-Regular.ttf differ diff --git a/examples/tour/resources/ferris.png b/examples/tour/resources/ferris.png new file mode 100644 index 00000000..ebce1a14 Binary files /dev/null and b/examples/tour/resources/ferris.png differ diff --git a/examples/tour/resources/ui.png b/examples/tour/resources/ui.png new file mode 100644 index 00000000..4fd3beb3 Binary files /dev/null and b/examples/tour/resources/ui.png differ diff --git a/examples/tour/src/iced_ggez.rs b/examples/tour/src/iced_ggez.rs new file mode 100644 index 00000000..4a9c0ef4 --- /dev/null +++ b/examples/tour/src/iced_ggez.rs @@ -0,0 +1,6 @@ +mod renderer; +mod widget; + +pub use renderer::Cache as ImageCache; +pub use renderer::Renderer; +pub use widget::*; diff --git a/examples/tour/src/iced_ggez/main.rs b/examples/tour/src/iced_ggez/main.rs new file mode 100644 index 00000000..a8cf09e5 --- /dev/null +++ b/examples/tour/src/iced_ggez/main.rs @@ -0,0 +1,189 @@ +use iced_tour::{iced_ggez, Tour}; + +use ggez; +use ggez::event; +use ggez::filesystem; +use ggez::graphics; +use ggez::input::mouse; + +pub fn main() -> ggez::GameResult { + let (context, event_loop) = { + &mut ggez::ContextBuilder::new("iced", "ggez") + .window_mode(ggez::conf::WindowMode { + width: 1280.0, + height: 1024.0, + resizable: true, + ..ggez::conf::WindowMode::default() + }) + .build()? + }; + + filesystem::mount( + context, + std::path::Path::new(env!("CARGO_MANIFEST_DIR")), + true, + ); + + let state = &mut Game::new(context)?; + + event::run(context, event_loop, state) +} + +struct Game { + spritesheet: graphics::Image, + font: graphics::Font, + images: iced_ggez::ImageCache, + tour: Tour, + + events: Vec, + cache: Option, +} + +impl Game { + fn new(context: &mut ggez::Context) -> ggez::GameResult { + graphics::set_default_filter(context, graphics::FilterMode::Nearest); + + Ok(Game { + spritesheet: graphics::Image::new(context, "/resources/ui.png") + .unwrap(), + font: graphics::Font::new(context, "/resources/Roboto-Regular.ttf") + .unwrap(), + images: iced_ggez::ImageCache::new(), + tour: Tour::new(), + + events: Vec::new(), + cache: Some(iced::Cache::default()), + }) + } +} + +impl event::EventHandler for Game { + fn update(&mut self, _ctx: &mut ggez::Context) -> ggez::GameResult { + Ok(()) + } + + fn mouse_button_down_event( + &mut self, + _context: &mut ggez::Context, + _button: mouse::MouseButton, + _x: f32, + _y: f32, + ) { + self.events.push(iced::Event::Mouse( + iced::input::mouse::Event::Input { + state: iced::input::ButtonState::Pressed, + button: iced::input::mouse::Button::Left, // TODO: Map `button` + }, + )); + } + + fn mouse_button_up_event( + &mut self, + _context: &mut ggez::Context, + _button: mouse::MouseButton, + _x: f32, + _y: f32, + ) { + self.events.push(iced::Event::Mouse( + iced::input::mouse::Event::Input { + state: iced::input::ButtonState::Released, + button: iced::input::mouse::Button::Left, // TODO: Map `button` + }, + )); + } + + fn mouse_motion_event( + &mut self, + _context: &mut ggez::Context, + x: f32, + y: f32, + _dx: f32, + _dy: f32, + ) { + self.events.push(iced::Event::Mouse( + iced::input::mouse::Event::CursorMoved { x, y }, + )); + } + + fn resize_event( + &mut self, + context: &mut ggez::Context, + width: f32, + height: f32, + ) { + graphics::set_screen_coordinates( + context, + graphics::Rect { + x: 0.0, + y: 0.0, + w: width, + h: height, + }, + ) + .expect("Set screen coordinates"); + } + + fn draw(&mut self, context: &mut ggez::Context) -> ggez::GameResult { + graphics::clear(context, graphics::WHITE); + + let screen = graphics::screen_coordinates(context); + + let (messages, cursor) = { + let view = self.tour.view(); + + let content = iced_ggez::Column::new() + .width(screen.w as u16) + .height(screen.h as u16) + .padding(20) + .align_items(iced::Align::Center) + .justify_content(iced::Justify::Center) + .push(view); + + let renderer = &mut iced_ggez::Renderer::new( + context, + &mut self.images, + self.spritesheet.clone(), + self.font, + ); + + let mut ui = iced::UserInterface::build( + content, + self.cache.take().unwrap(), + renderer, + ); + + let messages = ui.update(self.events.drain(..)); + let cursor = ui.draw(renderer); + + self.cache = Some(ui.into_cache()); + + renderer.flush(); + + (messages, cursor) + }; + + for message in messages { + self.tour.update(message); + } + + let cursor_type = into_cursor_type(cursor); + + if mouse::cursor_type(context) != cursor_type { + mouse::set_cursor_type(context, cursor_type); + } + + graphics::present(context)?; + Ok(()) + } +} + +fn into_cursor_type(cursor: iced::MouseCursor) -> mouse::MouseCursor { + match cursor { + iced::MouseCursor::OutOfBounds => mouse::MouseCursor::Default, + iced::MouseCursor::Idle => mouse::MouseCursor::Default, + iced::MouseCursor::Pointer => mouse::MouseCursor::Hand, + iced::MouseCursor::Working => mouse::MouseCursor::Progress, + iced::MouseCursor::Grab => mouse::MouseCursor::Grab, + iced::MouseCursor::Grabbing => mouse::MouseCursor::Grabbing, + } +} diff --git a/examples/tour/src/iced_ggez/renderer.rs b/examples/tour/src/iced_ggez/renderer.rs new file mode 100644 index 00000000..e3181eaa --- /dev/null +++ b/examples/tour/src/iced_ggez/renderer.rs @@ -0,0 +1,77 @@ +mod button; +mod checkbox; +mod debugger; +mod image; +mod radio; +mod slider; +mod text; + +use ggez::graphics::{ + self, spritebatch::SpriteBatch, Font, Image, MeshBuilder, +}; +use ggez::Context; + +pub use image::Cache; + +pub struct Renderer<'a> { + pub context: &'a mut Context, + pub images: &'a mut image::Cache, + pub sprites: SpriteBatch, + pub spritesheet: Image, + pub font: Font, + font_size: f32, + debug_mesh: Option, +} + +impl<'a> Renderer<'a> { + pub fn new( + context: &'a mut Context, + images: &'a mut image::Cache, + spritesheet: Image, + font: Font, + ) -> Renderer<'a> { + Renderer { + context, + images, + sprites: SpriteBatch::new(spritesheet.clone()), + spritesheet, + font, + font_size: 20.0, + debug_mesh: None, + } + } + + pub fn flush(&mut self) { + graphics::draw( + self.context, + &self.sprites, + graphics::DrawParam::default(), + ) + .expect("Draw sprites"); + + graphics::draw_queued_text( + self.context, + graphics::DrawParam::default(), + Default::default(), + graphics::FilterMode::Linear, + ) + .expect("Draw text"); + + if let Some(debug_mesh) = self.debug_mesh.take() { + let mesh = + debug_mesh.build(self.context).expect("Build debug mesh"); + + graphics::draw(self.context, &mesh, graphics::DrawParam::default()) + .expect("Draw debug mesh"); + } + } +} + +pub fn into_color(color: iced::Color) -> graphics::Color { + graphics::Color { + r: color.r, + g: color.g, + b: color.b, + a: color.a, + } +} diff --git a/examples/tour/src/iced_ggez/renderer/button.rs b/examples/tour/src/iced_ggez/renderer/button.rs new file mode 100644 index 00000000..486e07ed --- /dev/null +++ b/examples/tour/src/iced_ggez/renderer/button.rs @@ -0,0 +1,145 @@ +use super::Renderer; +use ggez::graphics::{ + self, Align, Color, DrawParam, Rect, Scale, Text, TextFragment, WHITE, +}; +use iced::{button, MouseCursor}; + +const LEFT: Rect = Rect { + x: 0.0, + y: 34.0, + w: 6.0, + h: 49.0, +}; + +const BACKGROUND: Rect = Rect { + x: LEFT.w, + y: LEFT.y, + w: 1.0, + h: LEFT.h, +}; + +const RIGHT: Rect = Rect { + x: LEFT.h - LEFT.w, + y: LEFT.y, + w: LEFT.w, + h: LEFT.h, +}; + +impl button::Renderer for Renderer<'_> { + fn draw( + &mut self, + cursor_position: iced::Point, + mut bounds: iced::Rectangle, + state: &button::State, + label: &str, + class: button::Class, + ) -> MouseCursor { + let mouse_over = bounds.contains(cursor_position); + + let mut state_offset = 0.0; + + if mouse_over { + if state.is_pressed() { + bounds.y += 4.0; + state_offset = RIGHT.x + RIGHT.w; + } else { + bounds.y -= 1.0; + } + } + + let class_index = match class { + button::Class::Primary => 0, + button::Class::Secondary => 1, + button::Class::Positive => 2, + }; + + let width = self.spritesheet.width() as f32; + let height = self.spritesheet.height() as f32; + + self.sprites.add(DrawParam { + src: Rect { + x: (LEFT.x + state_offset) / width, + y: (LEFT.y + class_index as f32 * LEFT.h) / height, + w: LEFT.w / width, + h: LEFT.h / height, + }, + dest: ggez::mint::Point2 { + x: bounds.x, + y: bounds.y, + }, + ..DrawParam::default() + }); + + self.sprites.add(DrawParam { + src: Rect { + x: (BACKGROUND.x + state_offset) / width, + y: (BACKGROUND.y + class_index as f32 * BACKGROUND.h) / height, + w: BACKGROUND.w / width, + h: BACKGROUND.h / height, + }, + dest: ggez::mint::Point2 { + x: bounds.x + LEFT.w, + y: bounds.y, + }, + scale: ggez::mint::Vector2 { + x: bounds.width - LEFT.w - RIGHT.w, + y: 1.0, + }, + ..DrawParam::default() + }); + + self.sprites.add(DrawParam { + src: Rect { + x: (RIGHT.x + state_offset) / width, + y: (RIGHT.y + class_index as f32 * RIGHT.h) / height, + w: RIGHT.w / width, + h: RIGHT.h / height, + }, + dest: ggez::mint::Point2 { + x: bounds.x + bounds.width - RIGHT.w, + y: bounds.y, + }, + ..DrawParam::default() + }); + + let mut text = Text::new(TextFragment { + text: String::from(label), + font: Some(self.font), + scale: Some(Scale { x: 20.0, y: 20.0 }), + ..Default::default() + }); + + text.set_bounds( + ggez::mint::Point2 { + x: bounds.width, + y: bounds.height, + }, + Align::Center, + ); + + graphics::queue_text( + self.context, + &text, + ggez::mint::Point2 { + x: bounds.x, + y: bounds.y + BACKGROUND.h / 4.0, + }, + Some(if mouse_over { + WHITE + } else { + Color { + r: 0.9, + g: 0.9, + b: 0.9, + a: 1.0, + } + }), + ); + + if mouse_over { + MouseCursor::Pointer + } else { + MouseCursor::OutOfBounds + } + } +} diff --git a/examples/tour/src/iced_ggez/renderer/checkbox.rs b/examples/tour/src/iced_ggez/renderer/checkbox.rs new file mode 100644 index 00000000..20a91be5 --- /dev/null +++ b/examples/tour/src/iced_ggez/renderer/checkbox.rs @@ -0,0 +1,64 @@ +use super::Renderer; + +use ggez::graphics::{DrawParam, Rect}; +use iced::{checkbox, MouseCursor}; + +const SPRITE: Rect = Rect { + x: 98.0, + y: 0.0, + w: 28.0, + h: 28.0, +}; + +impl checkbox::Renderer for Renderer<'_> { + fn draw( + &mut self, + cursor_position: iced::Point, + bounds: iced::Rectangle, + text_bounds: iced::Rectangle, + is_checked: bool, + ) -> MouseCursor { + let mouse_over = bounds.contains(cursor_position) + || text_bounds.contains(cursor_position); + + let width = self.spritesheet.width() as f32; + let height = self.spritesheet.height() as f32; + + self.sprites.add(DrawParam { + src: Rect { + x: (SPRITE.x + (if mouse_over { SPRITE.w } else { 0.0 })) + / width, + y: SPRITE.y / height, + w: SPRITE.w / width, + h: SPRITE.h / height, + }, + dest: ggez::mint::Point2 { + x: bounds.x, + y: bounds.y, + }, + ..DrawParam::default() + }); + + if is_checked { + self.sprites.add(DrawParam { + src: Rect { + x: (SPRITE.x + SPRITE.w * 2.0) / width, + y: SPRITE.y / height, + w: SPRITE.w / width, + h: SPRITE.h / height, + }, + dest: ggez::mint::Point2 { + x: bounds.x, + y: bounds.y, + }, + ..DrawParam::default() + }); + } + + if mouse_over { + MouseCursor::Pointer + } else { + MouseCursor::OutOfBounds + } + } +} diff --git a/examples/tour/src/iced_ggez/renderer/debugger.rs b/examples/tour/src/iced_ggez/renderer/debugger.rs new file mode 100644 index 00000000..c6727881 --- /dev/null +++ b/examples/tour/src/iced_ggez/renderer/debugger.rs @@ -0,0 +1,30 @@ +use super::{into_color, Renderer}; +use ggez::graphics::{DrawMode, MeshBuilder, Rect}; + +impl iced::renderer::Debugger for Renderer<'_> { + type Color = iced::Color; + + fn explain(&mut self, layout: &iced::Layout<'_>, color: iced::Color) { + let bounds = layout.bounds(); + + let mut debug_mesh = + self.debug_mesh.take().unwrap_or(MeshBuilder::new()); + + debug_mesh.rectangle( + DrawMode::stroke(1.0), + Rect { + x: bounds.x, + y: bounds.y, + w: bounds.width, + h: bounds.height, + }, + into_color(color), + ); + + self.debug_mesh = Some(debug_mesh); + + for child in layout.children() { + self.explain(&child, color); + } + } +} diff --git a/examples/tour/src/iced_ggez/renderer/image.rs b/examples/tour/src/iced_ggez/renderer/image.rs new file mode 100644 index 00000000..17c6a56e --- /dev/null +++ b/examples/tour/src/iced_ggez/renderer/image.rs @@ -0,0 +1,86 @@ +use super::Renderer; + +use ggez::{graphics, nalgebra}; +use iced::image; + +pub struct Cache { + images: std::collections::HashMap, +} + +impl Cache { + pub fn new() -> Self { + Self { + images: std::collections::HashMap::new(), + } + } + + fn get<'a>( + &mut self, + name: &'a str, + context: &mut ggez::Context, + ) -> graphics::Image { + if let Some(image) = self.images.get(name) { + return image.clone(); + } + + let mut image = graphics::Image::new(context, &format!("/{}", name)) + .expect("Load ferris image"); + + image.set_filter(graphics::FilterMode::Linear); + + self.images.insert(name.to_string(), image.clone()); + + image + } +} + +impl<'a> image::Renderer<&'a str> for Renderer<'_> { + fn node( + &mut self, + style: iced::Style, + name: &&'a str, + width: Option, + height: Option, + _source: Option>, + ) -> iced::Node { + let image = self.images.get(name, self.context); + + let aspect_ratio = image.width() as f32 / image.height() as f32; + + let style = match (width, height) { + (Some(width), Some(height)) => style.width(width).height(height), + (Some(width), None) => style + .width(width) + .height((width as f32 / aspect_ratio).round() as u16), + (None, Some(height)) => style + .height(height) + .width((height as f32 * aspect_ratio).round() as u16), + (None, None) => style.width(image.width()).height(image.height()), + }; + + iced::Node::new(style) + } + + fn draw( + &mut self, + name: &&'a str, + bounds: iced::Rectangle, + _source: Option>, + ) { + let image = self.images.get(name, self.context); + + // We should probably use batches to draw images efficiently and keep + // draw side-effect free, but this is good enough for the example. + graphics::draw( + self.context, + &image, + graphics::DrawParam::new() + .dest(nalgebra::Point2::new(bounds.x, bounds.y)) + .scale(nalgebra::Vector2::new( + bounds.width / image.width() as f32, + bounds.height / image.height() as f32, + )), + ) + .expect("Draw image"); + } +} diff --git a/examples/tour/src/iced_ggez/renderer/radio.rs b/examples/tour/src/iced_ggez/renderer/radio.rs new file mode 100644 index 00000000..0f7815d6 --- /dev/null +++ b/examples/tour/src/iced_ggez/renderer/radio.rs @@ -0,0 +1,63 @@ +use super::Renderer; + +use ggez::graphics::{DrawParam, Rect}; +use iced::{radio, MouseCursor, Point, Rectangle}; + +const SPRITE: Rect = Rect { + x: 98.0, + y: 28.0, + w: 28.0, + h: 28.0, +}; + +impl radio::Renderer for Renderer<'_> { + fn draw( + &mut self, + cursor_position: Point, + bounds: Rectangle, + bounds_with_label: Rectangle, + is_selected: bool, + ) -> MouseCursor { + let mouse_over = bounds_with_label.contains(cursor_position); + + let width = self.spritesheet.width() as f32; + let height = self.spritesheet.height() as f32; + + self.sprites.add(DrawParam { + src: Rect { + x: (SPRITE.x + (if mouse_over { SPRITE.w } else { 0.0 })) + / width, + y: SPRITE.y / height, + w: SPRITE.w / width, + h: SPRITE.h / height, + }, + dest: ggez::mint::Point2 { + x: bounds.x, + y: bounds.y, + }, + ..DrawParam::default() + }); + + if is_selected { + self.sprites.add(DrawParam { + src: Rect { + x: (SPRITE.x + SPRITE.w * 2.0) / width, + y: SPRITE.y / height, + w: SPRITE.w / width, + h: SPRITE.h / height, + }, + dest: ggez::mint::Point2 { + x: bounds.x, + y: bounds.y, + }, + ..DrawParam::default() + }); + } + + if mouse_over { + MouseCursor::Pointer + } else { + MouseCursor::OutOfBounds + } + } +} diff --git a/examples/tour/src/iced_ggez/renderer/slider.rs b/examples/tour/src/iced_ggez/renderer/slider.rs new file mode 100644 index 00000000..146cee18 --- /dev/null +++ b/examples/tour/src/iced_ggez/renderer/slider.rs @@ -0,0 +1,82 @@ +use super::Renderer; + +use ggez::graphics::{DrawParam, Rect}; +use iced::{slider, MouseCursor, Point, Rectangle}; +use std::ops::RangeInclusive; + +const RAIL: Rect = Rect { + x: 98.0, + y: 56.0, + w: 1.0, + h: 4.0, +}; + +const MARKER: Rect = Rect { + x: RAIL.x + 28.0, + y: RAIL.y, + w: 16.0, + h: 24.0, +}; + +impl slider::Renderer for Renderer<'_> { + fn draw( + &mut self, + cursor_position: Point, + bounds: Rectangle, + state: &slider::State, + range: RangeInclusive, + value: f32, + ) -> MouseCursor { + let width = self.spritesheet.width() as f32; + let height = self.spritesheet.height() as f32; + + self.sprites.add(DrawParam { + src: Rect { + x: RAIL.x / width, + y: RAIL.y / height, + w: RAIL.w / width, + h: RAIL.h / height, + }, + dest: ggez::mint::Point2 { + x: bounds.x + MARKER.w as f32 / 2.0, + y: bounds.y + 12.5, + }, + scale: ggez::mint::Vector2 { + x: bounds.width - MARKER.w as f32, + y: 1.0, + }, + ..DrawParam::default() + }); + + let (range_start, range_end) = range.into_inner(); + + let marker_offset = (bounds.width - MARKER.w as f32) + * ((value - range_start) / (range_end - range_start).max(1.0)); + + let mouse_over = bounds.contains(cursor_position); + let is_active = state.is_dragging() || mouse_over; + + self.sprites.add(DrawParam { + src: Rect { + x: (MARKER.x + (if is_active { MARKER.w } else { 0.0 })) + / width, + y: MARKER.y / height, + w: MARKER.w / width, + h: MARKER.h / height, + }, + dest: ggez::mint::Point2 { + x: bounds.x + marker_offset.round(), + y: bounds.y + (if state.is_dragging() { 2.0 } else { 0.0 }), + }, + ..DrawParam::default() + }); + + if state.is_dragging() { + MouseCursor::Grabbing + } else if mouse_over { + MouseCursor::Grab + } else { + MouseCursor::OutOfBounds + } + } +} diff --git a/examples/tour/src/iced_ggez/renderer/text.rs b/examples/tour/src/iced_ggez/renderer/text.rs new file mode 100644 index 00000000..b5010639 --- /dev/null +++ b/examples/tour/src/iced_ggez/renderer/text.rs @@ -0,0 +1,118 @@ +use super::{into_color, Renderer}; +use ggez::graphics::{self, mint, Align, Scale, Text, TextFragment}; + +use iced::text; +use std::cell::RefCell; +use std::f32; + +impl text::Renderer for Renderer<'_> { + fn node( + &self, + style: iced::Style, + content: &str, + size: Option, + ) -> iced::Node { + let font = self.font; + let font_cache = graphics::font_cache(self.context); + let content = String::from(content); + + // 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 = size.map(f32::from).unwrap_or(self.font_size); + + iced::Node::with_measure(style, move |bounds| { + let mut measure = measure.borrow_mut(); + + if measure.is_none() { + let bounds = ( + match bounds.width { + iced::Number::Undefined => f32::INFINITY, + iced::Number::Defined(w) => w, + }, + match bounds.height { + iced::Number::Undefined => f32::INFINITY, + iced::Number::Defined(h) => h, + }, + ); + + let mut text = Text::new(TextFragment { + text: content.clone(), + font: Some(font), + scale: Some(Scale { x: size, y: size }), + ..Default::default() + }); + + text.set_bounds( + mint::Point2 { + x: bounds.0, + y: bounds.1, + }, + Align::Left, + ); + + let (width, height) = font_cache.dimensions(&text); + + let size = iced::Size { + width: width as f32, + height: height as f32, + }; + + // 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, + bounds: iced::Rectangle, + content: &str, + size: Option, + color: Option, + horizontal_alignment: text::HorizontalAlignment, + _vertical_alignment: text::VerticalAlignment, + ) { + let size = size.map(f32::from).unwrap_or(self.font_size); + + let mut text = Text::new(TextFragment { + text: String::from(content), + font: Some(self.font), + scale: Some(Scale { x: size, y: size }), + ..Default::default() + }); + + text.set_bounds( + mint::Point2 { + x: bounds.width, + y: bounds.height, + }, + match horizontal_alignment { + text::HorizontalAlignment::Left => graphics::Align::Left, + text::HorizontalAlignment::Center => graphics::Align::Center, + text::HorizontalAlignment::Right => graphics::Align::Right, + }, + ); + + graphics::queue_text( + self.context, + &text, + mint::Point2 { + x: bounds.x, + y: bounds.y, + }, + color.or(Some(iced::Color::BLACK)).map(into_color), + ); + } +} diff --git a/examples/tour/src/iced_ggez/widget.rs b/examples/tour/src/iced_ggez/widget.rs new file mode 100644 index 00000000..a365daca --- /dev/null +++ b/examples/tour/src/iced_ggez/widget.rs @@ -0,0 +1,12 @@ +use super::Renderer; + +pub use iced::{button, slider, text, Align, Button, Color, Slider}; + +pub type Text = iced::Text; +pub type Checkbox = iced::Checkbox; +pub type Radio = iced::Radio; +pub type Image<'a> = iced::Image<&'a str>; + +pub type Column<'a, Message> = iced::Column<'a, Message, Renderer<'a>>; +pub type Row<'a, Message> = iced::Row<'a, Message, Renderer<'a>>; +pub type Element<'a, Message> = iced::Element<'a, Message, Renderer<'a>>; diff --git a/examples/tour/src/lib.rs b/examples/tour/src/lib.rs new file mode 100644 index 00000000..eb41fcd9 --- /dev/null +++ b/examples/tour/src/lib.rs @@ -0,0 +1,11 @@ +pub mod tour; + +pub use tour::{Message, Tour}; + +mod widget; + +#[cfg(target_arch = "wasm32")] +mod web; + +#[cfg(not(target_arch = "wasm32"))] +pub mod iced_ggez; diff --git a/examples/tour/src/tour.rs b/examples/tour/src/tour.rs new file mode 100644 index 00000000..4bd7c8a3 --- /dev/null +++ b/examples/tour/src/tour.rs @@ -0,0 +1,563 @@ +use crate::widget::{ + button, slider, text::HorizontalAlignment, Align, Button, Checkbox, Color, + Column, Element, Image, Radio, Row, Slider, Text, +}; + +pub struct Tour { + steps: Steps, + back_button: button::State, + next_button: button::State, + debug: bool, +} + +impl Tour { + pub fn new() -> Tour { + Tour { + steps: Steps::new(), + back_button: button::State::new(), + next_button: button::State::new(), + debug: false, + } + } + + pub fn update(&mut self, event: Message) { + match event { + Message::BackPressed => { + self.steps.go_back(); + } + Message::NextPressed => { + self.steps.advance(); + } + Message::StepMessage(step_msg) => { + self.steps.update(step_msg, &mut self.debug); + } + } + } + + pub fn view(&mut self) -> Element { + let Tour { + steps, + back_button, + next_button, + .. + } = self; + + let mut controls = Row::new(); + + if steps.has_previous() { + controls = controls.push( + Button::new(back_button, "Back") + .on_press(Message::BackPressed) + .class(button::Class::Secondary), + ); + } + + controls = controls.push(Column::new()); + + if steps.can_continue() { + controls = controls.push( + Button::new(next_button, "Next").on_press(Message::NextPressed), + ); + } + + let element: Element<_> = Column::new() + .max_width(500) + .spacing(20) + .push(steps.view(self.debug).map(Message::StepMessage)) + .push(controls) + .into(); + + if self.debug { + element.explain(Color::BLACK) + } else { + element + } + } +} + +#[derive(Debug, Clone, Copy)] +pub enum Message { + BackPressed, + NextPressed, + StepMessage(StepMessage), +} + +struct Steps { + steps: Vec, + current: usize, +} + +impl Steps { + fn new() -> Steps { + Steps { + steps: vec![ + Step::Welcome, + Step::Slider { + state: slider::State::new(), + value: 50, + }, + Step::RowsAndColumns { + layout: Layout::Row, + spacing_slider: slider::State::new(), + spacing: 20, + }, + Step::Text { + size_slider: slider::State::new(), + size: 30, + color_sliders: [slider::State::new(); 3], + color: Color::BLACK, + }, + Step::Radio { selection: None }, + Step::Image { + width: 300, + slider: slider::State::new(), + }, + Step::Debugger, + Step::End, + ], + current: 0, + } + } + + fn update(&mut self, msg: StepMessage, debug: &mut bool) { + self.steps[self.current].update(msg, debug); + } + + fn view(&mut self, debug: bool) -> Element { + self.steps[self.current].view(debug) + } + + fn advance(&mut self) { + if self.can_continue() { + self.current += 1; + } + } + + fn go_back(&mut self) { + if self.has_previous() { + self.current -= 1; + } + } + + fn has_previous(&self) -> bool { + self.current > 0 + } + + fn can_continue(&self) -> bool { + self.current + 1 < self.steps.len() + && self.steps[self.current].can_continue() + } +} + +enum Step { + Welcome, + Slider { + state: slider::State, + value: u16, + }, + RowsAndColumns { + layout: Layout, + spacing_slider: slider::State, + spacing: u16, + }, + Text { + size_slider: slider::State, + size: u16, + color_sliders: [slider::State; 3], + color: Color, + }, + Radio { + selection: Option, + }, + Image { + width: u16, + slider: slider::State, + }, + Debugger, + End, +} + +#[derive(Debug, Clone, Copy)] +pub enum StepMessage { + SliderChanged(f32), + LayoutChanged(Layout), + SpacingChanged(f32), + TextSizeChanged(f32), + TextColorChanged(Color), + LanguageSelected(Language), + ImageWidthChanged(f32), + DebugToggled(bool), +} + +impl<'a> Step { + fn update(&mut self, msg: StepMessage, debug: &mut bool) { + match msg { + StepMessage::DebugToggled(value) => { + if let Step::Debugger = self { + *debug = value; + } + } + StepMessage::LanguageSelected(language) => { + if let Step::Radio { selection } = self { + *selection = Some(language); + } + } + StepMessage::SliderChanged(new_value) => { + if let Step::Slider { value, .. } = self { + *value = new_value.round() as u16; + } + } + StepMessage::TextSizeChanged(new_size) => { + if let Step::Text { size, .. } = self { + *size = new_size.round() as u16; + } + } + StepMessage::TextColorChanged(new_color) => { + if let Step::Text { color, .. } = self { + *color = new_color; + } + } + StepMessage::LayoutChanged(new_layout) => { + if let Step::RowsAndColumns { layout, .. } = self { + *layout = new_layout; + } + } + StepMessage::SpacingChanged(new_spacing) => { + if let Step::RowsAndColumns { spacing, .. } = self { + *spacing = new_spacing.round() as u16; + } + } + StepMessage::ImageWidthChanged(new_width) => { + if let Step::Image { width, .. } = self { + *width = new_width.round() as u16; + } + } + }; + } + + fn can_continue(&self) -> bool { + match self { + Step::Welcome => true, + Step::Radio { selection } => *selection == Some(Language::Rust), + Step::Slider { .. } => true, + Step::Text { .. } => true, + Step::Image { .. } => true, + Step::RowsAndColumns { .. } => true, + Step::Debugger => true, + Step::End => false, + } + } + + fn view(&mut self, debug: bool) -> Element { + match self { + Step::Welcome => Self::welcome().into(), + Step::Radio { selection } => Self::radio(*selection).into(), + Step::Slider { state, value } => Self::slider(state, *value).into(), + Step::Text { + size_slider, + size, + color_sliders, + color, + } => Self::text(size_slider, *size, color_sliders, *color).into(), + Step::Image { width, slider } => Self::image(*width, slider).into(), + Step::RowsAndColumns { + layout, + spacing_slider, + spacing, + } => { + Self::rows_and_columns(*layout, spacing_slider, *spacing).into() + } + Step::Debugger => Self::debugger(debug).into(), + Step::End => Self::end().into(), + } + } + + fn container(title: &str) -> Column<'a, StepMessage> { + Column::new() + .spacing(20) + .align_items(Align::Stretch) + .push(Text::new(title).size(50)) + } + + fn welcome() -> Column<'a, StepMessage> { + Self::container("Welcome!") + .push(Text::new( + "This a simple tour meant to showcase a bunch of widgets that \ + can be easily implemented on top of Iced.", + )) + .push(Text::new( + "Iced is a renderer-agnostic GUI library for Rust focused on \ + simplicity and type-safety. It is heavily inspired by Elm.", + )) + .push(Text::new( + "It was originally born as part of Coffee, an opinionated \ + 2D game engine for Rust.", + )) + .push(Text::new( + "Iced does not provide a built-in renderer. This example runs \ + on WebAssembly using dodrio, an experimental VDOM library \ + for Rust.", + )) + .push(Text::new( + "You will need to interact with the UI in order to reach the \ + end!", + )) + } + + fn slider( + state: &'a mut slider::State, + value: u16, + ) -> Column<'a, StepMessage> { + Self::container("Slider") + .push(Text::new( + "A slider allows you to smoothly select a value from a range \ + of values.", + )) + .push(Text::new( + "The following slider lets you choose an integer from \ + 0 to 100:", + )) + .push(Slider::new( + state, + 0.0..=100.0, + value as f32, + StepMessage::SliderChanged, + )) + .push( + Text::new(&value.to_string()) + .horizontal_alignment(HorizontalAlignment::Center), + ) + } + + fn rows_and_columns( + layout: Layout, + spacing_slider: &'a mut slider::State, + spacing: u16, + ) -> Column<'a, StepMessage> { + let row_radio = Radio::new( + Layout::Row, + "Row", + Some(layout), + StepMessage::LayoutChanged, + ); + + let column_radio = Radio::new( + Layout::Column, + "Column", + Some(layout), + StepMessage::LayoutChanged, + ); + + let layout_section: Element<_> = match layout { + Layout::Row => Row::new() + .spacing(spacing) + .push(row_radio) + .push(column_radio) + .into(), + Layout::Column => Column::new() + .spacing(spacing) + .push(row_radio) + .push(column_radio) + .into(), + }; + + let spacing_section = Column::new() + .spacing(10) + .push(Slider::new( + spacing_slider, + 0.0..=80.0, + spacing as f32, + StepMessage::SpacingChanged, + )) + .push( + Text::new(&format!("{} px", spacing)) + .horizontal_alignment(HorizontalAlignment::Center), + ); + + Self::container("Rows and columns") + .spacing(spacing) + .push(Text::new( + "Iced uses a layout model based on flexbox to position UI \ + elements.", + )) + .push(Text::new( + "Rows and columns can be used to distribute content \ + horizontally or vertically, respectively.", + )) + .push(layout_section) + .push(Text::new( + "You can also easily change the spacing between elements:", + )) + .push(spacing_section) + } + + fn text( + size_slider: &'a mut slider::State, + size: u16, + color_sliders: &'a mut [slider::State; 3], + color: Color, + ) -> Column<'a, StepMessage> { + let size_section = Column::new() + .padding(20) + .spacing(20) + .push(Text::new("You can change its size:")) + .push( + Text::new(&format!("This text is {} pixels", size)).size(size), + ) + .push(Slider::new( + size_slider, + 10.0..=70.0, + size as f32, + StepMessage::TextSizeChanged, + )); + + let [red, green, blue] = color_sliders; + let color_section = Column::new() + .padding(20) + .spacing(20) + .push(Text::new("And its color:")) + .push(Text::new(&format!("{:?}", color)).color(color)) + .push( + Row::new() + .spacing(10) + .push(Slider::new(red, 0.0..=1.0, color.r, move |r| { + StepMessage::TextColorChanged(Color { r, ..color }) + })) + .push(Slider::new(green, 0.0..=1.0, color.g, move |g| { + StepMessage::TextColorChanged(Color { g, ..color }) + })) + .push(Slider::new(blue, 0.0..=1.0, color.b, move |b| { + StepMessage::TextColorChanged(Color { b, ..color }) + })), + ); + + Self::container("Text") + .push(Text::new( + "Text is probably the most essential widget for your UI. \ + It will try to adapt to the dimensions of its container.", + )) + .push(size_section) + .push(color_section) + } + + fn radio(selection: Option) -> Column<'a, StepMessage> { + let question = Column::new() + .padding(20) + .spacing(10) + .push(Text::new("Iced is written in...").size(24)) + .push(Language::all().iter().cloned().fold( + Column::new().padding(10).spacing(20), + |choices, language| { + choices.push(Radio::new( + language, + language.into(), + selection, + StepMessage::LanguageSelected, + )) + }, + )); + + Self::container("Radio button") + .push(Text::new( + "A radio button is normally used to represent a choice... \ + Surprise test!", + )) + .push(question) + .push(Text::new( + "Iced works very well with iterators! The list above is \ + basically created by folding a column over the different \ + choices, creating a radio button for each one of them!", + )) + } + + fn image( + width: u16, + slider: &'a mut slider::State, + ) -> Column<'a, StepMessage> { + Self::container("Image") + .push(Text::new("An image that tries to keep its aspect ratio.")) + .push( + Image::new("resources/ferris.png") + .width(width) + .align_self(Align::Center), + ) + .push(Slider::new( + slider, + 100.0..=500.0, + width as f32, + StepMessage::ImageWidthChanged, + )) + .push( + Text::new(&format!("Width: {} px", width.to_string())) + .horizontal_alignment(HorizontalAlignment::Center), + ) + } + + fn debugger(debug: bool) -> Column<'a, StepMessage> { + Self::container("Debugger") + .push(Text::new( + "You can ask Iced to visually explain the layouting of the \ + different elements comprising your UI!", + )) + .push(Text::new( + "Give it a shot! Check the following checkbox to be able to \ + see element boundaries.", + )) + .push(Checkbox::new( + debug, + "Explain layout", + StepMessage::DebugToggled, + )) + .push(Text::new("Feel free to go back and take a look.")) + } + + fn end() -> Column<'a, StepMessage> { + Self::container("You reached the end!") + .push(Text::new( + "This tour will be updated as more features are added.", + )) + .push(Text::new("Make sure to keep an eye on it!")) + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Language { + Rust, + Elm, + Ruby, + Haskell, + C, + Other, +} + +impl Language { + fn all() -> [Language; 6] { + [ + Language::C, + Language::Elm, + Language::Ruby, + Language::Haskell, + Language::Rust, + Language::Other, + ] + } +} + +impl From for &str { + fn from(language: Language) -> &'static str { + match language { + Language::Rust => "Rust", + Language::Elm => "Elm", + Language::Ruby => "Ruby", + Language::Haskell => "Haskell", + Language::C => "C", + Language::Other => "Other", + } + } +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Layout { + Row, + Column, +} diff --git a/examples/tour/src/web.rs b/examples/tour/src/web.rs new file mode 100644 index 00000000..a0a3060f --- /dev/null +++ b/examples/tour/src/web.rs @@ -0,0 +1,33 @@ +use futures::Future; +use iced_web::UserInterface; +use wasm_bindgen::prelude::*; + +use crate::tour::{self, Tour}; + +#[wasm_bindgen(start)] +pub fn run() { + console_error_panic_hook::set_once(); + console_log::init_with_level(log::Level::Trace) + .expect("Initialize logging"); + + let tour = Tour::new(); + + tour.run(); +} + +impl iced_web::UserInterface for Tour { + type Message = tour::Message; + + fn update( + &mut self, + message: tour::Message, + ) -> Option>> { + self.update(message); + + None + } + + fn view(&mut self) -> iced_web::Element { + self.view() + } +} diff --git a/examples/tour/src/widget.rs b/examples/tour/src/widget.rs new file mode 100644 index 00000000..9c2c4d5b --- /dev/null +++ b/examples/tour/src/widget.rs @@ -0,0 +1,5 @@ +#[cfg(target_arch = "wasm32")] +pub use iced_web::*; + +#[cfg(not(target_arch = "wasm32"))] +pub use crate::iced_ggez::*; diff --git a/examples/tour/tour.rs b/examples/tour/tour.rs deleted file mode 100644 index d0be99b0..00000000 --- a/examples/tour/tour.rs +++ /dev/null @@ -1,578 +0,0 @@ -use super::widget::{ - button, slider, Button, Checkbox, Column, Element, Image, Radio, Row, - Slider, Text, -}; - -use ggez::graphics::{self, Color, FilterMode, BLACK}; -use ggez::Context; -use iced::{text::HorizontalAlignment, Align}; - -pub struct Tour { - steps: Steps, - back_button: button::State, - next_button: button::State, - debug: bool, -} - -impl Tour { - pub fn new(context: &mut Context) -> Tour { - Tour { - steps: Steps::new(context), - back_button: button::State::new(), - next_button: button::State::new(), - debug: false, - } - } - - pub fn update(&mut self, event: Message) { - match event { - Message::BackPressed => { - self.steps.go_back(); - } - Message::NextPressed => { - self.steps.advance(); - } - Message::StepMessage(step_msg) => { - self.steps.update(step_msg, &mut self.debug); - } - } - } - - pub fn view(&mut self) -> Element { - let Tour { - steps, - back_button, - next_button, - .. - } = self; - - let mut controls = Row::new(); - - if steps.has_previous() { - controls = controls.push( - Button::new(back_button, "Back") - .on_press(Message::BackPressed) - .class(button::Class::Secondary), - ); - } - - controls = controls.push(Column::new()); - - if steps.can_continue() { - controls = controls.push( - Button::new(next_button, "Next").on_press(Message::NextPressed), - ); - } - - let element: Element<_> = Column::new() - .max_width(500) - .spacing(20) - .push(steps.view(self.debug).map(Message::StepMessage)) - .push(controls) - .into(); - - if self.debug { - element.explain(BLACK) - } else { - element - } - } -} - -#[derive(Debug, Clone, Copy)] -pub enum Message { - BackPressed, - NextPressed, - StepMessage(StepMessage), -} - -struct Steps { - steps: Vec, - current: usize, -} - -impl Steps { - fn new(context: &mut Context) -> Steps { - Steps { - steps: vec![ - Step::Welcome, - Step::Slider { - state: slider::State::new(), - value: 50, - }, - Step::RowsAndColumns { - layout: Layout::Row, - spacing_slider: slider::State::new(), - spacing: 20, - }, - Step::Text { - size_slider: slider::State::new(), - size: 30, - color_sliders: [slider::State::new(); 3], - color: BLACK, - }, - Step::Radio { selection: None }, - Step::Image { - ferris: { - let mut image = - graphics::Image::new(context, "/ferris.png") - .expect("Load ferris image"); - - image.set_filter(FilterMode::Linear); - - image - }, - width: 300, - slider: slider::State::new(), - }, - Step::Debugger, - Step::End, - ], - current: 0, - } - } - - fn update(&mut self, msg: StepMessage, debug: &mut bool) { - self.steps[self.current].update(msg, debug); - } - - fn view(&mut self, debug: bool) -> Element { - self.steps[self.current].view(debug) - } - - fn advance(&mut self) { - if self.can_continue() { - self.current += 1; - } - } - - fn go_back(&mut self) { - if self.has_previous() { - self.current -= 1; - } - } - - fn has_previous(&self) -> bool { - self.current > 0 - } - - fn can_continue(&self) -> bool { - self.current + 1 < self.steps.len() - && self.steps[self.current].can_continue() - } -} - -enum Step { - Welcome, - Slider { - state: slider::State, - value: u16, - }, - RowsAndColumns { - layout: Layout, - spacing_slider: slider::State, - spacing: u16, - }, - Text { - size_slider: slider::State, - size: u16, - color_sliders: [slider::State; 3], - color: Color, - }, - Radio { - selection: Option, - }, - Image { - ferris: graphics::Image, - width: u16, - slider: slider::State, - }, - Debugger, - End, -} - -#[derive(Debug, Clone, Copy)] -pub enum StepMessage { - SliderChanged(f32), - LayoutChanged(Layout), - SpacingChanged(f32), - TextSizeChanged(f32), - TextColorChanged(Color), - LanguageSelected(Language), - ImageWidthChanged(f32), - DebugToggled(bool), -} - -impl<'a> Step { - fn update(&mut self, msg: StepMessage, debug: &mut bool) { - match msg { - StepMessage::DebugToggled(value) => { - if let Step::Debugger = self { - *debug = value; - } - } - StepMessage::LanguageSelected(language) => { - if let Step::Radio { selection } = self { - *selection = Some(language); - } - } - StepMessage::SliderChanged(new_value) => { - if let Step::Slider { value, .. } = self { - *value = new_value.round() as u16; - } - } - StepMessage::TextSizeChanged(new_size) => { - if let Step::Text { size, .. } = self { - *size = new_size.round() as u16; - } - } - StepMessage::TextColorChanged(new_color) => { - if let Step::Text { color, .. } = self { - *color = new_color; - } - } - StepMessage::LayoutChanged(new_layout) => { - if let Step::RowsAndColumns { layout, .. } = self { - *layout = new_layout; - } - } - StepMessage::SpacingChanged(new_spacing) => { - if let Step::RowsAndColumns { spacing, .. } = self { - *spacing = new_spacing.round() as u16; - } - } - StepMessage::ImageWidthChanged(new_width) => { - if let Step::Image { width, .. } = self { - *width = new_width.round() as u16; - } - } - }; - } - - fn can_continue(&self) -> bool { - match self { - Step::Welcome => true, - Step::Radio { selection } => *selection == Some(Language::Rust), - Step::Slider { .. } => true, - Step::Text { .. } => true, - Step::Image { .. } => true, - Step::RowsAndColumns { .. } => true, - Step::Debugger => true, - Step::End => false, - } - } - - fn view(&mut self, debug: bool) -> Element { - match self { - Step::Welcome => Self::welcome().into(), - Step::Radio { selection } => Self::radio(*selection).into(), - Step::Slider { state, value } => Self::slider(state, *value).into(), - Step::Text { - size_slider, - size, - color_sliders, - color, - } => Self::text(size_slider, *size, color_sliders, *color).into(), - Step::Image { - ferris, - width, - slider, - } => Self::image(ferris.clone(), *width, slider).into(), - Step::RowsAndColumns { - layout, - spacing_slider, - spacing, - } => { - Self::rows_and_columns(*layout, spacing_slider, *spacing).into() - } - Step::Debugger => Self::debugger(debug).into(), - Step::End => Self::end().into(), - } - } - - fn container(title: &str) -> Column<'a, StepMessage> { - Column::new() - .spacing(20) - .align_items(Align::Stretch) - .push(Text::new(title).size(50)) - } - - fn welcome() -> Column<'a, StepMessage> { - Self::container("Welcome!") - .push(Text::new( - "This a simple tour meant to showcase a bunch of widgets that \ - can be easily implemented on top of Iced.", - )) - .push(Text::new( - "Iced is a renderer-agnostic GUI library for Rust focused on \ - simplicity and type-safety. It is heavily inspired by Elm.", - )) - .push(Text::new( - "It was originally born as part of Coffee, an opinionated \ - 2D game engine for Rust.", - )) - .push(Text::new( - "Iced does not provide a built-in renderer. This example runs \ - on a fairly simple renderer built on top of ggez, another \ - game library.", - )) - .push(Text::new( - "You will need to interact with the UI in order to reach the \ - end!", - )) - } - - fn slider( - state: &'a mut slider::State, - value: u16, - ) -> Column<'a, StepMessage> { - Self::container("Slider") - .push(Text::new( - "A slider allows you to smoothly select a value from a range \ - of values.", - )) - .push(Text::new( - "The following slider lets you choose an integer from \ - 0 to 100:", - )) - .push(Slider::new( - state, - 0.0..=100.0, - value as f32, - StepMessage::SliderChanged, - )) - .push( - Text::new(&value.to_string()) - .horizontal_alignment(HorizontalAlignment::Center), - ) - } - - fn rows_and_columns( - layout: Layout, - spacing_slider: &'a mut slider::State, - spacing: u16, - ) -> Column<'a, StepMessage> { - let row_radio = Radio::new( - Layout::Row, - "Row", - Some(layout), - StepMessage::LayoutChanged, - ); - - let column_radio = Radio::new( - Layout::Column, - "Column", - Some(layout), - StepMessage::LayoutChanged, - ); - - let layout_section: Element<_> = match layout { - Layout::Row => Row::new() - .spacing(spacing) - .push(row_radio) - .push(column_radio) - .into(), - Layout::Column => Column::new() - .spacing(spacing) - .push(row_radio) - .push(column_radio) - .into(), - }; - - let spacing_section = Column::new() - .spacing(10) - .push(Slider::new( - spacing_slider, - 0.0..=80.0, - spacing as f32, - StepMessage::SpacingChanged, - )) - .push( - Text::new(&format!("{} px", spacing)) - .horizontal_alignment(HorizontalAlignment::Center), - ); - - Self::container("Rows and columns") - .spacing(spacing) - .push(Text::new( - "Iced uses a layout model based on flexbox to position UI \ - elements.", - )) - .push(Text::new( - "Rows and columns can be used to distribute content \ - horizontally or vertically, respectively.", - )) - .push(layout_section) - .push(Text::new( - "You can also easily change the spacing between elements:", - )) - .push(spacing_section) - } - - fn text( - size_slider: &'a mut slider::State, - size: u16, - color_sliders: &'a mut [slider::State; 3], - color: Color, - ) -> Column<'a, StepMessage> { - let size_section = Column::new() - .padding(20) - .spacing(20) - .push(Text::new("You can change its size:")) - .push( - Text::new(&format!("This text is {} pixels", size)).size(size), - ) - .push(Slider::new( - size_slider, - 10.0..=70.0, - size as f32, - StepMessage::TextSizeChanged, - )); - - let [red, green, blue] = color_sliders; - let color_section = Column::new() - .padding(20) - .spacing(20) - .push(Text::new("And its color:")) - .push(Text::new(&format!("{:?}", color)).color(color)) - .push( - Row::new() - .spacing(10) - .push(Slider::new(red, 0.0..=1.0, color.r, move |r| { - StepMessage::TextColorChanged(Color { r, ..color }) - })) - .push(Slider::new(green, 0.0..=1.0, color.g, move |g| { - StepMessage::TextColorChanged(Color { g, ..color }) - })) - .push(Slider::new(blue, 0.0..=1.0, color.b, move |b| { - StepMessage::TextColorChanged(Color { b, ..color }) - })), - ); - - Self::container("Text") - .push(Text::new( - "Text is probably the most essential widget for your UI. \ - It will try to adapt to the dimensions of its container.", - )) - .push(size_section) - .push(color_section) - } - - fn radio(selection: Option) -> Column<'a, StepMessage> { - let question = Column::new() - .padding(20) - .spacing(10) - .push(Text::new("Iced is written in...").size(24)) - .push(Language::all().iter().cloned().fold( - Column::new().padding(10).spacing(20), - |choices, language| { - choices.push(Radio::new( - language, - language.into(), - selection, - StepMessage::LanguageSelected, - )) - }, - )); - - Self::container("Radio button") - .push(Text::new( - "A radio button is normally used to represent a choice... \ - Surprise test!", - )) - .push(question) - .push(Text::new( - "Iced works very well with iterators! The list above is \ - basically created by folding a column over the different \ - choices, creating a radio button for each one of them!", - )) - } - - fn image( - ferris: graphics::Image, - width: u16, - slider: &'a mut slider::State, - ) -> Column<'a, StepMessage> { - Self::container("Image") - .push(Text::new("An image that tries to keep its aspect ratio.")) - .push(Image::new(ferris).width(width).align_self(Align::Center)) - .push(Slider::new( - slider, - 100.0..=500.0, - width as f32, - StepMessage::ImageWidthChanged, - )) - .push( - Text::new(&format!("Width: {} px", width.to_string())) - .horizontal_alignment(HorizontalAlignment::Center), - ) - } - - fn debugger(debug: bool) -> Column<'a, StepMessage> { - Self::container("Debugger") - .push(Text::new( - "You can ask Iced to visually explain the layouting of the \ - different elements comprising your UI!", - )) - .push(Text::new( - "Give it a shot! Check the following checkbox to be able to \ - see element boundaries.", - )) - .push(Checkbox::new( - debug, - "Explain layout", - StepMessage::DebugToggled, - )) - .push(Text::new("Feel free to go back and take a look.")) - } - - fn end() -> Column<'a, StepMessage> { - Self::container("You reached the end!") - .push(Text::new( - "This tour will be updated as more features are added.", - )) - .push(Text::new("Make sure to keep an eye on it!")) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Language { - Rust, - Elm, - Ruby, - Haskell, - C, - Other, -} - -impl Language { - fn all() -> [Language; 6] { - [ - Language::C, - Language::Elm, - Language::Ruby, - Language::Haskell, - Language::Rust, - Language::Other, - ] - } -} - -impl From for &str { - fn from(language: Language) -> &'static str { - match language { - Language::Rust => "Rust", - Language::Elm => "Elm", - Language::Ruby => "Ruby", - Language::Haskell => "Haskell", - Language::C => "C", - Language::Other => "Other", - } - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Layout { - Row, - Column, -} diff --git a/examples/tour/widget.rs b/examples/tour/widget.rs deleted file mode 100644 index 9a141c83..00000000 --- a/examples/tour/widget.rs +++ /dev/null @@ -1,14 +0,0 @@ -use super::Renderer; - -use ggez::graphics::{self, Color}; - -pub use iced::{button, slider, Button, Slider}; - -pub type Text = iced::Text; -pub type Checkbox = iced::Checkbox; -pub type Radio = iced::Radio; -pub type Image = iced::Image; - -pub type Column<'a, Message> = iced::Column<'a, Message, Renderer<'a>>; -pub type Row<'a, Message> = iced::Row<'a, Message, Renderer<'a>>; -pub type Element<'a, Message> = iced::Element<'a, Message, Renderer<'a>>; diff --git a/src/color.rs b/src/color.rs new file mode 100644 index 00000000..5cc3a084 --- /dev/null +++ b/src/color.rs @@ -0,0 +1,19 @@ +/// A color in the sRGB color space. +#[derive(Debug, Clone, Copy, PartialEq)] +#[allow(missing_docs)] +pub struct Color { + pub r: f32, + pub g: f32, + pub b: f32, + pub a: f32, +} + +impl Color { + /// The black color. + pub const BLACK: Color = Color { + r: 0.0, + g: 0.0, + b: 0.0, + a: 1.0, + }; +} diff --git a/src/element.rs b/src/element.rs index 70d06f42..c13bf4a0 100644 --- a/src/element.rs +++ b/src/element.rs @@ -223,7 +223,10 @@ impl<'a, Message, Renderer> Element<'a, Message, Renderer> { } } - pub(crate) fn compute_layout(&self, renderer: &Renderer) -> result::Layout { + pub(crate) fn compute_layout( + &self, + renderer: &mut Renderer, + ) -> result::Layout { let node = self.widget.node(renderer); node.0.compute_layout(geometry::Size::undefined()).unwrap() @@ -264,7 +267,7 @@ impl<'a, A, B, Renderer> Widget for Map<'a, A, B, Renderer> where A: Copy, { - fn node(&self, renderer: &Renderer) -> Node { + fn node(&self, renderer: &mut Renderer) -> Node { self.widget.node(renderer) } @@ -337,7 +340,7 @@ impl<'a, Message, Renderer> Widget where Renderer: renderer::Debugger, { - fn node(&self, renderer: &Renderer) -> Node { + fn node(&self, renderer: &mut Renderer) -> Node { self.element.widget.node(renderer) } diff --git a/src/lib.rs b/src/lib.rs index c1c18b41..56eee559 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -200,6 +200,7 @@ pub mod input; pub mod renderer; pub mod widget; +mod color; mod element; mod event; mod hasher; @@ -215,6 +216,7 @@ mod vector; #[doc(no_inline)] pub use stretch::{geometry::Size, number::Number}; +pub use color::Color; pub use element::Element; pub use event::Event; pub use hasher::Hasher; diff --git a/src/user_interface.rs b/src/user_interface.rs index 2c7cbf82..6a69f81a 100644 --- a/src/user_interface.rs +++ b/src/user_interface.rs @@ -69,7 +69,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { /// let user_interface = UserInterface::build( /// counter.view(), /// cache, - /// &renderer, + /// &mut renderer, /// ); /// /// // Update and draw the user interface here... @@ -82,7 +82,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { pub fn build>>( root: E, cache: Cache, - renderer: &Renderer, + renderer: &mut Renderer, ) -> Self { let root = root.into(); @@ -153,7 +153,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { /// let mut user_interface = UserInterface::build( /// counter.view(), /// cache, - /// &renderer, + /// &mut renderer, /// ); /// /// // Update the user interface @@ -236,7 +236,7 @@ impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { /// let mut user_interface = UserInterface::build( /// counter.view(), /// cache, - /// &renderer, + /// &mut renderer, /// ); /// /// let messages = user_interface.update(events.drain(..)); @@ -302,7 +302,7 @@ impl Cache { Cache { hash: hasher.finish(), - layout: root.compute_layout(&()), + layout: root.compute_layout(&mut ()), cursor_position: Point::new(0.0, 0.0), } } diff --git a/src/widget.rs b/src/widget.rs index 30606934..45451f47 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -59,7 +59,7 @@ pub trait Widget: std::fmt::Debug { /// [`Node`]: ../struct.Node.html /// [`Widget`]: trait.Widget.html /// [`Layout`]: ../struct.Layout.html - fn node(&self, renderer: &Renderer) -> Node; + fn node(&self, renderer: &mut Renderer) -> Node; /// Draws the [`Widget`] using the associated `Renderer`. /// diff --git a/src/widget/button.rs b/src/widget/button.rs index 6f5d9908..d2ea70e4 100644 --- a/src/widget/button.rs +++ b/src/widget/button.rs @@ -133,7 +133,7 @@ where Renderer: self::Renderer, Message: Copy + std::fmt::Debug, { - fn node(&self, _renderer: &Renderer) -> Node { + fn node(&self, _renderer: &mut Renderer) -> Node { Node::new(self.style.height(50)) } diff --git a/src/widget/checkbox.rs b/src/widget/checkbox.rs index 4ae167ad..6c13252d 100644 --- a/src/widget/checkbox.rs +++ b/src/widget/checkbox.rs @@ -98,7 +98,7 @@ where Color: 'static + Copy + std::fmt::Debug, Renderer: self::Renderer + text::Renderer, { - fn node(&self, renderer: &Renderer) -> Node { + fn node(&self, renderer: &mut Renderer) -> Node { Row::<(), Renderer>::new() .spacing(15) .align_items(Align::Center) diff --git a/src/widget/column.rs b/src/widget/column.rs index ff754e98..831f5b8f 100644 --- a/src/widget/column.rs +++ b/src/widget/column.rs @@ -136,7 +136,7 @@ impl<'a, Message, Renderer> Column<'a, Message, Renderer> { impl<'a, Message, Renderer> Widget for Column<'a, Message, Renderer> { - fn node(&self, renderer: &Renderer) -> Node { + fn node(&self, renderer: &mut Renderer) -> Node { let mut children: Vec = self .children .iter() diff --git a/src/widget/image.rs b/src/widget/image.rs index 8c869ab8..1601234e 100644 --- a/src/widget/image.rs +++ b/src/widget/image.rs @@ -99,7 +99,7 @@ where Renderer: self::Renderer, I: Clone, { - fn node(&self, renderer: &Renderer) -> Node { + fn node(&self, renderer: &mut Renderer) -> Node { renderer.node( self.style, &self.image, @@ -144,7 +144,7 @@ pub trait Renderer { /// [`Style`]: ../../struct.Style.html /// [`Image`]: struct.Image.html fn node( - &self, + &mut self, style: Style, image: &I, width: Option, diff --git a/src/widget/radio.rs b/src/widget/radio.rs index 27c0ff17..ba082ef5 100644 --- a/src/widget/radio.rs +++ b/src/widget/radio.rs @@ -111,7 +111,7 @@ where Renderer: self::Renderer + text::Renderer, Message: Copy + std::fmt::Debug, { - fn node(&self, renderer: &Renderer) -> Node { + fn node(&self, renderer: &mut Renderer) -> Node { Row::<(), Renderer>::new() .spacing(15) .align_items(Align::Center) diff --git a/src/widget/row.rs b/src/widget/row.rs index 959528dc..181020e3 100644 --- a/src/widget/row.rs +++ b/src/widget/row.rs @@ -133,7 +133,7 @@ impl<'a, Message, Renderer> Row<'a, Message, Renderer> { impl<'a, Message, Renderer> Widget for Row<'a, Message, Renderer> { - fn node(&self, renderer: &Renderer) -> Node { + fn node(&self, renderer: &mut Renderer) -> Node { let mut children: Vec = self .children .iter() diff --git a/src/widget/slider.rs b/src/widget/slider.rs index 8a0cea01..fb6db8c9 100644 --- a/src/widget/slider.rs +++ b/src/widget/slider.rs @@ -107,7 +107,7 @@ impl<'a, Message, Renderer> Widget for Slider<'a, Message> where Renderer: self::Renderer, { - fn node(&self, _renderer: &Renderer) -> Node { + fn node(&self, _renderer: &mut Renderer) -> Node { Node::new(self.style.height(25)) } diff --git a/src/widget/text.rs b/src/widget/text.rs index b529cfd2..457a6814 100644 --- a/src/widget/text.rs +++ b/src/widget/text.rs @@ -113,7 +113,7 @@ where Color: Copy + std::fmt::Debug, Renderer: self::Renderer, { - fn node(&self, renderer: &Renderer) -> Node { + fn node(&self, renderer: &mut Renderer) -> Node { renderer.node(self.style, &self.content, self.size) } diff --git a/web/examples/tour/Cargo.toml b/web/examples/tour/Cargo.toml deleted file mode 100644 index 65a860e2..00000000 --- a/web/examples/tour/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "iced_web_tour" -version = "0.0.0" -authors = ["Héctor Ramón Jiménez "] -edition = "2018" -publish = false - -[lib] -crate-type = ["cdylib"] - -[dependencies] -iced_web = { path = "../.." } -wasm-bindgen = "0.2.50" -futures-preview = "=0.3.0-alpha.18" -log = "0.4" -console_error_panic_hook = "0.1.6" -console_log = "0.1.2" diff --git a/web/examples/tour/index.html b/web/examples/tour/index.html deleted file mode 100644 index 527cc54c..00000000 --- a/web/examples/tour/index.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - Web Tour - Iced - - - - - diff --git a/web/examples/tour/resources/ferris.png b/web/examples/tour/resources/ferris.png deleted file mode 120000 index 9c4fb51c..00000000 --- a/web/examples/tour/resources/ferris.png +++ /dev/null @@ -1 +0,0 @@ -../../../../examples/resources/ferris.png \ No newline at end of file diff --git a/web/examples/tour/src/lib.rs b/web/examples/tour/src/lib.rs deleted file mode 100644 index dbf04df8..00000000 --- a/web/examples/tour/src/lib.rs +++ /dev/null @@ -1,35 +0,0 @@ -use futures::Future; -use iced_web::UserInterface; -use wasm_bindgen::prelude::*; - -mod tour; - -use tour::Tour; - -#[wasm_bindgen(start)] -pub fn run() { - console_error_panic_hook::set_once(); - console_log::init_with_level(log::Level::Trace) - .expect("Initialize logging"); - - let tour = Tour::new(); - - tour.run(); -} - -impl iced_web::UserInterface for Tour { - type Message = tour::Message; - - fn update( - &mut self, - message: tour::Message, - ) -> Option>> { - self.update(message); - - None - } - - fn view(&mut self) -> iced_web::Element { - self.view() - } -} diff --git a/web/examples/tour/src/tour.rs b/web/examples/tour/src/tour.rs deleted file mode 100644 index 9a60e092..00000000 --- a/web/examples/tour/src/tour.rs +++ /dev/null @@ -1,563 +0,0 @@ -use iced_web::{ - button, slider, text::HorizontalAlignment, Align, Button, Checkbox, Color, - Column, Element, Image, Radio, Row, Slider, Text, -}; - -pub struct Tour { - steps: Steps, - back_button: button::State, - next_button: button::State, - debug: bool, -} - -impl Tour { - pub fn new() -> Tour { - Tour { - steps: Steps::new(), - back_button: button::State::new(), - next_button: button::State::new(), - debug: false, - } - } - - pub fn update(&mut self, event: Message) { - match event { - Message::BackPressed => { - self.steps.go_back(); - } - Message::NextPressed => { - self.steps.advance(); - } - Message::StepMessage(step_msg) => { - self.steps.update(step_msg, &mut self.debug); - } - } - } - - pub fn view(&mut self) -> Element { - let Tour { - steps, - back_button, - next_button, - .. - } = self; - - let mut controls = Row::new(); - - if steps.has_previous() { - controls = controls.push( - Button::new(back_button, "Back") - .on_press(Message::BackPressed) - .class(button::Class::Secondary), - ); - } - - controls = controls.push(Column::new()); - - if steps.can_continue() { - controls = controls.push( - Button::new(next_button, "Next").on_press(Message::NextPressed), - ); - } - - let element: Element<_> = Column::new() - .max_width(500) - .spacing(20) - .push(steps.view(self.debug).map(Message::StepMessage)) - .push(controls) - .into(); - - if self.debug { - element.explain(Color::BLACK) - } else { - element - } - } -} - -#[derive(Debug, Clone, Copy)] -pub enum Message { - BackPressed, - NextPressed, - StepMessage(StepMessage), -} - -struct Steps { - steps: Vec, - current: usize, -} - -impl Steps { - fn new() -> Steps { - Steps { - steps: vec![ - Step::Welcome, - Step::Slider { - state: slider::State::new(), - value: 50, - }, - Step::RowsAndColumns { - layout: Layout::Row, - spacing_slider: slider::State::new(), - spacing: 20, - }, - Step::Text { - size_slider: slider::State::new(), - size: 30, - color_sliders: [slider::State::new(); 3], - color: Color::BLACK, - }, - Step::Radio { selection: None }, - Step::Image { - width: 300, - slider: slider::State::new(), - }, - Step::Debugger, - Step::End, - ], - current: 0, - } - } - - fn update(&mut self, msg: StepMessage, debug: &mut bool) { - self.steps[self.current].update(msg, debug); - } - - fn view(&mut self, debug: bool) -> Element { - self.steps[self.current].view(debug) - } - - fn advance(&mut self) { - if self.can_continue() { - self.current += 1; - } - } - - fn go_back(&mut self) { - if self.has_previous() { - self.current -= 1; - } - } - - fn has_previous(&self) -> bool { - self.current > 0 - } - - fn can_continue(&self) -> bool { - self.current + 1 < self.steps.len() - && self.steps[self.current].can_continue() - } -} - -enum Step { - Welcome, - Slider { - state: slider::State, - value: u16, - }, - RowsAndColumns { - layout: Layout, - spacing_slider: slider::State, - spacing: u16, - }, - Text { - size_slider: slider::State, - size: u16, - color_sliders: [slider::State; 3], - color: Color, - }, - Radio { - selection: Option, - }, - Image { - width: u16, - slider: slider::State, - }, - Debugger, - End, -} - -#[derive(Debug, Clone, Copy)] -pub enum StepMessage { - SliderChanged(f32), - LayoutChanged(Layout), - SpacingChanged(f32), - TextSizeChanged(f32), - TextColorChanged(Color), - LanguageSelected(Language), - ImageWidthChanged(f32), - DebugToggled(bool), -} - -impl<'a> Step { - fn update(&mut self, msg: StepMessage, debug: &mut bool) { - match msg { - StepMessage::DebugToggled(value) => { - if let Step::Debugger = self { - *debug = value; - } - } - StepMessage::LanguageSelected(language) => { - if let Step::Radio { selection } = self { - *selection = Some(language); - } - } - StepMessage::SliderChanged(new_value) => { - if let Step::Slider { value, .. } = self { - *value = new_value.round() as u16; - } - } - StepMessage::TextSizeChanged(new_size) => { - if let Step::Text { size, .. } = self { - *size = new_size.round() as u16; - } - } - StepMessage::TextColorChanged(new_color) => { - if let Step::Text { color, .. } = self { - *color = new_color; - } - } - StepMessage::LayoutChanged(new_layout) => { - if let Step::RowsAndColumns { layout, .. } = self { - *layout = new_layout; - } - } - StepMessage::SpacingChanged(new_spacing) => { - if let Step::RowsAndColumns { spacing, .. } = self { - *spacing = new_spacing.round() as u16; - } - } - StepMessage::ImageWidthChanged(new_width) => { - if let Step::Image { width, .. } = self { - *width = new_width.round() as u16; - } - } - }; - } - - fn can_continue(&self) -> bool { - match self { - Step::Welcome => true, - Step::Radio { selection } => *selection == Some(Language::Rust), - Step::Slider { .. } => true, - Step::Text { .. } => true, - Step::Image { .. } => true, - Step::RowsAndColumns { .. } => true, - Step::Debugger => true, - Step::End => false, - } - } - - fn view(&mut self, debug: bool) -> Element { - match self { - Step::Welcome => Self::welcome().into(), - Step::Radio { selection } => Self::radio(*selection).into(), - Step::Slider { state, value } => Self::slider(state, *value).into(), - Step::Text { - size_slider, - size, - color_sliders, - color, - } => Self::text(size_slider, *size, color_sliders, *color).into(), - Step::Image { width, slider } => Self::image(*width, slider).into(), - Step::RowsAndColumns { - layout, - spacing_slider, - spacing, - } => { - Self::rows_and_columns(*layout, spacing_slider, *spacing).into() - } - Step::Debugger => Self::debugger(debug).into(), - Step::End => Self::end().into(), - } - } - - fn container(title: &str) -> Column<'a, StepMessage> { - Column::new() - .spacing(20) - .align_items(Align::Stretch) - .push(Text::new(title).size(50)) - } - - fn welcome() -> Column<'a, StepMessage> { - Self::container("Welcome!") - .push(Text::new( - "This a simple tour meant to showcase a bunch of widgets that \ - can be easily implemented on top of Iced.", - )) - .push(Text::new( - "Iced is a renderer-agnostic GUI library for Rust focused on \ - simplicity and type-safety. It is heavily inspired by Elm.", - )) - .push(Text::new( - "It was originally born as part of Coffee, an opinionated \ - 2D game engine for Rust.", - )) - .push(Text::new( - "Iced does not provide a built-in renderer. This example runs \ - on WebAssembly using dodrio, an experimental VDOM library \ - for Rust.", - )) - .push(Text::new( - "You will need to interact with the UI in order to reach the \ - end!", - )) - } - - fn slider( - state: &'a mut slider::State, - value: u16, - ) -> Column<'a, StepMessage> { - Self::container("Slider") - .push(Text::new( - "A slider allows you to smoothly select a value from a range \ - of values.", - )) - .push(Text::new( - "The following slider lets you choose an integer from \ - 0 to 100:", - )) - .push(Slider::new( - state, - 0.0..=100.0, - value as f32, - StepMessage::SliderChanged, - )) - .push( - Text::new(&value.to_string()) - .horizontal_alignment(HorizontalAlignment::Center), - ) - } - - fn rows_and_columns( - layout: Layout, - spacing_slider: &'a mut slider::State, - spacing: u16, - ) -> Column<'a, StepMessage> { - let row_radio = Radio::new( - Layout::Row, - "Row", - Some(layout), - StepMessage::LayoutChanged, - ); - - let column_radio = Radio::new( - Layout::Column, - "Column", - Some(layout), - StepMessage::LayoutChanged, - ); - - let layout_section: Element<_> = match layout { - Layout::Row => Row::new() - .spacing(spacing) - .push(row_radio) - .push(column_radio) - .into(), - Layout::Column => Column::new() - .spacing(spacing) - .push(row_radio) - .push(column_radio) - .into(), - }; - - let spacing_section = Column::new() - .spacing(10) - .push(Slider::new( - spacing_slider, - 0.0..=80.0, - spacing as f32, - StepMessage::SpacingChanged, - )) - .push( - Text::new(&format!("{} px", spacing)) - .horizontal_alignment(HorizontalAlignment::Center), - ); - - Self::container("Rows and columns") - .spacing(spacing) - .push(Text::new( - "Iced uses a layout model based on flexbox to position UI \ - elements.", - )) - .push(Text::new( - "Rows and columns can be used to distribute content \ - horizontally or vertically, respectively.", - )) - .push(layout_section) - .push(Text::new( - "You can also easily change the spacing between elements:", - )) - .push(spacing_section) - } - - fn text( - size_slider: &'a mut slider::State, - size: u16, - color_sliders: &'a mut [slider::State; 3], - color: Color, - ) -> Column<'a, StepMessage> { - let size_section = Column::new() - .padding(20) - .spacing(20) - .push(Text::new("You can change its size:")) - .push( - Text::new(&format!("This text is {} pixels", size)).size(size), - ) - .push(Slider::new( - size_slider, - 10.0..=70.0, - size as f32, - StepMessage::TextSizeChanged, - )); - - let [red, green, blue] = color_sliders; - let color_section = Column::new() - .padding(20) - .spacing(20) - .push(Text::new("And its color:")) - .push(Text::new(&format!("{:?}", color)).color(color)) - .push( - Row::new() - .spacing(10) - .push(Slider::new(red, 0.0..=1.0, color.r, move |r| { - StepMessage::TextColorChanged(Color { r, ..color }) - })) - .push(Slider::new(green, 0.0..=1.0, color.g, move |g| { - StepMessage::TextColorChanged(Color { g, ..color }) - })) - .push(Slider::new(blue, 0.0..=1.0, color.b, move |b| { - StepMessage::TextColorChanged(Color { b, ..color }) - })), - ); - - Self::container("Text") - .push(Text::new( - "Text is probably the most essential widget for your UI. \ - It will try to adapt to the dimensions of its container.", - )) - .push(size_section) - .push(color_section) - } - - fn radio(selection: Option) -> Column<'a, StepMessage> { - let question = Column::new() - .padding(20) - .spacing(10) - .push(Text::new("Iced is written in...").size(24)) - .push(Language::all().iter().cloned().fold( - Column::new().padding(10).spacing(20), - |choices, language| { - choices.push(Radio::new( - language, - language.into(), - selection, - StepMessage::LanguageSelected, - )) - }, - )); - - Self::container("Radio button") - .push(Text::new( - "A radio button is normally used to represent a choice... \ - Surprise test!", - )) - .push(question) - .push(Text::new( - "Iced works very well with iterators! The list above is \ - basically created by folding a column over the different \ - choices, creating a radio button for each one of them!", - )) - } - - fn image( - width: u16, - slider: &'a mut slider::State, - ) -> Column<'a, StepMessage> { - Self::container("Image") - .push(Text::new("An image that tries to keep its aspect ratio.")) - .push( - Image::new("resources/ferris.png") - .width(width) - .align_self(Align::Center), - ) - .push(Slider::new( - slider, - 100.0..=500.0, - width as f32, - StepMessage::ImageWidthChanged, - )) - .push( - Text::new(&format!("Width: {} px", width.to_string())) - .horizontal_alignment(HorizontalAlignment::Center), - ) - } - - fn debugger(debug: bool) -> Column<'a, StepMessage> { - Self::container("Debugger") - .push(Text::new( - "You can ask Iced to visually explain the layouting of the \ - different elements comprising your UI!", - )) - .push(Text::new( - "Give it a shot! Check the following checkbox to be able to \ - see element boundaries.", - )) - .push(Checkbox::new( - debug, - "Explain layout", - StepMessage::DebugToggled, - )) - .push(Text::new("Feel free to go back and take a look.")) - } - - fn end() -> Column<'a, StepMessage> { - Self::container("You reached the end!") - .push(Text::new( - "This tour will be updated as more features are added.", - )) - .push(Text::new("Make sure to keep an eye on it!")) - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Language { - Rust, - Elm, - Ruby, - Haskell, - C, - Other, -} - -impl Language { - fn all() -> [Language; 6] { - [ - Language::C, - Language::Elm, - Language::Ruby, - Language::Haskell, - Language::Rust, - Language::Other, - ] - } -} - -impl From for &str { - fn from(language: Language) -> &'static str { - match language { - Language::Rust => "Rust", - Language::Elm => "Elm", - Language::Ruby => "Ruby", - Language::Haskell => "Haskell", - Language::C => "C", - Language::Other => "Other", - } - } -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Layout { - Row, - Column, -} diff --git a/web/src/color.rs b/web/src/color.rs deleted file mode 100644 index 2624c3c9..00000000 --- a/web/src/color.rs +++ /dev/null @@ -1,16 +0,0 @@ -#[derive(Debug, Clone, Copy, PartialEq)] -pub struct Color { - pub r: f32, - pub g: f32, - pub b: f32, - pub a: f32, -} - -impl Color { - pub const BLACK: Color = Color { - r: 0.0, - g: 0.0, - b: 0.0, - a: 1.0, - }; -} diff --git a/web/src/lib.rs b/web/src/lib.rs index 5a0dd6f2..a6dc2b79 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -3,14 +3,12 @@ use futures::Future; use std::cell::RefCell; mod bus; -mod color; mod element; mod widget; pub use bus::Bus; -pub use color::Color; pub use element::Element; -pub use iced::Align; +pub use iced::{Align, Color}; pub use widget::*; pub trait UserInterface { -- cgit From b83a4b42dd912b5f59d40e7d4f7f7ccdabc43019 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 19 Sep 2019 18:47:01 +0200 Subject: Remove generic `Color` in widgets --- examples/tour/src/iced_ggez/renderer/debugger.rs | 2 - examples/tour/src/iced_ggez/renderer/text.rs | 2 +- examples/tour/src/iced_ggez/widget.rs | 7 ++- src/element.rs | 15 +++---- src/lib.rs | 6 +-- src/renderer.rs | 11 +---- src/widget/checkbox.rs | 57 +++++++++++------------- src/widget/radio.rs | 49 +++++++++----------- src/widget/text.rs | 40 +++++++---------- web/src/widget/checkbox.rs | 4 +- web/src/widget/radio.rs | 4 +- web/src/widget/text.rs | 6 +-- 12 files changed, 83 insertions(+), 120 deletions(-) diff --git a/examples/tour/src/iced_ggez/renderer/debugger.rs b/examples/tour/src/iced_ggez/renderer/debugger.rs index c6727881..eba03ebf 100644 --- a/examples/tour/src/iced_ggez/renderer/debugger.rs +++ b/examples/tour/src/iced_ggez/renderer/debugger.rs @@ -2,8 +2,6 @@ use super::{into_color, Renderer}; use ggez::graphics::{DrawMode, MeshBuilder, Rect}; impl iced::renderer::Debugger for Renderer<'_> { - type Color = iced::Color; - fn explain(&mut self, layout: &iced::Layout<'_>, color: iced::Color) { let bounds = layout.bounds(); diff --git a/examples/tour/src/iced_ggez/renderer/text.rs b/examples/tour/src/iced_ggez/renderer/text.rs index b5010639..450ca2cd 100644 --- a/examples/tour/src/iced_ggez/renderer/text.rs +++ b/examples/tour/src/iced_ggez/renderer/text.rs @@ -5,7 +5,7 @@ use iced::text; use std::cell::RefCell; use std::f32; -impl text::Renderer for Renderer<'_> { +impl text::Renderer for Renderer<'_> { fn node( &self, style: iced::Style, diff --git a/examples/tour/src/iced_ggez/widget.rs b/examples/tour/src/iced_ggez/widget.rs index a365daca..c9f857f6 100644 --- a/examples/tour/src/iced_ggez/widget.rs +++ b/examples/tour/src/iced_ggez/widget.rs @@ -1,10 +1,9 @@ use super::Renderer; -pub use iced::{button, slider, text, Align, Button, Color, Slider}; +pub use iced::{ + button, slider, text, Align, Button, Checkbox, Color, Radio, Slider, Text, +}; -pub type Text = iced::Text; -pub type Checkbox = iced::Checkbox; -pub type Radio = iced::Radio; pub type Image<'a> = iced::Image<&'a str>; pub type Column<'a, Message> = iced::Column<'a, Message, Renderer<'a>>; diff --git a/src/element.rs b/src/element.rs index c13bf4a0..f6276fbf 100644 --- a/src/element.rs +++ b/src/element.rs @@ -1,7 +1,7 @@ use stretch::{geometry, result}; use crate::{ - renderer, Event, Hasher, Layout, MouseCursor, Node, Point, Widget, + renderer, Color, Event, Hasher, Layout, MouseCursor, Node, Point, Widget, }; /// A generic [`Widget`]. @@ -210,16 +210,16 @@ impl<'a, Message, Renderer> Element<'a, Message, Renderer> { /// /// [`Element`]: struct.Element.html /// [`Renderer`]: trait.Renderer.html - pub fn explain( + pub fn explain>( self, - color: Renderer::Color, + color: C, ) -> Element<'a, Message, Renderer> where Message: 'static, Renderer: 'a + renderer::Debugger, { Element { - widget: Box::new(Explain::new(self, color)), + widget: Box::new(Explain::new(self, color.into())), } } @@ -309,7 +309,7 @@ where struct Explain<'a, Message, Renderer: renderer::Debugger> { element: Element<'a, Message, Renderer>, - color: Renderer::Color, + color: Color, } impl<'a, Message, Renderer> std::fmt::Debug for Explain<'a, Message, Renderer> @@ -327,10 +327,7 @@ impl<'a, Message, Renderer> Explain<'a, Message, Renderer> where Renderer: renderer::Debugger, { - fn new( - element: Element<'a, Message, Renderer>, - color: Renderer::Color, - ) -> Self { + fn new(element: Element<'a, Message, Renderer>, color: Color) -> Self { Explain { element, color } } } diff --git a/src/lib.rs b/src/lib.rs index 56eee559..8dd6163e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,7 +78,7 @@ //! # mod iced_wgpu { //! # use iced::{ //! # button, text, text::HorizontalAlignment, text::VerticalAlignment, -//! # MouseCursor, Node, Point, Rectangle, Style, +//! # MouseCursor, Node, Point, Rectangle, Style, Color //! # }; //! # //! # pub struct Renderer {} @@ -96,7 +96,7 @@ //! # } //! # } //! # -//! # impl text::Renderer<[f32; 4]> for Renderer { +//! # impl text::Renderer for Renderer { //! # fn node(&self, style: Style, _content: &str, _size: Option) -> Node { //! # Node::new(style) //! # } @@ -106,7 +106,7 @@ //! # _bounds: Rectangle, //! # _content: &str, //! # _size: Option, -//! # _color: Option<[f32; 4]>, +//! # _color: Option, //! # _horizontal_alignment: HorizontalAlignment, //! # _vertical_alignment: VerticalAlignment, //! # ) { diff --git a/src/renderer.rs b/src/renderer.rs index b445190b..2244f00b 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -17,19 +17,12 @@ //! [`text::Renderer`]: ../widget/text/trait.Renderer.html //! [`Checkbox`]: ../widget/checkbox/struct.Checkbox.html //! [`checkbox::Renderer`]: ../widget/checkbox/trait.Renderer.html -use crate::Layout; +use crate::{Color, Layout}; /// A renderer able to graphically explain a [`Layout`]. /// /// [`Layout`]: ../struct.Layout.html pub trait Debugger { - /// The color type that will be used to configure the _explanation_. - /// - /// This is the type that will be asked in [`Element::explain`]. - /// - /// [`Element::explain`]: ../struct.Element.html#method.explain - type Color: Copy; - /// Explains the [`Layout`] of an [`Element`] for debugging purposes. /// /// This will be called when [`Element::explain`] has been used. It should @@ -41,5 +34,5 @@ pub trait Debugger { /// [`Layout`]: struct.Layout.html /// [`Element`]: struct.Element.html /// [`Element::explain`]: struct.Element.html#method.explain - fn explain(&mut self, layout: &Layout<'_>, color: Self::Color); + fn explain(&mut self, layout: &Layout<'_>, color: Color); } diff --git a/src/widget/checkbox.rs b/src/widget/checkbox.rs index 6c13252d..48400fae 100644 --- a/src/widget/checkbox.rs +++ b/src/widget/checkbox.rs @@ -4,11 +4,11 @@ use std::hash::Hash; use crate::input::{mouse, ButtonState}; use crate::widget::{text, Column, Row, Text}; use crate::{ - Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, - Widget, + Align, Color, Element, Event, Hasher, Layout, MouseCursor, Node, Point, + Rectangle, Widget, }; -/// A box that can be checked, with a generic text `Color`. +/// A box that can be checked. /// /// It implements [`Widget`] when the associated `Renderer` implements the /// [`checkbox::Renderer`] trait. @@ -19,12 +19,7 @@ use crate::{ /// # Example /// /// ``` -/// use iced::Checkbox; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } +/// use iced::{Checkbox, Color}; /// /// pub enum Message { /// CheckboxToggled(bool), @@ -32,25 +27,28 @@ use crate::{ /// /// let is_checked = true; /// -/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled) -/// .label_color(Color::Black); +/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled); /// ``` /// /// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true) -pub struct Checkbox { +pub struct Checkbox { /// Whether the checkbox is checked or not pub is_checked: bool, - /// Toggle message to fire + + /// Function to call when checkbox is toggled to produce a __message__. + /// + /// The function should be provided `true` when the checkbox is checked + /// and `false` otherwise. pub on_toggle: Box Message>, + /// The label of the checkbox pub label: String, - label_color: Option, + + /// The color of the label + pub label_color: Option, } -impl std::fmt::Debug for Checkbox -where - Color: std::fmt::Debug, -{ +impl std::fmt::Debug for Checkbox { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("Checkbox") .field("is_checked", &self.is_checked) @@ -60,7 +58,7 @@ where } } -impl Checkbox { +impl Checkbox { /// Creates a new [`Checkbox`]. /// /// It expects: @@ -83,20 +81,18 @@ impl Checkbox { } } - /// Sets the `Color` of the label of the [`Checkbox`]. + /// Sets the color of the label of the [`Checkbox`]. /// /// [`Checkbox`]: struct.Checkbox.html - pub fn label_color(mut self, color: Color) -> Self { - self.label_color = Some(color); + pub fn label_color>(mut self, color: C) -> Self { + self.label_color = Some(color.into()); self } } -impl Widget - for Checkbox +impl Widget for Checkbox where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, + Renderer: self::Renderer + text::Renderer, { fn node(&self, renderer: &mut Renderer) -> Node { Row::<(), Renderer>::new() @@ -191,16 +187,13 @@ pub trait Renderer { ) -> MouseCursor; } -impl<'a, Color, Message, Renderer> From> +impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, + Renderer: self::Renderer + text::Renderer, Message: 'static, { - fn from( - checkbox: Checkbox, - ) -> Element<'a, Message, Renderer> { + fn from(checkbox: Checkbox) -> Element<'a, Message, Renderer> { Element::new(checkbox) } } diff --git a/src/widget/radio.rs b/src/widget/radio.rs index ba082ef5..048aea94 100644 --- a/src/widget/radio.rs +++ b/src/widget/radio.rs @@ -2,13 +2,13 @@ use crate::input::{mouse, ButtonState}; use crate::widget::{text, Column, Row, Text}; use crate::{ - Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, - Widget, + Align, Color, Element, Event, Hasher, Layout, MouseCursor, Node, Point, + Rectangle, Widget, }; use std::hash::Hash; -/// A circular button representing a choice, with a generic text `Color`. +/// A circular button representing a choice. /// /// It implements [`Widget`] when the associated `Renderer` implements the /// [`radio::Renderer`] trait. @@ -18,12 +18,7 @@ use std::hash::Hash; /// /// # Example /// ``` -/// use iced::{Column, Radio}; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } +/// use iced::Radio; /// /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] /// pub enum Choice { @@ -38,27 +33,28 @@ use std::hash::Hash; /// /// let selected_choice = Some(Choice::A); /// -/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected) -/// .label_color(Color::Black); +/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected); /// -/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected) -/// .label_color(Color::Black); +/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected); /// ``` /// /// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true) -pub struct Radio { +pub struct Radio { /// Whether the radio button is selected or not pub is_selected: bool, + /// The message to produce when the radio button is clicked pub on_click: Message, + /// The label of the radio button pub label: String, - label_color: Option, + + /// The color of the label + pub label_color: Option, } -impl std::fmt::Debug for Radio +impl std::fmt::Debug for Radio where - Color: std::fmt::Debug, Message: std::fmt::Debug, { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -71,7 +67,7 @@ where } } -impl Radio { +impl Radio { /// Creates a new [`Radio`] button. /// /// It expects: @@ -98,17 +94,15 @@ impl Radio { /// Sets the `Color` of the label of the [`Radio`]. /// /// [`Radio`]: struct.Radio.html - pub fn label_color(mut self, color: Color) -> Self { - self.label_color = Some(color); + pub fn label_color>(mut self, color: C) -> Self { + self.label_color = Some(color.into()); self } } -impl Widget - for Radio +impl Widget for Radio where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, + Renderer: self::Renderer + text::Renderer, Message: Copy + std::fmt::Debug, { fn node(&self, renderer: &mut Renderer) -> Node { @@ -201,14 +195,13 @@ pub trait Renderer { ) -> MouseCursor; } -impl<'a, Color, Message, Renderer> From> +impl<'a, Message, Renderer> From> for Element<'a, Message, Renderer> where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer + text::Renderer, + Renderer: self::Renderer + text::Renderer, Message: 'static + Copy + std::fmt::Debug, { - fn from(checkbox: Radio) -> Element<'a, Message, Renderer> { + fn from(checkbox: Radio) -> Element<'a, Message, Renderer> { Element::new(checkbox) } } diff --git a/src/widget/text.rs b/src/widget/text.rs index 457a6814..4ef10d52 100644 --- a/src/widget/text.rs +++ b/src/widget/text.rs @@ -1,11 +1,12 @@ //! Write some text for your users to read. use crate::{ - Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, + Color, Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, + Widget, }; use std::hash::Hash; -/// A fragment of text with a generic `Color`. +/// A paragraph of text. /// /// It implements [`Widget`] when the associated `Renderer` implements the /// [`text::Renderer`] trait. @@ -16,19 +17,13 @@ use std::hash::Hash; /// # Example /// /// ``` -/// use iced::Text; -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Color { -/// Black, -/// } +/// use iced::{Text, Color}; /// /// Text::new("I <3 iced!") -/// .size(40) -/// .color(Color::Black); +/// .size(40); /// ``` #[derive(Debug, Clone)] -pub struct Text { +pub struct Text { /// The text contents pub content: String, /// The text size @@ -39,7 +34,7 @@ pub struct Text { vertical_alignment: VerticalAlignment, } -impl Text { +impl Text { /// Create a new fragment of [`Text`] with the given contents. /// /// [`Text`]: struct.Text.html @@ -65,8 +60,8 @@ impl Text { /// Sets the `Color` of the [`Text`]. /// /// [`Text`]: struct.Text.html - pub fn color(mut self, color: Color) -> Self { - self.color = Some(color); + pub fn color>(mut self, color: C) -> Self { + self.color = Some(color.into()); self } @@ -108,10 +103,9 @@ impl Text { } } -impl Widget for Text +impl Widget for Text where - Color: Copy + std::fmt::Debug, - Renderer: self::Renderer, + Renderer: self::Renderer, { fn node(&self, renderer: &mut Renderer) -> Node { renderer.node(self.style, &self.content, self.size) @@ -143,7 +137,7 @@ where } } -/// The renderer of a [`Text`] fragment with a generic `Color`. +/// The renderer of a [`Text`] fragment. /// /// Your [renderer] will need to implement this trait before being /// able to use [`Text`] in your [`UserInterface`]. @@ -151,7 +145,7 @@ where /// [`Text`]: struct.Text.html /// [renderer]: ../../renderer/index.html /// [`UserInterface`]: ../../struct.UserInterface.html -pub trait Renderer { +pub trait Renderer { /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] /// contents and size. /// @@ -188,13 +182,11 @@ pub trait Renderer { ); } -impl<'a, Message, Renderer, Color> From> - for Element<'a, Message, Renderer> +impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> where - Color: 'static + Copy + std::fmt::Debug, - Renderer: self::Renderer, + Renderer: self::Renderer, { - fn from(text: Text) -> Element<'a, Message, Renderer> { + fn from(text: Text) -> Element<'a, Message, Renderer> { Element::new(text) } } diff --git a/web/src/widget/checkbox.rs b/web/src/widget/checkbox.rs index e3c61ca7..5aadd65d 100644 --- a/web/src/widget/checkbox.rs +++ b/web/src/widget/checkbox.rs @@ -1,8 +1,8 @@ -use crate::{Bus, Color, Element, Widget}; +use crate::{Bus, Element, Widget}; use dodrio::bumpalo; -pub type Checkbox = iced::Checkbox; +pub use iced::Checkbox; impl Widget for Checkbox where diff --git a/web/src/widget/radio.rs b/web/src/widget/radio.rs index df08a977..e762ae28 100644 --- a/web/src/widget/radio.rs +++ b/web/src/widget/radio.rs @@ -1,8 +1,8 @@ -use crate::{Bus, Color, Element, Widget}; +use crate::{Bus, Element, Widget}; use dodrio::bumpalo; -pub type Radio = iced::Radio; +pub use iced::Radio; impl Widget for Radio where diff --git a/web/src/widget/text.rs b/web/src/widget/text.rs index b8fe9565..a5709775 100644 --- a/web/src/widget/text.rs +++ b/web/src/widget/text.rs @@ -1,9 +1,7 @@ -use crate::{Bus, Color, Element, Widget}; +use crate::{Bus, Element, Widget}; use dodrio::bumpalo; -pub use iced::text::HorizontalAlignment; - -pub type Text = iced::Text; +pub use iced::text::*; impl<'a, Message> Widget for Text { fn node<'b>( -- cgit From b9e0f7494881ad7cdfbcbc16878ecc6ef717753f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 20 Sep 2019 19:15:31 +0200 Subject: Create `iced_core` and `iced_native` --- Cargo.toml | 14 +- core/Cargo.toml | 13 + core/src/align.rs | 47 +++ core/src/color.rs | 19 ++ core/src/justify.rs | 44 +++ core/src/length.rs | 6 + core/src/lib.rs | 18 ++ core/src/point.rs | 31 ++ core/src/rectangle.rs | 30 ++ core/src/vector.rs | 15 + core/src/widget.rs | 27 ++ core/src/widget/button.rs | 158 ++++++++++ core/src/widget/checkbox.rs | 78 +++++ core/src/widget/column.rs | 147 +++++++++ core/src/widget/image.rs | 89 ++++++ core/src/widget/radio.rs | 88 ++++++ core/src/widget/row.rs | 142 +++++++++ core/src/widget/slider.rs | 123 ++++++++ core/src/widget/text.rs | 119 ++++++++ examples/tour/Cargo.toml | 2 +- examples/tour/src/iced_ggez/main.rs | 50 +-- examples/tour/src/iced_ggez/renderer.rs | 2 +- examples/tour/src/iced_ggez/renderer/button.rs | 29 +- examples/tour/src/iced_ggez/renderer/checkbox.rs | 46 ++- examples/tour/src/iced_ggez/renderer/debugger.rs | 8 +- examples/tour/src/iced_ggez/renderer/image.rs | 46 ++- examples/tour/src/iced_ggez/renderer/radio.rs | 45 ++- examples/tour/src/iced_ggez/renderer/slider.rs | 35 ++- examples/tour/src/iced_ggez/renderer/text.rs | 56 ++-- examples/tour/src/iced_ggez/widget.rs | 13 +- examples/tour/src/tour.rs | 8 +- native/Cargo.toml | 19 ++ native/src/element.rs | 372 ++++++++++++++++++++++ native/src/event.rs | 16 + native/src/hasher.rs | 19 ++ native/src/input.rs | 7 + native/src/input/button_state.rs | 24 ++ native/src/input/keyboard.rs | 6 + native/src/input/keyboard/event.rs | 23 ++ native/src/input/keyboard/key_code.rs | 374 +++++++++++++++++++++++ native/src/input/mouse.rs | 6 + native/src/input/mouse/button.rs | 32 ++ native/src/input/mouse/event.rs | 44 +++ native/src/layout.rs | 62 ++++ native/src/lib.rs | 228 ++++++++++++++ native/src/mouse_cursor.rs | 35 +++ native/src/node.rs | 60 ++++ native/src/renderer.rs | 38 +++ native/src/style.rs | 170 +++++++++++ native/src/user_interface.rs | 323 ++++++++++++++++++++ native/src/widget.rs | 120 ++++++++ native/src/widget/button.rs | 111 +++++++ native/src/widget/checkbox.rs | 95 ++++++ native/src/widget/column.rs | 118 +++++++ native/src/widget/image.rs | 67 ++++ native/src/widget/radio.rs | 92 ++++++ native/src/widget/row.rs | 117 +++++++ native/src/widget/slider.rs | 126 ++++++++ native/src/widget/text.rs | 77 +++++ src/color.rs | 19 -- src/element.rs | 370 ---------------------- src/event.rs | 16 - src/hasher.rs | 19 -- src/input.rs | 7 - src/input/button_state.rs | 24 -- src/input/keyboard.rs | 6 - src/input/keyboard/event.rs | 23 -- src/input/keyboard/key_code.rs | 374 ----------------------- src/input/mouse.rs | 6 - src/input/mouse/button.rs | 32 -- src/input/mouse/event.rs | 44 --- src/layout.rs | 62 ---- src/lib.rs | 231 -------------- src/mouse_cursor.rs | 35 --- src/node.rs | 60 ---- src/point.rs | 31 -- src/rectangle.rs | 30 -- src/renderer.rs | 38 --- src/style.rs | 262 ---------------- src/user_interface.rs | 323 -------------------- src/vector.rs | 15 - src/widget.rs | 114 ------- src/widget/button.rs | 284 ----------------- src/widget/checkbox.rs | 199 ------------ src/widget/column.rs | 224 -------------- src/widget/image.rs | 180 ----------- src/widget/panel.rs | 94 ------ src/widget/progress_bar.rs | 106 ------- src/widget/radio.rs | 207 ------------- src/widget/row.rs | 219 ------------- src/widget/slider.rs | 245 --------------- src/widget/text.rs | 218 ------------- web/Cargo.toml | 2 +- web/src/lib.rs | 2 +- web/src/widget/button.rs | 3 +- web/src/widget/checkbox.rs | 2 +- web/src/widget/column.rs | 38 +-- web/src/widget/image.rs | 20 +- web/src/widget/radio.rs | 2 +- web/src/widget/row.rs | 24 +- web/src/widget/slider.rs | 4 +- web/src/widget/text.rs | 2 +- 102 files changed, 4206 insertions(+), 4339 deletions(-) create mode 100644 core/Cargo.toml create mode 100644 core/src/align.rs create mode 100644 core/src/color.rs create mode 100644 core/src/justify.rs create mode 100644 core/src/length.rs create mode 100644 core/src/lib.rs create mode 100644 core/src/point.rs create mode 100644 core/src/rectangle.rs create mode 100644 core/src/vector.rs create mode 100644 core/src/widget.rs create mode 100644 core/src/widget/button.rs create mode 100644 core/src/widget/checkbox.rs create mode 100644 core/src/widget/column.rs create mode 100644 core/src/widget/image.rs create mode 100644 core/src/widget/radio.rs create mode 100644 core/src/widget/row.rs create mode 100644 core/src/widget/slider.rs create mode 100644 core/src/widget/text.rs create mode 100644 native/Cargo.toml create mode 100644 native/src/element.rs create mode 100644 native/src/event.rs create mode 100644 native/src/hasher.rs create mode 100644 native/src/input.rs create mode 100644 native/src/input/button_state.rs create mode 100644 native/src/input/keyboard.rs create mode 100644 native/src/input/keyboard/event.rs create mode 100644 native/src/input/keyboard/key_code.rs create mode 100644 native/src/input/mouse.rs create mode 100644 native/src/input/mouse/button.rs create mode 100644 native/src/input/mouse/event.rs create mode 100644 native/src/layout.rs create mode 100644 native/src/lib.rs create mode 100644 native/src/mouse_cursor.rs create mode 100644 native/src/node.rs create mode 100644 native/src/renderer.rs create mode 100644 native/src/style.rs create mode 100644 native/src/user_interface.rs create mode 100644 native/src/widget.rs create mode 100644 native/src/widget/button.rs create mode 100644 native/src/widget/checkbox.rs create mode 100644 native/src/widget/column.rs create mode 100644 native/src/widget/image.rs create mode 100644 native/src/widget/radio.rs create mode 100644 native/src/widget/row.rs create mode 100644 native/src/widget/slider.rs create mode 100644 native/src/widget/text.rs delete mode 100644 src/color.rs delete mode 100644 src/element.rs delete mode 100644 src/event.rs delete mode 100644 src/hasher.rs delete mode 100644 src/input.rs delete mode 100644 src/input/button_state.rs delete mode 100644 src/input/keyboard.rs delete mode 100644 src/input/keyboard/event.rs delete mode 100644 src/input/keyboard/key_code.rs delete mode 100644 src/input/mouse.rs delete mode 100644 src/input/mouse/button.rs delete mode 100644 src/input/mouse/event.rs delete mode 100644 src/layout.rs delete mode 100644 src/mouse_cursor.rs delete mode 100644 src/node.rs delete mode 100644 src/point.rs delete mode 100644 src/rectangle.rs delete mode 100644 src/renderer.rs delete mode 100644 src/style.rs delete mode 100644 src/user_interface.rs delete mode 100644 src/vector.rs delete mode 100644 src/widget.rs delete mode 100644 src/widget/button.rs delete mode 100644 src/widget/checkbox.rs delete mode 100644 src/widget/column.rs delete mode 100644 src/widget/image.rs delete mode 100644 src/widget/panel.rs delete mode 100644 src/widget/progress_bar.rs delete mode 100644 src/widget/radio.rs delete mode 100644 src/widget/row.rs delete mode 100644 src/widget/slider.rs delete mode 100644 src/widget/text.rs diff --git a/Cargo.toml b/Cargo.toml index 51c7305a..0975aeeb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ name = "iced" version = "0.1.0-alpha" authors = ["Héctor Ramón Jiménez "] edition = "2018" -description = "A GUI runtime, heavily inspired by Elm." +description = "A cross-platform GUI library inspired by Elm" license = "MIT" repository = "https://github.com/hecrj/iced" documentation = "https://docs.rs/iced" @@ -14,18 +14,10 @@ categories = ["gui"] [badges] maintenance = { status = "actively-developed" } -[package.metadata.docs.rs] -features = ["winit"] - -[dependencies] -stretch = "0.2" -twox-hash = "1.5" - -# Enable to obtain conversion traits -winit = { version = "0.20.0-alpha3", optional = true } - [workspace] members = [ + "core", + "native", "web", "examples/tour", ] diff --git a/core/Cargo.toml b/core/Cargo.toml new file mode 100644 index 00000000..40a8bcfb --- /dev/null +++ b/core/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "iced_core" +version = "0.1.0-alpha" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +description = "The essential concepts of Iced" +license = "MIT" +repository = "https://github.com/hecrj/iced" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +stretch = { version = "0.2", optional = true } diff --git a/core/src/align.rs b/core/src/align.rs new file mode 100644 index 00000000..5876e0f8 --- /dev/null +++ b/core/src/align.rs @@ -0,0 +1,47 @@ +/// Alignment on the cross axis of a container. +/// +/// * On a [`Column`], it describes __horizontal__ alignment. +/// * On a [`Row`], it describes __vertical__ alignment. +/// +/// [`Column`]: widget/struct.Column.html +/// [`Row`]: widget/struct.Row.html +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Align { + /// Align at the start of the cross axis. + Start, + + /// Align at the center of the cross axis. + Center, + + /// Align at the end of the cross axis. + End, + + /// Stretch over the cross axis. + Stretch, +} + +#[cfg(feature = "stretch")] +#[doc(hidden)] +impl From for stretch::style::AlignItems { + fn from(align: Align) -> Self { + match align { + Align::Start => stretch::style::AlignItems::FlexStart, + Align::Center => stretch::style::AlignItems::Center, + Align::End => stretch::style::AlignItems::FlexEnd, + Align::Stretch => stretch::style::AlignItems::Stretch, + } + } +} + +#[cfg(feature = "stretch")] +#[doc(hidden)] +impl From for stretch::style::AlignSelf { + fn from(align: Align) -> Self { + match align { + Align::Start => stretch::style::AlignSelf::FlexStart, + Align::Center => stretch::style::AlignSelf::Center, + Align::End => stretch::style::AlignSelf::FlexEnd, + Align::Stretch => stretch::style::AlignSelf::Stretch, + } + } +} diff --git a/core/src/color.rs b/core/src/color.rs new file mode 100644 index 00000000..5cc3a084 --- /dev/null +++ b/core/src/color.rs @@ -0,0 +1,19 @@ +/// A color in the sRGB color space. +#[derive(Debug, Clone, Copy, PartialEq)] +#[allow(missing_docs)] +pub struct Color { + pub r: f32, + pub g: f32, + pub b: f32, + pub a: f32, +} + +impl Color { + /// The black color. + pub const BLACK: Color = Color { + r: 0.0, + g: 0.0, + b: 0.0, + a: 1.0, + }; +} diff --git a/core/src/justify.rs b/core/src/justify.rs new file mode 100644 index 00000000..878573b0 --- /dev/null +++ b/core/src/justify.rs @@ -0,0 +1,44 @@ +/// Distribution on the main axis of a container. +/// +/// * On a [`Column`], it describes __vertical__ distribution. +/// * On a [`Row`], it describes __horizontal__ distribution. +/// +/// [`Column`]: widget/struct.Column.html +/// [`Row`]: widget/struct.Row.html +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum Justify { + /// Place items at the start of the main axis. + Start, + + /// Place items at the center of the main axis. + Center, + + /// Place items at the end of the main axis. + End, + + /// Place items with space between. + SpaceBetween, + + /// Place items with space around. + SpaceAround, + + /// Place items with evenly distributed space. + SpaceEvenly, +} + +#[cfg(feature = "stretch")] +#[doc(hidden)] +impl From for stretch::style::JustifyContent { + fn from(justify: Justify) -> Self { + match justify { + Justify::Start => stretch::style::JustifyContent::FlexStart, + Justify::Center => stretch::style::JustifyContent::Center, + Justify::End => stretch::style::JustifyContent::FlexEnd, + Justify::SpaceBetween => { + stretch::style::JustifyContent::SpaceBetween + } + Justify::SpaceAround => stretch::style::JustifyContent::SpaceAround, + Justify::SpaceEvenly => stretch::style::JustifyContent::SpaceEvenly, + } + } +} diff --git a/core/src/length.rs b/core/src/length.rs new file mode 100644 index 00000000..3547b246 --- /dev/null +++ b/core/src/length.rs @@ -0,0 +1,6 @@ +#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] +pub enum Length { + Fill, + Shrink, + Units(u16), +} diff --git a/core/src/lib.rs b/core/src/lib.rs new file mode 100644 index 00000000..1f43b2b7 --- /dev/null +++ b/core/src/lib.rs @@ -0,0 +1,18 @@ +pub mod widget; + +mod align; +mod color; +mod justify; +mod length; +mod point; +mod rectangle; +mod vector; + +pub use align::Align; +pub use color::Color; +pub use justify::Justify; +pub use length::Length; +pub use point::Point; +pub use rectangle::Rectangle; +pub use vector::Vector; +pub use widget::*; diff --git a/core/src/point.rs b/core/src/point.rs new file mode 100644 index 00000000..183998dd --- /dev/null +++ b/core/src/point.rs @@ -0,0 +1,31 @@ +use crate::Vector; + +/// A 2D point. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Point { + /// The X coordinate. + pub x: f32, + + /// The Y coordinate. + pub y: f32, +} + +impl Point { + /// Creates a new [`Point`] with the given coordinates. + /// + /// [`Point`]: struct.Point.html + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} + +impl std::ops::Add for Point { + type Output = Self; + + fn add(self, vector: Vector) -> Self { + Self { + x: self.x + vector.x, + y: self.y + vector.y, + } + } +} diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs new file mode 100644 index 00000000..95c2570c --- /dev/null +++ b/core/src/rectangle.rs @@ -0,0 +1,30 @@ +use crate::Point; + +/// A rectangle. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Rectangle { + /// X coordinate of the top-left corner. + pub x: T, + + /// Y coordinate of the top-left corner. + pub y: T, + + /// Width of the rectangle. + pub width: T, + + /// Height of the rectangle. + pub height: T, +} + +impl Rectangle { + /// Returns true if the given [`Point`] is contained in the [`Rectangle`]. + /// + /// [`Point`]: struct.Point.html + /// [`Rectangle`]: struct.Rectangle.html + pub fn contains(&self, point: Point) -> bool { + self.x <= point.x + && point.x <= self.x + self.width + && self.y <= point.y + && point.y <= self.y + self.height + } +} diff --git a/core/src/vector.rs b/core/src/vector.rs new file mode 100644 index 00000000..f45daab9 --- /dev/null +++ b/core/src/vector.rs @@ -0,0 +1,15 @@ +/// A 2D vector. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct Vector { + pub x: f32, + pub y: f32, +} + +impl Vector { + /// Creates a new [`Vector`] with the given components. + /// + /// [`Vector`]: struct.Vector.html + pub fn new(x: f32, y: f32) -> Self { + Self { x, y } + } +} diff --git a/core/src/widget.rs b/core/src/widget.rs new file mode 100644 index 00000000..4700be0e --- /dev/null +++ b/core/src/widget.rs @@ -0,0 +1,27 @@ +//! Use the essential widgets. +//! +//! # Re-exports +//! For convenience, the contents of this module are available at the root +//! module. Therefore, you can directly type: +//! +//! ``` +//! use iced_core::{button, Button}; +//! ``` +mod column; +mod row; + +pub mod button; +pub mod checkbox; +pub mod image; +pub mod radio; +pub mod slider; +pub mod text; + +pub use button::Button; +pub use checkbox::Checkbox; +pub use column::Column; +pub use image::Image; +pub use radio::Radio; +pub use row::Row; +pub use slider::Slider; +pub use text::Text; diff --git a/core/src/widget/button.rs b/core/src/widget/button.rs new file mode 100644 index 00000000..b98bb443 --- /dev/null +++ b/core/src/widget/button.rs @@ -0,0 +1,158 @@ +//! Allow your users to perform actions by pressing a button. +//! +//! A [`Button`] has some local [`State`]. +//! +//! [`Button`]: struct.Button.html +//! [`State`]: struct.State.html + +use crate::{Align, Length}; + +/// A generic widget that produces a message when clicked. +/// +/// # Example +/// +/// ``` +/// use iced_core::{button, Button}; +/// +/// pub enum Message { +/// ButtonClicked, +/// } +/// +/// let state = &mut button::State::new(); +/// +/// Button::new(state, "Click me!") +/// .on_press(Message::ButtonClicked); +/// ``` +/// +/// ![Button drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/button.png?raw=true) +pub struct Button<'a, Message> { + /// The current state of the button + pub state: &'a mut State, + + /// The label of the button + pub label: String, + + /// The message to produce when the button is pressed + pub on_press: Option, + + pub class: Class, + + pub width: Length, + + pub align_self: Option, +} + +impl<'a, Message> std::fmt::Debug for Button<'a, Message> +where + Message: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Button") + .field("state", &self.state) + .field("label", &self.label) + .field("on_press", &self.on_press) + .finish() + } +} + +impl<'a, Message> Button<'a, Message> { + /// Creates a new [`Button`] with some local [`State`] and the given label. + /// + /// [`Button`]: struct.Button.html + /// [`State`]: struct.State.html + pub fn new(state: &'a mut State, label: &str) -> Self { + Button { + state, + label: String::from(label), + on_press: None, + class: Class::Primary, + width: Length::Shrink, + align_self: None, + } + } + + /// Sets the width of the [`Button`]. + /// + /// [`Button`]: struct.Button.html + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } + + /// Sets the alignment of the [`Button`] itself. + /// + /// This is useful if you want to override the default alignment given by + /// the parent container. + /// + /// [`Button`]: struct.Button.html + pub fn align_self(mut self, align: Align) -> Self { + self.align_self = Some(align); + self + } + + /// Sets the [`Class`] of the [`Button`]. + /// + /// + /// [`Button`]: struct.Button.html + /// [`Class`]: enum.Class.html + pub fn class(mut self, class: Class) -> Self { + self.class = class; + self + } + + /// Sets the message that will be produced when the [`Button`] is pressed. + /// + /// [`Button`]: struct.Button.html + pub fn on_press(mut self, msg: Message) -> Self { + self.on_press = Some(msg); + self + } +} + +/// The local state of a [`Button`]. +/// +/// [`Button`]: struct.Button.html +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct State { + pub is_pressed: bool, +} + +impl State { + /// Creates a new [`State`]. + /// + /// [`State`]: struct.State.html + pub fn new() -> State { + State::default() + } + + /// Returns whether the associated [`Button`] is currently being pressed or + /// not. + /// + /// [`Button`]: struct.Button.html + pub fn is_pressed(&self) -> bool { + self.is_pressed + } +} + +/// The type of a [`Button`]. +/// +/// ![Different buttons drawn by the built-in renderer in Coffee](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/button_classes.png?raw=true) +/// +/// [`Button`]: struct.Button.html +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Class { + /// The [`Button`] performs the main action. + /// + /// [`Button`]: struct.Button.html + Primary, + + /// The [`Button`] performs an alternative action. + /// + /// [`Button`]: struct.Button.html + Secondary, + + /// The [`Button`] performs a productive action. + /// + /// [`Button`]: struct.Button.html + Positive, +} diff --git a/core/src/widget/checkbox.rs b/core/src/widget/checkbox.rs new file mode 100644 index 00000000..1f0a0c04 --- /dev/null +++ b/core/src/widget/checkbox.rs @@ -0,0 +1,78 @@ +//! Show toggle controls using checkboxes. +use crate::Color; + +/// A box that can be checked. +/// +/// # Example +/// +/// ``` +/// use iced_core::Checkbox; +/// +/// pub enum Message { +/// CheckboxToggled(bool), +/// } +/// +/// let is_checked = true; +/// +/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled); +/// ``` +/// +/// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true) +pub struct Checkbox { + /// Whether the checkbox is checked or not + pub is_checked: bool, + + /// Function to call when checkbox is toggled to produce a __message__. + /// + /// The function should be provided `true` when the checkbox is checked + /// and `false` otherwise. + pub on_toggle: Box Message>, + + /// The label of the checkbox + pub label: String, + + /// The color of the label + pub label_color: Option, +} + +impl std::fmt::Debug for Checkbox { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Checkbox") + .field("is_checked", &self.is_checked) + .field("label", &self.label) + .field("label_color", &self.label_color) + .finish() + } +} + +impl Checkbox { + /// Creates a new [`Checkbox`]. + /// + /// It expects: + /// * a boolean describing whether the [`Checkbox`] is checked or not + /// * the label of the [`Checkbox`] + /// * a function that will be called when the [`Checkbox`] is toggled. + /// It will receive the new state of the [`Checkbox`] and must produce + /// a `Message`. + /// + /// [`Checkbox`]: struct.Checkbox.html + pub fn new(is_checked: bool, label: &str, f: F) -> Self + where + F: 'static + Fn(bool) -> Message, + { + Checkbox { + is_checked, + on_toggle: Box::new(f), + label: String::from(label), + label_color: None, + } + } + + /// Sets the color of the label of the [`Checkbox`]. + /// + /// [`Checkbox`]: struct.Checkbox.html + pub fn label_color>(mut self, color: C) -> Self { + self.label_color = Some(color.into()); + self + } +} diff --git a/core/src/widget/column.rs b/core/src/widget/column.rs new file mode 100644 index 00000000..2df327a0 --- /dev/null +++ b/core/src/widget/column.rs @@ -0,0 +1,147 @@ +use crate::{Align, Justify, Length}; + +/// A container that distributes its contents vertically. +/// +/// A [`Column`] will try to fill the horizontal space of its container. +/// +/// [`Column`]: struct.Column.html +pub struct Column { + pub spacing: u16, + pub padding: u16, + pub width: Length, + pub height: Length, + pub max_width: Length, + pub max_height: Length, + pub align_self: Option, + pub align_items: Align, + pub justify_content: Justify, + pub children: Vec, +} + +impl Column { + /// Creates an empty [`Column`]. + /// + /// [`Column`]: struct.Column.html + pub fn new() -> Self { + Column { + spacing: 0, + padding: 0, + width: Length::Fill, + height: Length::Shrink, + max_width: Length::Shrink, + max_height: Length::Shrink, + align_self: None, + align_items: Align::Start, + justify_content: Justify::Start, + children: Vec::new(), + } + } + + /// Sets the vertical spacing _between_ elements. + /// + /// Custom margins per element do not exist in Iced. You should use this + /// method instead! While less flexible, it helps you keep spacing between + /// elements consistent. + pub fn spacing(mut self, units: u16) -> Self { + self.spacing = units; + self + } + + /// Sets the padding of the [`Column`]. + /// + /// [`Column`]: struct.Column.html + pub fn padding(mut self, units: u16) -> Self { + self.padding = units; + self + } + + /// Sets the width of the [`Column`]. + /// + /// [`Column`]: struct.Column.html + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } + + /// Sets the height of the [`Column`]. + /// + /// [`Column`]: struct.Column.html + pub fn height(mut self, height: Length) -> Self { + self.height = height; + self + } + + /// Sets the maximum width of the [`Column`]. + /// + /// [`Column`]: struct.Column.html + pub fn max_width(mut self, max_width: Length) -> Self { + self.max_width = max_width; + self + } + + /// Sets the maximum height of the [`Column`] in pixels. + /// + /// [`Column`]: struct.Column.html + pub fn max_height(mut self, max_height: Length) -> Self { + self.max_height = max_height; + self + } + + /// Sets the alignment of the [`Column`] itself. + /// + /// This is useful if you want to override the default alignment given by + /// the parent container. + /// + /// [`Column`]: struct.Column.html + pub fn align_self(mut self, align: Align) -> Self { + self.align_self = Some(align); + self + } + + /// Sets the horizontal alignment of the contents of the [`Column`] . + /// + /// [`Column`]: struct.Column.html + pub fn align_items(mut self, align: Align) -> Self { + self.align_items = align; + self + } + + /// Sets the vertical distribution strategy for the contents of the + /// [`Column`] . + /// + /// [`Column`]: struct.Column.html + pub fn justify_content(mut self, justify: Justify) -> Self { + self.justify_content = justify; + self + } + + /// Adds an element to the [`Column`]. + /// + /// [`Column`]: struct.Column.html + pub fn push(mut self, child: E) -> Column + where + E: Into, + { + self.children.push(child.into()); + self + } +} + +impl Default for Column { + fn default() -> Self { + Self::new() + } +} + +impl std::fmt::Debug for Column +where + Element: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // TODO: Complete once stabilized + f.debug_struct("Column") + .field("spacing", &self.spacing) + .field("children", &self.children) + .finish() + } +} diff --git a/core/src/widget/image.rs b/core/src/widget/image.rs new file mode 100644 index 00000000..110ba99a --- /dev/null +++ b/core/src/widget/image.rs @@ -0,0 +1,89 @@ +//! Display images in your user interface. + +use crate::{Align, Length, Rectangle}; + +/// A frame that displays an image while keeping aspect ratio. +/// +/// # Example +/// +/// ``` +/// use iced_core::Image; +/// +/// # let my_handle = String::from("some_handle"); +/// let image = Image::new(my_handle); +/// ``` +pub struct Image { + /// The image handle + pub handle: I, + + /// The part of the image to show + pub clip: Option>, + + /// The width of the image + pub width: Length, + + /// The height of the image + pub height: Length, + + pub align_self: Option, +} + +impl std::fmt::Debug for Image { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Image") + .field("clip", &self.clip) + .field("width", &self.width) + .field("height", &self.height) + .finish() + } +} + +impl Image { + /// Creates a new [`Image`] with given image handle. + /// + /// [`Image`]: struct.Image.html + pub fn new(handle: I) -> Self { + Image { + handle, + clip: None, + width: Length::Shrink, + height: Length::Shrink, + align_self: None, + } + } + + /// Sets the portion of the [`Image`] to draw. + /// + /// [`Image`]: struct.Image.html + pub fn clip(mut self, clip: Rectangle) -> Self { + self.clip = Some(clip); + self + } + + /// Sets the width of the [`Image`] boundaries. + /// + /// [`Image`]: struct.Image.html + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } + + /// Sets the height of the [`Image`] boundaries. + /// + /// [`Image`]: struct.Image.html + pub fn height(mut self, height: Length) -> Self { + self.height = height; + self + } + + /// Sets the alignment of the [`Image`] itself. + /// + /// This is useful if you want to override the default alignment given by + /// the parent container. + /// + /// [`Image`]: struct.Image.html + pub fn align_self(mut self, align: Align) -> Self { + self.align_self = Some(align); + self + } +} diff --git a/core/src/widget/radio.rs b/core/src/widget/radio.rs new file mode 100644 index 00000000..9765e928 --- /dev/null +++ b/core/src/widget/radio.rs @@ -0,0 +1,88 @@ +//! Create choices using radio buttons. +use crate::Color; + +/// A circular button representing a choice. +/// +/// # Example +/// ``` +/// use iced_core::Radio; +/// +/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] +/// pub enum Choice { +/// A, +/// B, +/// } +/// +/// #[derive(Debug, Clone, Copy)] +/// pub enum Message { +/// RadioSelected(Choice), +/// } +/// +/// let selected_choice = Some(Choice::A); +/// +/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected); +/// +/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected); +/// ``` +/// +/// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true) +pub struct Radio { + /// Whether the radio button is selected or not + pub is_selected: bool, + + /// The message to produce when the radio button is clicked + pub on_click: Message, + + /// The label of the radio button + pub label: String, + + /// The color of the label + pub label_color: Option, +} + +impl std::fmt::Debug for Radio +where + Message: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Radio") + .field("is_selected", &self.is_selected) + .field("on_click", &self.on_click) + .field("label", &self.label) + .field("label_color", &self.label_color) + .finish() + } +} + +impl Radio { + /// Creates a new [`Radio`] button. + /// + /// It expects: + /// * the value related to the [`Radio`] button + /// * the label of the [`Radio`] button + /// * the current selected value + /// * a function that will be called when the [`Radio`] is selected. It + /// receives the value of the radio and must produce a `Message`. + /// + /// [`Radio`]: struct.Radio.html + pub fn new(value: V, label: &str, selected: Option, f: F) -> Self + where + V: Eq + Copy, + F: 'static + Fn(V) -> Message, + { + Radio { + is_selected: Some(value) == selected, + on_click: f(value), + label: String::from(label), + label_color: None, + } + } + + /// Sets the `Color` of the label of the [`Radio`]. + /// + /// [`Radio`]: struct.Radio.html + pub fn label_color>(mut self, color: C) -> Self { + self.label_color = Some(color.into()); + self + } +} diff --git a/core/src/widget/row.rs b/core/src/widget/row.rs new file mode 100644 index 00000000..6bdb4ed2 --- /dev/null +++ b/core/src/widget/row.rs @@ -0,0 +1,142 @@ +use crate::{Align, Justify, Length}; + +/// A container that distributes its contents horizontally. +/// +/// A [`Row`] will try to fill the horizontal space of its container. +/// +/// [`Row`]: struct.Row.html +pub struct Row { + pub spacing: u16, + pub padding: u16, + pub width: Length, + pub height: Length, + pub max_width: Length, + pub max_height: Length, + pub align_self: Option, + pub align_items: Align, + pub justify_content: Justify, + pub children: Vec, +} + +impl Row { + /// Creates an empty [`Row`]. + /// + /// [`Row`]: struct.Row.html + pub fn new() -> Self { + Row { + spacing: 0, + padding: 0, + width: Length::Fill, + height: Length::Shrink, + max_width: Length::Shrink, + max_height: Length::Shrink, + align_self: None, + align_items: Align::Start, + justify_content: Justify::Start, + children: Vec::new(), + } + } + + /// Sets the horizontal spacing _between_ elements. + /// + /// Custom margins per element do not exist in Iced. You should use this + /// method instead! While less flexible, it helps you keep spacing between + /// elements consistent. + pub fn spacing(mut self, units: u16) -> Self { + self.spacing = units; + self + } + + /// Sets the padding of the [`Row`]. + /// + /// [`Row`]: struct.Row.html + pub fn padding(mut self, units: u16) -> Self { + self.padding = units; + self + } + + /// Sets the width of the [`Row`]. + /// + /// [`Row`]: struct.Row.html + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } + + /// Sets the height of the [`Row`]. + /// + /// [`Row`]: struct.Row.html + pub fn height(mut self, height: Length) -> Self { + self.height = height; + self + } + + /// Sets the maximum width of the [`Row`]. + /// + /// [`Row`]: struct.Row.html + pub fn max_width(mut self, max_width: Length) -> Self { + self.max_width = max_width; + self + } + + /// Sets the maximum height of the [`Row`]. + /// + /// [`Row`]: struct.Row.html + pub fn max_height(mut self, max_height: Length) -> Self { + self.max_height = max_height; + self + } + + /// Sets the alignment of the [`Row`] itself. + /// + /// This is useful if you want to override the default alignment given by + /// the parent container. + /// + /// [`Row`]: struct.Row.html + pub fn align_self(mut self, align: Align) -> Self { + self.align_self = Some(align); + self + } + + /// Sets the vertical alignment of the contents of the [`Row`] . + /// + /// [`Row`]: struct.Row.html + pub fn align_items(mut self, align: Align) -> Self { + self.align_items = align; + self + } + + /// Sets the horizontal distribution strategy for the contents of the + /// [`Row`] . + /// + /// [`Row`]: struct.Row.html + pub fn justify_content(mut self, justify: Justify) -> Self { + self.justify_content = justify; + self + } + + /// Adds an [`Element`] to the [`Row`]. + /// + /// [`Element`]: ../struct.Element.html + /// [`Row`]: struct.Row.html + pub fn push(mut self, child: E) -> Row + where + E: Into, + { + self.children.push(child.into()); + self + } +} + +impl std::fmt::Debug for Row +where + Element: std::fmt::Debug, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + // TODO: Complete once stabilized + f.debug_struct("Row") + .field("spacing", &self.spacing) + .field("children", &self.children) + .finish() + } +} diff --git a/core/src/widget/slider.rs b/core/src/widget/slider.rs new file mode 100644 index 00000000..b65e3991 --- /dev/null +++ b/core/src/widget/slider.rs @@ -0,0 +1,123 @@ +//! Display an interactive selector of a single value from a range of values. +//! +//! A [`Slider`] has some local [`State`]. +//! +//! [`Slider`]: struct.Slider.html +//! [`State`]: struct.State.html +use crate::Length; + +use std::ops::RangeInclusive; +use std::rc::Rc; + +/// An horizontal bar and a handle that selects a single value from a range of +/// values. +/// +/// A [`Slider`] will try to fill the horizontal space of its container. +/// +/// [`Slider`]: struct.Slider.html +/// +/// # Example +/// ``` +/// use iced_core::{slider, Slider}; +/// +/// pub enum Message { +/// SliderChanged(f32), +/// } +/// +/// let state = &mut slider::State::new(); +/// let value = 50.0; +/// +/// Slider::new(state, 0.0..=100.0, value, Message::SliderChanged); +/// ``` +/// +/// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true) +pub struct Slider<'a, Message> { + /// The state of the slider + pub state: &'a mut State, + + /// The range of the slider + pub range: RangeInclusive, + + /// The current value of the slider + pub value: f32, + + /// The function to produce messages on change + pub on_change: Rc Message>>, + + pub width: Length, +} + +impl<'a, Message> std::fmt::Debug for Slider<'a, Message> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Slider") + .field("state", &self.state) + .field("range", &self.range) + .field("value", &self.value) + .finish() + } +} + +impl<'a, Message> Slider<'a, Message> { + /// Creates a new [`Slider`]. + /// + /// It expects: + /// * the local [`State`] of the [`Slider`] + /// * an inclusive range of possible values + /// * the current value of the [`Slider`] + /// * a function that will be called when the [`Slider`] is dragged. + /// It receives the new value of the [`Slider`] and must produce a + /// `Message`. + /// + /// [`Slider`]: struct.Slider.html + /// [`State`]: struct.State.html + pub fn new( + state: &'a mut State, + range: RangeInclusive, + value: f32, + on_change: F, + ) -> Self + where + F: 'static + Fn(f32) -> Message, + { + Slider { + state, + value: value.max(*range.start()).min(*range.end()), + range, + on_change: Rc::new(Box::new(on_change)), + width: Length::Fill, + } + } + + /// Sets the width of the [`Slider`]. + /// + /// [`Slider`]: struct.Slider.html + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } +} + +/// The local state of a [`Slider`]. +/// +/// [`Slider`]: struct.Slider.html +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub struct State { + pub is_dragging: bool, +} + +impl State { + /// Creates a new [`State`]. + /// + /// [`State`]: struct.State.html + pub fn new() -> State { + State::default() + } + + /// Returns whether the associated [`Slider`] is currently being dragged or + /// not. + /// + /// [`Slider`]: struct.Slider.html + pub fn is_dragging(&self) -> bool { + self.is_dragging + } +} diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs new file mode 100644 index 00000000..427d9471 --- /dev/null +++ b/core/src/widget/text.rs @@ -0,0 +1,119 @@ +//! Write some text for your users to read. +use crate::{Color, Length}; + +/// A paragraph of text. +/// +/// # Example +/// +/// ``` +/// use iced_core::Text; +/// +/// Text::new("I <3 iced!") +/// .size(40); +/// ``` +#[derive(Debug, Clone)] +pub struct Text { + pub content: String, + pub size: Option, + pub color: Option, + pub width: Length, + pub height: Length, + pub horizontal_alignment: HorizontalAlignment, + pub vertical_alignment: VerticalAlignment, +} + +impl Text { + /// Create a new fragment of [`Text`] with the given contents. + /// + /// [`Text`]: struct.Text.html + pub fn new(label: &str) -> Self { + Text { + content: String::from(label), + size: None, + color: None, + width: Length::Fill, + height: Length::Shrink, + horizontal_alignment: HorizontalAlignment::Left, + vertical_alignment: VerticalAlignment::Top, + } + } + + /// Sets the size of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + pub fn size(mut self, size: u16) -> Self { + self.size = Some(size); + self + } + + /// Sets the `Color` of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + pub fn color>(mut self, color: C) -> Self { + self.color = Some(color.into()); + self + } + + /// Sets the width of the [`Text`] boundaries. + /// + /// [`Text`]: struct.Text.html + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } + + /// Sets the height of the [`Text`] boundaries. + /// + /// [`Text`]: struct.Text.html + pub fn height(mut self, height: Length) -> Self { + self.height = height; + self + } + + /// Sets the [`HorizontalAlignment`] of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html + pub fn horizontal_alignment( + mut self, + alignment: HorizontalAlignment, + ) -> Self { + self.horizontal_alignment = alignment; + self + } + + /// Sets the [`VerticalAlignment`] of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + /// [`VerticalAlignment`]: enum.VerticalAlignment.html + pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self { + self.vertical_alignment = alignment; + self + } +} + +/// The horizontal alignment of some resource. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum HorizontalAlignment { + /// Align left + Left, + + /// Horizontally centered + Center, + + /// Align right + Right, +} + +/// The vertical alignment of some resource. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum VerticalAlignment { + /// Align top + Top, + + /// Vertically centered + Center, + + /// Align bottom + Bottom, +} diff --git a/examples/tour/Cargo.toml b/examples/tour/Cargo.toml index 08d108d9..a0e3eee8 100644 --- a/examples/tour/Cargo.toml +++ b/examples/tour/Cargo.toml @@ -16,7 +16,7 @@ path = "src/iced_ggez/main.rs" futures-preview = "=0.3.0-alpha.18" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -iced = { path = "../.." } +iced_native = { version = "0.1.0-alpha", path = "../../native" } # A personal `ggez` fork that introduces a `FontCache` type to measure text # efficiently and fixes HiDPI issues. ggez = { version = "0.5", git = "https://github.com/hecrj/ggez.git" } diff --git a/examples/tour/src/iced_ggez/main.rs b/examples/tour/src/iced_ggez/main.rs index a8cf09e5..72774d38 100644 --- a/examples/tour/src/iced_ggez/main.rs +++ b/examples/tour/src/iced_ggez/main.rs @@ -35,8 +35,8 @@ struct Game { images: iced_ggez::ImageCache, tour: Tour, - events: Vec, - cache: Option, + events: Vec, + cache: Option, } impl Game { @@ -52,7 +52,7 @@ impl Game { tour: Tour::new(), events: Vec::new(), - cache: Some(iced::Cache::default()), + cache: Some(iced_native::Cache::default()), }) } } @@ -69,10 +69,10 @@ impl event::EventHandler for Game { _x: f32, _y: f32, ) { - self.events.push(iced::Event::Mouse( - iced::input::mouse::Event::Input { - state: iced::input::ButtonState::Pressed, - button: iced::input::mouse::Button::Left, // TODO: Map `button` + self.events.push(iced_native::Event::Mouse( + iced_native::input::mouse::Event::Input { + state: iced_native::input::ButtonState::Pressed, + button: iced_native::input::mouse::Button::Left, // TODO: Map `button` }, )); } @@ -84,10 +84,10 @@ impl event::EventHandler for Game { _x: f32, _y: f32, ) { - self.events.push(iced::Event::Mouse( - iced::input::mouse::Event::Input { - state: iced::input::ButtonState::Released, - button: iced::input::mouse::Button::Left, // TODO: Map `button` + self.events.push(iced_native::Event::Mouse( + iced_native::input::mouse::Event::Input { + state: iced_native::input::ButtonState::Released, + button: iced_native::input::mouse::Button::Left, // TODO: Map `button` }, )); } @@ -100,8 +100,8 @@ impl event::EventHandler for Game { _dx: f32, _dy: f32, ) { - self.events.push(iced::Event::Mouse( - iced::input::mouse::Event::CursorMoved { x, y }, + self.events.push(iced_native::Event::Mouse( + iced_native::input::mouse::Event::CursorMoved { x, y }, )); } @@ -132,11 +132,11 @@ impl event::EventHandler for Game { let view = self.tour.view(); let content = iced_ggez::Column::new() - .width(screen.w as u16) - .height(screen.h as u16) + .width(iced_native::Length::Units(screen.w as u16)) + .height(iced_native::Length::Units(screen.h as u16)) .padding(20) - .align_items(iced::Align::Center) - .justify_content(iced::Justify::Center) + .align_items(iced_native::Align::Center) + .justify_content(iced_native::Justify::Center) .push(view); let renderer = &mut iced_ggez::Renderer::new( @@ -146,7 +146,7 @@ impl event::EventHandler for Game { self.font, ); - let mut ui = iced::UserInterface::build( + let mut ui = iced_native::UserInterface::build( content, self.cache.take().unwrap(), renderer, @@ -177,13 +177,13 @@ impl event::EventHandler for Game { } } -fn into_cursor_type(cursor: iced::MouseCursor) -> mouse::MouseCursor { +fn into_cursor_type(cursor: iced_native::MouseCursor) -> mouse::MouseCursor { match cursor { - iced::MouseCursor::OutOfBounds => mouse::MouseCursor::Default, - iced::MouseCursor::Idle => mouse::MouseCursor::Default, - iced::MouseCursor::Pointer => mouse::MouseCursor::Hand, - iced::MouseCursor::Working => mouse::MouseCursor::Progress, - iced::MouseCursor::Grab => mouse::MouseCursor::Grab, - iced::MouseCursor::Grabbing => mouse::MouseCursor::Grabbing, + iced_native::MouseCursor::OutOfBounds => mouse::MouseCursor::Default, + iced_native::MouseCursor::Idle => mouse::MouseCursor::Default, + iced_native::MouseCursor::Pointer => mouse::MouseCursor::Hand, + iced_native::MouseCursor::Working => mouse::MouseCursor::Progress, + iced_native::MouseCursor::Grab => mouse::MouseCursor::Grab, + iced_native::MouseCursor::Grabbing => mouse::MouseCursor::Grabbing, } } diff --git a/examples/tour/src/iced_ggez/renderer.rs b/examples/tour/src/iced_ggez/renderer.rs index e3181eaa..c0e6d559 100644 --- a/examples/tour/src/iced_ggez/renderer.rs +++ b/examples/tour/src/iced_ggez/renderer.rs @@ -67,7 +67,7 @@ impl<'a> Renderer<'a> { } } -pub fn into_color(color: iced::Color) -> graphics::Color { +pub fn into_color(color: iced_native::Color) -> graphics::Color { graphics::Color { r: color.r, g: color.g, diff --git a/examples/tour/src/iced_ggez/renderer/button.rs b/examples/tour/src/iced_ggez/renderer/button.rs index 486e07ed..78a5de07 100644 --- a/examples/tour/src/iced_ggez/renderer/button.rs +++ b/examples/tour/src/iced_ggez/renderer/button.rs @@ -2,7 +2,7 @@ use super::Renderer; use ggez::graphics::{ self, Align, Color, DrawParam, Rect, Scale, Text, TextFragment, WHITE, }; -use iced::{button, MouseCursor}; +use iced_native::{button, Button, Layout, Length, MouseCursor, Node, Style}; const LEFT: Rect = Rect { x: 0.0, @@ -26,20 +26,29 @@ const RIGHT: Rect = Rect { }; impl button::Renderer for Renderer<'_> { - fn draw( + fn node(&self, button: &Button<'_, Message>) -> Node { + let style = Style::default() + .width(button.width) + .height(Length::Units(LEFT.h as u16)) + .min_width(Length::Units(100)) + .align_self(button.align_self); + + Node::new(style) + } + + fn draw( &mut self, - cursor_position: iced::Point, - mut bounds: iced::Rectangle, - state: &button::State, - label: &str, - class: button::Class, + button: &Button<'_, Message>, + layout: Layout<'_>, + cursor_position: iced_native::Point, ) -> MouseCursor { + let mut bounds = layout.bounds(); let mouse_over = bounds.contains(cursor_position); let mut state_offset = 0.0; if mouse_over { - if state.is_pressed() { + if button.state.is_pressed() { bounds.y += 4.0; state_offset = RIGHT.x + RIGHT.w; } else { @@ -47,7 +56,7 @@ impl button::Renderer for Renderer<'_> { } } - let class_index = match class { + let class_index = match button.class { button::Class::Primary => 0, button::Class::Secondary => 1, button::Class::Positive => 2, @@ -103,7 +112,7 @@ impl button::Renderer for Renderer<'_> { }); let mut text = Text::new(TextFragment { - text: String::from(label), + text: button.label.clone(), font: Some(self.font), scale: Some(Scale { x: 20.0, y: 20.0 }), ..Default::default() diff --git a/examples/tour/src/iced_ggez/renderer/checkbox.rs b/examples/tour/src/iced_ggez/renderer/checkbox.rs index 20a91be5..807185d9 100644 --- a/examples/tour/src/iced_ggez/renderer/checkbox.rs +++ b/examples/tour/src/iced_ggez/renderer/checkbox.rs @@ -1,7 +1,10 @@ use super::Renderer; use ggez::graphics::{DrawParam, Rect}; -use iced::{checkbox, MouseCursor}; +use iced_native::{ + checkbox, text, Align, Checkbox, Column, Layout, Length, MouseCursor, Node, + Row, Text, Widget, +}; const SPRITE: Rect = Rect { x: 98.0, @@ -10,14 +13,41 @@ const SPRITE: Rect = Rect { h: 28.0, }; -impl checkbox::Renderer for Renderer<'_> { - fn draw( +impl checkbox::Renderer for Renderer<'_> +where + Self: text::Renderer, +{ + fn node(&mut self, checkbox: &Checkbox) -> Node { + Row::<(), Self>::new() + .spacing(15) + .align_items(Align::Center) + .push( + Column::new() + .width(Length::Units(SPRITE.w as u16)) + .height(Length::Units(SPRITE.h as u16)), + ) + .push(Text::new(&checkbox.label)) + .node(self) + } + + fn draw( &mut self, - cursor_position: iced::Point, - bounds: iced::Rectangle, - text_bounds: iced::Rectangle, - is_checked: bool, + checkbox: &Checkbox, + layout: Layout<'_>, + cursor_position: iced_native::Point, ) -> MouseCursor { + let bounds = layout.bounds(); + let children: Vec<_> = layout.children().collect(); + let text_bounds = children[1].bounds(); + + let mut text = Text::new(&checkbox.label); + + if let Some(label_color) = checkbox.label_color { + text = text.color(label_color); + } + + text::Renderer::draw(self, &text, children[1]); + let mouse_over = bounds.contains(cursor_position) || text_bounds.contains(cursor_position); @@ -39,7 +69,7 @@ impl checkbox::Renderer for Renderer<'_> { ..DrawParam::default() }); - if is_checked { + if checkbox.is_checked { self.sprites.add(DrawParam { src: Rect { x: (SPRITE.x + SPRITE.w * 2.0) / width, diff --git a/examples/tour/src/iced_ggez/renderer/debugger.rs b/examples/tour/src/iced_ggez/renderer/debugger.rs index eba03ebf..ffb658af 100644 --- a/examples/tour/src/iced_ggez/renderer/debugger.rs +++ b/examples/tour/src/iced_ggez/renderer/debugger.rs @@ -1,8 +1,12 @@ use super::{into_color, Renderer}; use ggez::graphics::{DrawMode, MeshBuilder, Rect}; -impl iced::renderer::Debugger for Renderer<'_> { - fn explain(&mut self, layout: &iced::Layout<'_>, color: iced::Color) { +impl iced_native::renderer::Debugger for Renderer<'_> { + fn explain( + &mut self, + layout: &iced_native::Layout<'_>, + color: iced_native::Color, + ) { let bounds = layout.bounds(); let mut debug_mesh = diff --git a/examples/tour/src/iced_ggez/renderer/image.rs b/examples/tour/src/iced_ggez/renderer/image.rs index 17c6a56e..b12b65c3 100644 --- a/examples/tour/src/iced_ggez/renderer/image.rs +++ b/examples/tour/src/iced_ggez/renderer/image.rs @@ -1,7 +1,7 @@ use super::Renderer; use ggez::{graphics, nalgebra}; -use iced::image; +use iced_native::{image, Image, Layout, Length, Style}; pub struct Cache { images: std::collections::HashMap, @@ -35,39 +35,29 @@ impl Cache { } impl<'a> image::Renderer<&'a str> for Renderer<'_> { - fn node( - &mut self, - style: iced::Style, - name: &&'a str, - width: Option, - height: Option, - _source: Option>, - ) -> iced::Node { - let image = self.images.get(name, self.context); + fn node(&mut self, image: &Image<&'a str>) -> iced_native::Node { + let ggez_image = self.images.get(image.handle, self.context); + + let aspect_ratio = + ggez_image.width() as f32 / ggez_image.height() as f32; - let aspect_ratio = image.width() as f32 / image.height() as f32; + let mut style = Style::default().align_self(image.align_self); - let style = match (width, height) { - (Some(width), Some(height)) => style.width(width).height(height), - (Some(width), None) => style - .width(width) - .height((width as f32 / aspect_ratio).round() as u16), - (None, Some(height)) => style - .height(height) - .width((height as f32 * aspect_ratio).round() as u16), - (None, None) => style.width(image.width()).height(image.height()), + style = match (image.width, image.height) { + (Length::Units(width), _) => style.width(image.width).height( + Length::Units((width as f32 / aspect_ratio).round() as u16), + ), + (_, _) => style + .width(Length::Units(ggez_image.width())) + .height(Length::Units(ggez_image.height())), }; - iced::Node::new(style) + iced_native::Node::new(style) } - fn draw( - &mut self, - name: &&'a str, - bounds: iced::Rectangle, - _source: Option>, - ) { - let image = self.images.get(name, self.context); + fn draw(&mut self, image: &Image<&'a str>, layout: Layout<'_>) { + let image = self.images.get(image.handle, self.context); + let bounds = layout.bounds(); // We should probably use batches to draw images efficiently and keep // draw side-effect free, but this is good enough for the example. diff --git a/examples/tour/src/iced_ggez/renderer/radio.rs b/examples/tour/src/iced_ggez/renderer/radio.rs index 0f7815d6..dbd29ecd 100644 --- a/examples/tour/src/iced_ggez/renderer/radio.rs +++ b/examples/tour/src/iced_ggez/renderer/radio.rs @@ -1,7 +1,10 @@ use super::Renderer; use ggez::graphics::{DrawParam, Rect}; -use iced::{radio, MouseCursor, Point, Rectangle}; +use iced_native::{ + radio, text, Align, Column, Layout, Length, MouseCursor, Node, Point, + Radio, Row, Text, Widget, +}; const SPRITE: Rect = Rect { x: 98.0, @@ -10,15 +13,41 @@ const SPRITE: Rect = Rect { h: 28.0, }; -impl radio::Renderer for Renderer<'_> { - fn draw( +impl radio::Renderer for Renderer<'_> +where + Self: text::Renderer, +{ + fn node(&mut self, radio: &Radio) -> Node { + Row::<(), Self>::new() + .spacing(15) + .align_items(Align::Center) + .push( + Column::new() + .width(Length::Units(SPRITE.w as u16)) + .height(Length::Units(SPRITE.h as u16)), + ) + .push(Text::new(&radio.label)) + .node(self) + } + + fn draw( &mut self, + radio: &Radio, + layout: Layout<'_>, cursor_position: Point, - bounds: Rectangle, - bounds_with_label: Rectangle, - is_selected: bool, ) -> MouseCursor { - let mouse_over = bounds_with_label.contains(cursor_position); + let children: Vec<_> = layout.children().collect(); + + let mut text = Text::new(&radio.label); + + if let Some(label_color) = radio.label_color { + text = text.color(label_color); + } + + text::Renderer::draw(self, &text, children[1]); + + let bounds = layout.bounds(); + let mouse_over = bounds.contains(cursor_position); let width = self.spritesheet.width() as f32; let height = self.spritesheet.height() as f32; @@ -38,7 +67,7 @@ impl radio::Renderer for Renderer<'_> { ..DrawParam::default() }); - if is_selected { + if radio.is_selected { self.sprites.add(DrawParam { src: Rect { x: (SPRITE.x + SPRITE.w * 2.0) / width, diff --git a/examples/tour/src/iced_ggez/renderer/slider.rs b/examples/tour/src/iced_ggez/renderer/slider.rs index 146cee18..60c40c55 100644 --- a/examples/tour/src/iced_ggez/renderer/slider.rs +++ b/examples/tour/src/iced_ggez/renderer/slider.rs @@ -1,8 +1,9 @@ use super::Renderer; use ggez::graphics::{DrawParam, Rect}; -use iced::{slider, MouseCursor, Point, Rectangle}; -use std::ops::RangeInclusive; +use iced_native::{ + slider, Layout, Length, MouseCursor, Node, Point, Slider, Style, +}; const RAIL: Rect = Rect { x: 98.0, @@ -19,14 +20,22 @@ const MARKER: Rect = Rect { }; impl slider::Renderer for Renderer<'_> { - fn draw( + fn node(&self, slider: &Slider<'_, Message>) -> Node { + let style = Style::default() + .width(slider.width) + .height(Length::Units(25)) + .min_width(Length::Units(100)); + + Node::new(style) + } + + fn draw( &mut self, + slider: &Slider<'_, Message>, + layout: Layout<'_>, cursor_position: Point, - bounds: Rectangle, - state: &slider::State, - range: RangeInclusive, - value: f32, ) -> MouseCursor { + let bounds = layout.bounds(); let width = self.spritesheet.width() as f32; let height = self.spritesheet.height() as f32; @@ -48,13 +57,14 @@ impl slider::Renderer for Renderer<'_> { ..DrawParam::default() }); - let (range_start, range_end) = range.into_inner(); + let (range_start, range_end) = slider.range.clone().into_inner(); let marker_offset = (bounds.width - MARKER.w as f32) - * ((value - range_start) / (range_end - range_start).max(1.0)); + * ((slider.value - range_start) + / (range_end - range_start).max(1.0)); let mouse_over = bounds.contains(cursor_position); - let is_active = state.is_dragging() || mouse_over; + let is_active = slider.state.is_dragging() || mouse_over; self.sprites.add(DrawParam { src: Rect { @@ -66,12 +76,13 @@ impl slider::Renderer for Renderer<'_> { }, dest: ggez::mint::Point2 { x: bounds.x + marker_offset.round(), - y: bounds.y + (if state.is_dragging() { 2.0 } else { 0.0 }), + y: bounds.y + + (if slider.state.is_dragging() { 2.0 } else { 0.0 }), }, ..DrawParam::default() }); - if state.is_dragging() { + if slider.state.is_dragging() { MouseCursor::Grabbing } else if mouse_over { MouseCursor::Grab diff --git a/examples/tour/src/iced_ggez/renderer/text.rs b/examples/tour/src/iced_ggez/renderer/text.rs index 450ca2cd..b51cc220 100644 --- a/examples/tour/src/iced_ggez/renderer/text.rs +++ b/examples/tour/src/iced_ggez/renderer/text.rs @@ -1,20 +1,15 @@ use super::{into_color, Renderer}; use ggez::graphics::{self, mint, Align, Scale, Text, TextFragment}; -use iced::text; +use iced_native::{text, Layout, Node, Style}; use std::cell::RefCell; use std::f32; impl text::Renderer for Renderer<'_> { - fn node( - &self, - style: iced::Style, - content: &str, - size: Option, - ) -> iced::Node { + fn node(&self, text: &iced_native::Text) -> Node { let font = self.font; let font_cache = graphics::font_cache(self.context); - let content = String::from(content); + let content = String::from(&text.content); // TODO: Investigate why stretch tries to measure this MANY times // with every ancestor's bounds. @@ -23,20 +18,22 @@ impl text::Renderer for Renderer<'_> { // 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 = size.map(f32::from).unwrap_or(self.font_size); + let size = text.size.map(f32::from).unwrap_or(self.font_size); - iced::Node::with_measure(style, move |bounds| { + 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::Number::Undefined => f32::INFINITY, - iced::Number::Defined(w) => w, + iced_native::Number::Undefined => f32::INFINITY, + iced_native::Number::Defined(w) => w, }, match bounds.height { - iced::Number::Undefined => f32::INFINITY, - iced::Number::Defined(h) => h, + iced_native::Number::Undefined => f32::INFINITY, + iced_native::Number::Defined(h) => h, }, ); @@ -57,7 +54,7 @@ impl text::Renderer for Renderer<'_> { let (width, height) = font_cache.dimensions(&text); - let size = iced::Size { + let size = iced_native::Size { width: width as f32, height: height as f32, }; @@ -75,30 +72,23 @@ impl text::Renderer for Renderer<'_> { }) } - fn draw( - &mut self, - bounds: iced::Rectangle, - content: &str, - size: Option, - color: Option, - horizontal_alignment: text::HorizontalAlignment, - _vertical_alignment: text::VerticalAlignment, - ) { - let size = size.map(f32::from).unwrap_or(self.font_size); - - let mut text = Text::new(TextFragment { - text: String::from(content), + fn draw(&mut self, text: &iced_native::Text, layout: Layout<'_>) { + let size = text.size.map(f32::from).unwrap_or(self.font_size); + let bounds = layout.bounds(); + + let mut ggez_text = Text::new(TextFragment { + text: text.content.clone(), font: Some(self.font), scale: Some(Scale { x: size, y: size }), ..Default::default() }); - text.set_bounds( + ggez_text.set_bounds( mint::Point2 { x: bounds.width, y: bounds.height, }, - match horizontal_alignment { + match text.horizontal_alignment { text::HorizontalAlignment::Left => graphics::Align::Left, text::HorizontalAlignment::Center => graphics::Align::Center, text::HorizontalAlignment::Right => graphics::Align::Right, @@ -107,12 +97,14 @@ impl text::Renderer for Renderer<'_> { graphics::queue_text( self.context, - &text, + &ggez_text, mint::Point2 { x: bounds.x, y: bounds.y, }, - color.or(Some(iced::Color::BLACK)).map(into_color), + text.color + .or(Some(iced_native::Color::BLACK)) + .map(into_color), ); } } diff --git a/examples/tour/src/iced_ggez/widget.rs b/examples/tour/src/iced_ggez/widget.rs index c9f857f6..948f9fc6 100644 --- a/examples/tour/src/iced_ggez/widget.rs +++ b/examples/tour/src/iced_ggez/widget.rs @@ -1,11 +1,12 @@ use super::Renderer; -pub use iced::{ - button, slider, text, Align, Button, Checkbox, Color, Radio, Slider, Text, +pub use iced_native::{ + button, slider, text, Align, Button, Checkbox, Color, Length, Radio, + Slider, Text, }; -pub type Image<'a> = iced::Image<&'a str>; +pub type Image<'a> = iced_native::Image<&'a str>; -pub type Column<'a, Message> = iced::Column<'a, Message, Renderer<'a>>; -pub type Row<'a, Message> = iced::Row<'a, Message, Renderer<'a>>; -pub type Element<'a, Message> = iced::Element<'a, Message, Renderer<'a>>; +pub type Column<'a, Message> = iced_native::Column<'a, Message, Renderer<'a>>; +pub type Row<'a, Message> = iced_native::Row<'a, Message, Renderer<'a>>; +pub type Element<'a, Message> = iced_native::Element<'a, Message, Renderer<'a>>; diff --git a/examples/tour/src/tour.rs b/examples/tour/src/tour.rs index 4bd7c8a3..fd9c2dde 100644 --- a/examples/tour/src/tour.rs +++ b/examples/tour/src/tour.rs @@ -1,6 +1,6 @@ use crate::widget::{ button, slider, text::HorizontalAlignment, Align, Button, Checkbox, Color, - Column, Element, Image, Radio, Row, Slider, Text, + Column, Element, Image, Length, Radio, Row, Slider, Text, }; pub struct Tour { @@ -16,7 +16,7 @@ impl Tour { steps: Steps::new(), back_button: button::State::new(), next_button: button::State::new(), - debug: false, + debug: true, } } @@ -61,7 +61,7 @@ impl Tour { } let element: Element<_> = Column::new() - .max_width(500) + .max_width(Length::Units(500)) .spacing(20) .push(steps.view(self.debug).map(Message::StepMessage)) .push(controls) @@ -478,7 +478,7 @@ impl<'a> Step { .push(Text::new("An image that tries to keep its aspect ratio.")) .push( Image::new("resources/ferris.png") - .width(width) + .width(Length::Units(width)) .align_self(Align::Center), ) .push(Slider::new( diff --git a/native/Cargo.toml b/native/Cargo.toml new file mode 100644 index 00000000..1dbccdee --- /dev/null +++ b/native/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "iced_native" +version = "0.1.0-alpha" +authors = ["Héctor Ramón Jiménez "] +edition = "2018" +description = "A renderer-agnostic library for native GUIs" +license = "MIT" +repository = "https://github.com/hecrj/iced" + +[package.metadata.docs.rs] +features = ["winit"] + +[dependencies] +iced_core = { version = "0.1.0-alpha", features = ["stretch"], path = "../core" } +stretch = "0.2" +twox-hash = "1.5" + +# Enable to obtain conversion traits +winit = { version = "0.20.0-alpha3", optional = true } diff --git a/native/src/element.rs b/native/src/element.rs new file mode 100644 index 00000000..dd5ce621 --- /dev/null +++ b/native/src/element.rs @@ -0,0 +1,372 @@ +use stretch::{geometry, result}; + +use crate::{ + renderer, Color, Event, Hasher, Layout, MouseCursor, Node, Point, Widget, +}; + +/// A generic [`Widget`]. +/// +/// It is useful to build composable user interfaces that do not leak +/// implementation details in their __view logic__. +/// +/// If you have a [built-in widget], you should be able to use `Into` +/// to turn it into an [`Element`]. +/// +/// [built-in widget]: widget/index.html#built-in-widgets +/// [`Widget`]: widget/trait.Widget.html +/// [`Element`]: struct.Element.html +pub struct Element<'a, Message, Renderer> { + pub(crate) widget: Box + 'a>, +} + +impl<'a, Message, Renderer> std::fmt::Debug for Element<'a, Message, Renderer> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Element") + .field("widget", &self.widget) + .finish() + } +} + +impl<'a, Message, Renderer> Element<'a, Message, Renderer> { + /// Create a new [`Element`] containing the given [`Widget`]. + /// + /// [`Element`]: struct.Element.html + /// [`Widget`]: widget/trait.Widget.html + pub fn new( + widget: impl Widget + 'a, + ) -> Element<'a, Message, Renderer> { + Element { + widget: Box::new(widget), + } + } + + /// Applies a transformation to the produced message of the [`Element`]. + /// + /// This method is useful when you want to decouple different parts of your + /// UI and make them __composable__. + /// + /// [`Element`]: struct.Element.html + /// + /// # Example + /// Imagine we want to use [our counter](index.html#usage). But instead of + /// showing a single counter, we want to display many of them. We can reuse + /// the `Counter` type as it is! + /// + /// We use composition to model the __state__ of our new application: + /// + /// ``` + /// # mod counter { + /// # pub struct Counter; + /// # } + /// use counter::Counter; + /// + /// struct ManyCounters { + /// counters: Vec, + /// } + /// ``` + /// + /// We can store the state of multiple counters now. However, the + /// __messages__ we implemented before describe the user interactions + /// of a __single__ counter. Right now, we need to also identify which + /// counter is receiving user interactions. Can we use composition again? + /// Yes. + /// + /// ``` + /// # mod counter { + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message {} + /// # } + /// #[derive(Debug, Clone, Copy)] + /// pub enum Message { + /// Counter(usize, counter::Message) + /// } + /// ``` + /// + /// We compose the previous __messages__ with the index of the counter + /// producing them. Let's implement our __view logic__ now: + /// + /// ``` + /// # mod counter { + /// # use iced_native::{button, Button}; + /// # + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message {} + /// # pub struct Counter(button::State); + /// # + /// # impl Counter { + /// # pub fn view(&mut self) -> Button { + /// # Button::new(&mut self.0, "_") + /// # } + /// # } + /// # } + /// # + /// # mod iced_wgpu { + /// # use iced_native::{ + /// # button, Button, MouseCursor, Node, Point, Rectangle, Style, Layout + /// # }; + /// # pub struct Renderer; + /// # + /// # impl button::Renderer for Renderer { + /// # fn node(&self, _button: &Button<'_, Message>) -> Node { + /// # Node::new(Style::default()) + /// # } + /// # + /// # fn draw( + /// # &mut self, + /// # _button: &Button<'_, Message>, + /// # _layout: Layout<'_>, + /// # _cursor_position: Point, + /// # ) -> MouseCursor { + /// # MouseCursor::OutOfBounds + /// # } + /// # } + /// # } + /// # + /// # use counter::Counter; + /// # + /// # struct ManyCounters { + /// # counters: Vec, + /// # } + /// # + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message { + /// # Counter(usize, counter::Message) + /// # } + /// use iced_native::{Element, Row}; + /// use iced_wgpu::Renderer; + /// + /// impl ManyCounters { + /// pub fn view(&mut self) -> Row { + /// // We can quickly populate a `Row` by folding over our counters + /// self.counters.iter_mut().enumerate().fold( + /// Row::new().spacing(20), + /// |row, (index, counter)| { + /// // We display the counter + /// let element: Element = + /// counter.view().into(); + /// + /// row.push( + /// // Here we turn our `Element` into + /// // an `Element` by combining the `index` and the + /// // message of the `element`. + /// element.map(move |message| Message::Counter(index, message)) + /// ) + /// } + /// ) + /// } + /// } + /// ``` + /// + /// Finally, our __update logic__ is pretty straightforward: simple + /// delegation. + /// + /// ``` + /// # mod counter { + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message {} + /// # pub struct Counter; + /// # + /// # impl Counter { + /// # pub fn update(&mut self, _message: Message) {} + /// # } + /// # } + /// # + /// # use counter::Counter; + /// # + /// # struct ManyCounters { + /// # counters: Vec, + /// # } + /// # + /// # #[derive(Debug, Clone, Copy)] + /// # pub enum Message { + /// # Counter(usize, counter::Message) + /// # } + /// impl ManyCounters { + /// pub fn update(&mut self, message: Message) { + /// match message { + /// Message::Counter(index, counter_msg) => { + /// if let Some(counter) = self.counters.get_mut(index) { + /// counter.update(counter_msg); + /// } + /// } + /// } + /// } + /// } + /// ``` + pub fn map(self, f: F) -> Element<'a, B, Renderer> + where + Message: 'static + Copy, + Renderer: 'a, + B: 'static, + F: 'static + Fn(Message) -> B, + { + Element { + widget: Box::new(Map::new(self.widget, f)), + } + } + + /// Marks the [`Element`] as _to-be-explained_. + /// + /// The [`Renderer`] will explain the layout of the [`Element`] graphically. + /// This can be very useful for debugging your layout! + /// + /// [`Element`]: struct.Element.html + /// [`Renderer`]: trait.Renderer.html + pub fn explain>( + self, + color: C, + ) -> Element<'a, Message, Renderer> + where + Message: 'static, + Renderer: 'a + renderer::Debugger, + { + Element { + widget: Box::new(Explain::new(self, color.into())), + } + } + + pub(crate) fn compute_layout( + &self, + renderer: &mut Renderer, + ) -> result::Layout { + let node = self.widget.node(renderer); + + node.0.compute_layout(geometry::Size::undefined()).unwrap() + } + + pub(crate) fn hash_layout(&self, state: &mut Hasher) { + self.widget.hash_layout(state); + } +} + +struct Map<'a, A, B, Renderer> { + widget: Box + 'a>, + mapper: Box B>, +} + +impl<'a, A, B, Renderer> std::fmt::Debug for Map<'a, A, B, Renderer> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Map").field("widget", &self.widget).finish() + } +} + +impl<'a, A, B, Renderer> Map<'a, A, B, Renderer> { + pub fn new( + widget: Box + 'a>, + mapper: F, + ) -> Map<'a, A, B, Renderer> + where + F: 'static + Fn(A) -> B, + { + Map { + widget, + mapper: Box::new(mapper), + } + } +} + +impl<'a, A, B, Renderer> Widget for Map<'a, A, B, Renderer> +where + A: Copy, +{ + fn node(&self, renderer: &mut Renderer) -> Node { + self.widget.node(renderer) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + let mut original_messages = Vec::new(); + + self.widget.on_event( + event, + layout, + cursor_position, + &mut original_messages, + ); + + original_messages + .iter() + .cloned() + .for_each(|message| messages.push((self.mapper)(message))); + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + self.widget.draw(renderer, layout, cursor_position) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.widget.hash_layout(state); + } +} + +struct Explain<'a, Message, Renderer: renderer::Debugger> { + element: Element<'a, Message, Renderer>, + color: Color, +} + +impl<'a, Message, Renderer> std::fmt::Debug for Explain<'a, Message, Renderer> +where + Renderer: renderer::Debugger, +{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Explain") + .field("element", &self.element) + .finish() + } +} + +impl<'a, Message, Renderer> Explain<'a, Message, Renderer> +where + Renderer: renderer::Debugger, +{ + fn new(element: Element<'a, Message, Renderer>, color: Color) -> Self { + Explain { element, color } + } +} + +impl<'a, Message, Renderer> Widget + for Explain<'a, Message, Renderer> +where + Renderer: renderer::Debugger, +{ + fn node(&self, renderer: &mut Renderer) -> Node { + self.element.widget.node(renderer) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + self.element + .widget + .on_event(event, layout, cursor_position, messages) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + renderer.explain(&layout, self.color); + + self.element.widget.draw(renderer, layout, cursor_position) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.element.widget.hash_layout(state); + } +} diff --git a/native/src/event.rs b/native/src/event.rs new file mode 100644 index 00000000..71f06006 --- /dev/null +++ b/native/src/event.rs @@ -0,0 +1,16 @@ +use crate::input::{keyboard, mouse}; + +/// A user interface event. +/// +/// _**Note:** This type is largely incomplete! If you need to track +/// additional events, feel free to [open an issue] and share your use case!_ +/// +/// [open an issue]: https://github.com/hecrj/iced/issues +#[derive(PartialEq, Clone, Copy, Debug)] +pub enum Event { + /// A keyboard event + Keyboard(keyboard::Event), + + /// A mouse event + Mouse(mouse::Event), +} diff --git a/native/src/hasher.rs b/native/src/hasher.rs new file mode 100644 index 00000000..9f6aacce --- /dev/null +++ b/native/src/hasher.rs @@ -0,0 +1,19 @@ +/// The hasher used to compare layouts. +#[derive(Debug)] +pub struct Hasher(twox_hash::XxHash64); + +impl Default for Hasher { + fn default() -> Self { + Hasher(twox_hash::XxHash64::default()) + } +} + +impl core::hash::Hasher for Hasher { + fn write(&mut self, bytes: &[u8]) { + self.0.write(bytes) + } + + fn finish(&self) -> u64 { + self.0.finish() + } +} diff --git a/native/src/input.rs b/native/src/input.rs new file mode 100644 index 00000000..097fa730 --- /dev/null +++ b/native/src/input.rs @@ -0,0 +1,7 @@ +//! Map your system events into input events that the runtime can understand. +pub mod keyboard; +pub mod mouse; + +mod button_state; + +pub use button_state::ButtonState; diff --git a/native/src/input/button_state.rs b/native/src/input/button_state.rs new file mode 100644 index 00000000..e9dc05d7 --- /dev/null +++ b/native/src/input/button_state.rs @@ -0,0 +1,24 @@ +/// The state of a button. +/// +/// If you are using [`winit`], consider enabling the `winit` feature to get +/// conversion implementations for free! +/// +/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ +#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] +pub enum ButtonState { + /// The button is pressed. + Pressed, + + /// The button is __not__ pressed. + Released, +} + +#[cfg(feature = "winit")] +impl From for ButtonState { + fn from(element_state: winit::event::ElementState) -> Self { + match element_state { + winit::event::ElementState::Pressed => ButtonState::Pressed, + winit::event::ElementState::Released => ButtonState::Released, + } + } +} diff --git a/native/src/input/keyboard.rs b/native/src/input/keyboard.rs new file mode 100644 index 00000000..57c24484 --- /dev/null +++ b/native/src/input/keyboard.rs @@ -0,0 +1,6 @@ +//! Build keyboard events. +mod event; +mod key_code; + +pub use event::Event; +pub use key_code::KeyCode; diff --git a/native/src/input/keyboard/event.rs b/native/src/input/keyboard/event.rs new file mode 100644 index 00000000..8118f112 --- /dev/null +++ b/native/src/input/keyboard/event.rs @@ -0,0 +1,23 @@ +use super::KeyCode; +use crate::input::ButtonState; + +#[derive(Debug, Clone, Copy, PartialEq)] +/// A keyboard event. +/// +/// _**Note:** This type is largely incomplete! If you need to track +/// additional events, feel free to [open an issue] and share your use case!_ +/// +/// [open an issue]: https://github.com/hecrj/iced/issues +pub enum Event { + /// A keyboard key was pressed or released. + Input { + /// The state of the key + state: ButtonState, + + /// The key identifier + key_code: KeyCode, + }, + + /// A unicode character was received. + CharacterReceived(char), +} diff --git a/native/src/input/keyboard/key_code.rs b/native/src/input/keyboard/key_code.rs new file mode 100644 index 00000000..207ddeac --- /dev/null +++ b/native/src/input/keyboard/key_code.rs @@ -0,0 +1,374 @@ +/// The symbolic name of a keyboard key. +/// +/// This is mostly the `KeyCode` type found in [`winit`]. +/// +/// If you are using [`winit`], consider enabling the `winit` feature to get +/// conversion implementations for free! +/// +/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ +#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] +#[repr(u32)] +#[allow(missing_docs)] +pub enum KeyCode { + /// The '1' key over the letters. + Key1, + /// The '2' key over the letters. + Key2, + /// The '3' key over the letters. + Key3, + /// The '4' key over the letters. + Key4, + /// The '5' key over the letters. + Key5, + /// The '6' key over the letters. + Key6, + /// The '7' key over the letters. + Key7, + /// The '8' key over the letters. + Key8, + /// The '9' key over the letters. + Key9, + /// The '0' key over the 'O' and 'P' keys. + Key0, + + A, + B, + C, + D, + E, + F, + G, + H, + I, + J, + K, + L, + M, + N, + O, + P, + Q, + R, + S, + T, + U, + V, + W, + X, + Y, + Z, + + /// The Escape key, next to F1 + Escape, + + F1, + F2, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + F10, + F11, + F12, + F13, + F14, + F15, + F16, + F17, + F18, + F19, + F20, + F21, + F22, + F23, + F24, + + /// Print Screen/SysRq + Snapshot, + /// Scroll Lock + Scroll, + /// Pause/Break key, next to Scroll lock + Pause, + + /// `Insert`, next to Backspace + Insert, + Home, + Delete, + End, + PageDown, + PageUp, + + Left, + Up, + Right, + Down, + + Backspace, + Enter, + Space, + + /// The "Compose" key on Linux + Compose, + + Caret, + + Numlock, + Numpad0, + Numpad1, + Numpad2, + Numpad3, + Numpad4, + Numpad5, + Numpad6, + Numpad7, + Numpad8, + Numpad9, + + AbntC1, + AbntC2, + Add, + Apostrophe, + Apps, + At, + Ax, + Backslash, + Calculator, + Capital, + Colon, + Comma, + Convert, + Decimal, + Divide, + Equals, + Grave, + Kana, + Kanji, + LAlt, + LBracket, + LControl, + LShift, + LWin, + Mail, + MediaSelect, + MediaStop, + Minus, + Multiply, + Mute, + MyComputer, + NavigateForward, // also called "Prior" + NavigateBackward, // also called "Next" + NextTrack, + NoConvert, + NumpadComma, + NumpadEnter, + NumpadEquals, + OEM102, + Period, + PlayPause, + Power, + PrevTrack, + RAlt, + RBracket, + RControl, + RShift, + RWin, + Semicolon, + Slash, + Sleep, + Stop, + Subtract, + Sysrq, + Tab, + Underline, + Unlabeled, + VolumeDown, + VolumeUp, + Wake, + WebBack, + WebFavorites, + WebForward, + WebHome, + WebRefresh, + WebSearch, + WebStop, + Yen, + Copy, + Paste, + Cut, +} + +#[cfg(feature = "winit")] +impl From for KeyCode { + fn from(virtual_keycode: winit::event::VirtualKeyCode) -> Self { + match virtual_keycode { + winit::event::VirtualKeyCode::Key1 => KeyCode::Key1, + winit::event::VirtualKeyCode::Key2 => KeyCode::Key2, + winit::event::VirtualKeyCode::Key3 => KeyCode::Key3, + winit::event::VirtualKeyCode::Key4 => KeyCode::Key4, + winit::event::VirtualKeyCode::Key5 => KeyCode::Key5, + winit::event::VirtualKeyCode::Key6 => KeyCode::Key6, + winit::event::VirtualKeyCode::Key7 => KeyCode::Key7, + winit::event::VirtualKeyCode::Key8 => KeyCode::Key8, + winit::event::VirtualKeyCode::Key9 => KeyCode::Key9, + winit::event::VirtualKeyCode::Key0 => KeyCode::Key0, + winit::event::VirtualKeyCode::A => KeyCode::A, + winit::event::VirtualKeyCode::B => KeyCode::B, + winit::event::VirtualKeyCode::C => KeyCode::C, + winit::event::VirtualKeyCode::D => KeyCode::D, + winit::event::VirtualKeyCode::E => KeyCode::E, + winit::event::VirtualKeyCode::F => KeyCode::F, + winit::event::VirtualKeyCode::G => KeyCode::G, + winit::event::VirtualKeyCode::H => KeyCode::H, + winit::event::VirtualKeyCode::I => KeyCode::I, + winit::event::VirtualKeyCode::J => KeyCode::J, + winit::event::VirtualKeyCode::K => KeyCode::K, + winit::event::VirtualKeyCode::L => KeyCode::L, + winit::event::VirtualKeyCode::M => KeyCode::M, + winit::event::VirtualKeyCode::N => KeyCode::N, + winit::event::VirtualKeyCode::O => KeyCode::O, + winit::event::VirtualKeyCode::P => KeyCode::P, + winit::event::VirtualKeyCode::Q => KeyCode::Q, + winit::event::VirtualKeyCode::R => KeyCode::R, + winit::event::VirtualKeyCode::S => KeyCode::S, + winit::event::VirtualKeyCode::T => KeyCode::T, + winit::event::VirtualKeyCode::U => KeyCode::U, + winit::event::VirtualKeyCode::V => KeyCode::V, + winit::event::VirtualKeyCode::W => KeyCode::W, + winit::event::VirtualKeyCode::X => KeyCode::X, + winit::event::VirtualKeyCode::Y => KeyCode::Y, + winit::event::VirtualKeyCode::Z => KeyCode::Z, + winit::event::VirtualKeyCode::Escape => KeyCode::Escape, + winit::event::VirtualKeyCode::F1 => KeyCode::F1, + winit::event::VirtualKeyCode::F2 => KeyCode::F2, + winit::event::VirtualKeyCode::F3 => KeyCode::F3, + winit::event::VirtualKeyCode::F4 => KeyCode::F4, + winit::event::VirtualKeyCode::F5 => KeyCode::F5, + winit::event::VirtualKeyCode::F6 => KeyCode::F6, + winit::event::VirtualKeyCode::F7 => KeyCode::F7, + winit::event::VirtualKeyCode::F8 => KeyCode::F8, + winit::event::VirtualKeyCode::F9 => KeyCode::F9, + winit::event::VirtualKeyCode::F10 => KeyCode::F10, + winit::event::VirtualKeyCode::F11 => KeyCode::F11, + winit::event::VirtualKeyCode::F12 => KeyCode::F12, + winit::event::VirtualKeyCode::F13 => KeyCode::F13, + winit::event::VirtualKeyCode::F14 => KeyCode::F14, + winit::event::VirtualKeyCode::F15 => KeyCode::F15, + winit::event::VirtualKeyCode::F16 => KeyCode::F16, + winit::event::VirtualKeyCode::F17 => KeyCode::F17, + winit::event::VirtualKeyCode::F18 => KeyCode::F18, + winit::event::VirtualKeyCode::F19 => KeyCode::F19, + winit::event::VirtualKeyCode::F20 => KeyCode::F20, + winit::event::VirtualKeyCode::F21 => KeyCode::F21, + winit::event::VirtualKeyCode::F22 => KeyCode::F22, + winit::event::VirtualKeyCode::F23 => KeyCode::F23, + winit::event::VirtualKeyCode::F24 => KeyCode::F24, + winit::event::VirtualKeyCode::Snapshot => KeyCode::Snapshot, + winit::event::VirtualKeyCode::Scroll => KeyCode::Scroll, + winit::event::VirtualKeyCode::Pause => KeyCode::Pause, + winit::event::VirtualKeyCode::Insert => KeyCode::Insert, + winit::event::VirtualKeyCode::Home => KeyCode::Home, + winit::event::VirtualKeyCode::Delete => KeyCode::Delete, + winit::event::VirtualKeyCode::End => KeyCode::End, + winit::event::VirtualKeyCode::PageDown => KeyCode::PageDown, + winit::event::VirtualKeyCode::PageUp => KeyCode::PageUp, + winit::event::VirtualKeyCode::Left => KeyCode::Left, + winit::event::VirtualKeyCode::Up => KeyCode::Up, + winit::event::VirtualKeyCode::Right => KeyCode::Right, + winit::event::VirtualKeyCode::Down => KeyCode::Down, + winit::event::VirtualKeyCode::Back => KeyCode::Backspace, + winit::event::VirtualKeyCode::Return => KeyCode::Enter, + winit::event::VirtualKeyCode::Space => KeyCode::Space, + winit::event::VirtualKeyCode::Compose => KeyCode::Compose, + winit::event::VirtualKeyCode::Caret => KeyCode::Caret, + winit::event::VirtualKeyCode::Numlock => KeyCode::Numlock, + winit::event::VirtualKeyCode::Numpad0 => KeyCode::Numpad0, + winit::event::VirtualKeyCode::Numpad1 => KeyCode::Numpad1, + winit::event::VirtualKeyCode::Numpad2 => KeyCode::Numpad2, + winit::event::VirtualKeyCode::Numpad3 => KeyCode::Numpad3, + winit::event::VirtualKeyCode::Numpad4 => KeyCode::Numpad4, + winit::event::VirtualKeyCode::Numpad5 => KeyCode::Numpad5, + winit::event::VirtualKeyCode::Numpad6 => KeyCode::Numpad6, + winit::event::VirtualKeyCode::Numpad7 => KeyCode::Numpad7, + winit::event::VirtualKeyCode::Numpad8 => KeyCode::Numpad8, + winit::event::VirtualKeyCode::Numpad9 => KeyCode::Numpad9, + winit::event::VirtualKeyCode::AbntC1 => KeyCode::AbntC1, + winit::event::VirtualKeyCode::AbntC2 => KeyCode::AbntC2, + winit::event::VirtualKeyCode::Add => KeyCode::Add, + winit::event::VirtualKeyCode::Apostrophe => KeyCode::Apostrophe, + winit::event::VirtualKeyCode::Apps => KeyCode::Apps, + winit::event::VirtualKeyCode::At => KeyCode::At, + winit::event::VirtualKeyCode::Ax => KeyCode::Ax, + winit::event::VirtualKeyCode::Backslash => KeyCode::Backslash, + winit::event::VirtualKeyCode::Calculator => KeyCode::Calculator, + winit::event::VirtualKeyCode::Capital => KeyCode::Capital, + winit::event::VirtualKeyCode::Colon => KeyCode::Colon, + winit::event::VirtualKeyCode::Comma => KeyCode::Comma, + winit::event::VirtualKeyCode::Convert => KeyCode::Convert, + winit::event::VirtualKeyCode::Decimal => KeyCode::Decimal, + winit::event::VirtualKeyCode::Divide => KeyCode::Divide, + winit::event::VirtualKeyCode::Equals => KeyCode::Equals, + winit::event::VirtualKeyCode::Grave => KeyCode::Grave, + winit::event::VirtualKeyCode::Kana => KeyCode::Kana, + winit::event::VirtualKeyCode::Kanji => KeyCode::Kanji, + winit::event::VirtualKeyCode::LAlt => KeyCode::LAlt, + winit::event::VirtualKeyCode::LBracket => KeyCode::LBracket, + winit::event::VirtualKeyCode::LControl => KeyCode::LControl, + winit::event::VirtualKeyCode::LShift => KeyCode::LShift, + winit::event::VirtualKeyCode::LWin => KeyCode::LWin, + winit::event::VirtualKeyCode::Mail => KeyCode::Mail, + winit::event::VirtualKeyCode::MediaSelect => KeyCode::MediaSelect, + winit::event::VirtualKeyCode::MediaStop => KeyCode::MediaStop, + winit::event::VirtualKeyCode::Minus => KeyCode::Minus, + winit::event::VirtualKeyCode::Multiply => KeyCode::Multiply, + winit::event::VirtualKeyCode::Mute => KeyCode::Mute, + winit::event::VirtualKeyCode::MyComputer => KeyCode::MyComputer, + winit::event::VirtualKeyCode::NavigateForward => { + KeyCode::NavigateForward + } + winit::event::VirtualKeyCode::NavigateBackward => { + KeyCode::NavigateBackward + } + winit::event::VirtualKeyCode::NextTrack => KeyCode::NextTrack, + winit::event::VirtualKeyCode::NoConvert => KeyCode::NoConvert, + winit::event::VirtualKeyCode::NumpadComma => KeyCode::NumpadComma, + winit::event::VirtualKeyCode::NumpadEnter => KeyCode::NumpadEnter, + winit::event::VirtualKeyCode::NumpadEquals => KeyCode::NumpadEquals, + winit::event::VirtualKeyCode::OEM102 => KeyCode::OEM102, + winit::event::VirtualKeyCode::Period => KeyCode::Period, + winit::event::VirtualKeyCode::PlayPause => KeyCode::PlayPause, + winit::event::VirtualKeyCode::Power => KeyCode::Power, + winit::event::VirtualKeyCode::PrevTrack => KeyCode::PrevTrack, + winit::event::VirtualKeyCode::RAlt => KeyCode::RAlt, + winit::event::VirtualKeyCode::RBracket => KeyCode::RBracket, + winit::event::VirtualKeyCode::RControl => KeyCode::RControl, + winit::event::VirtualKeyCode::RShift => KeyCode::RShift, + winit::event::VirtualKeyCode::RWin => KeyCode::RWin, + winit::event::VirtualKeyCode::Semicolon => KeyCode::Semicolon, + winit::event::VirtualKeyCode::Slash => KeyCode::Slash, + winit::event::VirtualKeyCode::Sleep => KeyCode::Sleep, + winit::event::VirtualKeyCode::Stop => KeyCode::Stop, + winit::event::VirtualKeyCode::Subtract => KeyCode::Subtract, + winit::event::VirtualKeyCode::Sysrq => KeyCode::Sysrq, + winit::event::VirtualKeyCode::Tab => KeyCode::Tab, + winit::event::VirtualKeyCode::Underline => KeyCode::Underline, + winit::event::VirtualKeyCode::Unlabeled => KeyCode::Unlabeled, + winit::event::VirtualKeyCode::VolumeDown => KeyCode::VolumeDown, + winit::event::VirtualKeyCode::VolumeUp => KeyCode::VolumeUp, + winit::event::VirtualKeyCode::Wake => KeyCode::Wake, + winit::event::VirtualKeyCode::WebBack => KeyCode::WebBack, + winit::event::VirtualKeyCode::WebFavorites => KeyCode::WebFavorites, + winit::event::VirtualKeyCode::WebForward => KeyCode::WebForward, + winit::event::VirtualKeyCode::WebHome => KeyCode::WebHome, + winit::event::VirtualKeyCode::WebRefresh => KeyCode::WebRefresh, + winit::event::VirtualKeyCode::WebSearch => KeyCode::WebSearch, + winit::event::VirtualKeyCode::WebStop => KeyCode::WebStop, + winit::event::VirtualKeyCode::Yen => KeyCode::Yen, + winit::event::VirtualKeyCode::Copy => KeyCode::Copy, + winit::event::VirtualKeyCode::Paste => KeyCode::Paste, + winit::event::VirtualKeyCode::Cut => KeyCode::Cut, + } + } +} diff --git a/native/src/input/mouse.rs b/native/src/input/mouse.rs new file mode 100644 index 00000000..d37f5b96 --- /dev/null +++ b/native/src/input/mouse.rs @@ -0,0 +1,6 @@ +//! Build mouse events. +mod button; +mod event; + +pub use button::Button; +pub use event::Event; diff --git a/native/src/input/mouse/button.rs b/native/src/input/mouse/button.rs new file mode 100644 index 00000000..6320d701 --- /dev/null +++ b/native/src/input/mouse/button.rs @@ -0,0 +1,32 @@ +/// The button of a mouse. +/// +/// If you are using [`winit`], consider enabling the `winit` feature to get +/// conversion implementations for free! +/// +/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ +#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] +pub enum Button { + /// The left mouse button. + Left, + + /// The right mouse button. + Right, + + /// The middle (wheel) button. + Middle, + + /// Some other button. + Other(u8), +} + +#[cfg(feature = "winit")] +impl From for super::Button { + fn from(mouse_button: winit::event::MouseButton) -> Self { + match mouse_button { + winit::event::MouseButton::Left => Button::Left, + winit::event::MouseButton::Right => Button::Right, + winit::event::MouseButton::Middle => Button::Middle, + winit::event::MouseButton::Other(other) => Button::Other(other), + } + } +} diff --git a/native/src/input/mouse/event.rs b/native/src/input/mouse/event.rs new file mode 100644 index 00000000..7b68208f --- /dev/null +++ b/native/src/input/mouse/event.rs @@ -0,0 +1,44 @@ +use super::Button; +use crate::input::ButtonState; + +/// A mouse event. +/// +/// _**Note:** This type is largely incomplete! If you need to track +/// additional events, feel free to [open an issue] and share your use case!_ +/// +/// [open an issue]: https://github.com/hecrj/iced/issues +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Event { + /// The mouse cursor entered the window. + CursorEntered, + + /// The mouse cursor left the window. + CursorLeft, + + /// The mouse cursor was moved + CursorMoved { + /// The X coordinate of the mouse position + x: f32, + + /// The Y coordinate of the mouse position + y: f32, + }, + + /// A mouse button was pressed or released. + Input { + /// The state of the button + state: ButtonState, + + /// The button identifier + button: Button, + }, + + /// The mouse wheel was scrolled. + WheelScrolled { + /// The number of horizontal lines scrolled + delta_x: f32, + + /// The number of vertical lines scrolled + delta_y: f32, + }, +} diff --git a/native/src/layout.rs b/native/src/layout.rs new file mode 100644 index 00000000..32630f35 --- /dev/null +++ b/native/src/layout.rs @@ -0,0 +1,62 @@ +use stretch::result; + +use crate::{Point, Rectangle, Vector}; + +/// The computed bounds of a [`Node`] and its children. +/// +/// This type is provided by the GUI runtime to [`Widget::on_event`] and +/// [`Widget::draw`], describing the layout of the [`Node`] produced by +/// [`Widget::node`]. +/// +/// [`Node`]: struct.Node.html +/// [`Widget::on_event`]: widget/trait.Widget.html#method.on_event +/// [`Widget::draw`]: widget/trait.Widget.html#tymethod.draw +/// [`Widget::node`]: widget/trait.Widget.html#tymethod.node +#[derive(Debug, Clone, Copy)] +pub struct Layout<'a> { + layout: &'a result::Layout, + position: Point, +} + +impl<'a> Layout<'a> { + pub(crate) fn new(layout: &'a result::Layout) -> Self { + Self::with_parent_position(layout, Point::new(0.0, 0.0)) + } + + fn with_parent_position( + layout: &'a result::Layout, + parent_position: Point, + ) -> Self { + let position = + parent_position + Vector::new(layout.location.x, layout.location.y); + + Layout { layout, position } + } + + /// Gets the bounds of the [`Layout`]. + /// + /// The returned [`Rectangle`] describes the position and size of a + /// [`Node`]. + /// + /// [`Layout`]: struct.Layout.html + /// [`Rectangle`]: struct.Rectangle.html + /// [`Node`]: struct.Node.html + pub fn bounds(&self) -> Rectangle { + Rectangle { + x: self.position.x, + y: self.position.y, + width: self.layout.size.width, + height: self.layout.size.height, + } + } + + /// Returns an iterator over the [`Layout`] of the children of a [`Node`]. + /// + /// [`Layout`]: struct.Layout.html + /// [`Node`]: struct.Node.html + pub fn children(&'a self) -> impl Iterator> { + self.layout.children.iter().map(move |layout| { + Layout::with_parent_position(layout, self.position) + }) + } +} diff --git a/native/src/lib.rs b/native/src/lib.rs new file mode 100644 index 00000000..39da4943 --- /dev/null +++ b/native/src/lib.rs @@ -0,0 +1,228 @@ +//! Iced is a renderer-agnostic GUI library focused on simplicity and +//! type-safety. Inspired by [Elm]. +//! +//! # Features +//! * Simple, easy-to-use, renderer-agnostic API +//! * Responsive, flexbox-based layouting +//! * Type-safe, reactive programming model +//! * Built-in widgets +//! * Custom widget support +//! +//! Check out the [repository] and the [examples] for more details! +//! +//! [examples]: https://github.com/hecrj/iced/tree/0.1.0/examples +//! [repository]: https://github.com/hecrj/iced +//! +//! # Usage +//! Inspired by [The Elm Architecture], Iced expects you to split user interfaces +//! into four different concepts: +//! +//! * __State__ — the state of your application +//! * __Messages__ — user interactions or meaningful events that you care +//! about +//! * __View logic__ — a way to display your __state__ as widgets that +//! may produce __messages__ on user interaction +//! * __Update logic__ — a way to react to __messages__ and update your +//! __state__ +//! +//! We can build something to see how this works! Let's say we want a simple counter +//! that can be incremented and decremented using two buttons. +//! +//! We start by modelling the __state__ of our application: +//! +//! ``` +//! use iced_native::button; +//! +//! struct Counter { +//! // The counter value +//! value: i32, +//! +//! // The local state of the two buttons +//! increment_button: button::State, +//! decrement_button: button::State, +//! } +//! ``` +//! +//! Next, we need to define the possible user interactions of our counter: +//! the button presses. These interactions are our __messages__: +//! +//! ``` +//! #[derive(Debug, Clone, Copy)] +//! pub enum Message { +//! IncrementPressed, +//! DecrementPressed, +//! } +//! ``` +//! +//! Now, let's show the actual counter by putting it all together in our +//! __view logic__: +//! +//! ``` +//! # use iced_native::button; +//! # +//! # struct Counter { +//! # // The counter value +//! # value: i32, +//! # +//! # // The local state of the two buttons +//! # increment_button: button::State, +//! # decrement_button: button::State, +//! # } +//! # +//! # #[derive(Debug, Clone, Copy)] +//! # pub enum Message { +//! # IncrementPressed, +//! # DecrementPressed, +//! # } +//! # +//! # mod iced_wgpu { +//! # use iced_native::{ +//! # button, text, Button, Text, +//! # MouseCursor, Node, Point, Rectangle, Style, Color, Layout +//! # }; +//! # +//! # pub struct Renderer {} +//! # +//! # impl button::Renderer for Renderer { +//! # fn node( +//! # &self, +//! # _button: &Button<'_, Message> +//! # ) -> Node { +//! # Node::new(Style::default()) +//! # } +//! # +//! # fn draw( +//! # &mut self, +//! # _button: &Button<'_, Message>, +//! # _layout: Layout<'_>, +//! # _cursor_position: Point, +//! # ) -> MouseCursor { +//! # MouseCursor::OutOfBounds +//! # } +//! # } +//! # +//! # impl text::Renderer for Renderer { +//! # fn node(&self, _text: &Text) -> Node { +//! # Node::new(Style::default()) +//! # } +//! # +//! # fn draw( +//! # &mut self, +//! # _text: &Text, +//! # _layout: Layout<'_>, +//! # ) { +//! # } +//! # } +//! # } +//! use iced_native::{Button, Column, Text}; +//! use iced_wgpu::Renderer; // Iced does not include a renderer! We need to bring our own! +//! +//! impl Counter { +//! pub fn view(&mut self) -> Column { +//! // We use a column: a simple vertical layout +//! Column::new() +//! .push( +//! // The increment button. We tell it to produce an +//! // `IncrementPressed` message when pressed +//! Button::new(&mut self.increment_button, "+") +//! .on_press(Message::IncrementPressed), +//! ) +//! .push( +//! // We show the value of the counter here +//! Text::new(&self.value.to_string()).size(50), +//! ) +//! .push( +//! // The decrement button. We tell it to produce a +//! // `DecrementPressed` message when pressed +//! Button::new(&mut self.decrement_button, "-") +//! .on_press(Message::DecrementPressed), +//! ) +//! } +//! } +//! ``` +//! +//! Finally, we need to be able to react to any produced __messages__ and change +//! our __state__ accordingly in our __update logic__: +//! +//! ``` +//! # use iced_native::button; +//! # +//! # struct Counter { +//! # // The counter value +//! # value: i32, +//! # +//! # // The local state of the two buttons +//! # increment_button: button::State, +//! # decrement_button: button::State, +//! # } +//! # +//! # #[derive(Debug, Clone, Copy)] +//! # pub enum Message { +//! # IncrementPressed, +//! # DecrementPressed, +//! # } +//! impl Counter { +//! // ... +//! +//! pub fn update(&mut self, message: Message) { +//! match message { +//! Message::IncrementPressed => { +//! self.value += 1; +//! } +//! Message::DecrementPressed => { +//! self.value -= 1; +//! } +//! } +//! } +//! } +//! ``` +//! +//! And that's everything! We just wrote a whole user interface. Iced is now able +//! to: +//! +//! 1. Take the result of our __view logic__ and layout its widgets. +//! 1. Process events from our system and produce __messages__ for our +//! __update logic__. +//! 1. Draw the resulting user interface using our chosen __renderer__. +//! +//! Check out the [`UserInterface`] type to learn how to wire everything up! +//! +//! [Elm]: https://elm-lang.org/ +//! [The Elm Architecture]: https://guide.elm-lang.org/architecture/ +//! [documentation]: https://docs.rs/iced +//! [examples]: https://github.com/hecrj/iced/tree/master/examples +//! [`UserInterface`]: struct.UserInterface.html +#![deny(missing_docs)] +#![deny(missing_debug_implementations)] +#![deny(unused_results)] +#![deny(unsafe_code)] +#![deny(rust_2018_idioms)] +pub mod input; +pub mod renderer; +pub mod widget; + +mod element; +mod event; +mod hasher; +mod layout; +mod mouse_cursor; +mod node; +mod style; +mod user_interface; + +pub(crate) use iced_core::Vector; + +pub use iced_core::{Align, Color, Justify, Length, Point, Rectangle}; + +#[doc(no_inline)] +pub use stretch::{geometry::Size, number::Number}; + +pub use element::Element; +pub use event::Event; +pub use hasher::Hasher; +pub use layout::Layout; +pub use mouse_cursor::MouseCursor; +pub use node::Node; +pub use style::Style; +pub use user_interface::{Cache, UserInterface}; +pub use widget::*; diff --git a/native/src/mouse_cursor.rs b/native/src/mouse_cursor.rs new file mode 100644 index 00000000..4ef6361a --- /dev/null +++ b/native/src/mouse_cursor.rs @@ -0,0 +1,35 @@ +/// The state of the mouse cursor. +#[derive(Debug, Eq, PartialEq, Clone, Copy)] +pub enum MouseCursor { + /// The cursor is out of the bounds of the user interface. + OutOfBounds, + + /// The cursor is over a non-interactive widget. + Idle, + + /// The cursor is over a clickable widget. + Pointer, + + /// The cursor is over a busy widget. + Working, + + /// The cursor is over a grabbable widget. + Grab, + + /// The cursor is grabbing a widget. + Grabbing, +} + +#[cfg(feature = "winit")] +impl From for winit::window::CursorIcon { + fn from(mouse_cursor: MouseCursor) -> winit::window::CursorIcon { + match mouse_cursor { + MouseCursor::OutOfBounds => winit::window::CursorIcon::Default, + MouseCursor::Idle => winit::window::CursorIcon::Default, + MouseCursor::Pointer => winit::window::CursorIcon::Hand, + MouseCursor::Working => winit::window::CursorIcon::Progress, + MouseCursor::Grab => winit::window::CursorIcon::Grab, + MouseCursor::Grabbing => winit::window::CursorIcon::Grabbing, + } + } +} diff --git a/native/src/node.rs b/native/src/node.rs new file mode 100644 index 00000000..1db10d7f --- /dev/null +++ b/native/src/node.rs @@ -0,0 +1,60 @@ +use stretch::node; + +use crate::{Number, Size, Style}; + +/// The visual requirements of a [`Widget`] and its children. +/// +/// When there have been changes and the [`Layout`] needs to be recomputed, the +/// runtime obtains a [`Node`] by calling [`Widget::node`]. +/// +/// [`Style`]: struct.Style.html +/// [`Widget`]: widget/trait.Widget.html +/// [`Node`]: struct.Node.html +/// [`Widget::node`]: widget/trait.Widget.html#tymethod.node +/// [`Layout`]: struct.Layout.html +#[derive(Debug)] +pub struct Node(pub(crate) node::Node); + +impl Node { + /// Creates a new [`Node`] with the given [`Style`]. + /// + /// [`Node`]: struct.Node.html + /// [`Style`]: struct.Style.html + pub fn new(style: Style) -> Node { + Self::with_children(style, Vec::new()) + } + + /// Creates a new [`Node`] with the given [`Style`] and a measure function. + /// + /// This type of node cannot have any children. + /// + /// You should use this when your [`Widget`] can adapt its contents to the + /// size of its container. The measure function will receive the container + /// size as a parameter and must compute the size of the [`Node`] inside + /// the given bounds (if the `Number` for a dimension is `Undefined` it + /// means that it has no boundary). + /// + /// [`Node`]: struct.Node.html + /// [`Style`]: struct.Style.html + /// [`Widget`]: widget/trait.Widget.html + pub fn with_measure(style: Style, measure: F) -> Node + where + F: 'static + Fn(Size) -> Size, + { + Node(node::Node::new_leaf( + style.0, + Box::new(move |size| Ok(measure(size))), + )) + } + + /// Creates a new [`Node`] with the given [`Style`] and children. + /// + /// [`Node`]: struct.Node.html + /// [`Style`]: struct.Style.html + pub fn with_children(style: Style, children: Vec) -> Node { + Node(node::Node::new( + style.0, + children.iter().map(|c| &c.0).collect(), + )) + } +} diff --git a/native/src/renderer.rs b/native/src/renderer.rs new file mode 100644 index 00000000..2244f00b --- /dev/null +++ b/native/src/renderer.rs @@ -0,0 +1,38 @@ +//! Write your own renderer. +//! +//! There is not a common entrypoint or trait for a __renderer__ in Iced. +//! Instead, every [`Widget`] constrains its generic `Renderer` type as +//! necessary. +//! +//! This approach is flexible and composable. For instance, the +//! [`Text`] widget only needs a [`text::Renderer`] while a [`Checkbox`] widget +//! needs both a [`text::Renderer`] and a [`checkbox::Renderer`], reusing logic. +//! +//! In the end, a __renderer__ satisfying all the constraints is +//! needed to build a [`UserInterface`]. +//! +//! [`Widget`]: ../widget/trait.Widget.html +//! [`UserInterface`]: ../struct.UserInterface.html +//! [`Text`]: ../widget/text/struct.Text.html +//! [`text::Renderer`]: ../widget/text/trait.Renderer.html +//! [`Checkbox`]: ../widget/checkbox/struct.Checkbox.html +//! [`checkbox::Renderer`]: ../widget/checkbox/trait.Renderer.html +use crate::{Color, Layout}; + +/// A renderer able to graphically explain a [`Layout`]. +/// +/// [`Layout`]: ../struct.Layout.html +pub trait Debugger { + /// Explains the [`Layout`] of an [`Element`] for debugging purposes. + /// + /// This will be called when [`Element::explain`] has been used. It should + /// _explain_ the given [`Layout`] graphically. + /// + /// A common approach consists in recursively rendering the bounds of the + /// [`Layout`] and its children. + /// + /// [`Layout`]: struct.Layout.html + /// [`Element`]: struct.Element.html + /// [`Element::explain`]: struct.Element.html#method.explain + fn explain(&mut self, layout: &Layout<'_>, color: Color); +} diff --git a/native/src/style.rs b/native/src/style.rs new file mode 100644 index 00000000..3a75c925 --- /dev/null +++ b/native/src/style.rs @@ -0,0 +1,170 @@ +use crate::{Align, Justify, Length}; + +use std::hash::{Hash, Hasher}; +use stretch::{geometry, style}; + +/// The appearance of a [`Node`]. +/// +/// [`Node`]: struct.Node.html +#[derive(Debug, Clone, Copy)] +pub struct Style(pub(crate) style::Style); + +impl Style { + /// Defines the width of a [`Node`]. + /// + /// [`Node`]: struct.Node.html + pub fn width(mut self, width: Length) -> Self { + self.0.size.width = length_to_dimension(width); + self + } + + /// Defines the height of a [`Node`]. + /// + /// [`Node`]: struct.Node.html + pub fn height(mut self, height: Length) -> Self { + self.0.size.height = length_to_dimension(height); + self + } + + /// Defines the minimum width of a [`Node`]. + /// + /// [`Node`]: struct.Node.html + pub fn min_width(mut self, min_width: Length) -> Self { + self.0.min_size.width = length_to_dimension(min_width); + self + } + + /// Defines the maximum width of a [`Node`]. + /// + /// [`Node`]: struct.Node.html + pub fn max_width(mut self, max_width: Length) -> Self { + self.0.max_size.width = length_to_dimension(max_width); + self + } + + /// Defines the minimum height of a [`Node`]. + /// + /// [`Node`]: struct.Node.html + pub fn min_height(mut self, min_height: Length) -> Self { + self.0.min_size.height = length_to_dimension(min_height); + self + } + + /// Defines the maximum height of a [`Node`]. + /// + /// [`Node`]: struct.Node.html + pub fn max_height(mut self, max_height: Length) -> Self { + self.0.max_size.height = length_to_dimension(max_height); + self + } + + pub(crate) fn align_items(mut self, align: Align) -> Self { + self.0.align_items = align.into(); + self + } + + pub(crate) fn justify_content(mut self, justify: Justify) -> Self { + self.0.justify_content = justify.into(); + self + } + + /// Sets the alignment of a [`Node`]. + /// + /// If the [`Node`] is inside a... + /// + /// * [`Column`], this setting will affect its __horizontal__ alignment. + /// * [`Row`], this setting will affect its __vertical__ alignment. + /// + /// [`Node`]: struct.Node.html + /// [`Column`]: widget/struct.Column.html + /// [`Row`]: widget/struct.Row.html + pub fn align_self(mut self, align: Option) -> Self { + self.0.align_self = match align { + Some(align) => align.into(), + None => stretch::style::AlignSelf::Auto, + }; + + self + } + + /// Sets the padding of a [`Node`]. + /// + /// [`Node`]: struct.Node.html + pub fn padding(mut self, units: u16) -> Self { + self.0.padding = stretch::geometry::Rect { + start: style::Dimension::Points(units as f32), + end: style::Dimension::Points(units as f32), + top: style::Dimension::Points(units as f32), + bottom: style::Dimension::Points(units as f32), + }; + + self + } +} + +fn length_to_dimension(length: Length) -> style::Dimension { + match length { + Length::Shrink => style::Dimension::Undefined, + Length::Fill => style::Dimension::Percent(1.0), + Length::Units(units) => style::Dimension::Points(units as f32), + } +} + +impl Default for Style { + fn default() -> Style { + Style(style::Style { + align_items: style::AlignItems::FlexStart, + justify_content: style::JustifyContent::FlexStart, + ..style::Style::default() + }) + } +} + +impl Hash for Style { + fn hash(&self, state: &mut H) { + hash_size(&self.0.size, state); + hash_size(&self.0.min_size, state); + hash_size(&self.0.max_size, state); + + hash_rect(&self.0.margin, state); + + (self.0.flex_direction as u8).hash(state); + (self.0.align_items as u8).hash(state); + (self.0.justify_content as u8).hash(state); + (self.0.align_self as u8).hash(state); + (self.0.flex_grow as u32).hash(state); + } +} + +fn hash_size( + size: &geometry::Size, + state: &mut H, +) { + hash_dimension(size.width, state); + hash_dimension(size.height, state); +} + +fn hash_rect( + rect: &geometry::Rect, + state: &mut H, +) { + hash_dimension(rect.start, state); + hash_dimension(rect.end, state); + hash_dimension(rect.top, state); + hash_dimension(rect.bottom, state); +} + +fn hash_dimension(dimension: style::Dimension, state: &mut H) { + match dimension { + style::Dimension::Undefined => state.write_u8(0), + style::Dimension::Auto => state.write_u8(1), + style::Dimension::Points(points) => { + state.write_u8(2); + (points as u32).hash(state); + } + style::Dimension::Percent(percent) => { + state.write_u8(3); + (percent as u32).hash(state); + } + } +} diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs new file mode 100644 index 00000000..4bfacb2e --- /dev/null +++ b/native/src/user_interface.rs @@ -0,0 +1,323 @@ +use crate::{input::mouse, Column, Element, Event, Layout, MouseCursor, Point}; + +use std::hash::Hasher; +use stretch::result; + +/// A set of interactive graphical elements with a specific [`Layout`]. +/// +/// It can be updated and drawn. +/// +/// Iced tries to avoid dictating how to write your event loop. You are in +/// charge of using this type in your system in any way you want. +/// +/// [`Layout`]: struct.Layout.html +#[derive(Debug)] +pub struct UserInterface<'a, Message, Renderer> { + hash: u64, + root: Element<'a, Message, Renderer>, + layout: result::Layout, + cursor_position: Point, +} + +impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { + /// Builds a user interface for an [`Element`]. + /// + /// It is able to avoid expensive computations when using a [`Cache`] + /// obtained from a previous instance of a [`UserInterface`]. + /// + /// [`Element`]: struct.Element.html + /// [`Cache`]: struct.Cache.html + /// [`UserInterface`]: struct.UserInterface.html + /// + /// # Example + /// Imagine we want to build a [`UserInterface`] for + /// [the counter example that we previously wrote](index.html#usage). Here + /// is naive way to set up our application loop: + /// + /// ```no_run + /// use iced_native::{UserInterface, Cache}; + /// use iced_wgpu::Renderer; + /// + /// # mod iced_wgpu { + /// # pub struct Renderer; + /// # + /// # impl Renderer { + /// # pub fn new() -> Self { Renderer } + /// # } + /// # } + /// # + /// # use iced_native::Column; + /// # + /// # pub struct Counter; + /// # + /// # impl Counter { + /// # pub fn new() -> Self { Counter } + /// # pub fn view(&self) -> Column<(), Renderer> { + /// # Column::new() + /// # } + /// # } + /// // Initialization + /// let mut counter = Counter::new(); + /// let mut cache = Cache::new(); + /// let mut renderer = Renderer::new(); + /// + /// // Application loop + /// loop { + /// // Process system events here... + /// + /// // Build the user interface + /// let user_interface = UserInterface::build( + /// counter.view(), + /// cache, + /// &mut renderer, + /// ); + /// + /// // Update and draw the user interface here... + /// // ... + /// + /// // Obtain the cache for the next iteration + /// cache = user_interface.into_cache(); + /// } + /// ``` + pub fn build>>( + root: E, + cache: Cache, + renderer: &mut Renderer, + ) -> Self { + let root = root.into(); + + let hasher = &mut crate::Hasher::default(); + root.hash_layout(hasher); + + let hash = hasher.finish(); + + let layout = if hash == cache.hash { + cache.layout + } else { + root.compute_layout(renderer) + }; + + UserInterface { + hash, + root, + layout, + cursor_position: cache.cursor_position, + } + } + + /// Updates the [`UserInterface`] by processing each provided [`Event`]. + /// + /// It returns __messages__ that may have been produced as a result of user + /// interactions. You should feed these to your __update logic__. + /// + /// [`UserInterface`]: struct.UserInterface.html + /// [`Event`]: enum.Event.html + /// + /// # Example + /// Let's allow our [counter](index.html#usage) to change state by completing + /// [the previous example](#example): + /// + /// ```no_run + /// use iced_native::{UserInterface, Cache}; + /// use iced_wgpu::Renderer; + /// + /// # mod iced_wgpu { + /// # pub struct Renderer; + /// # + /// # impl Renderer { + /// # pub fn new() -> Self { Renderer } + /// # } + /// # } + /// # + /// # use iced_native::Column; + /// # + /// # pub struct Counter; + /// # + /// # impl Counter { + /// # pub fn new() -> Self { Counter } + /// # pub fn view(&self) -> Column<(), Renderer> { + /// # Column::new() + /// # } + /// # pub fn update(&mut self, message: ()) {} + /// # } + /// let mut counter = Counter::new(); + /// let mut cache = Cache::new(); + /// let mut renderer = Renderer::new(); + /// + /// // Initialize our event storage + /// let mut events = Vec::new(); + /// + /// loop { + /// // Process system events... + /// + /// let mut user_interface = UserInterface::build( + /// counter.view(), + /// cache, + /// &mut renderer, + /// ); + /// + /// // Update the user interface + /// let messages = user_interface.update(events.drain(..)); + /// + /// cache = user_interface.into_cache(); + /// + /// // Process the produced messages + /// for message in messages { + /// counter.update(message); + /// } + /// } + /// ``` + pub fn update( + &mut self, + events: impl Iterator, + ) -> Vec { + let mut messages = Vec::new(); + + for event in events { + if let Event::Mouse(mouse::Event::CursorMoved { x, y }) = event { + self.cursor_position = Point::new(x, y); + } + + self.root.widget.on_event( + event, + Layout::new(&self.layout), + self.cursor_position, + &mut messages, + ); + } + + messages + } + + /// Draws the [`UserInterface`] with the provided [`Renderer`]. + /// + /// It returns the current state of the [`MouseCursor`]. You should update + /// the icon of the mouse cursor accordingly in your system. + /// + /// [`UserInterface`]: struct.UserInterface.html + /// [`Renderer`]: trait.Renderer.html + /// [`MouseCursor`]: enum.MouseCursor.html + /// + /// # Example + /// We can finally draw our [counter](index.html#usage) by + /// [completing the last example](#example-1): + /// + /// ```no_run + /// use iced_native::{UserInterface, Cache}; + /// use iced_wgpu::Renderer; + /// + /// # mod iced_wgpu { + /// # pub struct Renderer; + /// # + /// # impl Renderer { + /// # pub fn new() -> Self { Renderer } + /// # } + /// # } + /// # + /// # use iced_native::Column; + /// # + /// # pub struct Counter; + /// # + /// # impl Counter { + /// # pub fn new() -> Self { Counter } + /// # pub fn view(&self) -> Column<(), Renderer> { + /// # Column::new() + /// # } + /// # pub fn update(&mut self, message: ()) {} + /// # } + /// let mut counter = Counter::new(); + /// let mut cache = Cache::new(); + /// let mut renderer = Renderer::new(); + /// let mut events = Vec::new(); + /// + /// loop { + /// // Process system events... + /// + /// let mut user_interface = UserInterface::build( + /// counter.view(), + /// cache, + /// &mut renderer, + /// ); + /// + /// let messages = user_interface.update(events.drain(..)); + /// + /// // Draw the user interface + /// let mouse_cursor = user_interface.draw(&mut renderer); + /// + /// cache = user_interface.into_cache(); + /// + /// for message in messages { + /// counter.update(message); + /// } + /// + /// // Update mouse cursor icon... + /// // Flush rendering operations... + /// } + /// ``` + pub fn draw(&self, renderer: &mut Renderer) -> MouseCursor { + self.root.widget.draw( + renderer, + Layout::new(&self.layout), + self.cursor_position, + ) + } + + /// Extract the [`Cache`] of the [`UserInterface`], consuming it in the + /// process. + /// + /// [`Cache`]: struct.Cache.html + /// [`UserInterface`]: struct.UserInterface.html + pub fn into_cache(self) -> Cache { + Cache { + hash: self.hash, + layout: self.layout, + cursor_position: self.cursor_position, + } + } +} + +/// Reusable data of a specific [`UserInterface`]. +/// +/// [`UserInterface`]: struct.UserInterface.html +#[derive(Debug, Clone)] +pub struct Cache { + hash: u64, + layout: result::Layout, + cursor_position: Point, +} + +impl Cache { + /// Creates an empty [`Cache`]. + /// + /// You should use this to initialize a [`Cache`] before building your first + /// [`UserInterface`]. + /// + /// [`Cache`]: struct.Cache.html + /// [`UserInterface`]: struct.UserInterface.html + pub fn new() -> Cache { + let root: Element<'_, (), ()> = Column::new().into(); + + let hasher = &mut crate::Hasher::default(); + root.hash_layout(hasher); + + Cache { + hash: hasher.finish(), + layout: root.compute_layout(&mut ()), + cursor_position: Point::new(0.0, 0.0), + } + } +} + +impl Default for Cache { + fn default() -> Cache { + Cache::new() + } +} + +impl PartialEq for Cache { + fn eq(&self, other: &Cache) -> bool { + self.hash == other.hash && self.cursor_position == other.cursor_position + } +} + +impl Eq for Cache {} diff --git a/native/src/widget.rs b/native/src/widget.rs new file mode 100644 index 00000000..9b770454 --- /dev/null +++ b/native/src/widget.rs @@ -0,0 +1,120 @@ +//! Use the built-in widgets or create your own. +//! +//! # Built-in widgets +//! Every built-in drawable widget has its own module with a `Renderer` trait +//! that must be implemented by a [renderer] before being able to use it as +//! a [`Widget`]. +//! +//! # Custom widgets +//! If you want to implement a custom widget, you simply need to implement the +//! [`Widget`] trait. You can use the API of the built-in widgets as a guide or +//! source of inspiration. +//! +//! # Re-exports +//! For convenience, the contents of this module are available at the root +//! module. Therefore, you can directly type: +//! +//! ``` +//! use iced_native::{button, Button, Widget}; +//! ``` +//! +//! [`Widget`]: trait.Widget.html +//! [renderer]: ../renderer/index.html +mod column; +mod row; + +pub mod button; +pub mod checkbox; +pub mod image; +pub mod radio; +pub mod slider; +pub mod text; + +#[doc(no_inline)] +pub use button::Button; +#[doc(no_inline)] +pub use checkbox::Checkbox; +#[doc(no_inline)] +pub use column::Column; +#[doc(no_inline)] +pub use image::Image; +#[doc(no_inline)] +pub use radio::Radio; +#[doc(no_inline)] +pub use row::Row; +#[doc(no_inline)] +pub use slider::Slider; +#[doc(no_inline)] +pub use text::Text; + +use crate::{Event, Hasher, Layout, MouseCursor, Node, Point}; + +/// A component that displays information and allows interaction. +/// +/// If you want to build your own widgets, you will need to implement this +/// trait. +/// +/// [`Widget`]: trait.Widget.html +/// [`Element`]: ../struct.Element.html +pub trait Widget: std::fmt::Debug { + /// Returns the [`Node`] of the [`Widget`]. + /// + /// This [`Node`] is used by the runtime to compute the [`Layout`] of the + /// user interface. + /// + /// [`Node`]: ../struct.Node.html + /// [`Widget`]: trait.Widget.html + /// [`Layout`]: ../struct.Layout.html + fn node(&self, renderer: &mut Renderer) -> Node; + + /// Draws the [`Widget`] using the associated `Renderer`. + /// + /// It must return the [`MouseCursor`] state for the [`Widget`]. + /// + /// [`Widget`]: trait.Widget.html + /// [`MouseCursor`]: ../enum.MouseCursor.html + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor; + + /// Computes the _layout_ hash of the [`Widget`]. + /// + /// The produced hash is used by the runtime to decide if the [`Layout`] + /// needs to be recomputed between frames. Therefore, to ensure maximum + /// efficiency, the hash should only be affected by the properties of the + /// [`Widget`] that can affect layouting. + /// + /// For example, the [`Text`] widget does not hash its color property, as + /// its value cannot affect the overall [`Layout`] of the user interface. + /// + /// [`Widget`]: trait.Widget.html + /// [`Layout`]: ../struct.Layout.html + /// [`Text`]: text/struct.Text.html + fn hash_layout(&self, state: &mut Hasher); + + /// Processes a runtime [`Event`]. + /// + /// It receives: + /// * an [`Event`] describing user interaction + /// * the computed [`Layout`] of the [`Widget`] + /// * the current cursor position + /// * a mutable `Message` list, allowing the [`Widget`] to produce + /// new messages based on user interaction. + /// + /// By default, it does nothing. + /// + /// [`Event`]: ../enum.Event.html + /// [`Widget`]: trait.Widget.html + /// [`Layout`]: ../struct.Layout.html + fn on_event( + &mut self, + _event: Event, + _layout: Layout<'_>, + _cursor_position: Point, + _messages: &mut Vec, + ) { + } +} diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs new file mode 100644 index 00000000..c9436dc4 --- /dev/null +++ b/native/src/widget/button.rs @@ -0,0 +1,111 @@ +//! Allow your users to perform actions by pressing a button. +//! +//! A [`Button`] has some local [`State`] and a [`Class`]. +//! +//! [`Button`]: struct.Button.html +//! [`State`]: struct.State.html +//! [`Class`]: enum.Class.html + +use crate::input::{mouse, ButtonState}; +use crate::{ + Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Widget, +}; +use std::hash::Hash; + +pub use iced_core::button::*; + +impl<'a, Message, Renderer> Widget for Button<'a, Message> +where + Renderer: self::Renderer, + Message: Copy + std::fmt::Debug, +{ + fn node(&self, renderer: &mut Renderer) -> Node { + renderer.node(&self) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + match event { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state, + }) => { + if let Some(on_press) = self.on_press { + let bounds = layout.bounds(); + + match state { + ButtonState::Pressed => { + self.state.is_pressed = + bounds.contains(cursor_position); + } + ButtonState::Released => { + let is_clicked = self.state.is_pressed + && bounds.contains(cursor_position); + + self.state.is_pressed = false; + + if is_clicked { + messages.push(on_press); + } + } + } + } + } + _ => {} + } + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + renderer.draw(&self, layout, cursor_position) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.width.hash(state); + } +} + +/// The renderer of a [`Button`]. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`Button`] in your user interface. +/// +/// [`Button`]: struct.Button.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Creates a [`Node`] for the provided [`Button`]. + /// + /// [`Node`]: ../../struct.Node.html + /// [`Button`]: struct.Button.html + fn node(&self, button: &Button<'_, Message>) -> Node; + + /// Draws a [`Button`]. + /// + /// [`Button`]: struct.Button.html + fn draw( + &mut self, + button: &Button<'_, Message>, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor; +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + Message: 'static + Copy + std::fmt::Debug, +{ + fn from(button: Button<'a, Message>) -> Element<'a, Message, Renderer> { + Element::new(button) + } +} diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs new file mode 100644 index 00000000..3e307f64 --- /dev/null +++ b/native/src/widget/checkbox.rs @@ -0,0 +1,95 @@ +//! Show toggle controls using checkboxes. +use std::hash::Hash; + +use crate::input::{mouse, ButtonState}; +use crate::{Element, Event, Hasher, Layout, MouseCursor, Node, Point, Widget}; + +pub use iced_core::Checkbox; + +impl Widget for Checkbox +where + Renderer: self::Renderer, +{ + fn node(&self, renderer: &mut Renderer) -> Node { + renderer.node(&self) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + match event { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state: ButtonState::Pressed, + }) => { + let mouse_over = layout + .children() + .any(|child| child.bounds().contains(cursor_position)); + + if mouse_over { + messages.push((self.on_toggle)(!self.is_checked)); + } + } + _ => {} + } + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + renderer.draw(&self, layout, cursor_position) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.label.hash(state); + } +} + +/// The renderer of a [`Checkbox`]. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`Checkbox`] in your user interface. +/// +/// [`Checkbox`]: struct.Checkbox.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Creates a [`Node`] for the provided [`Checkbox`]. + /// + /// [`Node`]: ../../struct.Node.html + /// [`Checkbox`]: struct.Checkbox.html + fn node(&mut self, checkbox: &Checkbox) -> Node; + + /// Draws a [`Checkbox`]. + /// + /// It receives: + /// * the current cursor position + /// * the bounds of the [`Checkbox`] + /// * the bounds of the label of the [`Checkbox`] + /// * whether the [`Checkbox`] is checked or not + /// + /// [`Checkbox`]: struct.Checkbox.html + fn draw( + &mut self, + checkbox: &Checkbox, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor; +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + Message: 'static, +{ + fn from(checkbox: Checkbox) -> Element<'a, Message, Renderer> { + Element::new(checkbox) + } +} diff --git a/native/src/widget/column.rs b/native/src/widget/column.rs new file mode 100644 index 00000000..9da2e161 --- /dev/null +++ b/native/src/widget/column.rs @@ -0,0 +1,118 @@ +use std::hash::Hash; + +use crate::{ + Element, Event, Hasher, Layout, MouseCursor, Node, Point, Style, Widget, +}; + +/// A container that distributes its contents vertically. +pub type Column<'a, Message, Renderer> = + iced_core::Column>; + +impl<'a, Message, Renderer> Widget + for Column<'a, Message, Renderer> +{ + fn node(&self, renderer: &mut Renderer) -> Node { + let mut children: Vec = self + .children + .iter() + .map(|child| { + let mut node = child.widget.node(renderer); + + let mut style = node.0.style(); + style.margin.bottom = + stretch::style::Dimension::Points(f32::from(self.spacing)); + + node.0.set_style(style); + node + }) + .collect(); + + if let Some(node) = children.last_mut() { + let mut style = node.0.style(); + style.margin.bottom = stretch::style::Dimension::Undefined; + + node.0.set_style(style); + } + + let mut style = Style::default() + .width(self.width) + .height(self.height) + .max_width(self.max_width) + .max_height(self.max_height) + .padding(self.padding) + .align_self(self.align_self) + .align_items(self.align_items) + .justify_content(self.justify_content); + + style.0.flex_direction = stretch::style::FlexDirection::Column; + + Node::with_children(style, children) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + self.children.iter_mut().zip(layout.children()).for_each( + |(child, layout)| { + child + .widget + .on_event(event, layout, cursor_position, messages) + }, + ); + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + let mut cursor = MouseCursor::OutOfBounds; + + self.children.iter().zip(layout.children()).for_each( + |(child, layout)| { + let new_cursor = + child.widget.draw(renderer, layout, cursor_position); + + if new_cursor != MouseCursor::OutOfBounds { + cursor = new_cursor; + } + }, + ); + + cursor + } + + fn hash_layout(&self, state: &mut Hasher) { + 0.hash(state); + self.width.hash(state); + self.height.hash(state); + self.max_width.hash(state); + self.max_height.hash(state); + self.align_self.hash(state); + self.align_items.hash(state); + self.justify_content.hash(state); + self.spacing.hash(state); + + for child in &self.children { + child.widget.hash_layout(state); + } + } +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: 'a, + Message: 'static, +{ + fn from( + column: Column<'a, Message, Renderer>, + ) -> Element<'a, Message, Renderer> { + Element::new(column) + } +} diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs new file mode 100644 index 00000000..17f06ebe --- /dev/null +++ b/native/src/widget/image.rs @@ -0,0 +1,67 @@ +//! Display images in your user interface. + +use crate::{ + Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Widget, +}; + +use std::hash::Hash; + +pub use iced_core::Image; + +impl Widget for Image +where + Renderer: self::Renderer, + I: Clone, +{ + fn node(&self, renderer: &mut Renderer) -> Node { + renderer.node(&self) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + renderer.draw(&self, layout); + + MouseCursor::OutOfBounds + } + + fn hash_layout(&self, state: &mut Hasher) { + self.width.hash(state); + self.height.hash(state); + } +} + +/// The renderer of an [`Image`]. +/// +/// Your [renderer] will need to implement this trait before being able to use +/// an [`Image`] in your user interface. +/// +/// [`Image`]: struct.Image.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Creates a [`Node`] for the provided [`Image`]. + /// + /// You should probably keep the original aspect ratio, if possible. + /// + /// [`Node`]: ../../struct.Node.html + /// [`Image`]: struct.Image.html + fn node(&mut self, image: &Image) -> Node; + + /// Draws an [`Image`]. + /// + /// [`Image`]: struct.Image.html + fn draw(&mut self, image: &Image, layout: Layout<'_>); +} + +impl<'a, I, Message, Renderer> From> for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + I: Clone + 'a, +{ + fn from(image: Image) -> Element<'a, Message, Renderer> { + Element::new(image) + } +} diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs new file mode 100644 index 00000000..33d42e61 --- /dev/null +++ b/native/src/widget/radio.rs @@ -0,0 +1,92 @@ +//! Create choices using radio buttons. +use crate::input::{mouse, ButtonState}; +use crate::{Element, Event, Hasher, Layout, MouseCursor, Node, Point, Widget}; + +use std::hash::Hash; + +pub use iced_core::Radio; + +impl Widget for Radio +where + Renderer: self::Renderer, + Message: Copy + std::fmt::Debug, +{ + fn node(&self, renderer: &mut Renderer) -> Node { + renderer.node(&self) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + match event { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state: ButtonState::Pressed, + }) => { + if layout.bounds().contains(cursor_position) { + messages.push(self.on_click); + } + } + _ => {} + } + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + renderer.draw(&self, layout, cursor_position) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.label.hash(state); + } +} + +/// The renderer of a [`Radio`] button. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`Radio`] button in your user interface. +/// +/// [`Radio`]: struct.Radio.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Creates a [`Node`] for the provided [`Radio`]. + /// + /// [`Node`]: ../../struct.Node.html + /// [`Radio`]: struct.Radio.html + fn node(&mut self, radio: &Radio) -> Node; + + /// Draws a [`Radio`] button. + /// + /// It receives: + /// * the current cursor position + /// * the bounds of the [`Radio`] + /// * the bounds of the label of the [`Radio`] + /// * whether the [`Radio`] is selected or not + /// + /// [`Radio`]: struct.Radio.html + fn draw( + &mut self, + radio: &Radio, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor; +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + Message: 'static + Copy + std::fmt::Debug, +{ + fn from(checkbox: Radio) -> Element<'a, Message, Renderer> { + Element::new(checkbox) + } +} diff --git a/native/src/widget/row.rs b/native/src/widget/row.rs new file mode 100644 index 00000000..3cd451b7 --- /dev/null +++ b/native/src/widget/row.rs @@ -0,0 +1,117 @@ +use std::hash::Hash; + +use crate::{ + Element, Event, Hasher, Layout, MouseCursor, Node, Point, Style, Widget, +}; + +/// A container that distributes its contents horizontally. +pub type Row<'a, Message, Renderer> = + iced_core::Row>; + +impl<'a, Message, Renderer> Widget + for Row<'a, Message, Renderer> +{ + fn node(&self, renderer: &mut Renderer) -> Node { + let mut children: Vec = self + .children + .iter() + .map(|child| { + let mut node = child.widget.node(renderer); + + let mut style = node.0.style(); + style.margin.end = + stretch::style::Dimension::Points(f32::from(self.spacing)); + + node.0.set_style(style); + node + }) + .collect(); + + if let Some(node) = children.last_mut() { + let mut style = node.0.style(); + style.margin.end = stretch::style::Dimension::Undefined; + + node.0.set_style(style); + } + + let mut style = Style::default() + .width(self.width) + .height(self.height) + .max_width(self.max_width) + .max_height(self.max_height) + .padding(self.padding) + .align_self(self.align_self) + .align_items(self.align_items) + .justify_content(self.justify_content); + + style.0.flex_direction = stretch::style::FlexDirection::Row; + + Node::with_children(style, children) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + self.children.iter_mut().zip(layout.children()).for_each( + |(child, layout)| { + child + .widget + .on_event(event, layout, cursor_position, messages) + }, + ); + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + let mut cursor = MouseCursor::OutOfBounds; + + self.children.iter().zip(layout.children()).for_each( + |(child, layout)| { + let new_cursor = + child.widget.draw(renderer, layout, cursor_position); + + if new_cursor != MouseCursor::OutOfBounds { + cursor = new_cursor; + } + }, + ); + + cursor + } + + fn hash_layout(&self, state: &mut Hasher) { + 1.hash(state); + self.width.hash(state); + self.height.hash(state); + self.max_width.hash(state); + self.max_height.hash(state); + self.align_self.hash(state); + self.align_items.hash(state); + self.justify_content.hash(state); + self.spacing.hash(state); + self.spacing.hash(state); + + for child in &self.children { + child.widget.hash_layout(state); + } + } +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: 'a, + Message: 'static, +{ + fn from(row: Row<'a, Message, Renderer>) -> Element<'a, Message, Renderer> { + Element::new(row) + } +} diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs new file mode 100644 index 00000000..481296bd --- /dev/null +++ b/native/src/widget/slider.rs @@ -0,0 +1,126 @@ +//! Display an interactive selector of a single value from a range of values. +//! +//! A [`Slider`] has some local [`State`]. +//! +//! [`Slider`]: struct.Slider.html +//! [`State`]: struct.State.html +use std::hash::Hash; + +use crate::input::{mouse, ButtonState}; +use crate::{Element, Event, Hasher, Layout, MouseCursor, Node, Point, Widget}; + +pub use iced_core::slider::*; + +impl<'a, Message, Renderer> Widget for Slider<'a, Message> +where + Renderer: self::Renderer, +{ + fn node(&self, renderer: &mut Renderer) -> Node { + renderer.node(&self) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + messages: &mut Vec, + ) { + let mut change = || { + let bounds = layout.bounds(); + + if cursor_position.x <= bounds.x { + messages.push((self.on_change)(*self.range.start())); + } else if cursor_position.x >= bounds.x + bounds.width { + messages.push((self.on_change)(*self.range.end())); + } else { + let percent = (cursor_position.x - bounds.x) / bounds.width; + let value = (self.range.end() - self.range.start()) * percent + + self.range.start(); + + messages.push((self.on_change)(value)); + } + }; + + match event { + Event::Mouse(mouse::Event::Input { + button: mouse::Button::Left, + state, + }) => match state { + ButtonState::Pressed => { + if layout.bounds().contains(cursor_position) { + change(); + self.state.is_dragging = true; + } + } + ButtonState::Released => { + self.state.is_dragging = false; + } + }, + Event::Mouse(mouse::Event::CursorMoved { .. }) => { + if self.state.is_dragging { + change(); + } + } + _ => {} + } + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor { + renderer.draw(&self, layout, cursor_position) + } + + fn hash_layout(&self, state: &mut Hasher) { + self.width.hash(state); + } +} + +/// The renderer of a [`Slider`]. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`Slider`] in your user interface. +/// +/// [`Slider`]: struct.Slider.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Creates a [`Node`] for the provided [`Radio`]. + /// + /// [`Node`]: ../../struct.Node.html + /// [`Radio`]: struct.Radio.html + fn node(&self, slider: &Slider<'_, Message>) -> Node; + + /// Draws a [`Slider`]. + /// + /// It receives: + /// * the current cursor position + /// * the bounds of the [`Slider`] + /// * the local state of the [`Slider`] + /// * the range of values of the [`Slider`] + /// * the current value of the [`Slider`] + /// + /// [`Slider`]: struct.Slider.html + /// [`State`]: struct.State.html + /// [`Class`]: enum.Class.html + fn draw( + &mut self, + slider: &Slider<'_, Message>, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor; +} + +impl<'a, Message, Renderer> From> + for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + Message: 'static, +{ + fn from(slider: Slider<'a, Message>) -> Element<'a, Message, Renderer> { + Element::new(slider) + } +} diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs new file mode 100644 index 00000000..affdfd64 --- /dev/null +++ b/native/src/widget/text.rs @@ -0,0 +1,77 @@ +//! Write some text for your users to read. +use crate::{Element, Hasher, Layout, MouseCursor, Node, Point, Widget}; + +use std::hash::Hash; + +pub use iced_core::text::*; + +impl Widget for Text +where + Renderer: self::Renderer, +{ + fn node(&self, renderer: &mut Renderer) -> Node { + renderer.node(&self) + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + _cursor_position: Point, + ) -> MouseCursor { + renderer.draw(&self, layout); + + MouseCursor::OutOfBounds + } + + fn hash_layout(&self, state: &mut Hasher) { + self.content.hash(state); + self.size.hash(state); + } +} + +/// The renderer of a [`Text`] fragment. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use [`Text`] in your [`UserInterface`]. +/// +/// [`Text`]: struct.Text.html +/// [renderer]: ../../renderer/index.html +/// [`UserInterface`]: ../../struct.UserInterface.html +pub trait Renderer { + /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] + /// contents and size. + /// + /// You should probably use [`Node::with_measure`] to allow [`Text`] to + /// adapt to the dimensions of its container. + /// + /// [`Node`]: ../../struct.Node.html + /// [`Style`]: ../../struct.Style.html + /// [`Text`]: struct.Text.html + /// [`Node::with_measure`]: ../../struct.Node.html#method.with_measure + fn node(&self, text: &Text) -> Node; + + /// Draws a [`Text`] fragment. + /// + /// It receives: + /// * the bounds of the [`Text`] + /// * the contents of the [`Text`] + /// * the size of the [`Text`] + /// * the color of the [`Text`] + /// * the [`HorizontalAlignment`] of the [`Text`] + /// * the [`VerticalAlignment`] of the [`Text`] + /// + /// [`Text`]: struct.Text.html + /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html + /// [`VerticalAlignment`]: enum.VerticalAlignment.html + fn draw(&mut self, text: &Text, layout: Layout<'_>); +} + +impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, +{ + fn from(text: Text) -> Element<'a, Message, Renderer> { + Element::new(text) + } +} diff --git a/src/color.rs b/src/color.rs deleted file mode 100644 index 5cc3a084..00000000 --- a/src/color.rs +++ /dev/null @@ -1,19 +0,0 @@ -/// A color in the sRGB color space. -#[derive(Debug, Clone, Copy, PartialEq)] -#[allow(missing_docs)] -pub struct Color { - pub r: f32, - pub g: f32, - pub b: f32, - pub a: f32, -} - -impl Color { - /// The black color. - pub const BLACK: Color = Color { - r: 0.0, - g: 0.0, - b: 0.0, - a: 1.0, - }; -} diff --git a/src/element.rs b/src/element.rs deleted file mode 100644 index f6276fbf..00000000 --- a/src/element.rs +++ /dev/null @@ -1,370 +0,0 @@ -use stretch::{geometry, result}; - -use crate::{ - renderer, Color, Event, Hasher, Layout, MouseCursor, Node, Point, Widget, -}; - -/// A generic [`Widget`]. -/// -/// It is useful to build composable user interfaces that do not leak -/// implementation details in their __view logic__. -/// -/// If you have a [built-in widget], you should be able to use `Into` -/// to turn it into an [`Element`]. -/// -/// [built-in widget]: widget/index.html#built-in-widgets -/// [`Widget`]: widget/trait.Widget.html -/// [`Element`]: struct.Element.html -pub struct Element<'a, Message, Renderer> { - pub(crate) widget: Box + 'a>, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Element<'a, Message, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Element") - .field("widget", &self.widget) - .finish() - } -} - -impl<'a, Message, Renderer> Element<'a, Message, Renderer> { - /// Create a new [`Element`] containing the given [`Widget`]. - /// - /// [`Element`]: struct.Element.html - /// [`Widget`]: widget/trait.Widget.html - pub fn new( - widget: impl Widget + 'a, - ) -> Element<'a, Message, Renderer> { - Element { - widget: Box::new(widget), - } - } - - /// Applies a transformation to the produced message of the [`Element`]. - /// - /// This method is useful when you want to decouple different parts of your - /// UI and make them __composable__. - /// - /// [`Element`]: struct.Element.html - /// - /// # Example - /// Imagine we want to use [our counter](index.html#usage). But instead of - /// showing a single counter, we want to display many of them. We can reuse - /// the `Counter` type as it is! - /// - /// We use composition to model the __state__ of our new application: - /// - /// ``` - /// # mod counter { - /// # pub struct Counter; - /// # } - /// use counter::Counter; - /// - /// struct ManyCounters { - /// counters: Vec, - /// } - /// ``` - /// - /// We can store the state of multiple counters now. However, the - /// __messages__ we implemented before describe the user interactions - /// of a __single__ counter. Right now, we need to also identify which - /// counter is receiving user interactions. Can we use composition again? - /// Yes. - /// - /// ``` - /// # mod counter { - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message {} - /// # } - /// #[derive(Debug, Clone, Copy)] - /// pub enum Message { - /// Counter(usize, counter::Message) - /// } - /// ``` - /// - /// We compose the previous __messages__ with the index of the counter - /// producing them. Let's implement our __view logic__ now: - /// - /// ``` - /// # mod counter { - /// # use iced::{button, Button}; - /// # - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message {} - /// # pub struct Counter(button::State); - /// # - /// # impl Counter { - /// # pub fn view(&mut self) -> Button { - /// # Button::new(&mut self.0, "_") - /// # } - /// # } - /// # } - /// # - /// # mod iced_wgpu { - /// # use iced::{ - /// # button, MouseCursor, Node, Point, Rectangle, Style, - /// # }; - /// # pub struct Renderer; - /// # - /// # impl button::Renderer for Renderer { - /// # fn draw( - /// # &mut self, - /// # _cursor_position: Point, - /// # _bounds: Rectangle, - /// # _state: &button::State, - /// # _label: &str, - /// # _class: button::Class, - /// # ) -> MouseCursor { - /// # MouseCursor::OutOfBounds - /// # } - /// # } - /// # } - /// # - /// # use counter::Counter; - /// # - /// # struct ManyCounters { - /// # counters: Vec, - /// # } - /// # - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message { - /// # Counter(usize, counter::Message) - /// # } - /// use iced::{Element, Row}; - /// use iced_wgpu::Renderer; - /// - /// impl ManyCounters { - /// pub fn view(&mut self) -> Row { - /// // We can quickly populate a `Row` by folding over our counters - /// self.counters.iter_mut().enumerate().fold( - /// Row::new().spacing(20), - /// |row, (index, counter)| { - /// // We display the counter - /// let element: Element = - /// counter.view().into(); - /// - /// row.push( - /// // Here we turn our `Element` into - /// // an `Element` by combining the `index` and the - /// // message of the `element`. - /// element.map(move |message| Message::Counter(index, message)) - /// ) - /// } - /// ) - /// } - /// } - /// ``` - /// - /// Finally, our __update logic__ is pretty straightforward: simple - /// delegation. - /// - /// ``` - /// # mod counter { - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message {} - /// # pub struct Counter; - /// # - /// # impl Counter { - /// # pub fn update(&mut self, _message: Message) {} - /// # } - /// # } - /// # - /// # use counter::Counter; - /// # - /// # struct ManyCounters { - /// # counters: Vec, - /// # } - /// # - /// # #[derive(Debug, Clone, Copy)] - /// # pub enum Message { - /// # Counter(usize, counter::Message) - /// # } - /// impl ManyCounters { - /// pub fn update(&mut self, message: Message) { - /// match message { - /// Message::Counter(index, counter_msg) => { - /// if let Some(counter) = self.counters.get_mut(index) { - /// counter.update(counter_msg); - /// } - /// } - /// } - /// } - /// } - /// ``` - pub fn map(self, f: F) -> Element<'a, B, Renderer> - where - Message: 'static + Copy, - Renderer: 'a, - B: 'static, - F: 'static + Fn(Message) -> B, - { - Element { - widget: Box::new(Map::new(self.widget, f)), - } - } - - /// Marks the [`Element`] as _to-be-explained_. - /// - /// The [`Renderer`] will explain the layout of the [`Element`] graphically. - /// This can be very useful for debugging your layout! - /// - /// [`Element`]: struct.Element.html - /// [`Renderer`]: trait.Renderer.html - pub fn explain>( - self, - color: C, - ) -> Element<'a, Message, Renderer> - where - Message: 'static, - Renderer: 'a + renderer::Debugger, - { - Element { - widget: Box::new(Explain::new(self, color.into())), - } - } - - pub(crate) fn compute_layout( - &self, - renderer: &mut Renderer, - ) -> result::Layout { - let node = self.widget.node(renderer); - - node.0.compute_layout(geometry::Size::undefined()).unwrap() - } - - pub(crate) fn hash_layout(&self, state: &mut Hasher) { - self.widget.hash_layout(state); - } -} - -struct Map<'a, A, B, Renderer> { - widget: Box + 'a>, - mapper: Box B>, -} - -impl<'a, A, B, Renderer> std::fmt::Debug for Map<'a, A, B, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Map").field("widget", &self.widget).finish() - } -} - -impl<'a, A, B, Renderer> Map<'a, A, B, Renderer> { - pub fn new( - widget: Box + 'a>, - mapper: F, - ) -> Map<'a, A, B, Renderer> - where - F: 'static + Fn(A) -> B, - { - Map { - widget, - mapper: Box::new(mapper), - } - } -} - -impl<'a, A, B, Renderer> Widget for Map<'a, A, B, Renderer> -where - A: Copy, -{ - fn node(&self, renderer: &mut Renderer) -> Node { - self.widget.node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - let mut original_messages = Vec::new(); - - self.widget.on_event( - event, - layout, - cursor_position, - &mut original_messages, - ); - - original_messages - .iter() - .cloned() - .for_each(|message| messages.push((self.mapper)(message))); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - self.widget.draw(renderer, layout, cursor_position) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.widget.hash_layout(state); - } -} - -struct Explain<'a, Message, Renderer: renderer::Debugger> { - element: Element<'a, Message, Renderer>, - color: Color, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Explain<'a, Message, Renderer> -where - Renderer: renderer::Debugger, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Explain") - .field("element", &self.element) - .finish() - } -} - -impl<'a, Message, Renderer> Explain<'a, Message, Renderer> -where - Renderer: renderer::Debugger, -{ - fn new(element: Element<'a, Message, Renderer>, color: Color) -> Self { - Explain { element, color } - } -} - -impl<'a, Message, Renderer> Widget - for Explain<'a, Message, Renderer> -where - Renderer: renderer::Debugger, -{ - fn node(&self, renderer: &mut Renderer) -> Node { - self.element.widget.node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - self.element - .widget - .on_event(event, layout, cursor_position, messages) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - renderer.explain(&layout, self.color); - - self.element.widget.draw(renderer, layout, cursor_position) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.element.widget.hash_layout(state); - } -} diff --git a/src/event.rs b/src/event.rs deleted file mode 100644 index 71f06006..00000000 --- a/src/event.rs +++ /dev/null @@ -1,16 +0,0 @@ -use crate::input::{keyboard, mouse}; - -/// A user interface event. -/// -/// _**Note:** This type is largely incomplete! If you need to track -/// additional events, feel free to [open an issue] and share your use case!_ -/// -/// [open an issue]: https://github.com/hecrj/iced/issues -#[derive(PartialEq, Clone, Copy, Debug)] -pub enum Event { - /// A keyboard event - Keyboard(keyboard::Event), - - /// A mouse event - Mouse(mouse::Event), -} diff --git a/src/hasher.rs b/src/hasher.rs deleted file mode 100644 index 9f6aacce..00000000 --- a/src/hasher.rs +++ /dev/null @@ -1,19 +0,0 @@ -/// The hasher used to compare layouts. -#[derive(Debug)] -pub struct Hasher(twox_hash::XxHash64); - -impl Default for Hasher { - fn default() -> Self { - Hasher(twox_hash::XxHash64::default()) - } -} - -impl core::hash::Hasher for Hasher { - fn write(&mut self, bytes: &[u8]) { - self.0.write(bytes) - } - - fn finish(&self) -> u64 { - self.0.finish() - } -} diff --git a/src/input.rs b/src/input.rs deleted file mode 100644 index 097fa730..00000000 --- a/src/input.rs +++ /dev/null @@ -1,7 +0,0 @@ -//! Map your system events into input events that the runtime can understand. -pub mod keyboard; -pub mod mouse; - -mod button_state; - -pub use button_state::ButtonState; diff --git a/src/input/button_state.rs b/src/input/button_state.rs deleted file mode 100644 index e9dc05d7..00000000 --- a/src/input/button_state.rs +++ /dev/null @@ -1,24 +0,0 @@ -/// The state of a button. -/// -/// If you are using [`winit`], consider enabling the `winit` feature to get -/// conversion implementations for free! -/// -/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ -#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] -pub enum ButtonState { - /// The button is pressed. - Pressed, - - /// The button is __not__ pressed. - Released, -} - -#[cfg(feature = "winit")] -impl From for ButtonState { - fn from(element_state: winit::event::ElementState) -> Self { - match element_state { - winit::event::ElementState::Pressed => ButtonState::Pressed, - winit::event::ElementState::Released => ButtonState::Released, - } - } -} diff --git a/src/input/keyboard.rs b/src/input/keyboard.rs deleted file mode 100644 index 57c24484..00000000 --- a/src/input/keyboard.rs +++ /dev/null @@ -1,6 +0,0 @@ -//! Build keyboard events. -mod event; -mod key_code; - -pub use event::Event; -pub use key_code::KeyCode; diff --git a/src/input/keyboard/event.rs b/src/input/keyboard/event.rs deleted file mode 100644 index 8118f112..00000000 --- a/src/input/keyboard/event.rs +++ /dev/null @@ -1,23 +0,0 @@ -use super::KeyCode; -use crate::input::ButtonState; - -#[derive(Debug, Clone, Copy, PartialEq)] -/// A keyboard event. -/// -/// _**Note:** This type is largely incomplete! If you need to track -/// additional events, feel free to [open an issue] and share your use case!_ -/// -/// [open an issue]: https://github.com/hecrj/iced/issues -pub enum Event { - /// A keyboard key was pressed or released. - Input { - /// The state of the key - state: ButtonState, - - /// The key identifier - key_code: KeyCode, - }, - - /// A unicode character was received. - CharacterReceived(char), -} diff --git a/src/input/keyboard/key_code.rs b/src/input/keyboard/key_code.rs deleted file mode 100644 index 207ddeac..00000000 --- a/src/input/keyboard/key_code.rs +++ /dev/null @@ -1,374 +0,0 @@ -/// The symbolic name of a keyboard key. -/// -/// This is mostly the `KeyCode` type found in [`winit`]. -/// -/// If you are using [`winit`], consider enabling the `winit` feature to get -/// conversion implementations for free! -/// -/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ -#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] -#[repr(u32)] -#[allow(missing_docs)] -pub enum KeyCode { - /// The '1' key over the letters. - Key1, - /// The '2' key over the letters. - Key2, - /// The '3' key over the letters. - Key3, - /// The '4' key over the letters. - Key4, - /// The '5' key over the letters. - Key5, - /// The '6' key over the letters. - Key6, - /// The '7' key over the letters. - Key7, - /// The '8' key over the letters. - Key8, - /// The '9' key over the letters. - Key9, - /// The '0' key over the 'O' and 'P' keys. - Key0, - - A, - B, - C, - D, - E, - F, - G, - H, - I, - J, - K, - L, - M, - N, - O, - P, - Q, - R, - S, - T, - U, - V, - W, - X, - Y, - Z, - - /// The Escape key, next to F1 - Escape, - - F1, - F2, - F3, - F4, - F5, - F6, - F7, - F8, - F9, - F10, - F11, - F12, - F13, - F14, - F15, - F16, - F17, - F18, - F19, - F20, - F21, - F22, - F23, - F24, - - /// Print Screen/SysRq - Snapshot, - /// Scroll Lock - Scroll, - /// Pause/Break key, next to Scroll lock - Pause, - - /// `Insert`, next to Backspace - Insert, - Home, - Delete, - End, - PageDown, - PageUp, - - Left, - Up, - Right, - Down, - - Backspace, - Enter, - Space, - - /// The "Compose" key on Linux - Compose, - - Caret, - - Numlock, - Numpad0, - Numpad1, - Numpad2, - Numpad3, - Numpad4, - Numpad5, - Numpad6, - Numpad7, - Numpad8, - Numpad9, - - AbntC1, - AbntC2, - Add, - Apostrophe, - Apps, - At, - Ax, - Backslash, - Calculator, - Capital, - Colon, - Comma, - Convert, - Decimal, - Divide, - Equals, - Grave, - Kana, - Kanji, - LAlt, - LBracket, - LControl, - LShift, - LWin, - Mail, - MediaSelect, - MediaStop, - Minus, - Multiply, - Mute, - MyComputer, - NavigateForward, // also called "Prior" - NavigateBackward, // also called "Next" - NextTrack, - NoConvert, - NumpadComma, - NumpadEnter, - NumpadEquals, - OEM102, - Period, - PlayPause, - Power, - PrevTrack, - RAlt, - RBracket, - RControl, - RShift, - RWin, - Semicolon, - Slash, - Sleep, - Stop, - Subtract, - Sysrq, - Tab, - Underline, - Unlabeled, - VolumeDown, - VolumeUp, - Wake, - WebBack, - WebFavorites, - WebForward, - WebHome, - WebRefresh, - WebSearch, - WebStop, - Yen, - Copy, - Paste, - Cut, -} - -#[cfg(feature = "winit")] -impl From for KeyCode { - fn from(virtual_keycode: winit::event::VirtualKeyCode) -> Self { - match virtual_keycode { - winit::event::VirtualKeyCode::Key1 => KeyCode::Key1, - winit::event::VirtualKeyCode::Key2 => KeyCode::Key2, - winit::event::VirtualKeyCode::Key3 => KeyCode::Key3, - winit::event::VirtualKeyCode::Key4 => KeyCode::Key4, - winit::event::VirtualKeyCode::Key5 => KeyCode::Key5, - winit::event::VirtualKeyCode::Key6 => KeyCode::Key6, - winit::event::VirtualKeyCode::Key7 => KeyCode::Key7, - winit::event::VirtualKeyCode::Key8 => KeyCode::Key8, - winit::event::VirtualKeyCode::Key9 => KeyCode::Key9, - winit::event::VirtualKeyCode::Key0 => KeyCode::Key0, - winit::event::VirtualKeyCode::A => KeyCode::A, - winit::event::VirtualKeyCode::B => KeyCode::B, - winit::event::VirtualKeyCode::C => KeyCode::C, - winit::event::VirtualKeyCode::D => KeyCode::D, - winit::event::VirtualKeyCode::E => KeyCode::E, - winit::event::VirtualKeyCode::F => KeyCode::F, - winit::event::VirtualKeyCode::G => KeyCode::G, - winit::event::VirtualKeyCode::H => KeyCode::H, - winit::event::VirtualKeyCode::I => KeyCode::I, - winit::event::VirtualKeyCode::J => KeyCode::J, - winit::event::VirtualKeyCode::K => KeyCode::K, - winit::event::VirtualKeyCode::L => KeyCode::L, - winit::event::VirtualKeyCode::M => KeyCode::M, - winit::event::VirtualKeyCode::N => KeyCode::N, - winit::event::VirtualKeyCode::O => KeyCode::O, - winit::event::VirtualKeyCode::P => KeyCode::P, - winit::event::VirtualKeyCode::Q => KeyCode::Q, - winit::event::VirtualKeyCode::R => KeyCode::R, - winit::event::VirtualKeyCode::S => KeyCode::S, - winit::event::VirtualKeyCode::T => KeyCode::T, - winit::event::VirtualKeyCode::U => KeyCode::U, - winit::event::VirtualKeyCode::V => KeyCode::V, - winit::event::VirtualKeyCode::W => KeyCode::W, - winit::event::VirtualKeyCode::X => KeyCode::X, - winit::event::VirtualKeyCode::Y => KeyCode::Y, - winit::event::VirtualKeyCode::Z => KeyCode::Z, - winit::event::VirtualKeyCode::Escape => KeyCode::Escape, - winit::event::VirtualKeyCode::F1 => KeyCode::F1, - winit::event::VirtualKeyCode::F2 => KeyCode::F2, - winit::event::VirtualKeyCode::F3 => KeyCode::F3, - winit::event::VirtualKeyCode::F4 => KeyCode::F4, - winit::event::VirtualKeyCode::F5 => KeyCode::F5, - winit::event::VirtualKeyCode::F6 => KeyCode::F6, - winit::event::VirtualKeyCode::F7 => KeyCode::F7, - winit::event::VirtualKeyCode::F8 => KeyCode::F8, - winit::event::VirtualKeyCode::F9 => KeyCode::F9, - winit::event::VirtualKeyCode::F10 => KeyCode::F10, - winit::event::VirtualKeyCode::F11 => KeyCode::F11, - winit::event::VirtualKeyCode::F12 => KeyCode::F12, - winit::event::VirtualKeyCode::F13 => KeyCode::F13, - winit::event::VirtualKeyCode::F14 => KeyCode::F14, - winit::event::VirtualKeyCode::F15 => KeyCode::F15, - winit::event::VirtualKeyCode::F16 => KeyCode::F16, - winit::event::VirtualKeyCode::F17 => KeyCode::F17, - winit::event::VirtualKeyCode::F18 => KeyCode::F18, - winit::event::VirtualKeyCode::F19 => KeyCode::F19, - winit::event::VirtualKeyCode::F20 => KeyCode::F20, - winit::event::VirtualKeyCode::F21 => KeyCode::F21, - winit::event::VirtualKeyCode::F22 => KeyCode::F22, - winit::event::VirtualKeyCode::F23 => KeyCode::F23, - winit::event::VirtualKeyCode::F24 => KeyCode::F24, - winit::event::VirtualKeyCode::Snapshot => KeyCode::Snapshot, - winit::event::VirtualKeyCode::Scroll => KeyCode::Scroll, - winit::event::VirtualKeyCode::Pause => KeyCode::Pause, - winit::event::VirtualKeyCode::Insert => KeyCode::Insert, - winit::event::VirtualKeyCode::Home => KeyCode::Home, - winit::event::VirtualKeyCode::Delete => KeyCode::Delete, - winit::event::VirtualKeyCode::End => KeyCode::End, - winit::event::VirtualKeyCode::PageDown => KeyCode::PageDown, - winit::event::VirtualKeyCode::PageUp => KeyCode::PageUp, - winit::event::VirtualKeyCode::Left => KeyCode::Left, - winit::event::VirtualKeyCode::Up => KeyCode::Up, - winit::event::VirtualKeyCode::Right => KeyCode::Right, - winit::event::VirtualKeyCode::Down => KeyCode::Down, - winit::event::VirtualKeyCode::Back => KeyCode::Backspace, - winit::event::VirtualKeyCode::Return => KeyCode::Enter, - winit::event::VirtualKeyCode::Space => KeyCode::Space, - winit::event::VirtualKeyCode::Compose => KeyCode::Compose, - winit::event::VirtualKeyCode::Caret => KeyCode::Caret, - winit::event::VirtualKeyCode::Numlock => KeyCode::Numlock, - winit::event::VirtualKeyCode::Numpad0 => KeyCode::Numpad0, - winit::event::VirtualKeyCode::Numpad1 => KeyCode::Numpad1, - winit::event::VirtualKeyCode::Numpad2 => KeyCode::Numpad2, - winit::event::VirtualKeyCode::Numpad3 => KeyCode::Numpad3, - winit::event::VirtualKeyCode::Numpad4 => KeyCode::Numpad4, - winit::event::VirtualKeyCode::Numpad5 => KeyCode::Numpad5, - winit::event::VirtualKeyCode::Numpad6 => KeyCode::Numpad6, - winit::event::VirtualKeyCode::Numpad7 => KeyCode::Numpad7, - winit::event::VirtualKeyCode::Numpad8 => KeyCode::Numpad8, - winit::event::VirtualKeyCode::Numpad9 => KeyCode::Numpad9, - winit::event::VirtualKeyCode::AbntC1 => KeyCode::AbntC1, - winit::event::VirtualKeyCode::AbntC2 => KeyCode::AbntC2, - winit::event::VirtualKeyCode::Add => KeyCode::Add, - winit::event::VirtualKeyCode::Apostrophe => KeyCode::Apostrophe, - winit::event::VirtualKeyCode::Apps => KeyCode::Apps, - winit::event::VirtualKeyCode::At => KeyCode::At, - winit::event::VirtualKeyCode::Ax => KeyCode::Ax, - winit::event::VirtualKeyCode::Backslash => KeyCode::Backslash, - winit::event::VirtualKeyCode::Calculator => KeyCode::Calculator, - winit::event::VirtualKeyCode::Capital => KeyCode::Capital, - winit::event::VirtualKeyCode::Colon => KeyCode::Colon, - winit::event::VirtualKeyCode::Comma => KeyCode::Comma, - winit::event::VirtualKeyCode::Convert => KeyCode::Convert, - winit::event::VirtualKeyCode::Decimal => KeyCode::Decimal, - winit::event::VirtualKeyCode::Divide => KeyCode::Divide, - winit::event::VirtualKeyCode::Equals => KeyCode::Equals, - winit::event::VirtualKeyCode::Grave => KeyCode::Grave, - winit::event::VirtualKeyCode::Kana => KeyCode::Kana, - winit::event::VirtualKeyCode::Kanji => KeyCode::Kanji, - winit::event::VirtualKeyCode::LAlt => KeyCode::LAlt, - winit::event::VirtualKeyCode::LBracket => KeyCode::LBracket, - winit::event::VirtualKeyCode::LControl => KeyCode::LControl, - winit::event::VirtualKeyCode::LShift => KeyCode::LShift, - winit::event::VirtualKeyCode::LWin => KeyCode::LWin, - winit::event::VirtualKeyCode::Mail => KeyCode::Mail, - winit::event::VirtualKeyCode::MediaSelect => KeyCode::MediaSelect, - winit::event::VirtualKeyCode::MediaStop => KeyCode::MediaStop, - winit::event::VirtualKeyCode::Minus => KeyCode::Minus, - winit::event::VirtualKeyCode::Multiply => KeyCode::Multiply, - winit::event::VirtualKeyCode::Mute => KeyCode::Mute, - winit::event::VirtualKeyCode::MyComputer => KeyCode::MyComputer, - winit::event::VirtualKeyCode::NavigateForward => { - KeyCode::NavigateForward - } - winit::event::VirtualKeyCode::NavigateBackward => { - KeyCode::NavigateBackward - } - winit::event::VirtualKeyCode::NextTrack => KeyCode::NextTrack, - winit::event::VirtualKeyCode::NoConvert => KeyCode::NoConvert, - winit::event::VirtualKeyCode::NumpadComma => KeyCode::NumpadComma, - winit::event::VirtualKeyCode::NumpadEnter => KeyCode::NumpadEnter, - winit::event::VirtualKeyCode::NumpadEquals => KeyCode::NumpadEquals, - winit::event::VirtualKeyCode::OEM102 => KeyCode::OEM102, - winit::event::VirtualKeyCode::Period => KeyCode::Period, - winit::event::VirtualKeyCode::PlayPause => KeyCode::PlayPause, - winit::event::VirtualKeyCode::Power => KeyCode::Power, - winit::event::VirtualKeyCode::PrevTrack => KeyCode::PrevTrack, - winit::event::VirtualKeyCode::RAlt => KeyCode::RAlt, - winit::event::VirtualKeyCode::RBracket => KeyCode::RBracket, - winit::event::VirtualKeyCode::RControl => KeyCode::RControl, - winit::event::VirtualKeyCode::RShift => KeyCode::RShift, - winit::event::VirtualKeyCode::RWin => KeyCode::RWin, - winit::event::VirtualKeyCode::Semicolon => KeyCode::Semicolon, - winit::event::VirtualKeyCode::Slash => KeyCode::Slash, - winit::event::VirtualKeyCode::Sleep => KeyCode::Sleep, - winit::event::VirtualKeyCode::Stop => KeyCode::Stop, - winit::event::VirtualKeyCode::Subtract => KeyCode::Subtract, - winit::event::VirtualKeyCode::Sysrq => KeyCode::Sysrq, - winit::event::VirtualKeyCode::Tab => KeyCode::Tab, - winit::event::VirtualKeyCode::Underline => KeyCode::Underline, - winit::event::VirtualKeyCode::Unlabeled => KeyCode::Unlabeled, - winit::event::VirtualKeyCode::VolumeDown => KeyCode::VolumeDown, - winit::event::VirtualKeyCode::VolumeUp => KeyCode::VolumeUp, - winit::event::VirtualKeyCode::Wake => KeyCode::Wake, - winit::event::VirtualKeyCode::WebBack => KeyCode::WebBack, - winit::event::VirtualKeyCode::WebFavorites => KeyCode::WebFavorites, - winit::event::VirtualKeyCode::WebForward => KeyCode::WebForward, - winit::event::VirtualKeyCode::WebHome => KeyCode::WebHome, - winit::event::VirtualKeyCode::WebRefresh => KeyCode::WebRefresh, - winit::event::VirtualKeyCode::WebSearch => KeyCode::WebSearch, - winit::event::VirtualKeyCode::WebStop => KeyCode::WebStop, - winit::event::VirtualKeyCode::Yen => KeyCode::Yen, - winit::event::VirtualKeyCode::Copy => KeyCode::Copy, - winit::event::VirtualKeyCode::Paste => KeyCode::Paste, - winit::event::VirtualKeyCode::Cut => KeyCode::Cut, - } - } -} diff --git a/src/input/mouse.rs b/src/input/mouse.rs deleted file mode 100644 index d37f5b96..00000000 --- a/src/input/mouse.rs +++ /dev/null @@ -1,6 +0,0 @@ -//! Build mouse events. -mod button; -mod event; - -pub use button::Button; -pub use event::Event; diff --git a/src/input/mouse/button.rs b/src/input/mouse/button.rs deleted file mode 100644 index 6320d701..00000000 --- a/src/input/mouse/button.rs +++ /dev/null @@ -1,32 +0,0 @@ -/// The button of a mouse. -/// -/// If you are using [`winit`], consider enabling the `winit` feature to get -/// conversion implementations for free! -/// -/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ -#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)] -pub enum Button { - /// The left mouse button. - Left, - - /// The right mouse button. - Right, - - /// The middle (wheel) button. - Middle, - - /// Some other button. - Other(u8), -} - -#[cfg(feature = "winit")] -impl From for super::Button { - fn from(mouse_button: winit::event::MouseButton) -> Self { - match mouse_button { - winit::event::MouseButton::Left => Button::Left, - winit::event::MouseButton::Right => Button::Right, - winit::event::MouseButton::Middle => Button::Middle, - winit::event::MouseButton::Other(other) => Button::Other(other), - } - } -} diff --git a/src/input/mouse/event.rs b/src/input/mouse/event.rs deleted file mode 100644 index 7b68208f..00000000 --- a/src/input/mouse/event.rs +++ /dev/null @@ -1,44 +0,0 @@ -use super::Button; -use crate::input::ButtonState; - -/// A mouse event. -/// -/// _**Note:** This type is largely incomplete! If you need to track -/// additional events, feel free to [open an issue] and share your use case!_ -/// -/// [open an issue]: https://github.com/hecrj/iced/issues -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum Event { - /// The mouse cursor entered the window. - CursorEntered, - - /// The mouse cursor left the window. - CursorLeft, - - /// The mouse cursor was moved - CursorMoved { - /// The X coordinate of the mouse position - x: f32, - - /// The Y coordinate of the mouse position - y: f32, - }, - - /// A mouse button was pressed or released. - Input { - /// The state of the button - state: ButtonState, - - /// The button identifier - button: Button, - }, - - /// The mouse wheel was scrolled. - WheelScrolled { - /// The number of horizontal lines scrolled - delta_x: f32, - - /// The number of vertical lines scrolled - delta_y: f32, - }, -} diff --git a/src/layout.rs b/src/layout.rs deleted file mode 100644 index de284a43..00000000 --- a/src/layout.rs +++ /dev/null @@ -1,62 +0,0 @@ -use stretch::result; - -use crate::{Point, Rectangle, Vector}; - -/// The computed bounds of a [`Node`] and its children. -/// -/// This type is provided by the GUI runtime to [`Widget::on_event`] and -/// [`Widget::draw`], describing the layout of the [`Node`] produced by -/// [`Widget::node`]. -/// -/// [`Node`]: struct.Node.html -/// [`Widget::on_event`]: widget/trait.Widget.html#method.on_event -/// [`Widget::draw`]: widget/trait.Widget.html#tymethod.draw -/// [`Widget::node`]: widget/trait.Widget.html#tymethod.node -#[derive(Debug)] -pub struct Layout<'a> { - layout: &'a result::Layout, - position: Point, -} - -impl<'a> Layout<'a> { - pub(crate) fn new(layout: &'a result::Layout) -> Self { - Self::with_parent_position(layout, Point::new(0.0, 0.0)) - } - - fn with_parent_position( - layout: &'a result::Layout, - parent_position: Point, - ) -> Self { - let position = - parent_position + Vector::new(layout.location.x, layout.location.y); - - Layout { layout, position } - } - - /// Gets the bounds of the [`Layout`]. - /// - /// The returned [`Rectangle`] describes the position and size of a - /// [`Node`]. - /// - /// [`Layout`]: struct.Layout.html - /// [`Rectangle`]: struct.Rectangle.html - /// [`Node`]: struct.Node.html - pub fn bounds(&self) -> Rectangle { - Rectangle { - x: self.position.x, - y: self.position.y, - width: self.layout.size.width, - height: self.layout.size.height, - } - } - - /// Returns an iterator over the [`Layout`] of the children of a [`Node`]. - /// - /// [`Layout`]: struct.Layout.html - /// [`Node`]: struct.Node.html - pub fn children(&'a self) -> impl Iterator> { - self.layout.children.iter().map(move |layout| { - Layout::with_parent_position(layout, self.position) - }) - } -} diff --git a/src/lib.rs b/src/lib.rs index 8dd6163e..e69de29b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,231 +0,0 @@ -//! Iced is a renderer-agnostic GUI library focused on simplicity and -//! type-safety. Inspired by [Elm]. -//! -//! # Features -//! * Simple, easy-to-use, renderer-agnostic API -//! * Responsive, flexbox-based layouting -//! * Type-safe, reactive programming model -//! * Built-in widgets -//! * Custom widget support -//! -//! Check out the [repository] and the [examples] for more details! -//! -//! [examples]: https://github.com/hecrj/iced/tree/0.1.0/examples -//! [repository]: https://github.com/hecrj/iced -//! -//! # Usage -//! Inspired by [The Elm Architecture], Iced expects you to split user interfaces -//! into four different concepts: -//! -//! * __State__ — the state of your application -//! * __Messages__ — user interactions or meaningful events that you care -//! about -//! * __View logic__ — a way to display your __state__ as widgets that -//! may produce __messages__ on user interaction -//! * __Update logic__ — a way to react to __messages__ and update your -//! __state__ -//! -//! We can build something to see how this works! Let's say we want a simple counter -//! that can be incremented and decremented using two buttons. -//! -//! We start by modelling the __state__ of our application: -//! -//! ``` -//! use iced::button; -//! -//! struct Counter { -//! // The counter value -//! value: i32, -//! -//! // The local state of the two buttons -//! increment_button: button::State, -//! decrement_button: button::State, -//! } -//! ``` -//! -//! Next, we need to define the possible user interactions of our counter: -//! the button presses. These interactions are our __messages__: -//! -//! ``` -//! #[derive(Debug, Clone, Copy)] -//! pub enum Message { -//! IncrementPressed, -//! DecrementPressed, -//! } -//! ``` -//! -//! Now, let's show the actual counter by putting it all together in our -//! __view logic__: -//! -//! ``` -//! # use iced::button; -//! # -//! # struct Counter { -//! # // The counter value -//! # value: i32, -//! # -//! # // The local state of the two buttons -//! # increment_button: button::State, -//! # decrement_button: button::State, -//! # } -//! # -//! # #[derive(Debug, Clone, Copy)] -//! # pub enum Message { -//! # IncrementPressed, -//! # DecrementPressed, -//! # } -//! # -//! # mod iced_wgpu { -//! # use iced::{ -//! # button, text, text::HorizontalAlignment, text::VerticalAlignment, -//! # MouseCursor, Node, Point, Rectangle, Style, Color -//! # }; -//! # -//! # pub struct Renderer {} -//! # -//! # impl button::Renderer for Renderer { -//! # fn draw( -//! # &mut self, -//! # _cursor_position: Point, -//! # _bounds: Rectangle, -//! # _state: &button::State, -//! # _label: &str, -//! # _class: button::Class, -//! # ) -> MouseCursor { -//! # MouseCursor::OutOfBounds -//! # } -//! # } -//! # -//! # impl text::Renderer for Renderer { -//! # fn node(&self, style: Style, _content: &str, _size: Option) -> Node { -//! # Node::new(style) -//! # } -//! # -//! # fn draw( -//! # &mut self, -//! # _bounds: Rectangle, -//! # _content: &str, -//! # _size: Option, -//! # _color: Option, -//! # _horizontal_alignment: HorizontalAlignment, -//! # _vertical_alignment: VerticalAlignment, -//! # ) { -//! # } -//! # } -//! # } -//! use iced::{Button, Column, Text}; -//! use iced_wgpu::Renderer; // Iced does not include a renderer! We need to bring our own! -//! -//! impl Counter { -//! pub fn view(&mut self) -> Column { -//! // We use a column: a simple vertical layout -//! Column::new() -//! .push( -//! // The increment button. We tell it to produce an -//! // `IncrementPressed` message when pressed -//! Button::new(&mut self.increment_button, "+") -//! .on_press(Message::IncrementPressed), -//! ) -//! .push( -//! // We show the value of the counter here -//! Text::new(&self.value.to_string()).size(50), -//! ) -//! .push( -//! // The decrement button. We tell it to produce a -//! // `DecrementPressed` message when pressed -//! Button::new(&mut self.decrement_button, "-") -//! .on_press(Message::DecrementPressed), -//! ) -//! } -//! } -//! ``` -//! -//! Finally, we need to be able to react to any produced __messages__ and change -//! our __state__ accordingly in our __update logic__: -//! -//! ``` -//! # use iced::button; -//! # -//! # struct Counter { -//! # // The counter value -//! # value: i32, -//! # -//! # // The local state of the two buttons -//! # increment_button: button::State, -//! # decrement_button: button::State, -//! # } -//! # -//! # #[derive(Debug, Clone, Copy)] -//! # pub enum Message { -//! # IncrementPressed, -//! # DecrementPressed, -//! # } -//! impl Counter { -//! // ... -//! -//! pub fn update(&mut self, message: Message) { -//! match message { -//! Message::IncrementPressed => { -//! self.value += 1; -//! } -//! Message::DecrementPressed => { -//! self.value -= 1; -//! } -//! } -//! } -//! } -//! ``` -//! -//! And that's everything! We just wrote a whole user interface. Iced is now able -//! to: -//! -//! 1. Take the result of our __view logic__ and layout its widgets. -//! 1. Process events from our system and produce __messages__ for our -//! __update logic__. -//! 1. Draw the resulting user interface using our chosen __renderer__. -//! -//! Check out the [`UserInterface`] type to learn how to wire everything up! -//! -//! [Elm]: https://elm-lang.org/ -//! [The Elm Architecture]: https://guide.elm-lang.org/architecture/ -//! [documentation]: https://docs.rs/iced -//! [examples]: https://github.com/hecrj/iced/tree/master/examples -//! [`UserInterface`]: struct.UserInterface.html -#![deny(missing_docs)] -#![deny(missing_debug_implementations)] -#![deny(unused_results)] -#![deny(unsafe_code)] -#![deny(rust_2018_idioms)] -pub mod input; -pub mod renderer; -pub mod widget; - -mod color; -mod element; -mod event; -mod hasher; -mod layout; -mod mouse_cursor; -mod node; -mod point; -mod rectangle; -mod style; -mod user_interface; -mod vector; - -#[doc(no_inline)] -pub use stretch::{geometry::Size, number::Number}; - -pub use color::Color; -pub use element::Element; -pub use event::Event; -pub use hasher::Hasher; -pub use layout::Layout; -pub use mouse_cursor::MouseCursor; -pub use node::Node; -pub use point::Point; -pub use rectangle::Rectangle; -pub use style::{Align, Justify, Style}; -pub use user_interface::{Cache, UserInterface}; -pub(crate) use vector::Vector; -pub use widget::*; diff --git a/src/mouse_cursor.rs b/src/mouse_cursor.rs deleted file mode 100644 index 4ef6361a..00000000 --- a/src/mouse_cursor.rs +++ /dev/null @@ -1,35 +0,0 @@ -/// The state of the mouse cursor. -#[derive(Debug, Eq, PartialEq, Clone, Copy)] -pub enum MouseCursor { - /// The cursor is out of the bounds of the user interface. - OutOfBounds, - - /// The cursor is over a non-interactive widget. - Idle, - - /// The cursor is over a clickable widget. - Pointer, - - /// The cursor is over a busy widget. - Working, - - /// The cursor is over a grabbable widget. - Grab, - - /// The cursor is grabbing a widget. - Grabbing, -} - -#[cfg(feature = "winit")] -impl From for winit::window::CursorIcon { - fn from(mouse_cursor: MouseCursor) -> winit::window::CursorIcon { - match mouse_cursor { - MouseCursor::OutOfBounds => winit::window::CursorIcon::Default, - MouseCursor::Idle => winit::window::CursorIcon::Default, - MouseCursor::Pointer => winit::window::CursorIcon::Hand, - MouseCursor::Working => winit::window::CursorIcon::Progress, - MouseCursor::Grab => winit::window::CursorIcon::Grab, - MouseCursor::Grabbing => winit::window::CursorIcon::Grabbing, - } - } -} diff --git a/src/node.rs b/src/node.rs deleted file mode 100644 index 1db10d7f..00000000 --- a/src/node.rs +++ /dev/null @@ -1,60 +0,0 @@ -use stretch::node; - -use crate::{Number, Size, Style}; - -/// The visual requirements of a [`Widget`] and its children. -/// -/// When there have been changes and the [`Layout`] needs to be recomputed, the -/// runtime obtains a [`Node`] by calling [`Widget::node`]. -/// -/// [`Style`]: struct.Style.html -/// [`Widget`]: widget/trait.Widget.html -/// [`Node`]: struct.Node.html -/// [`Widget::node`]: widget/trait.Widget.html#tymethod.node -/// [`Layout`]: struct.Layout.html -#[derive(Debug)] -pub struct Node(pub(crate) node::Node); - -impl Node { - /// Creates a new [`Node`] with the given [`Style`]. - /// - /// [`Node`]: struct.Node.html - /// [`Style`]: struct.Style.html - pub fn new(style: Style) -> Node { - Self::with_children(style, Vec::new()) - } - - /// Creates a new [`Node`] with the given [`Style`] and a measure function. - /// - /// This type of node cannot have any children. - /// - /// You should use this when your [`Widget`] can adapt its contents to the - /// size of its container. The measure function will receive the container - /// size as a parameter and must compute the size of the [`Node`] inside - /// the given bounds (if the `Number` for a dimension is `Undefined` it - /// means that it has no boundary). - /// - /// [`Node`]: struct.Node.html - /// [`Style`]: struct.Style.html - /// [`Widget`]: widget/trait.Widget.html - pub fn with_measure(style: Style, measure: F) -> Node - where - F: 'static + Fn(Size) -> Size, - { - Node(node::Node::new_leaf( - style.0, - Box::new(move |size| Ok(measure(size))), - )) - } - - /// Creates a new [`Node`] with the given [`Style`] and children. - /// - /// [`Node`]: struct.Node.html - /// [`Style`]: struct.Style.html - pub fn with_children(style: Style, children: Vec) -> Node { - Node(node::Node::new( - style.0, - children.iter().map(|c| &c.0).collect(), - )) - } -} diff --git a/src/point.rs b/src/point.rs deleted file mode 100644 index 183998dd..00000000 --- a/src/point.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::Vector; - -/// A 2D point. -#[derive(Debug, Clone, Copy, PartialEq)] -pub struct Point { - /// The X coordinate. - pub x: f32, - - /// The Y coordinate. - pub y: f32, -} - -impl Point { - /// Creates a new [`Point`] with the given coordinates. - /// - /// [`Point`]: struct.Point.html - pub fn new(x: f32, y: f32) -> Self { - Self { x, y } - } -} - -impl std::ops::Add for Point { - type Output = Self; - - fn add(self, vector: Vector) -> Self { - Self { - x: self.x + vector.x, - y: self.y + vector.y, - } - } -} diff --git a/src/rectangle.rs b/src/rectangle.rs deleted file mode 100644 index 95c2570c..00000000 --- a/src/rectangle.rs +++ /dev/null @@ -1,30 +0,0 @@ -use crate::Point; - -/// A rectangle. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub struct Rectangle { - /// X coordinate of the top-left corner. - pub x: T, - - /// Y coordinate of the top-left corner. - pub y: T, - - /// Width of the rectangle. - pub width: T, - - /// Height of the rectangle. - pub height: T, -} - -impl Rectangle { - /// Returns true if the given [`Point`] is contained in the [`Rectangle`]. - /// - /// [`Point`]: struct.Point.html - /// [`Rectangle`]: struct.Rectangle.html - pub fn contains(&self, point: Point) -> bool { - self.x <= point.x - && point.x <= self.x + self.width - && self.y <= point.y - && point.y <= self.y + self.height - } -} diff --git a/src/renderer.rs b/src/renderer.rs deleted file mode 100644 index 2244f00b..00000000 --- a/src/renderer.rs +++ /dev/null @@ -1,38 +0,0 @@ -//! Write your own renderer. -//! -//! There is not a common entrypoint or trait for a __renderer__ in Iced. -//! Instead, every [`Widget`] constrains its generic `Renderer` type as -//! necessary. -//! -//! This approach is flexible and composable. For instance, the -//! [`Text`] widget only needs a [`text::Renderer`] while a [`Checkbox`] widget -//! needs both a [`text::Renderer`] and a [`checkbox::Renderer`], reusing logic. -//! -//! In the end, a __renderer__ satisfying all the constraints is -//! needed to build a [`UserInterface`]. -//! -//! [`Widget`]: ../widget/trait.Widget.html -//! [`UserInterface`]: ../struct.UserInterface.html -//! [`Text`]: ../widget/text/struct.Text.html -//! [`text::Renderer`]: ../widget/text/trait.Renderer.html -//! [`Checkbox`]: ../widget/checkbox/struct.Checkbox.html -//! [`checkbox::Renderer`]: ../widget/checkbox/trait.Renderer.html -use crate::{Color, Layout}; - -/// A renderer able to graphically explain a [`Layout`]. -/// -/// [`Layout`]: ../struct.Layout.html -pub trait Debugger { - /// Explains the [`Layout`] of an [`Element`] for debugging purposes. - /// - /// This will be called when [`Element::explain`] has been used. It should - /// _explain_ the given [`Layout`] graphically. - /// - /// A common approach consists in recursively rendering the bounds of the - /// [`Layout`] and its children. - /// - /// [`Layout`]: struct.Layout.html - /// [`Element`]: struct.Element.html - /// [`Element::explain`]: struct.Element.html#method.explain - fn explain(&mut self, layout: &Layout<'_>, color: Color); -} diff --git a/src/style.rs b/src/style.rs deleted file mode 100644 index 575ea366..00000000 --- a/src/style.rs +++ /dev/null @@ -1,262 +0,0 @@ -use std::hash::{Hash, Hasher}; -use stretch::{geometry, style}; - -/// The appearance of a [`Node`]. -/// -/// [`Node`]: struct.Node.html -#[derive(Debug, Clone, Copy)] -pub struct Style(pub(crate) style::Style); - -impl Style { - /// Defines the width of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn width(mut self, width: u16) -> Self { - self.0.size.width = style::Dimension::Points(width as f32); - self - } - - /// Defines the height of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn height(mut self, height: u16) -> Self { - self.0.size.height = style::Dimension::Points(height as f32); - self - } - - /// Defines the minimum width of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn min_width(mut self, min_width: u16) -> Self { - self.0.min_size.width = style::Dimension::Points(min_width as f32); - self - } - - /// Defines the maximum width of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn max_width(mut self, max_width: u16) -> Self { - self.0.max_size.width = style::Dimension::Points(max_width as f32); - self.fill_width() - } - - /// Defines the minimum height of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn min_height(mut self, min_height: u16) -> Self { - self.0.min_size.height = - style::Dimension::Points(f32::from(min_height)); - self - } - - /// Defines the maximum height of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn max_height(mut self, max_height: u16) -> Self { - self.0.max_size.height = - style::Dimension::Points(f32::from(max_height)); - self.fill_height() - } - - /// Makes a [`Node`] fill all the horizontal available space. - /// - /// [`Node`]: struct.Node.html - pub fn fill_width(mut self) -> Self { - self.0.size.width = stretch::style::Dimension::Percent(1.0); - self - } - - /// Makes a [`Node`] fill all the vertical available space. - /// - /// [`Node`]: struct.Node.html - pub fn fill_height(mut self) -> Self { - self.0.size.height = stretch::style::Dimension::Percent(1.0); - self - } - - pub(crate) fn align_items(mut self, align: Align) -> Self { - self.0.align_items = align.into(); - self - } - - pub(crate) fn justify_content(mut self, justify: Justify) -> Self { - self.0.justify_content = justify.into(); - self - } - - /// Sets the alignment of a [`Node`]. - /// - /// If the [`Node`] is inside a... - /// - /// * [`Column`], this setting will affect its __horizontal__ alignment. - /// * [`Row`], this setting will affect its __vertical__ alignment. - /// - /// [`Node`]: struct.Node.html - /// [`Column`]: widget/struct.Column.html - /// [`Row`]: widget/struct.Row.html - pub fn align_self(mut self, align: Align) -> Self { - self.0.align_self = align.into(); - self - } - - /// Sets the padding of a [`Node`] in pixels. - /// - /// [`Node`]: struct.Node.html - pub fn padding(mut self, px: u16) -> Self { - self.0.padding = stretch::geometry::Rect { - start: style::Dimension::Points(px as f32), - end: style::Dimension::Points(px as f32), - top: style::Dimension::Points(px as f32), - bottom: style::Dimension::Points(px as f32), - }; - - self - } -} - -impl Default for Style { - fn default() -> Style { - Style(style::Style { - align_items: style::AlignItems::FlexStart, - justify_content: style::JustifyContent::FlexStart, - ..style::Style::default() - }) - } -} - -impl Hash for Style { - fn hash(&self, state: &mut H) { - hash_size(&self.0.size, state); - hash_size(&self.0.min_size, state); - hash_size(&self.0.max_size, state); - - hash_rect(&self.0.margin, state); - - (self.0.flex_direction as u8).hash(state); - (self.0.align_items as u8).hash(state); - (self.0.justify_content as u8).hash(state); - (self.0.align_self as u8).hash(state); - (self.0.flex_grow as u32).hash(state); - } -} - -fn hash_size( - size: &geometry::Size, - state: &mut H, -) { - hash_dimension(size.width, state); - hash_dimension(size.height, state); -} - -fn hash_rect( - rect: &geometry::Rect, - state: &mut H, -) { - hash_dimension(rect.start, state); - hash_dimension(rect.end, state); - hash_dimension(rect.top, state); - hash_dimension(rect.bottom, state); -} - -fn hash_dimension(dimension: style::Dimension, state: &mut H) { - match dimension { - style::Dimension::Undefined => state.write_u8(0), - style::Dimension::Auto => state.write_u8(1), - style::Dimension::Points(points) => { - state.write_u8(2); - (points as u32).hash(state); - } - style::Dimension::Percent(percent) => { - state.write_u8(3); - (percent as u32).hash(state); - } - } -} - -/// Alignment on the cross axis of a container. -/// -/// * On a [`Column`], it describes __horizontal__ alignment. -/// * On a [`Row`], it describes __vertical__ alignment. -/// -/// [`Column`]: widget/struct.Column.html -/// [`Row`]: widget/struct.Row.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Align { - /// Align at the start of the cross axis. - Start, - - /// Align at the center of the cross axis. - Center, - - /// Align at the end of the cross axis. - End, - - /// Stretch over the cross axis. - Stretch, -} - -#[doc(hidden)] -impl From for style::AlignItems { - fn from(align: Align) -> Self { - match align { - Align::Start => style::AlignItems::FlexStart, - Align::Center => style::AlignItems::Center, - Align::End => style::AlignItems::FlexEnd, - Align::Stretch => style::AlignItems::Stretch, - } - } -} - -#[doc(hidden)] -impl From for style::AlignSelf { - fn from(align: Align) -> Self { - match align { - Align::Start => style::AlignSelf::FlexStart, - Align::Center => style::AlignSelf::Center, - Align::End => style::AlignSelf::FlexEnd, - Align::Stretch => style::AlignSelf::Stretch, - } - } -} - -/// Distribution on the main axis of a container. -/// -/// * On a [`Column`], it describes __vertical__ distribution. -/// * On a [`Row`], it describes __horizontal__ distribution. -/// -/// [`Column`]: widget/struct.Column.html -/// [`Row`]: widget/struct.Row.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Justify { - /// Place items at the start of the main axis. - Start, - - /// Place items at the center of the main axis. - Center, - - /// Place items at the end of the main axis. - End, - - /// Place items with space between. - SpaceBetween, - - /// Place items with space around. - SpaceAround, - - /// Place items with evenly distributed space. - SpaceEvenly, -} - -#[doc(hidden)] -impl From for style::JustifyContent { - fn from(justify: Justify) -> Self { - match justify { - Justify::Start => style::JustifyContent::FlexStart, - Justify::Center => style::JustifyContent::Center, - Justify::End => style::JustifyContent::FlexEnd, - Justify::SpaceBetween => style::JustifyContent::SpaceBetween, - Justify::SpaceAround => style::JustifyContent::SpaceAround, - Justify::SpaceEvenly => style::JustifyContent::SpaceEvenly, - } - } -} diff --git a/src/user_interface.rs b/src/user_interface.rs deleted file mode 100644 index 6a69f81a..00000000 --- a/src/user_interface.rs +++ /dev/null @@ -1,323 +0,0 @@ -use crate::{input::mouse, Column, Element, Event, Layout, MouseCursor, Point}; - -use std::hash::Hasher; -use stretch::result; - -/// A set of interactive graphical elements with a specific [`Layout`]. -/// -/// It can be updated and drawn. -/// -/// Iced tries to avoid dictating how to write your event loop. You are in -/// charge of using this type in your system in any way you want. -/// -/// [`Layout`]: struct.Layout.html -#[derive(Debug)] -pub struct UserInterface<'a, Message, Renderer> { - hash: u64, - root: Element<'a, Message, Renderer>, - layout: result::Layout, - cursor_position: Point, -} - -impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> { - /// Builds a user interface for an [`Element`]. - /// - /// It is able to avoid expensive computations when using a [`Cache`] - /// obtained from a previous instance of a [`UserInterface`]. - /// - /// [`Element`]: struct.Element.html - /// [`Cache`]: struct.Cache.html - /// [`UserInterface`]: struct.UserInterface.html - /// - /// # Example - /// Imagine we want to build a [`UserInterface`] for - /// [the counter example that we previously wrote](index.html#usage). Here - /// is naive way to set up our application loop: - /// - /// ```no_run - /// use iced::{UserInterface, Cache}; - /// use iced_wgpu::Renderer; - /// - /// # mod iced_wgpu { - /// # pub struct Renderer; - /// # - /// # impl Renderer { - /// # pub fn new() -> Self { Renderer } - /// # } - /// # } - /// # - /// # use iced::Column; - /// # - /// # pub struct Counter; - /// # - /// # impl Counter { - /// # pub fn new() -> Self { Counter } - /// # pub fn view(&self) -> Column<(), Renderer> { - /// # Column::new() - /// # } - /// # } - /// // Initialization - /// let mut counter = Counter::new(); - /// let mut cache = Cache::new(); - /// let mut renderer = Renderer::new(); - /// - /// // Application loop - /// loop { - /// // Process system events here... - /// - /// // Build the user interface - /// let user_interface = UserInterface::build( - /// counter.view(), - /// cache, - /// &mut renderer, - /// ); - /// - /// // Update and draw the user interface here... - /// // ... - /// - /// // Obtain the cache for the next iteration - /// cache = user_interface.into_cache(); - /// } - /// ``` - pub fn build>>( - root: E, - cache: Cache, - renderer: &mut Renderer, - ) -> Self { - let root = root.into(); - - let hasher = &mut crate::Hasher::default(); - root.hash_layout(hasher); - - let hash = hasher.finish(); - - let layout = if hash == cache.hash { - cache.layout - } else { - root.compute_layout(renderer) - }; - - UserInterface { - hash, - root, - layout, - cursor_position: cache.cursor_position, - } - } - - /// Updates the [`UserInterface`] by processing each provided [`Event`]. - /// - /// It returns __messages__ that may have been produced as a result of user - /// interactions. You should feed these to your __update logic__. - /// - /// [`UserInterface`]: struct.UserInterface.html - /// [`Event`]: enum.Event.html - /// - /// # Example - /// Let's allow our [counter](index.html#usage) to change state by completing - /// [the previous example](#example): - /// - /// ```no_run - /// use iced::{UserInterface, Cache}; - /// use iced_wgpu::Renderer; - /// - /// # mod iced_wgpu { - /// # pub struct Renderer; - /// # - /// # impl Renderer { - /// # pub fn new() -> Self { Renderer } - /// # } - /// # } - /// # - /// # use iced::Column; - /// # - /// # pub struct Counter; - /// # - /// # impl Counter { - /// # pub fn new() -> Self { Counter } - /// # pub fn view(&self) -> Column<(), Renderer> { - /// # Column::new() - /// # } - /// # pub fn update(&mut self, message: ()) {} - /// # } - /// let mut counter = Counter::new(); - /// let mut cache = Cache::new(); - /// let mut renderer = Renderer::new(); - /// - /// // Initialize our event storage - /// let mut events = Vec::new(); - /// - /// loop { - /// // Process system events... - /// - /// let mut user_interface = UserInterface::build( - /// counter.view(), - /// cache, - /// &mut renderer, - /// ); - /// - /// // Update the user interface - /// let messages = user_interface.update(events.drain(..)); - /// - /// cache = user_interface.into_cache(); - /// - /// // Process the produced messages - /// for message in messages { - /// counter.update(message); - /// } - /// } - /// ``` - pub fn update( - &mut self, - events: impl Iterator, - ) -> Vec { - let mut messages = Vec::new(); - - for event in events { - if let Event::Mouse(mouse::Event::CursorMoved { x, y }) = event { - self.cursor_position = Point::new(x, y); - } - - self.root.widget.on_event( - event, - Layout::new(&self.layout), - self.cursor_position, - &mut messages, - ); - } - - messages - } - - /// Draws the [`UserInterface`] with the provided [`Renderer`]. - /// - /// It returns the current state of the [`MouseCursor`]. You should update - /// the icon of the mouse cursor accordingly in your system. - /// - /// [`UserInterface`]: struct.UserInterface.html - /// [`Renderer`]: trait.Renderer.html - /// [`MouseCursor`]: enum.MouseCursor.html - /// - /// # Example - /// We can finally draw our [counter](index.html#usage) by - /// [completing the last example](#example-1): - /// - /// ```no_run - /// use iced::{UserInterface, Cache}; - /// use iced_wgpu::Renderer; - /// - /// # mod iced_wgpu { - /// # pub struct Renderer; - /// # - /// # impl Renderer { - /// # pub fn new() -> Self { Renderer } - /// # } - /// # } - /// # - /// # use iced::Column; - /// # - /// # pub struct Counter; - /// # - /// # impl Counter { - /// # pub fn new() -> Self { Counter } - /// # pub fn view(&self) -> Column<(), Renderer> { - /// # Column::new() - /// # } - /// # pub fn update(&mut self, message: ()) {} - /// # } - /// let mut counter = Counter::new(); - /// let mut cache = Cache::new(); - /// let mut renderer = Renderer::new(); - /// let mut events = Vec::new(); - /// - /// loop { - /// // Process system events... - /// - /// let mut user_interface = UserInterface::build( - /// counter.view(), - /// cache, - /// &mut renderer, - /// ); - /// - /// let messages = user_interface.update(events.drain(..)); - /// - /// // Draw the user interface - /// let mouse_cursor = user_interface.draw(&mut renderer); - /// - /// cache = user_interface.into_cache(); - /// - /// for message in messages { - /// counter.update(message); - /// } - /// - /// // Update mouse cursor icon... - /// // Flush rendering operations... - /// } - /// ``` - pub fn draw(&self, renderer: &mut Renderer) -> MouseCursor { - self.root.widget.draw( - renderer, - Layout::new(&self.layout), - self.cursor_position, - ) - } - - /// Extract the [`Cache`] of the [`UserInterface`], consuming it in the - /// process. - /// - /// [`Cache`]: struct.Cache.html - /// [`UserInterface`]: struct.UserInterface.html - pub fn into_cache(self) -> Cache { - Cache { - hash: self.hash, - layout: self.layout, - cursor_position: self.cursor_position, - } - } -} - -/// Reusable data of a specific [`UserInterface`]. -/// -/// [`UserInterface`]: struct.UserInterface.html -#[derive(Debug, Clone)] -pub struct Cache { - hash: u64, - layout: result::Layout, - cursor_position: Point, -} - -impl Cache { - /// Creates an empty [`Cache`]. - /// - /// You should use this to initialize a [`Cache`] before building your first - /// [`UserInterface`]. - /// - /// [`Cache`]: struct.Cache.html - /// [`UserInterface`]: struct.UserInterface.html - pub fn new() -> Cache { - let root: Element<'_, (), ()> = Column::new().into(); - - let hasher = &mut crate::Hasher::default(); - root.hash_layout(hasher); - - Cache { - hash: hasher.finish(), - layout: root.compute_layout(&mut ()), - cursor_position: Point::new(0.0, 0.0), - } - } -} - -impl Default for Cache { - fn default() -> Cache { - Cache::new() - } -} - -impl PartialEq for Cache { - fn eq(&self, other: &Cache) -> bool { - self.hash == other.hash && self.cursor_position == other.cursor_position - } -} - -impl Eq for Cache {} diff --git a/src/vector.rs b/src/vector.rs deleted file mode 100644 index f45daab9..00000000 --- a/src/vector.rs +++ /dev/null @@ -1,15 +0,0 @@ -/// A 2D vector. -#[derive(Debug, Clone, Copy, PartialEq)] -pub struct Vector { - pub x: f32, - pub y: f32, -} - -impl Vector { - /// Creates a new [`Vector`] with the given components. - /// - /// [`Vector`]: struct.Vector.html - pub fn new(x: f32, y: f32) -> Self { - Self { x, y } - } -} diff --git a/src/widget.rs b/src/widget.rs deleted file mode 100644 index 45451f47..00000000 --- a/src/widget.rs +++ /dev/null @@ -1,114 +0,0 @@ -//! Use the built-in widgets or create your own. -//! -//! # Built-in widgets -//! Every built-in drawable widget has its own module with a `Renderer` trait -//! that must be implemented by a [renderer] before being able to use it as -//! a [`Widget`]. -//! -//! # Custom widgets -//! If you want to implement a custom widget, you simply need to implement the -//! [`Widget`] trait. You can use the API of the built-in widgets as a guide or -//! source of inspiration. -//! -//! # Re-exports -//! For convenience, the contents of this module are available at the root -//! module. Therefore, you can directly type: -//! -//! ``` -//! use iced::{button, Button, Widget}; -//! ``` -//! -//! [`Widget`]: trait.Widget.html -//! [renderer]: ../renderer/index.html -mod column; -mod row; - -pub mod button; -pub mod checkbox; -pub mod image; -//pub mod progress_bar; -pub mod radio; -pub mod slider; -pub mod text; - -pub use button::Button; -pub use checkbox::Checkbox; -pub use column::Column; -pub use image::Image; -//pub use progress_bar::ProgressBar; -pub use radio::Radio; -pub use row::Row; -pub use slider::Slider; -pub use text::Text; - -use crate::{Event, Hasher, Layout, MouseCursor, Node, Point}; - -/// A component that displays information and allows interaction. -/// -/// If you want to build your own widgets, you will need to implement this -/// trait. -/// -/// [`Widget`]: trait.Widget.html -/// [`Element`]: ../struct.Element.html -pub trait Widget: std::fmt::Debug { - /// Returns the [`Node`] of the [`Widget`]. - /// - /// This [`Node`] is used by the runtime to compute the [`Layout`] of the - /// user interface. - /// - /// [`Node`]: ../struct.Node.html - /// [`Widget`]: trait.Widget.html - /// [`Layout`]: ../struct.Layout.html - fn node(&self, renderer: &mut Renderer) -> Node; - - /// Draws the [`Widget`] using the associated `Renderer`. - /// - /// It must return the [`MouseCursor`] state for the [`Widget`]. - /// - /// [`Widget`]: trait.Widget.html - /// [`MouseCursor`]: ../enum.MouseCursor.html - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor; - - /// Computes the _layout_ hash of the [`Widget`]. - /// - /// The produced hash is used by the runtime to decide if the [`Layout`] - /// needs to be recomputed between frames. Therefore, to ensure maximum - /// efficiency, the hash should only be affected by the properties of the - /// [`Widget`] that can affect layouting. - /// - /// For example, the [`Text`] widget does not hash its color property, as - /// its value cannot affect the overall [`Layout`] of the user interface. - /// - /// [`Widget`]: trait.Widget.html - /// [`Layout`]: ../struct.Layout.html - /// [`Text`]: text/struct.Text.html - fn hash_layout(&self, state: &mut Hasher); - - /// Processes a runtime [`Event`]. - /// - /// It receives: - /// * an [`Event`] describing user interaction - /// * the computed [`Layout`] of the [`Widget`] - /// * the current cursor position - /// * a mutable `Message` list, allowing the [`Widget`] to produce - /// new messages based on user interaction. - /// - /// By default, it does nothing. - /// - /// [`Event`]: ../enum.Event.html - /// [`Widget`]: trait.Widget.html - /// [`Layout`]: ../struct.Layout.html - fn on_event( - &mut self, - _event: Event, - _layout: Layout<'_>, - _cursor_position: Point, - _messages: &mut Vec, - ) { - } -} diff --git a/src/widget/button.rs b/src/widget/button.rs deleted file mode 100644 index d2ea70e4..00000000 --- a/src/widget/button.rs +++ /dev/null @@ -1,284 +0,0 @@ -//! Allow your users to perform actions by pressing a button. -//! -//! A [`Button`] has some local [`State`] and a [`Class`]. -//! -//! [`Button`]: struct.Button.html -//! [`State`]: struct.State.html -//! [`Class`]: enum.Class.html - -use crate::input::{mouse, ButtonState}; -use crate::{ - Align, Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, - Style, Widget, -}; - -use std::hash::Hash; - -/// A generic widget that produces a message when clicked. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`button::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`button::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::{button, Button}; -/// -/// pub enum Message { -/// ButtonClicked, -/// } -/// -/// let state = &mut button::State::new(); -/// -/// Button::new(state, "Click me!") -/// .on_press(Message::ButtonClicked); -/// ``` -/// -/// ![Button drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/button.png?raw=true) -pub struct Button<'a, Message> { - state: &'a mut State, - /// The label of the button. - pub label: String, - class: Class, - /// The message to produce when the button is pressed - pub on_press: Option, - style: Style, -} - -impl<'a, Message> std::fmt::Debug for Button<'a, Message> -where - Message: std::fmt::Debug, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Button") - .field("state", &self.state) - .field("label", &self.label) - .field("class", &self.class) - .field("on_press", &self.on_press) - .field("style", &self.style) - .finish() - } -} - -impl<'a, Message> Button<'a, Message> { - /// Creates a new [`Button`] with some local [`State`] and the given label. - /// - /// The default [`Class`] of a new [`Button`] is [`Class::Primary`]. - /// - /// [`Button`]: struct.Button.html - /// [`State`]: struct.State.html - /// [`Class`]: enum.Class.html - /// [`Class::Primary`]: enum.Class.html#variant.Primary - pub fn new(state: &'a mut State, label: &str) -> Self { - Button { - state, - label: String::from(label), - class: Class::Primary, - on_press: None, - style: Style::default().min_width(100), - } - } - - /// Sets the width of the [`Button`] in pixels. - /// - /// [`Button`]: struct.Button.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Makes the [`Button`] fill the horizontal space of its container. - /// - /// [`Button`]: struct.Button.html - pub fn fill_width(mut self) -> Self { - self.style = self.style.fill_width(); - self - } - - /// Sets the alignment of the [`Button`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Button`]: struct.Button.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } - - /// Sets the [`Class`] of the [`Button`]. - /// - /// - /// [`Button`]: struct.Button.html - /// [`Class`]: enum.Class.html - pub fn class(mut self, class: Class) -> Self { - self.class = class; - self - } - - /// Sets the message that will be produced when the [`Button`] is pressed. - /// - /// [`Button`]: struct.Button.html - pub fn on_press(mut self, msg: Message) -> Self { - self.on_press = Some(msg); - self - } -} - -impl<'a, Message, Renderer> Widget for Button<'a, Message> -where - Renderer: self::Renderer, - Message: Copy + std::fmt::Debug, -{ - fn node(&self, _renderer: &mut Renderer) -> Node { - Node::new(self.style.height(50)) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state, - }) => { - if let Some(on_press) = self.on_press { - let bounds = layout.bounds(); - - match state { - ButtonState::Pressed => { - self.state.is_pressed = - bounds.contains(cursor_position); - } - ButtonState::Released => { - let is_clicked = self.state.is_pressed - && bounds.contains(cursor_position); - - self.state.is_pressed = false; - - if is_clicked { - messages.push(on_press); - } - } - } - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - cursor_position, - layout.bounds(), - self.state, - &self.label, - self.class, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -/// The local state of a [`Button`]. -/// -/// [`Button`]: struct.Button.html -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub struct State { - is_pressed: bool, -} - -impl State { - /// Creates a new [`State`]. - /// - /// [`State`]: struct.State.html - pub fn new() -> State { - State::default() - } - - /// Returns whether the associated [`Button`] is currently being pressed or - /// not. - /// - /// [`Button`]: struct.Button.html - pub fn is_pressed(&self) -> bool { - self.is_pressed - } -} - -/// The type of a [`Button`]. -/// -/// ![Different buttons drawn by the built-in renderer in Coffee](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/button_classes.png?raw=true) -/// -/// [`Button`]: struct.Button.html -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Class { - /// The [`Button`] performs the main action. - /// - /// [`Button`]: struct.Button.html - Primary, - - /// The [`Button`] performs an alternative action. - /// - /// [`Button`]: struct.Button.html - Secondary, - - /// The [`Button`] performs a productive action. - /// - /// [`Button`]: struct.Button.html - Positive, -} - -/// The renderer of a [`Button`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Button`] in your user interface. -/// -/// [`Button`]: struct.Button.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Button`]. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Button`] - /// * the local state of the [`Button`] - /// * the label of the [`Button`] - /// * the [`Class`] of the [`Button`] - /// - /// [`Button`]: struct.Button.html - /// [`State`]: struct.State.html - /// [`Class`]: enum.Class.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - state: &State, - label: &str, - class: Class, - ) -> MouseCursor; -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, - Message: 'static + Copy + std::fmt::Debug, -{ - fn from(button: Button<'a, Message>) -> Element<'a, Message, Renderer> { - Element::new(button) - } -} diff --git a/src/widget/checkbox.rs b/src/widget/checkbox.rs deleted file mode 100644 index 48400fae..00000000 --- a/src/widget/checkbox.rs +++ /dev/null @@ -1,199 +0,0 @@ -//! Show toggle controls using checkboxes. -use std::hash::Hash; - -use crate::input::{mouse, ButtonState}; -use crate::widget::{text, Column, Row, Text}; -use crate::{ - Align, Color, Element, Event, Hasher, Layout, MouseCursor, Node, Point, - Rectangle, Widget, -}; - -/// A box that can be checked. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`checkbox::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`checkbox::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::{Checkbox, Color}; -/// -/// pub enum Message { -/// CheckboxToggled(bool), -/// } -/// -/// let is_checked = true; -/// -/// Checkbox::new(is_checked, "Toggle me!", Message::CheckboxToggled); -/// ``` -/// -/// ![Checkbox drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/checkbox.png?raw=true) -pub struct Checkbox { - /// Whether the checkbox is checked or not - pub is_checked: bool, - - /// Function to call when checkbox is toggled to produce a __message__. - /// - /// The function should be provided `true` when the checkbox is checked - /// and `false` otherwise. - pub on_toggle: Box Message>, - - /// The label of the checkbox - pub label: String, - - /// The color of the label - pub label_color: Option, -} - -impl std::fmt::Debug for Checkbox { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Checkbox") - .field("is_checked", &self.is_checked) - .field("label", &self.label) - .field("label_color", &self.label_color) - .finish() - } -} - -impl Checkbox { - /// Creates a new [`Checkbox`]. - /// - /// It expects: - /// * a boolean describing whether the [`Checkbox`] is checked or not - /// * the label of the [`Checkbox`] - /// * a function that will be called when the [`Checkbox`] is toggled. - /// It will receive the new state of the [`Checkbox`] and must produce - /// a `Message`. - /// - /// [`Checkbox`]: struct.Checkbox.html - pub fn new(is_checked: bool, label: &str, f: F) -> Self - where - F: 'static + Fn(bool) -> Message, - { - Checkbox { - is_checked, - on_toggle: Box::new(f), - label: String::from(label), - label_color: None, - } - } - - /// Sets the color of the label of the [`Checkbox`]. - /// - /// [`Checkbox`]: struct.Checkbox.html - pub fn label_color>(mut self, color: C) -> Self { - self.label_color = Some(color.into()); - self - } -} - -impl Widget for Checkbox -where - Renderer: self::Renderer + text::Renderer, -{ - fn node(&self, renderer: &mut Renderer) -> Node { - Row::<(), Renderer>::new() - .spacing(15) - .align_items(Align::Center) - .push(Column::new().width(28).height(28)) - .push(Text::new(&self.label)) - .node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Pressed, - }) => { - let mouse_over = layout - .children() - .any(|child| child.bounds().contains(cursor_position)); - - if mouse_over { - messages.push((self.on_toggle)(!self.is_checked)); - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let children: Vec<_> = layout.children().collect(); - - let text_bounds = children[1].bounds(); - - text::Renderer::draw( - renderer, - text_bounds, - &self.label, - None, - self.label_color, - text::HorizontalAlignment::Left, - text::VerticalAlignment::Top, - ); - - self::Renderer::draw( - renderer, - cursor_position, - children[0].bounds(), - text_bounds, - self.is_checked, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.label.hash(state); - } -} - -/// The renderer of a [`Checkbox`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Checkbox`] in your user interface. -/// -/// [`Checkbox`]: struct.Checkbox.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Checkbox`]. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Checkbox`] - /// * the bounds of the label of the [`Checkbox`] - /// * whether the [`Checkbox`] is checked or not - /// - /// [`Checkbox`]: struct.Checkbox.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - label_bounds: Rectangle, - is_checked: bool, - ) -> MouseCursor; -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: self::Renderer + text::Renderer, - Message: 'static, -{ - fn from(checkbox: Checkbox) -> Element<'a, Message, Renderer> { - Element::new(checkbox) - } -} diff --git a/src/widget/column.rs b/src/widget/column.rs deleted file mode 100644 index 831f5b8f..00000000 --- a/src/widget/column.rs +++ /dev/null @@ -1,224 +0,0 @@ -use std::hash::Hash; - -use crate::{ - Align, Element, Event, Hasher, Justify, Layout, MouseCursor, Node, Point, - Style, Widget, -}; - -/// A container that distributes its contents vertically. -/// -/// A [`Column`] will try to fill the horizontal space of its container. -/// -/// [`Column`]: struct.Column.html -#[derive(Default)] -pub struct Column<'a, Message, Renderer> { - style: Style, - spacing: u16, - children: Vec>, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Column<'a, Message, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Column") - .field("style", &self.style) - .field("spacing", &self.spacing) - .field("children", &self.children) - .finish() - } -} - -impl<'a, Message, Renderer> Column<'a, Message, Renderer> { - /// Creates an empty [`Column`]. - /// - /// [`Column`]: struct.Column.html - pub fn new() -> Self { - let mut style = Style::default().fill_width(); - style.0.flex_direction = stretch::style::FlexDirection::Column; - - Column { - style, - spacing: 0, - children: Vec::new(), - } - } - - /// Sets the vertical spacing _between_ elements in pixels. - /// - /// Custom margins per element do not exist in Iced. You should use this - /// method instead! While less flexible, it helps you keep spacing between - /// elements consistent. - pub fn spacing(mut self, px: u16) -> Self { - self.spacing = px; - self - } - - /// Sets the padding of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn padding(mut self, px: u16) -> Self { - self.style = self.style.padding(px); - self - } - - /// Sets the width of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the maximum width of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn max_width(mut self, max_width: u16) -> Self { - self.style = self.style.max_width(max_width); - self - } - - /// Sets the maximum height of the [`Column`] in pixels. - /// - /// [`Column`]: struct.Column.html - pub fn max_height(mut self, max_height: u16) -> Self { - self.style = self.style.max_height(max_height); - self - } - - /// Sets the alignment of the [`Column`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Column`]: struct.Column.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } - - /// Sets the horizontal alignment of the contents of the [`Column`] . - /// - /// [`Column`]: struct.Column.html - pub fn align_items(mut self, align: Align) -> Self { - self.style = self.style.align_items(align); - self - } - - /// Sets the vertical distribution strategy for the contents of the - /// [`Column`] . - /// - /// [`Column`]: struct.Column.html - pub fn justify_content(mut self, justify: Justify) -> Self { - self.style = self.style.justify_content(justify); - self - } - - /// Adds an [`Element`] to the [`Column`]. - /// - /// [`Element`]: ../struct.Element.html - /// [`Column`]: struct.Column.html - pub fn push(mut self, child: E) -> Column<'a, Message, Renderer> - where - E: Into>, - { - self.children.push(child.into()); - self - } -} - -impl<'a, Message, Renderer> Widget - for Column<'a, Message, Renderer> -{ - fn node(&self, renderer: &mut Renderer) -> Node { - let mut children: Vec = self - .children - .iter() - .map(|child| { - let mut node = child.widget.node(renderer); - - let mut style = node.0.style(); - style.margin.bottom = - stretch::style::Dimension::Points(f32::from(self.spacing)); - - node.0.set_style(style); - node - }) - .collect(); - - if let Some(node) = children.last_mut() { - let mut style = node.0.style(); - style.margin.bottom = stretch::style::Dimension::Undefined; - - node.0.set_style(style); - } - - Node::with_children(self.style, children) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - self.children.iter_mut().zip(layout.children()).for_each( - |(child, layout)| { - child - .widget - .on_event(event, layout, cursor_position, messages) - }, - ); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let mut cursor = MouseCursor::OutOfBounds; - - self.children.iter().zip(layout.children()).for_each( - |(child, layout)| { - let new_cursor = - child.widget.draw(renderer, layout, cursor_position); - - if new_cursor != MouseCursor::OutOfBounds { - cursor = new_cursor; - } - }, - ); - - cursor - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - self.spacing.hash(state); - - for child in &self.children { - child.widget.hash_layout(state); - } - } -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: 'a, - Message: 'static, -{ - fn from( - column: Column<'a, Message, Renderer>, - ) -> Element<'a, Message, Renderer> { - Element::new(column) - } -} diff --git a/src/widget/image.rs b/src/widget/image.rs deleted file mode 100644 index 1601234e..00000000 --- a/src/widget/image.rs +++ /dev/null @@ -1,180 +0,0 @@ -//! Display images in your user interface. - -use crate::{ - Align, Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, - Widget, -}; - -use std::hash::Hash; - -/// A frame that displays an image while keeping aspect ratio. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`image::Renderer`] trait. -/// -/// [`Widget`]: ../../core/trait.Widget.html -/// [`image::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::Image; -/// -/// # let my_handle = String::from("some_handle"); -/// let image = Image::new(my_handle); -/// ``` -pub struct Image { - /// The image handle - pub image: I, - source: Option>, - /// The width of the image - pub width: Option, - height: Option, - style: Style, -} - -impl std::fmt::Debug for Image { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Image") - .field("source", &self.source) - .field("width", &self.width) - .field("height", &self.height) - .field("style", &self.style) - .finish() - } -} - -impl Image { - /// Creates a new [`Image`] with given image handle. - /// - /// [`Image`]: struct.Image.html - pub fn new(image: I) -> Self { - Image { - image, - source: None, - width: None, - height: None, - style: Style::default(), - } - } - - /// Sets the portion of the [`Image`] to draw. - /// - /// [`Image`]: struct.Image.html - pub fn clip(mut self, source: Rectangle) -> Self { - self.source = Some(source); - self - } - - /// Sets the width of the [`Image`] boundaries in pixels. - /// - /// [`Image`]: struct.Image.html - pub fn width(mut self, width: u16) -> Self { - self.width = Some(width); - self - } - - /// Sets the height of the [`Image`] boundaries in pixels. - /// - /// [`Image`]: struct.Image.html - pub fn height(mut self, height: u16) -> Self { - self.height = Some(height); - self - } - - /// Sets the alignment of the [`Image`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Image`]: struct.Image.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } -} - -impl Widget for Image -where - Renderer: self::Renderer, - I: Clone, -{ - fn node(&self, renderer: &mut Renderer) -> Node { - renderer.node( - self.style, - &self.image, - self.width, - self.height, - self.source, - ) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw(&self.image, layout.bounds(), self.source); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - self.width.hash(state); - self.height.hash(state); - } -} - -/// The renderer of an [`Image`]. -/// -/// Your [renderer] will need to implement this trait before being able to use -/// an [`Image`] in your user interface. -/// -/// [`Image`]: struct.Image.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Creates a [`Node`] with the given [`Style`] for the provided [`Image`] - /// and its size. - /// - /// You should probably keep the original aspect ratio, if possible. - /// - /// [`Node`]: ../../struct.Node.html - /// [`Style`]: ../../struct.Style.html - /// [`Image`]: struct.Image.html - fn node( - &mut self, - style: Style, - image: &I, - width: Option, - height: Option, - source: Option>, - ) -> Node; - - /// Draws an [`Image`]. - /// - /// It receives: - /// * the bounds of the [`Image`] - /// * the handle of the loaded [`Image`] - /// * the portion of the image to draw. If not specified, the entire image - /// should be drawn. - /// - /// [`Image`]: struct.Image.html - fn draw( - &mut self, - image: &I, - bounds: Rectangle, - source: Option>, - ); -} - -impl<'a, I, Message, Renderer> From> for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, - I: Clone + 'a, -{ - fn from(image: Image) -> Element<'a, Message, Renderer> { - Element::new(image) - } -} diff --git a/src/widget/panel.rs b/src/widget/panel.rs deleted file mode 100644 index d43d6fb6..00000000 --- a/src/widget/panel.rs +++ /dev/null @@ -1,94 +0,0 @@ -use std::hash::Hash; - -use crate::graphics::{Point, Rectangle}; -use crate::ui::core::{ - Event, Hasher, Layout, MouseCursor, Node, Style, Widget, -}; - -pub struct Panel<'a, Message, Renderer> { - style: Style, - content: Box + 'a>, -} - -impl<'a, Message, Renderer> Panel<'a, Message, Renderer> { - pub fn new(content: impl Widget + 'a) -> Self { - Panel { - style: Style::default().padding(20), - content: Box::new(content), - } - } - - pub fn width(mut self, width: u32) -> Self { - self.style = self.style.width(width); - self - } - - pub fn max_width(mut self, max_width: u32) -> Self { - self.style = self.style.max_width(max_width); - self - } -} - -impl<'a, Message, Renderer> Widget - for Panel<'a, Message, Renderer> -where - Renderer: self::Renderer, -{ - fn node(&self, renderer: &Renderer) -> Node { - Node::with_children(self.style, vec![self.content.node(renderer)]) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout, - cursor_position: Point, - messages: &mut Vec, - ) { - [&mut self.content] - .iter_mut() - .zip(layout.children()) - .for_each(|(child, layout)| { - child.on_event(event, layout, cursor_position, messages) - }); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout, - cursor_position: Point, - ) -> MouseCursor { - let bounds = layout.bounds(); - let mut cursor = MouseCursor::OutOfBounds; - renderer.draw(bounds); - - [&self.content].iter().zip(layout.children()).for_each( - |(child, layout)| { - let new_cursor = child.draw(renderer, layout, cursor_position); - - if new_cursor != MouseCursor::OutOfBounds { - cursor = new_cursor; - } - }, - ); - - if cursor == MouseCursor::OutOfBounds { - if bounds.contains(cursor_position) { - MouseCursor::Idle - } else { - MouseCursor::OutOfBounds - } - } else { - cursor - } - } - - fn hash(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -pub trait Renderer { - fn draw(&mut self, bounds: Rectangle); -} diff --git a/src/widget/progress_bar.rs b/src/widget/progress_bar.rs deleted file mode 100644 index d4499160..00000000 --- a/src/widget/progress_bar.rs +++ /dev/null @@ -1,106 +0,0 @@ -//! Provide visual feedback to your users when performing a slow task. - -use crate::{ - Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, Widget, -}; - -use std::hash::Hash; - -/// A bar that is filled based on an amount of progress. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`progress_bar::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`progress_bar::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::ProgressBar; -/// -/// let progress = 0.75; -/// -/// ProgressBar::new(progress); -/// ``` -#[derive(Debug)] -pub struct ProgressBar { - progress: f32, - style: Style, -} - -impl ProgressBar { - /// Creates a new [`ProgressBar`] filled based on the given amount of - /// progress. - /// - /// The progress should be in the `0.0..=1.0` range. `0` meaning no work - /// done, and `1` meaning work finished. - /// - /// [`ProgressBar`]: struct.ProgressBar.html - pub fn new(progress: f32) -> Self { - ProgressBar { - progress, - style: Style::default().fill_width(), - } - } - - /// Sets the width of the [`ProgressBar`] in pixels. - /// - /// [`ProgressBar`]: struct.ProgressBar.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } -} - -impl Widget for ProgressBar -where - Renderer: self::Renderer, -{ - fn node(&self, _renderer: &Renderer) -> Node { - Node::new(self.style.height(50)) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw(layout.bounds(), self.progress); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -/// The renderer of a [`ProgressBar`]. -/// -/// Your [renderer] will need to implement this trait before being able to use -/// a [`ProgressBar`] in your user interface. -/// -/// [`ProgressBar`]: struct.ProgressBar.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`ProgressBar`]. - /// - /// It receives: - /// * the bounds of the [`ProgressBar`] - /// * the current progress of the [`ProgressBar`], in the `0.0..=1.0` - /// range. - /// - /// [`ProgressBar`]: struct.ProgressBar.html - fn draw(&mut self, bounds: Rectangle, progress: f32); -} - -impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, -{ - fn from(progress_bar: ProgressBar) -> Element<'a, Message, Renderer> { - Element::new(progress_bar) - } -} diff --git a/src/widget/radio.rs b/src/widget/radio.rs deleted file mode 100644 index 048aea94..00000000 --- a/src/widget/radio.rs +++ /dev/null @@ -1,207 +0,0 @@ -//! Create choices using radio buttons. -use crate::input::{mouse, ButtonState}; -use crate::widget::{text, Column, Row, Text}; -use crate::{ - Align, Color, Element, Event, Hasher, Layout, MouseCursor, Node, Point, - Rectangle, Widget, -}; - -use std::hash::Hash; - -/// A circular button representing a choice. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`radio::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`radio::Renderer`]: trait.Renderer.html -/// -/// # Example -/// ``` -/// use iced::Radio; -/// -/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] -/// pub enum Choice { -/// A, -/// B, -/// } -/// -/// #[derive(Debug, Clone, Copy)] -/// pub enum Message { -/// RadioSelected(Choice), -/// } -/// -/// let selected_choice = Some(Choice::A); -/// -/// Radio::new(Choice::A, "This is A", selected_choice, Message::RadioSelected); -/// -/// Radio::new(Choice::B, "This is B", selected_choice, Message::RadioSelected); -/// ``` -/// -/// ![Radio buttons drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/radio.png?raw=true) -pub struct Radio { - /// Whether the radio button is selected or not - pub is_selected: bool, - - /// The message to produce when the radio button is clicked - pub on_click: Message, - - /// The label of the radio button - pub label: String, - - /// The color of the label - pub label_color: Option, -} - -impl std::fmt::Debug for Radio -where - Message: std::fmt::Debug, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Radio") - .field("is_selected", &self.is_selected) - .field("on_click", &self.on_click) - .field("label", &self.label) - .field("label_color", &self.label_color) - .finish() - } -} - -impl Radio { - /// Creates a new [`Radio`] button. - /// - /// It expects: - /// * the value related to the [`Radio`] button - /// * the label of the [`Radio`] button - /// * the current selected value - /// * a function that will be called when the [`Radio`] is selected. It - /// receives the value of the radio and must produce a `Message`. - /// - /// [`Radio`]: struct.Radio.html - pub fn new(value: V, label: &str, selected: Option, f: F) -> Self - where - V: Eq + Copy, - F: 'static + Fn(V) -> Message, - { - Radio { - is_selected: Some(value) == selected, - on_click: f(value), - label: String::from(label), - label_color: None, - } - } - - /// Sets the `Color` of the label of the [`Radio`]. - /// - /// [`Radio`]: struct.Radio.html - pub fn label_color>(mut self, color: C) -> Self { - self.label_color = Some(color.into()); - self - } -} - -impl Widget for Radio -where - Renderer: self::Renderer + text::Renderer, - Message: Copy + std::fmt::Debug, -{ - fn node(&self, renderer: &mut Renderer) -> Node { - Row::<(), Renderer>::new() - .spacing(15) - .align_items(Align::Center) - .push(Column::new().width(28).height(28)) - .push(Text::new(&self.label)) - .node(renderer) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state: ButtonState::Pressed, - }) => { - if layout.bounds().contains(cursor_position) { - messages.push(self.on_click); - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let children: Vec<_> = layout.children().collect(); - - let mut text_bounds = children[1].bounds(); - text_bounds.y -= 2.0; - - text::Renderer::draw( - renderer, - text_bounds, - &self.label, - None, - self.label_color, - text::HorizontalAlignment::Left, - text::VerticalAlignment::Top, - ); - - self::Renderer::draw( - renderer, - cursor_position, - children[0].bounds(), - layout.bounds(), - self.is_selected, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.label.hash(state); - } -} - -/// The renderer of a [`Radio`] button. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Radio`] button in your user interface. -/// -/// [`Radio`]: struct.Radio.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Radio`] button. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Radio`] - /// * the bounds of the label of the [`Radio`] - /// * whether the [`Radio`] is selected or not - /// - /// [`Radio`]: struct.Radio.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - label_bounds: Rectangle, - is_selected: bool, - ) -> MouseCursor; -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: self::Renderer + text::Renderer, - Message: 'static + Copy + std::fmt::Debug, -{ - fn from(checkbox: Radio) -> Element<'a, Message, Renderer> { - Element::new(checkbox) - } -} diff --git a/src/widget/row.rs b/src/widget/row.rs deleted file mode 100644 index 181020e3..00000000 --- a/src/widget/row.rs +++ /dev/null @@ -1,219 +0,0 @@ -use std::hash::Hash; - -use crate::{ - Align, Element, Event, Hasher, Justify, Layout, MouseCursor, Node, Point, - Style, Widget, -}; - -/// A container that distributes its contents horizontally. -/// -/// A [`Row`] will try to fill the horizontal space of its container. -/// -/// [`Row`]: struct.Row.html -#[derive(Default)] -pub struct Row<'a, Message, Renderer> { - style: Style, - spacing: u16, - children: Vec>, -} - -impl<'a, Message, Renderer> std::fmt::Debug for Row<'a, Message, Renderer> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Row") - .field("style", &self.style) - .field("spacing", &self.spacing) - .field("children", &self.children) - .finish() - } -} - -impl<'a, Message, Renderer> Row<'a, Message, Renderer> { - /// Creates an empty [`Row`]. - /// - /// [`Row`]: struct.Row.html - pub fn new() -> Self { - Row { - style: Style::default().fill_width(), - spacing: 0, - children: Vec::new(), - } - } - - /// Sets the horizontal spacing _between_ elements in pixels. - /// - /// Custom margins per element do not exist in Iced. You should use this - /// method instead! While less flexible, it helps you keep spacing between - /// elements consistent. - pub fn spacing(mut self, px: u16) -> Self { - self.spacing = px; - self - } - - /// Sets the padding of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn padding(mut self, px: u16) -> Self { - self.style = self.style.padding(px); - self - } - - /// Sets the width of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the maximum width of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn max_width(mut self, max_width: u16) -> Self { - self.style = self.style.max_width(max_width); - self - } - - /// Sets the maximum height of the [`Row`] in pixels. - /// - /// [`Row`]: struct.Row.html - pub fn max_height(mut self, max_height: u16) -> Self { - self.style = self.style.max_height(max_height); - self - } - - /// Sets the alignment of the [`Row`] itself. - /// - /// This is useful if you want to override the default alignment given by - /// the parent container. - /// - /// [`Row`]: struct.Row.html - pub fn align_self(mut self, align: Align) -> Self { - self.style = self.style.align_self(align); - self - } - - /// Sets the vertical alignment of the contents of the [`Row`] . - /// - /// [`Row`]: struct.Row.html - pub fn align_items(mut self, align: Align) -> Self { - self.style = self.style.align_items(align); - self - } - - /// Sets the horizontal distribution strategy for the contents of the - /// [`Row`] . - /// - /// [`Row`]: struct.Row.html - pub fn justify_content(mut self, justify: Justify) -> Self { - self.style = self.style.justify_content(justify); - self - } - - /// Adds an [`Element`] to the [`Row`]. - /// - /// [`Element`]: ../struct.Element.html - /// [`Row`]: struct.Row.html - pub fn push(mut self, child: E) -> Row<'a, Message, Renderer> - where - E: Into>, - { - self.children.push(child.into()); - self - } -} - -impl<'a, Message, Renderer> Widget - for Row<'a, Message, Renderer> -{ - fn node(&self, renderer: &mut Renderer) -> Node { - let mut children: Vec = self - .children - .iter() - .map(|child| { - let mut node = child.widget.node(renderer); - - let mut style = node.0.style(); - style.margin.end = - stretch::style::Dimension::Points(f32::from(self.spacing)); - - node.0.set_style(style); - node - }) - .collect(); - - if let Some(node) = children.last_mut() { - let mut style = node.0.style(); - style.margin.end = stretch::style::Dimension::Undefined; - - node.0.set_style(style); - } - - Node::with_children(self.style, children) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - self.children.iter_mut().zip(layout.children()).for_each( - |(child, layout)| { - child - .widget - .on_event(event, layout, cursor_position, messages) - }, - ); - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - let mut cursor = MouseCursor::OutOfBounds; - - self.children.iter().zip(layout.children()).for_each( - |(child, layout)| { - let new_cursor = - child.widget.draw(renderer, layout, cursor_position); - - if new_cursor != MouseCursor::OutOfBounds { - cursor = new_cursor; - } - }, - ); - - cursor - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - self.spacing.hash(state); - - for child in &self.children { - child.widget.hash_layout(state); - } - } -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: 'a, - Message: 'static, -{ - fn from(row: Row<'a, Message, Renderer>) -> Element<'a, Message, Renderer> { - Element::new(row) - } -} diff --git a/src/widget/slider.rs b/src/widget/slider.rs deleted file mode 100644 index fb6db8c9..00000000 --- a/src/widget/slider.rs +++ /dev/null @@ -1,245 +0,0 @@ -//! Display an interactive selector of a single value from a range of values. -//! -//! A [`Slider`] has some local [`State`]. -//! -//! [`Slider`]: struct.Slider.html -//! [`State`]: struct.State.html -use std::hash::Hash; -use std::ops::RangeInclusive; -use std::rc::Rc; - -use crate::input::{mouse, ButtonState}; -use crate::{ - Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, - Widget, -}; - -/// An horizontal bar and a handle that selects a single value from a range of -/// values. -/// -/// A [`Slider`] will try to fill the horizontal space of its container. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`slider::Renderer`] trait. -/// -/// [`Slider`]: struct.Slider.html -/// [`Widget`]: ../trait.Widget.html -/// [`slider::Renderer`]: trait.Renderer.html -/// -/// # Example -/// ``` -/// use iced::{slider, Slider}; -/// -/// pub enum Message { -/// SliderChanged(f32), -/// } -/// -/// let state = &mut slider::State::new(); -/// let value = 50.0; -/// -/// Slider::new(state, 0.0..=100.0, value, Message::SliderChanged); -/// ``` -/// -/// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true) -pub struct Slider<'a, Message> { - state: &'a mut State, - /// The range of the slider - pub range: RangeInclusive, - /// The current value of the slider - pub value: f32, - /// The function to produce messages on change - pub on_change: Rc Message>>, - style: Style, -} - -impl<'a, Message> std::fmt::Debug for Slider<'a, Message> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("Slider") - .field("state", &self.state) - .field("range", &self.range) - .field("value", &self.value) - .field("style", &self.style) - .finish() - } -} - -impl<'a, Message> Slider<'a, Message> { - /// Creates a new [`Slider`]. - /// - /// It expects: - /// * the local [`State`] of the [`Slider`] - /// * an inclusive range of possible values - /// * the current value of the [`Slider`] - /// * a function that will be called when the [`Slider`] is dragged. - /// It receives the new value of the [`Slider`] and must produce a - /// `Message`. - /// - /// [`Slider`]: struct.Slider.html - /// [`State`]: struct.State.html - pub fn new( - state: &'a mut State, - range: RangeInclusive, - value: f32, - on_change: F, - ) -> Self - where - F: 'static + Fn(f32) -> Message, - { - Slider { - state, - value: value.max(*range.start()).min(*range.end()), - range, - on_change: Rc::new(Box::new(on_change)), - style: Style::default().min_width(100).fill_width(), - } - } - - /// Sets the width of the [`Slider`] in pixels. - /// - /// [`Slider`]: struct.Slider.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } -} - -impl<'a, Message, Renderer> Widget for Slider<'a, Message> -where - Renderer: self::Renderer, -{ - fn node(&self, _renderer: &mut Renderer) -> Node { - Node::new(self.style.height(25)) - } - - fn on_event( - &mut self, - event: Event, - layout: Layout<'_>, - cursor_position: Point, - messages: &mut Vec, - ) { - let mut change = || { - let bounds = layout.bounds(); - - if cursor_position.x <= bounds.x { - messages.push((self.on_change)(*self.range.start())); - } else if cursor_position.x >= bounds.x + bounds.width { - messages.push((self.on_change)(*self.range.end())); - } else { - let percent = (cursor_position.x - bounds.x) / bounds.width; - let value = (self.range.end() - self.range.start()) * percent - + self.range.start(); - - messages.push((self.on_change)(value)); - } - }; - - match event { - Event::Mouse(mouse::Event::Input { - button: mouse::Button::Left, - state, - }) => match state { - ButtonState::Pressed => { - if layout.bounds().contains(cursor_position) { - change(); - self.state.is_dragging = true; - } - } - ButtonState::Released => { - self.state.is_dragging = false; - } - }, - Event::Mouse(mouse::Event::CursorMoved { .. }) => { - if self.state.is_dragging { - change(); - } - } - _ => {} - } - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - cursor_position, - layout.bounds(), - self.state, - self.range.clone(), - self.value, - ) - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - } -} - -/// The local state of a [`Slider`]. -/// -/// [`Slider`]: struct.Slider.html -#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] -pub struct State { - is_dragging: bool, -} - -impl State { - /// Creates a new [`State`]. - /// - /// [`State`]: struct.State.html - pub fn new() -> State { - State::default() - } - - /// Returns whether the associated [`Slider`] is currently being dragged or - /// not. - /// - /// [`Slider`]: struct.Slider.html - pub fn is_dragging(&self) -> bool { - self.is_dragging - } -} - -/// The renderer of a [`Slider`]. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use a [`Slider`] in your user interface. -/// -/// [`Slider`]: struct.Slider.html -/// [renderer]: ../../renderer/index.html -pub trait Renderer { - /// Draws a [`Slider`]. - /// - /// It receives: - /// * the current cursor position - /// * the bounds of the [`Slider`] - /// * the local state of the [`Slider`] - /// * the range of values of the [`Slider`] - /// * the current value of the [`Slider`] - /// - /// [`Slider`]: struct.Slider.html - /// [`State`]: struct.State.html - /// [`Class`]: enum.Class.html - fn draw( - &mut self, - cursor_position: Point, - bounds: Rectangle, - state: &State, - range: RangeInclusive, - value: f32, - ) -> MouseCursor; -} - -impl<'a, Message, Renderer> From> - for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, - Message: 'static, -{ - fn from(slider: Slider<'a, Message>) -> Element<'a, Message, Renderer> { - Element::new(slider) - } -} diff --git a/src/widget/text.rs b/src/widget/text.rs deleted file mode 100644 index 4ef10d52..00000000 --- a/src/widget/text.rs +++ /dev/null @@ -1,218 +0,0 @@ -//! Write some text for your users to read. -use crate::{ - Color, Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Style, - Widget, -}; - -use std::hash::Hash; - -/// A paragraph of text. -/// -/// It implements [`Widget`] when the associated `Renderer` implements the -/// [`text::Renderer`] trait. -/// -/// [`Widget`]: ../trait.Widget.html -/// [`text::Renderer`]: trait.Renderer.html -/// -/// # Example -/// -/// ``` -/// use iced::{Text, Color}; -/// -/// Text::new("I <3 iced!") -/// .size(40); -/// ``` -#[derive(Debug, Clone)] -pub struct Text { - /// The text contents - pub content: String, - /// The text size - pub size: Option, - color: Option, - style: Style, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, -} - -impl Text { - /// Create a new fragment of [`Text`] with the given contents. - /// - /// [`Text`]: struct.Text.html - pub fn new(label: &str) -> Self { - Text { - content: String::from(label), - size: None, - color: None, - style: Style::default().fill_width(), - horizontal_alignment: HorizontalAlignment::Left, - vertical_alignment: VerticalAlignment::Top, - } - } - - /// Sets the size of the [`Text`] in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn size(mut self, size: u16) -> Self { - self.size = Some(size); - self - } - - /// Sets the `Color` of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - pub fn color>(mut self, color: C) -> Self { - self.color = Some(color.into()); - self - } - - /// Sets the width of the [`Text`] boundaries in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn width(mut self, width: u16) -> Self { - self.style = self.style.width(width); - self - } - - /// Sets the height of the [`Text`] boundaries in pixels. - /// - /// [`Text`]: struct.Text.html - pub fn height(mut self, height: u16) -> Self { - self.style = self.style.height(height); - self - } - - /// Sets the [`HorizontalAlignment`] of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html - pub fn horizontal_alignment( - mut self, - alignment: HorizontalAlignment, - ) -> Self { - self.horizontal_alignment = alignment; - self - } - - /// Sets the [`VerticalAlignment`] of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - /// [`VerticalAlignment`]: enum.VerticalAlignment.html - pub fn vertical_alignment(mut self, alignment: VerticalAlignment) -> Self { - self.vertical_alignment = alignment; - self - } -} - -impl Widget for Text -where - Renderer: self::Renderer, -{ - fn node(&self, renderer: &mut Renderer) -> Node { - renderer.node(self.style, &self.content, self.size) - } - - fn draw( - &self, - renderer: &mut Renderer, - layout: Layout<'_>, - _cursor_position: Point, - ) -> MouseCursor { - renderer.draw( - layout.bounds(), - &self.content, - self.size, - self.color, - self.horizontal_alignment, - self.vertical_alignment, - ); - - MouseCursor::OutOfBounds - } - - fn hash_layout(&self, state: &mut Hasher) { - self.style.hash(state); - - self.content.hash(state); - self.size.hash(state); - } -} - -/// The renderer of a [`Text`] fragment. -/// -/// Your [renderer] will need to implement this trait before being -/// able to use [`Text`] in your [`UserInterface`]. -/// -/// [`Text`]: struct.Text.html -/// [renderer]: ../../renderer/index.html -/// [`UserInterface`]: ../../struct.UserInterface.html -pub trait Renderer { - /// Creates a [`Node`] with the given [`Style`] for the provided [`Text`] - /// contents and size. - /// - /// You should probably use [`Node::with_measure`] to allow [`Text`] to - /// adapt to the dimensions of its container. - /// - /// [`Node`]: ../../struct.Node.html - /// [`Style`]: ../../struct.Style.html - /// [`Text`]: struct.Text.html - /// [`Node::with_measure`]: ../../struct.Node.html#method.with_measure - fn node(&self, style: Style, content: &str, size: Option) -> Node; - - /// Draws a [`Text`] fragment. - /// - /// It receives: - /// * the bounds of the [`Text`] - /// * the contents of the [`Text`] - /// * the size of the [`Text`] - /// * the color of the [`Text`] - /// * the [`HorizontalAlignment`] of the [`Text`] - /// * the [`VerticalAlignment`] of the [`Text`] - /// - /// [`Text`]: struct.Text.html - /// [`HorizontalAlignment`]: enum.HorizontalAlignment.html - /// [`VerticalAlignment`]: enum.VerticalAlignment.html - fn draw( - &mut self, - bounds: Rectangle, - content: &str, - size: Option, - color: Option, - horizontal_alignment: HorizontalAlignment, - vertical_alignment: VerticalAlignment, - ); -} - -impl<'a, Message, Renderer> From for Element<'a, Message, Renderer> -where - Renderer: self::Renderer, -{ - fn from(text: Text) -> Element<'a, Message, Renderer> { - Element::new(text) - } -} - -/// The horizontal alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum HorizontalAlignment { - /// Align left - Left, - - /// Horizontally centered - Center, - - /// Align right - Right, -} - -/// The vertical alignment of some resource. -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum VerticalAlignment { - /// Align top - Top, - - /// Vertically centered - Center, - - /// Align bottom - Bottom, -} diff --git a/web/Cargo.toml b/web/Cargo.toml index 43377d46..d5a987b0 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -15,7 +15,7 @@ categories = ["web-programming"] maintenance = { status = "actively-developed" } [dependencies] -iced = { version = "0.1.0-alpha", path = ".." } +iced_core = { version = "0.1.0-alpha", path = "../core" } dodrio = "0.1.0" futures-preview = "=0.3.0-alpha.18" wasm-bindgen = "0.2.50" diff --git a/web/src/lib.rs b/web/src/lib.rs index a6dc2b79..09ca3460 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -8,7 +8,7 @@ mod widget; pub use bus::Bus; pub use element::Element; -pub use iced::{Align, Color}; +pub use iced_core::{Align, Color, Length}; pub use widget::*; pub trait UserInterface { diff --git a/web/src/widget/button.rs b/web/src/widget/button.rs index 63c0262a..36b35901 100644 --- a/web/src/widget/button.rs +++ b/web/src/widget/button.rs @@ -1,9 +1,8 @@ use crate::{Bus, Element, Widget}; -pub use iced::button::{Class, State}; use dodrio::bumpalo; -pub type Button<'a, Message> = iced::Button<'a, Message>; +pub type Button<'a, Message> = iced_core::Button<'a, Message>; impl<'a, Message> Widget for Button<'a, Message> where diff --git a/web/src/widget/checkbox.rs b/web/src/widget/checkbox.rs index 5aadd65d..34995781 100644 --- a/web/src/widget/checkbox.rs +++ b/web/src/widget/checkbox.rs @@ -2,7 +2,7 @@ use crate::{Bus, Element, Widget}; use dodrio::bumpalo; -pub use iced::Checkbox; +pub use iced_core::Checkbox; impl Widget for Checkbox where diff --git a/web/src/widget/column.rs b/web/src/widget/column.rs index a2b8232e..99491647 100644 --- a/web/src/widget/column.rs +++ b/web/src/widget/column.rs @@ -1,42 +1,8 @@ -use crate::{Align, Bus, Element, Widget}; +use crate::{Bus, Element, Widget}; use dodrio::bumpalo; -pub struct Column<'a, Message> { - children: Vec>, -} - -impl<'a, Message> Column<'a, Message> { - pub fn new() -> Self { - Self { - children: Vec::new(), - } - } - - pub fn spacing(self, _spacing: u16) -> Self { - self - } - - pub fn padding(self, _padding: u16) -> Self { - self - } - - pub fn max_width(self, _max_width: u16) -> Self { - self - } - - pub fn align_items(self, _align: Align) -> Self { - self - } - - pub fn push(mut self, element: E) -> Self - where - E: Into>, - { - self.children.push(element.into()); - self - } -} +pub type Column<'a, Message> = iced_core::Column>; impl<'a, Message> Widget for Column<'a, Message> { fn node<'b>( diff --git a/web/src/widget/image.rs b/web/src/widget/image.rs index a882faff..48ff539f 100644 --- a/web/src/widget/image.rs +++ b/web/src/widget/image.rs @@ -1,8 +1,8 @@ -use crate::{Bus, Element, Widget}; +use crate::{Bus, Element, Length, Widget}; use dodrio::bumpalo; -pub type Image<'a> = iced::Image<&'a str>; +pub type Image<'a> = iced_core::Image<&'a str>; impl<'a, Message> Widget for Image<'a> { fn node<'b>( @@ -12,13 +12,21 @@ impl<'a, Message> Widget for Image<'a> { ) -> dodrio::Node<'b> { use dodrio::builder::*; - let src = bumpalo::format!(in bump, "{}", self.image); + let src = bumpalo::format!(in bump, "{}", self.handle); let mut image = img(bump).attr("src", src.into_bump_str()); - if let Some(width) = self.width { - let width = bumpalo::format!(in bump, "{}", width); - image = image.attr("width", width.into_bump_str()); + match self.width { + Length::Shrink => {} + Length::Fill => { + image = image.attr("width", "100%"); + } + Length::Units(px) => { + image = image.attr( + "width", + bumpalo::format!(in bump, "{}px", px).into_bump_str(), + ); + } } image.finish() diff --git a/web/src/widget/radio.rs b/web/src/widget/radio.rs index e762ae28..9063770a 100644 --- a/web/src/widget/radio.rs +++ b/web/src/widget/radio.rs @@ -2,7 +2,7 @@ use crate::{Bus, Element, Widget}; use dodrio::bumpalo; -pub use iced::Radio; +pub use iced_core::Radio; impl Widget for Radio where diff --git a/web/src/widget/row.rs b/web/src/widget/row.rs index 71532245..d4f4c4a0 100644 --- a/web/src/widget/row.rs +++ b/web/src/widget/row.rs @@ -2,29 +2,7 @@ use crate::{Bus, Element, Widget}; use dodrio::bumpalo; -pub struct Row<'a, Message> { - children: Vec>, -} - -impl<'a, Message> Row<'a, Message> { - pub fn new() -> Self { - Self { - children: Vec::new(), - } - } - - pub fn spacing(self, _spacing: u16) -> Self { - self - } - - pub fn push(mut self, element: E) -> Self - where - E: Into>, - { - self.children.push(element.into()); - self - } -} +pub type Row<'a, Message> = iced_core::Row>; impl<'a, Message> Widget for Row<'a, Message> { fn node<'b>( diff --git a/web/src/widget/slider.rs b/web/src/widget/slider.rs index 31bfcbf3..19668025 100644 --- a/web/src/widget/slider.rs +++ b/web/src/widget/slider.rs @@ -2,9 +2,9 @@ use crate::{Bus, Element, Widget}; use dodrio::bumpalo; -pub type Slider<'a, Message> = iced::Slider<'a, Message>; +pub type Slider<'a, Message> = iced_core::Slider<'a, Message>; -pub use iced::slider::State; +pub use iced_core::slider::State; impl<'a, Message> Widget for Slider<'a, Message> where diff --git a/web/src/widget/text.rs b/web/src/widget/text.rs index a5709775..ef9170ef 100644 --- a/web/src/widget/text.rs +++ b/web/src/widget/text.rs @@ -1,7 +1,7 @@ use crate::{Bus, Element, Widget}; use dodrio::bumpalo; -pub use iced::text::*; +pub use iced_core::text::*; impl<'a, Message> Widget for Text { fn node<'b>( -- cgit From eda1048dbcf5753dc6171c3ee1478ec44468d5e3 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 20 Sep 2019 19:31:49 +0200 Subject: Complete some `hash_layout` implementations --- native/src/widget/button.rs | 6 +++--- native/src/widget/image.rs | 5 ++--- native/src/widget/text.rs | 2 ++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/native/src/widget/button.rs b/native/src/widget/button.rs index c9436dc4..7b5c4a86 100644 --- a/native/src/widget/button.rs +++ b/native/src/widget/button.rs @@ -7,9 +7,7 @@ //! [`Class`]: enum.Class.html use crate::input::{mouse, ButtonState}; -use crate::{ - Element, Event, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Widget, -}; +use crate::{Element, Event, Hasher, Layout, MouseCursor, Node, Point, Widget}; use std::hash::Hash; pub use iced_core::button::*; @@ -70,7 +68,9 @@ where } fn hash_layout(&self, state: &mut Hasher) { + self.label.hash(state); self.width.hash(state); + self.align_self.hash(state); } } diff --git a/native/src/widget/image.rs b/native/src/widget/image.rs index 17f06ebe..81f99acb 100644 --- a/native/src/widget/image.rs +++ b/native/src/widget/image.rs @@ -1,8 +1,6 @@ //! Display images in your user interface. -use crate::{ - Element, Hasher, Layout, MouseCursor, Node, Point, Rectangle, Widget, -}; +use crate::{Element, Hasher, Layout, MouseCursor, Node, Point, Widget}; use std::hash::Hash; @@ -31,6 +29,7 @@ where fn hash_layout(&self, state: &mut Hasher) { self.width.hash(state); self.height.hash(state); + self.align_self.hash(state); } } diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index affdfd64..5ca6ebf3 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -27,6 +27,8 @@ where fn hash_layout(&self, state: &mut Hasher) { self.content.hash(state); self.size.hash(state); + self.width.hash(state); + self.height.hash(state); } } -- cgit From 86dede4c4cc2bca9be7d2e6bd831daa98bd7043d Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 21 Sep 2019 13:38:14 +0200 Subject: Make example work on web and update READMEs --- examples/README.md | 51 +++++++--- examples/tour/Cargo.toml | 7 +- examples/tour/README.md | 57 ++++++++--- examples/tour/src/iced_ggez/main.rs | 189 ------------------------------------ examples/tour/src/main.rs | 189 ++++++++++++++++++++++++++++++++++++ examples/tour/src/tour.rs | 2 +- web/src/widget/button.rs | 4 +- web/src/widget/checkbox.rs | 1 + web/src/widget/column.rs | 1 + web/src/widget/image.rs | 2 + web/src/widget/radio.rs | 1 + web/src/widget/row.rs | 1 + web/src/widget/slider.rs | 5 +- web/src/widget/text.rs | 1 + 14 files changed, 290 insertions(+), 221 deletions(-) delete mode 100644 examples/tour/src/iced_ggez/main.rs create mode 100644 examples/tour/src/main.rs diff --git a/examples/README.md b/examples/README.md index 2032df49..5ca0eb69 100644 --- a/examples/README.md +++ b/examples/README.md @@ -11,31 +11,60 @@ you want to learn about a specific release, check out [the release list]. A simple UI tour showcasing different widgets that can be built using Iced. It also shows how the library can be integrated into an existing system. -The example is built on top of [`ggez`], a game library for Rust. Currently, it -is using a [personal fork] to [add a `FontCache` type] and -[fix some issues with HiDPI]. +The example can run both on native and web platforms, using the same GUI code! + +The native renderer of the example is built on top of [`ggez`], a game library +for Rust. Currently, it is using a [personal fork] to [add a `FontCache` type] +and [fix some issues with HiDPI]. + +The web version uses `iced_web` directly. This crate is still a work in +progress. In particular, the styling of elements is not finished yet +(text color, alignment, sizing, etc). The implementation consists of different modules: - - __[`tour`]__ contains the actual GUI code: __state__, __messages__, - __update logic__ and __view logic__. - - __[`renderer`]__ implements a simple renderer for each of the used widgets on - top of the graphics module of [`ggez`]. - - __[`widget`]__ re-exposes Iced's built-in widgets with the renderer type parameter - replaced with the implemented [`renderer`], for convenience. + - __[`tour`]__ contains the actual cross-platform GUI code: __state__, + __messages__, __update logic__ and __view logic__. + - __[`iced_ggez`]__ implements a simple renderer for each of the used widgets + on top of the graphics module of [`ggez`]. + - __[`widget`]__ conditionally re-exposes the correct platform widgets based + on the target architecture. - __[`main`]__ integrates Iced with [`ggez`] and connects the [`tour`] with the [`renderer`]. + - __[`lib`]__ exposes the [`tour`] types and conditionally implements the + WebAssembly entrypoint in the [`web`] module. + +The conditional compilation awkwardness from targetting both native and web +platforms should be handled seamlessly by the `iced` crate in the near future! + +If you want to run it as a native app: + +``` +cd examples/tour +cargo run +``` + +If you want to run it on web, you will need [`wasm-pack`]: + +``` +cd examples/tour +wasm-pack build --target web +``` + +Then, simply serve the directory with any HTTP server. For instance: ``` -cargo run --package iced_ggez_tour +python3 -m http.server ``` [![Tour - Iced][gui_gif]][gui_gfycat] [`ggez`]: https://github.com/ggez/ggez [`tour`]: tour/src/tour.rs -[`renderer`]: tour/src/renderer +[`iced_ggez`]: tour/src/iced_ggez [`widget`]: tour/src/widget.rs [`main`]: tour/src/main.rs +[`lib`]: tour/src/lib.rs +[`web`]: tour/src/web.rs [personal fork]: https://github.com/hecrj/ggez [add a `FontCache` type]: https://github.com/ggez/ggez/pull/679 [fix some issues with HiDPI]: https://github.com/hecrj/ggez/commit/dfe2fd2423c51a6daf42c75f66dfaeaacd439fb1 diff --git a/examples/tour/Cargo.toml b/examples/tour/Cargo.toml index a0e3eee8..8cdce295 100644 --- a/examples/tour/Cargo.toml +++ b/examples/tour/Cargo.toml @@ -8,9 +8,12 @@ repository = "https://github.com/hecrj/iced" edition = "2018" publish = false +[lib] +crate-type = ["cdylib", "rlib"] + [[bin]] -name = "ggez" -path = "src/iced_ggez/main.rs" +name = "main" +path = "src/main.rs" [dependencies] futures-preview = "=0.3.0-alpha.18" diff --git a/examples/tour/README.md b/examples/tour/README.md index 2af048cc..a33de7f5 100644 --- a/examples/tour/README.md +++ b/examples/tour/README.md @@ -3,31 +3,60 @@ A simple UI tour showcasing different widgets that can be built using Iced. It also shows how the library can be integrated into an existing system. -The example is built on top of [`ggez`], a game library for Rust. Currently, it -is using a [personal fork] to [add a `FontCache` type] and -[fix some issues with HiDPI]. +The example can run both on native and web platforms, using the same GUI code! + +The native renderer of the example is built on top of [`ggez`], a game library +for Rust. Currently, it is using a [personal fork] to [add a `FontCache` type] +and [fix some issues with HiDPI]. + +The web version uses `iced_web` directly. This crate is still a work in +progress. In particular, the styling of elements is not finished yet +(text color, alignment, sizing, etc). The implementation consists of different modules: - - __[`tour`]__ contains the actual GUI code: __state__, __messages__, - __update logic__ and __view logic__. - - __[`renderer`]__ implements a simple renderer for each of the used widgets on - top of the graphics module of [`ggez`]. - - __[`widget`]__ re-exposes Iced's built-in widgets with the renderer type parameter - replaced with the implemented [`renderer`], for convenience. + - __[`tour`]__ contains the actual cross-platform GUI code: __state__, + __messages__, __update logic__ and __view logic__. + - __[`iced_ggez`]__ implements a simple renderer for each of the used widgets + on top of the graphics module of [`ggez`]. + - __[`widget`]__ conditionally re-exposes the correct platform widgets based + on the target architecture. - __[`main`]__ integrates Iced with [`ggez`] and connects the [`tour`] with the [`renderer`]. + - __[`lib`]__ exposes the [`tour`] types and conditionally implements the + WebAssembly entrypoint in the [`web`] module. + +The conditional compilation awkwardness from targetting both native and web +platforms should be handled seamlessly by the `iced` crate in the near future! + +If you want to run it as a native app: + +``` +cd examples/tour +cargo run +``` + +If you want to run it on web, you will need [`wasm-pack`]: + +``` +cd examples/tour +wasm-pack build --target web +``` + +Then, simply serve the directory with any HTTP server. For instance: ``` -cargo run --example tour +python3 -m http.server ``` [![Tour - Iced][gui_gif]][gui_gfycat] [`ggez`]: https://github.com/ggez/ggez -[`tour`]: tour.rs -[`renderer`]: renderer -[`widget`]: widget.rs -[`main`]: main.rs +[`tour`]: src/tour.rs +[`iced_ggez`]: src/iced_ggez +[`widget`]: src/widget.rs +[`main`]: src/main.rs +[`lib`]: src/lib.rs +[`web`]: src/web.rs [personal fork]: https://github.com/hecrj/ggez [add a `FontCache` type]: https://github.com/ggez/ggez/pull/679 [fix some issues with HiDPI]: https://github.com/hecrj/ggez/commit/dfe2fd2423c51a6daf42c75f66dfaeaacd439fb1 diff --git a/examples/tour/src/iced_ggez/main.rs b/examples/tour/src/iced_ggez/main.rs deleted file mode 100644 index 72774d38..00000000 --- a/examples/tour/src/iced_ggez/main.rs +++ /dev/null @@ -1,189 +0,0 @@ -use iced_tour::{iced_ggez, Tour}; - -use ggez; -use ggez::event; -use ggez::filesystem; -use ggez::graphics; -use ggez::input::mouse; - -pub fn main() -> ggez::GameResult { - let (context, event_loop) = { - &mut ggez::ContextBuilder::new("iced", "ggez") - .window_mode(ggez::conf::WindowMode { - width: 1280.0, - height: 1024.0, - resizable: true, - ..ggez::conf::WindowMode::default() - }) - .build()? - }; - - filesystem::mount( - context, - std::path::Path::new(env!("CARGO_MANIFEST_DIR")), - true, - ); - - let state = &mut Game::new(context)?; - - event::run(context, event_loop, state) -} - -struct Game { - spritesheet: graphics::Image, - font: graphics::Font, - images: iced_ggez::ImageCache, - tour: Tour, - - events: Vec, - cache: Option, -} - -impl Game { - fn new(context: &mut ggez::Context) -> ggez::GameResult { - graphics::set_default_filter(context, graphics::FilterMode::Nearest); - - Ok(Game { - spritesheet: graphics::Image::new(context, "/resources/ui.png") - .unwrap(), - font: graphics::Font::new(context, "/resources/Roboto-Regular.ttf") - .unwrap(), - images: iced_ggez::ImageCache::new(), - tour: Tour::new(), - - events: Vec::new(), - cache: Some(iced_native::Cache::default()), - }) - } -} - -impl event::EventHandler for Game { - fn update(&mut self, _ctx: &mut ggez::Context) -> ggez::GameResult { - Ok(()) - } - - fn mouse_button_down_event( - &mut self, - _context: &mut ggez::Context, - _button: mouse::MouseButton, - _x: f32, - _y: f32, - ) { - self.events.push(iced_native::Event::Mouse( - iced_native::input::mouse::Event::Input { - state: iced_native::input::ButtonState::Pressed, - button: iced_native::input::mouse::Button::Left, // TODO: Map `button` - }, - )); - } - - fn mouse_button_up_event( - &mut self, - _context: &mut ggez::Context, - _button: mouse::MouseButton, - _x: f32, - _y: f32, - ) { - self.events.push(iced_native::Event::Mouse( - iced_native::input::mouse::Event::Input { - state: iced_native::input::ButtonState::Released, - button: iced_native::input::mouse::Button::Left, // TODO: Map `button` - }, - )); - } - - fn mouse_motion_event( - &mut self, - _context: &mut ggez::Context, - x: f32, - y: f32, - _dx: f32, - _dy: f32, - ) { - self.events.push(iced_native::Event::Mouse( - iced_native::input::mouse::Event::CursorMoved { x, y }, - )); - } - - fn resize_event( - &mut self, - context: &mut ggez::Context, - width: f32, - height: f32, - ) { - graphics::set_screen_coordinates( - context, - graphics::Rect { - x: 0.0, - y: 0.0, - w: width, - h: height, - }, - ) - .expect("Set screen coordinates"); - } - - fn draw(&mut self, context: &mut ggez::Context) -> ggez::GameResult { - graphics::clear(context, graphics::WHITE); - - let screen = graphics::screen_coordinates(context); - - let (messages, cursor) = { - let view = self.tour.view(); - - let content = iced_ggez::Column::new() - .width(iced_native::Length::Units(screen.w as u16)) - .height(iced_native::Length::Units(screen.h as u16)) - .padding(20) - .align_items(iced_native::Align::Center) - .justify_content(iced_native::Justify::Center) - .push(view); - - let renderer = &mut iced_ggez::Renderer::new( - context, - &mut self.images, - self.spritesheet.clone(), - self.font, - ); - - let mut ui = iced_native::UserInterface::build( - content, - self.cache.take().unwrap(), - renderer, - ); - - let messages = ui.update(self.events.drain(..)); - let cursor = ui.draw(renderer); - - self.cache = Some(ui.into_cache()); - - renderer.flush(); - - (messages, cursor) - }; - - for message in messages { - self.tour.update(message); - } - - let cursor_type = into_cursor_type(cursor); - - if mouse::cursor_type(context) != cursor_type { - mouse::set_cursor_type(context, cursor_type); - } - - graphics::present(context)?; - Ok(()) - } -} - -fn into_cursor_type(cursor: iced_native::MouseCursor) -> mouse::MouseCursor { - match cursor { - iced_native::MouseCursor::OutOfBounds => mouse::MouseCursor::Default, - iced_native::MouseCursor::Idle => mouse::MouseCursor::Default, - iced_native::MouseCursor::Pointer => mouse::MouseCursor::Hand, - iced_native::MouseCursor::Working => mouse::MouseCursor::Progress, - iced_native::MouseCursor::Grab => mouse::MouseCursor::Grab, - iced_native::MouseCursor::Grabbing => mouse::MouseCursor::Grabbing, - } -} diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs new file mode 100644 index 00000000..72774d38 --- /dev/null +++ b/examples/tour/src/main.rs @@ -0,0 +1,189 @@ +use iced_tour::{iced_ggez, Tour}; + +use ggez; +use ggez::event; +use ggez::filesystem; +use ggez::graphics; +use ggez::input::mouse; + +pub fn main() -> ggez::GameResult { + let (context, event_loop) = { + &mut ggez::ContextBuilder::new("iced", "ggez") + .window_mode(ggez::conf::WindowMode { + width: 1280.0, + height: 1024.0, + resizable: true, + ..ggez::conf::WindowMode::default() + }) + .build()? + }; + + filesystem::mount( + context, + std::path::Path::new(env!("CARGO_MANIFEST_DIR")), + true, + ); + + let state = &mut Game::new(context)?; + + event::run(context, event_loop, state) +} + +struct Game { + spritesheet: graphics::Image, + font: graphics::Font, + images: iced_ggez::ImageCache, + tour: Tour, + + events: Vec, + cache: Option, +} + +impl Game { + fn new(context: &mut ggez::Context) -> ggez::GameResult { + graphics::set_default_filter(context, graphics::FilterMode::Nearest); + + Ok(Game { + spritesheet: graphics::Image::new(context, "/resources/ui.png") + .unwrap(), + font: graphics::Font::new(context, "/resources/Roboto-Regular.ttf") + .unwrap(), + images: iced_ggez::ImageCache::new(), + tour: Tour::new(), + + events: Vec::new(), + cache: Some(iced_native::Cache::default()), + }) + } +} + +impl event::EventHandler for Game { + fn update(&mut self, _ctx: &mut ggez::Context) -> ggez::GameResult { + Ok(()) + } + + fn mouse_button_down_event( + &mut self, + _context: &mut ggez::Context, + _button: mouse::MouseButton, + _x: f32, + _y: f32, + ) { + self.events.push(iced_native::Event::Mouse( + iced_native::input::mouse::Event::Input { + state: iced_native::input::ButtonState::Pressed, + button: iced_native::input::mouse::Button::Left, // TODO: Map `button` + }, + )); + } + + fn mouse_button_up_event( + &mut self, + _context: &mut ggez::Context, + _button: mouse::MouseButton, + _x: f32, + _y: f32, + ) { + self.events.push(iced_native::Event::Mouse( + iced_native::input::mouse::Event::Input { + state: iced_native::input::ButtonState::Released, + button: iced_native::input::mouse::Button::Left, // TODO: Map `button` + }, + )); + } + + fn mouse_motion_event( + &mut self, + _context: &mut ggez::Context, + x: f32, + y: f32, + _dx: f32, + _dy: f32, + ) { + self.events.push(iced_native::Event::Mouse( + iced_native::input::mouse::Event::CursorMoved { x, y }, + )); + } + + fn resize_event( + &mut self, + context: &mut ggez::Context, + width: f32, + height: f32, + ) { + graphics::set_screen_coordinates( + context, + graphics::Rect { + x: 0.0, + y: 0.0, + w: width, + h: height, + }, + ) + .expect("Set screen coordinates"); + } + + fn draw(&mut self, context: &mut ggez::Context) -> ggez::GameResult { + graphics::clear(context, graphics::WHITE); + + let screen = graphics::screen_coordinates(context); + + let (messages, cursor) = { + let view = self.tour.view(); + + let content = iced_ggez::Column::new() + .width(iced_native::Length::Units(screen.w as u16)) + .height(iced_native::Length::Units(screen.h as u16)) + .padding(20) + .align_items(iced_native::Align::Center) + .justify_content(iced_native::Justify::Center) + .push(view); + + let renderer = &mut iced_ggez::Renderer::new( + context, + &mut self.images, + self.spritesheet.clone(), + self.font, + ); + + let mut ui = iced_native::UserInterface::build( + content, + self.cache.take().unwrap(), + renderer, + ); + + let messages = ui.update(self.events.drain(..)); + let cursor = ui.draw(renderer); + + self.cache = Some(ui.into_cache()); + + renderer.flush(); + + (messages, cursor) + }; + + for message in messages { + self.tour.update(message); + } + + let cursor_type = into_cursor_type(cursor); + + if mouse::cursor_type(context) != cursor_type { + mouse::set_cursor_type(context, cursor_type); + } + + graphics::present(context)?; + Ok(()) + } +} + +fn into_cursor_type(cursor: iced_native::MouseCursor) -> mouse::MouseCursor { + match cursor { + iced_native::MouseCursor::OutOfBounds => mouse::MouseCursor::Default, + iced_native::MouseCursor::Idle => mouse::MouseCursor::Default, + iced_native::MouseCursor::Pointer => mouse::MouseCursor::Hand, + iced_native::MouseCursor::Working => mouse::MouseCursor::Progress, + iced_native::MouseCursor::Grab => mouse::MouseCursor::Grab, + iced_native::MouseCursor::Grabbing => mouse::MouseCursor::Grabbing, + } +} diff --git a/examples/tour/src/tour.rs b/examples/tour/src/tour.rs index fd9c2dde..04740fce 100644 --- a/examples/tour/src/tour.rs +++ b/examples/tour/src/tour.rs @@ -16,7 +16,7 @@ impl Tour { steps: Steps::new(), back_button: button::State::new(), next_button: button::State::new(), - debug: true, + debug: false, } } diff --git a/web/src/widget/button.rs b/web/src/widget/button.rs index 36b35901..23a4165a 100644 --- a/web/src/widget/button.rs +++ b/web/src/widget/button.rs @@ -2,7 +2,7 @@ use crate::{Bus, Element, Widget}; use dodrio::bumpalo; -pub type Button<'a, Message> = iced_core::Button<'a, Message>; +pub use iced_core::button::*; impl<'a, Message> Widget for Button<'a, Message> where @@ -29,6 +29,8 @@ where }); } + // TODO: Complete styling + node.finish() } } diff --git a/web/src/widget/checkbox.rs b/web/src/widget/checkbox.rs index 34995781..72f0a2aa 100644 --- a/web/src/widget/checkbox.rs +++ b/web/src/widget/checkbox.rs @@ -20,6 +20,7 @@ where let event_bus = bus.clone(); let msg = (self.on_toggle)(!self.is_checked); + // TODO: Complete styling label(bump) .children(vec![ input(bump) diff --git a/web/src/widget/column.rs b/web/src/widget/column.rs index 99491647..becd6bc6 100644 --- a/web/src/widget/column.rs +++ b/web/src/widget/column.rs @@ -18,6 +18,7 @@ impl<'a, Message> Widget for Column<'a, Message> { .map(|element| element.widget.node(bump, publish)) .collect(); + // TODO: Complete styling div(bump) .attr("style", "display: flex; flex-direction: column") .children(children) diff --git a/web/src/widget/image.rs b/web/src/widget/image.rs index 48ff539f..fd4ff0df 100644 --- a/web/src/widget/image.rs +++ b/web/src/widget/image.rs @@ -29,6 +29,8 @@ impl<'a, Message> Widget for Image<'a> { } } + // TODO: Complete styling + image.finish() } } diff --git a/web/src/widget/radio.rs b/web/src/widget/radio.rs index 9063770a..d249ad26 100644 --- a/web/src/widget/radio.rs +++ b/web/src/widget/radio.rs @@ -20,6 +20,7 @@ where let event_bus = bus.clone(); let on_click = self.on_click; + // TODO: Complete styling label(bump) .attr("style", "display: block") .children(vec![ diff --git a/web/src/widget/row.rs b/web/src/widget/row.rs index d4f4c4a0..cf6ae594 100644 --- a/web/src/widget/row.rs +++ b/web/src/widget/row.rs @@ -18,6 +18,7 @@ impl<'a, Message> Widget for Row<'a, Message> { .map(|element| element.widget.node(bump, publish)) .collect(); + // TODO: Complete styling div(bump) .attr("style", "display: flex; flex-direction: row") .children(children) diff --git a/web/src/widget/slider.rs b/web/src/widget/slider.rs index 19668025..54b2fdf6 100644 --- a/web/src/widget/slider.rs +++ b/web/src/widget/slider.rs @@ -2,9 +2,7 @@ use crate::{Bus, Element, Widget}; use dodrio::bumpalo; -pub type Slider<'a, Message> = iced_core::Slider<'a, Message>; - -pub use iced_core::slider::State; +pub use iced_core::slider::*; impl<'a, Message> Widget for Slider<'a, Message> where @@ -28,6 +26,7 @@ where let event_bus = bus.clone(); // TODO: Make `step` configurable + // TODO: Complete styling label(bump) .children(vec![input(bump) .attr("type", "range") diff --git a/web/src/widget/text.rs b/web/src/widget/text.rs index ef9170ef..41ccd6fc 100644 --- a/web/src/widget/text.rs +++ b/web/src/widget/text.rs @@ -14,6 +14,7 @@ impl<'a, Message> Widget for Text { let content = bumpalo::format!(in bump, "{}", self.content); let size = bumpalo::format!(in bump, "font-size: {}px", self.size.unwrap_or(20)); + // TODO: Complete styling p(bump) .attr("style", size.into_bump_str()) .children(vec![text(content.into_bump_str())]) -- cgit From 05889ecea82365b2af812a2902f1722660235259 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 21 Sep 2019 13:42:58 +0200 Subject: Add `wasm-pack` link --- examples/README.md | 4 +++- examples/tour/README.md | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/examples/README.md b/examples/README.md index 5ca0eb69..4e83faf1 100644 --- a/examples/README.md +++ b/examples/README.md @@ -29,7 +29,7 @@ The implementation consists of different modules: - __[`widget`]__ conditionally re-exposes the correct platform widgets based on the target architecture. - __[`main`]__ integrates Iced with [`ggez`] and connects the [`tour`] with - the [`renderer`]. + the native [`renderer`]. - __[`lib`]__ exposes the [`tour`] types and conditionally implements the WebAssembly entrypoint in the [`web`] module. @@ -61,10 +61,12 @@ python3 -m http.server [`ggez`]: https://github.com/ggez/ggez [`tour`]: tour/src/tour.rs [`iced_ggez`]: tour/src/iced_ggez +[`renderer`]: src/iced_ggez/renderer [`widget`]: tour/src/widget.rs [`main`]: tour/src/main.rs [`lib`]: tour/src/lib.rs [`web`]: tour/src/web.rs +[`wasm-pack`]: https://rustwasm.github.io/wasm-pack/installer/ [personal fork]: https://github.com/hecrj/ggez [add a `FontCache` type]: https://github.com/ggez/ggez/pull/679 [fix some issues with HiDPI]: https://github.com/hecrj/ggez/commit/dfe2fd2423c51a6daf42c75f66dfaeaacd439fb1 diff --git a/examples/tour/README.md b/examples/tour/README.md index a33de7f5..7ef1a212 100644 --- a/examples/tour/README.md +++ b/examples/tour/README.md @@ -21,7 +21,7 @@ The implementation consists of different modules: - __[`widget`]__ conditionally re-exposes the correct platform widgets based on the target architecture. - __[`main`]__ integrates Iced with [`ggez`] and connects the [`tour`] with - the [`renderer`]. + the native [`renderer`]. - __[`lib`]__ exposes the [`tour`] types and conditionally implements the WebAssembly entrypoint in the [`web`] module. @@ -53,10 +53,12 @@ python3 -m http.server [`ggez`]: https://github.com/ggez/ggez [`tour`]: src/tour.rs [`iced_ggez`]: src/iced_ggez +[`renderer`]: src/iced_ggez/renderer [`widget`]: src/widget.rs [`main`]: src/main.rs [`lib`]: src/lib.rs [`web`]: src/web.rs +[`wasm-pack`]: https://rustwasm.github.io/wasm-pack/installer/ [personal fork]: https://github.com/hecrj/ggez [add a `FontCache` type]: https://github.com/ggez/ggez/pull/679 [fix some issues with HiDPI]: https://github.com/hecrj/ggez/commit/dfe2fd2423c51a6daf42c75f66dfaeaacd439fb1 -- cgit From a975754ab0983b9dc12bc0d0fc63049ee812d1c5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 24 Sep 2019 14:38:40 +0200 Subject: Remove `stretch` optional dependency in `core` --- core/Cargo.toml | 5 --- core/src/align.rs | 26 ------------- core/src/justify.rs | 17 -------- native/Cargo.toml | 2 +- native/src/style.rs | 110 +++++++++++++++++++++++----------------------------- 5 files changed, 49 insertions(+), 111 deletions(-) diff --git a/core/Cargo.toml b/core/Cargo.toml index 40a8bcfb..a244bcba 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -6,8 +6,3 @@ edition = "2018" description = "The essential concepts of Iced" license = "MIT" repository = "https://github.com/hecrj/iced" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -stretch = { version = "0.2", optional = true } diff --git a/core/src/align.rs b/core/src/align.rs index 5876e0f8..5dbd658d 100644 --- a/core/src/align.rs +++ b/core/src/align.rs @@ -19,29 +19,3 @@ pub enum Align { /// Stretch over the cross axis. Stretch, } - -#[cfg(feature = "stretch")] -#[doc(hidden)] -impl From for stretch::style::AlignItems { - fn from(align: Align) -> Self { - match align { - Align::Start => stretch::style::AlignItems::FlexStart, - Align::Center => stretch::style::AlignItems::Center, - Align::End => stretch::style::AlignItems::FlexEnd, - Align::Stretch => stretch::style::AlignItems::Stretch, - } - } -} - -#[cfg(feature = "stretch")] -#[doc(hidden)] -impl From for stretch::style::AlignSelf { - fn from(align: Align) -> Self { - match align { - Align::Start => stretch::style::AlignSelf::FlexStart, - Align::Center => stretch::style::AlignSelf::Center, - Align::End => stretch::style::AlignSelf::FlexEnd, - Align::Stretch => stretch::style::AlignSelf::Stretch, - } - } -} diff --git a/core/src/justify.rs b/core/src/justify.rs index 878573b0..53aa7319 100644 --- a/core/src/justify.rs +++ b/core/src/justify.rs @@ -25,20 +25,3 @@ pub enum Justify { /// Place items with evenly distributed space. SpaceEvenly, } - -#[cfg(feature = "stretch")] -#[doc(hidden)] -impl From for stretch::style::JustifyContent { - fn from(justify: Justify) -> Self { - match justify { - Justify::Start => stretch::style::JustifyContent::FlexStart, - Justify::Center => stretch::style::JustifyContent::Center, - Justify::End => stretch::style::JustifyContent::FlexEnd, - Justify::SpaceBetween => { - stretch::style::JustifyContent::SpaceBetween - } - Justify::SpaceAround => stretch::style::JustifyContent::SpaceAround, - Justify::SpaceEvenly => stretch::style::JustifyContent::SpaceEvenly, - } - } -} diff --git a/native/Cargo.toml b/native/Cargo.toml index 1dbccdee..5f7e5e41 100644 --- a/native/Cargo.toml +++ b/native/Cargo.toml @@ -11,7 +11,7 @@ repository = "https://github.com/hecrj/iced" features = ["winit"] [dependencies] -iced_core = { version = "0.1.0-alpha", features = ["stretch"], path = "../core" } +iced_core = { version = "0.1.0-alpha", path = "../core" } stretch = "0.2" twox-hash = "1.5" diff --git a/native/src/style.rs b/native/src/style.rs index 3a75c925..b1c49fd4 100644 --- a/native/src/style.rs +++ b/native/src/style.rs @@ -1,7 +1,6 @@ use crate::{Align, Justify, Length}; -use std::hash::{Hash, Hasher}; -use stretch::{geometry, style}; +use stretch::style; /// The appearance of a [`Node`]. /// @@ -9,12 +8,29 @@ use stretch::{geometry, style}; #[derive(Debug, Clone, Copy)] pub struct Style(pub(crate) style::Style); +impl Default for Style { + fn default() -> Style { + Style::new() + } +} + impl Style { + /// Creates a new [`Style`]. + /// + /// [`Style`]: struct.Style.html + pub fn new() -> Self { + Style(style::Style { + align_items: style::AlignItems::FlexStart, + justify_content: style::JustifyContent::FlexStart, + ..style::Style::default() + }) + } + /// Defines the width of a [`Node`]. /// /// [`Node`]: struct.Node.html pub fn width(mut self, width: Length) -> Self { - self.0.size.width = length_to_dimension(width); + self.0.size.width = into_dimension(width); self } @@ -22,7 +38,7 @@ impl Style { /// /// [`Node`]: struct.Node.html pub fn height(mut self, height: Length) -> Self { - self.0.size.height = length_to_dimension(height); + self.0.size.height = into_dimension(height); self } @@ -30,7 +46,7 @@ impl Style { /// /// [`Node`]: struct.Node.html pub fn min_width(mut self, min_width: Length) -> Self { - self.0.min_size.width = length_to_dimension(min_width); + self.0.min_size.width = into_dimension(min_width); self } @@ -38,7 +54,7 @@ impl Style { /// /// [`Node`]: struct.Node.html pub fn max_width(mut self, max_width: Length) -> Self { - self.0.max_size.width = length_to_dimension(max_width); + self.0.max_size.width = into_dimension(max_width); self } @@ -46,7 +62,7 @@ impl Style { /// /// [`Node`]: struct.Node.html pub fn min_height(mut self, min_height: Length) -> Self { - self.0.min_size.height = length_to_dimension(min_height); + self.0.min_size.height = into_dimension(min_height); self } @@ -54,17 +70,17 @@ impl Style { /// /// [`Node`]: struct.Node.html pub fn max_height(mut self, max_height: Length) -> Self { - self.0.max_size.height = length_to_dimension(max_height); + self.0.max_size.height = into_dimension(max_height); self } pub(crate) fn align_items(mut self, align: Align) -> Self { - self.0.align_items = align.into(); + self.0.align_items = into_align_items(align); self } pub(crate) fn justify_content(mut self, justify: Justify) -> Self { - self.0.justify_content = justify.into(); + self.0.justify_content = into_justify_content(justify); self } @@ -80,7 +96,7 @@ impl Style { /// [`Row`]: widget/struct.Row.html pub fn align_self(mut self, align: Option) -> Self { self.0.align_self = match align { - Some(align) => align.into(), + Some(align) => into_align_self(align), None => stretch::style::AlignSelf::Auto, }; @@ -102,7 +118,7 @@ impl Style { } } -fn length_to_dimension(length: Length) -> style::Dimension { +fn into_dimension(length: Length) -> style::Dimension { match length { Length::Shrink => style::Dimension::Undefined, Length::Fill => style::Dimension::Percent(1.0), @@ -110,61 +126,31 @@ fn length_to_dimension(length: Length) -> style::Dimension { } } -impl Default for Style { - fn default() -> Style { - Style(style::Style { - align_items: style::AlignItems::FlexStart, - justify_content: style::JustifyContent::FlexStart, - ..style::Style::default() - }) +fn into_align_items(align: Align) -> style::AlignItems { + match align { + Align::Start => style::AlignItems::FlexStart, + Align::Center => style::AlignItems::Center, + Align::End => style::AlignItems::FlexEnd, + Align::Stretch => style::AlignItems::Stretch, } } -impl Hash for Style { - fn hash(&self, state: &mut H) { - hash_size(&self.0.size, state); - hash_size(&self.0.min_size, state); - hash_size(&self.0.max_size, state); - - hash_rect(&self.0.margin, state); - - (self.0.flex_direction as u8).hash(state); - (self.0.align_items as u8).hash(state); - (self.0.justify_content as u8).hash(state); - (self.0.align_self as u8).hash(state); - (self.0.flex_grow as u32).hash(state); +fn into_align_self(align: Align) -> style::AlignSelf { + match align { + Align::Start => style::AlignSelf::FlexStart, + Align::Center => style::AlignSelf::Center, + Align::End => style::AlignSelf::FlexEnd, + Align::Stretch => style::AlignSelf::Stretch, } } -fn hash_size( - size: &geometry::Size, - state: &mut H, -) { - hash_dimension(size.width, state); - hash_dimension(size.height, state); -} - -fn hash_rect( - rect: &geometry::Rect, - state: &mut H, -) { - hash_dimension(rect.start, state); - hash_dimension(rect.end, state); - hash_dimension(rect.top, state); - hash_dimension(rect.bottom, state); -} - -fn hash_dimension(dimension: style::Dimension, state: &mut H) { - match dimension { - style::Dimension::Undefined => state.write_u8(0), - style::Dimension::Auto => state.write_u8(1), - style::Dimension::Points(points) => { - state.write_u8(2); - (points as u32).hash(state); - } - style::Dimension::Percent(percent) => { - state.write_u8(3); - (percent as u32).hash(state); - } +fn into_justify_content(justify: Justify) -> style::JustifyContent { + match justify { + Justify::Start => style::JustifyContent::FlexStart, + Justify::Center => style::JustifyContent::Center, + Justify::End => style::JustifyContent::FlexEnd, + Justify::SpaceBetween => style::JustifyContent::SpaceBetween, + Justify::SpaceAround => style::JustifyContent::SpaceAround, + Justify::SpaceEvenly => style::JustifyContent::SpaceEvenly, } } -- cgit From c08171e89e9336f87a7eb27881897cfa34529125 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 24 Sep 2019 14:51:12 +0200 Subject: Clarify `README` --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5301144e..f7035fb4 100644 --- a/README.md +++ b/README.md @@ -156,11 +156,10 @@ its own standalone crate, as it could potentially benefit other engines and applications. I thought it was a great idea, and after a bit of work... Iced is here! -As an interesting note, Iced does not rely on reference counting and interior -mutability. There is not a single `Rc`, `RefCell`, or similar used -directly in the library. As a consequence, compiler guarantees stay intact and -many kinds of pesky bugs and runtime errors are banished. No spooky action at -a distance! +As an interesting note, the core of Iced does not rely on interior mutability. +Usage of types like `RefCell` is restricted to runtime boundaries. As a +consequence, compiler guarantees stay intact and many kinds of pesky bugs and +runtime errors are banished. No spooky action at a distance! [this pull request]: https://github.com/hecrj/coffee/pull/35 [`stretch`]: https://github.com/vislyhq/stretch -- cgit From 5e28f80af8349be2638eb80d2a06c81fd14d631c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 24 Sep 2019 15:15:34 +0200 Subject: Improve documentation --- core/src/length.rs | 1 + core/src/widget.rs | 16 +++++++++++----- web/src/lib.rs | 4 ++-- web/src/widget.rs | 10 ++++++++-- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/core/src/length.rs b/core/src/length.rs index 3547b246..0e670038 100644 --- a/core/src/length.rs +++ b/core/src/length.rs @@ -1,3 +1,4 @@ +/// The strategy used to fill space in a specific dimension. #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub enum Length { Fill, diff --git a/core/src/widget.rs b/core/src/widget.rs index 4700be0e..f9d4bf2a 100644 --- a/core/src/widget.rs +++ b/core/src/widget.rs @@ -7,21 +7,27 @@ //! ``` //! use iced_core::{button, Button}; //! ``` +mod checkbox; mod column; +mod image; +mod radio; mod row; pub mod button; -pub mod checkbox; -pub mod image; -pub mod radio; pub mod slider; pub mod text; +#[doc(no_inline)] pub use button::Button; + +#[doc(no_inline)] +pub use slider::Slider; + +#[doc(no_inline)] +pub use text::Text; + pub use checkbox::Checkbox; pub use column::Column; pub use image::Image; pub use radio::Radio; pub use row::Row; -pub use slider::Slider; -pub use text::Text; diff --git a/web/src/lib.rs b/web/src/lib.rs index 09ca3460..caf17df5 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -4,11 +4,11 @@ use std::cell::RefCell; mod bus; mod element; -mod widget; +pub mod widget; pub use bus::Bus; pub use element::Element; -pub use iced_core::{Align, Color, Length}; +pub use iced_core::{Align, Color, Justify, Length}; pub use widget::*; pub trait UserInterface { diff --git a/web/src/widget.rs b/web/src/widget.rs index 67ca8e81..88b2efc9 100644 --- a/web/src/widget.rs +++ b/web/src/widget.rs @@ -11,14 +11,20 @@ mod image; mod radio; mod row; +#[doc(no_inline)] pub use button::Button; + +#[doc(no_inline)] +pub use slider::Slider; + +#[doc(no_inline)] +pub use text::Text; + pub use checkbox::Checkbox; pub use column::Column; pub use image::Image; pub use radio::Radio; pub use row::Row; -pub use slider::Slider; -pub use text::Text; pub trait Widget { fn node<'b>( -- cgit From 05c7c39ecb8910c75b82dc4052a7720fb2d42b4a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 24 Sep 2019 15:23:32 +0200 Subject: Build tour for WebAssembly in CI --- .github/workflows/integration.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index fa1cb997..eb54d170 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -7,10 +7,15 @@ jobs: matrix: os: [ubuntu-latest, windows-latest, macOS-latest] rust: [stable, beta] + include: + - os: ubuntu-latest + rust: stable + targets: 'wasm32-unknown-unknown' steps: - uses: hecrj/setup-rust-action@v1 with: rust-version: ${{ matrix.rust }} + targets: ${{ matrix.targets }} - name: Install libinput if: matrix.os == 'ubuntu-latest' run: | @@ -19,3 +24,6 @@ jobs: - uses: actions/checkout@master - name: Run tests run: cargo test --verbose --all --all-features + - name: Build tour for WebAssembly + if: matrix.targets == 'wasm32-unknown-unknown' + run: cargo build --verbose --package iced_tour --lib --target wasm32-unknown-unknown -- cgit