Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom styling #146

Merged
merged 36 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
c7b170d
Draft `Style` and `StyleSheet` for `Button`
hecrj Dec 29, 2019
f74ab46
Add `background_color` to `Settings`
hecrj Dec 29, 2019
89a6b8a
Rename `Settings::background_color` to `background`
hecrj Dec 29, 2019
8caa66b
Add `Renderer::Defaults` and style inheritance
hecrj Dec 30, 2019
fb9cc02
Draft basic styling for `Container`
hecrj Dec 31, 2019
e98471d
Merge branch 'master' into feature/custom-styling
hecrj Dec 31, 2019
649d72e
Fix `Widget::draw` for `Space` widget
hecrj Dec 31, 2019
9ab7c47
Add `border_width` and `border_color` to `Quad`
hecrj Dec 31, 2019
e1062a0
Move styling to a brand new `iced_style` crate
hecrj Jan 1, 2020
d96ced8
Allow configuration of default font
hecrj Jan 1, 2020
5af4159
Draft basic styling for `TextInput`
hecrj Jan 1, 2020
8d6f86b
Remove `background` from `Settings`
hecrj Jan 5, 2020
2116fbb
Add border styling to `Container`
hecrj Jan 5, 2020
1a0effa
Add border and shadow styling to `Button`
hecrj Jan 5, 2020
07ef59a
Implement `Default` for `container::Style`
hecrj Jan 5, 2020
7c4dba2
Implement `Default` for `text_input::Style`
hecrj Jan 5, 2020
a848306
Fix styling in `button::Renderer` implementation
hecrj Jan 5, 2020
bbc8f83
Merge branch 'master' into feature/custom-styling
hecrj Jan 5, 2020
2bbd395
Draft `styling` example
hecrj Jan 6, 2020
fbc25f8
Implement `Default` for `Theme` in `styling` example
hecrj Jan 6, 2020
f7dfd65
Use default styles in `styling` example
hecrj Jan 6, 2020
d0dc7ce
Implement styling for `Scrollable`
hecrj Jan 6, 2020
b329003
Implement styling for `Slider`
hecrj Jan 6, 2020
fce89d0
Use constants for colors in `styling` example
hecrj Jan 7, 2020
48b3b78
Implement styling for `ProgressBar`
hecrj Jan 7, 2020
387fc0b
Implement styling for `Radio`
hecrj Jan 7, 2020
ed30b48
Implement styling for `Checkbox`
hecrj Jan 7, 2020
3d26eb7
Always show scroller if scrollbar is visible
hecrj Jan 7, 2020
3e3f426
Add `Scrollable` to `styling` example
hecrj Jan 7, 2020
f7a8b69
Remove `Mul` implementation for `Vector`
hecrj Jan 7, 2020
08faaaf
Color borders of `Checkbox` and `Radio` in `styling` example
hecrj Jan 7, 2020
cae4463
Allow `Checkbox` style to change based on its state
hecrj Jan 8, 2020
89b1ac6
Fix drawing empty `Quad` on empty `ProgressBar`
hecrj Jan 8, 2020
5e01896
Merge branch 'master' into feature/custom-styling
hecrj Jan 9, 2020
775500c
Remove leftover `debug_color` in `styling` example
hecrj Jan 9, 2020
7b27875
Write missing docs and reenable deny statements
hecrj Jan 9, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ maintenance = { status = "actively-developed" }
members = [
"core",
"native",
"style",
"web",
"wgpu",
"winit",
Expand Down
22 changes: 15 additions & 7 deletions core/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ 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
Expand All @@ -37,13 +52,6 @@ impl Color {
}
}

/// Creates a [`Color`] from its RGB components.
///
/// [`Color`]: struct.Color.html
pub fn from_rgb(r: f32, g: f32, b: f32) -> Color {
Color { r, g, b, a: 1.0 }
}

/// Converts the [`Color`] into its linear values.
///
/// [`Color`]: struct.Color.html
Expand Down
12 changes: 12 additions & 0 deletions core/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
}
5 changes: 4 additions & 1 deletion examples/custom_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod circle {
layout, Background, Color, Element, Hasher, Layout, Length,
MouseCursor, Point, Size, Widget,
};
use iced_wgpu::{Primitive, Renderer};
use iced_wgpu::{Defaults, Primitive, Renderer};

