gpui-component/crates/ui/src/badge.rs
Jason Lee 327d40e0e0
chore: Upgrade GPUI 3 (#587)
The pervious version has keep on
[gpui2](https://github.com/longbridge/gpui-component/tree/gpui2) branch,
if there need to use.

Ref https://github.com/zed-industries/zed/pull/22632

## Prepare

### Generate index.scip

```bash
rust-analyzer scip ./ > index.scip
```

### Get [refactor](https://github.com/zed-industries/refactor)

```bash
https://github.com/zed-industries/refactor.git
```

## Refactor exist code

```bash
cd refactor
cargo run -- ~/work/gpui-component
```

Then will get result if successed:

```
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/refactor /Users/jason/work/gpui-component`
Loaded 115 SCIP documents
Processing files starting with focus path: ""
Changed 90 files
```

-------------

## Changes

```diff
- View<T>
- Model<T>
+ Entity<T>

- WeakView<T>
+ WeakEntity<T>

- FocusableView
+ Focusable

- cx.bounds()
+ window.bounds()
- cx.refresh()
+ window.refresh()
- cx.focused()
+ window.focused(cx)
- cx.with_optional_element_state
- window.with_optional_element_state
```

```diff
- &mut WindowContext
+ &mut Window, &mut App

- cx: &mut WindowContext
+ window: &mut Window, cx: &mut App

- &mut ViewContext<
+ &mut Window, &mut Context<

- cx: &mut ViewContext<
+ window: &mut Window, cx: &mut Context<
```

```diff
- cx.spawn(|_, mut cx| async move { 
+ cx.spawn_in(window, |_, mut cx| async move {
- cx.dispatch_action(Box::new(...))
+ window.dispatch_action(Box::new(...), cx)
```

`cx.spawn`

```diff
- cx.spawn(|view, mut cx| async move {
+ cx.spawn_in(window, |view, mut window| async move {
+     _ = view.update_in(&mut window, move |view, window, cx| {
```
2025-01-27 03:58:48 +08:00

87 lines
2.2 KiB
Rust

use gpui::{
div, prelude::FluentBuilder, px, relative, AnyElement, App, Div, IntoElement, ParentElement,
RenderOnce, Styled, Window,
};
use crate::{h_flex, red_500, white};
#[derive(Default)]
enum BadgeStyle {
Dot,
#[default]
Number,
}
#[derive(IntoElement)]
pub struct Badge {
base: Div,
count: usize,
max: usize,
style: BadgeStyle,
}
impl Badge {
pub fn new() -> Self {
Self {
base: div(),
count: 0,
max: 99,
style: Default::default(),
}
}
pub fn dot(mut self) -> Self {
self.style = BadgeStyle::Dot;
self
}
pub fn count(mut self, count: usize) -> Self {
self.count = count;
self
}
pub fn max(mut self, max: usize) -> Self {
self.max = max;
self
}
}
impl ParentElement for Badge {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.base.extend(elements);
}
}
impl RenderOnce for Badge {
fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
self.base.relative().when(self.count > 0, |this| {
this.child(
h_flex()
.absolute()
.justify_center()
.rounded_full()
.bg(red_500())
.map(|this| match self.style {
BadgeStyle::Dot => this.top(px(0.)).right(px(0.)).size(px(6.)),
BadgeStyle::Number => {
let count = if self.count > self.max {
format!("{}+", self.max)
} else {
self.count.to_string()
};
this.top(px(-2.))
.right_neg_1p5()
.py_0p5()
.px_0p5()
.min_w_3p5()
.text_color(white())
.text_size(px(10.))
.line_height(relative(1.))
.child(count)
}
}),
)
})
}
}