Move styling to a brand new iced_style crate

This commit is contained in:
Héctor Ramón Jiménez 2020-01-01 14:16:10 +01:00
parent 9ab7c47dc7
commit e1062a02d1
10 changed files with 154 additions and 121 deletions

View File

@ -24,6 +24,7 @@ maintenance = { status = "actively-developed" }
members = [
"core",
"native",
"style",
"web",
"wgpu",
"winit",

View File

@ -22,7 +22,7 @@ pub mod widget {
//!
//! [`TextInput`]: text_input/struct.TextInput.html
//! [`text_input::State`]: text_input/struct.State.html
pub use iced_wgpu::button;
pub use iced_wgpu::widget::*;
pub mod scrollable {
//! Navigate an endless amount of content with a scrollbar.
@ -73,8 +73,9 @@ pub mod widget {
#[doc(no_inline)]
pub use {
button::Button, image::Image, scrollable::Scrollable, slider::Slider,
svg::Svg, text_input::TextInput,
button::Button, container::Container, image::Image,
scrollable::Scrollable, slider::Slider, svg::Svg,
text_input::TextInput,
};
/// A container that distributes its contents vertically.
@ -88,13 +89,6 @@ pub mod widget {
/// This is an alias of an `iced_native` row with a default `Renderer`.
pub type Row<'a, Message> =
iced_winit::Row<'a, Message, iced_wgpu::Renderer>;
/// An element decorating some content.
///
/// This is an alias of an `iced_native` container with a default
/// `Renderer`.
pub type Container<'a, Message> =
iced_winit::Container<'a, Message, iced_wgpu::Renderer>;
}
#[doc(no_inline)]

14
style/Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "iced_style"
version = "0.1.0-alpha"
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
edition = "2018"
description = "The default set of styles of Iced"
license = "MIT"
repository = "https://github.com/hecrj/iced"
documentation = "https://docs.rs/iced_style"
keywords = ["gui", "ui", "graphics", "interface", "widgets"]
categories = ["gui"]
[dependencies]
iced_core = { version = "0.1.0", path = "../core" }

79
style/src/button.rs Normal file
View File

@ -0,0 +1,79 @@
//! Allow your users to perform actions by pressing a button.
use iced_core::{Background, Color};
/// The appearance of a button.
#[derive(Debug)]
pub struct Style {
pub shadow_offset: f32,
pub background: Option<Background>,
pub border_radius: u16,
pub text_color: Color,
}
/// A set of rules that dictate the style of a button.
pub trait StyleSheet {
fn active(&self) -> Style;
fn hovered(&self) -> Style {
let active = self.active();
Style {
shadow_offset: active.shadow_offset + 1.0,
..active
}
}
fn pressed(&self) -> Style {
Style {
shadow_offset: 0.0,
..self.active()
}
}
fn disabled(&self) -> Style {
let active = self.active();
Style {
shadow_offset: 0.0,
background: active.background.map(|background| match background {
Background::Color(color) => Background::Color(Color {
a: color.a * 0.5,
..color
}),
}),
text_color: Color {
a: active.text_color.a * 0.5,
..active.text_color
},
..active
}
}
}
struct Default;
impl StyleSheet for Default {
fn active(&self) -> Style {
Style {
shadow_offset: 1.0,
background: Some(Background::Color([0.5, 0.5, 0.5].into())),
border_radius: 5,
text_color: Color::BLACK,
}
}
}
impl std::default::Default for Box<dyn StyleSheet> {
fn default() -> Self {
Box::new(Default)
}
}
impl<T> From<T> for Box<dyn StyleSheet>
where
T: 'static + StyleSheet,
{
fn from(style: T) -> Self {
Box::new(style)
}
}

41
style/src/container.rs Normal file
View File

@ -0,0 +1,41 @@
//! Decorate content and apply alignment.
use iced_core::{Background, Color};
/// The appearance of a container.
#[derive(Debug, Clone, Copy)]
pub struct Style {
pub text_color: Option<Color>,
pub background: Option<Background>,
pub border_radius: u16,
}
/// A set of rules that dictate the style of a container.
pub trait StyleSheet {
/// Produces the style of a container.
fn style(&self) -> Style {
Style {
text_color: None,
background: None,
border_radius: 0,
}
}
}
struct Default;
impl StyleSheet for Default {}
impl std::default::Default for Box<dyn StyleSheet> {
fn default() -> Self {
Box::new(Default)
}
}
impl<T> From<T> for Box<dyn StyleSheet>
where
T: 'static + StyleSheet,
{
fn from(style: T) -> Self {
Box::new(style)
}
}

2
style/src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod button;
pub mod container;

View File

@ -12,6 +12,7 @@ svg = ["resvg"]
[dependencies]
iced_native = { version = "0.1.0", path = "../native" }
iced_style = { version = "0.1.0-alpha", path = "../style" }
wgpu = "0.4"
glyph_brush = "0.6"
wgpu_glyph = { version = "0.7", git = "https://github.com/hecrj/wgpu_glyph", branch = "fix/font-load-panic" }

View File

@ -5,86 +5,11 @@
//! [`Button`]: type.Button.html
//! [`State`]: struct.State.html
use crate::Renderer;
use iced_native::{Background, Color};
pub use iced_native::button::State;
pub use iced_style::button::{Style, StyleSheet};
/// A widget that produces a message when clicked.
///
/// This is an alias of an `iced_native` button with an `iced_wgpu::Renderer`.
pub type Button<'a, Message> = iced_native::Button<'a, Message, Renderer>;
#[derive(Debug)]
pub struct Style {
pub shadow_offset: f32,
pub background: Option<Background>,
pub border_radius: u16,
pub text_color: Color,
}
pub trait StyleSheet {
fn active(&self) -> Style;
fn hovered(&self) -> Style {
let active = self.active();
Style {
shadow_offset: active.shadow_offset + 1.0,
..active
}
}
fn pressed(&self) -> Style {
Style {
shadow_offset: 0.0,
..self.active()
}
}
fn disabled(&self) -> Style {
let active = self.active();
Style {
shadow_offset: 0.0,
background: active.background.map(|background| match background {
Background::Color(color) => Background::Color(Color {
a: color.a * 0.5,
..color
}),
}),
text_color: Color {
a: active.text_color.a * 0.5,
..active.text_color
},
..active
}
}
}
struct Default;
impl StyleSheet for Default {
fn active(&self) -> Style {
Style {
shadow_offset: 1.0,
background: Some(Background::Color([0.5, 0.5, 0.5].into())),
border_radius: 5,
text_color: Color::BLACK,
}
}
}
impl std::default::Default for Box<dyn StyleSheet> {
fn default() -> Self {
Box::new(Default)
}
}
impl<T> From<T> for Box<dyn StyleSheet>
where
T: 'static + StyleSheet,
{
fn from(style: T) -> Self {
Box::new(style)
}
}

View File

@ -1,37 +1,10 @@
use iced_native::{Background, Color};
//! Decorate content and apply alignment.
use crate::Renderer;
#[derive(Debug, Clone, Copy)]
pub struct Style {
pub text_color: Option<Color>,
pub background: Option<Background>,
pub border_radius: u16,
}
pub use iced_style::container::{Style, StyleSheet};
pub trait StyleSheet {
fn style(&self) -> Style {
Style {
text_color: None,
background: None,
border_radius: 0,
}
}
}
struct Default;
impl StyleSheet for Default {}
impl std::default::Default for Box<dyn StyleSheet> {
fn default() -> Self {
Box::new(Default)
}
}
impl<T> From<T> for Box<dyn StyleSheet>
where
T: 'static + StyleSheet,
{
fn from(style: T) -> Self {
Box::new(style)
}
}
/// An element decorating some content.
///
/// This is an alias of an `iced_native` container with a default
/// `Renderer`.
pub type Container<'a, Message> = iced_native::Container<'a, Message, Renderer>;

View File

@ -6,6 +6,9 @@ edition = "2018"
description = "A winit runtime for Iced"
license = "MIT"
repository = "https://github.com/hecrj/iced"
documentation = "https://docs.rs/iced_winit"
keywords = ["gui", "ui", "graphics", "interface", "widgets"]
categories = ["gui"]
[features]
debug = []