use html_view_shared::{BehaviourOptions, EnvironmentOptions, ViewerContent, WindowOptions};
#[derive(Debug, Clone)]
pub struct ViewerOptions {
pub content: ViewerContent,
pub window: WindowOptions,
pub behaviour: BehaviourOptions,
pub environment: EnvironmentOptions,
pub dialog: html_view_shared::DialogOptions,
pub wait: ViewerWaitMode,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ViewerWaitMode {
Blocking,
NonBlocking,
}
impl ViewerOptions {
pub fn inline_html<S: Into<String>>(html: S) -> Self {
Self {
content: ViewerContent::InlineHtml {
html: html.into(),
base_dir: None,
},
window: WindowOptions::default(),
behaviour: BehaviourOptions::default(),
environment: EnvironmentOptions::default(),
dialog: html_view_shared::DialogOptions::default(),
wait: ViewerWaitMode::Blocking,
}
}
pub fn local_file(path: std::path::PathBuf) -> Self {
Self {
content: ViewerContent::LocalFile { path },
window: WindowOptions::default(),
behaviour: BehaviourOptions::default(),
environment: EnvironmentOptions::default(),
dialog: html_view_shared::DialogOptions::default(),
wait: ViewerWaitMode::Blocking,
}
}
pub fn app_dir(root: std::path::PathBuf) -> Self {
Self {
content: ViewerContent::AppDir { root, entry: None },
window: WindowOptions::default(),
behaviour: BehaviourOptions::default(),
environment: EnvironmentOptions::default(),
dialog: html_view_shared::DialogOptions::default(),
wait: ViewerWaitMode::Blocking,
}
}
pub fn remote_url(url: url::Url) -> Self {
Self {
content: ViewerContent::RemoteUrl { url },
window: WindowOptions::default(),
behaviour: BehaviourOptions {
allow_remote_content: true,
..Default::default()
},
environment: EnvironmentOptions::default(),
dialog: html_view_shared::DialogOptions::default(),
wait: ViewerWaitMode::Blocking,
}
}
#[allow(clippy::new_ret_no_self)]
pub fn new() -> ViewerOptionsBuilder {
ViewerOptionsBuilder::default()
}
}
#[derive(Default)]
pub struct ViewerOptionsBuilder {
options: ViewerOptions,
}
impl ViewerOptionsBuilder {
pub fn content(mut self, content: ViewerContent) -> Self {
self.options.content = content;
self
}
pub fn title<S: Into<String>>(mut self, title: S) -> Self {
self.options.window.title = Some(title.into());
self
}
pub fn size(mut self, width: u32, height: u32) -> Self {
self.options.window.width = Some(width);
self.options.window.height = Some(height);
self
}
pub fn position(mut self, x: i32, y: i32) -> Self {
self.options.window.x = Some(x);
self.options.window.y = Some(y);
self
}
pub fn width(mut self, width: u32) -> Self {
self.options.window.width = Some(width);
self
}
pub fn height(mut self, height: u32) -> Self {
self.options.window.height = Some(height);
self
}
pub fn x(mut self, x: i32) -> Self {
self.options.window.x = Some(x);
self
}
pub fn y(mut self, y: i32) -> Self {
self.options.window.y = Some(y);
self
}
pub fn no_decorations(mut self) -> Self {
self.options.window.decorations = false;
self
}
pub fn transparent(mut self) -> Self {
self.options.window.transparent = true;
self
}
pub fn always_on_top(mut self) -> Self {
self.options.window.always_on_top = true;
self
}
pub fn devtools(mut self) -> Self {
self.options.behaviour.enable_devtools = true;
self
}
pub fn allow_navigation(mut self) -> Self {
self.options.behaviour.allow_external_navigation = true;
self
}
pub fn timeout(mut self, seconds: u64) -> Self {
self.options.environment.timeout_seconds = Some(seconds);
self
}
pub fn working_dir(mut self, dir: std::path::PathBuf) -> Self {
self.options.environment.working_dir = Some(dir);
self
}
pub fn allow_remote_content(mut self) -> Self {
self.options.behaviour.allow_remote_content = true;
self
}
pub fn allowed_domains(mut self, domains: Vec<String>) -> Self {
self.options.behaviour.allowed_domains = Some(domains);
self.options.behaviour.allow_external_navigation = true;
self
}
pub fn fixed_size(mut self) -> Self {
self.options.window.resizable = false;
self
}
pub fn non_blocking(mut self) -> Self {
self.options.wait = ViewerWaitMode::NonBlocking;
self
}
pub fn theme(mut self, theme: html_view_shared::WindowTheme) -> Self {
self.options.window.theme_enum = Some(theme);
self
}
pub fn enable_notifications(mut self) -> Self {
self.options.behaviour.allow_notifications = true;
self
}
pub fn enable_dialogs(mut self) -> Self {
self.options.dialog.allow_file_dialogs = true;
self.options.dialog.allow_message_dialogs = true;
self
}
pub fn toolbar(mut self, toolbar: html_view_shared::ToolbarOptions) -> Self {
self.options.window.toolbar = toolbar;
self
}
pub fn show(self) -> Result<crate::ViewerResult, crate::ViewerError> {
crate::open(self.options)
}
pub fn show_html<S: Into<String>>(
mut self,
html: S,
) -> Result<crate::ViewerResult, crate::ViewerError> {
self.options.content = ViewerContent::InlineHtml {
html: html.into(),
base_dir: None,
};
crate::open(self.options)
}
pub fn show_file(
mut self,
path: std::path::PathBuf,
) -> Result<crate::ViewerResult, crate::ViewerError> {
self.options.content = ViewerContent::LocalFile { path };
crate::open(self.options)
}
pub fn show_app_dir(
mut self,
root: std::path::PathBuf,
entry: Option<String>,
) -> Result<crate::ViewerResult, crate::ViewerError> {
self.options.content = ViewerContent::AppDir { root, entry };
crate::open(self.options)
}
pub fn show_url(mut self, url: url::Url) -> Result<crate::ViewerResult, crate::ViewerError> {
self.options.content = ViewerContent::RemoteUrl { url };
self.options.behaviour.allow_remote_content = true;
crate::open(self.options)
}
pub fn base_dir(mut self, dir: std::path::PathBuf) -> Self {
if let ViewerContent::InlineHtml { html, .. } = &self.options.content {
self.options.content = ViewerContent::InlineHtml {
html: html.clone(),
base_dir: Some(dir),
};
}
self
}
}
impl Default for ViewerOptions {
fn default() -> Self {
Self {
content: ViewerContent::InlineHtml {
html: String::new(),
base_dir: None,
},
window: WindowOptions::default(),
behaviour: BehaviourOptions::default(),
environment: EnvironmentOptions::default(),
dialog: html_view_shared::DialogOptions::default(),
wait: ViewerWaitMode::Blocking,
}
}
}