Skip to content

Commit

Permalink
gui: Use the active link color from env
Browse files Browse the repository at this point in the history
This is more in line with the rest of the link colors, as we customize them through the `EnvScope` widget.
  • Loading branch information
jpochyla committed Jul 18, 2021
1 parent 7827eca commit 309d66b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
2 changes: 1 addition & 1 deletion psst-gui/src/ui/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ fn track_widget(display: TrackDisplay) -> impl Widget<TrackRow> {
.with_child(minor)
.padding(theme::grid(1.0))
.link()
.active_background(|tr: &TrackRow, _env: &Env| tr.ctx.is_track_playing(&tr.track), theme::LINK_ACTIVE_COLOR)
.active(|tr: &TrackRow, _| tr.ctx.is_track_playing(&tr.track))
.rounded(theme::BUTTON_BORDER_RADIUS)
.on_ex_click(move |ctx, event, tr: &mut TrackRow, _| match event.button {
MouseButton::Left => {
Expand Down
40 changes: 14 additions & 26 deletions psst-gui/src/widget/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Link<T> {
border_color: KeyOrValue<Color>,
border_width: KeyOrValue<f64>,
corner_radius: KeyOrValue<f64>,
active_background: Option<(Box<dyn Fn(&T, &Env) -> bool>, KeyOrValue<Color>)>,
is_active: Option<Box<dyn Fn(&T, &Env) -> bool>>,
}

impl<T: Data> Link<T> {
Expand All @@ -19,7 +19,7 @@ impl<T: Data> Link<T> {
border_color: theme::LINK_HOT_COLOR.into(),
border_width: 0.0.into(),
corner_radius: 0.0.into(),
active_background: None,
is_active: None,
}
}

Expand All @@ -28,35 +28,22 @@ impl<T: Data> Link<T> {
color: impl Into<KeyOrValue<Color>>,
width: impl Into<KeyOrValue<f64>>,
) -> Self {
self.set_border(color, width);
self
}

pub fn set_border(
&mut self,
color: impl Into<KeyOrValue<Color>>,
width: impl Into<KeyOrValue<f64>>,
) {
self.border_color = color.into();
self.border_width = width.into();
}

pub fn circle(mut self) -> Self {
self.set_rounded(64.0);
self
}

pub fn rounded(mut self, radius: impl Into<KeyOrValue<f64>>) -> Self {
self.set_rounded(radius);
self.corner_radius = radius.into();
self
}

pub fn set_rounded(&mut self, radius: impl Into<KeyOrValue<f64>>) {
self.corner_radius = radius.into();
pub fn circle(self) -> Self {
self.rounded(f64::INFINITY)
}

pub fn active_background(mut self, predicate: impl Fn(&T, &Env) -> bool + 'static, color: impl Into<KeyOrValue<Color>>) -> Self {
self.active_background = Some((Box::new(predicate), color.into()));
pub fn active(mut self, predicate: impl Fn(&T, &Env) -> bool + 'static) -> Self {
self.is_active = Some(Box::new(predicate));
self
}
}
Expand Down Expand Up @@ -87,12 +74,13 @@ impl<T: Data> Widget<T> for Link<T> {
let background = if ctx.is_hot() {
env.get(theme::LINK_HOT_COLOR)
} else {
if let Some((predicate, active_background)) = &self.active_background {
if (predicate)(data, env) {
active_background.resolve(env)
} else {
env.get(theme::LINK_COLD_COLOR)
}
let is_active = self
.is_active
.as_ref()
.map(|predicate| predicate(data, env))
.unwrap_or(false);
if is_active {
env.get(theme::LINK_ACTIVE_COLOR)
} else {
env.get(theme::LINK_COLD_COLOR)
}
Expand Down

0 comments on commit 309d66b

Please sign in to comment.