diff options
-rw-r--r-- | examples/game_of_life/src/main.rs | 2 | ||||
-rw-r--r-- | examples/pick_list/src/main.rs | 2 | ||||
-rw-r--r-- | glow/Cargo.toml | 4 | ||||
-rw-r--r-- | glow/src/quad.rs | 12 | ||||
-rw-r--r-- | glow/src/triangle.rs | 4 | ||||
-rw-r--r-- | native/src/overlay/menu.rs | 74 | ||||
-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 |
11 files changed, 151 insertions, 94 deletions
diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 27d4eec1..c934467d 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -788,7 +788,7 @@ struct Controls { next_button: button::State, clear_button: button::State, speed_slider: slider::State, - preset_list: pick_list::State, + preset_list: pick_list::State<Preset>, } impl Controls { diff --git a/examples/pick_list/src/main.rs b/examples/pick_list/src/main.rs index 66ed6c6f..3917a554 100644 --- a/examples/pick_list/src/main.rs +++ b/examples/pick_list/src/main.rs @@ -10,7 +10,7 @@ pub fn main() { #[derive(Default)] struct Example { scroll: scrollable::State, - pick_list: pick_list::State, + pick_list: pick_list::State<Language>, selected_language: Language, } diff --git a/glow/Cargo.toml b/glow/Cargo.toml index baf2eb2f..f1f491b3 100644 --- a/glow/Cargo.toml +++ b/glow/Cargo.toml @@ -15,8 +15,8 @@ image = [] svg = [] [dependencies] -glow = "0.4" -glow_glyph = "0.2" +glow = "0.5" +glow_glyph = "0.3" glyph_brush = "0.7" euclid = "0.20" bytemuck = "1.2" diff --git a/glow/src/quad.rs b/glow/src/quad.rs index 3a65338a..a8fbb9e5 100644 --- a/glow/src/quad.rs +++ b/glow/src/quad.rs @@ -48,13 +48,13 @@ impl Pipeline { let matrix: [f32; 16] = Transformation::identity().into(); gl.uniform_matrix_4_f32_slice( - Some(transform_location), + Some(&transform_location), false, &matrix, ); - gl.uniform_1_f32(Some(scale_location), 1.0); - gl.uniform_1_f32(Some(screen_height_location), 0.0); + gl.uniform_1_f32(Some(&scale_location), 1.0); + gl.uniform_1_f32(Some(&screen_height_location), 0.0); gl.use_program(None); } @@ -102,7 +102,7 @@ impl Pipeline { unsafe { let matrix: [f32; 16] = transformation.into(); gl.uniform_matrix_4_f32_slice( - Some(self.transform_location), + Some(&self.transform_location), false, &matrix, ); @@ -113,7 +113,7 @@ impl Pipeline { if scale != self.current_scale { unsafe { - gl.uniform_1_f32(Some(self.scale_location), scale); + gl.uniform_1_f32(Some(&self.scale_location), scale); } self.current_scale = scale; @@ -122,7 +122,7 @@ impl Pipeline { if target_height != self.current_target_height { unsafe { gl.uniform_1_f32( - Some(self.screen_height_location), + Some(&self.screen_height_location), target_height as f32, ); } diff --git a/glow/src/triangle.rs b/glow/src/triangle.rs index eefd1c1f..9202bcb2 100644 --- a/glow/src/triangle.rs +++ b/glow/src/triangle.rs @@ -44,7 +44,7 @@ impl Pipeline { let transform: [f32; 16] = Transformation::identity().into(); gl.uniform_matrix_4_f32_slice( - Some(transform_location), + Some(&transform_location), false, &transform, ); @@ -182,7 +182,7 @@ impl Pipeline { if self.current_transform != transform { let matrix: [f32; 16] = transform.into(); gl.uniform_matrix_4_f32_slice( - Some(self.transform_location), + Some(&self.transform_location), false, &matrix, ); diff --git a/native/src/overlay/menu.rs b/native/src/overlay/menu.rs index 0d4bc63c..c2df468e 100644 --- a/native/src/overlay/menu.rs +++ b/native/src/overlay/menu.rs @@ -7,10 +7,11 @@ use crate::{ /// A list of selectable options. #[allow(missing_debug_implementations)] -pub struct Menu<'a, T, Message, Renderer: self::Renderer> { +pub struct Menu<'a, T, Renderer: self::Renderer> { state: &'a mut State, options: &'a [T], - on_selected: &'a dyn Fn(T) -> Message, + hovered_option: &'a mut Option<usize>, + last_selection: &'a mut Option<T>, width: u16, padding: u16, text_size: Option<u16>, @@ -18,10 +19,9 @@ pub struct Menu<'a, T, Message, Renderer: self::Renderer> { style: <Renderer as self::Renderer>::Style, } -impl<'a, T, Message, Renderer> Menu<'a, T, Message, Renderer> +impl<'a, T, Renderer> Menu<'a, T, Renderer> where T: ToString + Clone, - Message: 'a, Renderer: self::Renderer + 'a, { /// Creates a new [`Menu`] with the given [`State`], a list of options, and @@ -32,12 +32,14 @@ where pub fn new( state: &'a mut State, options: &'a [T], - on_selected: &'a dyn Fn(T) -> Message, + hovered_option: &'a mut Option<usize>, + last_selection: &'a mut Option<T>, ) -> Self { Menu { state, options, - on_selected, + hovered_option, + last_selection, width: 0, padding: 0, text_size: None, @@ -97,7 +99,7 @@ where /// dimensions of the [`Menu`]. /// /// [`Menu`]: struct.Menu.html - pub fn overlay( + pub fn overlay<Message: 'a>( self, position: Point, target_height: f32, @@ -115,30 +117,20 @@ where #[derive(Debug, Clone, Default)] pub struct State { scrollable: scrollable::State, - hovered_option: Option<usize>, - is_open: bool, } impl State { - /// Returns whether the [`Menu`] is currently open or not. - /// - /// [`Menu`]: struct.Menu.html - pub fn is_open(&self) -> bool { - self.is_open - } - - /// Opens the [`Menu`] with the given option hovered by default. + /// Creates a new [`State`] for a [`Menu`]. /// + /// [`State`]: struct.State.html /// [`Menu`]: struct.Menu.html - pub fn open(&mut self, hovered_option: Option<usize>) { - self.is_open = true; - self.hovered_option = hovered_option; + pub fn new() -> Self { + Self::default() } } struct Overlay<'a, Message, Renderer: self::Renderer> { container: Container<'a, Message, Renderer>, - is_open: &'a mut bool, width: u16, target_height: f32, style: <Renderer as self::Renderer>::Style, @@ -149,17 +141,15 @@ where Message: 'a, Renderer: 'a, { - pub fn new<T>( - menu: Menu<'a, T, Message, Renderer>, - target_height: f32, - ) -> Self + pub fn new<T>(menu: Menu<'a, T, Renderer>, target_height: f32) -> Self where T: Clone + ToString, { let Menu { state, options, - on_selected, + hovered_option, + last_selection, width, padding, font, @@ -170,8 +160,8 @@ where let container = Container::new(Scrollable::new(&mut state.scrollable).push(List { options, - hovered_option: &mut state.hovered_option, - on_selected, + hovered_option, + last_selection, font, text_size, padding, @@ -181,7 +171,6 @@ where Self { container, - is_open: &mut state.is_open, width: width, target_height, style: style, @@ -235,6 +224,7 @@ where (position.x as u32).hash(state); (position.y as u32).hash(state); + self.container.hash_layout(state); } fn on_event( @@ -246,9 +236,6 @@ where renderer: &Renderer, clipboard: Option<&dyn Clipboard>, ) { - let bounds = layout.bounds(); - let current_messages = messages.len(); - self.container.on_event( event.clone(), layout, @@ -257,17 +244,6 @@ where renderer, clipboard, ); - - let option_was_selected = current_messages < messages.len(); - - match event { - Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) - if !bounds.contains(cursor_position) || option_was_selected => - { - *self.is_open = false; - } - _ => {} - } } fn draw( @@ -290,10 +266,10 @@ where } } -struct List<'a, T, Message, Renderer: self::Renderer> { +struct List<'a, T, Renderer: self::Renderer> { options: &'a [T], hovered_option: &'a mut Option<usize>, - on_selected: &'a dyn Fn(T) -> Message, + last_selection: &'a mut Option<T>, padding: u16, text_size: Option<u16>, font: Renderer::Font, @@ -301,7 +277,7 @@ struct List<'a, T, Message, Renderer: self::Renderer> { } impl<'a, T, Message, Renderer: self::Renderer> Widget<Message, Renderer> - for List<'a, T, Message, Renderer> + for List<'a, T, Renderer> where T: Clone + ToString, Renderer: self::Renderer, @@ -353,7 +329,7 @@ where event: Event, layout: Layout<'_>, cursor_position: Point, - messages: &mut Vec<Message>, + _messages: &mut Vec<Message>, renderer: &Renderer, _clipboard: Option<&dyn Clipboard>, ) { @@ -364,7 +340,7 @@ where if bounds.contains(cursor_position) { if let Some(index) = *self.hovered_option { if let Some(option) = self.options.get(index) { - messages.push((self.on_selected)(option.clone())); + *self.last_selection = Some(option.clone()); } } } @@ -452,7 +428,7 @@ pub trait Renderer: } impl<'a, T, Message, Renderer> Into<Element<'a, Message, Renderer>> - for List<'a, T, Message, Renderer> + for List<'a, T, Renderer> where T: ToString + Clone, Message: 'a, 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 { |