Skip to content

Commit

Permalink
refactor(widgets): 💡 use variant_color in the class of Slider
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Dec 17, 2024
1 parent dc0fa7c commit 2df2f6d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 79 deletions.
124 changes: 45 additions & 79 deletions themes/material/src/classes/slider_cls.rs
Original file line number Diff line number Diff line change
@@ -1,114 +1,80 @@
use ribir_core::prelude::*;
use ribir_widgets::prelude::*;

use crate::md;

const INDICATOR_HEIGHT: f32 = 44.;
const TRACK_HEIGHT: f32 = 16.;
const TRACK_WIDTH: f32 = 4.;

const SMALL_RADIUS: f32 = 2.;
const LARGE_RADIUS: f32 = 8.;
const STOP_INDICATOR_MARGIN: EdgeInsets = EdgeInsets::horizontal(6.);
const STOP_INDICATOR_SIZE: Size = Size::new(4., 4.);
const RADIUS_L2_R8: Radius = Radius::new(2., 8., 2., 8.);
const RADIUS_L8_R2: Radius = Radius::new(8., 2., 8., 2.);

macro_rules! stop_indicator_class {
($($field: ident: $value: expr),* ) => {
style_class! {
v_align: VAlign::Center,
border_radius: Radius::all(SMALL_RADIUS),
margin: STOP_INDICATOR_MARGIN,
clamp: BoxClamp::fixed_size(STOP_INDICATOR_SIZE),
border_radius: md::RADIUS_2,
margin: md::EDGES_HOR_6,
clamp: BoxClamp::fixed_size(md::SIZE_4),
$($field: $value),*
}
};
}

