From c9510db551893775d3233340dd114d971e24323a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 7 Oct 2019 03:56:16 +0200 Subject: Render colored quads --- core/src/color.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'core/src') diff --git a/core/src/color.rs b/core/src/color.rs index 5cc3a084..2b64c78d 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -16,4 +16,23 @@ impl Color { b: 0.0, a: 1.0, }; + + pub fn into_linear(self) -> [f32; 4] { + // As described in: + // https://en.wikipedia.org/wiki/SRGB#The_reverse_transformation + fn linear_component(u: f32) -> f32 { + if u < 0.04045 { + u / 12.92 + } else { + ((u + 0.055) / 1.055).powf(2.4) + } + } + + [ + linear_component(self.r), + linear_component(self.g), + linear_component(self.b), + self.a, + ] + } } -- cgit From 10e10e5e06841574425d2633f1c2916733f7b4ff Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 8 Oct 2019 03:13:41 +0200 Subject: Make `iced_core::Button` customizable Now it supports: - Any kind of content - Custom border radius - Custom background --- core/src/background.rs | 7 ++++ core/src/color.rs | 8 ++++ core/src/lib.rs | 2 + core/src/widget/button.rs | 96 +++++++++++++++++------------------------------ core/src/widget/text.rs | 2 +- 5 files changed, 52 insertions(+), 63 deletions(-) create mode 100644 core/src/background.rs (limited to 'core/src') diff --git a/core/src/background.rs b/core/src/background.rs new file mode 100644 index 00000000..59b67a2c --- /dev/null +++ b/core/src/background.rs @@ -0,0 +1,7 @@ +use crate::Color; + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum Background { + Color(Color), + // TODO: Add gradient and image variants +} diff --git a/core/src/color.rs b/core/src/color.rs index 2b64c78d..79910dd8 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -17,6 +17,14 @@ impl Color { a: 1.0, }; + /// The white color. + pub const WHITE: Color = Color { + r: 1.0, + g: 1.0, + b: 1.0, + a: 1.0, + }; + pub fn into_linear(self) -> [f32; 4] { // As described in: // https://en.wikipedia.org/wiki/SRGB#The_reverse_transformation diff --git a/core/src/lib.rs b/core/src/lib.rs index 1f43b2b7..877a8f85 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,6 +1,7 @@ pub mod widget; mod align; +mod background; mod color; mod justify; mod length; @@ -9,6 +10,7 @@ mod rectangle; mod vector; pub use align::Align; +pub use background::Background; pub use color::Color; pub use justify::Justify; pub use length::Length; diff --git a/core/src/widget/button.rs b/core/src/widget/button.rs index b98bb443..a57f2dd8 100644 --- a/core/src/widget/button.rs +++ b/core/src/widget/button.rs @@ -5,68 +5,58 @@ //! [`Button`]: struct.Button.html //! [`State`]: struct.State.html -use crate::{Align, Length}; +use crate::{Align, Background, 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> { +pub struct Button<'a, Message, Element> { /// The current state of the button pub state: &'a mut State, - /// The label of the button - pub label: String, + pub content: Element, /// The message to produce when the button is pressed pub on_press: Option, - pub class: Class, - pub width: Length, + pub padding: u16, + + pub background: Option, + + pub border_radius: u16, + pub align_self: Option, } -impl<'a, Message> std::fmt::Debug for Button<'a, Message> +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("label", &self.label) .field("on_press", &self.on_press) .finish() } } -impl<'a, Message> Button<'a, Message> { +impl<'a, Message, Element> Button<'a, Message, Element> { /// 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 { + pub fn new(state: &'a mut State, content: E) -> Self + where + E: Into, + { Button { state, - label: String::from(label), + content: content.into(), on_press: None, - class: Class::Primary, width: Length::Shrink, + padding: 0, + background: None, + border_radius: 0, align_self: None, } } @@ -79,6 +69,21 @@ impl<'a, Message> Button<'a, Message> { self } + pub fn padding(mut self, padding: u16) -> Self { + self.padding = padding; + self + } + + pub fn background(mut self, background: Background) -> Self { + self.background = Some(background); + self + } + + pub fn border_radius(mut self, border_radius: u16) -> Self { + self.border_radius = border_radius; + self + } + /// Sets the alignment of the [`Button`] itself. /// /// This is useful if you want to override the default alignment given by @@ -90,16 +95,6 @@ impl<'a, Message> Button<'a, Message> { 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 @@ -133,26 +128,3 @@ impl State { 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/text.rs b/core/src/widget/text.rs index 427d9471..cd94dbb2 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -1,5 +1,5 @@ //! Write some text for your users to read. -use crate::{Color, Length}; +use crate::{Align, Color, Length}; /// A paragraph of text. /// -- cgit From f8a232c8af4c50557fbf0c2e0b2ba46fb63f6adc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 22 Oct 2019 23:20:24 +0200 Subject: Remove generic handle in `Image` For now, we will simply assume images will be loaded from a given path. --- core/src/widget/image.rs | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) (limited to 'core/src') diff --git a/core/src/widget/image.rs b/core/src/widget/image.rs index 110ba99a..6e410dce 100644 --- a/core/src/widget/image.rs +++ b/core/src/widget/image.rs @@ -9,12 +9,12 @@ use crate::{Align, Length, Rectangle}; /// ``` /// use iced_core::Image; /// -/// # let my_handle = String::from("some_handle"); -/// let image = Image::new(my_handle); +/// let image = Image::new("resources/ferris.png"); /// ``` -pub struct Image { - /// The image handle - pub handle: I, +#[derive(Debug)] +pub struct Image { + /// The image path + pub path: String, /// The part of the image to show pub clip: Option>, @@ -28,23 +28,13 @@ pub struct Image { 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. +impl Image { + /// Creates a new [`Image`] with the given path. /// /// [`Image`]: struct.Image.html - pub fn new(handle: I) -> Self { + pub fn new>(path: T) -> Self { Image { - handle, + path: path.into(), clip: None, width: Length::Shrink, height: Length::Shrink, -- cgit From 7fdd7f595269eb043bbd7f392c2750783fa0a4b7 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 23 Oct 2019 02:35:08 +0200 Subject: Remove unused `Align` in `core::Text` --- core/src/widget/text.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src') diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index cd94dbb2..427d9471 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -1,5 +1,5 @@ //! Write some text for your users to read. -use crate::{Align, Color, Length}; +use crate::{Color, Length}; /// A paragraph of text. /// -- cgit