SimplePanoramaViewer/src/main.rs
2022-09-05 14:37:55 +02:00

84 lines
2.4 KiB
Rust

use anyhow::{anyhow, Result};
use log::info;
use rfd::FileDialog;
use rust_embed::RustEmbed;
use simplelog::{ConfigBuilder, ColorChoice, LevelFilter, TermLogger, TerminalMode};
use std::path::PathBuf;
use wry::{
application::{
event::{Event, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
},
webview::WebViewBuilder,
};
#[derive(RustEmbed)]
#[folder = "embed/"]
struct Embed;
fn main() -> Result<()> {
// Init log
TermLogger::init(
LevelFilter::Info,
ConfigBuilder::new()
.set_time_offset_to_local().unwrap()
.build(),
TerminalMode::Mixed,
ColorChoice::Auto,
)?;
let img_path = if let Some(img_path) = std::env::args_os().skip(1).next() {
PathBuf::from(img_path)
} else {
let user_dirs = directories::UserDirs::new().unwrap();
let dir = if let Some(img_dir) = user_dirs.picture_dir() {
img_dir
} else {
user_dirs.home_dir()
};
FileDialog::new()
.add_filter(
"Images",
&[
"jpg", "JPG", "jpeg", "pjpeg", "pjpg", "PJPG", "webp", "avif",
],
)
.set_directory(dir)
.pick_file()
.ok_or_else(|| anyhow!("No file"))?
};
info!("Open `{}`", img_path.display());
let img_data = std::fs::read(img_path)?;
let img_base_64 = base64::encode(img_data);
info!("Generate HTML");
let html = std::str::from_utf8(Embed::get("index.html").unwrap().data.as_ref())?
.replace("__BASE_64_IMG__", &img_base_64);
info!("Create webview");
let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_title("Simple panorama viewer")
.with_maximized(true)
.build(&event_loop)?;
let _webview = WebViewBuilder::new(window)?.with_html(html)?.build()?;
info!("Event loop");
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::NewEvents(StartCause::Init) => info!("Panorama open"),
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => {
info!("Close");
*control_flow = ControlFlow::Exit
}
_ => (),
}
});
}