summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/CHANGELOG.md5
-rw-r--r--core/Cargo.toml5
-rw-r--r--core/src/background.rs6
-rw-r--r--core/src/color.rs34
-rw-r--r--core/src/command.rs72
-rw-r--r--core/src/length.rs16
-rw-r--r--core/src/lib.rs12
-rw-r--r--core/src/point.rs17
-rw-r--r--core/src/rectangle.rs50
-rw-r--r--core/src/size.rs51
-rw-r--r--core/src/vector.rs12
11 files changed, 195 insertions, 85 deletions
diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md
index 30050238..c0796e66 100644
--- a/core/CHANGELOG.md
+++ b/core/CHANGELOG.md
@@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
+### Added
+- `Color::from_rgb8` to easily build a `Color` from its hexadecimal representation. [#90]
+
+[#90]: https://github.com/hecrj/iced/pull/90
+
## [0.1.0] - 2019-11-25
### Added
diff --git a/core/Cargo.toml b/core/Cargo.toml
index c623ba78..22bc7ceb 100644
--- a/core/Cargo.toml
+++ b/core/Cargo.toml
@@ -7,9 +7,4 @@ description = "The essential concepts of Iced"
license = "MIT"
repository = "https://github.com/hecrj/iced"
-[features]
-# Exposes a future-based `Command` type
-command = ["futures"]
-
[dependencies]
-futures = { version = "0.3", optional = true }
diff --git a/core/src/background.rs b/core/src/background.rs
index 98047172..e1a37ddc 100644
--- a/core/src/background.rs
+++ b/core/src/background.rs
@@ -7,3 +7,9 @@ pub enum Background {
Color(Color),
// TODO: Add gradient and image variants
}
+
+impl From<Color> for Background {
+ fn from(color: Color) -> Self {
+ Background::Color(color)
+ }
+}
diff --git a/core/src/color.rs b/core/src/color.rs
index ec48c185..db509b88 100644
--- a/core/src/color.rs
+++ b/core/src/color.rs
@@ -25,6 +25,40 @@ impl Color {
a: 1.0,
};
+ /// A color with no opacity.
+ pub const TRANSPARENT: Color = Color {
+ r: 0.0,
+ g: 0.0,
+ b: 0.0,
+ a: 0.0,
+ };
+
+ /// Creates a [`Color`] from its RGB components.
+ ///
+ /// [`Color`]: struct.Color.html
+ pub const fn from_rgb(r: f32, g: f32, b: f32) -> Color {
+ Color { r, g, b, a: 1.0 }
+ }
+
+ /// Creates a [`Color`] from its RGB8 components.
+ ///
+ /// [`Color`]: struct.Color.html
+ pub fn from_rgb8(r: u8, g: u8, b: u8) -> Color {
+ Color::from_rgba8(r, g, b, 1.0)
+ }
+
+ /// Creates a [`Color`] from its RGB8 components and an alpha value.
+ ///
+ /// [`Color`]: struct.Color.html
+ pub fn from_rgba8(r: u8, g: u8, b: u8, a: f32) -> Color {
+ Color {
+ r: f32::from(r) / 255.0,
+ g: f32::from(g) / 255.0,
+ b: f32::from(b) / 255.0,
+ a,
+ }
+ }
+
/// Converts the [`Color`] into its linear values.
///
/// [`Color`]: struct.Color.html
diff --git a/core/src/command.rs b/core/src/command.rs
deleted file mode 100644
index 14b48b5b..00000000
--- a/core/src/command.rs
+++ /dev/null
@@ -1,72 +0,0 @@
-use futures::future::{BoxFuture, Future, FutureExt};
-
-/// A collection of async operations.
-///
-/// 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
-pub struct Command<T> {
- futures: Vec<BoxFuture<'static, T>>,
-}
-
-impl<T> Command<T> {
- /// 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<A>(
- future: impl Future<Output = T> + 'static + Send,
- f: impl Fn(T) -> A + 'static + Send,
- ) -> Command<A> {
- Command {
- futures: vec![future.map(f).boxed()],
- }
- }
-
- /// 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<Item = Command<T>>) -> 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<BoxFuture<'static, T>> {
- self.futures
- }
-}
-
-impl<T, A> From<A> for Command<T>
-where
- A: Future<Output = T> + 'static + Send,
-{
- fn from(future: A) -> Self {
- Self {
- futures: vec![future.boxed()],
- }
- }
-}
-
-impl<T> std::fmt::Debug for Command<T> {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- f.debug_struct("Command").finish()
- }
-}
diff --git a/core/src/length.rs b/core/src/length.rs
index 63ba6207..06d8cf0a 100644
--- a/core/src/length.rs
+++ b/core/src/length.rs
@@ -4,6 +4,15 @@ pub enum Length {
/// Fill all the remaining space
Fill,
+ /// Fill a portion of the remaining space relative to other elements.
+ ///
+ /// Let's say we have two elements: one with `FillPortion(2)` and one with
+ /// `FillPortion(3)`. The first will get 2 portions of the available space,
+ /// while the second one would get 3.
+ ///
+ /// `Length::Fill` is equivalent to `Length::FillPortion(1)`.
+ FillPortion(u16),
+
/// Fill the least amount of space
Shrink,
@@ -22,8 +31,15 @@ impl Length {
pub fn fill_factor(&self) -> u16 {
match self {
Length::Fill => 1,
+ Length::FillPortion(factor) => *factor,
Length::Shrink => 0,
Length::Units(_) => 0,
}
}
}
+
+impl From<u16> for Length {
+ fn from(units: u16) -> Self {
+ Length::Units(units)
+ }
+}
diff --git a/core/src/lib.rs b/core/src/lib.rs
index 65304e8b..ea5e8b43 100644
--- a/core/src/lib.rs
+++ b/core/src/lib.rs
@@ -12,8 +12,8 @@
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![deny(unused_results)]
-#![deny(unsafe_code)]
-#![deny(rust_2018_idioms)]
+#![forbid(unsafe_code)]
+#![forbid(rust_2018_idioms)]
mod align;
mod background;
@@ -22,6 +22,7 @@ mod font;
mod length;
mod point;
mod rectangle;
+mod size;
mod vector;
pub use align::{Align, HorizontalAlignment, VerticalAlignment};
@@ -31,10 +32,5 @@ pub use font::Font;
pub use length::Length;
pub use point::Point;
pub use rectangle::Rectangle;
+pub use size::Size;
pub use vector::Vector;
-
-#[cfg(feature = "command")]
-mod command;
-
-#[cfg(feature = "command")]
-pub use command::Command;
diff --git a/core/src/point.rs b/core/src/point.rs
index 52307bba..b9a8149c 100644
--- a/core/src/point.rs
+++ b/core/src/point.rs
@@ -11,6 +11,11 @@ pub struct Point {
}
impl Point {
+ /// The origin (i.e. a [`Point`] with both X=0 and Y=0).
+ ///
+ /// [`Point`]: struct.Point.html
+ pub const ORIGIN: Point = Point::new(0.0, 0.0);
+
/// Creates a new [`Point`] with the given coordinates.
///
/// [`Point`]: struct.Point.html
@@ -19,6 +24,18 @@ impl Point {
}
}
+impl From<[f32; 2]> for Point {
+ fn from([x, y]: [f32; 2]) -> Self {
+ Point { x, y }
+ }
+}
+
+impl From<[u16; 2]> for Point {
+ fn from([x, y]: [u16; 2]) -> Self {
+ Point::new(x.into(), y.into())
+ }
+}
+
impl std::ops::Add<Vector> for Point {
type Output = Self;
diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs
index ee1e3807..7ed3d2df 100644
--- a/core/src/rectangle.rs
+++ b/core/src/rectangle.rs
@@ -27,6 +27,34 @@ impl Rectangle<f32> {
&& self.y <= point.y
&& point.y <= self.y + self.height
}
+
+ /// Computes the intersection with the given [`Rectangle`].
+ ///
+ /// [`Rectangle`]: struct.Rectangle.html
+ pub fn intersection(
+ &self,
+ other: &Rectangle<f32>,
+ ) -> Option<Rectangle<f32>> {
+ let x = self.x.max(other.x);
+ let y = self.y.max(other.y);
+
+ let lower_right_x = (self.x + self.width).min(other.x + other.width);
+ let lower_right_y = (self.y + self.height).min(other.y + other.height);
+
+ let width = lower_right_x - x;
+ let height = lower_right_y - y;
+
+ if width > 0.0 && height > 0.0 {
+ Some(Rectangle {
+ x,
+ y,
+ width,
+ height,
+ })
+ } else {
+ None
+ }
+ }
}
impl std::ops::Mul<f32> for Rectangle<u32> {
@@ -41,3 +69,25 @@ impl std::ops::Mul<f32> for Rectangle<u32> {
}
}
}
+
+impl From<Rectangle<u32>> for Rectangle<f32> {
+ fn from(rectangle: Rectangle<u32>) -> Rectangle<f32> {
+ Rectangle {
+ x: rectangle.x as f32,
+ y: rectangle.y as f32,
+ width: rectangle.width as f32,
+ height: rectangle.height as f32,
+ }
+ }
+}
+
+impl From<Rectangle<f32>> for Rectangle<u32> {
+ fn from(rectangle: Rectangle<f32>) -> Rectangle<u32> {
+ Rectangle {
+ x: rectangle.x as u32,
+ y: rectangle.y as u32,
+ width: rectangle.width.ceil() as u32,
+ height: rectangle.height.ceil() as u32,
+ }
+ }
+}
diff --git a/core/src/size.rs b/core/src/size.rs
new file mode 100644
index 00000000..389b3247
--- /dev/null
+++ b/core/src/size.rs
@@ -0,0 +1,51 @@
+use std::f32;
+
+/// An amount of space in 2 dimensions.
+#[derive(Debug, Clone, Copy, PartialEq)]
+pub struct Size {
+ /// The width.
+ pub width: f32,
+ /// The height.
+ pub height: f32,
+}
+
+impl Size {
+ /// A [`Size`] with zero width and height.
+ ///
+ /// [`Size`]: struct.Size.html
+ pub const ZERO: Size = Size::new(0., 0.);
+
+ /// A [`Size`] with infinite width and height.
+ ///
+ /// [`Size`]: struct.Size.html
+ pub const INFINITY: Size = Size::new(f32::INFINITY, f32::INFINITY);
+
+ /// A [`Size`] of infinite width and height.
+ ///
+ /// [`Size`]: struct.Size.html
+ pub const fn new(width: f32, height: f32) -> Self {
+ Size { width, height }
+ }
+
+ /// Increments the [`Size`] to account for the given padding.
+ ///
+ /// [`Size`]: struct.Size.html
+ pub fn pad(&self, padding: f32) -> Self {
+ Size {
+ width: self.width + padding * 2.0,
+ height: self.height + padding * 2.0,
+ }
+ }
+}
+
+impl From<[f32; 2]> for Size {
+ fn from([width, height]: [f32; 2]) -> Self {
+ Size { width, height }
+ }
+}
+
+impl From<[u16; 2]> for Size {
+ fn from([width, height]: [u16; 2]) -> Self {
+ Size::new(width.into(), height.into())
+ }
+}
diff --git a/core/src/vector.rs b/core/src/vector.rs
index e0c5f073..4c1cbfab 100644
--- a/core/src/vector.rs
+++ b/core/src/vector.rs
@@ -31,3 +31,15 @@ where
Self::new(self.x + b.x, self.y + b.y)
}
}
+
+impl<T> Default for Vector<T>
+where
+ T: Default,
+{
+ fn default() -> Self {
+ Self {
+ x: T::default(),
+ y: T::default(),
+ }
+ }
+}