Keeps its content at a fixed width-to-height ratio.
Loading GPUI…
use gpui::{div, prelude::*, px, App, Window};
use crate::theme::ActiveTheme;
use crate::ui::aspect_ratio::AspectRatio;
pub fn aspect_ratio_demo(_window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
div().w(px(400.0)).child(
AspectRatio::new(16.0 / 9.0).child(
div()
.size_full()
.flex()
.flex_col()
.items_center()
.justify_center()
.gap_1()
.rounded(theme.radius)
.bg(theme.muted)
.text_color(theme.muted_foreground)
.child(div().text_2xl().text_color(theme.foreground).child("16 : 9"))
.child(div().text_sm().child("Resize the parent; the height follows.")),
),
)
}Installation
$ cargo hyuskui add aspect-ratio
Copies src/ui/aspect_ratio.rs into your project, along with any components it depends on, and registers the module in src/ui/mod.rs.
Install the theme
Every component reads colors from
crate::theme. Follow the installation guide if you have not added it yet.Copy the source
Paste the code below into
src/ui/aspect_ratio.rs.src/ui/aspect_ratio.rs use gpui::{div, prelude::*, AnyElement, App, Window}; /// Keeps its content at a fixed width-to-height ratio, e.g. `AspectRatio::new(16.0 / 9.0)`. /// It fills the width of its parent and derives its height from that. #[derive(IntoElement)] pub struct AspectRatio { ratio: f32, children: Vec<AnyElement>, } impl AspectRatio { pub fn new(ratio: f32) -> Self { Self { ratio, children: Vec::new(), } } } impl ParentElement for AspectRatio { fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { self.children.extend(elements); } } impl RenderOnce for AspectRatio { fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement { // A non-positive or NaN ratio would collapse or blow up the layout, so fall back to square. let ratio = if self.ratio.is_finite() && self.ratio > 0.0 { self.ratio } else { 1.0 }; div() .relative() .w_full() .aspect_ratio(ratio) .overflow_hidden() .child(div().absolute().inset_0().children(self.children)) } }Register the module
// src/ui/mod.rs pub mod aspect_ratio;
Usage
use crate::ui::aspect_ratio::*;Examples
Square
AspectRatio::new(1.0) keeps content square, the usual frame for thumbnails and avatars.
Loading GPUI…
use gpui::{div, linear_color_stop, linear_gradient, prelude::*, px, rgb, App, Window};
use crate::theme::ActiveTheme;
use crate::ui::aspect_ratio::AspectRatio;
pub fn aspect_ratio_square(_window: &mut Window, cx: &mut App) -> impl IntoElement {
let radius = cx.theme().radius;
div().w(px(240.0)).child(
AspectRatio::new(1.0).child(
div()
.size_full()
.flex()
.items_center()
.justify_center()
.rounded(radius)
.bg(linear_gradient(
135.0,
linear_color_stop(rgb(0x0ea5e9), 0.0),
linear_color_stop(rgb(0x6366f1), 1.0),
))
.text_2xl()
.text_color(rgb(0xffffff))
.child("1 : 1"),
),
)
}Portrait
AspectRatio::new(9.0 / 16.0) frames tall content such as phone screenshots and stories.
Loading GPUI…
use gpui::{div, linear_color_stop, linear_gradient, prelude::*, px, rgb, App, Window};
use crate::theme::ActiveTheme;
use crate::ui::aspect_ratio::AspectRatio;
pub fn aspect_ratio_portrait(_window: &mut Window, cx: &mut App) -> impl IntoElement {
let radius = cx.theme().radius;
div().w(px(160.0)).child(
AspectRatio::new(9.0 / 16.0).child(
div()
.size_full()
.flex()
.items_center()
.justify_center()
.rounded(radius)
.bg(linear_gradient(
180.0,
linear_color_stop(rgb(0xf97316), 0.0),
linear_color_stop(rgb(0xdb2777), 1.0),
))
.text_2xl()
.text_color(rgb(0xffffff))
.child("9 : 16"),
),
)
}API Reference
AspectRatio
Keeps its content at a fixed width-to-height ratio, e.g. AspectRatio::new(16.0 / 9.0). It fills the width of its parent and derives its height from that.
Method
AspectRatio::new(ratio: f32) -> Self