use bevy::prelude::*;
use bevy_cef_core::prelude::{HOST_CEF, SCHEME_CEF};
use serde::{Deserialize, Serialize};
pub(crate) struct WebviewCoreComponentsPlugin;
impl Plugin for WebviewCoreComponentsPlugin {
fn build(&self, app: &mut App) {
app.register_type::<WebviewSize>()
.register_type::<WebviewSource>()
.register_type::<HostWindow>()
.register_type::<ZoomLevel>()
.register_type::<AudioMuted>()
.register_type::<PreloadScripts>();
}
}
#[derive(Component, Debug, Clone, Reflect)]
#[reflect(Component, Debug)]
#[require(WebviewSize, ZoomLevel, AudioMuted, PreloadScripts)]
pub enum WebviewSource {
Url(String),
InlineHtml(String),
}
impl WebviewSource {
pub fn new(url: impl Into<String>) -> Self {
Self::Url(url.into())
}
pub fn local(path: impl Into<String>) -> Self {
Self::Url(format!("{SCHEME_CEF}://{HOST_CEF}/{}", path.into()))
}
pub fn inline(html: impl Into<String>) -> Self {
Self::InlineHtml(html.into())
}
}
#[derive(Component, Debug, Clone)]
pub(crate) struct ResolvedWebviewUri(pub(crate) String);
#[derive(Reflect, Component, Debug, Copy, Clone, PartialEq)]
#[reflect(Component, Debug, Default)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
pub struct WebviewSize(pub Vec2);
impl Default for WebviewSize {
fn default() -> Self {
Self(Vec2::splat(800.0))
}
}
#[derive(Reflect, Component, Debug, Copy, Clone, PartialEq)]
#[reflect(Component, Debug)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serialize", reflect(Serialize, Deserialize))]
pub struct HostWindow(pub Entity);
#[derive(Reflect, Component, Debug, Copy, Clone, PartialEq, Serialize, Deserialize, Default)]
#[reflect(Component, Debug, Serialize, Deserialize, Default)]
pub struct ZoomLevel(pub f64);
#[derive(Reflect, Component, Debug, Copy, Clone, PartialEq, Default, Serialize, Deserialize)]
#[reflect(Component, Debug, Default, Serialize, Deserialize)]
pub struct AudioMuted(pub bool);
#[derive(Reflect, Component, Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[reflect(Component, Debug, Default, Serialize, Deserialize)]
pub struct PreloadScripts(pub Vec<String>);
impl<L, S> From<L> for PreloadScripts
where
L: IntoIterator<Item = S>,
S: Into<String>,
{
fn from(scripts: L) -> Self {
Self(scripts.into_iter().map(Into::into).collect())
}
}
#[derive(Component, Debug, Clone)]
pub(crate) struct WebviewSurface(pub(crate) Handle<Image>);