pub struct Circle {
radius: u16,
Expand Down Expand Up @@ -54,6 +54,7 @@ mod circle {
fn draw(
&self,
_renderer: &mut Renderer,
_defaults: &Defaults,
layout: Layout<'_>,
_cursor_position: Point,
) -> (Primitive, MouseCursor) {
Expand All @@ -62,6 +63,8 @@ mod circle {
bounds: layout.bounds(),
background: Background::Color(Color::BLACK),
border_radius: self.radius,
border_width: 0,
border_color: Color::TRANSPARENT,
},
MouseCursor::OutOfBounds,
)
Expand Down
3 changes: 2 additions & 1 deletion examples/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod rainbow {
};
use iced_wgpu::{
triangle::{Mesh2D, Vertex2D},
Primitive, Renderer,
Defaults, Primitive, Renderer,
};

pub struct Rainbow;
Expand Down Expand Up @@ -51,6 +51,7 @@ mod rainbow {
fn draw(
&self,
_renderer: &mut Renderer,
_defaults: &Defaults,
layout: Layout<'_>,
cursor_position: Point,
) -> (Primitive, MouseCursor) {
Expand Down
31 changes: 26 additions & 5 deletions examples/pokedex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use iced::{
button, image, Align, Application, Button, Color, Column, Command,
Container, Element, Image, Length, Row, Settings, Text,
button, image, Align, Application, Button, Column, Command, Container,
Element, Image, Length, Row, Settings, Text,
};

pub fn main() {
Expand Down Expand Up @@ -214,8 +214,29 @@ impl From<surf::Exception> for Error {
}

fn button<'a>(state: &'a mut button::State, text: &str) -> Button<'a, Message> {
Button::new(state, Text::new(text).color(Color::WHITE))
.background(Color::from_rgb(0.11, 0.42, 0.87))
.border_radius(10)
Button::new(state, Text::new(text))
.padding(10)
.style(style::Button::Primary)
}

mod style {
use iced::{button, Background, Color, Vector};

pub enum Button {
Primary,
}

impl button::StyleSheet for Button {
fn active(&self) -> button::Style {
button::Style {
background: Some(Background::Color(match self {
Button::Primary => Color::from_rgb(0.11, 0.42, 0.87),
})),
border_radius: 12,
shadow_offset: Vector::new(1.0, 1.0),
text_color: Color::WHITE,
..button::Style::default()
}
}
}
}
22 changes: 3 additions & 19 deletions examples/progress_bar.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
use iced::{
settings::Window, slider, Background, Color, Column, Element, Length,
ProgressBar, Sandbox, Settings, Slider,
};
use iced::{slider, Column, Element, ProgressBar, Sandbox, Settings, Slider};

pub fn main() {
Progress::run(Settings {
window: Window {
size: (700, 300),
resizable: true,
decorations: true,
},
})
Progress::run(Settings::default())
}

#[derive(Default)]
Expand Down Expand Up @@ -44,14 +35,7 @@ impl Sandbox for Progress {
fn view(&mut self) -> Element<Message> {
Column::new()
.padding(20)
.push(
ProgressBar::new(0.0..=100.0, self.value)
.background(Background::Color(Color::from_rgb(
0.6, 0.6, 0.6,
)))
.active_color(Color::from_rgb(0.0, 0.95, 0.0))
.height(Length::Units(30)),
)
.push(ProgressBar::new(0.0..=100.0, self.value))
.push(Slider::new(
&mut self.progress_bar_slider,
0.0..=100.0,
Expand Down
46 changes: 35 additions & 11 deletions examples/stopwatch.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use iced::{
button, Align, Application, Background, Button, Color, Column, Command,
Container, Element, HorizontalAlignment, Length, Row, Settings,
Subscription, Text,
button, Align, Application, Button, Column, Command, Container, Element,
HorizontalAlignment, Length, Row, Settings, Subscription, Text,
};
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -98,30 +97,29 @@ impl Application for Stopwatch {
))
.size(40);

let button = |state, label, color: [f32; 3]| {
let button = |state, label, style| {
Button::new(
state,
Text::new(label)
.color(Color::WHITE)
.horizontal_alignment(HorizontalAlignment::Center),
)
.min_width(80)
.background(Background::Color(color.into()))
.border_radius(10)
.padding(10)
.style(style)
};

let toggle_button = {
let (label, color) = match self.state {
State::Idle => ("Start", [0.11, 0.42, 0.87]),
State::Ticking { .. } => ("Stop", [0.9, 0.4, 0.4]),
State::Idle => ("Start", style::Button::Primary),
State::Ticking { .. } => ("Stop", style::Button::Destructive),
};

button(&mut self.toggle, label, color).on_press(Message::Toggle)
};

let reset_button = button(&mut self.reset, "Reset", [0.7, 0.7, 0.7])
.on_press(Message::Reset);
let reset_button =
button(&mut self.reset, "Reset", style::Button::Secondary)
.on_press(Message::Reset);

let controls = Row::new()
.spacing(20)
Expand Down Expand Up @@ -177,3 +175,29 @@ mod time {
}
}
}

mod style {
use iced::{button, Background, Color, Vector};

pub enum Button {
Primary,
Secondary,
Destructive,
}

impl button::StyleSheet for Button {
fn active(&self) -> button::Style {
button::Style {
background: Some(Background::Color(match self {
Button::Primary => Color::from_rgb(0.11, 0.42, 0.87),
Button::Secondary => Color::from_rgb(0.5, 0.5, 0.5),
Button::Destructive => Color::from_rgb(0.8, 0.2, 0.2),
})),
border_radius: 12,
shadow_offset: Vector::new(1.0, 1.0),
text_color: Color::WHITE,
..button::Style::default()
}
}
}
}
Loading