select.rs

  1use crate::{
  2    elements::*,
  3    platform::{CursorStyle, MouseButton},
  4    AppContext, Entity, View, ViewContext, WeakViewHandle,
  5};
  6
  7pub struct Select {
  8    handle: WeakViewHandle<Self>,
  9    render_item: Box<dyn Fn(usize, ItemType, bool, &mut ViewContext<Select>) -> AnyElement<Self>>,
 10    selected_item_ix: usize,
 11    item_count: usize,
 12    is_open: bool,
 13    list_state: UniformListState,
 14    build_style: Option<Box<dyn FnMut(&mut AppContext) -> SelectStyle>>,
 15}
 16
 17#[derive(Clone, Default)]
 18pub struct SelectStyle {
 19    pub header: ContainerStyle,
 20    pub menu: ContainerStyle,
 21}
 22
 23pub enum ItemType {
 24    Header,
 25    Selected,
 26    Unselected,
 27}
 28
 29pub enum Event {}
 30
 31impl Select {
 32    pub fn new<
 33        F: 'static + Fn(usize, ItemType, bool, &mut ViewContext<Self>) -> AnyElement<Self>,
 34    >(
 35        item_count: usize,
 36        cx: &mut ViewContext<Self>,
 37        render_item: F,
 38    ) -> Self {
 39        Self {
 40            handle: cx.weak_handle(),
 41            render_item: Box::new(render_item),
 42            selected_item_ix: 0,
 43            item_count,
 44            is_open: false,
 45            list_state: UniformListState::default(),
 46            build_style: Default::default(),
 47        }
 48    }
 49
 50    pub fn with_style(mut self, f: impl 'static + FnMut(&mut AppContext) -> SelectStyle) -> Self {
 51        self.build_style = Some(Box::new(f));
 52        self
 53    }
 54
 55    pub fn set_item_count(&mut self, count: usize, cx: &mut ViewContext<Self>) {
 56        self.item_count = count;
 57        cx.notify();
 58    }
 59
 60    fn toggle(&mut self, cx: &mut ViewContext<Self>) {
 61        self.is_open = !self.is_open;
 62        cx.notify();
 63    }
 64
 65    pub fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Self>) {
 66        self.selected_item_ix = ix;
 67        self.is_open = false;
 68        cx.notify();
 69    }
 70
 71    pub fn selected_index(&self) -> usize {
 72        self.selected_item_ix
 73    }
 74}
 75
 76impl Entity for Select {
 77    type Event = Event;
 78}
 79
 80impl View for Select {
 81    fn ui_name() -> &'static str {
 82        "Select"
 83    }
 84
 85    fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
 86        if self.item_count == 0 {
 87            return Empty::new().into_any();
 88        }
 89
 90        enum Header {}
 91        enum Item {}
 92
 93        let style = if let Some(build_style) = self.build_style.as_mut() {
 94            (build_style)(cx)
 95        } else {
 96            Default::default()
 97        };
 98        let mut result = Flex::column().with_child(
 99            MouseEventHandler::new::<Header, _>(self.handle.id(), cx, |mouse_state, cx| {
100                (self.render_item)(
101                    self.selected_item_ix,
102                    ItemType::Header,
103                    mouse_state.hovered(),
104                    cx,
105                )
106                .contained()
107                .with_style(style.header)
108            })
109            .with_cursor_style(CursorStyle::PointingHand)
110            .on_click(MouseButton::Left, move |_, this, cx| {
111                this.toggle(cx);
112            }),
113        );
114        if self.is_open {
115            result.add_child(Overlay::new(
116                UniformList::new(
117                    self.list_state.clone(),
118                    self.item_count,
119                    cx,
120                    move |this, mut range, items, cx| {
121                        let selected_item_ix = this.selected_item_ix;
122                        range.end = range.end.min(this.item_count);
123                        items.extend(range.map(|ix| {
124                            MouseEventHandler::new::<Item, _>(ix, cx, |mouse_state, cx| {
125                                (this.render_item)(
126                                    ix,
127                                    if ix == selected_item_ix {
128                                        ItemType::Selected
129                                    } else {
130                                        ItemType::Unselected
131                                    },
132                                    mouse_state.hovered(),
133                                    cx,
134                                )
135                            })
136                            .with_cursor_style(CursorStyle::PointingHand)
137                            .on_click(MouseButton::Left, move |_, this, cx| {
138                                this.set_selected_index(ix, cx);
139                            })
140                            .into_any()
141                        }))
142                    },
143                )
144                .constrained()
145                .with_max_height(200.)
146                .contained()
147                .with_style(style.menu),
148            ));
149        }
150        result.into_any()
151    }
152}