added FillMode::fill()
This commit is contained in:
parent
32561bd85c
commit
fed30ef775
@ -31,47 +31,8 @@ where
|
|||||||
- (style.width as f32 / 2.0))
|
- (style.width as f32 / 2.0))
|
||||||
.round();
|
.round();
|
||||||
|
|
||||||
let (line_x, line_width) = match style.fill_mode {
|
let (offset, line_width) = style.fill_mode.fill(bounds.width);
|
||||||
FillMode::Full => (bounds.x, bounds.width),
|
let line_x = bounds.x + offset;
|
||||||
FillMode::Percent(percent) => {
|
|
||||||
if percent >= 100.0 {
|
|
||||||
(bounds.x, bounds.width)
|
|
||||||
} else {
|
|
||||||
let percent_width =
|
|
||||||
(bounds.width * percent / 100.0).round();
|
|
||||||
|
|
||||||
(
|
|
||||||
bounds.x
|
|
||||||
+ ((bounds.width - percent_width) / 2.0)
|
|
||||||
.round(),
|
|
||||||
percent_width,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FillMode::Padded(padding) => {
|
|
||||||
if padding == 0 {
|
|
||||||
(bounds.x, bounds.width)
|
|
||||||
} else {
|
|
||||||
let padding = padding as f32;
|
|
||||||
let mut line_width = bounds.width - (padding * 2.0);
|
|
||||||
if line_width < 0.0 {
|
|
||||||
line_width = 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
(bounds.x + padding, line_width)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FillMode::AsymmetricPadding(first_pad, second_pad) => {
|
|
||||||
let first_pad = first_pad as f32;
|
|
||||||
let second_pad = second_pad as f32;
|
|
||||||
let mut line_width = bounds.width - first_pad - second_pad;
|
|
||||||
if line_width < 0.0 {
|
|
||||||
line_width = 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
(bounds.x + first_pad, line_width)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Primitive::Quad {
|
Primitive::Quad {
|
||||||
bounds: Rectangle {
|
bounds: Rectangle {
|
||||||
@ -90,48 +51,8 @@ where
|
|||||||
- (style.width as f32 / 2.0))
|
- (style.width as f32 / 2.0))
|
||||||
.round();
|
.round();
|
||||||
|
|
||||||
let (line_y, line_height) = match style.fill_mode {
|
let (offset, line_height) = style.fill_mode.fill(bounds.height);
|
||||||
FillMode::Full => (bounds.y, bounds.height),
|
let line_y = bounds.y + offset;
|
||||||
FillMode::Percent(percent) => {
|
|
||||||
if percent >= 100.0 {
|
|
||||||
(bounds.y, bounds.height)
|
|
||||||
} else {
|
|
||||||
let percent_height =
|
|
||||||
(bounds.height * percent / 100.0).round();
|
|
||||||
|
|
||||||
(
|
|
||||||
bounds.y
|
|
||||||
+ ((bounds.height - percent_height) / 2.0)
|
|
||||||
.round(),
|
|
||||||
percent_height,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FillMode::Padded(padding) => {
|
|
||||||
if padding == 0 {
|
|
||||||
(bounds.y, bounds.height)
|
|
||||||
} else {
|
|
||||||
let padding = padding as f32;
|
|
||||||
let mut line_height = bounds.height - (padding * 2.0);
|
|
||||||
if line_height < 0.0 {
|
|
||||||
line_height = 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
(bounds.y + padding, line_height)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
FillMode::AsymmetricPadding(first_pad, second_pad) => {
|
|
||||||
let first_pad = first_pad as f32;
|
|
||||||
let second_pad = second_pad as f32;
|
|
||||||
let mut line_height =
|
|
||||||
bounds.height - first_pad - second_pad;
|
|
||||||
if line_height < 0.0 {
|
|
||||||
line_height = 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
(bounds.y + first_pad, line_height)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Primitive::Quad {
|
Primitive::Quad {
|
||||||
bounds: Rectangle {
|
bounds: Rectangle {
|
||||||
|
@ -19,6 +19,53 @@ pub enum FillMode {
|
|||||||
AsymmetricPadding(u16, u16),
|
AsymmetricPadding(u16, u16),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FillMode {
|
||||||
|
/// Return the starting offset and length of the rule.
|
||||||
|
///
|
||||||
|
/// * `space` - The space to fill.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// * (starting_offset, length)
|
||||||
|
pub fn fill(&self, space: f32) -> (f32, f32) {
|
||||||
|
match *self {
|
||||||
|
FillMode::Full => (0.0, space),
|
||||||
|
FillMode::Percent(percent) => {
|
||||||
|
if percent >= 100.0 {
|
||||||
|
(0.0, space)
|
||||||
|
} else {
|
||||||
|
let percent_width = (space * percent / 100.0).round();
|
||||||
|
|
||||||
|
(((space - percent_width) / 2.0).round(), percent_width)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FillMode::Padded(padding) => {
|
||||||
|
if padding == 0 {
|
||||||
|
(0.0, space)
|
||||||
|
} else {
|
||||||
|
let padding = padding as f32;
|
||||||
|
let mut line_width = space - (padding * 2.0);
|
||||||
|
if line_width < 0.0 {
|
||||||
|
line_width = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
(padding, line_width)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FillMode::AsymmetricPadding(first_pad, second_pad) => {
|
||||||
|
let first_pad = first_pad as f32;
|
||||||
|
let second_pad = second_pad as f32;
|
||||||
|
let mut line_width = space - first_pad - second_pad;
|
||||||
|
if line_width < 0.0 {
|
||||||
|
line_width = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
(first_pad, line_width)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The appearance of a rule.
|
/// The appearance of a rule.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct Style {
|
pub struct Style {
|
||||||
@ -26,7 +73,7 @@ pub struct Style {
|
|||||||
pub color: Color,
|
pub color: Color,
|
||||||
/// The width (thickness) of the rule line.
|
/// The width (thickness) of the rule line.
|
||||||
pub width: u16,
|
pub width: u16,
|
||||||
/// The radius of the rectangle corners.
|
/// The radius of the line corners.
|
||||||
pub radius: u16,
|
pub radius: u16,
|
||||||
/// The [`FillMode`] of the rule.
|
/// The [`FillMode`] of the rule.
|
||||||
///
|
///
|
||||||
|
Loading…
Reference in New Issue
Block a user