use async_channel::Sender;
use bevy::prelude::*;
use bevy_remote::BrpMessage;
use raw_window_handle::RawWindowHandle;
use crate::browser_process::client_handler::IpcEventRaw;
use crate::browser_process::display_handler::SystemCursorIconSenderInner;
use crate::browser_process::localhost::Requester;
#[allow(deprecated)]
pub struct SendRawWindowHandle(pub RawWindowHandle);
unsafe impl Send for SendRawWindowHandle {}
unsafe impl Sync for SendRawWindowHandle {}
#[allow(dead_code)]
pub enum CefCommand {
CreateBrowser {
webview: Entity,
uri: String,
webview_size: Vec2,
requester: Requester,
ipc_event_sender: Sender<IpcEventRaw>,
brp_sender: Sender<BrpMessage>,
system_cursor_icon_sender: SystemCursorIconSenderInner,
initialize_scripts: Vec<String>,
window_handle: Option<SendRawWindowHandle>,
},
Close { entity: Entity },
Navigate { entity: Entity, url: String },
GoBack { entity: Entity },
GoForward { entity: Entity },
ReloadWebview { entity: Entity },
Resize { entity: Entity, size: Vec2 },
SendMouseMove {
webview: Entity,
buttons: Vec<MouseButton>,
position: Vec2,
mouse_leave: bool,
},
SendMouseClick {
webview: Entity,
position: Vec2,
button: PointerButton,
mouse_up: bool,
},
SendMouseWheel {
webview: Entity,
position: Vec2,
delta: Vec2,
},
SendKey {
webview: Entity,
event: cef::KeyEvent,
},
EmitEvent {
webview: Entity,
id: String,
event: serde_json::Value,
},
ShowDevTool { webview: Entity },
CloseDevTools { webview: Entity },
SetZoomLevel { webview: Entity, zoom_level: f64 },
SetAudioMuted { webview: Entity, muted: bool },
Reload,
SetImeComposition {
text: String,
cursor_utf16: Option<u32>,
},
ImeCancelComposition,
ImeFinishComposition { keep_selection: bool },
SetImeCommitText { text: String },
}
#[derive(Resource, Clone)]
pub struct BrowsersProxy {
tx: Sender<CefCommand>,
}
impl BrowsersProxy {
pub fn new(tx: Sender<CefCommand>) -> Self {
Self { tx }
}
pub fn is_empty(&self) -> bool {
self.tx.is_empty()
}
pub fn sender(&self) -> &Sender<CefCommand> {
&self.tx
}
#[allow(clippy::too_many_arguments, deprecated)]
pub fn create_browser(
&self,
webview: Entity,
uri: &str,
webview_size: Vec2,
requester: Requester,
ipc_event_sender: Sender<IpcEventRaw>,
brp_sender: Sender<BrpMessage>,
system_cursor_icon_sender: SystemCursorIconSenderInner,
initialize_scripts: &[String],
window_handle: Option<RawWindowHandle>,
) {
let _ = self.tx.send_blocking(CefCommand::CreateBrowser {
webview,
uri: uri.to_owned(),
webview_size,
requester,
ipc_event_sender,
brp_sender,
system_cursor_icon_sender,
initialize_scripts: initialize_scripts.to_vec(),
window_handle: window_handle.map(SendRawWindowHandle),
});
}
pub fn close(&self, entity: &Entity) {
let _ = self.tx.send_blocking(CefCommand::Close { entity: *entity });
}
pub fn navigate(&self, entity: &Entity, url: &str) {
let _ = self.tx.send_blocking(CefCommand::Navigate {
entity: *entity,
url: url.to_owned(),
});
}
pub fn go_back(&self, entity: &Entity) {
let _ = self
.tx
.send_blocking(CefCommand::GoBack { entity: *entity });
}
pub fn go_forward(&self, entity: &Entity) {
let _ = self
.tx
.send_blocking(CefCommand::GoForward { entity: *entity });
}
pub fn reload_webview(&self, entity: &Entity) {
let _ = self
.tx
.send_blocking(CefCommand::ReloadWebview { entity: *entity });
}
pub fn resize(&self, entity: &Entity, size: Vec2) {
let _ = self.tx.send_blocking(CefCommand::Resize {
entity: *entity,
size,
});
}
pub fn send_mouse_move(
&self,
webview: &Entity,
buttons: &[MouseButton],
position: Vec2,
mouse_leave: bool,
) {
let _ = self.tx.send_blocking(CefCommand::SendMouseMove {
webview: *webview,
buttons: buttons.to_vec(),
position,
mouse_leave,
});
}
pub fn send_mouse_click(
&self,
webview: &Entity,
position: Vec2,
button: PointerButton,
mouse_up: bool,
) {
let _ = self.tx.send_blocking(CefCommand::SendMouseClick {
webview: *webview,
position,
button,
mouse_up,
});
}
pub fn send_mouse_wheel(&self, webview: &Entity, position: Vec2, delta: Vec2) {
let _ = self.tx.send_blocking(CefCommand::SendMouseWheel {
webview: *webview,
position,
delta,
});
}
pub fn send_key(&self, webview: &Entity, event: cef::KeyEvent) {
let _ = self.tx.send_blocking(CefCommand::SendKey {
webview: *webview,
event,
});
}
pub fn emit_event(&self, webview: &Entity, id: impl Into<String>, event: &serde_json::Value) {
let _ = self.tx.send_blocking(CefCommand::EmitEvent {
webview: *webview,
id: id.into(),
event: event.clone(),
});
}
pub fn show_devtool(&self, webview: &Entity) {
let _ = self
.tx
.send_blocking(CefCommand::ShowDevTool { webview: *webview });
}
pub fn close_devtools(&self, webview: &Entity) {
let _ = self
.tx
.send_blocking(CefCommand::CloseDevTools { webview: *webview });
}
pub fn set_zoom_level(&self, webview: &Entity, zoom_level: f64) {
let _ = self.tx.send_blocking(CefCommand::SetZoomLevel {
webview: *webview,
zoom_level,
});
}
pub fn set_audio_muted(&self, webview: &Entity, muted: bool) {
let _ = self.tx.send_blocking(CefCommand::SetAudioMuted {
webview: *webview,
muted,
});
}
pub fn reload(&self) {
let _ = self.tx.send_blocking(CefCommand::Reload);
}
pub fn set_ime_composition(&self, text: &str, cursor_utf16: Option<u32>) {
let _ = self.tx.send_blocking(CefCommand::SetImeComposition {
text: text.to_owned(),
cursor_utf16,
});
}
pub fn ime_cancel_composition(&self) {
let _ = self.tx.send_blocking(CefCommand::ImeCancelComposition);
}
pub fn ime_finish_composition(&self, keep_selection: bool) {
let _ = self
.tx
.send_blocking(CefCommand::ImeFinishComposition { keep_selection });
}
pub fn set_ime_commit_text(&self, text: &str) {
let _ = self.tx.send_blocking(CefCommand::SetImeCommitText {
text: text.to_owned(),
});
}
}