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 a24dd6c
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 49 deletions.
15 changes: 10 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ egui_extras = { version = "0.25", default-features = false }

[patch.crates-io]
taffy = { git = "https://github.com/DioxusLabs/taffy", branch = "main" }
wry = { path = "../../RustroverProjects/github/wry" }

egui = { path = "../../IdeaProjects/egui/crates/egui" }
egui-wgpu = { path = "../../IdeaProjects/egui/crates/egui-wgpu" }
#wry = { path = "../../RustroverProjects/github/wry" }
#
#egui = { path = "../../IdeaProjects/egui/crates/egui" }
#egui-wgpu = { path = "../../IdeaProjects/egui/crates/egui-wgpu" }
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
63 changes: 33 additions & 30 deletions crates/egui_webview/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::fmt::Debug;
use std::sync::{Arc, Weak};

use egui::mutex::Mutex;
use egui::{ColorImage, Context, Id, Image, Sense, TextureHandle, Ui, Vec2, Widget};
use egui::{Context, Id, Image, Sense, TextureHandle, Ui, Vec2, Widget};
use serde::{Deserialize, Serialize};
use wry::raw_window_handle::{HasRawWindowHandle, RawWindowHandle};
use wry::{PageLoadEvent, ScreenshotRegion, WebView};
use wry::{PageLoadEvent, WebView};

use egui_inbox::UiInbox;

Expand All @@ -18,6 +18,7 @@ pub struct EguiWebView {
id: Id,
inbox: UiInbox<WebViewEvent>,
current_image: Option<TextureHandle>,
#[allow(dead_code)]
context: Context,

displayed_last_frame: bool,
Expand Down Expand Up @@ -87,6 +88,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 +100,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 @@ -156,30 +158,31 @@ impl EguiWebView {
}

fn take_screenshot(&mut self) {
let ctx = self.context.clone();
let tx = self.inbox.sender();

self.view
.screenshot(ScreenshotRegion::Visible, move |data| {
let ctx = ctx.clone();
let tx = tx.clone();
if let Ok(screenshot) = data {
let image = image::load_from_memory(&screenshot).unwrap();

let data = image.into_rgba8();

let handle = ctx.load_texture(
"browser_screenshot",
ColorImage::from_rgba_unmultiplied(
[data.width() as usize, data.height() as usize],
&data,
),
Default::default(),
);
tx.send(WebViewEvent::ScreenshotReceived(handle)).ok();
}
})
.ok();
// let ctx = self.context.clone();
// let tx = self.inbox.sender();

// TODO: This requires a screenshot feature in wry, https://github.com/tauri-apps/wry/pull/266
// self.view
// .screenshot(ScreenshotRegion::Visible, move |data| {
// let ctx = ctx.clone();
// let tx = tx.clone();
// if let Ok(screenshot) = data {
// let image = image::load_from_memory(&screenshot).unwrap();
//
// let data = image.into_rgba8();
//
// let handle = ctx.load_texture(
// "browser_screenshot",
// ColorImage::from_rgba_unmultiplied(
// [data.width() as usize, data.height() as usize],
// &data,
// ),
// Default::default(),
// );
// tx.send(WebViewEvent::ScreenshotReceived(handle)).ok();
// }
// })
// .ok();
}

fn send_command(&self, command: PageCommand) -> Result<(), Box<dyn Error>> {
Expand Down Expand Up @@ -236,7 +239,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 +256,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 a24dd6c

Please sign in to comment.