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| { ```
113 lines
3.1 KiB
Rust
113 lines
3.1 KiB
Rust
use gpui::{
|
|
div, App, AppContext, ClickEvent, Context, Entity, FocusHandle, Focusable, IntoElement,
|
|
ParentElement as _, Render, Styled as _, Window,
|
|
};
|
|
use ui::{
|
|
h_flex,
|
|
input::{InputEvent, TextInput},
|
|
v_flex,
|
|
webview::WebView,
|
|
ActiveTheme,
|
|
};
|
|
|
|
pub struct WebViewStory {
|
|
focus_handle: FocusHandle,
|
|
webview: Entity<WebView>,
|
|
address_input: Entity<TextInput>,
|
|
}
|
|
|
|
impl super::Story for WebViewStory {
|
|
fn title() -> &'static str {
|
|
"WebView"
|
|
}
|
|
|
|
fn new_view(window: &mut Window, cx: &mut App) -> Entity<impl Render + Focusable> {
|
|
Self::view(window, cx)
|
|
}
|
|
}
|
|
|
|
impl WebViewStory {
|
|
pub fn view(window: &mut Window, cx: &mut App) -> Entity<Self> {
|
|
let focus_handle = cx.focus_handle();
|
|
|
|
let webview = cx.new(|cx| {
|
|
let webview = ui::wry::WebViewBuilder::new()
|
|
.build_as_child(&window.raw_window_handle())
|
|
.unwrap();
|
|
WebView::new(webview, window, cx)
|
|
});
|
|
|
|
let address_input = cx.new(|cx| {
|
|
let mut input = TextInput::new(window, cx);
|
|
input.set_text("https://google.com", window, cx);
|
|
input
|
|
});
|
|
|
|
let url = address_input.read(cx).text();
|
|
webview.update(cx, |view, _| {
|
|
view.load_url(&url);
|
|
});
|
|
|
|
cx.new(|cx| {
|
|
let this = WebViewStory {
|
|
focus_handle,
|
|
webview,
|
|
address_input: address_input.clone(),
|
|
};
|
|
|
|
cx.subscribe(
|
|
&address_input,
|
|
|this: &mut Self, input, event: &InputEvent, cx| match event {
|
|
InputEvent::PressEnter => {
|
|
let url = input.read(cx).text();
|
|
this.webview.update(cx, |view, _| {
|
|
view.load_url(&url);
|
|
});
|
|
}
|
|
_ => {}
|
|
},
|
|
)
|
|
.detach();
|
|
|
|
this
|
|
})
|
|
}
|
|
|
|
pub fn hide(&self, _: &mut Window, cx: &mut App) {
|
|
self.webview.update(cx, |webview, _| webview.hide())
|
|
}
|
|
|
|
#[allow(unused)]
|
|
fn go_back(&mut self, _: &ClickEvent, window: &mut Window, cx: &mut Context<Self>) {
|
|
self.webview.update(cx, |webview, _| {
|
|
webview.back().unwrap();
|
|
});
|
|
}
|
|
}
|
|
|
|
impl Focusable for WebViewStory {
|
|
fn focus_handle(&self, _cx: &gpui::App) -> FocusHandle {
|
|
self.focus_handle.clone()
|
|
}
|
|
}
|
|
|
|
impl Render for WebViewStory {
|
|
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
let webview = self.webview.clone();
|
|
let address_input = self.address_input.clone();
|
|
|
|
v_flex()
|
|
.p_2()
|
|
.gap_3()
|
|
.size_full()
|
|
.child(h_flex().gap_2().items_center().child(address_input.clone()))
|
|
.child(
|
|
div()
|
|
.flex_1()
|
|
.border_1()
|
|
.h(gpui::px(400.))
|
|
.border_color(cx.theme().border)
|
|
.child(webview.clone()),
|
|
)
|
|
}
|
|
}
|