Skip to content

Commit

Permalink
Inline Handle::content for simplicity and efficiency
Browse files Browse the repository at this point in the history
We can avoid downcasting `state` :^)
  • Loading branch information
hecrj committed Feb 14, 2023
1 parent fee1ab6 commit 7f1d58a
Showing 1 changed file with 22 additions and 31 deletions.
53 changes: 22 additions & 31 deletions native/src/widget/pick_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ where
self.selected.as_ref(),
&self.handle,
&self.style,
tree.state.downcast_ref::<State<T>>(),
|| tree.state.downcast_ref::<State<T>>(),
)
}

Expand Down Expand Up @@ -325,32 +325,6 @@ impl<Font> Default for Handle<Font> {
}
}

impl<Font: Clone> Handle<Font> {
fn content<Renderer: text::Renderer<Font = Font>>(
&self,
is_open: bool,
) -> Option<(Font, char, Option<u16>)> {
match self {
Self::Arrow { size } => {
Some((Renderer::ICON_FONT, Renderer::ARROW_DOWN_ICON, *size))
}
Self::Static(Icon {
font,
code_point,
size,
}) => Some((font.clone(), *code_point, *size)),
Self::Dynamic { open, closed } => {
if is_open {
Some((open.font.clone(), open.code_point, open.size))
} else {
Some((closed.font.clone(), closed.code_point, closed.size))
}
}
Self::None => None,
}
}
}

/// The icon of a [`Handle`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Icon<Font> {
Expand Down Expand Up @@ -593,7 +567,7 @@ pub fn draw<'a, T, Renderer>(
selected: Option<&T>,
handle: &Handle<Renderer::Font>,
style: &<Renderer::Theme as StyleSheet>::Style,
state: &State<T>,
state: impl FnOnce() -> &'a State<T>,
) where
Renderer: text::Renderer,
Renderer::Theme: StyleSheet,
Expand All @@ -619,9 +593,26 @@ pub fn draw<'a, T, Renderer>(
style.background,
);

if let Some((font, code_point, size)) =
handle.content::<Renderer>(state.is_open)
{
let handle = match handle {
Handle::Arrow { size } => {
Some((Renderer::ICON_FONT, Renderer::ARROW_DOWN_ICON, *size))
}
Handle::Static(Icon {
font,
code_point,
size,
}) => Some((font.clone(), *code_point, *size)),
Handle::Dynamic { open, closed } => {
if state().is_open {
Some((open.font.clone(), open.code_point, open.size))
} else {
Some((closed.font.clone(), closed.code_point, closed.size))
}
}
Handle::None => None,
};

if let Some((font, code_point, size)) = handle {
let size = f32::from(size.unwrap_or_else(|| renderer.default_size()));

renderer.fill_text(Text {
Expand Down

0 comments on commit 7f1d58a

Please sign in to comment.