Skip to content

Commit 84874ac

Browse files
authored
Merge pull request #68 from hecrj/feature/application-settings
Allow `Application` configuration with `Settings`
2 parents 8628591 + d70021f commit 84874ac

File tree

9 files changed

+98
-19
lines changed

9 files changed

+98
-19
lines changed

examples/todos.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use iced::{
22
button, scrollable, text_input, Align, Application, Background, Button,
33
Checkbox, Color, Column, Command, Container, Element, Font,
4-
HorizontalAlignment, Length, Row, Scrollable, Text, TextInput,
4+
HorizontalAlignment, Length, Row, Scrollable, Settings, Text, TextInput,
55
};
66
use serde::{Deserialize, Serialize};
77

88
pub fn main() {
9-
Todos::run()
9+
Todos::run(Settings::default())
1010
}
1111

1212
#[derive(Debug)]

examples/tour.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use iced::{
22
button, scrollable, slider, text_input, Background, Button, Checkbox,
33
Color, Column, Container, Element, HorizontalAlignment, Image, Length,
4-
Radio, Row, Sandbox, Scrollable, Slider, Text, TextInput,
4+
Radio, Row, Sandbox, Scrollable, Settings, Slider, Text, TextInput,
55
};
66

77
pub fn main() {
88
env_logger::init();
99

10-
Tour::run()
10+
Tour::run(Settings::default())
1111
}
1212

1313
pub struct Tour {

src/application.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Command, Element};
1+
use crate::{Command, Element, Settings};
22

33
/// An interactive cross-platform application.
44
///
@@ -19,10 +19,10 @@ use crate::{Command, Element};
1919
/// before](index.html#overview). We just need to fill in the gaps:
2020
///
2121
/// ```no_run
22-
/// use iced::{button, Application, Button, Column, Command, Element, Text};
22+
/// use iced::{button, Application, Button, Column, Command, Element, Settings, Text};
2323
///
2424
/// pub fn main() {
25-
/// Counter::run()
25+
/// Counter::run(Settings::default())
2626
/// }
2727
///
2828
/// #[derive(Default)]
@@ -132,12 +132,12 @@ pub trait Application: Sized {
132132
/// It should probably be that last thing you call in your `main` function.
133133
///
134134
/// [`Application`]: trait.Application.html
135-
fn run()
135+
fn run(settings: Settings)
136136
where
137137
Self: 'static,
138138
{
139139
#[cfg(not(target_arch = "wasm32"))]
140-
<Instance<Self> as iced_winit::Application>::run();
140+
<Instance<Self> as iced_winit::Application>::run(settings.into());
141141

142142
#[cfg(target_arch = "wasm32")]
143143
<Instance<Self> as iced_web::Application>::run();

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ mod application;
185185
mod platform;
186186
mod sandbox;
187187

188+
pub mod settings;
189+
188190
pub use application::Application;
189191
pub use platform::*;
190192
pub use sandbox::Sandbox;
193+
pub use settings::Settings;

src/sandbox.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Application, Command, Element};
1+
use crate::{Application, Command, Element, Settings};
22

33
/// A sandboxed [`Application`].
44
///
@@ -19,10 +19,10 @@ use crate::{Application, Command, Element};
1919
/// to remove the use of [`Command`]:
2020
///
2121
/// ```no_run
22-
/// use iced::{button, Button, Column, Element, Sandbox, Text};
22+
/// use iced::{button, Button, Column, Element, Sandbox, Settings, Text};
2323
///
2424
/// pub fn main() {
25-
/// Counter::run()
25+
/// Counter::run(Settings::default())
2626
/// }
2727
///
2828
/// #[derive(Default)]
@@ -121,11 +121,11 @@ pub trait Sandbox {
121121
/// It should probably be that last thing you call in your `main` function.
122122
///
123123
/// [`Sandbox`]: trait.Sandbox.html
124-
fn run()
124+
fn run(settings: Settings)
125125
where
126126
Self: 'static + Sized,
127127
{
128-
<Self as Application>::run()
128+
<Self as Application>::run(settings)
129129
}
130130
}
131131

src/settings.rs

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! Configure your application.
2+
3+
/// The settings of an application.
4+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
5+
pub struct Settings {
6+
/// The [`Window`] settings.
7+
///
8+
/// They will be ignored on the Web.
9+
///
10+
/// [`Window`]: struct.Window.html
11+
pub window: Window,
12+
}
13+
14+
/// The window settings of an application.
15+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16+
pub struct Window {
17+
/// The size of the window.
18+
pub size: (u32, u32),
19+
20+
/// Whether the window should be resizable or not.
21+
pub resizable: bool,
22+
}
23+
24+
impl Default for Window {
25+
fn default() -> Window {
26+
Window {
27+
size: (1024, 768),
28+
resizable: true,
29+
}
30+
}
31+
}
32+
33+
#[cfg(not(target_arch = "wasm32"))]
34+
impl From<Settings> for iced_winit::Settings {
35+
fn from(settings: Settings) -> iced_winit::Settings {
36+
iced_winit::Settings {
37+
window: iced_winit::settings::Window {
38+
size: settings.window.size,
39+
resizable: settings.window.resizable,
40+
},
41+
}
42+
}
43+
}

winit/src/application.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
input::{keyboard, mouse},
44
renderer::{Target, Windowed},
55
Cache, Command, Container, Debug, Element, Event, Length, MouseCursor,
6-
UserInterface,
6+
Settings, UserInterface,
77
};
88

99
/// An interactive, native cross-platform application.
@@ -72,7 +72,7 @@ pub trait Application: Sized {
7272
/// It should probably be that last thing you call in your `main` function.
7373
///
7474
/// [`Application`]: trait.Application.html
75-
fn run()
75+
fn run(settings: Settings)
7676
where
7777
Self: 'static,
7878
{
@@ -96,13 +96,15 @@ pub trait Application: Sized {
9696

9797
let mut title = application.title();
9898

99-
// TODO: Ask for window settings and configure this properly
99+
let (width, height) = settings.window.size;
100+
100101
let window = WindowBuilder::new()
101102
.with_title(&title)
102103
.with_inner_size(winit::dpi::LogicalSize {
103-
width: 1280.0,
104-
height: 1024.0,
104+
width: f64::from(width),
105+
height: f64::from(height),
105106
})
107+
.with_resizable(settings.window.resizable)
106108
.build(&event_loop)
107109
.expect("Open window");
108110

winit/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ pub use iced_native::*;
2626
pub use winit;
2727

2828
pub mod conversion;
29+
pub mod settings;
2930

3031
mod application;
3132

3233
pub use application::Application;
34+
pub use settings::Settings;
3335

3436
// We disable debug capabilities on release builds unless the `debug` feature
3537
// is explicitly enabled.

winit/src/settings.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//! Configure your application.
2+
3+
/// The settings of an application.
4+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
5+
pub struct Settings {
6+
/// The [`Window`] settings
7+
///
8+
/// [`Window`]: struct.Window.html
9+
pub window: Window,
10+
}
11+
12+
/// The window settings of an application.
13+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14+
pub struct Window {
15+
/// The size of the window.
16+
pub size: (u32, u32),
17+
18+
/// Whether the window should be resizable or not.
19+
pub resizable: bool,
20+
}
21+
22+
impl Default for Window {
23+
fn default() -> Window {
24+
Window {
25+
size: (1024, 768),
26+
resizable: true,
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)