Skip to content

Commit

Permalink
feat: min_width + min_height and max_width + max_height window modifi…
Browse files Browse the repository at this point in the history
…ers (#331)
  • Loading branch information
marc2332 authored Oct 15, 2023
1 parent c17af3b commit c128ca5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
60 changes: 47 additions & 13 deletions crates/renderer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ pub struct WindowConfig<T: Clone> {
pub width: f64,
/// Height of the window.
pub height: f64,
/// Minimum width of the Window.
pub min_width: Option<f64>,
/// Minimum height of the window.
pub min_height: Option<f64>,
/// Maximum width of the Window.
pub max_width: Option<f64>,
/// Maximum height of the window.
pub max_height: Option<f64>,
/// Enable Window decorations.
pub decorations: bool,
/// Title for the Window.
Expand All @@ -29,17 +37,7 @@ pub struct WindowConfig<T: Clone> {

impl<T: Clone> Default for WindowConfig<T> {
fn default() -> Self {
Self {
width: 600.0,
height: 600.0,
decorations: true,
title: "Freya app",
transparent: false,
state: None,
background: Color::WHITE,
on_setup: None,
on_exit: None,
}
LaunchConfigBuilder::default().build().window
}
}

Expand All @@ -63,6 +61,10 @@ pub type WindowCallback = Arc<Box<fn(&mut Window)>>;
pub struct LaunchConfigBuilder<'a, T> {
pub width: f64,
pub height: f64,
pub min_width: Option<f64>,
pub min_height: Option<f64>,
pub max_width: Option<f64>,
pub max_height: Option<f64>,
pub decorations: bool,
pub title: &'static str,
pub transparent: bool,
Expand All @@ -76,8 +78,12 @@ pub struct LaunchConfigBuilder<'a, T> {
impl<T> Default for LaunchConfigBuilder<'_, T> {
fn default() -> Self {
Self {
width: 350.0,
height: 350.0,
width: 600.0,
height: 600.0,
min_width: None,
min_height: None,
max_height: None,
max_width: None,
decorations: true,
title: "Freya app",
transparent: false,
Expand All @@ -103,6 +109,30 @@ impl<'a, T: Clone> LaunchConfigBuilder<'a, T> {
self
}

/// Specify a minimum Window width.
pub fn with_min_width(mut self, min_width: f64) -> Self {
self.min_width = Some(min_width);
self
}

/// Specify a minimum Window height.
pub fn with_min_height(mut self, min_height: f64) -> Self {
self.min_height = Some(min_height);
self
}

/// Specify a maximum Window width.
pub fn with_max_width(mut self, max_width: f64) -> Self {
self.max_width = Some(max_width);
self
}

/// Specify a maximum Window height.
pub fn with_max_height(mut self, max_height: f64) -> Self {
self.max_height = Some(max_height);
self
}

/// Whether the Window will have decorations or not.
pub fn with_decorations(mut self, decorations: bool) -> Self {
self.decorations = decorations;
Expand Down Expand Up @@ -157,6 +187,10 @@ impl<'a, T: Clone> LaunchConfigBuilder<'a, T> {
window: WindowConfig {
width: self.width,
height: self.height,
min_width: self.min_width,
min_height: self.min_height,
max_width: self.max_width,
max_height: self.max_height,
title: self.title,
decorations: self.decorations,
transparent: self.transparent,
Expand Down
10 changes: 9 additions & 1 deletion crates/renderer/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<T: Clone> WindowEnv<T> {
window_config: WindowConfig<T>,
event_loop: &EventLoop<EventMessage>,
) -> Self {
let window_builder = WindowBuilder::new()
let mut window_builder = WindowBuilder::new()
.with_visible(false)
.with_title(window_config.title)
.with_decorations(window_config.decorations)
Expand All @@ -62,6 +62,14 @@ impl<T: Clone> WindowEnv<T> {
window_config.height,
));

if let Some(min_size) = window_config.min_width.zip(window_config.min_height) {
window_builder = window_builder.with_min_inner_size(LogicalSize::<f64>::from(min_size))
}

if let Some(max_size) = window_config.max_width.zip(window_config.max_height) {
window_builder = window_builder.with_max_inner_size(LogicalSize::<f64>::from(max_size))
}

let template = ConfigTemplateBuilder::new()
.with_alpha_size(8)
.with_transparency(window_config.transparent);
Expand Down

0 comments on commit c128ca5

Please sign in to comment.