pub(super) fn init(classes: &mut Classes) {
classes.insert(
SLIDER_CONTAINER,
style_class!(
cursor: CursorIcon::Pointer,
clamp: BoxClamp::fixed_height(INDICATOR_HEIGHT)
),
);
classes.insert(SLIDER_ACTIVE_TRACK, |w| {
fn_widget! {
let w = FatObj::new(w);
@ $w {
background: Palette::of(BuildCtx::get()).primary(),
border_radius: Radius::new(LARGE_RADIUS, SMALL_RADIUS, LARGE_RADIUS, SMALL_RADIUS),
clamp: BoxClamp::fixed_height(TRACK_HEIGHT),
}
}
.into_widget()
classes.insert(SLIDER_CONTAINER, style_class! {
cursor: CursorIcon::Pointer,
clamp: BoxClamp::fixed_height(INDICATOR_HEIGHT)
});
classes.insert(SLIDER_ACTIVE_TRACK, style_class! {
background: BuildCtx::get().variant_color(),
border_radius: RADIUS_L8_R2,
clamp: BoxClamp::fixed_height(TRACK_HEIGHT),
});

classes.insert(SLIDER_INACTIVE_TRACK, |w| {
fn_widget! {
let w = FatObj::new(w);
@ $w {
border_radius: Radius::new(SMALL_RADIUS, LARGE_RADIUS, SMALL_RADIUS, LARGE_RADIUS),
background: Palette::of(BuildCtx::get()).secondary_container(),
clamp: BoxClamp::fixed_height(TRACK_HEIGHT),
}
}
.into_widget()
classes.insert(SLIDER_INACTIVE_TRACK, style_class! {
border_radius: RADIUS_L2_R8,
background: BuildCtx::get().variant_color_container(),
clamp: BoxClamp::fixed_height(TRACK_HEIGHT),
});

classes.insert(SLIDER_INDICATOR, |w| {
fn_widget! {
let w = FatObj::new(w);
@ $w {
v_align: VAlign::Center,
background: Palette::of(BuildCtx::get()).primary(),
border_radius: Radius::all(SMALL_RADIUS),
margin: EdgeInsets::horizontal(6.),
clamp: BoxClamp::fixed_size(Size::new(TRACK_WIDTH, INDICATOR_HEIGHT)),
}
}
.into_widget()
classes.insert(SLIDER_INDICATOR, style_class! {
v_align: VAlign::Center,
background: BuildCtx::get().variant_color(),
border_radius: md::RADIUS_2,
margin: EdgeInsets::horizontal(6.),
clamp: BoxClamp::fixed_size(Size::new(md::THICKNESS_4, INDICATOR_HEIGHT)),
});

classes.insert(RANGE_SLIDER_INACTIVE_TRACK_LEFT, |w| {
fn_widget! {
let w = FatObj::new(w);
@ $w {
border_radius: Radius::new(LARGE_RADIUS, SMALL_RADIUS, LARGE_RADIUS, SMALL_RADIUS),
background: Palette::of(BuildCtx::get()).secondary_container(),
clamp: BoxClamp::fixed_height(TRACK_HEIGHT),
}
}
.into_widget()
classes.insert(RANGE_SLIDER_INACTIVE_TRACK_LEFT, style_class! {
border_radius: RADIUS_L8_R2,
background: BuildCtx::get().variant_color_container(),
clamp: BoxClamp::fixed_height(TRACK_HEIGHT),
});

classes.insert(RANGE_SLIDER_INACTIVE_TRACK_RIGHT, |w| {
fn_widget! {
let w = FatObj::new(w);
@ $w {
border_radius: Radius::new(SMALL_RADIUS, LARGE_RADIUS, SMALL_RADIUS, LARGE_RADIUS,),
background: Palette::of(BuildCtx::get()).secondary_container(),
clamp: BoxClamp::fixed_height(TRACK_HEIGHT),
}
}
.into_widget()
classes.insert(RANGE_SLIDER_INACTIVE_TRACK_RIGHT, style_class! {
border_radius: RADIUS_L2_R8,
background: BuildCtx::get().variant_color_container(),
clamp: BoxClamp::fixed_height(TRACK_HEIGHT),
});

classes.insert(RANGE_SLIDER_ACTIVE_TRACK, |w| {
fn_widget! {
let w = FatObj::new(w);
@ $w {
border_radius: Radius::all(SMALL_RADIUS),
background: Palette::of(BuildCtx::get()).primary(),
clamp: BoxClamp::fixed_height(TRACK_HEIGHT),
}
}
.into_widget()
classes.insert(RANGE_SLIDER_ACTIVE_TRACK, style_class! {
border_radius: md::RADIUS_2,
background: BuildCtx::get().variant_color(),
clamp: BoxClamp::fixed_height(TRACK_HEIGHT),
});

classes.insert(STOP_INDICATOR_ACTIVE, stop_indicator_class! {
background: Palette::of(BuildCtx::get()).on_primary()
background: {
let ctx = BuildCtx::get();
Palette::of(ctx).on_of(&ctx.variant_color())
}
});

classes.insert(STOP_INDICATOR_INACTIVE, stop_indicator_class! {
background: Palette::of(BuildCtx::get()).on_secondary_container()
background: {
let ctx = BuildCtx::get();
Palette::of(ctx).on_of(&ctx.variant_color_container())
}
});
}
4 changes: 4 additions & 0 deletions themes/material/src/md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ pub const SIZE_10: Size = Size::new(10., 10.);
pub const SIZE_20: Size = Size::new(20., 20.);
pub const SIZE_40: Size = Size::new(40., 40.);

pub const SIZE_4: Size = Size::new(4., 4.);
pub const SIZE_8: Size = Size::new(8., 8.);
pub const SIZE_18: Size = Size::new(18., 18.);
pub const SIZE_24: Size = Size::new(24., 24.);
pub const SIZE_36: Size = Size::new(36., 36.);
Expand All @@ -90,6 +92,8 @@ pub const EDGES_RIGHT_4: EdgeInsets = EdgeInsets::only_right(4.);
pub const EDGES_TOP_4: EdgeInsets = EdgeInsets::only_top(4.);
pub const EDGES_BOTTOM_4: EdgeInsets = EdgeInsets::only_bottom(4.);

pub const EDGES_HOR_6: EdgeInsets = EdgeInsets::horizontal(6.);

// Borders
pub fn border_primary_2() -> Border {
let primary = Palette::of(BuildCtx::get()).primary();
Expand Down

0 comments on commit 2df2f6d

Please sign in to comment.