webview: Invoke blur on call hide method for WebView and update example. (#321)

This commit is contained in:
Jason Lee 2024-10-08 18:09:25 +08:00 committed by GitHub
parent 33200aa685
commit 4fd233ae6b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 61 additions and 27 deletions

View file

@ -1,6 +1,6 @@
use gpui::{ use gpui::{
div, ClickEvent, FocusHandle, FocusableView, ParentElement as _, Render, Styled as _, View, div, px, ClickEvent, FocusHandle, FocusableView, IntoElement, ParentElement as _, Render,
ViewContext, VisualContext as _, WindowContext, Styled as _, View, ViewContext, VisualContext as _, WindowContext,
}; };
use ui::{ use ui::{
button::Button, button::Button,
@ -9,7 +9,7 @@ use ui::{
theme::ActiveTheme, theme::ActiveTheme,
v_flex, v_flex,
webview::WebView, webview::WebView,
IconName, ContextModal, Placement,
}; };
pub struct WebViewStory { pub struct WebViewStory {
@ -79,6 +79,7 @@ impl WebViewStory {
self.webview.update(cx, |webview, _| webview.hide()) self.webview.update(cx, |webview, _| webview.hide())
} }
#[allow(unused)]
fn go_back(&mut self, _: &ClickEvent, cx: &mut ViewContext<Self>) { fn go_back(&mut self, _: &ClickEvent, cx: &mut ViewContext<Self>) {
self.webview.update(cx, |webview, _| { self.webview.update(cx, |webview, _| {
webview.back().unwrap(); webview.back().unwrap();
@ -93,28 +94,54 @@ impl FocusableView for WebViewStory {
} }
impl Render for WebViewStory { impl Render for WebViewStory {
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl gpui::IntoElement { fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
v_flex() let webview = self.webview.clone();
.p_2() let address_input = self.address_input.clone();
.gap_3() div().child(
.size_full() Button::new("show-webview")
.child( .label("Open WebView")
h_flex() .on_click(cx.listener(move |_, _: &ClickEvent, cx| {
.gap_2() let webview = webview.clone();
.items_center() let address_input = address_input.clone();
.child(
Button::new("go-back") cx.open_drawer(move |this, cx| {
.icon(IconName::ArrowLeft) webview.update(cx, |view, _| {
.on_click(cx.listener(Self::go_back)), view.show();
) });
.child(self.address_input.clone()),
) let webview1 = webview.clone();
.child( this.size(px(640.))
div() .title("WebView")
.size_full() .placement(Placement::Bottom)
.border_1() .child(
.border_color(cx.theme().border) v_flex()
.child(self.webview.clone()), .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()),
),
)
.on_close({
move |_, cx| {
webview1.update(cx, |view, _| {
view.hide();
});
}
})
});
})),
)
} }
} }

View file

@ -20,6 +20,12 @@ pub struct WebView {
visible: bool, visible: bool,
} }
impl Drop for WebView {
fn drop(&mut self) {
self.hide();
}
}
impl WebView { impl WebView {
pub fn new(cx: &mut WindowContext, webview: wry::WebView) -> Self { pub fn new(cx: &mut WindowContext, webview: wry::WebView) -> Self {
let _ = webview.set_bounds(Rect::default()); let _ = webview.set_bounds(Rect::default());
@ -36,7 +42,8 @@ impl WebView {
} }
pub fn hide(&mut self) { pub fn hide(&mut self) {
let _ = self.webview.set_visible(false); _ = self.webview.blur();
_ = self.webview.set_visible(false);
} }
pub fn visible(&self) -> bool { pub fn visible(&self) -> bool {