runner with window size

This commit is contained in:
Tim-Paik 2022-01-30 13:04:26 +08:00
parent acfb6b6c6b
commit ceae4590e7
Signed by: Tim-Paik
GPG Key ID: DC36A050DB42566D
2 changed files with 59 additions and 56 deletions

View File

@ -38,12 +38,13 @@ pub struct Data {
fs: Dir, fs: Dir,
} }
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Copy, Clone, Debug)]
pub enum WindowSize { pub enum WindowSize {
Large, Large,
Medium, Medium,
Small, Small,
Fixed(f64, f64), Fixed { width: f64, height: f64 },
Scale { factor: f64 },
} }
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
@ -68,7 +69,7 @@ pub struct Config {
pub initialization_script: Option<String>, pub initialization_script: Option<String>,
} }
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Icon { pub struct Icon {
pub rgba: Vec<u8>, pub rgba: Vec<u8>,
pub width: u32, pub width: u32,
@ -221,15 +222,15 @@ impl Data {
} }
pub fn pack<P: AsRef<path::Path>>(config_path: P) -> Result<()> { pub fn pack<P: AsRef<path::Path>>(config_path: P) -> Result<()> {
let config_path: &Path = config_path.as_ref().clone(); let config_path = config_path.as_ref().canonicalize()?;
let config: Config = toml::from_str(fs::read_to_string(config_path)?.as_str())?; let config: Config = toml::from_str(fs::read_to_string(&config_path)?.as_str())?;
let source = &match config_path.parent() { let source = match config_path.parent() {
Some(path) => path.join(&config.source).canonicalize()?, Some(path) => path.join(&config.source).canonicalize()?,
None => config.source.canonicalize()?, None => config.source.canonicalize()?,
}; };
let target = &match config_path.parent() { let target = match config_path.parent() {
Some(path) => path.join(&config.target).canonicalize()?, Some(path) => normalize_path(&path.join(&config.target)),
None => config.target.canonicalize()?, None => normalize_path(&config.target),
}; };
fs::write( fs::write(
target, target,
@ -346,9 +347,9 @@ impl Default for Config {
impl Config { impl Config {
pub fn window_attr(&self) -> Result<WindowAttr> { pub fn window_attr(&self) -> Result<WindowAttr> {
Ok(WindowAttr { Ok(WindowAttr {
inner_size: self.inner_size.clone(), inner_size: self.inner_size,
min_inner_size: self.min_inner_size.clone(), min_inner_size: self.min_inner_size,
max_inner_size: self.max_inner_size.clone(), max_inner_size: self.max_inner_size,
position: None, position: None,
resizable: self.resizable, resizable: self.resizable,
fullscreen: self.fullscreen, fullscreen: self.fullscreen,
@ -359,7 +360,7 @@ impl Config {
decorations: self.decorations, decorations: self.decorations,
always_on_top: self.always_on_top, always_on_top: self.always_on_top,
window_icon: match &self.window_icon { window_icon: match &self.window_icon {
Some(path) => Some(load_icon(&path.as_path())?), Some(path) => Some(load_icon(path.as_path())?),
None => None, None => None,
}, },
}) })
@ -388,20 +389,13 @@ pub fn pack<P: AsRef<path::Path>>(config: P) -> Result<()> {
} }
fn load_icon(path: &Path) -> Result<Icon> { fn load_icon(path: &Path) -> Result<Icon> {
let (icon_rgba, icon_width, icon_height) = { let image = image::open(path)
let image = match image::open(path) { .map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
Ok(img) => img, .to_rgba8();
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)
};
Ok(Icon { Ok(Icon {
rgba: icon_rgba, width: image.dimensions().0,
width: icon_width, height: image.dimensions().1,
height: icon_height, rgba: image.into_raw(),
}) })
} }

View File

@ -1,7 +1,8 @@
use wry::{ use wry::{
application::{ application::{
event::{Event, StartCause, WindowEvent}, dpi::{PhysicalSize, Size},
event_loop::{self, ControlFlow, EventLoop}, event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::{Fullscreen, Icon, Window, WindowBuilder}, window::{Fullscreen, Icon, Window, WindowBuilder},
}, },
webview::{RpcRequest, WebViewBuilder}, webview::{RpcRequest, WebViewBuilder},
@ -59,41 +60,25 @@ fn main() -> wry::Result<()> {
)?)), )?)),
None => window_builder, None => window_builder,
}; };
let monitor = event_loop let monitor_size = event_loop
.primary_monitor() .primary_monitor()
.unwrap_or_else(|| event_loop.available_monitors().next().unwrap()); .unwrap_or_else(|| {
dbg!( event_loop
monitor.size(), .available_monitors()
monitor.name(), .next()
monitor.position(), .expect("no monitor found")
monitor.scale_factor(), })
monitor.video_modes().collect::<Vec<_>>() .size();
);
let window_builder = match res.window_attr.inner_size { let window_builder = match res.window_attr.inner_size {
Some(size) => match size { Some(size) => window_builder.with_inner_size(get_size(size, monitor_size)),
data::WindowSize::Large => todo!(),
data::WindowSize::Medium => todo!(),
data::WindowSize::Small => todo!(),
data::WindowSize::Fixed(width, height) => todo!(),
},
None => window_builder, None => window_builder,
}; };
let window_builder = match res.window_attr.max_inner_size { let window_builder = match res.window_attr.max_inner_size {
Some(size) => match size { Some(size) => window_builder.with_max_inner_size(get_size(size, monitor_size)),
data::WindowSize::Large => todo!(),
data::WindowSize::Medium => todo!(),
data::WindowSize::Small => todo!(),
data::WindowSize::Fixed(width, height) => todo!(),
},
None => window_builder, None => window_builder,
}; };
let window_builder = match res.window_attr.min_inner_size { let window_builder = match res.window_attr.min_inner_size {
Some(size) => match size { Some(size) => window_builder.with_min_inner_size(get_size(size, monitor_size)),
data::WindowSize::Large => todo!(),
data::WindowSize::Medium => todo!(),
data::WindowSize::Small => todo!(),
data::WindowSize::Fixed(width, height) => todo!(),
},
None => window_builder, None => window_builder,
}; };
let window = window_builder.build(&event_loop)?; let window = window_builder.build(&event_loop)?;
@ -102,7 +87,7 @@ fn main() -> wry::Result<()> {
let url = res.webview_attr.url.clone(); let url = res.webview_attr.url.clone();
let webview_builder = match url { let webview_builder = match url {
Some(url) => { Some(url) => {
if url.starts_with("/") { if url.starts_with('/') {
webview_builder.with_url(&custom_protocol_uri(PROTOCOL, &url))? webview_builder.with_url(&custom_protocol_uri(PROTOCOL, &url))?
} else { } else {
webview_builder.with_url(&url)? webview_builder.with_url(&url)?
@ -161,6 +146,7 @@ fn main() -> wry::Result<()> {
*control_flow = ControlFlow::Wait; *control_flow = ControlFlow::Wait;
match event { match event {
Event::UserEvent(event) => println!("user event: {:#?}", event),
Event::WindowEvent { Event::WindowEvent {
event: WindowEvent::CloseRequested, event: WindowEvent::CloseRequested,
.. ..
@ -169,3 +155,26 @@ fn main() -> wry::Result<()> {
} }
}); });
} }
fn get_size(size: data::WindowSize, monitor_size: PhysicalSize<u32>) -> 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))
}