From 6a2c73d0e0ea710f2b6acfaad2b6a6551f807352 Mon Sep 17 00:00:00 2001 From: bansheerubber Date: Fri, 5 Jun 2020 08:58:34 -0700 Subject: sketch of move_cursor_to commands --- native/src/widget/text_input.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'native') diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index e3a5355b..9e7d504a 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -683,6 +683,22 @@ impl State { pub fn cursor(&self) -> Cursor { self.cursor } + + /// Moves the [`Cursor`] of the [`TextInput`] to the front of the input text. + /// + /// [`Cursor`]: struct.Cursor.html + /// [`TextInput`]: struct.TextInput.html + pub fn move_cursor_to_front(&mut self) { + self.cursor.move_to(0); + } + + /// Moves the [`Cursor`] of the [`TextInput`] to the end of the input text. + /// + /// [`Cursor`]: struct.Cursor.html + /// [`TextInput`]: struct.TextInput.html + pub fn move_cursor_to_end(&mut self) { + self.cursor.move_to(5000); + } } // TODO: Reduce allocations -- cgit From 0d119aa73197d545f12d95e5a2549a68d3067de9 Mon Sep 17 00:00:00 2001 From: bansheerubber Date: Fri, 5 Jun 2020 09:08:36 -0700 Subject: added value to move_cursor_to_end --- native/src/widget/text_input.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'native') diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 9e7d504a..0dbcc3d6 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -696,8 +696,8 @@ impl State { /// /// [`Cursor`]: struct.Cursor.html /// [`TextInput`]: struct.TextInput.html - pub fn move_cursor_to_end(&mut self) { - self.cursor.move_to(5000); + pub fn move_cursor_to_end(&mut self, value: &String) { + self.cursor.move_to(value.len()); } } -- cgit From 98cf9c455a201fec3069f415ce2ddf5e88def242 Mon Sep 17 00:00:00 2001 From: bansheerubber Date: Fri, 5 Jun 2020 09:19:46 -0700 Subject: added move_cursor_to --- native/src/widget/text_input.rs | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'native') diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 0dbcc3d6..c821247f 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -699,6 +699,14 @@ impl State { pub fn move_cursor_to_end(&mut self, value: &String) { self.cursor.move_to(value.len()); } + + /// Moves the [`Cursor`] of the [`TextInput`] to an arbitrary location. + /// + /// [`Cursor`]: struct.Cursor.html + /// [`TextInput`]: struct.TextInput.html + pub fn move_cursor_to(&mut self, position: usize) { + self.cursor.move_to(position); + } } // TODO: Reduce allocations -- cgit From 19c07da86f6481244577f30262d250df25f43c39 Mon Sep 17 00:00:00 2001 From: bansheerubber Date: Fri, 5 Jun 2020 09:57:18 -0700 Subject: fixed formatting --- native/src/widget/text_input.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'native') diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index c821247f..9d6afffb 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -685,7 +685,7 @@ impl State { } /// Moves the [`Cursor`] of the [`TextInput`] to the front of the input text. - /// + /// /// [`Cursor`]: struct.Cursor.html /// [`TextInput`]: struct.TextInput.html pub fn move_cursor_to_front(&mut self) { @@ -693,7 +693,7 @@ impl State { } /// Moves the [`Cursor`] of the [`TextInput`] to the end of the input text. - /// + /// /// [`Cursor`]: struct.Cursor.html /// [`TextInput`]: struct.TextInput.html pub fn move_cursor_to_end(&mut self, value: &String) { @@ -701,7 +701,7 @@ impl State { } /// Moves the [`Cursor`] of the [`TextInput`] to an arbitrary location. - /// + /// /// [`Cursor`]: struct.Cursor.html /// [`TextInput`]: struct.TextInput.html pub fn move_cursor_to(&mut self, position: usize) { -- cgit From 5260b3072ac0426489a16ab435dc9d533c5ed081 Mon Sep 17 00:00:00 2001 From: bansheerubber Date: Mon, 8 Jun 2020 10:00:25 -0700 Subject: implemented hecrj's suggestion --- native/src/widget/text_input.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'native') diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 9d6afffb..24085606 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -696,8 +696,8 @@ impl State { /// /// [`Cursor`]: struct.Cursor.html /// [`TextInput`]: struct.TextInput.html - pub fn move_cursor_to_end(&mut self, value: &String) { - self.cursor.move_to(value.len()); + pub fn move_cursor_to_end(&mut self) { + self.cursor.move_to(usize::MAX); } /// Moves the [`Cursor`] of the [`TextInput`] to an arbitrary location. -- cgit From 8b93c9cb6a623d8f936e7e7135a2598e3ce4e4ef Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 8 Jun 2020 19:41:33 +0200 Subject: Clarify `leeway` meaning in `PaneGrid` --- native/src/widget/pane_grid.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'native') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index f8788932..2d21a968 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -211,6 +211,10 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> { /// The `leeway` describes the amount of space around a split that can be /// used to grab it. /// + /// The grabbable area of a split will have a length of `spacing + leeway`, + /// properly centered. In other words, a length of + /// `(spacing + leeway) / 2.0` on either side of the split line. + /// /// [`PaneGrid`]: struct.PaneGrid.html pub fn on_resize(mut self, leeway: u16, f: F) -> Self where -- cgit From 49dbf2c14658cb5f2aafdbb75d826d8ba8fedc31 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 9 Jun 2020 15:45:57 +0200 Subject: Request a redraw only on relevant events --- native/src/program/state.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'native') diff --git a/native/src/program/state.rs b/native/src/program/state.rs index 8716d8b9..bb428198 100644 --- a/native/src/program/state.rs +++ b/native/src/program/state.rs @@ -88,6 +88,13 @@ where self.queued_messages.push(message); } + /// Returns whether the event queue of the [`State`] is empty or not. + /// + /// [`State`]: struct.State.html + pub fn is_queue_empty(&self) -> bool { + self.queued_events.is_empty() && self.queued_messages.is_empty() + } + /// Processes all the queued events and messages, rebuilding and redrawing /// the widgets of the linked [`Program`] if necessary. /// @@ -102,10 +109,6 @@ where renderer: &mut P::Renderer, debug: &mut Debug, ) -> Option> { - if self.queued_events.is_empty() && self.queued_messages.is_empty() { - return None; - } - let mut user_interface = build_user_interface( &mut self.program, self.cache.take().unwrap(), -- cgit From c3643eaf6d90dc8b60c0b762200bf1f8097cdfe9 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Tue, 2 Jun 2020 20:34:13 +0200 Subject: Add `step` member to slider widgets Both the native and the web slider now have a member `step` to control the least possible change of the slider's value. It defaults to 1.0 for all sliders and can be adjusted with the step method. --- native/src/widget/slider.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'native') diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 753a49fe..8670628c 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -16,6 +16,8 @@ use std::{hash::Hash, ops::RangeInclusive}; /// /// A [`Slider`] will try to fill the horizontal space of its container. /// +/// The step size defaults to 1.0. +/// /// [`Slider`]: struct.Slider.html /// /// # Example @@ -38,6 +40,7 @@ use std::{hash::Hash, ops::RangeInclusive}; pub struct Slider<'a, Message, Renderer: self::Renderer> { state: &'a mut State, range: RangeInclusive, + step: f32, value: f32, on_change: Box Message>, on_release: Option, @@ -71,6 +74,7 @@ impl<'a, Message, Renderer: self::Renderer> Slider<'a, Message, Renderer> { state, value: value.max(*range.start()).min(*range.end()), range, + step: 1.0, on_change: Box::new(on_change), on_release: None, width: Length::Fill, @@ -106,6 +110,14 @@ impl<'a, Message, Renderer: self::Renderer> Slider<'a, Message, Renderer> { self.style = style.into(); self } + + /// Sets the step size of the [`Slider`]. + /// + /// [`Slider`]: struct.Slider.html + pub fn step(mut self, step: f32) -> Self { + self.step = step; + self + } } /// The local state of a [`Slider`]. @@ -164,16 +176,16 @@ where ) { let mut change = || { let bounds = layout.bounds(); - if cursor_position.x <= bounds.x { messages.push((self.on_change)(*self.range.start())); } else if cursor_position.x >= bounds.x + bounds.width { messages.push((self.on_change)(*self.range.end())); } else { let percent = (cursor_position.x - bounds.x) / bounds.width; - let value = (self.range.end() - self.range.start()) * percent - + self.range.start(); - + let steps = (percent * (self.range.end() - self.range.start()) + / self.step) + .round(); + let value = steps * self.step + self.range.start(); messages.push((self.on_change)(value)); } }; -- cgit From 0b819de3e22837dc456d028f51bf492891d6c3a5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 13 Jun 2020 14:17:41 +0200 Subject: Make `Slider` value type generic --- native/Cargo.toml | 1 + native/src/widget/slider.rs | 78 ++++++++++++++++++++++++++++++--------------- 2 files changed, 54 insertions(+), 25 deletions(-) (limited to 'native') diff --git a/native/Cargo.toml b/native/Cargo.toml index 75b4a56b..13052a93 100644 --- a/native/Cargo.toml +++ b/native/Cargo.toml @@ -13,6 +13,7 @@ debug = [] [dependencies] twox-hash = "1.5" unicode-segmentation = "1.6" +num-traits = "0.2" [dependencies.iced_core] version = "0.2" diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 8670628c..94f743e8 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -24,7 +24,7 @@ use std::{hash::Hash, ops::RangeInclusive}; /// ``` /// # use iced_native::{slider, renderer::Null}; /// # -/// # pub type Slider<'a, Message> = iced_native::Slider<'a, Message, Null>; +/// # pub type Slider<'a, T, Message> = iced_native::Slider<'a, T, Message, Null>; /// pub enum Message { /// SliderChanged(f32), /// } @@ -37,18 +37,22 @@ use std::{hash::Hash, ops::RangeInclusive}; /// /// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true) #[allow(missing_debug_implementations)] -pub struct Slider<'a, Message, Renderer: self::Renderer> { +pub struct Slider<'a, T, Message, Renderer: self::Renderer> { state: &'a mut State, - range: RangeInclusive, - step: f32, - value: f32, - on_change: Box Message>, + range: RangeInclusive, + step: T, + value: T, + on_change: Box Message>, on_release: Option, width: Length, style: Renderer::Style, } -impl<'a, Message, Renderer: self::Renderer> Slider<'a, Message, Renderer> { +impl<'a, T, Message, Renderer> Slider<'a, T, Message, Renderer> +where + T: Copy + From + std::cmp::PartialOrd, + Renderer: self::Renderer, +{ /// Creates a new [`Slider`]. /// /// It expects: @@ -63,18 +67,30 @@ impl<'a, Message, Renderer: self::Renderer> Slider<'a, Message, Renderer> { /// [`State`]: struct.State.html pub fn new( state: &'a mut State, - range: RangeInclusive, - value: f32, + range: RangeInclusive, + value: T, on_change: F, ) -> Self where - F: 'static + Fn(f32) -> Message, + F: 'static + Fn(T) -> Message, { + let value = if value >= *range.start() { + value + } else { + *range.start() + }; + + let value = if value <= *range.end() { + value + } else { + *range.end() + }; + Slider { state, - value: value.max(*range.start()).min(*range.end()), + value, range, - step: 1.0, + step: T::from(1), on_change: Box::new(on_change), on_release: None, width: Length::Fill, @@ -114,7 +130,7 @@ impl<'a, Message, Renderer: self::Renderer> Slider<'a, Message, Renderer> { /// Sets the step size of the [`Slider`]. /// /// [`Slider`]: struct.Slider.html - pub fn step(mut self, step: f32) -> Self { + pub fn step(mut self, step: T) -> Self { self.step = step; self } @@ -137,9 +153,10 @@ impl State { } } -impl<'a, Message, Renderer> Widget - for Slider<'a, Message, Renderer> +impl<'a, T, Message, Renderer> Widget + for Slider<'a, T, Message, Renderer> where + T: Copy + Into + num_traits::FromPrimitive, Renderer: self::Renderer, Message: Clone, { @@ -181,12 +198,19 @@ where } else if cursor_position.x >= bounds.x + bounds.width { messages.push((self.on_change)(*self.range.end())); } else { - let percent = (cursor_position.x - bounds.x) / bounds.width; - let steps = (percent * (self.range.end() - self.range.start()) - / self.step) - .round(); - let value = steps * self.step + self.range.start(); - messages.push((self.on_change)(value)); + let step: f64 = self.step.into(); + let start: f64 = (*self.range.start()).into(); + let end: f64 = (*self.range.end()).into(); + + let percent = f64::from(cursor_position.x - bounds.x) + / f64::from(bounds.width); + + let steps = (percent * (end - start) / step).round(); + let value = steps * step + start; + + if let Some(value) = T::from_f64(value) { + messages.push((self.on_change)(value)); + } } }; @@ -224,11 +248,14 @@ where layout: Layout<'_>, cursor_position: Point, ) -> Renderer::Output { + let start = *self.range.start(); + let end = *self.range.end(); + renderer.draw( layout.bounds(), cursor_position, - self.range.clone(), - self.value, + start.into() as f32..=end.into() as f32, + self.value.into() as f32, self.state.is_dragging, &self.style, ) @@ -281,14 +308,15 @@ pub trait Renderer: crate::Renderer { ) -> Self::Output; } -impl<'a, Message, Renderer> From> +impl<'a, T, Message, Renderer> From> for Element<'a, Message, Renderer> where + T: 'a + Copy + Into + num_traits::FromPrimitive, Renderer: 'a + self::Renderer, Message: 'a + Clone, { fn from( - slider: Slider<'a, Message, Renderer>, + slider: Slider<'a, T, Message, Renderer>, ) -> Element<'a, Message, Renderer> { Element::new(slider) } -- cgit From c71d83fe0eb2b4cefe27690a922771b8e9e05435 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 13 Jun 2020 14:34:23 +0200 Subject: Remove unnecessary type annotations in `Slider` --- native/src/widget/slider.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'native') diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 94f743e8..0b7f424c 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -198,9 +198,9 @@ where } else if cursor_position.x >= bounds.x + bounds.width { messages.push((self.on_change)(*self.range.end())); } else { - let step: f64 = self.step.into(); - let start: f64 = (*self.range.start()).into(); - let end: f64 = (*self.range.end()).into(); + let step = self.step.into(); + let start = (*self.range.start()).into(); + let end = (*self.range.end()).into(); let percent = f64::from(cursor_position.x - bounds.x) / f64::from(bounds.width); -- cgit From 7bc7b603219b6c810e480ef5c7f8c4f7a9bf7cb6 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 13 Jun 2020 14:36:10 +0200 Subject: Mention generic range in `Slider` documentation --- native/src/widget/slider.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'native') diff --git a/native/src/widget/slider.rs b/native/src/widget/slider.rs index 0b7f424c..70f2b6ac 100644 --- a/native/src/widget/slider.rs +++ b/native/src/widget/slider.rs @@ -16,7 +16,8 @@ use std::{hash::Hash, ops::RangeInclusive}; /// /// A [`Slider`] will try to fill the horizontal space of its container. /// -/// The step size defaults to 1.0. +/// The [`Slider`] range of numeric values is generic and its step size defaults +/// to 1 unit. /// /// [`Slider`]: struct.Slider.html /// -- cgit From b3c192a2e478e9f2a101aecb417e316ed6900a80 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 19 Jun 2020 00:08:28 +0200 Subject: Make default text size configurable in `Settings` --- native/src/renderer/null.rs | 4 +++- native/src/widget/checkbox.rs | 10 +++++----- native/src/widget/radio.rs | 10 +++++----- native/src/widget/text.rs | 8 ++++---- 4 files changed, 17 insertions(+), 15 deletions(-) (limited to 'native') diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index b8695b9c..b8b0b996 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -49,7 +49,9 @@ impl row::Renderer for Null { impl text::Renderer for Null { type Font = Font; - const DEFAULT_SIZE: u16 = 20; + fn default_size(&self) -> u16 { + 20 + } fn measure( &self, diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs index 5fb13290..44962288 100644 --- a/native/src/widget/checkbox.rs +++ b/native/src/widget/checkbox.rs @@ -32,7 +32,7 @@ pub struct Checkbox { width: Length, size: u16, spacing: u16, - text_size: u16, + text_size: Option, style: Renderer::Style, } @@ -60,7 +60,7 @@ impl width: Length::Shrink, size: ::DEFAULT_SIZE, spacing: Renderer::DEFAULT_SPACING, - text_size: ::DEFAULT_SIZE, + text_size: None, style: Renderer::Style::default(), } } @@ -93,7 +93,7 @@ impl /// /// [`Checkbox`]: struct.Checkbox.html pub fn text_size(mut self, text_size: u16) -> Self { - self.text_size = text_size; + self.text_size = Some(text_size); self } @@ -136,7 +136,7 @@ where .push( Text::new(&self.label) .width(self.width) - .size(self.text_size), + .size(self.text_size.unwrap_or(renderer.default_size())), ) .layout(renderer, limits) } @@ -181,7 +181,7 @@ where defaults, label_layout.bounds(), &self.label, - self.text_size, + self.text_size.unwrap_or(renderer.default_size()), Default::default(), None, HorizontalAlignment::Left, diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs index d07a9012..5b8d00e9 100644 --- a/native/src/widget/radio.rs +++ b/native/src/widget/radio.rs @@ -41,7 +41,7 @@ pub struct Radio { width: Length, size: u16, spacing: u16, - text_size: u16, + text_size: Option, style: Renderer::Style, } @@ -75,7 +75,7 @@ impl width: Length::Shrink, size: ::DEFAULT_SIZE, spacing: Renderer::DEFAULT_SPACING, //15 - text_size: ::DEFAULT_SIZE, + text_size: None, style: Renderer::Style::default(), } } @@ -108,7 +108,7 @@ impl /// /// [`Radio`]: struct.Radio.html pub fn text_size(mut self, text_size: u16) -> Self { - self.text_size = text_size; + self.text_size = Some(text_size); self } @@ -151,7 +151,7 @@ where .push( Text::new(&self.label) .width(self.width) - .size(self.text_size), + .size(self.text_size.unwrap_or(renderer.default_size())), ) .layout(renderer, limits) } @@ -194,7 +194,7 @@ where defaults, label_layout.bounds(), &self.label, - self.text_size, + self.text_size.unwrap_or(renderer.default_size()), Default::default(), None, HorizontalAlignment::Left, diff --git a/native/src/widget/text.rs b/native/src/widget/text.rs index 0b05b67d..48a69e34 100644 --- a/native/src/widget/text.rs +++ b/native/src/widget/text.rs @@ -131,7 +131,7 @@ where ) -> layout::Node { let limits = limits.width(self.width).height(self.height); - let size = self.size.unwrap_or(Renderer::DEFAULT_SIZE); + let size = self.size.unwrap_or(renderer.default_size()); let bounds = limits.max(); @@ -154,7 +154,7 @@ where defaults, layout.bounds(), &self.content, - self.size.unwrap_or(Renderer::DEFAULT_SIZE), + self.size.unwrap_or(renderer.default_size()), self.font, self.color, self.horizontal_alignment, @@ -187,10 +187,10 @@ pub trait Renderer: crate::Renderer { /// [`Text`]: struct.Text.html type Font: Default + Copy; - /// The default size of [`Text`]. + /// Returns the default size of [`Text`]. /// /// [`Text`]: struct.Text.html - const DEFAULT_SIZE: u16; + fn default_size(&self) -> u16; /// Measures the [`Text`] in the given bounds and returns the minimum /// boundaries that can fit the contents. -- cgit From f30a666dc81fdc85d225dc83f1a33e32d5dccbd2 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 23 Jun 2020 06:44:34 +0200 Subject: Decouple `cursor_position` from `Cache` Instead, we ask explicitly for it in the different `update` and `draw` methods. This way, the runtime can derive the logical position of the cursor from the source of truth. --- native/src/program/state.rs | 13 ++++++++----- native/src/user_interface.rs | 46 ++++++++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 24 deletions(-) (limited to 'native') diff --git a/native/src/program/state.rs b/native/src/program/state.rs index bb428198..fdc42e8b 100644 --- a/native/src/program/state.rs +++ b/native/src/program/state.rs @@ -1,5 +1,5 @@ use crate::{ - Cache, Clipboard, Command, Debug, Event, Program, Renderer, Size, + Cache, Clipboard, Command, Debug, Event, Point, Program, Renderer, Size, UserInterface, }; @@ -31,6 +31,7 @@ where pub fn new( mut program: P, bounds: Size, + cursor_position: Point, renderer: &mut P::Renderer, debug: &mut Debug, ) -> Self { @@ -43,7 +44,7 @@ where ); debug.draw_started(); - let primitive = user_interface.draw(renderer); + let primitive = user_interface.draw(renderer, cursor_position); debug.draw_finished(); let cache = Some(user_interface.into_cache()); @@ -104,8 +105,9 @@ where /// [`Program`]: trait.Program.html pub fn update( &mut self, - clipboard: Option<&dyn Clipboard>, bounds: Size, + cursor_position: Point, + clipboard: Option<&dyn Clipboard>, renderer: &mut P::Renderer, debug: &mut Debug, ) -> Option> { @@ -120,6 +122,7 @@ where debug.event_processing_started(); let mut messages = user_interface.update( self.queued_events.drain(..), + cursor_position, clipboard, renderer, ); @@ -128,7 +131,7 @@ where if messages.is_empty() { debug.draw_started(); - self.primitive = user_interface.draw(renderer); + self.primitive = user_interface.draw(renderer, cursor_position); debug.draw_finished(); self.cache = Some(user_interface.into_cache()); @@ -159,7 +162,7 @@ where ); debug.draw_started(); - self.primitive = user_interface.draw(renderer); + self.primitive = user_interface.draw(renderer, cursor_position); debug.draw_finished(); self.cache = Some(user_interface.into_cache()); diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index e963b601..b9646043 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -1,4 +1,4 @@ -use crate::{layout, mouse, Clipboard, Element, Event, Layout, Point, Size}; +use crate::{layout, Clipboard, Element, Event, Layout, Point, Size}; use std::hash::Hasher; @@ -23,7 +23,6 @@ pub struct UserInterface<'a, Message, Renderer> { root: Element<'a, Message, Renderer>, layout: layout::Node, bounds: Size, - cursor_position: Point, } impl<'a, Message, Renderer> UserInterface<'a, Message, Renderer> @@ -115,7 +114,6 @@ where root, layout, bounds, - cursor_position: cache.cursor_position, } } @@ -132,7 +130,7 @@ where /// completing [the previous example](#example): /// /// ```no_run - /// use iced_native::{UserInterface, Cache, Size}; + /// use iced_native::{UserInterface, Cache, Size, Point}; /// use iced_wgpu::Renderer; /// /// # mod iced_wgpu { @@ -154,6 +152,7 @@ where /// let mut cache = Cache::new(); /// let mut renderer = Renderer::new(); /// let mut window_size = Size::new(1024.0, 768.0); + /// let mut cursor_position = Point::default(); /// /// // Initialize our event storage /// let mut events = Vec::new(); @@ -169,7 +168,12 @@ where /// ); /// /// // Update the user interface - /// let messages = user_interface.update(events.drain(..), None, &renderer); + /// let messages = user_interface.update( + /// events.drain(..), + /// cursor_position, + /// None, + /// &renderer, + /// ); /// /// cache = user_interface.into_cache(); /// @@ -182,20 +186,17 @@ where pub fn update( &mut self, events: impl IntoIterator, + cursor_position: Point, clipboard: Option<&dyn Clipboard>, renderer: &Renderer, ) -> Vec { let mut messages = Vec::new(); for event in events { - if let Event::Mouse(mouse::Event::CursorMoved { x, y }) = event { - self.cursor_position = Point::new(x, y); - } - self.root.widget.on_event( event, Layout::new(&self.layout), - self.cursor_position, + cursor_position, &mut messages, renderer, clipboard, @@ -219,7 +220,7 @@ where /// [completing the last example](#example-1): /// /// ```no_run - /// use iced_native::{UserInterface, Cache, Size}; + /// use iced_native::{UserInterface, Cache, Size, Point}; /// use iced_wgpu::Renderer; /// /// # mod iced_wgpu { @@ -241,6 +242,7 @@ where /// let mut cache = Cache::new(); /// let mut renderer = Renderer::new(); /// let mut window_size = Size::new(1024.0, 768.0); + /// let mut cursor_position = Point::default(); /// let mut events = Vec::new(); /// /// loop { @@ -253,10 +255,15 @@ where /// &mut renderer, /// ); /// - /// let messages = user_interface.update(events.drain(..), None, &renderer); + /// let messages = user_interface.update( + /// events.drain(..), + /// cursor_position, + /// None, + /// &renderer, + /// ); /// /// // Draw the user interface - /// let mouse_cursor = user_interface.draw(&mut renderer); + /// let mouse_cursor = user_interface.draw(&mut renderer, cursor_position); /// /// cache = user_interface.into_cache(); /// @@ -268,12 +275,16 @@ where /// // Flush rendering operations... /// } /// ``` - pub fn draw(&self, renderer: &mut Renderer) -> Renderer::Output { + pub fn draw( + &self, + renderer: &mut Renderer, + cursor_position: Point, + ) -> Renderer::Output { self.root.widget.draw( renderer, &Renderer::Defaults::default(), Layout::new(&self.layout), - self.cursor_position, + cursor_position, ) } @@ -287,7 +298,6 @@ where hash: self.hash, layout: self.layout, bounds: self.bounds, - cursor_position: self.cursor_position, } } } @@ -300,7 +310,6 @@ pub struct Cache { hash: u64, layout: layout::Node, bounds: Size, - cursor_position: Point, } impl Cache { @@ -316,7 +325,6 @@ impl Cache { hash: 0, layout: layout::Node::new(Size::new(0.0, 0.0)), bounds: Size::ZERO, - cursor_position: Point::new(-1.0, -1.0), } } } @@ -329,7 +337,7 @@ impl Default for Cache { impl PartialEq for Cache { fn eq(&self, other: &Cache) -> bool { - self.hash == other.hash && self.cursor_position == other.cursor_position + self.hash == other.hash } } -- cgit From cb530ccf2f0ba82e49be317a14eb61025777a24e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 30 Jun 2020 00:32:55 +0200 Subject: Rename `regions` and `splits` in `pane_grid::Node` --- native/src/widget/pane_grid.rs | 8 ++++---- native/src/widget/pane_grid/node.rs | 4 ++-- native/src/widget/pane_grid/state.rs | 14 ++++++++------ 3 files changed, 14 insertions(+), 12 deletions(-) (limited to 'native') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 2d21a968..de59795c 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -288,7 +288,7 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> { if let Some((split, _)) = self.state.picked_split() { let bounds = layout.bounds(); - let splits = self.state.splits( + let splits = self.state.split_regions( f32::from(self.spacing), Size::new(bounds.width, bounds.height), ); @@ -410,7 +410,7 @@ where let limits = limits.width(self.width).height(self.height); let size = limits.resolve(Size::ZERO); - let regions = self.state.regions(f32::from(self.spacing), size); + let regions = self.state.pane_regions(f32::from(self.spacing), size); let children = self .elements @@ -453,7 +453,7 @@ where cursor_position.y - bounds.y, ); - let splits = self.state.splits( + let splits = self.state.split_regions( f32::from(self.spacing), Size::new(bounds.width, bounds.height), ); @@ -590,7 +590,7 @@ where let splits = self .state - .splits(f32::from(self.spacing), bounds.size()); + .split_regions(f32::from(self.spacing), bounds.size()); hovered_split( splits.iter(), diff --git a/native/src/widget/pane_grid/node.rs b/native/src/widget/pane_grid/node.rs index b13c5e26..823caab1 100644 --- a/native/src/widget/pane_grid/node.rs +++ b/native/src/widget/pane_grid/node.rs @@ -48,7 +48,7 @@ impl Node { /// /// [`Pane`]: struct.Pane.html /// [`Node`]: enum.Node.html - pub fn regions( + pub fn pane_regions( &self, spacing: f32, size: Size, @@ -75,7 +75,7 @@ impl Node { /// /// [`Split`]: struct.Split.html /// [`Node`]: enum.Node.html - pub fn splits( + pub fn split_regions( &self, spacing: f32, size: Size, diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs index 4b13fb8e..57fdbc55 100644 --- a/native/src/widget/pane_grid/state.rs +++ b/native/src/widget/pane_grid/state.rs @@ -154,8 +154,10 @@ impl State { /// [`Pane`]: struct.Pane.html /// [`State::active`]: struct.State.html#method.active pub fn adjacent(&self, pane: &Pane, direction: Direction) -> Option { - let regions = - self.internal.layout.regions(0.0, Size::new(4096.0, 4096.0)); + let regions = self + .internal + .layout + .pane_regions(0.0, Size::new(4096.0, 4096.0)); let current_region = regions.get(pane)?; @@ -362,20 +364,20 @@ impl Internal { } } - pub fn regions( + pub fn pane_regions( &self, spacing: f32, size: Size, ) -> HashMap { - self.layout.regions(spacing, size) + self.layout.pane_regions(spacing, size) } - pub fn splits( + pub fn split_regions( &self, spacing: f32, size: Size, ) -> HashMap { - self.layout.splits(spacing, size) + self.layout.split_regions(spacing, size) } pub fn focus(&mut self, pane: &Pane) { -- cgit From 23f753e59978352292950e706d023ad02a3199bc Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 30 Jun 2020 00:58:14 +0200 Subject: Introduce `splits` method in `pane_grid::Node` --- native/src/widget/pane_grid/node.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'native') diff --git a/native/src/widget/pane_grid/node.rs b/native/src/widget/pane_grid/node.rs index 823caab1..cbfd8a43 100644 --- a/native/src/widget/pane_grid/node.rs +++ b/native/src/widget/pane_grid/node.rs @@ -43,6 +43,30 @@ pub enum Node { } impl Node { + /// Returns an iterator over each [`Split`] in this [`Node`]. + /// + /// [`Split`]: struct.Split.html + /// [`Node`]: enum.Node.html + pub fn splits(&self) -> impl Iterator { + let mut unvisited_nodes = vec![self]; + + std::iter::from_fn(move || { + while let Some(node) = unvisited_nodes.pop() { + match node { + Node::Split { id, a, b, .. } => { + unvisited_nodes.push(a); + unvisited_nodes.push(b); + + return Some(id); + } + _ => {} + } + } + + None + }) + } + /// Returns the rectangular region for each [`Pane`] in the [`Node`] given /// the spacing between panes and the total available space. /// -- cgit From cee8400663ac9a3ed5e56863021aabdbc4596198 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 30 Jun 2020 01:01:08 +0200 Subject: Unfocus `Pane` in `pane_grid` on click outbounds --- native/src/widget/pane_grid.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'native') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index de59795c..d5ce7fbb 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -273,8 +273,6 @@ impl<'a, Message, Renderer> PaneGrid<'a, Message, Renderer> { self.state.focus(pane); } } - } else { - self.state.unfocus(); } } @@ -482,6 +480,8 @@ where ); } } + } else { + self.state.unfocus(); } } mouse::Event::ButtonReleased(mouse::Button::Left) => { -- cgit From e50c61f7ff8c7bd559afb66025eaded78ca423bb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 30 Jun 2020 02:53:15 +0200 Subject: Add `unfocus` method to `pane_grid::State` --- native/src/widget/pane_grid/state.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'native') diff --git a/native/src/widget/pane_grid/state.rs b/native/src/widget/pane_grid/state.rs index 57fdbc55..a4cfb6f6 100644 --- a/native/src/widget/pane_grid/state.rs +++ b/native/src/widget/pane_grid/state.rs @@ -193,6 +193,13 @@ impl State { self.internal.focus(pane); } + /// Unfocuses the current focused [`Pane`]. + /// + /// [`Pane`]: struct.Pane.html + pub fn unfocus(&mut self) { + self.internal.unfocus(); + } + /// Splits the given [`Pane`] into two in the given [`Axis`] and /// initializing the new [`Pane`] with the provided internal state. /// -- cgit From e8aeb86698ae3f4ef23621d67685243d62158036 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 30 Jun 2020 07:38:04 +0200 Subject: Use `keyboard::ModifiersChanged` in `PaneGrid` --- native/src/widget/pane_grid.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'native') diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 2d21a968..f89c3588 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -539,10 +539,8 @@ where } } } - - *self.pressed_modifiers = modifiers; } - keyboard::Event::KeyReleased { modifiers, .. } => { + keyboard::Event::ModifiersChanged(modifiers) => { *self.pressed_modifiers = modifiers; } _ => {} -- cgit From 946bbd26835fef21b770c1ae56921fb4d8512287 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 4 Jul 2020 03:12:18 +0200 Subject: Truncate `Debug` messages after 100 characters --- native/src/debug/basic.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'native') diff --git a/native/src/debug/basic.rs b/native/src/debug/basic.rs index 5338d0d9..8a712038 100644 --- a/native/src/debug/basic.rs +++ b/native/src/debug/basic.rs @@ -174,9 +174,13 @@ impl Debug { lines.push(key_value("Render:", self.render_durations.average())); lines.push(key_value("Message count:", self.message_count)); lines.push(String::from("Last messages:")); - lines.extend( - self.last_messages.iter().map(|msg| format!(" {}", msg)), - ); + lines.extend(self.last_messages.iter().map(|msg| { + if msg.len() <= 100 { + format!(" {}", msg) + } else { + format!(" {:.100}...", msg) + } + })); lines } -- cgit From dfeb3db003d724a1c980329dab9cbfae55b7f589 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 6 Jul 2020 23:58:15 +0200 Subject: Use `default_font_size` for `TextInput` widget --- native/src/renderer/null.rs | 5 ----- native/src/widget/text_input.rs | 22 +++++++--------------- 2 files changed, 7 insertions(+), 20 deletions(-) (limited to 'native') diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index b8b0b996..5fd3627b 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -105,13 +105,8 @@ impl scrollable::Renderer for Null { } impl text_input::Renderer for Null { - type Font = Font; type Style = (); - fn default_size(&self) -> u16 { - 20 - } - fn measure_value(&self, _value: &str, _size: u16, _font: Font) -> f32 { 0.0 } diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index 24085606..3f415101 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -17,8 +17,8 @@ use editor::Editor; use crate::{ keyboard, layout, mouse::{self, click}, - Clipboard, Element, Event, Hasher, Layout, Length, Point, Rectangle, Size, - Widget, + text, Clipboard, Element, Event, Hasher, Layout, Length, Point, Rectangle, + Size, Widget, }; use std::u32; @@ -486,7 +486,8 @@ where let text_bounds = layout.children().next().unwrap().bounds(); if self.is_secure { - renderer.draw( + self::Renderer::draw( + renderer, bounds, text_bounds, cursor_position, @@ -498,7 +499,8 @@ where &self.style, ) } else { - renderer.draw( + self::Renderer::draw( + renderer, bounds, text_bounds, cursor_position, @@ -531,20 +533,10 @@ where /// /// [`TextInput`]: struct.TextInput.html /// [renderer]: ../../renderer/index.html -pub trait Renderer: crate::Renderer + Sized { - /// The font type used for [`TextInput`]. - /// - /// [`TextInput`]: struct.TextInput.html - type Font: Default + Copy; - +pub trait Renderer: text::Renderer + Sized { /// The style supported by this renderer. type Style: Default; - /// Returns the default size of the text of the [`TextInput`]. - /// - /// [`TextInput`]: struct.TextInput.html - fn default_size(&self) -> u16; - /// Returns the width of the value of the [`TextInput`]. /// /// [`TextInput`]: struct.TextInput.html -- cgit