Skip to content

Commit 4e409bb

Browse files
authored
Merge pull request #1669 from GyulyVGC/master
Added scrollable style `focused` to be displayed when mouse is over the scrollable area
2 parents b74ff9f + dcccf70 commit 4e409bb

File tree

4 files changed

+76
-38
lines changed

4 files changed

+76
-38
lines changed

examples/scrollable/src/main.rs

+25-11
Original file line numberDiff line numberDiff line change
@@ -338,22 +338,36 @@ impl scrollable::StyleSheet for ScrollbarCustomStyle {
338338
style.active(&theme::Scrollable::Default)
339339
}
340340

341-
fn hovered(&self, style: &Self::Style) -> Scrollbar {
342-
style.hovered(&theme::Scrollable::Default)
341+
fn hovered(
342+
&self,
343+
style: &Self::Style,
344+
is_mouse_over_scrollbar: bool,
345+
) -> Scrollbar {
346+
style.hovered(&theme::Scrollable::Default, is_mouse_over_scrollbar)
343347
}
344348

345-
fn hovered_horizontal(&self, style: &Self::Style) -> Scrollbar {
346-
Scrollbar {
347-
background: style.active(&theme::Scrollable::default()).background,
348-
border_radius: 0.0,
349-
border_width: 0.0,
350-
border_color: Default::default(),
351-
scroller: Scroller {
352-
color: Color::from_rgb8(250, 85, 134),
349+
fn hovered_horizontal(
350+
&self,
351+
style: &Self::Style,
352+
is_mouse_over_scrollbar: bool,
353+
) -> Scrollbar {
354+
if is_mouse_over_scrollbar {
355+
Scrollbar {
356+
background: style
357+
.active(&theme::Scrollable::default())
358+
.background,
353359
border_radius: 0.0,
354360
border_width: 0.0,
355361
border_color: Default::default(),
356-
},
362+
scroller: Scroller {
363+
color: Color::from_rgb8(250, 85, 134),
364+
border_radius: 0.0,
365+
border_width: 0.0,
366+
border_color: Default::default(),
367+
},
368+
}
369+
} else {
370+
self.active(style)
357371
}
358372
}
359373
}

native/src/widget/scrollable.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -857,8 +857,8 @@ pub fn draw<Renderer>(
857857
if let Some(scrollbar) = scrollbars.y {
858858
let style = if state.y_scroller_grabbed_at.is_some() {
859859
theme.dragging(style)
860-
} else if mouse_over_y_scrollbar {
861-
theme.hovered(style)
860+
} else if mouse_over_scrollable {
861+
theme.hovered(style, mouse_over_y_scrollbar)
862862
} else {
863863
theme.active(style)
864864
};
@@ -870,8 +870,8 @@ pub fn draw<Renderer>(
870870
if let Some(scrollbar) = scrollbars.x {
871871
let style = if state.x_scroller_grabbed_at.is_some() {
872872
theme.dragging_horizontal(style)
873-
} else if mouse_over_x_scrollbar {
874-
theme.hovered_horizontal(style)
873+
} else if mouse_over_scrollable {
874+
theme.hovered_horizontal(style, mouse_over_x_scrollbar)
875875
} else {
876876
theme.active_horizontal(style)
877877
};

style/src/scrollable.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,34 @@ pub trait StyleSheet {
3737
/// Produces the style of an active scrollbar.
3838
fn active(&self, style: &Self::Style) -> Scrollbar;
3939

40-
/// Produces the style of a hovered scrollbar.
41-
fn hovered(&self, style: &Self::Style) -> Scrollbar;
40+
/// Produces the style of a scrollbar when the scrollable is being hovered.
41+
fn hovered(
42+
&self,
43+
style: &Self::Style,
44+
is_mouse_over_scrollbar: bool,
45+
) -> Scrollbar;
4246

4347
/// Produces the style of a scrollbar that is being dragged.
4448
fn dragging(&self, style: &Self::Style) -> Scrollbar {
45-
self.hovered(style)
49+
self.hovered(style, true)
4650
}
4751

4852
/// Produces the style of an active horizontal scrollbar.
4953
fn active_horizontal(&self, style: &Self::Style) -> Scrollbar {
5054
self.active(style)
5155
}
5256

53-
/// Produces the style of a hovered horizontal scrollbar.
54-
fn hovered_horizontal(&self, style: &Self::Style) -> Scrollbar {
55-
self.hovered(style)
57+
/// Produces the style of a horizontal scrollbar when the scrollable is being hovered.
58+
fn hovered_horizontal(
59+
&self,
60+
style: &Self::Style,
61+
is_mouse_over_scrollbar: bool,
62+
) -> Scrollbar {
63+
self.hovered(style, is_mouse_over_scrollbar)
5664
}
5765

5866
/// Produces the style of a horizontal scrollbar that is being dragged.
5967
fn dragging_horizontal(&self, style: &Self::Style) -> Scrollbar {
60-
self.hovered_horizontal(style)
68+
self.hovered_horizontal(style, true)
6169
}
6270
}

style/src/theme.rs

+32-16
Original file line numberDiff line numberDiff line change
@@ -906,31 +906,41 @@ impl scrollable::StyleSheet for Theme {
906906
}
907907
}
908908

909-
fn hovered(&self, style: &Self::Style) -> scrollable::Scrollbar {
909+
fn hovered(
910+
&self,
911+
style: &Self::Style,
912+
is_mouse_over_scrollbar: bool,
913+
) -> scrollable::Scrollbar {
910914
match style {
911915
Scrollable::Default => {
912-
let palette = self.extended_palette();
916+
if is_mouse_over_scrollbar {
917+
let palette = self.extended_palette();
913918

914-
scrollable::Scrollbar {
915-
background: palette.background.weak.color.into(),
916-
border_radius: 2.0,
917-
border_width: 0.0,
918-
border_color: Color::TRANSPARENT,
919-
scroller: scrollable::Scroller {
920-
color: palette.primary.strong.color,
919+
scrollable::Scrollbar {
920+
background: palette.background.weak.color.into(),
921921
border_radius: 2.0,
922922
border_width: 0.0,
923923
border_color: Color::TRANSPARENT,
924-
},
924+
scroller: scrollable::Scroller {
925+
color: palette.primary.strong.color,
926+
border_radius: 2.0,
927+
border_width: 0.0,
928+
border_color: Color::TRANSPARENT,
929+
},
930+
}
931+
} else {
932+
self.active(style)
925933
}
926934
}
927-
Scrollable::Custom(custom) => custom.hovered(self),
935+
Scrollable::Custom(custom) => {
936+
custom.hovered(self, is_mouse_over_scrollbar)
937+
}
928938
}
929939
}
930940

931941
fn dragging(&self, style: &Self::Style) -> scrollable::Scrollbar {
932942
match style {
933-
Scrollable::Default => self.hovered(style),
943+
Scrollable::Default => self.hovered(style, true),
934944
Scrollable::Custom(custom) => custom.dragging(self),
935945
}
936946
}
@@ -942,10 +952,16 @@ impl scrollable::StyleSheet for Theme {
942952
}
943953
}
944954

945-
fn hovered_horizontal(&self, style: &Self::Style) -> scrollable::Scrollbar {
955+
fn hovered_horizontal(
956+
&self,
957+
style: &Self::Style,
958+
is_mouse_over_scrollbar: bool,
959+
) -> scrollable::Scrollbar {
946960
match style {
947-
Scrollable::Default => self.hovered(style),
948-
Scrollable::Custom(custom) => custom.hovered_horizontal(self),
961+
Scrollable::Default => self.hovered(style, is_mouse_over_scrollbar),
962+
Scrollable::Custom(custom) => {
963+
custom.hovered_horizontal(self, is_mouse_over_scrollbar)
964+
}
949965
}
950966
}
951967

@@ -954,7 +970,7 @@ impl scrollable::StyleSheet for Theme {
954970
style: &Self::Style,
955971
) -> scrollable::Scrollbar {
956972
match style {
957-
Scrollable::Default => self.hovered_horizontal(style),
973+
Scrollable::Default => self.hovered_horizontal(style, true),
958974
Scrollable::Custom(custom) => custom.dragging_horizontal(self),
959975
}
960976
}

0 commit comments

Comments
 (0)