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| { ```
94 lines
2.2 KiB
Rust
94 lines
2.2 KiB
Rust
use gpui::{
|
|
div, prelude::FluentBuilder, rems, App, Div, IntoElement, ParentElement, RenderOnce,
|
|
SharedString, Styled, Window,
|
|
};
|
|
|
|
use crate::{h_flex, ActiveTheme};
|
|
|
|
const MASKED: &'static str = "•";
|
|
|
|
#[derive(Default, PartialEq, Eq)]
|
|
pub enum TextAlign {
|
|
#[default]
|
|
Left,
|
|
Center,
|
|
Right,
|
|
}
|
|
|
|
#[derive(IntoElement)]
|
|
pub struct Label {
|
|
base: Div,
|
|
label: SharedString,
|
|
align: TextAlign,
|
|
marked: bool,
|
|
}
|
|
|
|
impl Label {
|
|
pub fn new(label: impl Into<SharedString>) -> Self {
|
|
Self {
|
|
base: h_flex().line_height(rems(1.25)),
|
|
label: label.into(),
|
|
align: TextAlign::default(),
|
|
marked: false,
|
|
}
|
|
}
|
|
|
|
pub fn text_align(mut self, align: TextAlign) -> Self {
|
|
self.align = align;
|
|
self
|
|
}
|
|
|
|
pub fn text_left(mut self) -> Self {
|
|
self.align = TextAlign::Left;
|
|
self
|
|
}
|
|
|
|
pub fn text_center(mut self) -> Self {
|
|
self.align = TextAlign::Center;
|
|
self
|
|
}
|
|
|
|
pub fn text_right(mut self) -> Self {
|
|
self.align = TextAlign::Right;
|
|
self
|
|
}
|
|
|
|
pub fn masked(mut self, masked: bool) -> Self {
|
|
self.marked = masked;
|
|
self
|
|
}
|
|
}
|
|
|
|
impl Styled for Label {
|
|
fn style(&mut self) -> &mut gpui::StyleRefinement {
|
|
self.base.style()
|
|
}
|
|
}
|
|
|
|
impl RenderOnce for Label {
|
|
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
|
|
let text = self.label;
|
|
|
|
let text_display = if self.marked {
|
|
MASKED.repeat(text.chars().count())
|
|
} else {
|
|
text.to_string()
|
|
};
|
|
|
|
div().text_color(cx.theme().foreground).child(
|
|
self.base
|
|
.map(|this| match self.align {
|
|
TextAlign::Left => this.justify_start(),
|
|
TextAlign::Center => this.justify_center(),
|
|
TextAlign::Right => this.justify_end(),
|
|
})
|
|
.map(|this| {
|
|
if self.align == TextAlign::Left {
|
|
this.child(div().size_full().child(text_display))
|
|
} else {
|
|
this.child(text_display)
|
|
}
|
|
}),
|
|
)
|
|
}
|
|
}
|