SimplePanoramaViewer/src/main.rs

85 lines
2.4 KiB
Rust
Raw Normal View History

2022-09-05 12:37:55 +00:00
use anyhow::{anyhow, Result};
use log::info;
2022-09-05 12:16:10 +00:00
use rfd::FileDialog;
use rust_embed::RustEmbed;
2022-09-05 12:37:55 +00:00
use simplelog::{ConfigBuilder, ColorChoice, LevelFilter, TermLogger, TerminalMode};
2022-09-05 12:16:10 +00:00
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,
2022-09-05 12:37:55 +00:00
ConfigBuilder::new()
.set_time_offset_to_local().unwrap()
.build(),
2022-09-05 12:16:10 +00:00
TerminalMode::Mixed,
ColorChoice::Auto,
)?;
let img_path = if let Some(img_path) = std::env::args_os().skip(1).next() {
PathBuf::from(img_path)
} else {
2022-09-05 12:37:55 +00:00
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()
};
2022-09-05 12:16:10 +00:00
FileDialog::new()
2022-09-05 12:37:55 +00:00
.add_filter(
"Images",
&[
"jpg", "JPG", "jpeg", "pjpeg", "pjpg", "PJPG", "webp", "avif",
],
)
.set_directory(dir)
2022-09-05 12:16:10 +00:00
.pick_file()
.ok_or_else(|| anyhow!("No file"))?
};
2022-09-05 12:37:55 +00:00
info!("Open `{}`", img_path.display());
2022-09-05 12:16:10 +00:00
let img_data = std::fs::read(img_path)?;
let img_base_64 = base64::encode(img_data);
2022-09-05 12:37:55 +00:00
info!("Generate HTML");
2022-09-05 12:16:10 +00:00
let html = std::str::from_utf8(Embed::get("index.html").unwrap().data.as_ref())?
.replace("__BASE_64_IMG__", &img_base_64);
2022-09-05 12:37:55 +00:00
info!("Create webview");
2022-09-05 12:16:10 +00:00
let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_title("Simple panorama viewer")
2022-09-05 12:37:55 +00:00
.with_maximized(true)
2022-09-05 12:16:10 +00:00
.build(&event_loop)?;
2022-09-05 12:37:55 +00:00
let _webview = WebViewBuilder::new(window)?.with_html(html)?.build()?;
2022-09-05 12:16:10 +00:00
2022-09-05 12:37:55 +00:00
info!("Event loop");
2022-09-05 12:16:10 +00:00
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
2022-09-05 12:37:55 +00:00
Event::NewEvents(StartCause::Init) => info!("Panorama open"),
2022-09-05 12:16:10 +00:00
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
2022-09-05 12:37:55 +00:00
} => {
info!("Close");
*control_flow = ControlFlow::Exit
}
2022-09-05 12:16:10 +00:00
_ => (),
}
});
}