ui: Add margin style methods to Label and LabelLike (#14032)

This PR adds margin style methods to the `Label` and `LabelLike`
components.

This allows for callers to provide a margin to these components without
needing to introduce a wrapping `div` to do so.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-07-09 17:41:54 -04:00 committed by GitHub
parent 4bb8a0845f
commit a46a562dc2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 81 additions and 50 deletions

View file

@ -332,7 +332,7 @@ impl ButtonSize {
/// This is also used to build the prebuilt buttons.
#[derive(IntoElement)]
pub struct ButtonLike {
pub base: Div,
pub(super) base: Div,
id: ElementId,
pub(super) style: ButtonStyle,
pub(super) disabled: bool,

View file

@ -1,4 +1,4 @@
use gpui::WindowContext;
use gpui::{StyleRefinement, WindowContext};
use crate::{prelude::*, LabelCommon, LabelLike, LabelSize, LineHeightStyle};
@ -70,6 +70,17 @@ impl Label {
}
}
// Style methods.
impl Label {
fn style(&mut self) -> &mut StyleRefinement {
self.base.base.style()
}
gpui::margin_style_methods!({
visibility: pub
});
}
impl LabelCommon for Label {
/// Sets the size of the label using a [`LabelSize`].
///

View file

@ -1,4 +1,4 @@
use gpui::{relative, AnyElement, FontWeight, Styled};
use gpui::{relative, AnyElement, FontWeight, StyleRefinement, Styled};
use smallvec::SmallVec;
use crate::prelude::*;
@ -43,6 +43,7 @@ pub trait LabelCommon {
#[derive(IntoElement)]
pub struct LabelLike {
pub(super) base: Div,
size: LabelSize,
weight: FontWeight,
line_height_style: LineHeightStyle,
@ -55,6 +56,7 @@ pub struct LabelLike {
impl LabelLike {
pub fn new() -> Self {
Self {
base: div(),
size: LabelSize::Default,
weight: FontWeight::default(),
line_height_style: LineHeightStyle::default(),
@ -66,6 +68,17 @@ impl LabelLike {
}
}
// Style methods.
impl LabelLike {
fn style(&mut self) -> &mut StyleRefinement {
self.base.style()
}
gpui::margin_style_methods!({
visibility: pub
});
}
impl LabelCommon for LabelLike {
fn size(mut self, size: LabelSize) -> Self {
self.size = size;
@ -106,7 +119,7 @@ impl ParentElement for LabelLike {
impl RenderOnce for LabelLike {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
div()
self.base
.when(self.strikethrough, |this| {
this.relative().child(
div()