From 267e242238fab0aba14fb4c2e27269ce3a3e3951 Mon Sep 17 00:00:00 2001 From: Nikolai Vazquez Date: Fri, 29 Nov 2019 21:24:52 -0500 Subject: Make many functions `const` The point is to set up repeated components or boilerplate before their use sites. The majority of these make sense as `const`. However, some functions such as those regarding state may not make sense as `const`. --- core/src/point.rs | 2 +- core/src/vector.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'core/src') diff --git a/core/src/point.rs b/core/src/point.rs index 183998dd..52307bba 100644 --- a/core/src/point.rs +++ b/core/src/point.rs @@ -14,7 +14,7 @@ impl Point { /// Creates a new [`Point`] with the given coordinates. /// /// [`Point`]: struct.Point.html - pub fn new(x: f32, y: f32) -> Self { + pub const fn new(x: f32, y: f32) -> Self { Self { x, y } } } diff --git a/core/src/vector.rs b/core/src/vector.rs index 7d87343a..e0c5f073 100644 --- a/core/src/vector.rs +++ b/core/src/vector.rs @@ -16,7 +16,7 @@ impl Vector { /// Creates a new [`Vector`] with the given components. /// /// [`Vector`]: struct.Vector.html - pub fn new(x: T, y: T) -> Self { + pub const fn new(x: T, y: T) -> Self { Self { x, y } } } -- cgit From 126133ead775fda064a6c23503e9a552a10dc2c5 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 22 Feb 2020 18:03:49 +0100 Subject: Fix `Clip` primitive intersection in `iced_wgpu` --- core/src/rectangle.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'core/src') diff --git a/core/src/rectangle.rs b/core/src/rectangle.rs index ee1e3807..7ed3d2df 100644 --- a/core/src/rectangle.rs +++ b/core/src/rectangle.rs @@ -27,6 +27,34 @@ impl Rectangle { && self.y <= point.y && point.y <= self.y + self.height } + + /// Computes the intersection with the given [`Rectangle`]. + /// + /// [`Rectangle`]: struct.Rectangle.html + pub fn intersection( + &self, + other: &Rectangle, + ) -> Option> { + let x = self.x.max(other.x); + let y = self.y.max(other.y); + + let lower_right_x = (self.x + self.width).min(other.x + other.width); + let lower_right_y = (self.y + self.height).min(other.y + other.height); + + let width = lower_right_x - x; + let height = lower_right_y - y; + + if width > 0.0 && height > 0.0 { + Some(Rectangle { + x, + y, + width, + height, + }) + } else { + None + } + } } impl std::ops::Mul for Rectangle { @@ -41,3 +69,25 @@ impl std::ops::Mul for Rectangle { } } } + +impl From> for Rectangle { + fn from(rectangle: Rectangle) -> Rectangle { + Rectangle { + x: rectangle.x as f32, + y: rectangle.y as f32, + width: rectangle.width as f32, + height: rectangle.height as f32, + } + } +} + +impl From> for Rectangle { + fn from(rectangle: Rectangle) -> Rectangle { + Rectangle { + x: rectangle.x as u32, + y: rectangle.y as u32, + width: rectangle.width.ceil() as u32, + height: rectangle.height.ceil() as u32, + } + } +} -- cgit From b74e7e7353d69ffb54cf0c0f0574ea7abf0f3a68 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 7 Mar 2020 23:45:54 +0100 Subject: Implement `Primitive::Cached` --- core/src/point.rs | 11 +++++++++++ core/src/vector.rs | 11 +++++++++++ 2 files changed, 22 insertions(+) (limited to 'core/src') diff --git a/core/src/point.rs b/core/src/point.rs index b9a8149c..b55f5099 100644 --- a/core/src/point.rs +++ b/core/src/point.rs @@ -46,3 +46,14 @@ impl std::ops::Add for Point { } } } + +impl std::ops::Sub for Point { + type Output = Self; + + fn sub(self, vector: Vector) -> Self { + Self { + x: self.x - vector.x, + y: self.y - vector.y, + } + } +} diff --git a/core/src/vector.rs b/core/src/vector.rs index 4c1cbfab..a75053a0 100644 --- a/core/src/vector.rs +++ b/core/src/vector.rs @@ -32,6 +32,17 @@ where } } +impl std::ops::Sub for Vector +where + T: std::ops::Sub, +{ + type Output = Self; + + fn sub(self, b: Self) -> Self { + Self::new(self.x - b.x, self.y - b.y) + } +} + impl Default for Vector where T: Default, -- cgit From f08cb4ad565799689d07bacc190fbe0436a63648 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 Mar 2020 08:10:50 +0100 Subject: Implement mouse-based pane resizing for `PaneGrid` --- core/src/point.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'core/src') diff --git a/core/src/point.rs b/core/src/point.rs index b55f5099..b855cd91 100644 --- a/core/src/point.rs +++ b/core/src/point.rs @@ -22,6 +22,16 @@ impl Point { pub const fn new(x: f32, y: f32) -> Self { Self { x, y } } + + /// Computes the distance to another [`Point`]. + /// + /// [`Point`]: struct.Point.html + pub fn distance(&self, to: Point) -> f32 { + let a = self.x - to.x; + let b = self.y - to.y; + + f32::sqrt(a * a + b * b) + } } impl From<[f32; 2]> for Point { -- cgit From 05beb878527b4d4e3141ca5ba09337d6ada858be Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 17 Mar 2020 07:28:28 +0100 Subject: Move common keyboard types to `iced_core` Also expose them in `iced` through `iced_native` and `iced_web`. --- core/src/keyboard.rs | 6 ++ core/src/keyboard/key_code.rs | 198 +++++++++++++++++++++++++++++++++++ core/src/keyboard/modifiers_state.rs | 15 +++ core/src/lib.rs | 1 + 4 files changed, 220 insertions(+) create mode 100644 core/src/keyboard.rs create mode 100644 core/src/keyboard/key_code.rs create mode 100644 core/src/keyboard/modifiers_state.rs (limited to 'core/src') diff --git a/core/src/keyboard.rs b/core/src/keyboard.rs new file mode 100644 index 00000000..d98b2989 --- /dev/null +++ b/core/src/keyboard.rs @@ -0,0 +1,6 @@ +//! Reuse basic keyboard types. +mod key_code; +mod modifiers_state; + +pub use key_code::KeyCode; +pub use modifiers_state::ModifiersState; diff --git a/core/src/keyboard/key_code.rs b/core/src/keyboard/key_code.rs new file mode 100644 index 00000000..26020a57 --- /dev/null +++ b/core/src/keyboard/key_code.rs @@ -0,0 +1,198 @@ +/// The symbolic name of a keyboard key. +/// +/// This is mostly the `KeyCode` type found in [`winit`]. +/// +/// [`winit`]: https://docs.rs/winit/0.20.0-alpha3/winit/ +#[derive(Debug, Hash, Ord, PartialOrd, PartialEq, Eq, Clone, Copy)] +#[repr(u32)] +#[allow(missing_docs)] +pub enum KeyCode { + /// The '1' key over the letters. + Key1, + /// The '2' key over the letters. + Key2, + /// The '3' key over the letters. + Key3, + /// The '4' key over the letters. + Key4, + /// The '5' key over the letters. + Key5, + /// The '6' key over the letters. + Key6, + /// The '7' key over the letters. + Key7, + /// The '8' key over the letters. + Key8, + /// The '9' key over the letters. + Key9, + /// The '0' key over the 'O' and 'P' keys. + Key0, + + A, + B, + C, + D, + E, + F, + G, + H, + I, + J, + K, + L, + M, + N, + O, + P, + Q, + R, + S, + T, + U, + V, + W, + X, + Y, + Z, + + /// The Escape key, next to F1 + Escape, + + F1, + F2, + F3, + F4, + F5, + F6, + F7, + F8, + F9, + F10, + F11, + F12, + F13, + F14, + F15, + F16, + F17, + F18, + F19, + F20, + F21, + F22, + F23, + F24, + + /// Print Screen/SysRq + Snapshot, + /// Scroll Lock + Scroll, + /// Pause/Break key, next to Scroll lock + Pause, + + /// `Insert`, next to Backspace + Insert, + Home, + Delete, + End, + PageDown, + PageUp, + + Left, + Up, + Right, + Down, + + Backspace, + Enter, + Space, + + /// The "Compose" key on Linux + Compose, + + Caret, + + Numlock, + Numpad0, + Numpad1, + Numpad2, + Numpad3, + Numpad4, + Numpad5, + Numpad6, + Numpad7, + Numpad8, + Numpad9, + + AbntC1, + AbntC2, + Add, + Apostrophe, + Apps, + At, + Ax, + Backslash, + Calculator, + Capital, + Colon, + Comma, + Convert, + Decimal, + Divide, + Equals, + Grave, + Kana, + Kanji, + LAlt, + LBracket, + LControl, + LShift, + LWin, + Mail, + MediaSelect, + MediaStop, + Minus, + Multiply, + Mute, + MyComputer, + NavigateForward, // also called "Prior" + NavigateBackward, // also called "Next" + NextTrack, + NoConvert, + NumpadComma, + NumpadEnter, + NumpadEquals, + OEM102, + Period, + PlayPause, + Power, + PrevTrack, + RAlt, + RBracket, + RControl, + RShift, + RWin, + Semicolon, + Slash, + Sleep, + Stop, + Subtract, + Sysrq, + Tab, + Underline, + Unlabeled, + VolumeDown, + VolumeUp, + Wake, + WebBack, + WebFavorites, + WebForward, + WebHome, + WebRefresh, + WebSearch, + WebStop, + Yen, + Copy, + Paste, + Cut, +} diff --git a/core/src/keyboard/modifiers_state.rs b/core/src/keyboard/modifiers_state.rs new file mode 100644 index 00000000..3058c065 --- /dev/null +++ b/core/src/keyboard/modifiers_state.rs @@ -0,0 +1,15 @@ +/// The current state of the keyboard modifiers. +#[derive(Debug, Clone, Copy, PartialEq, Default)] +pub struct ModifiersState { + /// Whether a shift key is pressed + pub shift: bool, + + /// Whether a control key is pressed + pub control: bool, + + /// Whether an alt key is pressed + pub alt: bool, + + /// Whether a logo key is pressed (e.g. windows key, command key...) + pub logo: bool, +} diff --git a/core/src/lib.rs b/core/src/lib.rs index ea5e8b43..c2887a0b 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -14,6 +14,7 @@ #![deny(unused_results)] #![forbid(unsafe_code)] #![forbid(rust_2018_idioms)] +pub mod keyboard; mod align; mod background; -- cgit From 50b02d41a01ad66e08045b320a30a0f5d76ee2f9 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 18 Mar 2020 07:10:36 +0100 Subject: Check only for partial match of modifier keys --- core/src/keyboard/modifiers_state.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'core/src') diff --git a/core/src/keyboard/modifiers_state.rs b/core/src/keyboard/modifiers_state.rs index 3058c065..0cfc6d69 100644 --- a/core/src/keyboard/modifiers_state.rs +++ b/core/src/keyboard/modifiers_state.rs @@ -1,5 +1,5 @@ /// The current state of the keyboard modifiers. -#[derive(Debug, Clone, Copy, PartialEq, Default)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub struct ModifiersState { /// Whether a shift key is pressed pub shift: bool, @@ -13,3 +13,18 @@ pub struct ModifiersState { /// Whether a logo key is pressed (e.g. windows key, command key...) pub logo: bool, } + +impl ModifiersState { + /// Returns true if the current [`ModifiersState`] has at least the same + /// modifiers enabled as the given value, and false otherwise. + /// + /// [`ModifiersState`]: struct.ModifiersState.html + pub fn matches(&self, modifiers: ModifiersState) -> bool { + let shift = !modifiers.shift || modifiers.shift && self.shift; + let control = !modifiers.control || modifiers.control && self.control; + let alt = !modifiers.alt || modifiers.alt && self.alt; + let logo = !modifiers.logo || modifiers.logo && self.logo; + + shift && control && alt && logo + } +} -- cgit From 18f016cba70bf59095ae65ce0e289d80a548ae58 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 20 Mar 2020 04:08:18 +0100 Subject: Use `f32::hypot` in `Point::distance` --- core/src/point.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/src') diff --git a/core/src/point.rs b/core/src/point.rs index b855cd91..43ee2143 100644 --- a/core/src/point.rs +++ b/core/src/point.rs @@ -30,7 +30,7 @@ impl Point { let a = self.x - to.x; let b = self.y - to.y; - f32::sqrt(a * a + b * b) + a.hypot(b) } } -- cgit From 31aaf207d6772e2ef332bf523cde262cac118d1a Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 20 Mar 2020 04:10:58 +0100 Subject: Remove redundant check in `ModifiersState::matches` --- core/src/keyboard/modifiers_state.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'core/src') diff --git a/core/src/keyboard/modifiers_state.rs b/core/src/keyboard/modifiers_state.rs index 0cfc6d69..4d24266f 100644 --- a/core/src/keyboard/modifiers_state.rs +++ b/core/src/keyboard/modifiers_state.rs @@ -20,10 +20,10 @@ impl ModifiersState { /// /// [`ModifiersState`]: struct.ModifiersState.html pub fn matches(&self, modifiers: ModifiersState) -> bool { - let shift = !modifiers.shift || modifiers.shift && self.shift; - let control = !modifiers.control || modifiers.control && self.control; - let alt = !modifiers.alt || modifiers.alt && self.alt; - let logo = !modifiers.logo || modifiers.logo && self.logo; + let shift = !modifiers.shift || self.shift; + let control = !modifiers.control || self.control; + let alt = !modifiers.alt || self.alt; + let logo = !modifiers.logo || self.logo; shift && control && alt && logo } -- cgit