From 7489a4ffddfb8916096cfba0996835a263b1e867 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B9=E5=90=89=E5=B3=B0?= Date: Wed, 24 Jul 2024 19:32:05 +0800 Subject: [PATCH] Update WebView to pass wry::WebView instance (#69) It's much prefer to use `WebViewBuilder` instead of [WebView::new](https://docs.rs/wry/latest/wry/struct.WebView.html#method.new) --- crates/story/src/webview_story.rs | 7 ++++++- crates/ui/src/input/blink_cursor.rs | 2 +- crates/ui/src/lib.rs | 3 +++ crates/ui/src/webview.rs | 15 +++++---------- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/crates/story/src/webview_story.rs b/crates/story/src/webview_story.rs index 1f82cc1a..897a0d7e 100644 --- a/crates/story/src/webview_story.rs +++ b/crates/story/src/webview_story.rs @@ -22,7 +22,12 @@ impl WebViewStory { pub fn view(cx: &mut WindowContext) -> View { let focus_handle = cx.focus_handle(); - let webview = cx.new_view(|cx| WebView::new(cx)); + let webview = cx.new_view(|cx| { + let webview = ui::wry::WebViewBuilder::new_as_child(&cx.raw_window_handle()) + .build() + .unwrap(); + WebView::new(cx, webview) + }); let address_input = cx.new_view(|cx| { let mut input = TextInput::new(cx); diff --git a/crates/ui/src/input/blink_cursor.rs b/crates/ui/src/input/blink_cursor.rs index 901d8232..c1aaae35 100644 --- a/crates/ui/src/input/blink_cursor.rs +++ b/crates/ui/src/input/blink_cursor.rs @@ -5,7 +5,7 @@ use gpui::{ModelContext, Timer}; /// To manage the Input cursor blinking. /// /// It will start blinking with a interval of 500ms. -/// Every loop will notify the view to update the `visable`, and Input will observe this update to touch repaint. +/// Every loop will notify the view to update the `visible`, and Input will observe this update to touch repaint. /// /// The input painter will check if this in visible state, then it will draw the cursor. pub struct BlinkCursor { diff --git a/crates/ui/src/lib.rs b/crates/ui/src/lib.rs index bca9d6aa..6d96dd93 100644 --- a/crates/ui/src/lib.rs +++ b/crates/ui/src/lib.rs @@ -33,6 +33,9 @@ pub mod theme; pub mod tooltip; pub mod webview; +// re-export +pub use wry; + pub use clickable::Clickable; pub use disableable::Disableable; pub use event::InterativeElementExt; diff --git a/crates/ui/src/webview.rs b/crates/ui/src/webview.rs index 95facd68..0ce97375 100644 --- a/crates/ui/src/webview.rs +++ b/crates/ui/src/webview.rs @@ -17,21 +17,16 @@ pub fn init(_cx: &AppContext) {} pub struct WebView { focus_handle: FocusHandle, webview: Rc, - visable: bool, + visible: bool, } impl WebView { - pub fn new(cx: &mut WindowContext) -> Self { - let focus_handle = cx.focus_handle(); - let window_handle = cx.raw_window_handle(); - - let webview = wry::WebView::new_as_child(&window_handle) - .expect("failed to create webview to child window"); + pub fn new(cx: &mut WindowContext, webview: wry::WebView) -> Self { let _ = webview.set_bounds(Rect::default()); Self { - focus_handle, - visable: true, + focus_handle: cx.focus_handle(), + visible: true, webview: Rc::new(webview), } } @@ -45,7 +40,7 @@ impl WebView { } pub fn visible(&self) -> bool { - self.visable + self.visible } /// Go back in the webview history.