diff options
Diffstat (limited to 'native/src/widget')
| -rw-r--r-- | native/src/widget/checkbox.rs | 14 | ||||
| -rw-r--r-- | native/src/widget/pane_grid.rs | 6 | ||||
| -rw-r--r-- | native/src/widget/pane_grid/state.rs | 22 | ||||
| -rw-r--r-- | native/src/widget/pick_list.rs | 93 | ||||
| -rw-r--r-- | native/src/widget/text_input.rs | 12 | 
5 files changed, 114 insertions, 33 deletions
diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index 44962288..99178aae 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -33,6 +33,7 @@ pub struct Checkbox<Message, Renderer: self::Renderer + text::Renderer> {      size: u16,      spacing: u16,      text_size: Option<u16>, +    font: Renderer::Font,      style: Renderer::Style,  } @@ -61,6 +62,7 @@ impl<Message, Renderer: self::Renderer + text::Renderer>              size: <Renderer as self::Renderer>::DEFAULT_SIZE,              spacing: Renderer::DEFAULT_SPACING,              text_size: None, +            font: Renderer::Font::default(),              style: Renderer::Style::default(),          }      } @@ -97,6 +99,15 @@ impl<Message, Renderer: self::Renderer + text::Renderer>          self      } +    /// Sets the [`Font`] of the text of the [`Checkbox`]. +    /// +    /// [`Checkbox`]: struct.Checkbox.html +    /// [`Font`]: ../../struct.Font.html +    pub fn font(mut self, font: Renderer::Font) -> Self { +        self.font = font; +        self +    } +      /// Sets the style of the [`Checkbox`].      ///      /// [`Checkbox`]: struct.Checkbox.html @@ -135,6 +146,7 @@ where              )              .push(                  Text::new(&self.label) +                    .font(self.font)                      .width(self.width)                      .size(self.text_size.unwrap_or(renderer.default_size())),              ) @@ -182,7 +194,7 @@ where              label_layout.bounds(),              &self.label,              self.text_size.unwrap_or(renderer.default_size()), -            Default::default(), +            self.font,              None,              HorizontalAlignment::Left,              VerticalAlignment::Center, diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 0b237b9e..5180fd3b 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -496,7 +496,10 @@ where                              }                          }                      } else { -                        self.state.unfocus(); +                        // TODO: Encode cursor availability in the type system +                        if cursor_position.x > 0.0 && cursor_position.y > 0.0 { +                            self.state.unfocus(); +                        }                      }                  }                  mouse::Event::ButtonReleased(mouse::Button::Left) => { @@ -625,6 +628,7 @@ where      fn hash_layout(&self, state: &mut Hasher) {          use std::hash::Hash; +          struct Marker;          std::any::TypeId::of::<Marker>().hash(state); diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs index e1585968..fb59c846 100644 --- a/native/src/widget/pane_grid/state.rs +++ b/native/src/widget/pane_grid/state.rs @@ -122,6 +122,14 @@ impl<T> State<T> {          &self.internal.layout      } +    /// Returns the focused [`Pane`] of the [`State`], if there is one. +    /// +    /// [`Pane`]: struct.Pane.html +    /// [`State`]: struct.State.html +    pub fn focused(&self) -> Option<Pane> { +        self.internal.focused_pane() +    } +      /// Returns the active [`Pane`] of the [`State`], if there is one.      ///      /// A [`Pane`] is active if it is focused and is __not__ being dragged. @@ -327,6 +335,7 @@ pub enum Action {      Dragging {          pane: Pane,          origin: Point, +        focus: Option<Pane>,      },      Resizing {          split: Split, @@ -351,6 +360,14 @@ impl Internal {          self.action      } +    pub fn focused_pane(&self) -> Option<Pane> { +        match self.action { +            Action::Idle { focus } => focus, +            Action::Dragging { focus, .. } => focus, +            Action::Resizing { focus, .. } => focus, +        } +    } +      pub fn active_pane(&self) -> Option<Pane> {          match self.action {              Action::Idle { focus } => focus, @@ -360,7 +377,7 @@ impl Internal {      pub fn picked_pane(&self) -> Option<(Pane, Point)> {          match self.action { -            Action::Dragging { pane, origin } => Some((pane, origin)), +            Action::Dragging { pane, origin, .. } => Some((pane, origin)),              _ => None,          }      } @@ -393,9 +410,12 @@ impl Internal {      }      pub fn pick_pane(&mut self, pane: &Pane, origin: Point) { +        let focus = self.focused_pane(); +          self.action = Action::Dragging {              pane: *pane,              origin, +            focus,          };      } diff --git a/native/src/widget/pick_list.rs b/native/src/widget/pick_list.rs index 9f62e550..04478225 100644 --- a/native/src/widget/pick_list.rs +++ b/native/src/widget/pick_list.rs @@ -14,6 +14,9 @@ where      [T]: ToOwned<Owned = Vec<T>>,  {      menu: &'a mut menu::State, +    is_open: &'a mut bool, +    hovered_option: &'a mut Option<usize>, +    last_selection: &'a mut Option<T>,      on_selected: Box<dyn Fn(T) -> Message>,      options: Cow<'a, [T]>,      selected: Option<T>, @@ -22,15 +25,28 @@ where      text_size: Option<u16>,      font: Renderer::Font,      style: <Renderer as self::Renderer>::Style, -    is_open: bool,  }  /// The local state of a [`PickList`].  ///  /// [`PickList`]: struct.PickList.html -#[derive(Debug, Clone, Default)] -pub struct State { +#[derive(Debug, Clone)] +pub struct State<T> {      menu: menu::State, +    is_open: bool, +    hovered_option: Option<usize>, +    last_selection: Option<T>, +} + +impl<T> Default for State<T> { +    fn default() -> Self { +        Self { +            menu: menu::State::default(), +            is_open: bool::default(), +            hovered_option: Option::default(), +            last_selection: Option::default(), +        } +    }  }  impl<'a, T: 'a, Message, Renderer: self::Renderer> @@ -46,15 +62,23 @@ where      /// [`PickList`]: struct.PickList.html      /// [`State`]: struct.State.html      pub fn new( -        state: &'a mut State, +        state: &'a mut State<T>,          options: impl Into<Cow<'a, [T]>>,          selected: Option<T>,          on_selected: impl Fn(T) -> Message + 'static,      ) -> Self { -        let is_open = state.menu.is_open(); +        let State { +            menu, +            is_open, +            hovered_option, +            last_selection, +        } = state;          Self { -            menu: &mut state.menu, +            menu, +            is_open, +            hovered_option, +            last_selection,              on_selected: Box::new(on_selected),              options: options.into(),              selected, @@ -63,7 +87,6 @@ where              padding: Renderer::DEFAULT_PADDING,              font: Default::default(),              style: Default::default(), -            is_open,          }      } @@ -197,27 +220,33 @@ where          event: Event,          layout: Layout<'_>,          cursor_position: Point, -        _messages: &mut Vec<Message>, +        messages: &mut Vec<Message>,          _renderer: &Renderer,          _clipboard: Option<&dyn Clipboard>,      ) { -        if !self.is_open { -            match event { -                Event::Mouse(mouse::Event::ButtonPressed( -                    mouse::Button::Left, -                )) => { -                    if layout.bounds().contains(cursor_position) { -                        let selected = self.selected.as_ref(); - -                        self.menu.open( -                            self.options -                                .iter() -                                .position(|option| Some(option) == selected), -                        ); -                    } +        match event { +            Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => { +                if *self.is_open { +                    // TODO: Encode cursor availability in the type system +                    *self.is_open = +                        cursor_position.x < 0.0 || cursor_position.y < 0.0; +                } else if layout.bounds().contains(cursor_position) { +                    let selected = self.selected.as_ref(); + +                    *self.is_open = true; +                    *self.hovered_option = self +                        .options +                        .iter() +                        .position(|option| Some(option) == selected); +                } + +                if let Some(last_selection) = self.last_selection.take() { +                    messages.push((self.on_selected)(last_selection)); + +                    *self.is_open = false;                  } -                _ => {}              } +            _ => {}          }      } @@ -244,15 +273,19 @@ where          &mut self,          layout: Layout<'_>,      ) -> Option<overlay::Element<'_, Message, Renderer>> { -        if self.menu.is_open() { +        if *self.is_open {              let bounds = layout.bounds(); -            let mut menu = -                Menu::new(&mut self.menu, &self.options, &self.on_selected) -                    .width(bounds.width.round() as u16) -                    .padding(self.padding) -                    .font(self.font) -                    .style(Renderer::menu_style(&self.style)); +            let mut menu = Menu::new( +                &mut self.menu, +                &self.options, +                &mut self.hovered_option, +                &mut self.last_selection, +            ) +            .width(bounds.width.round() as u16) +            .padding(self.padding) +            .font(self.font) +            .style(Renderer::menu_style(&self.style));              if let Some(text_size) = self.text_size {                  menu = menu.text_size(text_size); diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 230fe7dc..9e15f4be 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -163,6 +163,13 @@ impl<'a, Message, Renderer: self::Renderer> TextInput<'a, Message, Renderer> {          self.style = style.into();          self      } + +    /// Returns the current [`State`] of the [`TextInput`]. +    /// +    /// [`TextInput`]: struct.TextInput.html +    pub fn state(&self) -> &State { +        self.state +    }  }  impl<'a, Message, Renderer> Widget<Message, Renderer> @@ -461,6 +468,11 @@ where                          self.state.cursor.select_all(&self.value);                      }                  } +                keyboard::KeyCode::Escape => { +                    self.state.is_focused = false; +                    self.state.is_dragging = false; +                    self.state.is_pasting = None; +                }                  _ => {}              },              Event::Keyboard(keyboard::Event::KeyReleased {  | 
