Skip to content

Commit

Permalink
Fix Label, Checkbox, Radio to support label text wrap. (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
huacnlee authored Jul 31, 2024
1 parent c94e9fe commit d659713
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 24 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ serde = "1.0.203"
serde_json = "1"

[workspace.lints.clippy]
dbg_macro = "deny"
todo = "deny"
single_range_in_vec_init = "allow"
style = "allow"
almost_complete_range = "allow"
arc_with_non_send_sync = "allow"
borrowed_box = "allow"
dbg_macro = "deny"
let_underscore_future = "allow"
map_entry = "allow"
module_inception = "allow"
non_canonical_partial_ord_impl = "allow"
reversed_empty_ranges = "allow"
single_range_in_vec_init = "allow"
style = { level = "allow", priority = -1 }
todo = "deny"
type_complexity = "allow"
module_inception = "allow"

[profile.dev]
split-debuginfo = "unpacked"
Expand Down
33 changes: 28 additions & 5 deletions crates/story/src/checkbox_story.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use gpui::{
px, rems, IntoElement, ParentElement, Render, Styled, View, ViewContext, VisualContext as _,
WindowContext,
div, px, rems, IntoElement, ParentElement, Render, Styled, View, ViewContext,
VisualContext as _, WindowContext,
};

use ui::{
Expand Down Expand Up @@ -51,6 +51,7 @@ impl Render for CheckboxStory {
.gap_6()
.child(
section("Label", cx)
.items_start()
.child(
v_flex()
.w_full()
Expand All @@ -66,6 +67,13 @@ impl Render for CheckboxStory {
.text_left()
.font_semibold()
.line_height(rems(1.8)),
)
.child(
div().w(px(200.)).child(
Label::new("Label should support text wrap in default, if the text is too long, it should wrap to the next line.")
.text_left()
.line_height(rems(1.8)),
),
),
)
.child(
Expand Down Expand Up @@ -96,7 +104,7 @@ impl Render for CheckboxStory {
section("Checkbox", cx).child(
h_flex()
.w_full()
.items_center()
.items_start()
.gap_6()
.child(
Checkbox::new("check1")
Expand All @@ -120,7 +128,13 @@ impl Render for CheckboxStory {
.on_click(cx.listener(|v, _, _| {
v.check3 = v.check3.inverse();
})),
),
)
.child(
div().w(px(300.)).child(
Checkbox::new("longlong-checkbox")
.label("Warp: Label should support text wrap in default, if the text is too long, it should wrap to the next line.")
),
)
),
)
.child(
Expand Down Expand Up @@ -154,6 +168,7 @@ impl Render for CheckboxStory {
h_flex()
.w_full()
.gap_4()
.items_start()
.child(
Radio::new("radio1")
.selected(self.select1)
Expand All @@ -174,7 +189,15 @@ impl Render for CheckboxStory {
.label("Disabled Radio")
.selected(true)
.disabled(true),
),
)
.child(
div().w(px(200.)).child(
Radio::new("radio3")
.label("Warp: A long long long text radio label")
.selected(true)
.disabled(true),
),
)
),
)
}
Expand Down
18 changes: 13 additions & 5 deletions crates/ui/src/checkbox.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use gpui::{
prelude::FluentBuilder as _, svg, ElementId, InteractiveElement, IntoElement, ParentElement,
RenderOnce, SharedString, StatefulInteractiveElement as _, Styled as _, WindowContext,
div, prelude::FluentBuilder as _, relative, svg, ElementId, InteractiveElement, IntoElement,
ParentElement, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled as _,
WindowContext,
};

use crate::{
Expand Down Expand Up @@ -84,16 +85,16 @@ impl RenderOnce for Checkbox {
h_flex()
.id(self.id)
.group(group_id.clone())
.justify_center()
.items_center()
.gap_2()
.items_start()
.child(
v_flex()
.relative()
.border_1()
.border_color(color)
.rounded_sm()
.size_4()
.flex_shrink_0()
.map(|this| match self.checked {
Selection::Unselected => this.bg(theme.transparent),
_ => this.bg(color),
Expand Down Expand Up @@ -121,7 +122,14 @@ impl RenderOnce for Checkbox {
)
.map(|this| {
if let Some(label) = self.label {
this.child(label).text_color(color)
this.child(
div()
.w_full()
.overflow_hidden()
.line_height(relative(1.))
.child(label),
)
.text_color(color)
} else {
this
}
Expand Down
3 changes: 1 addition & 2 deletions crates/ui/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use gpui::{
Styled, WindowContext,
};

use crate::{theme::ActiveTheme, StyledExt};
use crate::theme::ActiveTheme;

#[derive(Default)]
pub enum TextAlign {
Expand Down Expand Up @@ -88,7 +88,6 @@ impl RenderOnce for Label {

div().text_color(cx.theme().foreground).child(
self.base
.h_flex()
.map(|this| match self.align {
TextAlign::Left => this.justify_start(),
TextAlign::Center => this.justify_center(),
Expand Down
27 changes: 20 additions & 7 deletions crates/ui/src/radio.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use gpui::{
div, prelude::FluentBuilder, svg, CursorStyle, ElementId, InteractiveElement, IntoElement,
ParentElement, RenderOnce, SharedString, StatefulInteractiveElement, Styled, WindowContext,
div, prelude::FluentBuilder, relative, svg, CursorStyle, ElementId, InteractiveElement,
IntoElement, ParentElement, RenderOnce, SharedString, StatefulInteractiveElement, Styled,
WindowContext,
};

use crate::{
Expand Down Expand Up @@ -63,29 +64,41 @@ impl RenderOnce for Radio {
.gap_x_2()
.cursor(CursorStyle::PointingHand)
.text_color(color)
.items_start()
.child(
div()
.relative()
.w_3p5()
.h_3p5()
.size_4()
.flex_shrink_0()
.rounded_full()
.border_1()
.border_color(color)
.mt_neg_0p5()
.when(self.selected, |this| this.bg(color))
.child(
svg()
.absolute()
.top_px()
.left_px()
.size_2p5()
.size_3()
.text_color(color)
.when(self.selected, |this| {
this.text_color(cx.theme().primary_foreground)
})
.map(|this| match self.selected {
true => this.path(IconName::Check.path()),
false => this,
}),
),
)
.when_some(self.label, |this, label| this.child(label))
.when_some(self.label, |this, label| {
this.child(
div()
.size_full()
.overflow_hidden()
.line_height(relative(1.))
.child(label),
)
})
.when_some(
self.on_click.filter(|_| !self.disabled),
|this, on_click| {
Expand Down

0 comments on commit d659713

Please sign in to comment.