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

Left and right colors for slider rails #1643

Merged
merged 5 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
88 changes: 51 additions & 37 deletions native/src/widget/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use crate::renderer;
use crate::touch;
use crate::widget::tree::{self, Tree};
use crate::{
Background, Clipboard, Color, Element, Layout, Length, Pixels, Point,
Rectangle, Shell, Size, Widget,
Clipboard, Color, Element, Layout, Length, Pixels, Point, Rectangle, Shell,
Size, Widget,
};

use std::ops::RangeInclusive;

pub use iced_style::slider::{Appearance, Handle, HandleShape, StyleSheet};
pub use iced_style::slider::{
Appearance, Handle, HandleShape, Rail, StyleSheet,
};

/// An horizontal bar and a handle that selects a single value from a range of
/// values.
Expand Down Expand Up @@ -368,38 +370,6 @@ pub fn draw<T, R>(
style_sheet.active(style)
};

let rail_y = bounds.y + (bounds.height / 2.0).round();

renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: bounds.x,
y: rail_y - 1.0,
width: bounds.width,
height: 2.0,
},
border_radius: 0.0.into(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
style.rail_colors.0,
);

renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: bounds.x,
y: rail_y + 1.0,
width: bounds.width,
height: 2.0,
},
border_radius: 0.0.into(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
Background::Color(style.rail_colors.1),
);

let (handle_width, handle_height, handle_border_radius) = match style
.handle
.shape
Expand All @@ -418,17 +388,61 @@ pub fn draw<T, R>(
(start.into() as f32, end.into() as f32)
};

let handle_offset = if range_start >= range_end {
let offset = if range_start >= range_end {
0.0
} else {
(bounds.width - handle_width) * (value - range_start)
/ (range_end - range_start)
};

let rail_y = bounds.y + bounds.height / 2.0;

renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: bounds.x,
y: rail_y - style.rail.width / 2.0,
width: offset,
height: style.rail.width,
},
border_radius: [
style.rail.border_radius,
0.0,
0.0,
style.rail.border_radius,
]
.into(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
style.rail.colors.0,
);

renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: bounds.x + offset,
y: rail_y - style.rail.width / 2.0,
width: bounds.width - offset,
height: style.rail.width,
},
border_radius: [
0.0,
style.rail.border_radius,
style.rail.border_radius,
0.0,
]
.into(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
style.rail.colors.1,
);

renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: bounds.x + handle_offset.round(),
x: bounds.x + offset.round(),
y: rail_y - handle_height / 2.0,
width: handle_width,
height: handle_height,
Expand Down
86 changes: 49 additions & 37 deletions native/src/widget/vertical_slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub use iced_style::slider::{Appearance, Handle, HandleShape, StyleSheet};
use crate::event::{self, Event};
use crate::widget::tree::{self, Tree};
use crate::{
layout, mouse, renderer, touch, Background, Clipboard, Color, Element,
Layout, Length, Pixels, Point, Rectangle, Shell, Size, Widget,
layout, mouse, renderer, touch, Clipboard, Color, Element, Layout, Length,
Pixels, Point, Rectangle, Shell, Size, Widget,
};

/// An vertical bar and a handle that selects a single value from a range of
Expand Down Expand Up @@ -363,38 +363,6 @@ pub fn draw<T, R>(
style_sheet.active(style)
};

let rail_x = bounds.x + (bounds.width / 2.0).round();

renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: rail_x - 1.0,
y: bounds.y,
width: 2.0,
height: bounds.height,
},
border_radius: 0.0.into(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
style.rail_colors.0,
);

renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: rail_x + 1.0,
y: bounds.y,
width: 2.0,
height: bounds.height,
},
border_radius: 0.0.into(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
Background::Color(style.rail_colors.1),
);

let (handle_width, handle_height, handle_border_radius) = match style
.handle
.shape
Expand All @@ -413,18 +381,62 @@ pub fn draw<T, R>(
(start.into() as f32, end.into() as f32)
};

let handle_offset = if range_start >= range_end {
let offset = if range_start >= range_end {
0.0
} else {
(bounds.height - handle_width) * (value - range_end)
/ (range_start - range_end)
};

let rail_x = bounds.x + bounds.width / 2.0;

renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: rail_x - style.rail.width / 2.0,
y: bounds.y,
width: style.rail.width,
height: offset,
},
border_radius: [
style.rail.border_radius,
style.rail.border_radius,
0.0,
0.0,
]
.into(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
style.rail.colors.1,
);

renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: rail_x - style.rail.width / 2.0,
y: bounds.y + offset,
width: style.rail.width,
height: bounds.height - offset,
},
border_radius: [
0.0,
0.0,
style.rail.border_radius,
style.rail.border_radius,
]
.into(),
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
style.rail.colors.0,
);

renderer.fill_quad(
renderer::Quad {
bounds: Rectangle {
x: rail_x - (handle_height / 2.0),
y: bounds.y + handle_offset.round(),
x: rail_x - handle_height / 2.0,
y: bounds.y + offset.round(),
width: handle_height,
height: handle_width,
},
Expand Down
13 changes: 12 additions & 1 deletion style/src/slider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@ use iced_core::Color;
#[derive(Debug, Clone, Copy)]
pub struct Appearance {
/// The colors of the rail of the slider.
pub rail_colors: (Color, Color),
pub rail: Rail,
/// The appearance of the [`Handle`] of the slider.
pub handle: Handle,
}

/// The appearance of a slider rail
#[derive(Debug, Clone, Copy)]
pub struct Rail {
/// The colors of the rail of the slider.
pub colors: (Color, Color),
/// The width of the stroke of a slider rail.
pub width: f32,
/// The border radius of the slider.
pub border_radius: f32,
}

/// The appearance of the handle of a slider.
#[derive(Debug, Clone, Copy)]
pub struct Handle {
Expand Down
12 changes: 8 additions & 4 deletions style/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,10 +416,14 @@ impl slider::StyleSheet for Theme {
};

slider::Appearance {
rail_colors: (
palette.primary.base.color,
Color::TRANSPARENT,
),
rail: slider::Rail {
colors: (
palette.primary.base.color,
palette.primary.base.color,
),
width: 2.0,
border_radius: 2.0,
},
handle: slider::Handle {
color: palette.background.base.color,
border_color: palette.primary.base.color,
Expand Down