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

Added 2 callbacks for pick_list #2152

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions style/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ pub trait StyleSheet {

/// Produces the [`Appearance`] of the svg.
fn appearance(&self, style: &Self::Style) -> Appearance;

/// Produces the hovered [`Appearance`] of a svg content.
fn hovered(&self, style: &Self::Style) -> Appearance;
}
8 changes: 8 additions & 0 deletions style/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,10 @@ impl svg::StyleSheet for Theme {
Svg::Custom(custom) => custom.appearance(self),
}
}

fn hovered(&self, style: &Self::Style) -> svg::Appearance {
self.appearance(style)
}
}

impl svg::StyleSheet for fn(&Theme) -> svg::Appearance {
Expand All @@ -917,6 +921,10 @@ impl svg::StyleSheet for fn(&Theme) -> svg::Appearance {
fn appearance(&self, style: &Self::Style) -> svg::Appearance {
(self)(style)
}

fn hovered(&self, style: &Self::Style) -> svg::Appearance {
self.appearance(style)
}
}

/// The style of a scrollable.
Expand Down
35 changes: 33 additions & 2 deletions widget/src/pick_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ where
Renderer::Theme: StyleSheet,
{
on_selected: Box<dyn Fn(T) -> Message + 'a>,
on_opened: Option<Message>,
on_closed: Option<Message>,
options: Cow<'a, [T]>,
placeholder: Option<String>,
selected: Option<T>,
Expand Down Expand Up @@ -67,6 +69,8 @@ where
) -> Self {
Self {
on_selected: Box::new(on_selected),
on_opened: None,
on_closed: None,
options: options.into(),
placeholder: None,
selected,
Expand Down Expand Up @@ -132,6 +136,18 @@ where
self
}

/// Sets the message that will be produced when the [`PickList`] Menu is openned menu.
pub fn on_opened(mut self, msg: Message) -> Self {
self.on_opened = Some(msg);
self
}

/// Sets the message that will be produced when the [`PickList`] Menu is closed menu.
pub fn on_closed(mut self, msg: Message) -> Self {
self.on_closed = Some(msg);
self
}

/// Sets the style of the [`PickList`].
pub fn style(
mut self,
Expand All @@ -147,7 +163,7 @@ impl<'a, T: 'a, Message, Renderer> Widget<Message, Renderer>
where
T: Clone + ToString + Eq + 'static,
[T]: ToOwned<Owned = Vec<T>>,
Message: 'a,
Message: 'a + Copy,
hicaru marked this conversation as resolved.
Show resolved Hide resolved
Renderer: text::Renderer + 'a,
Renderer::Theme: StyleSheet
+ scrollable::StyleSheet
Expand Down Expand Up @@ -210,6 +226,8 @@ where
cursor,
shell,
self.on_selected.as_ref(),
self.on_opened.as_ref(),
self.on_closed.as_ref(),
self.selected.as_ref(),
&self.options,
|| tree.state.downcast_mut::<State<Renderer::Paragraph>>(),
Expand Down Expand Up @@ -284,7 +302,7 @@ impl<'a, T: 'a, Message, Renderer> From<PickList<'a, T, Message, Renderer>>
where
T: Clone + ToString + Eq + 'static,
[T]: ToOwned<Owned = Vec<T>>,
Message: 'a,
Message: 'a + Copy,
Renderer: text::Renderer + 'a,
Renderer::Theme: StyleSheet
+ scrollable::StyleSheet
Expand Down Expand Up @@ -465,13 +483,16 @@ pub fn update<'a, T, P, Message>(
cursor: mouse::Cursor,
shell: &mut Shell<'_, Message>,
on_selected: &dyn Fn(T) -> Message,
on_opened: Option<&Message>,
on_closed: Option<&Message>,
selected: Option<&T>,
options: &[T],
state: impl FnOnce() -> &'a mut State<P>,
) -> event::Status
where
T: PartialEq + Clone + 'a,
P: text::Paragraph + 'a,
Message: Copy,
{
match event {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
Expand All @@ -483,12 +504,22 @@ where
// bounds or on the drop-down, either way we close the overlay.
state.is_open = false;

if let Some(on_closed) = on_closed {
// Send callback when popup just close.
shell.publish(*on_closed);
}

event::Status::Captured
} else if cursor.is_over(layout.bounds()) {
state.is_open = true;
state.hovered_option =
options.iter().position(|option| Some(option) == selected);

if let Some(on_opened) = on_opened {
// Send callback when popup just opened.
shell.publish(*on_opened);
}

event::Status::Captured
} else {
event::Status::Ignored
Expand Down
9 changes: 7 additions & 2 deletions widget/src/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,15 @@ where
theme: &Renderer::Theme,
_style: &renderer::Style,
layout: Layout<'_>,
_cursor: mouse::Cursor,
cursor: mouse::Cursor,
_viewport: &Rectangle,
) {
let Size { width, height } = renderer.dimensions(&self.handle);
let image_size = Size::new(width as f32, height as f32);

let bounds = layout.bounds();
let adjusted_fit = self.content_fit.fit(image_size, bounds.size());
let is_mouse_over = cursor.is_over(bounds);

let render = |renderer: &mut Renderer| {
let offset = Vector::new(
Expand All @@ -166,7 +167,11 @@ where
..bounds
};

let appearance = theme.appearance(&self.style);
let appearance = if is_mouse_over {
theme.appearance(&self.style)
} else {
theme.hovered(&self.style)
};

renderer.draw(
self.handle.clone(),
Expand Down