-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,038 additions
and
458 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
use gpui::{ | ||
div, px, FocusHandle, FocusableView, ParentElement as _, Render, Styled as _, View, | ||
VisualContext as _, WindowContext, | ||
}; | ||
use ui::{ | ||
input::{TextEvent, TextInput}, | ||
theme::ActiveTheme, | ||
v_flex, | ||
webview::WebViewContainer, | ||
StyledExt, | ||
}; | ||
|
||
pub struct WebViewStory { | ||
focus_handle: FocusHandle, | ||
webview: View<WebViewContainer>, | ||
address_input: View<TextInput>, | ||
} | ||
|
||
impl WebViewStory { | ||
pub fn view(cx: &mut WindowContext) -> View<Self> { | ||
let focus_handle = cx.focus_handle(); | ||
|
||
let webview = cx.new_view(|cx| WebViewContainer::new(cx)); | ||
|
||
let address_input = cx.new_view(|cx| { | ||
let mut input = TextInput::new(cx); | ||
input.set_text("https://google.com", cx); | ||
input | ||
}); | ||
|
||
let url = address_input.read(cx).text(); | ||
webview.update(cx, |view, cx| { | ||
view.load_url(&url); | ||
}); | ||
|
||
cx.new_view(|cx| { | ||
let this = WebViewStory { | ||
focus_handle, | ||
webview, | ||
address_input: address_input.clone(), | ||
}; | ||
|
||
cx.subscribe( | ||
&address_input, | ||
|this: &mut Self, input, event: &TextEvent, cx| match event { | ||
TextEvent::PressEnter => { | ||
let url = input.read(cx).text(); | ||
this.webview.update(cx, |view, cx| { | ||
view.load_url(&url); | ||
}); | ||
} | ||
_ => {} | ||
}, | ||
) | ||
.detach(); | ||
|
||
this | ||
}) | ||
} | ||
} | ||
|
||
impl FocusableView for WebViewStory { | ||
fn focus_handle(&self, _cx: &gpui::AppContext) -> FocusHandle { | ||
self.focus_handle.clone() | ||
} | ||
} | ||
|
||
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(self.address_input.clone()) | ||
.child( | ||
div() | ||
.size_full() | ||
.border_1() | ||
.border_color(cx.theme().border) | ||
.child(self.webview.clone()), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
use std::rc::Rc; | ||
|
||
use wry::{ | ||
dpi::{self, LogicalSize}, | ||
Rect, WebViewExtMacOS, | ||
}; | ||
|
||
use gpui::{ | ||
div, AppContext, Bounds, DismissEvent, Element, ElementId, EventEmitter, FocusHandle, | ||
FocusableView, GlobalElementId, Hitbox, IntoElement, LayoutId, ParentElement as _, Pixels, | ||
Render, Size, Style, Styled as _, View, WindowContext, | ||
}; | ||
|
||
pub fn init(_cx: &AppContext) {} | ||
|
||
pub struct WebViewContainer { | ||
focus_handle: FocusHandle, | ||
webview: Rc<wry::WebView>, | ||
visable: bool, | ||
} | ||
|
||
impl WebViewContainer { | ||
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).unwrap(); | ||
|
||
Self { | ||
focus_handle, | ||
visable: true, | ||
webview: Rc::new(webview), | ||
} | ||
} | ||
|
||
pub fn show(&mut self) { | ||
let _ = self.webview.set_visible(true); | ||
} | ||
|
||
pub fn hide(&mut self) { | ||
let _ = self.webview.set_visible(false); | ||
} | ||
|
||
pub fn visible(&self) -> bool { | ||
self.visable | ||
} | ||
|
||
pub fn load_url(&mut self, url: &str) { | ||
self.webview.load_url(url).unwrap(); | ||
} | ||
} | ||
|
||
impl FocusableView for WebViewContainer { | ||
fn focus_handle(&self, _cx: &gpui::AppContext) -> FocusHandle { | ||
self.focus_handle.clone() | ||
} | ||
} | ||
|
||
impl EventEmitter<DismissEvent> for WebViewContainer {} | ||
|
||
impl Render for WebViewContainer { | ||
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> impl IntoElement { | ||
let view = cx.view().clone(); | ||
|
||
div() | ||
.size_full() | ||
.debug() | ||
.child(WebView::new(self.webview.clone(), view, cx)) | ||
} | ||
} | ||
|
||
/// A webview element can display a wry webview. | ||
pub struct WebView { | ||
focus_handle: FocusHandle, | ||
parent: View<WebViewContainer>, | ||
view: Rc<wry::WebView>, | ||
} | ||
|
||
impl WebView { | ||
/// Create a new webview element from a wry WebView. | ||
pub fn new( | ||
view: Rc<wry::WebView>, | ||
parent: View<WebViewContainer>, | ||
cx: &mut WindowContext, | ||
) -> Self { | ||
let focus_handle = cx.focus_handle(); | ||
|
||
Self { | ||
view, | ||
parent, | ||
focus_handle, | ||
} | ||
} | ||
} | ||
|
||
impl IntoElement for WebView { | ||
type Element = WebView; | ||
|
||
fn into_element(self) -> Self::Element { | ||
self | ||
} | ||
} | ||
|
||
impl Element for WebView { | ||
type RequestLayoutState = (); | ||
type PrepaintState = Option<Hitbox>; | ||
|
||
fn id(&self) -> Option<ElementId> { | ||
None | ||
} | ||
|
||
fn request_layout( | ||
&mut self, | ||
id: Option<&GlobalElementId>, | ||
cx: &mut WindowContext, | ||
) -> (LayoutId, Self::RequestLayoutState) { | ||
let mut style = Style::default(); | ||
style.flex_grow = 0.0; | ||
style.flex_shrink = 1.; | ||
style.size = Size::full(); | ||
|
||
dbg!("---- request_layout"); | ||
// If the parent view is no longer visible, we don't need to layout the webview | ||
|
||
let id = cx.request_layout(style, []); | ||
(id, ()) | ||
} | ||
|
||
fn prepaint( | ||
&mut self, | ||
_: Option<&GlobalElementId>, | ||
bounds: Bounds<Pixels>, | ||
layout_state: &mut Self::RequestLayoutState, | ||
cx: &mut WindowContext, | ||
) -> Self::PrepaintState { | ||
if !self.parent.read(cx).visible() { | ||
return None; | ||
} | ||
|
||
if bounds.top() > cx.viewport_size().height || bounds.bottom() < Pixels::ZERO { | ||
dbg!("---------------- hidden"); | ||
self.view.set_visible(false).unwrap(); | ||
} else { | ||
dbg!("---------------- show", &bounds); | ||
self.view.set_visible(true).unwrap(); | ||
|
||
self.view | ||
.set_bounds(Rect { | ||
size: dpi::Size::Logical(LogicalSize { | ||
width: (bounds.size.width.0).into(), | ||
height: (bounds.size.height.0).into(), | ||
}), | ||
position: dpi::Position::Logical(dpi::LogicalPosition::new( | ||
bounds.origin.x.into(), | ||
bounds.origin.y.into(), | ||
)), | ||
}) | ||
.unwrap(); | ||
}; | ||
|
||
None | ||
} | ||
|
||
fn paint( | ||
&mut self, | ||
_: Option<&GlobalElementId>, | ||
bounds: Bounds<Pixels>, | ||
layout_state: &mut Self::RequestLayoutState, | ||
hitbox: &mut Self::PrepaintState, | ||
cx: &mut WindowContext, | ||
) { | ||
} | ||
} |