use std::{ops::Deref, rc::Rc};
use wry::{
Rect,
dpi::{self, LogicalSize},
};
use gpui::{
App, Bounds, ContentMask, DismissEvent, Element, ElementId, Entity, EventEmitter, FocusHandle,
Focusable, GlobalElementId, Hitbox, InteractiveElement, IntoElement, LayoutId, MouseDownEvent,
ParentElement as _, Pixels, Render, Size, Style, Styled as _, Window, canvas, div,
};
use crate::PixelsExt;
pub struct WebView {
focus_handle: FocusHandle,
webview: Rc<wry::WebView>,
visible: bool,
bounds: Bounds<Pixels>,
}
impl Drop for WebView {
fn drop(&mut self) {
self.hide();
}
}
impl WebView {
pub fn new(webview: wry::WebView, _: &mut Window, cx: &mut App) -> Self {
let _ = webview.set_bounds(Rect::default());
Self {
focus_handle: cx.focus_handle(),
visible: true,
bounds: Bounds::default(),
webview: Rc::new(webview),
}
}
pub fn show(&mut self) {
let _ = self.webview.set_visible(true);
self.visible = true;
}
pub fn hide(&mut self) {
_ = self.webview.focus_parent();
_ = self.webview.set_visible(false);
self.visible = false;
}
pub fn visible(&self) -> bool {
self.visible
}
pub fn bounds(&self) -> Bounds<Pixels> {
self.bounds
}
pub fn back(&mut self) -> anyhow::Result<()> {
Ok(self.webview.evaluate_script("history.back();")?)
}
pub fn load_url(&mut self, url: &str) {
self.webview.load_url(url).unwrap();
}
pub fn raw(&self) -> &wry::WebView {
&self.webview
}
}
impl Deref for WebView {
type Target = wry::WebView;
fn deref(&self) -> &Self::Target {
&self.webview
}
}
impl Focusable for WebView {
fn focus_handle(&self, _cx: &gpui::App) -> FocusHandle {
self.focus_handle.clone()
}
}
impl EventEmitter<DismissEvent> for WebView {}
impl Render for WebView {
fn render(
&mut self,
window: &mut gpui::Window,
cx: &mut gpui::Context<Self>,
) -> impl IntoElement {
let view = cx.entity().clone();
div()
.track_focus(&self.focus_handle)
.size_full()
.child({
let view = cx.entity().clone();
canvas(
move |bounds, _, cx| view.update(cx, |r, _| r.bounds = bounds),
|_, _, _, _| {},
)
.absolute()
.size_full()
})
.child(WebViewElement::new(self.webview.clone(), view, window, cx))
}
}
pub struct WebViewElement {
parent: Entity<WebView>,
view: Rc<wry::WebView>,
}
impl WebViewElement {
pub fn new(
view: Rc<wry::WebView>,
parent: Entity<WebView>,
_window: &mut Window,
_cx: &mut App,
) -> Self {
Self { view, parent }
}
}
impl IntoElement for WebViewElement {
type Element = WebViewElement;
fn into_element(self) -> Self::Element {
self
}
}
impl Element for WebViewElement {
type RequestLayoutState = ();
type PrepaintState = Option<Hitbox>;
fn id(&self) -> Option<ElementId> {
None
}
fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&gpui::InspectorElementId>,
window: &mut Window,
cx: &mut App,
) -> (LayoutId, Self::RequestLayoutState) {
let mut style = Style::default();
style.flex_grow = 0.0;
style.flex_shrink = 1.;
style.size = Size::full();
let id = window.request_layout(style, [], cx);
(id, ())
}
fn prepaint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&gpui::InspectorElementId>,
bounds: Bounds<Pixels>,
_: &mut Self::RequestLayoutState,
window: &mut Window,
cx: &mut App,
) -> Self::PrepaintState {
if !self.parent.read(cx).visible() {
return None;
}
self.view
.set_bounds(Rect {
size: dpi::Size::Logical(LogicalSize {
width: (bounds.size.width.as_f32()).into(),
height: (bounds.size.height.as_f32()).into(),
}),
position: dpi::Position::Logical(dpi::LogicalPosition::new(
bounds.origin.x.into(),
bounds.origin.y.into(),
)),
})
.unwrap();
Some(window.insert_hitbox(bounds, gpui::HitboxBehavior::Normal))
}
fn paint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&gpui::InspectorElementId>,
bounds: Bounds<Pixels>,
_: &mut Self::RequestLayoutState,
hitbox: &mut Self::PrepaintState,
window: &mut Window,
_: &mut App,
) {
let bounds = hitbox.clone().map(|h| h.bounds).unwrap_or(bounds);
window.with_content_mask(Some(ContentMask { bounds }), |window| {
let webview = self.view.clone();
window.on_mouse_event(move |event: &MouseDownEvent, _, _, _| {
if !bounds.contains(&event.position) {
let _ = webview.focus_parent();
}
});
});
}
}