Files
oak-gpui/crates/ui/src/components/chip.rs
T
Martin Pool b9cf5886e4 Run doctests in CI and fix up existing doctests (#37851)
Follows on from
https://github.com/zed-industries/zed/pull/37716#pullrequestreview-3195695110
by @SomeoneToIgnore

After this the doctests will be run in CI to check that the examples are
still accurate.

Note that doctests aren't run by Nextest: you can run them locally with
`cargo test --doc`.

Summary:
* Run tests from CI
* Loosen an exact float comparison to match approximately (otherwise it
fails)
* Fixed one actual bug in the tests for `dilate` where the test code
assumed that `dilate` mutates `self` rather than returning a new object
* Add some `must_use` on some functions that seemed at risk of similar
bugs, following the Rust stdlib style to add it where ignoring the
result is almost certainly a bug.
* Fix some cases where the doc examples seem to have gone out of date
with the code
* Add imports to doctests that need them
* Add some dev-dependencies to make the tests build
* Fix the `key_dispatch` module docstring, which was accidentally
attached to objects within that module
* Skip some doctest examples that seem like they need an async
environment or that just looked hard to get running

AI usage: I asked Claude to do some of the repetitive tests. I checked
the output and fixed up some things that seemed to not be in the right
spirit of the test, or too longwinded.

I think we could reasonably run the tests on only Linux to save CI
CPU-seconds and latency, but I haven't done that yet, partly because of
how it's implemented in the action.

Release Notes:

- N/A
2025-09-12 23:24:04 +03:00

107 lines
2.9 KiB
Rust

use crate::prelude::*;
use gpui::{AnyElement, Hsla, IntoElement, ParentElement, Styled};
/// Chips provide a container for an informative label.
///
/// # Usage Example
///
/// ```
/// use ui::Chip;
///
/// let chip = Chip::new("This Chip");
/// ```
#[derive(IntoElement, RegisterComponent)]
pub struct Chip {
label: SharedString,
label_color: Color,
label_size: LabelSize,
bg_color: Option<Hsla>,
}
impl Chip {
/// Creates a new `Chip` component with the specified label.
pub fn new(label: impl Into<SharedString>) -> Self {
Self {
label: label.into(),
label_color: Color::Default,
label_size: LabelSize::XSmall,
bg_color: None,
}
}
/// Sets the color of the label.
pub fn label_color(mut self, color: Color) -> Self {
self.label_color = color;
self
}
/// Sets the size of the label.
pub fn label_size(mut self, size: LabelSize) -> Self {
self.label_size = size;
self
}
/// Sets a custom background color for the callout content.
pub fn bg_color(mut self, color: Hsla) -> Self {
self.bg_color = Some(color);
self
}
}
impl RenderOnce for Chip {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
let bg_color = self
.bg_color
.unwrap_or(cx.theme().colors().element_background);
h_flex()
.min_w_0()
.flex_initial()
.px_1()
.border_1()
.rounded_sm()
.border_color(cx.theme().colors().border)
.bg(bg_color)
.overflow_hidden()
.child(
Label::new(self.label)
.size(self.label_size)
.color(self.label_color)
.buffer_font(cx),
)
}
}
impl Component for Chip {
fn scope() -> ComponentScope {
ComponentScope::DataDisplay
}
fn preview(_window: &mut Window, cx: &mut App) -> Option<AnyElement> {
let chip_examples = vec![
single_example("Default", Chip::new("Chip Example").into_any_element()),
single_example(
"Customized Label Color",
Chip::new("Chip Example")
.label_color(Color::Accent)
.into_any_element(),
),
single_example(
"Customized Label Size",
Chip::new("Chip Example")
.label_size(LabelSize::Large)
.label_color(Color::Accent)
.into_any_element(),
),
single_example(
"Customized Background Color",
Chip::new("Chip Example")
.bg_color(cx.theme().colors().text_accent.opacity(0.1))
.into_any_element(),
),
];
Some(example_group(chip_examples).vertical().into_any_element())
}
}