Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gpui: Add scrollbar_width method to div element #25392

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions crates/gpui/src/elements/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
//! constructed by combining these two systems into an all-in-one element.

use crate::{
point, px, size, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, App, Bounds, ClickEvent,
DispatchPhase, Element, ElementId, Entity, FocusHandle, Global, GlobalElementId, Hitbox,
HitboxId, IntoElement, IsZero, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId,
point, px, size, AbsoluteLength, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, App, Bounds,
ClickEvent, DispatchPhase, Element, ElementId, Entity, FocusHandle, Global, GlobalElementId,
Hitbox, HitboxId, IntoElement, IsZero, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId,
ModifiersChangedEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent,
ParentElement, Pixels, Point, Render, ScrollWheelEvent, SharedString, Size, Style,
StyleRefinement, Styled, Task, TooltipId, Visibility, Window,
Expand Down Expand Up @@ -952,6 +952,15 @@ pub trait StatefulInteractiveElement: InteractiveElement {
self
}

/// Set the space to be reserved for rendering the scrollbar.
///
/// This will only affect the layout of the element when overflow for this element is set to
/// `Overflow::Scroll`.
fn scrollbar_width(mut self, width: impl Into<AbsoluteLength>) -> Self {
self.interactivity().base_style.scrollbar_width = Some(width.into());
self
}

/// Track the scroll state of this element with the given handle.
fn track_scroll(mut self, scroll_handle: &ScrollHandle) -> Self {
self.interactivity().tracked_scroll_handle = Some(scroll_handle.clone());
Expand Down
4 changes: 2 additions & 2 deletions crates/gpui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ pub struct Style {
#[refineable]
pub overflow: Point<Overflow>,
/// How much space (in points) should be reserved for the scrollbars of `Overflow::Scroll` and `Overflow::Auto` nodes.
pub scrollbar_width: f32,
pub scrollbar_width: AbsoluteLength,
/// Whether both x and y axis should be scrollable at the same time.
pub allow_concurrent_scroll: bool,

Expand Down Expand Up @@ -702,7 +702,7 @@ impl Default for Style {
y: Overflow::Visible,
},
allow_concurrent_scroll: false,
scrollbar_width: 0.0,
scrollbar_width: AbsoluteLength::default(),
position: Position::Relative,
inset: Edges::auto(),
margin: Edges::<Length>::zero(),
Expand Down
47 changes: 18 additions & 29 deletions crates/gpui/src/taffy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl ToTaffy<taffy::style::Style> for Style {
taffy::style::Style {
display: self.display,
overflow: self.overflow.into(),
scrollbar_width: self.scrollbar_width,
scrollbar_width: self.scrollbar_width.to_taffy(rem_size),
position: self.position,
inset: self.inset.to_taffy(rem_size),
size: self.size.to_taffy(rem_size),
Expand Down Expand Up @@ -290,17 +290,19 @@ impl ToTaffy<taffy::style::Dimension> for Length {
}
}

impl ToTaffy<f32> for AbsoluteLength {
fn to_taffy(&self, rem_size: Pixels) -> f32 {
match self {
AbsoluteLength::Pixels(pixels) => pixels.into(),
AbsoluteLength::Rems(rems) => (*rems * rem_size).into(),
}
}
}

impl ToTaffy<taffy::style::LengthPercentage> for DefiniteLength {
fn to_taffy(&self, rem_size: Pixels) -> taffy::style::LengthPercentage {
match self {
DefiniteLength::Absolute(length) => match length {
AbsoluteLength::Pixels(pixels) => {
taffy::style::LengthPercentage::Length(pixels.into())
}
AbsoluteLength::Rems(rems) => {
taffy::style::LengthPercentage::Length((*rems * rem_size).into())
}
},
DefiniteLength::Absolute(length) => length.to_taffy(rem_size),
DefiniteLength::Fraction(fraction) => {
taffy::style::LengthPercentage::Percent(*fraction)
}
Expand All @@ -311,14 +313,9 @@ impl ToTaffy<taffy::style::LengthPercentage> for DefiniteLength {
impl ToTaffy<taffy::style::LengthPercentageAuto> for DefiniteLength {
fn to_taffy(&self, rem_size: Pixels) -> taffy::style::LengthPercentageAuto {
match self {
DefiniteLength::Absolute(length) => match length {
AbsoluteLength::Pixels(pixels) => {
taffy::style::LengthPercentageAuto::Length(pixels.into())
}
AbsoluteLength::Rems(rems) => {
taffy::style::LengthPercentageAuto::Length((*rems * rem_size).into())
}
},
DefiniteLength::Absolute(length) => {
taffy::style::LengthPercentageAuto::Length(length.to_taffy(rem_size))
}
DefiniteLength::Fraction(fraction) => {
taffy::style::LengthPercentageAuto::Percent(*fraction)
}
Expand All @@ -329,25 +326,17 @@ impl ToTaffy<taffy::style::LengthPercentageAuto> for DefiniteLength {
impl ToTaffy<taffy::style::Dimension> for DefiniteLength {
fn to_taffy(&self, rem_size: Pixels) -> taffy::style::Dimension {
match self {
DefiniteLength::Absolute(length) => match length {
AbsoluteLength::Pixels(pixels) => taffy::style::Dimension::Length(pixels.into()),
AbsoluteLength::Rems(rems) => {
taffy::style::Dimension::Length((*rems * rem_size).into())
}
},
DefiniteLength::Absolute(length) => {
taffy::style::Dimension::Length(length.to_taffy(rem_size))
}
DefiniteLength::Fraction(fraction) => taffy::style::Dimension::Percent(*fraction),
}
}
}

impl ToTaffy<taffy::style::LengthPercentage> for AbsoluteLength {
fn to_taffy(&self, rem_size: Pixels) -> taffy::style::LengthPercentage {
match self {
AbsoluteLength::Pixels(pixels) => taffy::style::LengthPercentage::Length(pixels.into()),
AbsoluteLength::Rems(rems) => {
taffy::style::LengthPercentage::Length((*rems * rem_size).into())
}
}
taffy::style::LengthPercentage::Length(self.to_taffy(rem_size))
}
}

Expand Down
Loading