gpui-component/crates/ui/src/table/loading.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

89 lines
2.4 KiB
Rust

use crate::{h_flex, skeleton::Skeleton, v_flex, ActiveTheme, Size};
use gpui::{prelude::FluentBuilder as _, IntoElement, ParentElement as _, RenderOnce, Styled};
#[derive(IntoElement)]
pub struct Loading {
size: Size,
}
impl Loading {
pub fn new() -> Self {
Self { size: Size::Medium }
}
pub fn size(mut self, size: Size) -> Self {
self.size = size;
self
}
}
#[derive(IntoElement)]
struct LoadingRow {
header: bool,
size: Size,
}
impl LoadingRow {
pub fn header() -> Self {
Self {
header: true,
size: Size::Medium,
}
}
pub fn row() -> Self {
Self {
header: false,
size: Size::Medium,
}
}
pub fn size(mut self, size: Size) -> Self {
self.size = size;
self
}
}
impl RenderOnce for LoadingRow {
fn render(self, _: &mut gpui::Window, cx: &mut gpui::App) -> impl IntoElement {
let paddings = self.size.table_cell_padding();
let height = self.size.table_row_height() * 0.5;
h_flex()
.gap_3()
.h(self.size.table_row_height())
.overflow_hidden()
.pt(paddings.top)
.pb(paddings.bottom)
.pl(paddings.left)
.pr(paddings.right)
.items_center()
.justify_between()
.overflow_hidden()
.when(self.header, |this| this.bg(cx.theme().table_head))
.when(!self.header, |this| {
this.border_t_1().border_color(cx.theme().table_row_border)
})
.child(
h_flex()
.gap_3()
.flex_1()
.child(Skeleton::new().secondary(self.header).h(height).w_24())
.child(Skeleton::new().secondary(self.header).h(height).w_48())
.child(Skeleton::new().secondary(self.header).h(height).w_16()),
)
.child(Skeleton::new().secondary(self.header).h(height).w_24())
}
}
impl RenderOnce for Loading {
fn render(self, _window: &mut gpui::Window, _cx: &mut gpui::App) -> impl IntoElement {
v_flex()
.gap_0()
.child(LoadingRow::header().size(self.size))
.child(LoadingRow::row().size(self.size))
.child(LoadingRow::row().size(self.size))
.child(LoadingRow::row().size(self.size))
.child(LoadingRow::row().size(self.size))
}
}