From d3553adf278e5b616fbd885f321faa83a4d24b56 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 18 Nov 2019 07:16:19 +0100 Subject: Write documentation for `iced_core` --- core/src/background.rs | 2 ++ core/src/color.rs | 3 ++ core/src/command.rs | 23 +++++++++++++++ core/src/font.rs | 10 +++++++ core/src/length.rs | 12 ++++++++ core/src/lib.rs | 24 +++++++++++++--- core/src/vector.rs | 7 +++++ core/src/widget/button.rs | 50 +++++++++++++------------------- core/src/widget/column.rs | 1 + core/src/widget/container.rs | 4 +++ core/src/widget/row.rs | 1 + core/src/widget/scrollable.rs | 35 +++++++++++++++++++++++ core/src/widget/slider.rs | 13 ++++----- core/src/widget/text.rs | 8 +++++- core/src/widget/text_input.rs | 66 ++++++++++++++++++++++++++++++++++++++++++- 15 files changed, 215 insertions(+), 44 deletions(-) (limited to 'core') diff --git a/core/src/background.rs b/core/src/background.rs index 59b67a2c..98047172 100644 --- a/core/src/background.rs +++ b/core/src/background.rs @@ -1,7 +1,9 @@ use crate::Color; +/// The background of some element. #[derive(Debug, Clone, Copy, PartialEq)] pub enum Background { + /// A solid color Color(Color), // TODO: Add gradient and image variants } diff --git a/core/src/color.rs b/core/src/color.rs index 150bd05b..ec48c185 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -25,6 +25,9 @@ impl Color { a: 1.0, }; + /// Converts the [`Color`] into its linear values. + /// + /// [`Color`]: struct.Color.html pub fn into_linear(self) -> [f32; 4] { // As described in: // https://en.wikipedia.org/wiki/SRGB#The_reverse_transformation diff --git a/core/src/command.rs b/core/src/command.rs index e1c865dd..698105aa 100644 --- a/core/src/command.rs +++ b/core/src/command.rs @@ -1,16 +1,30 @@ use futures::future::{BoxFuture, Future, FutureExt}; +/// A collection of async operations. +/// +/// You should be able to turn a future easily into a [`Command`], eiter by +/// using the `From` trait or [`Command::perform`]. +/// +/// [`Command`]: struct.Command.html pub struct Command { futures: Vec>, } impl Command { + /// Creates an empty [`Command`]. + /// + /// In other words, a [`Command`] that does nothing. + /// + /// [`Command`]: struct.Command.html pub fn none() -> Self { Self { futures: Vec::new(), } } + /// Creates a [`Command`] that performs the action of the given future. + /// + /// [`Command`]: struct.Command.html pub fn perform( future: impl Future + 'static + Send, f: impl Fn(T) -> A + 'static + Send, @@ -20,12 +34,21 @@ impl Command { } } + /// Creates a [`Command`] that performs the actions of all the givens + /// futures. + /// + /// Once this command is run, all the futures will be exectued at once. + /// + /// [`Command`]: struct.Command.html pub fn batch(commands: impl Iterator>) -> Self { Self { futures: commands.flat_map(|command| command.futures).collect(), } } + /// Converts a [`Command`] into its underlying list of futures. + /// + /// [`Command`]: struct.Command.html pub fn futures(self) -> Vec> { self.futures } diff --git a/core/src/font.rs b/core/src/font.rs index 75ba6a72..be49c825 100644 --- a/core/src/font.rs +++ b/core/src/font.rs @@ -1,8 +1,18 @@ +/// A font. #[derive(Debug, Clone, Copy)] pub enum Font { + /// The default font. + /// + /// This is normally a font configured in a renderer or loaded from the + /// system. Default, + + /// An external font. External { + /// The name of the external font name: &'static str, + + /// The bytes of the external font bytes: &'static [u8], }, } diff --git a/core/src/length.rs b/core/src/length.rs index 73c227d8..63ba6207 100644 --- a/core/src/length.rs +++ b/core/src/length.rs @@ -1,12 +1,24 @@ /// The strategy used to fill space in a specific dimension. #[derive(Debug, Clone, Copy, PartialEq, Hash)] pub enum Length { + /// Fill all the remaining space Fill, + + /// Fill the least amount of space Shrink, + + /// Fill a fixed amount of space Units(u16), } impl Length { + /// Returns the _fill factor_ of the [`Length`]. + /// + /// The _fill factor_ is a relative unit describing how much of the + /// remaining space should be filled when compared to other elements. It + /// is only meant to be used by layout engines. + /// + /// [`Length`]: enum.Length.html pub fn fill_factor(&self) -> u16 { match self { Length::Fill => 1, diff --git a/core/src/lib.rs b/core/src/lib.rs index 3816f8a2..692e40d4 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,10 +1,22 @@ +//! The core library of [Iced]. +//! +//! This library holds basic types that can be reused and re-exported in +//! different runtime implementations. For instance, both [`iced_native`] and +//! [`iced_web`] are built on top of `iced_core`. +//! +//! [Iced]: https://github.com/hecrj/iced +//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native +//! [`iced_web`]: https://github.com/hecrj/iced/tree/master/web +#![deny(missing_docs)] +#![deny(missing_debug_implementations)] +#![deny(unused_results)] +#![deny(unsafe_code)] +#![deny(rust_2018_idioms)] pub mod widget; mod align; mod background; mod color; -#[cfg(feature = "command")] -mod command; mod font; mod length; mod point; @@ -14,11 +26,15 @@ mod vector; pub use align::Align; pub use background::Background; pub use color::Color; -#[cfg(feature = "command")] -pub use command::Command; pub use font::Font; pub use length::Length; pub use point::Point; pub use rectangle::Rectangle; pub use vector::Vector; pub use widget::*; + +#[cfg(feature = "command")] +mod command; + +#[cfg(feature = "command")] +pub use command::Command; diff --git a/core/src/vector.rs b/core/src/vector.rs index 92bf64ff..7d87343a 100644 --- a/core/src/vector.rs +++ b/core/src/vector.rs @@ -1,7 +1,14 @@ /// A 2D vector. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Vector { + /// The X component of the [`Vector`] + /// + /// [`Vector`]: struct.Vector.html pub x: T, + + /// The Y component of the [`Vector`] + /// + /// [`Vector`]: struct.Vector.html pub y: T, } diff --git a/core/src/widget/button.rs b/core/src/widget/button.rs index e7961284..0ac88671 100644 --- a/core/src/widget/button.rs +++ b/core/src/widget/button.rs @@ -8,40 +8,22 @@ use crate::{Background, Length}; /// A generic widget that produces a message when clicked. +#[allow(missing_docs)] +#[derive(Debug)] pub struct Button<'a, Message, Element> { - /// The current state of the button pub state: &'a mut State, - pub content: Element, - - /// The message to produce when the button is pressed pub on_press: Option, - pub width: Length, - pub min_width: u32, - pub padding: u16, - pub background: Option, - pub border_radius: u16, } -impl<'a, Message, Element> std::fmt::Debug for Button<'a, Message, Element> -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("on_press", &self.on_press) - .finish() - } -} - impl<'a, Message, Element> Button<'a, Message, Element> { - /// Creates a new [`Button`] with some local [`State`] and the given label. + /// Creates a new [`Button`] with some local [`State`] and the given + /// content. /// /// [`Button`]: struct.Button.html /// [`State`]: struct.State.html @@ -69,21 +51,34 @@ impl<'a, Message, Element> Button<'a, Message, Element> { self } + /// Sets the minimum width of the [`Button`]. + /// + /// [`Button`]: struct.Button.html pub fn min_width(mut self, min_width: u32) -> Self { self.min_width = min_width; self } + /// Sets the padding of the [`Button`]. + /// + /// [`Button`]: struct.Button.html pub fn padding(mut self, padding: u16) -> Self { self.padding = padding; self } + /// Sets the [`Background`] of the [`Button`]. + /// + /// [`Button`]: struct.Button.html + /// [`Background`]: ../../struct.Background.html pub fn background(mut self, background: Background) -> Self { self.background = Some(background); self } + /// Sets the border radius of the [`Button`]. + /// + /// [`Button`]: struct.Button.html pub fn border_radius(mut self, border_radius: u16) -> Self { self.border_radius = border_radius; self @@ -103,6 +98,9 @@ impl<'a, Message, Element> Button<'a, Message, Element> { /// [`Button`]: struct.Button.html #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub struct State { + /// Whether the [`Button`] is currently being pressed. + /// + /// [`Button`]: struct.Button.html pub is_pressed: bool, } @@ -113,12 +111,4 @@ impl State { 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 - } } diff --git a/core/src/widget/column.rs b/core/src/widget/column.rs index 4a97ad98..34dbecf7 100644 --- a/core/src/widget/column.rs +++ b/core/src/widget/column.rs @@ -7,6 +7,7 @@ use std::u32; /// A [`Column`] will try to fill the horizontal space of its container. /// /// [`Column`]: struct.Column.html +#[allow(missing_docs)] pub struct Column { pub spacing: u16, pub padding: u16, diff --git a/core/src/widget/container.rs b/core/src/widget/container.rs index 9bc92fe0..3fe340d1 100644 --- a/core/src/widget/container.rs +++ b/core/src/widget/container.rs @@ -2,7 +2,11 @@ use crate::{Align, Length}; use std::u32; +/// An element decorating some content. +/// +/// It is normally used for alignment purposes. #[derive(Debug)] +#[allow(missing_docs)] pub struct Container { pub width: Length, pub height: Length, diff --git a/core/src/widget/row.rs b/core/src/widget/row.rs index 3d882b47..d2b23bdb 100644 --- a/core/src/widget/row.rs +++ b/core/src/widget/row.rs @@ -7,6 +7,7 @@ use std::u32; /// A [`Row`] will try to fill the horizontal space of its container. /// /// [`Row`]: struct.Row.html +#[allow(missing_docs)] pub struct Row { pub spacing: u16, pub padding: u16, diff --git a/core/src/widget/scrollable.rs b/core/src/widget/scrollable.rs index 7f2f0e99..da03cd93 100644 --- a/core/src/widget/scrollable.rs +++ b/core/src/widget/scrollable.rs @@ -1,7 +1,11 @@ +//! Navigate an endless amount of content with a scrollbar. use crate::{Align, Column, Length, Point, Rectangle}; use std::u32; +/// A widget that can vertically display an infinite amount of content with a +/// scrollbar. +#[allow(missing_docs)] #[derive(Debug)] pub struct Scrollable<'a, Element> { pub state: &'a mut State, @@ -11,6 +15,10 @@ pub struct Scrollable<'a, Element> { } impl<'a, Element> Scrollable<'a, Element> { + /// Creates a new [`Scrollable`] with the given [`State`]. + /// + /// [`Scrollable`]: struct.Scrollable.html + /// [`State`]: struct.State.html pub fn new(state: &'a mut State) -> Self { Scrollable { state, @@ -90,17 +98,30 @@ impl<'a, Element> Scrollable<'a, Element> { } } +/// The local state of a [`Scrollable`]. +/// +/// [`Scrollable`]: struct.Scrollable.html #[derive(Debug, Clone, Copy, Default)] pub struct State { + /// The position where the scrollbar was grabbed at, if it's currently + /// grabbed. pub scrollbar_grabbed_at: Option, offset: u32, } impl State { + /// Creates a new [`State`] with the scrollbar located at the top. + /// + /// [`State`]: struct.State.html pub fn new() -> Self { State::default() } + /// Apply a scrolling offset to the current [`State`], given the bounds of + /// the [`Scrollable`] and its contents. + /// + /// [`Scrollable`]: struct.Scrollable.html + /// [`State`]: struct.State.html pub fn scroll( &mut self, delta_y: f32, @@ -117,6 +138,14 @@ impl State { as u32; } + /// Moves the scroll position to a relative amount, given the bounds of + /// the [`Scrollable`] and its contents. + /// + /// `0` represents scrollbar at the top, while `1` represents scrollbar at + /// the bottom. + /// + /// [`Scrollable`]: struct.Scrollable.html + /// [`State`]: struct.State.html pub fn scroll_to( &mut self, percentage: f32, @@ -127,6 +156,11 @@ impl State { .max(0.0) as u32; } + /// Returns the current scrolling offset of the [`State`], given the bounds + /// of the [`Scrollable`] and its contents. + /// + /// [`Scrollable`]: struct.Scrollable.html + /// [`State`]: struct.State.html pub fn offset(&self, bounds: Rectangle, content_bounds: Rectangle) -> u32 { let hidden_content = (content_bounds.height - bounds.height).max(0.0).round() as u32; @@ -134,6 +168,7 @@ impl State { self.offset.min(hidden_content) } + /// Returns whether the scrollbar is currently grabbed or not. pub fn is_scrollbar_grabbed(&self) -> bool { self.scrollbar_grabbed_at.is_some() } diff --git a/core/src/widget/slider.rs b/core/src/widget/slider.rs index b65e3991..d6fe8ade 100644 --- a/core/src/widget/slider.rs +++ b/core/src/widget/slider.rs @@ -31,19 +31,12 @@ use std::rc::Rc; /// ``` /// /// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true) +#[allow(missing_docs)] 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, } @@ -53,6 +46,7 @@ impl<'a, Message> std::fmt::Debug for Slider<'a, Message> { .field("state", &self.state) .field("range", &self.range) .field("value", &self.value) + .field("width", &self.width) .finish() } } @@ -102,6 +96,9 @@ impl<'a, Message> Slider<'a, Message> { /// [`Slider`]: struct.Slider.html #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub struct State { + /// Whether the [`Slider`] is currently being dragged or not. + /// + /// [`Slider`]: struct.Slider.html pub is_dragging: bool, } diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 0996e7ff..1746160e 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -12,6 +12,7 @@ use crate::{Color, Font, Length}; /// .size(40); /// ``` #[derive(Debug, Clone)] +#[allow(missing_docs)] pub struct Text { pub content: String, pub size: Option, @@ -48,14 +49,19 @@ impl Text { self } - /// Sets the `Color` of the [`Text`]. + /// Sets the [`Color`] of the [`Text`]. /// /// [`Text`]: struct.Text.html + /// [`Color`]: ../../struct.Color.html pub fn color>(mut self, color: C) -> Self { self.color = Some(color.into()); self } + /// Sets the [`Font`] of the [`Text`]. + /// + /// [`Text`]: struct.Text.html + /// [`Font`]: ../../struct.Font.html pub fn font(mut self, font: Font) -> Self { self.font = font; self diff --git a/core/src/widget/text_input.rs b/core/src/widget/text_input.rs index 16c67954..df933380 100644 --- a/core/src/widget/text_input.rs +++ b/core/src/widget/text_input.rs @@ -1,5 +1,13 @@ +//! Display fields that can be filled with text. +//! +//! A [`TextInput`] has some local [`State`]. +//! +//! [`TextInput`]: struct.TextInput.html +//! [`State`]: struct.State.html use crate::Length; +/// A widget that can be filled with text by using a keyboard. +#[allow(missing_docs)] pub struct TextInput<'a, Message> { pub state: &'a mut State, pub placeholder: String, @@ -13,6 +21,9 @@ pub struct TextInput<'a, Message> { } impl<'a, Message> TextInput<'a, Message> { + /// Creates a new [`TextInput`]. + /// + /// [`TextInput`]: struct.TextInput.html pub fn new( state: &'a mut State, placeholder: &str, @@ -59,11 +70,18 @@ impl<'a, Message> TextInput<'a, Message> { self } + /// Sets the text size of the [`TextInput`]. + /// + /// [`TextInput`]: struct.TextInput.html pub fn size(mut self, size: u16) -> Self { self.size = Some(size); self } + /// Sets the message that should be produced when the [`TextInput`] is + /// focused and the enter key is pressed. + /// + /// [`TextInput`]: struct.TextInput.html pub fn on_submit(mut self, message: Message) -> Self { self.on_submit = Some(message); self @@ -80,17 +98,29 @@ where } } +/// The state of a [`TextInput`]. +/// +/// [`TextInput`]: struct.TextInput.html #[derive(Debug, Default, Clone)] pub struct State { + /// Whether the [`TextInput`] is focused or not. + /// + /// [`TextInput`]: struct.TextInput.html pub is_focused: bool, cursor_position: usize, } impl State { + /// Creates a new [`State`], representing an unfocused [`TextInput`]. + /// + /// [`State`]: struct.State.html pub fn new() -> Self { Self::default() } + /// Creates a new [`State`], representing a focused [`TextInput`]. + /// + /// [`State`]: struct.State.html pub fn focused() -> Self { use std::usize; @@ -100,6 +130,9 @@ impl State { } } + /// Moves the cursor of a [`TextInput`] to the right. + /// + /// [`TextInput`]: struct.TextInput.html pub fn move_cursor_right(&mut self, value: &Value) { let current = self.cursor_position(value); @@ -108,6 +141,9 @@ impl State { } } + /// Moves the cursor of a [`TextInput`] to the left. + /// + /// [`TextInput`]: struct.TextInput.html pub fn move_cursor_left(&mut self, value: &Value) { let current = self.cursor_position(value); @@ -116,41 +152,69 @@ impl State { } } + /// Moves the cursor of a [`TextInput`] to the end. + /// + /// [`TextInput`]: struct.TextInput.html pub fn move_cursor_to_end(&mut self, value: &Value) { self.cursor_position = value.len(); } + /// Returns the cursor position of a [`TextInput`]. + /// + /// [`TextInput`]: struct.TextInput.html pub fn cursor_position(&self, value: &Value) -> usize { self.cursor_position.min(value.len()) } } +/// The value of a [`TextInput`]. +/// +/// [`TextInput`]: struct.TextInput.html // TODO: Use `unicode-segmentation` +#[derive(Debug)] pub struct Value(Vec); impl Value { + /// Creates a new [`Value`] from a string slice. + /// + /// [`Value`]: struct.Value.html pub fn new(string: &str) -> Self { Self(string.chars().collect()) } + /// Returns the total amount of `char` in the [`Value`]. + /// + /// [`Value`]: struct.Value.html pub fn len(&self) -> usize { self.0.len() } + /// Returns a new [`Value`] containing the `char` until the given `index`. + /// + /// [`Value`]: struct.Value.html pub fn until(&self, index: usize) -> Self { Self(self.0[..index.min(self.len())].to_vec()) } + /// Converts the [`Value`] into a `String`. + /// + /// [`Value`]: struct.Value.html pub fn to_string(&self) -> String { use std::iter::FromIterator; String::from_iter(self.0.iter()) } + /// Inserts a new `char` at the given `index`. + /// + /// [`Value`]: struct.Value.html pub fn insert(&mut self, index: usize, c: char) { self.0.insert(index, c); } + /// Removes the `char` at the given `index`. + /// + /// [`Value`]: struct.Value.html pub fn remove(&mut self, index: usize) { - self.0.remove(index); + let _ = self.0.remove(index); } } -- cgit From 65eb218d3d7ba52b2869a586a1480eeb3c8f84e4 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 21 Nov 2019 13:47:20 +0100 Subject: Move widgets from `core` to `native` and `web` Also made fields private and improved `Renderer` traits. --- core/src/align.rs | 26 +++++ core/src/lib.rs | 4 +- core/src/widget.rs | 39 -------- core/src/widget/button.rs | 114 ---------------------- core/src/widget/checkbox.rs | 78 --------------- core/src/widget/column.rs | 126 ------------------------ core/src/widget/container.rs | 88 ----------------- core/src/widget/image.rs | 65 ------------- core/src/widget/radio.rs | 88 ----------------- core/src/widget/row.rs | 121 ----------------------- core/src/widget/scrollable.rs | 175 --------------------------------- core/src/widget/slider.rs | 120 ----------------------- core/src/widget/text.rs | 132 ------------------------- core/src/widget/text_input.rs | 220 ------------------------------------------ 14 files changed, 27 insertions(+), 1369 deletions(-) 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/container.rs delete mode 100644 core/src/widget/image.rs delete mode 100644 core/src/widget/radio.rs delete mode 100644 core/src/widget/row.rs delete mode 100644 core/src/widget/scrollable.rs delete mode 100644 core/src/widget/slider.rs delete mode 100644 core/src/widget/text.rs delete mode 100644 core/src/widget/text_input.rs (limited to 'core') diff --git a/core/src/align.rs b/core/src/align.rs index d6915086..8b571db4 100644 --- a/core/src/align.rs +++ b/core/src/align.rs @@ -16,3 +16,29 @@ pub enum Align { /// Align at the end of the cross axis. End, } + +/// 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/core/src/lib.rs b/core/src/lib.rs index 692e40d4..5a608fe8 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -12,7 +12,6 @@ #![deny(unused_results)] #![deny(unsafe_code)] #![deny(rust_2018_idioms)] -pub mod widget; mod align; mod background; @@ -23,7 +22,7 @@ mod point; mod rectangle; mod vector; -pub use align::Align; +pub use align::{Align, HorizontalAlignment, VerticalAlignment}; pub use background::Background; pub use color::Color; pub use font::Font; @@ -31,7 +30,6 @@ pub use length::Length; pub use point::Point; pub use rectangle::Rectangle; pub use vector::Vector; -pub use widget::*; #[cfg(feature = "command")] mod command; diff --git a/core/src/widget.rs b/core/src/widget.rs deleted file mode 100644 index 9e629e4f..00000000 --- a/core/src/widget.rs +++ /dev/null @@ -1,39 +0,0 @@ -//! 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 checkbox; -mod column; -mod container; -mod image; -mod radio; -mod row; - -pub mod button; -pub mod scrollable; -pub mod slider; -pub mod text; -pub mod text_input; - -#[doc(no_inline)] -pub use button::Button; -#[doc(no_inline)] -pub use scrollable::Scrollable; -#[doc(no_inline)] -pub use slider::Slider; -#[doc(no_inline)] -pub use text::Text; -#[doc(no_inline)] -pub use text_input::TextInput; - -pub use checkbox::Checkbox; -pub use column::Column; -pub use container::Container; -pub use image::Image; -pub use radio::Radio; -pub use row::Row; diff --git a/core/src/widget/button.rs b/core/src/widget/button.rs deleted file mode 100644 index 0ac88671..00000000 --- a/core/src/widget/button.rs +++ /dev/null @@ -1,114 +0,0 @@ -//! 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::{Background, Length}; - -/// A generic widget that produces a message when clicked. -#[allow(missing_docs)] -#[derive(Debug)] -pub struct Button<'a, Message, Element> { - pub state: &'a mut State, - pub content: Element, - pub on_press: Option, - pub width: Length, - pub min_width: u32, - pub padding: u16, - pub background: Option, - pub border_radius: u16, -} - -impl<'a, Message, Element> Button<'a, Message, Element> { - /// Creates a new [`Button`] with some local [`State`] and the given - /// content. - /// - /// [`Button`]: struct.Button.html - /// [`State`]: struct.State.html - pub fn new(state: &'a mut State, content: E) -> Self - where - E: Into, - { - Button { - state, - content: content.into(), - on_press: None, - width: Length::Shrink, - min_width: 0, - padding: 0, - background: None, - border_radius: 0, - } - } - - /// Sets the width of the [`Button`]. - /// - /// [`Button`]: struct.Button.html - pub fn width(mut self, width: Length) -> Self { - self.width = width; - self - } - - /// Sets the minimum width of the [`Button`]. - /// - /// [`Button`]: struct.Button.html - pub fn min_width(mut self, min_width: u32) -> Self { - self.min_width = min_width; - self - } - - /// Sets the padding of the [`Button`]. - /// - /// [`Button`]: struct.Button.html - pub fn padding(mut self, padding: u16) -> Self { - self.padding = padding; - self - } - - /// Sets the [`Background`] of the [`Button`]. - /// - /// [`Button`]: struct.Button.html - /// [`Background`]: ../../struct.Background.html - pub fn background(mut self, background: Background) -> Self { - self.background = Some(background); - self - } - - /// Sets the border radius of the [`Button`]. - /// - /// [`Button`]: struct.Button.html - pub fn border_radius(mut self, border_radius: u16) -> Self { - self.border_radius = border_radius; - 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 { - /// Whether the [`Button`] is currently being pressed. - /// - /// [`Button`]: struct.Button.html - pub is_pressed: bool, -} - -impl State { - /// Creates a new [`State`]. - /// - /// [`State`]: struct.State.html - pub fn new() -> State { - State::default() - } -} diff --git a/core/src/widget/checkbox.rs b/core/src/widget/checkbox.rs deleted file mode 100644 index 1f0a0c04..00000000 --- a/core/src/widget/checkbox.rs +++ /dev/null @@ -1,78 +0,0 @@ -//! 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 deleted file mode 100644 index 34dbecf7..00000000 --- a/core/src/widget/column.rs +++ /dev/null @@ -1,126 +0,0 @@ -use crate::{Align, Length}; - -use std::u32; - -/// A container that distributes its contents vertically. -/// -/// A [`Column`] will try to fill the horizontal space of its container. -/// -/// [`Column`]: struct.Column.html -#[allow(missing_docs)] -pub struct Column { - pub spacing: u16, - pub padding: u16, - pub width: Length, - pub height: Length, - pub max_width: u32, - pub max_height: u32, - pub align_items: Align, - 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: u32::MAX, - max_height: u32::MAX, - align_items: Align::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: u32) -> 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: u32) -> Self { - self.max_height = max_height; - 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 - } - - /// 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/container.rs b/core/src/widget/container.rs deleted file mode 100644 index 3fe340d1..00000000 --- a/core/src/widget/container.rs +++ /dev/null @@ -1,88 +0,0 @@ -use crate::{Align, Length}; - -use std::u32; - -/// An element decorating some content. -/// -/// It is normally used for alignment purposes. -#[derive(Debug)] -#[allow(missing_docs)] -pub struct Container { - pub width: Length, - pub height: Length, - pub max_width: u32, - pub max_height: u32, - pub horizontal_alignment: Align, - pub vertical_alignment: Align, - pub content: Element, -} - -impl Container { - /// Creates an empty [`Container`]. - /// - /// [`Container`]: struct.Container.html - pub fn new(content: T) -> Self - where - T: Into, - { - Container { - width: Length::Shrink, - height: Length::Shrink, - max_width: u32::MAX, - max_height: u32::MAX, - horizontal_alignment: Align::Start, - vertical_alignment: Align::Start, - content: content.into(), - } - } - - /// Sets the width of the [`Container`]. - /// - /// [`Container`]: struct.Container.html - pub fn width(mut self, width: Length) -> Self { - self.width = width; - self - } - - /// Sets the height of the [`Container`]. - /// - /// [`Container`]: struct.Container.html - pub fn height(mut self, height: Length) -> Self { - self.height = height; - self - } - - /// Sets the maximum width of the [`Container`]. - /// - /// [`Container`]: struct.Container.html - pub fn max_width(mut self, max_width: u32) -> Self { - self.max_width = max_width; - self - } - - /// Sets the maximum height of the [`Container`] in pixels. - /// - /// [`Container`]: struct.Container.html - pub fn max_height(mut self, max_height: u32) -> Self { - self.max_height = max_height; - self - } - - /// Centers the contents in the horizontal axis of the [`Container`]. - /// - /// [`Container`]: struct.Container.html - pub fn center_x(mut self) -> Self { - self.horizontal_alignment = Align::Center; - - self - } - - /// Centers the contents in the vertical axis of the [`Container`]. - /// - /// [`Container`]: struct.Container.html - pub fn center_y(mut self) -> Self { - self.vertical_alignment = Align::Center; - - self - } -} diff --git a/core/src/widget/image.rs b/core/src/widget/image.rs deleted file mode 100644 index 996ab5e1..00000000 --- a/core/src/widget/image.rs +++ /dev/null @@ -1,65 +0,0 @@ -//! Display images in your user interface. - -use crate::{Length, Rectangle}; - -/// A frame that displays an image while keeping aspect ratio. -/// -/// # Example -/// -/// ``` -/// use iced_core::Image; -/// -/// let image = Image::new("resources/ferris.png"); -/// ``` -#[derive(Debug)] -pub struct Image { - /// The image path - pub path: String, - - /// 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, -} - -impl Image { - /// Creates a new [`Image`] with the given path. - /// - /// [`Image`]: struct.Image.html - pub fn new>(path: T) -> Self { - Image { - path: path.into(), - clip: None, - width: Length::Shrink, - height: Length::Shrink, - } - } - - /// 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 - } -} diff --git a/core/src/widget/radio.rs b/core/src/widget/radio.rs deleted file mode 100644 index 9765e928..00000000 --- a/core/src/widget/radio.rs +++ /dev/null @@ -1,88 +0,0 @@ -//! 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 deleted file mode 100644 index d2b23bdb..00000000 --- a/core/src/widget/row.rs +++ /dev/null @@ -1,121 +0,0 @@ -use crate::{Align, Length}; - -use std::u32; - -/// A container that distributes its contents horizontally. -/// -/// A [`Row`] will try to fill the horizontal space of its container. -/// -/// [`Row`]: struct.Row.html -#[allow(missing_docs)] -pub struct Row { - pub spacing: u16, - pub padding: u16, - pub width: Length, - pub height: Length, - pub max_width: u32, - pub max_height: u32, - pub align_items: Align, - 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: u32::MAX, - max_height: u32::MAX, - align_items: Align::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: u32) -> 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: u32) -> Self { - self.max_height = max_height; - 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 - } - - /// 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/scrollable.rs b/core/src/widget/scrollable.rs deleted file mode 100644 index da03cd93..00000000 --- a/core/src/widget/scrollable.rs +++ /dev/null @@ -1,175 +0,0 @@ -//! Navigate an endless amount of content with a scrollbar. -use crate::{Align, Column, Length, Point, Rectangle}; - -use std::u32; - -/// A widget that can vertically display an infinite amount of content with a -/// scrollbar. -#[allow(missing_docs)] -#[derive(Debug)] -pub struct Scrollable<'a, Element> { - pub state: &'a mut State, - pub height: Length, - pub max_height: u32, - pub content: Column, -} - -impl<'a, Element> Scrollable<'a, Element> { - /// Creates a new [`Scrollable`] with the given [`State`]. - /// - /// [`Scrollable`]: struct.Scrollable.html - /// [`State`]: struct.State.html - pub fn new(state: &'a mut State) -> Self { - Scrollable { - state, - height: Length::Shrink, - max_height: u32::MAX, - content: Column::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.content = self.content.spacing(units); - self - } - - /// Sets the padding of the [`Scrollable`]. - /// - /// [`Scrollable`]: struct.Scrollable.html - pub fn padding(mut self, units: u16) -> Self { - self.content = self.content.padding(units); - self - } - - /// Sets the width of the [`Scrollable`]. - /// - /// [`Scrollable`]: struct.Scrollable.html - pub fn width(mut self, width: Length) -> Self { - self.content = self.content.width(width); - self - } - - /// Sets the height of the [`Scrollable`]. - /// - /// [`Scrollable`]: struct.Scrollable.html - pub fn height(mut self, height: Length) -> Self { - self.height = height; - self - } - - /// Sets the maximum width of the [`Scrollable`]. - /// - /// [`Scrollable`]: struct.Scrollable.html - pub fn max_width(mut self, max_width: u32) -> Self { - self.content = self.content.max_width(max_width); - self - } - - /// Sets the maximum height of the [`Scrollable`] in pixels. - /// - /// [`Scrollable`]: struct.Scrollable.html - pub fn max_height(mut self, max_height: u32) -> Self { - self.max_height = max_height; - self - } - - /// Sets the horizontal alignment of the contents of the [`Scrollable`] . - /// - /// [`Scrollable`]: struct.Scrollable.html - pub fn align_items(mut self, align_items: Align) -> Self { - self.content = self.content.align_items(align_items); - self - } - - /// Adds an element to the [`Scrollable`]. - /// - /// [`Scrollable`]: struct.Scrollable.html - pub fn push(mut self, child: E) -> Scrollable<'a, Element> - where - E: Into, - { - self.content = self.content.push(child); - self - } -} - -/// The local state of a [`Scrollable`]. -/// -/// [`Scrollable`]: struct.Scrollable.html -#[derive(Debug, Clone, Copy, Default)] -pub struct State { - /// The position where the scrollbar was grabbed at, if it's currently - /// grabbed. - pub scrollbar_grabbed_at: Option, - offset: u32, -} - -impl State { - /// Creates a new [`State`] with the scrollbar located at the top. - /// - /// [`State`]: struct.State.html - pub fn new() -> Self { - State::default() - } - - /// Apply a scrolling offset to the current [`State`], given the bounds of - /// the [`Scrollable`] and its contents. - /// - /// [`Scrollable`]: struct.Scrollable.html - /// [`State`]: struct.State.html - pub fn scroll( - &mut self, - delta_y: f32, - bounds: Rectangle, - content_bounds: Rectangle, - ) { - if bounds.height >= content_bounds.height { - return; - } - - self.offset = (self.offset as i32 - delta_y.round() as i32) - .max(0) - .min((content_bounds.height - bounds.height) as i32) - as u32; - } - - /// Moves the scroll position to a relative amount, given the bounds of - /// the [`Scrollable`] and its contents. - /// - /// `0` represents scrollbar at the top, while `1` represents scrollbar at - /// the bottom. - /// - /// [`Scrollable`]: struct.Scrollable.html - /// [`State`]: struct.State.html - pub fn scroll_to( - &mut self, - percentage: f32, - bounds: Rectangle, - content_bounds: Rectangle, - ) { - self.offset = ((content_bounds.height - bounds.height) * percentage) - .max(0.0) as u32; - } - - /// Returns the current scrolling offset of the [`State`], given the bounds - /// of the [`Scrollable`] and its contents. - /// - /// [`Scrollable`]: struct.Scrollable.html - /// [`State`]: struct.State.html - pub fn offset(&self, bounds: Rectangle, content_bounds: Rectangle) -> u32 { - let hidden_content = - (content_bounds.height - bounds.height).max(0.0).round() as u32; - - self.offset.min(hidden_content) - } - - /// Returns whether the scrollbar is currently grabbed or not. - pub fn is_scrollbar_grabbed(&self) -> bool { - self.scrollbar_grabbed_at.is_some() - } -} diff --git a/core/src/widget/slider.rs b/core/src/widget/slider.rs deleted file mode 100644 index d6fe8ade..00000000 --- a/core/src/widget/slider.rs +++ /dev/null @@ -1,120 +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 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) -#[allow(missing_docs)] -pub struct Slider<'a, Message> { - pub state: &'a mut State, - pub range: RangeInclusive, - pub value: f32, - 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) - .field("width", &self.width) - .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 { - /// Whether the [`Slider`] is currently being dragged or not. - /// - /// [`Slider`]: struct.Slider.html - 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 deleted file mode 100644 index 1746160e..00000000 --- a/core/src/widget/text.rs +++ /dev/null @@ -1,132 +0,0 @@ -//! Write some text for your users to read. -use crate::{Color, Font, Length}; - -/// A paragraph of text. -/// -/// # Example -/// -/// ``` -/// use iced_core::Text; -/// -/// Text::new("I <3 iced!") -/// .size(40); -/// ``` -#[derive(Debug, Clone)] -#[allow(missing_docs)] -pub struct Text { - pub content: String, - pub size: Option, - pub color: Option, - pub font: Font, - 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, - font: Font::Default, - 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 - /// [`Color`]: ../../struct.Color.html - pub fn color>(mut self, color: C) -> Self { - self.color = Some(color.into()); - self - } - - /// Sets the [`Font`] of the [`Text`]. - /// - /// [`Text`]: struct.Text.html - /// [`Font`]: ../../struct.Font.html - pub fn font(mut self, font: Font) -> Self { - self.font = font; - 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/core/src/widget/text_input.rs b/core/src/widget/text_input.rs deleted file mode 100644 index df933380..00000000 --- a/core/src/widget/text_input.rs +++ /dev/null @@ -1,220 +0,0 @@ -//! Display fields that can be filled with text. -//! -//! A [`TextInput`] has some local [`State`]. -//! -//! [`TextInput`]: struct.TextInput.html -//! [`State`]: struct.State.html -use crate::Length; - -/// A widget that can be filled with text by using a keyboard. -#[allow(missing_docs)] -pub struct TextInput<'a, Message> { - pub state: &'a mut State, - pub placeholder: String, - pub value: Value, - pub width: Length, - pub max_width: Length, - pub padding: u16, - pub size: Option, - pub on_change: Box Message>, - pub on_submit: Option, -} - -impl<'a, Message> TextInput<'a, Message> { - /// Creates a new [`TextInput`]. - /// - /// [`TextInput`]: struct.TextInput.html - pub fn new( - state: &'a mut State, - placeholder: &str, - value: &str, - on_change: F, - ) -> Self - where - F: 'static + Fn(String) -> Message, - { - Self { - state, - placeholder: String::from(placeholder), - value: Value::new(value), - width: Length::Fill, - max_width: Length::Shrink, - padding: 0, - size: None, - on_change: Box::new(on_change), - on_submit: None, - } - } - - /// Sets the width of the [`TextInput`]. - /// - /// [`TextInput`]: struct.TextInput.html - pub fn width(mut self, width: Length) -> Self { - self.width = width; - self - } - - /// Sets the maximum width of the [`TextInput`]. - /// - /// [`TextInput`]: struct.TextInput.html - pub fn max_width(mut self, max_width: Length) -> Self { - self.max_width = max_width; - self - } - - /// Sets the padding of the [`TextInput`]. - /// - /// [`TextInput`]: struct.TextInput.html - pub fn padding(mut self, units: u16) -> Self { - self.padding = units; - self - } - - /// Sets the text size of the [`TextInput`]. - /// - /// [`TextInput`]: struct.TextInput.html - pub fn size(mut self, size: u16) -> Self { - self.size = Some(size); - self - } - - /// Sets the message that should be produced when the [`TextInput`] is - /// focused and the enter key is pressed. - /// - /// [`TextInput`]: struct.TextInput.html - pub fn on_submit(mut self, message: Message) -> Self { - self.on_submit = Some(message); - self - } -} - -impl<'a, Message> std::fmt::Debug for TextInput<'a, Message> -where - Message: std::fmt::Debug, -{ - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - // TODO: Complete once stabilized - f.debug_struct("TextInput").finish() - } -} - -/// The state of a [`TextInput`]. -/// -/// [`TextInput`]: struct.TextInput.html -#[derive(Debug, Default, Clone)] -pub struct State { - /// Whether the [`TextInput`] is focused or not. - /// - /// [`TextInput`]: struct.TextInput.html - pub is_focused: bool, - cursor_position: usize, -} - -impl State { - /// Creates a new [`State`], representing an unfocused [`TextInput`]. - /// - /// [`State`]: struct.State.html - pub fn new() -> Self { - Self::default() - } - - /// Creates a new [`State`], representing a focused [`TextInput`]. - /// - /// [`State`]: struct.State.html - pub fn focused() -> Self { - use std::usize; - - Self { - is_focused: true, - cursor_position: usize::MAX, - } - } - - /// Moves the cursor of a [`TextInput`] to the right. - /// - /// [`TextInput`]: struct.TextInput.html - pub fn move_cursor_right(&mut self, value: &Value) { - let current = self.cursor_position(value); - - if current < value.len() { - self.cursor_position = current + 1; - } - } - - /// Moves the cursor of a [`TextInput`] to the left. - /// - /// [`TextInput`]: struct.TextInput.html - pub fn move_cursor_left(&mut self, value: &Value) { - let current = self.cursor_position(value); - - if current > 0 { - self.cursor_position = current - 1; - } - } - - /// Moves the cursor of a [`TextInput`] to the end. - /// - /// [`TextInput`]: struct.TextInput.html - pub fn move_cursor_to_end(&mut self, value: &Value) { - self.cursor_position = value.len(); - } - - /// Returns the cursor position of a [`TextInput`]. - /// - /// [`TextInput`]: struct.TextInput.html - pub fn cursor_position(&self, value: &Value) -> usize { - self.cursor_position.min(value.len()) - } -} - -/// The value of a [`TextInput`]. -/// -/// [`TextInput`]: struct.TextInput.html -// TODO: Use `unicode-segmentation` -#[derive(Debug)] -pub struct Value(Vec); - -impl Value { - /// Creates a new [`Value`] from a string slice. - /// - /// [`Value`]: struct.Value.html - pub fn new(string: &str) -> Self { - Self(string.chars().collect()) - } - - /// Returns the total amount of `char` in the [`Value`]. - /// - /// [`Value`]: struct.Value.html - pub fn len(&self) -> usize { - self.0.len() - } - - /// Returns a new [`Value`] containing the `char` until the given `index`. - /// - /// [`Value`]: struct.Value.html - pub fn until(&self, index: usize) -> Self { - Self(self.0[..index.min(self.len())].to_vec()) - } - - /// Converts the [`Value`] into a `String`. - /// - /// [`Value`]: struct.Value.html - pub fn to_string(&self) -> String { - use std::iter::FromIterator; - String::from_iter(self.0.iter()) - } - - /// Inserts a new `char` at the given `index`. - /// - /// [`Value`]: struct.Value.html - pub fn insert(&mut self, index: usize, c: char) { - self.0.insert(index, c); - } - - /// Removes the `char` at the given `index`. - /// - /// [`Value`]: struct.Value.html - pub fn remove(&mut self, index: usize) { - let _ = self.0.remove(index); - } -} -- cgit From a7dba612f03e58d7bd9527499d893987986b347c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 22 Nov 2019 19:36:57 +0100 Subject: Write docs for `iced` and `iced_native` --- core/src/align.rs | 8 +------- core/src/command.rs | 2 +- core/src/lib.rs | 2 ++ 3 files changed, 4 insertions(+), 8 deletions(-) (limited to 'core') diff --git a/core/src/align.rs b/core/src/align.rs index 8b571db4..05bd5e63 100644 --- a/core/src/align.rs +++ b/core/src/align.rs @@ -1,10 +1,4 @@ -/// 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 +/// Alignment on an unspecified axis of a container. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Align { /// Align at the start of the cross axis. diff --git a/core/src/command.rs b/core/src/command.rs index 698105aa..14b48b5b 100644 --- a/core/src/command.rs +++ b/core/src/command.rs @@ -2,7 +2,7 @@ use futures::future::{BoxFuture, Future, FutureExt}; /// A collection of async operations. /// -/// You should be able to turn a future easily into a [`Command`], eiter by +/// You should be able to turn a future easily into a [`Command`], either by /// using the `From` trait or [`Command::perform`]. /// /// [`Command`]: struct.Command.html diff --git a/core/src/lib.rs b/core/src/lib.rs index 5a608fe8..6c36e683 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -4,6 +4,8 @@ //! different runtime implementations. For instance, both [`iced_native`] and //! [`iced_web`] are built on top of `iced_core`. //! +//! ![`iced_core` crate graph](https://github.com/hecrj/iced/blob/cae26cb7bc627f4a5b3bcf1cd023a0c552e8c65e/docs/graphs/core.png?raw=true) +//! //! [Iced]: https://github.com/hecrj/iced //! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native //! [`iced_web`]: https://github.com/hecrj/iced/tree/master/web -- cgit From d136b7ce648cde0dcdcc5388d8cb82b3e7e0fc58 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 22 Nov 2019 21:16:40 +0100 Subject: Uncomment missing debug implementations rule --- core/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/src/lib.rs b/core/src/lib.rs index 6c36e683..65304e8b 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,11 +1,11 @@ //! The core library of [Iced]. //! +//! ![`iced_core` crate graph](https://github.com/hecrj/iced/blob/cae26cb7bc627f4a5b3bcf1cd023a0c552e8c65e/docs/graphs/core.png?raw=true) +//! //! This library holds basic types that can be reused and re-exported in //! different runtime implementations. For instance, both [`iced_native`] and //! [`iced_web`] are built on top of `iced_core`. //! -//! ![`iced_core` crate graph](https://github.com/hecrj/iced/blob/cae26cb7bc627f4a5b3bcf1cd023a0c552e8c65e/docs/graphs/core.png?raw=true) -//! //! [Iced]: https://github.com/hecrj/iced //! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native //! [`iced_web`]: https://github.com/hecrj/iced/tree/master/web -- cgit From 3a678561f2da92e089390ee79bd4f9efc2c1a8c7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 23 Nov 2019 12:00:50 +0100 Subject: Fix documentation for `Axis` --- core/src/align.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'core') diff --git a/core/src/align.rs b/core/src/align.rs index 05bd5e63..8a59afa1 100644 --- a/core/src/align.rs +++ b/core/src/align.rs @@ -1,13 +1,13 @@ -/// Alignment on an unspecified axis of a container. +/// Alignment on an axis of a container. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Align { - /// Align at the start of the cross axis. + /// Align at the start of the axis. Start, - /// Align at the center of the cross axis. + /// Align at the center of the axis. Center, - /// Align at the end of the cross axis. + /// Align at the end of the axis. End, } -- cgit