Skip to content

Commit 0a83024

Browse files
authored
Merge pull request gfx-rs#146 from hecrj/feature/custom-styling
Custom styling
2 parents 6699329 + 7b27875 commit 0a83024

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+2288
-595
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ maintenance = { status = "actively-developed" }
2424
members = [
2525
"core",
2626
"native",
27+
"style",
2728
"web",
2829
"wgpu",
2930
"winit",

core/src/color.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ impl Color {
2525
a: 1.0,
2626
};
2727

28+
/// A color with no opacity.
29+
pub const TRANSPARENT: Color = Color {
30+
r: 0.0,
31+
g: 0.0,
32+
b: 0.0,
33+
a: 0.0,
34+
};
35+
36+
/// Creates a [`Color`] from its RGB components.
37+
///
38+
/// [`Color`]: struct.Color.html
39+
pub const fn from_rgb(r: f32, g: f32, b: f32) -> Color {
40+
Color { r, g, b, a: 1.0 }
41+
}
42+
2843
/// Creates a [`Color`] from its RGB8 components.
2944
///
3045
/// [`Color`]: struct.Color.html
@@ -37,13 +52,6 @@ impl Color {
3752
}
3853
}
3954

40-
/// Creates a [`Color`] from its RGB components.
41-
///
42-
/// [`Color`]: struct.Color.html
43-
pub fn from_rgb(r: f32, g: f32, b: f32) -> Color {
44-
Color { r, g, b, a: 1.0 }
45-
}
46-
4755
/// Converts the [`Color`] into its linear values.
4856
///
4957
/// [`Color`]: struct.Color.html

core/src/vector.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ where
3131
Self::new(self.x + b.x, self.y + b.y)
3232
}
3333
}
34+
35+
impl<T> Default for Vector<T>
36+
where
37+
T: Default,
38+
{
39+
fn default() -> Self {
40+
Self {
41+
x: T::default(),
42+
y: T::default(),
43+
}
44+
}
45+
}

examples/custom_widget.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mod circle {
1313
layout, Background, Color, Element, Hasher, Layout, Length,
1414
MouseCursor, Point, Size, Widget,
1515
};
16-
use iced_wgpu::{Primitive, Renderer};
16+
use iced_wgpu::{Defaults, Primitive, Renderer};
1717

