From ceae4590e7bca0a1572e32b1443d702444168446 Mon Sep 17 00:00:00 2001 From: Tim-Paik Date: Sun, 30 Jan 2022 13:04:26 +0800 Subject: [PATCH] runner with window size --- neutauri_runtime/src/data.rs | 46 +++++++++++------------- neutauri_runtime/src/main.rs | 69 ++++++++++++++++++++---------------- 2 files changed, 59 insertions(+), 56 deletions(-) diff --git a/neutauri_runtime/src/data.rs b/neutauri_runtime/src/data.rs index 2ddea1a..4e120eb 100644 --- a/neutauri_runtime/src/data.rs +++ b/neutauri_runtime/src/data.rs @@ -38,12 +38,13 @@ pub struct Data { fs: Dir, } -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Copy, Clone, Debug)] pub enum WindowSize { Large, Medium, Small, - Fixed(f64, f64), + Fixed { width: f64, height: f64 }, + Scale { factor: f64 }, } #[derive(Serialize, Deserialize, Clone, Debug)] @@ -68,7 +69,7 @@ pub struct Config { pub initialization_script: Option, } -#[derive(Serialize, Deserialize, Clone, Debug)] +#[derive(Serialize, Deserialize, Clone, Debug, Default)] pub struct Icon { pub rgba: Vec, pub width: u32, @@ -221,15 +222,15 @@ impl Data { } pub fn pack>(config_path: P) -> Result<()> { - let config_path: &Path = config_path.as_ref().clone(); - let config: Config = toml::from_str(fs::read_to_string(config_path)?.as_str())?; - let source = &match config_path.parent() { + let config_path = config_path.as_ref().canonicalize()?; + let config: Config = toml::from_str(fs::read_to_string(&config_path)?.as_str())?; + let source = match config_path.parent() { Some(path) => path.join(&config.source).canonicalize()?, None => config.source.canonicalize()?, }; - let target = &match config_path.parent() { - Some(path) => path.join(&config.target).canonicalize()?, - None => config.target.canonicalize()?, + let target = match config_path.parent() { + Some(path) => normalize_path(&path.join(&config.target)), + None => normalize_path(&config.target), }; fs::write( target, @@ -346,9 +347,9 @@ impl Default for Config { impl Config { pub fn window_attr(&self) -> Result { Ok(WindowAttr { - inner_size: self.inner_size.clone(), - min_inner_size: self.min_inner_size.clone(), - max_inner_size: self.max_inner_size.clone(), + inner_size: self.inner_size, + min_inner_size: self.min_inner_size, + max_inner_size: self.max_inner_size, position: None, resizable: self.resizable, fullscreen: self.fullscreen, @@ -359,7 +360,7 @@ impl Config { decorations: self.decorations, always_on_top: self.always_on_top, window_icon: match &self.window_icon { - Some(path) => Some(load_icon(&path.as_path())?), + Some(path) => Some(load_icon(path.as_path())?), None => None, }, }) @@ -388,20 +389,13 @@ pub fn pack>(config: P) -> Result<()> { } fn load_icon(path: &Path) -> Result { - let (icon_rgba, icon_width, icon_height) = { - let image = match image::open(path) { - Ok(img) => img, - Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, e)), - } - .into_rgba8(); - let (width, height) = image.dimensions(); - let rgba = image.into_raw(); - (rgba, width, height) - }; + let image = image::open(path) + .map_err(|e| io::Error::new(io::ErrorKind::Other, e))? + .to_rgba8(); Ok(Icon { - rgba: icon_rgba, - width: icon_width, - height: icon_height, + width: image.dimensions().0, + height: image.dimensions().1, + rgba: image.into_raw(), }) } diff --git a/neutauri_runtime/src/main.rs b/neutauri_runtime/src/main.rs index 0ef8efc..f5ed74f 100644 --- a/neutauri_runtime/src/main.rs +++ b/neutauri_runtime/src/main.rs @@ -1,7 +1,8 @@ use wry::{ application::{ - event::{Event, StartCause, WindowEvent}, - event_loop::{self, ControlFlow, EventLoop}, + dpi::{PhysicalSize, Size}, + event::{Event, WindowEvent}, + event_loop::{ControlFlow, EventLoop}, window::{Fullscreen, Icon, Window, WindowBuilder}, }, webview::{RpcRequest, WebViewBuilder}, @@ -59,41 +60,25 @@ fn main() -> wry::Result<()> { )?)), None => window_builder, }; - let monitor = event_loop + let monitor_size = event_loop .primary_monitor() - .unwrap_or_else(|| event_loop.available_monitors().next().unwrap()); - dbg!( - monitor.size(), - monitor.name(), - monitor.position(), - monitor.scale_factor(), - monitor.video_modes().collect::>() - ); + .unwrap_or_else(|| { + event_loop + .available_monitors() + .next() + .expect("no monitor found") + }) + .size(); let window_builder = match res.window_attr.inner_size { - Some(size) => match size { - data::WindowSize::Large => todo!(), - data::WindowSize::Medium => todo!(), - data::WindowSize::Small => todo!(), - data::WindowSize::Fixed(width, height) => todo!(), - }, + Some(size) => window_builder.with_inner_size(get_size(size, monitor_size)), None => window_builder, }; let window_builder = match res.window_attr.max_inner_size { - Some(size) => match size { - data::WindowSize::Large => todo!(), - data::WindowSize::Medium => todo!(), - data::WindowSize::Small => todo!(), - data::WindowSize::Fixed(width, height) => todo!(), - }, + Some(size) => window_builder.with_max_inner_size(get_size(size, monitor_size)), None => window_builder, }; let window_builder = match res.window_attr.min_inner_size { - Some(size) => match size { - data::WindowSize::Large => todo!(), - data::WindowSize::Medium => todo!(), - data::WindowSize::Small => todo!(), - data::WindowSize::Fixed(width, height) => todo!(), - }, + Some(size) => window_builder.with_min_inner_size(get_size(size, monitor_size)), None => window_builder, }; let window = window_builder.build(&event_loop)?; @@ -102,7 +87,7 @@ fn main() -> wry::Result<()> { let url = res.webview_attr.url.clone(); let webview_builder = match url { Some(url) => { - if url.starts_with("/") { + if url.starts_with('/') { webview_builder.with_url(&custom_protocol_uri(PROTOCOL, &url))? } else { webview_builder.with_url(&url)? @@ -161,6 +146,7 @@ fn main() -> wry::Result<()> { *control_flow = ControlFlow::Wait; match event { + Event::UserEvent(event) => println!("user event: {:#?}", event), Event::WindowEvent { event: WindowEvent::CloseRequested, .. @@ -169,3 +155,26 @@ fn main() -> wry::Result<()> { } }); } + +fn get_size(size: data::WindowSize, monitor_size: PhysicalSize) -> Size { + let (width, height) = match size { + data::WindowSize::Large => ( + monitor_size.width as f64 * 0.7, + monitor_size.height as f64 * 0.7, + ), + data::WindowSize::Medium => ( + monitor_size.width as f64 * 0.6, + monitor_size.height as f64 * 0.6, + ), + data::WindowSize::Small => ( + monitor_size.width as f64 * 0.5, + monitor_size.height as f64 * 0.5, + ), + data::WindowSize::Fixed { width, height } => (width, height), + data::WindowSize::Scale { factor } => ( + monitor_size.width as f64 * factor, + monitor_size.height as f64 * factor, + ), + }; + Size::Physical(PhysicalSize::new(width as u32, height as u32)) +}