Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Application configuration with Settings #68

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/todos.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use iced::{
button, scrollable, text_input, Align, Application, Background, Button,
Checkbox, Color, Column, Command, Container, Element, Font,
HorizontalAlignment, Length, Row, Scrollable, Text, TextInput,
HorizontalAlignment, Length, Row, Scrollable, Settings, Text, TextInput,
};
use serde::{Deserialize, Serialize};

pub fn main() {
Todos::run()
Todos::run(Settings::default())
}

#[derive(Debug)]
Expand Down
4 changes: 2 additions & 2 deletions examples/tour.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use iced::{
button, scrollable, slider, text_input, Background, Button, Checkbox,
Color, Column, Container, Element, HorizontalAlignment, Image, Length,
Radio, Row, Sandbox, Scrollable, Slider, Text, TextInput,
Radio, Row, Sandbox, Scrollable, Settings, Slider, Text, TextInput,
};

pub fn main() {
env_logger::init();

Tour::run()
Tour::run(Settings::default())
}

pub struct Tour {
Expand Down
10 changes: 5 additions & 5 deletions src/application.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Command, Element};
use crate::{Command, Element, Settings};

/// An interactive cross-platform application.
///
Expand All @@ -19,10 +19,10 @@ use crate::{Command, Element};
/// before](index.html#overview). We just need to fill in the gaps:
///
/// ```no_run
/// use iced::{button, Application, Button, Column, Command, Element, Text};
/// use iced::{button, Application, Button, Column, Command, Element, Settings, Text};
///
/// pub fn main() {
/// Counter::run()
/// Counter::run(Settings::default())
/// }
///
/// #[derive(Default)]
Expand Down Expand Up @@ -132,12 +132,12 @@ pub trait Application: Sized {
/// It should probably be that last thing you call in your `main` function.
///
/// [`Application`]: trait.Application.html
fn run()
fn run(settings: Settings)
where
Self: 'static,
{
#[cfg(not(target_arch = "wasm32"))]
<Instance<Self> as iced_winit::Application>::run();
<Instance<Self> as iced_winit::Application>::run(settings.into());

#[cfg(target_arch = "wasm32")]
<Instance<Self> as iced_web::Application>::run();
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ mod application;
mod platform;
mod sandbox;

pub mod settings;

pub use application::Application;
pub use platform::*;
pub use sandbox::Sandbox;
pub use settings::Settings;
10 changes: 5 additions & 5 deletions src/sandbox.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Application, Command, Element};
use crate::{Application, Command, Element, Settings};

/// A sandboxed [`Application`].
///
Expand All @@ -19,10 +19,10 @@ use crate::{Application, Command, Element};
/// to remove the use of [`Command`]:
///
/// ```no_run
/// use iced::{button, Button, Column, Element, Sandbox, Text};
/// use iced::{button, Button, Column, Element, Sandbox, Settings, Text};
///
/// pub fn main() {
/// Counter::run()
/// Counter::run(Settings::default())
/// }
///
/// #[derive(Default)]
Expand Down Expand Up @@ -121,11 +121,11 @@ pub trait Sandbox {
/// It should probably be that last thing you call in your `main` function.
///
/// [`Sandbox`]: trait.Sandbox.html
fn run()
fn run(settings: Settings)
where
Self: 'static + Sized,
{
<Self as Application>::run()
<Self as Application>::run(settings)
}
}

Expand Down
43 changes: 43 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! Configure your application.

/// The settings of an application.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Settings {
/// The [`Window`] settings.
///
/// They will be ignored on the Web.
///
/// [`Window`]: struct.Window.html
pub window: Window,
}

/// The window settings of an application.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Window {
/// The size of the window.
pub size: (u32, u32),

/// Whether the window should be resizable or not.
pub resizable: bool,
}
Comment on lines +16 to +22
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the title field and fullscreen field would be nice as well. But the user should be able to dynamically change the size.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The title is meant to be dynamic, Application::title already addresses that.

I think we should research a bit before adding fullscreen support. I rarely use GUI apps that start in fullscreen mode by default.

I am also not sure if making the size dynamic, like the title, is a good idea. Applications resizing themselves can cause bad UX. I'd like to gather some use cases first.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wanted to say that the user should be able to switch between normal windows size, to maximized, or to fullscreen. I also remember reading some solutions that recommended making the window size a bit bigger than the screen, hence the fullscreen.


impl Default for Window {
fn default() -> Window {
Window {
size: (1024, 768),
resizable: true,
}
}
}

#[cfg(not(target_arch = "wasm32"))]
impl From<Settings> for iced_winit::Settings {
fn from(settings: Settings) -> iced_winit::Settings {
iced_winit::Settings {
window: iced_winit::settings::Window {
size: settings.window.size,
resizable: settings.window.resizable,
},
}
}
}
12 changes: 7 additions & 5 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
input::{keyboard, mouse},
renderer::{Target, Windowed},
Cache, Command, Container, Debug, Element, Event, Length, MouseCursor,
UserInterface,
Settings, UserInterface,
};

/// An interactive, native cross-platform application.
Expand Down Expand Up @@ -72,7 +72,7 @@ pub trait Application: Sized {
/// It should probably be that last thing you call in your `main` function.
///
/// [`Application`]: trait.Application.html
fn run()
fn run(settings: Settings)
where
Self: 'static,
{
Expand All @@ -96,13 +96,15 @@ pub trait Application: Sized {

let mut title = application.title();

// TODO: Ask for window settings and configure this properly
let (width, height) = settings.window.size;

let window = WindowBuilder::new()
.with_title(&title)
.with_inner_size(winit::dpi::LogicalSize {
width: 1280.0,
height: 1024.0,
width: f64::from(width),
height: f64::from(height),
})
.with_resizable(settings.window.resizable)
.build(&event_loop)
.expect("Open window");

Expand Down
2 changes: 2 additions & 0 deletions winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ pub use iced_native::*;
pub use winit;

pub mod conversion;
pub mod settings;

mod application;

pub use application::Application;
pub use settings::Settings;

// We disable debug capabilities on release builds unless the `debug` feature
// is explicitly enabled.
Expand Down
29 changes: 29 additions & 0 deletions winit/src/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! Configure your application.

/// The settings of an application.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Settings {
/// The [`Window`] settings
///
/// [`Window`]: struct.Window.html
pub window: Window,
}

/// The window settings of an application.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Window {
/// The size of the window.
pub size: (u32, u32),

/// Whether the window should be resizable or not.
pub resizable: bool,
}

impl Default for Window {
fn default() -> Window {
Window {
size: (1024, 768),
resizable: true,
}
}
}