Skip to content

Commit

Permalink
webview: Invoke blur on call hide method for WebView and update exa…
Browse files Browse the repository at this point in the history
…mple. (#321)
  • Loading branch information
huacnlee authored Oct 8, 2024
1 parent 33200aa commit 4fd233a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
79 changes: 53 additions & 26 deletions crates/story/src/webview_story.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use gpui::{
div, ClickEvent, FocusHandle, FocusableView, ParentElement as _, Render, Styled as _, View,
ViewContext, VisualContext as _, WindowContext,
div, px, ClickEvent, FocusHandle, FocusableView, IntoElement, ParentElement as _, Render,
Styled as _, View, ViewContext, VisualContext as _, WindowContext,
};
use ui::{
button::Button,
Expand All @@ -9,7 +9,7 @@ use ui::{
theme::ActiveTheme,
v_flex,
webview::WebView,
IconName,
ContextModal, Placement,
};

pub struct WebViewStory {
Expand Down Expand Up @@ -79,6 +79,7 @@ impl WebViewStory {
self.webview.update(cx, |webview, _| webview.hide())
}

#[allow(unused)]
fn go_back(&mut self, _: &ClickEvent, cx: &mut ViewContext<Self>) {
self.webview.update(cx, |webview, _| {
webview.back().unwrap();
Expand All @@ -93,28 +94,54 @@ impl FocusableView for WebViewStory {
}

impl Render for WebViewStory {
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl gpui::IntoElement {
v_flex()
.p_2()
.gap_3()
.size_full()
.child(
h_flex()
.gap_2()
.items_center()
.child(
Button::new("go-back")
.icon(IconName::ArrowLeft)
.on_click(cx.listener(Self::go_back)),
)
.child(self.address_input.clone()),
)
.child(
div()
.size_full()
.border_1()
.border_color(cx.theme().border)
.child(self.webview.clone()),
)
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let webview = self.webview.clone();
let address_input = self.address_input.clone();
div().child(
Button::new("show-webview")
.label("Open WebView")
.on_click(cx.listener(move |_, _: &ClickEvent, cx| {
let webview = webview.clone();
let address_input = address_input.clone();

cx.open_drawer(move |this, cx| {
webview.update(cx, |view, _| {
view.show();
});

let webview1 = webview.clone();
this.size(px(640.))
.title("WebView")
.placement(Placement::Bottom)
.child(
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()),
),
)
.on_close({
move |_, cx| {
webview1.update(cx, |view, _| {
view.hide();
});
}
})
});
})),
)
}
}
9 changes: 8 additions & 1 deletion crates/ui/src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pub struct WebView {
visible: bool,
}

impl Drop for WebView {
fn drop(&mut self) {
self.hide();
}
}

impl WebView {
pub fn new(cx: &mut WindowContext, webview: wry::WebView) -> Self {
let _ = webview.set_bounds(Rect::default());
Expand All @@ -36,7 +42,8 @@ impl WebView {
}

pub fn hide(&mut self) {
let _ = self.webview.set_visible(false);
_ = self.webview.blur();
_ = self.webview.set_visible(false);
}

pub fn visible(&self) -> bool {
Expand Down

0 comments on commit 4fd233a

Please sign in to comment.