#![cfg_attr(docsrs, feature(doc_cfg))]
#[cfg(not(any(feature = "linked", feature = "loaded")))]
compile_error!(
"At least one of the features `linked`, `appcore_linked` or `loaded` must be enabled."
);
#[macro_use]
pub(crate) mod config_macros;
#[macro_use]
pub(crate) mod callback_macros;
#[cfg(any(feature = "appcore_linked", feature = "loaded"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "appcore_linked", feature = "loaded"))))]
pub mod app;
pub mod bitmap;
pub mod config;
pub mod error;
pub mod event;
pub mod gpu_driver;
pub mod image_source;
pub mod key_code;
#[cfg(any(feature = "appcore_linked", feature = "loaded"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "appcore_linked", feature = "loaded"))))]
pub mod overlay;
pub mod platform;
pub mod rect;
pub mod renderer;
pub(crate) mod string;
pub mod surface;
pub mod view;
#[cfg(any(feature = "appcore_linked", feature = "loaded"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "appcore_linked", feature = "loaded"))))]
pub mod window;
pub mod javascript;
use std::{ffi::CStr, sync::Arc};
#[cfg(any(feature = "appcore_linked", feature = "loaded"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "appcore_linked", feature = "loaded"))))]
pub use app::App;
pub use config::Config;
pub use gpu_driver::GpuDriver;
#[cfg(any(feature = "appcore_linked", feature = "loaded"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "appcore_linked", feature = "loaded"))))]
pub use overlay::Overlay;
pub use rect::Rect;
pub use renderer::{Renderer, Session};
pub use surface::Surface;
pub use view::View;
#[cfg(any(feature = "appcore_linked", feature = "loaded"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "appcore_linked", feature = "loaded"))))]
pub use window::Window;
use ul_sys::library::Library as LibrarySys;
#[derive(Clone, Copy, Debug)]
pub struct Version {
pub major: u32,
pub minor: u32,
pub patch: u32,
}
impl std::fmt::Display for Version {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
}
}
#[derive(Clone)]
pub struct Library {
lib: LibrarySys,
}
impl Library {
#[cfg(any(feature = "linked", feature = "appcore_linked"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "linked", feature = "appcore_linked"))))]
pub fn linked() -> Arc<Library> {
Arc::new(Library {
lib: LibrarySys::linked(),
})
}
#[cfg(feature = "loaded")]
#[cfg_attr(docsrs, doc(cfg(feature = "loaded")))]
pub unsafe fn load() -> Result<Arc<Library>, ul_sys::library::LoadingError> {
Ok(Arc::new(Library {
lib: LibrarySys::load()?,
}))
}
#[cfg(feature = "loaded")]
#[cfg_attr(docsrs, doc(cfg(feature = "loaded")))]
pub unsafe fn load_with_appcore() -> Result<Arc<Library>, ul_sys::library::LoadingError> {
Ok(Arc::new(Library {
lib: LibrarySys::load_with_appcore()?,
}))
}
#[cfg(feature = "loaded")]
#[cfg_attr(docsrs, doc(cfg(feature = "loaded")))]
pub unsafe fn load_from<P: AsRef<::std::ffi::OsStr>>(
ultralight_path: P,
) -> Result<Arc<Library>, ul_sys::library::LoadingError> {
Ok(Arc::new(Library {
lib: LibrarySys::load_from(ultralight_path.as_ref())?,
}))
}
#[cfg(feature = "loaded")]
#[cfg_attr(docsrs, doc(cfg(feature = "loaded")))]
pub unsafe fn load_from_appcore<P>(
appcore_path: P,
) -> Result<Arc<Library>, ul_sys::library::LoadingError>
where
P: AsRef<::std::ffi::OsStr>,
{
Ok(Arc::new(Library {
lib: LibrarySys::load_from_appcore(appcore_path.as_ref())?,
}))
}
}
impl Library {
pub(crate) fn ultralight(&self) -> &ul_sys::library::Ultralight {
self.lib.ultralight()
}
#[cfg(any(feature = "appcore_linked", feature = "loaded"))]
pub(crate) fn appcore(&self) -> &ul_sys::library::AppCore {
self.lib.appcore()
}
pub fn version(&self) -> Version {
unsafe {
Version {
major: self.lib.ultralight().ulVersionMajor(),
minor: self.lib.ultralight().ulVersionMinor(),
patch: self.lib.ultralight().ulVersionPatch(),
}
}
}
pub fn webkit_version(&self) -> String {
unsafe {
let cstr_ptr = self.lib.ultralight().ulWebKitVersionString();
if cstr_ptr.is_null() {
return String::new();
}
let version_cstr = CStr::from_ptr(cstr_ptr);
version_cstr.to_string_lossy().into_owned()
}
}
}