Skip to content

Commit

Permalink
Add simple readme
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Jan 22, 2024
1 parent d37c985 commit d085118
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
7 changes: 7 additions & 0 deletions crates/egui_webview/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# egui_webview

This is a proof of concept of how a webview crate for egui could look like.
It works by taking a screenshot of the webview and rendering it as an egui texture
whenever something is shown above the webview.

For this to work this screenshot PR on wry would be required: https://github.com/tauri-apps/wry/pull/266
2 changes: 1 addition & 1 deletion crates/egui_webview/examples/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl WebBrowser {

pub fn ui(&mut self, ctx: &Context) -> bool {
let mut open = true;
Window::new(format!("Browser"))
Window::new("Browser")
.id(self.id)
.open(&mut open)
.show(ctx, |ui| {
Expand Down
9 changes: 5 additions & 4 deletions crates/egui_webview/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ impl EguiWebView {

builder = build(builder);

#[allow(clippy::arc_with_non_send_sync)]
let view_ref = Arc::new(Mutex::new(None::<Arc<WebView>>));
let view_ref_weak = view_ref.clone();
let ctx_clone = ctx.clone();
Expand All @@ -98,7 +99,7 @@ impl EguiWebView {
.with_on_page_load_handler(move |event, url| {
match event {
PageLoadEvent::Started => {
let mut guard = view_ref_weak.lock();
let guard = view_ref_weak.lock();
if let Some(view) = guard.as_ref() {
if let Err(err) = view.evaluate_script(include_str!("webview.js")) {
println!("Error loading webview script: {}", err);
Expand Down Expand Up @@ -236,7 +237,7 @@ impl EguiWebView {
let my_layer = ui.layer_id();

let is_my_layer_top =
ui.memory(|mut mem| mem.areas().top_layer_id(my_layer.order) == Some(my_layer));
ui.memory(|mem| mem.areas().top_layer_id(my_layer.order) == Some(my_layer));

if !is_my_layer_top {
//response.surrender_focus();
Expand All @@ -253,13 +254,13 @@ impl EguiWebView {

let should_display = ui.memory(|mem| (is_my_layer_top && !mem.any_popup_open()));

if (!should_display && self.displayed_last_frame) {
if !should_display && self.displayed_last_frame {
self.current_image = None;
self.take_screenshot();
}
self.displayed_last_frame = should_display;

if should_display || !self.current_image.is_some() {
if should_display || self.current_image.is_none() {
ui.ctx().memory_mut(|mem| {
let state = mem.data.get_temp_mut_or_insert_with::<GlobalWebViewState>(
Id::new(WEBVIEW_ID),
Expand Down
18 changes: 9 additions & 9 deletions crates/egui_webview/src/native_text_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ impl NativeTextField {
self.webview.view.set_visible(false);
self.webview.view.set_visible(true);
//response.egui_response.surrender_focus();
let shift_key = ui.input(|i| i.modifiers.shift);
// let shift_key = ui.input(|i| i.modifiers.shift);
//response.egui_response.surrender_focus();
ui.ctx().memory_mut(|mem| {
println!("Focus out 2");
// if shift_key {
// mem.focus_item_in_direction(FocusDirection::Previous);
// } else {
// mem.focus_item_in_direction(FocusDirection::Next);
// }
})
// ui.ctx().memory_mut(|mem| {
// println!("Focus out 2");
// // if shift_key {
// // mem.focus_item_in_direction(FocusDirection::Previous);
// // } else {
// // mem.focus_item_in_direction(FocusDirection::Next);
// // }
// })
}
Err(_) => {}
}
Expand Down

0 comments on commit d085118

Please sign in to comment.