1818
pub struct Circle {
1919
radius: u16,
@@ -54,6 +54,7 @@ mod circle {
5454
fn draw(
5555
&self,
5656
_renderer: &mut Renderer,
57+
_defaults: &Defaults,
5758
layout: Layout<'_>,
5859
_cursor_position: Point,
5960
) -> (Primitive, MouseCursor) {
@@ -62,6 +63,8 @@ mod circle {
6263
bounds: layout.bounds(),
6364
background: Background::Color(Color::BLACK),
6465
border_radius: self.radius,
66+
border_width: 0,
67+
border_color: Color::TRANSPARENT,
6568
},
6669
MouseCursor::OutOfBounds,
6770
)

examples/geometry.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod rainbow {
1616
};
1717
use iced_wgpu::{
1818
triangle::{Mesh2D, Vertex2D},
19-
Primitive, Renderer,
19+
Defaults, Primitive, Renderer,
2020
};
2121

2222
pub struct Rainbow;
@@ -51,6 +51,7 @@ mod rainbow {
5151
fn draw(
5252
&self,
5353
_renderer: &mut Renderer,
54+
_defaults: &Defaults,
5455
layout: Layout<'_>,
5556
cursor_position: Point,
5657
) -> (Primitive, MouseCursor) {

examples/pokedex.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use iced::{
2-
button, image, Align, Application, Button, Color, Column, Command,
3-
Container, Element, Image, Length, Row, Settings, Text,
2+
button, image, Align, Application, Button, Column, Command, Container,
3+
Element, Image, Length, Row, Settings, Text,
44
};
55

66
pub fn main() {
@@ -214,8 +214,29 @@ impl From<surf::Exception> for Error {
214214
}
215215

216216
fn button<'a>(state: &'a mut button::State, text: &str) -> Button<'a, Message> {
217-
Button::new(state, Text::new(text).color(Color::WHITE))
218-
.background(Color::from_rgb(0.11, 0.42, 0.87))
219-
.border_radius(10)
217+
Button::new(state, Text::new(text))
220218
.padding(10)
219+
.style(style::Button::Primary)
220+
}
221+
222+
mod style {
223+
use iced::{button, Background, Color, Vector};
224+
225+
pub enum Button {
226+
Primary,
227+
}
228+
229+
impl button::StyleSheet for Button {
230+
fn active(&self) -> button::Style {
231+
button::Style {
232+
background: Some(Background::Color(match self {
233+
Button::Primary => Color::from_rgb(0.11, 0.42, 0.87),
234+
})),
235+
border_radius: 12,
236+
shadow_offset: Vector::new(1.0, 1.0),
237+
text_color: Color::WHITE,
238+
..button::Style::default()
239+
}
240+
}
241+
}
221242
}

examples/progress_bar.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
use iced::{
2-
settings::Window, slider, Background, Color, Column, Element, Length,
3-
ProgressBar, Sandbox, Settings, Slider,
4-
};
1+
use iced::{slider, Column, Element, ProgressBar, Sandbox, Settings, Slider};
52

63
pub fn main() {
7-
Progress::run(Settings {
8-
window: Window {
9-
size: (700, 300),
10-
resizable: true,
11-
decorations: true,
12-
},
13-
})
4+
Progress::run(Settings::default())
145
}
156

167
#[derive(Default)]
@@ -44,14 +35,7 @@ impl Sandbox for Progress {
4435
fn view(&mut self) -> Element<Message> {
4536
Column::new()
4637
.padding(20)
47-
.push(
48-
ProgressBar::new(0.0..=100.0, self.value)
49-
.background(Background::Color(Color::from_rgb(
50-
0.6, 0.6, 0.6,
51-
)))
52-
.active_color(Color::from_rgb(0.0, 0.95, 0.0))
53-
.height(Length::Units(30)),
54-
)
38+
.push(ProgressBar::new(0.0..=100.0, self.value))
5539
.push(Slider::new(
5640
&mut self.progress_bar_slider,
5741
0.0..=100.0,

examples/stopwatch.rs

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use iced::{
2-
button, Align, Application, Background, Button, Color, Column, Command,
3-
Container, Element, HorizontalAlignment, Length, Row, Settings,
4-
Subscription, Text,
2+
button, Align, Application, Button, Column, Command, Container, Element,
3+
HorizontalAlignment, Length, Row, Settings, Subscription, Text,
54
};
65
use std::time::{Duration, Instant};
76

@@ -98,30 +97,29 @@ impl Application for Stopwatch {
9897
))
9998
.size(40);
10099

101-
let button = |state, label, color: [f32; 3]| {
100+
let button = |state, label, style| {
102101
Button::new(
103102
state,
104103
Text::new(label)
105-
.color(Color::WHITE)
106104
.horizontal_alignment(HorizontalAlignment::Center),
107105
)
108106
.min_width(80)
109-
.background(Background::Color(color.into()))
110-
.border_radius(10)
111107
.padding(10)
108+
.style(style)
112109
};
113110

114111
let toggle_button = {
115112
let (label, color) = match self.state {
116-
State::Idle => ("Start", [0.11, 0.42, 0.87]),
117-
State::Ticking { .. } => ("Stop", [0.9, 0.4, 0.4]),
113+
State::Idle => ("Start", style::Button::Primary),
114+
State::Ticking { .. } => ("Stop", style::Button::Destructive),
118115
};
119116

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

123-
let reset_button = button(&mut self.reset, "Reset", [0.7, 0.7, 0.7])
124-
.on_press(Message::Reset);
120+
let reset_button =
121+
button(&mut self.reset, "Reset", style::Button::Secondary)
122+
.on_press(Message::Reset);
125123

126124
let controls = Row::new()
127125
.spacing(20)
@@ -177,3 +175,29 @@ mod time {
177175
}
178176
}
179177
}
178+
179+
mod style {
180+
use iced::{button, Background, Color, Vector};
181+
182+
pub enum Button {
183+
Primary,
184+
Secondary,
185+
Destructive,
186+
}
187+
188+
impl button::StyleSheet for Button {
189+
fn active(&self) -> button::Style {
190+
button::Style {
191+
background: Some(Background::Color(match self {
192+
Button::Primary => Color::from_rgb(0.11, 0.42, 0.87),
193+
Button::Secondary => Color::from_rgb(0.5, 0.5, 0.5),
194+
Button::Destructive => Color::from_rgb(0.8, 0.2, 0.2),
195+
})),
196+
border_radius: 12,
197+
shadow_offset: Vector::new(1.0, 1.0),
198+
text_color: Color::WHITE,
199+
..button::Style::default()
200+
}
201+
}
202+
}
203+
}

0 commit comments

Comments
 (0)