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.

Add `w_vw` and `h_vh` to `StyledExt` for setting sizes in viewport units (#3600)

Marshall Bowers75c1f0e99722
diff --git a/crates/storybook2/src/stories.rs b/crates/storybook2/src/stories.rs
index f7ab3ef5489..d0c363b4b4c 100644
--- a/crates/storybook2/src/stories.rs
+++ b/crates/storybook2/src/stories.rs
@@ -5,6 +5,7 @@ mod kitchen_sink;
mod picker;
mod scroll;
mod text;
+mod viewport_units;
mod z_index;
pub use auto_height_editor::*;
@@ -14,4 +15,5 @@ pub use kitchen_sink::*;
pub use picker::*;
pub use scroll::*;
pub use text::*;
+pub use viewport_units::*;
pub use z_index::*;
diff --git a/crates/storybook2/src/stories/viewport_units.rs b/crates/storybook2/src/stories/viewport_units.rs
new file mode 100644
index 00000000000..8a95a6b66c3
--- /dev/null
+++ b/crates/storybook2/src/stories/viewport_units.rs
@@ -0,0 +1,34 @@
+use gpui::{Div, Render};
+use story::Story;
+
+use ui::prelude::*;
+
+pub struct ViewportUnitsStory;
+
+impl Render for ViewportUnitsStory {
+ type Element = Div;
+
+ fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
+ Story::container().child(
+ div()
+ .flex()
+ .flex_row()
+ .child(
+ div()
+ .w_vw(0.5, cx)
+ .h_vh(0.8, cx)
+ .bg(gpui::red())
+ .text_color(gpui::white())
+ .child("50vw, 80vh"),
+ )
+ .child(
+ div()
+ .w_vw(0.25, cx)
+ .h_vh(0.33, cx)
+ .bg(gpui::green())
+ .text_color(gpui::white())
+ .child("25vw, 33vh"),
+ ),
+ )
+ }
+}
diff --git a/crates/storybook2/src/story_selector.rs b/crates/storybook2/src/story_selector.rs
index 1e899a5783e..266511dcfd0 100644
--- a/crates/storybook2/src/story_selector.rs
+++ b/crates/storybook2/src/story_selector.rs
@@ -30,6 +30,7 @@ pub enum ComponentStory {
Scroll,
Tab,
Text,
+ ViewportUnits,
ZIndex,
Picker,
}
@@ -55,6 +56,7 @@ impl ComponentStory {
Self::Scroll => ScrollStory::view(cx).into(),
Self::Text => TextStory::view(cx).into(),
Self::Tab => cx.build_view(|_| ui::TabStory).into(),
+ Self::ViewportUnits => cx.build_view(|_| crate::stories::ViewportUnitsStory).into(),
Self::ZIndex => cx.build_view(|_| ZIndexStory).into(),
Self::Picker => PickerStory::new(cx).into(),
}
diff --git a/crates/ui2/src/styled_ext.rs b/crates/ui2/src/styled_ext.rs
index a04deea7580..ed81c2cd0a7 100644
--- a/crates/ui2/src/styled_ext.rs
+++ b/crates/ui2/src/styled_ext.rs
@@ -16,6 +16,20 @@ fn elevated<E: Styled>(this: E, cx: &mut WindowContext, index: ElevationIndex) -
/// Extends [`Styled`](gpui::Styled) with Zed specific styling methods.
pub trait StyledExt: Styled + Sized {
+ /// Sets the width of the element as a percentage of the viewport's width.
+ ///
+ /// `percent` should be a value between `0.0` and `1.0`.
+ fn w_vw(self, percent: f32, cx: &mut WindowContext) -> Self {
+ self.w(cx.viewport_size().width * percent)
+ }
+
+ /// Sets the height of the element as a percentage of the viewport's height.
+ ///
+ /// `percent` should be a value between `0.0` and `1.0`.
+ fn h_vh(self, percent: f32, cx: &mut WindowContext) -> Self {
+ self.h(cx.viewport_size().height * percent)
+ }
+
/// Horizontally stacks elements.
///
/// Sets `flex()`, `flex_row()`, `items_center()`