Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui: Implement `Along` for `Edges` (#66614)

Finn Evers7bb5656e63ef
diff --git a/crates/gpui/src/geometry.rs b/crates/gpui/src/geometry.rs
index 8aded9780a3..d8f4bb51994 100644
--- a/crates/gpui/src/geometry.rs
+++ b/crates/gpui/src/geometry.rs
@@ -48,7 +48,7 @@ pub trait Along {
fn along(&self, axis: Axis) -> Self::Unit;
/// Applies the given function to the unit along the given axis and returns a new value.
- fn apply_along(&self, axis: Axis, f: impl FnOnce(Self::Unit) -> Self::Unit) -> Self;
+ fn apply_along(&self, axis: Axis, f: impl Fn(Self::Unit) -> Self::Unit) -> Self;
}
/// Describes a location in a 2D cartesian space.
@@ -169,7 +169,7 @@ impl<T: Clone + Debug + Default + PartialEq> Along for Point<T> {
}
}
- fn apply_along(&self, axis: Axis, f: impl FnOnce(T) -> T) -> Point<T> {
+ fn apply_along(&self, axis: Axis, f: impl Fn(T) -> T) -> Point<T> {
match axis {
Axis::Horizontal => Point {
x: f(self.x.clone()),
@@ -516,7 +516,7 @@ where
}
/// Returns the value of this size along the given axis.
- fn apply_along(&self, axis: Axis, f: impl FnOnce(T) -> T) -> Self {
+ fn apply_along(&self, axis: Axis, f: impl Fn(T) -> T) -> Self {
match axis {
Axis::Horizontal => Size {
width: f(self.width.clone()),
@@ -2145,6 +2145,37 @@ impl Edges<Pixels> {
}
}
+impl<T> Along for Edges<T>
+where
+ T: Clone + Debug + Default + PartialEq + Add<Output = T>,
+{
+ type Unit = T;
+
+ fn along(&self, axis: Axis) -> Self::Unit {
+ match axis {
+ Axis::Horizontal => self.left.clone() + self.right.clone(),
+ Axis::Vertical => self.top.clone() + self.bottom.clone(),
+ }
+ }
+
+ fn apply_along(&self, axis: Axis, f: impl Fn(Self::Unit) -> Self::Unit) -> Self {
+ match axis {
+ Axis::Horizontal => Edges {
+ top: self.top.clone(),
+ bottom: self.bottom.clone(),
+ right: f(self.right.clone()),
+ left: f(self.left.clone()),
+ },
+ Axis::Vertical => Edges {
+ top: f(self.top.clone()),
+ bottom: f(self.bottom.clone()),
+ right: self.right.clone(),
+ left: self.left.clone(),
+ },
+ }
+ }
+}
+
impl From<f32> for Edges<Pixels> {
fn from(val: f32) -> Self {
let val: Pixels = val.into();
diff --git a/crates/ui/src/components/scrollbar.rs b/crates/ui/src/components/scrollbar.rs
index 3cc42efc7f3..679b6bc17f0 100644
--- a/crates/ui/src/components/scrollbar.rs
+++ b/crates/ui/src/components/scrollbar.rs
@@ -355,8 +355,8 @@ impl ScrollAxes {
T: Debug + Default + PartialEq + Clone,
{
match self {
- Self::Horizontal => point.apply_along(ScrollbarAxis::Horizontal, |_| value),
- Self::Vertical => point.apply_along(ScrollbarAxis::Vertical, |_| value),
+ Self::Horizontal => point.apply_along(ScrollbarAxis::Horizontal, |_| value.clone()),
+ Self::Vertical => point.apply_along(ScrollbarAxis::Vertical, |_| value.clone()),
Self::Both => Point::new(value.clone(), value),
}
}