From 7f1d58aa4591eba40dd6f3e6bc2869dcdc3e9adb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 14 Feb 2023 07:09:24 +0100 Subject: Inline `Handle::content` for simplicity and efficiency We can avoid downcasting `state` :^) --- native/src/widget/pick_list.rs | 53 ++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 31 deletions(-) (limited to 'native/src') diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 8189dd61..b1cdfad4 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -224,7 +224,7 @@ where self.selected.as_ref(), &self.handle, &self.style, - tree.state.downcast_ref::>(), + || tree.state.downcast_ref::>(), ) } @@ -325,32 +325,6 @@ impl Default for Handle { } } -impl Handle { - fn content>( - &self, - is_open: bool, - ) -> Option<(Font, char, Option)> { - 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 { @@ -593,7 +567,7 @@ pub fn draw<'a, T, Renderer>( selected: Option<&T>, handle: &Handle, style: &::Style, - state: &State, + state: impl FnOnce() -> &'a State, ) where Renderer: text::Renderer, Renderer::Theme: StyleSheet, @@ -619,9 +593,26 @@ pub fn draw<'a, T, Renderer>( style.background, ); - if let Some((font, code_point, size)) = - handle.content::(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 { -- cgit