Merge pull request #68 from hecrj/feature/application-settings

Allow `Application` configuration with `Settings`
This commit is contained in:
Héctor Ramón 2019-11-25 15:00:53 +01:00 committed by GitHub
commit 84874ac5dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 98 additions and 19 deletions

View file

@ -1,4 +1,4 @@
use crate::{Command, Element};
use crate::{Command, Element, Settings};
/// An interactive cross-platform application.
///
@ -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)]
@ -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();

View file

@ -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;

View file

@ -1,4 +1,4 @@
use crate::{Application, Command, Element};
use crate::{Application, Command, Element, Settings};
/// A sandboxed [`Application`].
///
@ -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)]
@ -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)
}
}

43
src/settings.rs Normal file
View file

@ -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,
}
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,
},
}
}
}