summaryrefslogtreecommitdiffstats
path: root/winit
diff options
context:
space:
mode:
Diffstat (limited to 'winit')
-rw-r--r--winit/Cargo.toml16
-rw-r--r--winit/README.md6
-rw-r--r--winit/src/application.rs601
-rw-r--r--winit/src/application/state.rs224
-rw-r--r--winit/src/clipboard.rs49
-rw-r--r--winit/src/conversion.rs442
-rw-r--r--winit/src/lib.rs7
-rw-r--r--winit/src/mode.rs3
-rw-r--r--winit/src/position.rs22
-rw-r--r--winit/src/proxy.rs2
-rw-r--r--winit/src/settings.rs61
-rw-r--r--winit/src/settings/windows.rs16
12 files changed, 1141 insertions, 308 deletions
diff --git a/winit/Cargo.toml b/winit/Cargo.toml
index 06e5df9a..b1192135 100644
--- a/winit/Cargo.toml
+++ b/winit/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "iced_winit"
-version = "0.1.1"
+version = "0.3.0"
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
edition = "2018"
description = "A winit runtime for Iced"
@@ -14,21 +14,25 @@ categories = ["gui"]
debug = ["iced_native/debug"]
[dependencies]
-winit = "0.22"
-window_clipboard = "0.1"
+window_clipboard = "0.2"
log = "0.4"
thiserror = "1.0"
+[dependencies.winit]
+version = "0.25"
+git = "https://github.com/iced-rs/winit"
+rev = "844485272a7412cb35cdbfac3524decdf59475ca"
+
[dependencies.iced_native]
-version = "0.2"
+version = "0.4"
path = "../native"
[dependencies.iced_graphics]
-version = "0.1"
+version = "0.2"
path = "../graphics"
[dependencies.iced_futures]
-version = "0.1"
+version = "0.3"
path = "../futures"
[target.'cfg(target_os = "windows")'.dependencies.winapi]
diff --git a/winit/README.md b/winit/README.md
index 34dec1b3..58c782c6 100644
--- a/winit/README.md
+++ b/winit/README.md
@@ -8,7 +8,9 @@
It exposes a renderer-agnostic `Application` trait that can be implemented and then run with a simple call. The use of this trait is optional. A `conversion` module is provided for users that decide to implement a custom event loop.
-![iced_winit](../docs/graphs/winit.png)
+<p align="center">
+ <img alt="The native target" src="../docs/graphs/native.png" width="80%">
+</p>
[documentation]: https://docs.rs/iced_winit
[`iced_native`]: ../native
@@ -18,7 +20,7 @@ It exposes a renderer-agnostic `Application` trait that can be implemented and t
Add `iced_winit` as a dependency in your `Cargo.toml`:
```toml
-iced_winit = "0.1"
+iced_winit = "0.3"
```
__Iced moves fast and the `master` branch can contain breaking changes!__ If
diff --git a/winit/src/application.rs b/winit/src/application.rs
index 12f92053..c43eed11 100644
--- a/winit/src/application.rs
+++ b/winit/src/application.rs
@@ -1,13 +1,23 @@
//! Create interactive, native cross-platform applications.
+mod state;
+
+pub use state::State;
+
use crate::conversion;
use crate::mouse;
use crate::{
Clipboard, Color, Command, Debug, Error, Executor, Mode, Proxy, Runtime,
Settings, Size, Subscription,
};
+
+use iced_futures::futures;
+use iced_futures::futures::channel::mpsc;
use iced_graphics::window;
-use iced_graphics::Viewport;
-use iced_native::program::{self, Program};
+use iced_native::program::Program;
+use iced_native::Menu;
+use iced_native::{Cache, UserInterface};
+
+use std::mem::ManuallyDrop;
/// An interactive, native cross-platform application.
///
@@ -15,17 +25,13 @@ use iced_native::program::{self, Program};
/// your GUI application by simply calling [`run`](#method.run). It will run in
/// its own window.
///
-/// An [`Application`](trait.Application.html) can execute asynchronous actions
-/// by returning a [`Command`](struct.Command.html) in some of its methods.
+/// An [`Application`] can execute asynchronous actions by returning a
+/// [`Command`] in some of its methods.
///
/// When using an [`Application`] with the `debug` feature enabled, a debug view
/// can be toggled by pressing `F12`.
-///
-/// [`Application`]: trait.Application.html
-pub trait Application: Program {
+pub trait Application: Program<Clipboard = Clipboard> {
/// The data needed to initialize your [`Application`].
- ///
- /// [`Application`]: trait.Application.html
type Flags;
/// Initializes the [`Application`] with the flags provided to
@@ -33,22 +39,15 @@ pub trait Application: Program {
///
/// Here is where you should return the initial state of your app.
///
- /// Additionally, you can return a [`Command`](struct.Command.html) if you
- /// need to perform some async action in the background on startup. This is
- /// useful if you want to load state from a file, perform an initial HTTP
- /// request, etc.
- ///
- /// [`Application`]: trait.Application.html
- /// [`run`]: #method.run.html
- /// [`Settings`]: ../settings/struct.Settings.html
+ /// Additionally, you can return a [`Command`] if you need to perform some
+ /// async action in the background on startup. This is useful if you want to
+ /// load state from a file, perform an initial HTTP request, etc.
fn new(flags: Self::Flags) -> (Self, Command<Self::Message>);
/// Returns the current title of the [`Application`].
///
/// This title can be dynamic! The runtime will automatically update the
/// title of your application when necessary.
- ///
- /// [`Application`]: trait.Application.html
fn title(&self) -> String;
/// Returns the event `Subscription` for the current state of the
@@ -70,8 +69,6 @@ pub trait Application: Program {
/// is returned.
///
/// By default, an application will run in windowed mode.
- ///
- /// [`Application`]: trait.Application.html
fn mode(&self) -> Mode {
Mode::Windowed
}
@@ -79,10 +76,6 @@ pub trait Application: Program {
/// Returns the background [`Color`] of the [`Application`].
///
/// By default, it returns [`Color::WHITE`].
- ///
- /// [`Color`]: struct.Color.html
- /// [`Application`]: trait.Application.html
- /// [`Color::WHITE`]: struct.Color.html#const.WHITE
fn background_color(&self) -> Color {
Color::WHITE
}
@@ -96,17 +89,27 @@ pub trait Application: Program {
/// while a scale factor of `0.5` will shrink them to half their size.
///
/// By default, it returns `1.0`.
- ///
- /// [`Application`]: trait.Application.html
fn scale_factor(&self) -> f64 {
1.0
}
+
+ /// Returns whether the [`Application`] should be terminated.
+ ///
+ /// By default, it returns `false`.
+ fn should_exit(&self) -> bool {
+ false
+ }
+
+ /// Returns the current system [`Menu`] of the [`Application`].
+ ///
+ /// By default, it returns an empty [`Menu`].
+ fn menu(&self) -> Menu<Self::Message> {
+ Menu::new()
+ }
}
/// Runs an [`Application`] with an executor, compositor, and the provided
/// settings.
-///
-/// [`Application`]: trait.Application.html
pub fn run<A, E, C>(
settings: Settings<A::Flags>,
compositor_settings: C::Settings,
@@ -116,279 +119,340 @@ where
E: Executor + 'static,
C: window::Compositor<Renderer = A::Renderer> + 'static,
{
- use winit::{
- event,
- event_loop::{ControlFlow, EventLoop},
- };
+ use futures::task;
+ use futures::Future;
+ use winit::event_loop::EventLoop;
let mut debug = Debug::new();
debug.startup_started();
let event_loop = EventLoop::with_user_event();
+
let mut runtime = {
- let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
let proxy = Proxy::new(event_loop.create_proxy());
+ let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
Runtime::new(executor, proxy)
};
- let flags = settings.flags;
- let (application, init_command) = runtime.enter(|| A::new(flags));
- runtime.spawn(init_command);
+ let (application, init_command) = {
+ let flags = settings.flags;
+
+ runtime.enter(|| A::new(flags))
+ };
let subscription = application.subscription();
- runtime.track(subscription);
- let mut title = application.title();
- let mut mode = application.mode();
- let mut background_color = application.background_color();
- let mut scale_factor = application.scale_factor();
+ runtime.spawn(init_command);
+ runtime.track(subscription);
let window = settings
.window
- .into_builder(&title, mode, event_loop.primary_monitor())
+ .into_builder(
+ &application.title(),
+ application.mode(),
+ event_loop.primary_monitor(),
+ settings.id,
+ )
+ .with_menu(Some(conversion::menu(&application.menu())))
.build(&event_loop)
.map_err(Error::WindowCreationFailed)?;
- let clipboard = Clipboard::new(&window);
- // TODO: Encode cursor availability in the type-system
- let mut cursor_position = winit::dpi::PhysicalPosition::new(-1.0, -1.0);
- let mut mouse_interaction = mouse::Interaction::default();
- let mut modifiers = winit::event::ModifiersState::default();
-
- let physical_size = window.inner_size();
- let mut viewport = Viewport::with_physical_size(
- Size::new(physical_size.width, physical_size.height),
- window.scale_factor() * scale_factor,
- );
- let mut resized = false;
+ let (compositor, renderer) = C::new(compositor_settings, Some(&window))?;
- let (mut compositor, mut renderer) = C::new(compositor_settings)?;
-
- let surface = compositor.create_surface(&window);
+ let (mut sender, receiver) = mpsc::unbounded();
- let mut swap_chain = compositor.create_swap_chain(
- &surface,
- physical_size.width,
- physical_size.height,
- );
-
- let mut state = program::State::new(
+ let mut instance = Box::pin(run_instance::<A, E, C>(
application,
- viewport.logical_size(),
- conversion::cursor_position(cursor_position, viewport.scale_factor()),
- &mut renderer,
- &mut debug,
- );
- debug.startup_finished();
+ compositor,
+ renderer,
+ runtime,
+ debug,
+ receiver,
+ window,
+ settings.exit_on_close_request,
+ ));
+
+ let mut context = task::Context::from_waker(task::noop_waker_ref());
+
+ event_loop.run(move |event, _, control_flow| {
+ use winit::event_loop::ControlFlow;
+
+ if let ControlFlow::Exit = control_flow {
+ return;
+ }
- event_loop.run(move |event, _, control_flow| match event {
- event::Event::MainEventsCleared => {
- if state.is_queue_empty() {
- return;
- }
+ let event = match event {
+ winit::event::Event::WindowEvent {
+ event:
+ winit::event::WindowEvent::ScaleFactorChanged {
+ new_inner_size,
+ ..
+ },
+ window_id,
+ } => Some(winit::event::Event::WindowEvent {
+ event: winit::event::WindowEvent::Resized(*new_inner_size),
+ window_id,
+ }),
+ _ => event.to_static(),
+ };
+
+ if let Some(event) = event {
+ sender.start_send(event).expect("Send event");
+
+ let poll = instance.as_mut().poll(&mut context);
+
+ *control_flow = match poll {
+ task::Poll::Pending => ControlFlow::Wait,
+ task::Poll::Ready(_) => ControlFlow::Exit,
+ };
+ }
+ });
+}
- let command = runtime.enter(|| {
- state.update(
- viewport.logical_size(),
- conversion::cursor_position(
- cursor_position,
- viewport.scale_factor(),
- ),
- clipboard.as_ref().map(|c| c as _),
- &mut renderer,
- &mut debug,
- )
- });
+async fn run_instance<A, E, C>(
+ mut application: A,
+ mut compositor: C,
+ mut renderer: A::Renderer,
+ mut runtime: Runtime<E, Proxy<A::Message>, A::Message>,
+ mut debug: Debug,
+ mut receiver: mpsc::UnboundedReceiver<winit::event::Event<'_, A::Message>>,
+ window: winit::window::Window,
+ exit_on_close_request: bool,
+) where
+ A: Application + 'static,
+ E: Executor + 'static,
+ C: window::Compositor<Renderer = A::Renderer> + 'static,
+{
+ use iced_futures::futures::stream::StreamExt;
+ use winit::event;
- // If the application was updated
- if let Some(command) = command {
- runtime.spawn(command);
+ let surface = compositor.create_surface(&window);
+ let mut clipboard = Clipboard::connect(&window);
+
+ let mut state = State::new(&application, &window);
+ let mut viewport_version = state.viewport_version();
+ let mut swap_chain = {
+ let physical_size = state.physical_size();
+
+ compositor.create_swap_chain(
+ &surface,
+ physical_size.width,
+ physical_size.height,
+ )
+ };
- let program = state.program();
+ let mut user_interface = ManuallyDrop::new(build_user_interface(
+ &mut application,
+ Cache::default(),
+ &mut renderer,
+ state.logical_size(),
+ &mut debug,
+ ));
- // Update subscriptions
- let subscription = program.subscription();
- runtime.track(subscription);
+ let mut primitive =
+ user_interface.draw(&mut renderer, state.cursor_position());
+ let mut mouse_interaction = mouse::Interaction::default();
- // Update window title
- let new_title = program.title();
+ let mut events = Vec::new();
+ let mut messages = Vec::new();
- if title != new_title {
- window.set_title(&new_title);
+ debug.startup_finished();
- title = new_title;
+ while let Some(event) = receiver.next().await {
+ match event {
+ event::Event::MainEventsCleared => {
+ if events.is_empty() && messages.is_empty() {
+ continue;
}
- // Update window mode
- let new_mode = program.mode();
+ debug.event_processing_started();
- if mode != new_mode {
- window.set_fullscreen(conversion::fullscreen(
- window.current_monitor(),
- new_mode,
- ));
+ let statuses = user_interface.update(
+ &events,
+ state.cursor_position(),
+ &mut renderer,
+ &mut clipboard,
+ &mut messages,
+ );
+
+ debug.event_processing_finished();
- mode = new_mode;
+ for event in events.drain(..).zip(statuses.into_iter()) {
+ runtime.broadcast(event);
}
- // Update background color
- background_color = program.background_color();
+ if !messages.is_empty() {
+ let cache =
+ ManuallyDrop::into_inner(user_interface).into_cache();
- // Update scale factor
- let new_scale_factor = program.scale_factor();
+ // Update application
+ update(
+ &mut application,
+ &mut runtime,
+ &mut debug,
+ &mut clipboard,
+ &mut messages,
+ );
- if scale_factor != new_scale_factor {
- let size = window.inner_size();
+ // Update window
+ state.synchronize(&application, &window);
- viewport = Viewport::with_physical_size(
- Size::new(size.width, size.height),
- window.scale_factor() * new_scale_factor,
- );
+ let should_exit = application.should_exit();
- // We relayout the UI with the new logical size.
- // The queue is empty, therefore this will never produce
- // a `Command`.
- //
- // TODO: Properly queue `WindowResized`
- let _ = state.update(
- viewport.logical_size(),
- conversion::cursor_position(
- cursor_position,
- viewport.scale_factor(),
- ),
- clipboard.as_ref().map(|c| c as _),
+ user_interface = ManuallyDrop::new(build_user_interface(
+ &mut application,
+ cache,
&mut renderer,
+ state.logical_size(),
&mut debug,
- );
+ ));
- scale_factor = new_scale_factor;
+ if should_exit {
+ break;
+ }
}
+
+ debug.draw_started();
+ primitive =
+ user_interface.draw(&mut renderer, state.cursor_position());
+ debug.draw_finished();
+
+ window.request_redraw();
+ }
+ event::Event::PlatformSpecific(event::PlatformSpecific::MacOS(
+ event::MacOS::ReceivedUrl(url),
+ )) => {
+ use iced_native::event;
+ events.push(iced_native::Event::PlatformSpecific(
+ event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl(
+ url,
+ )),
+ ));
+ }
+ event::Event::UserEvent(message) => {
+ messages.push(message);
}
+ event::Event::RedrawRequested(_) => {
+ let physical_size = state.physical_size();
- window.request_redraw();
- }
- event::Event::UserEvent(message) => {
- state.queue_message(message);
- }
- event::Event::RedrawRequested(_) => {
- debug.render_started();
+ if physical_size.width == 0 || physical_size.height == 0 {
+ continue;
+ }
- if resized {
- let physical_size = viewport.physical_size();
+ debug.render_started();
+ let current_viewport_version = state.viewport_version();
- swap_chain = compositor.create_swap_chain(
- &surface,
- physical_size.width,
- physical_size.height,
- );
+ if viewport_version != current_viewport_version {
+ let logical_size = state.logical_size();
- resized = false;
- }
+ debug.layout_started();
+ user_interface = ManuallyDrop::new(
+ ManuallyDrop::into_inner(user_interface)
+ .relayout(logical_size, &mut renderer),
+ );
+ debug.layout_finished();
- let new_mouse_interaction = compositor.draw(
- &mut renderer,
- &mut swap_chain,
- &viewport,
- background_color,
- state.primitive(),
- &debug.overlay(),
- );
+ debug.draw_started();
+ primitive = user_interface
+ .draw(&mut renderer, state.cursor_position());
+ debug.draw_finished();
- debug.render_finished();
+ swap_chain = compositor.create_swap_chain(
+ &surface,
+ physical_size.width,
+ physical_size.height,
+ );
- if new_mouse_interaction != mouse_interaction {
- window.set_cursor_icon(conversion::mouse_interaction(
- new_mouse_interaction,
- ));
+ viewport_version = current_viewport_version;
+ }
- mouse_interaction = new_mouse_interaction;
+ match compositor.draw(
+ &mut renderer,
+ &mut swap_chain,
+ state.viewport(),
+ state.background_color(),
+ &primitive,
+ &debug.overlay(),
+ ) {
+ Ok(new_mouse_interaction) => {
+ debug.render_finished();
+
+ if new_mouse_interaction != mouse_interaction {
+ window.set_cursor_icon(
+ conversion::mouse_interaction(
+ new_mouse_interaction,
+ ),
+ );
+
+ mouse_interaction = new_mouse_interaction;
+ }
+
+ // TODO: Handle animations!
+ // Maybe we can use `ControlFlow::WaitUntil` for this.
+ }
+ Err(error) => match error {
+ // This is an unrecoverable error.
+ window::SwapChainError::OutOfMemory => {
+ panic!("{}", error);
+ }
+ _ => {
+ debug.render_finished();
+
+ // Try rendering again next frame.
+ window.request_redraw();
+ }
+ },
+ }
+ }
+ event::Event::WindowEvent {
+ event: event::WindowEvent::MenuEntryActivated(entry_id),
+ ..
+ } => {
+ if let Some(message) =
+ conversion::menu_message(state.menu(), entry_id)
+ {
+ messages.push(message);
+ }
}
+ event::Event::WindowEvent {
+ event: window_event,
+ ..
+ } => {
+ if requests_exit(&window_event, state.modifiers())
+ && exit_on_close_request
+ {
+ break;
+ }
- // TODO: Handle animations!
- // Maybe we can use `ControlFlow::WaitUntil` for this.
- }
- event::Event::WindowEvent {
- event: window_event,
- ..
- } => {
- handle_window_event(
- &window_event,
- &window,
- scale_factor,
- control_flow,
- &mut cursor_position,
- &mut modifiers,
- &mut viewport,
- &mut resized,
- &mut debug,
- );
-
- if let Some(event) = conversion::window_event(
- &window_event,
- viewport.scale_factor(),
- modifiers,
- ) {
- state.queue_event(event.clone());
- runtime.broadcast(event);
+ state.update(&window, &window_event, &mut debug);
+
+ if let Some(event) = conversion::window_event(
+ &window_event,
+ state.scale_factor(),
+ state.modifiers(),
+ ) {
+ events.push(event);
+ }
}
+ _ => {}
}
- _ => {
- *control_flow = ControlFlow::Wait;
- }
- })
+ }
+
+ // Manually drop the user interface
+ drop(ManuallyDrop::into_inner(user_interface));
}
-/// Handles a `WindowEvent` and mutates the provided control flow, keyboard
-/// modifiers, viewport, and resized flag accordingly.
-pub fn handle_window_event(
+/// Returns true if the provided event should cause an [`Application`] to
+/// exit.
+pub fn requests_exit(
event: &winit::event::WindowEvent<'_>,
- window: &winit::window::Window,
- scale_factor: f64,
- control_flow: &mut winit::event_loop::ControlFlow,
- cursor_position: &mut winit::dpi::PhysicalPosition<f64>,
- modifiers: &mut winit::event::ModifiersState,
- viewport: &mut Viewport,
- resized: &mut bool,
- _debug: &mut Debug,
-) {
- use winit::{event::WindowEvent, event_loop::ControlFlow};
+ _modifiers: winit::event::ModifiersState,
+) -> bool {
+ use winit::event::WindowEvent;
match event {
- WindowEvent::Resized(new_size) => {
- let size = Size::new(new_size.width, new_size.height);
-
- *viewport = Viewport::with_physical_size(
- size,
- window.scale_factor() * scale_factor,
- );
- *resized = true;
- }
- WindowEvent::ScaleFactorChanged {
- scale_factor: new_scale_factor,
- new_inner_size,
- } => {
- let size = Size::new(new_inner_size.width, new_inner_size.height);
-
- *viewport = Viewport::with_physical_size(
- size,
- new_scale_factor * scale_factor,
- );
- *resized = true;
- }
- WindowEvent::CloseRequested => {
- *control_flow = ControlFlow::Exit;
- }
- WindowEvent::CursorMoved { position, .. } => {
- *cursor_position = *position;
- }
- WindowEvent::CursorLeft { .. } => {
- // TODO: Encode cursor availability in the type-system
- *cursor_position = winit::dpi::PhysicalPosition::new(-1.0, -1.0);
- }
- WindowEvent::ModifiersChanged(new_modifiers) => {
- *modifiers = *new_modifiers;
- }
+ WindowEvent::CloseRequested => true,
#[cfg(target_os = "macos")]
WindowEvent::KeyboardInput {
input:
@@ -398,19 +462,50 @@ pub fn handle_window_event(
..
},
..
- } if modifiers.logo() => {
- *control_flow = ControlFlow::Exit;
- }
- #[cfg(feature = "debug")]
- WindowEvent::KeyboardInput {
- input:
- winit::event::KeyboardInput {
- virtual_keycode: Some(winit::event::VirtualKeyCode::F12),
- state: winit::event::ElementState::Pressed,
- ..
- },
- ..
- } => _debug.toggle(),
- _ => {}
+ } if _modifiers.logo() => true,
+ _ => false,
+ }
+}
+
+/// Builds a [`UserInterface`] for the provided [`Application`], logging
+/// [`struct@Debug`] information accordingly.
+pub fn build_user_interface<'a, A: Application>(
+ application: &'a mut A,
+ cache: Cache,
+ renderer: &mut A::Renderer,
+ size: Size,
+ debug: &mut Debug,
+) -> UserInterface<'a, A::Message, A::Renderer> {
+ debug.view_started();
+ let view = application.view();
+ debug.view_finished();
+
+ debug.layout_started();
+ let user_interface = UserInterface::build(view, size, cache, renderer);
+ debug.layout_finished();
+
+ user_interface
+}
+
+/// Updates an [`Application`] by feeding it the provided messages, spawning any
+/// resulting [`Command`], and tracking its [`Subscription`].
+pub fn update<A: Application, E: Executor>(
+ application: &mut A,
+ runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
+ debug: &mut Debug,
+ clipboard: &mut A::Clipboard,
+ messages: &mut Vec<A::Message>,
+) {
+ for message in messages.drain(..) {
+ debug.log_message(&message);
+
+ debug.update_started();
+ let command = runtime.enter(|| application.update(message, clipboard));
+ debug.update_finished();
+
+ runtime.spawn(command);
}
+
+ let subscription = application.subscription();
+ runtime.track(subscription);
}
diff --git a/winit/src/application/state.rs b/winit/src/application/state.rs
new file mode 100644
index 00000000..f60f09be
--- /dev/null
+++ b/winit/src/application/state.rs
@@ -0,0 +1,224 @@
+use crate::conversion;
+use crate::{Application, Color, Debug, Menu, Mode, Point, Size, Viewport};
+
+use std::marker::PhantomData;
+use winit::event::{Touch, WindowEvent};
+use winit::window::Window;
+
+/// The state of a windowed [`Application`].
+#[derive(Debug, Clone)]
+pub struct State<A: Application> {
+ title: String,
+ menu: Menu<A::Message>,
+ mode: Mode,
+ background_color: Color,
+ scale_factor: f64,
+ viewport: Viewport,
+ viewport_version: usize,
+ cursor_position: winit::dpi::PhysicalPosition<f64>,
+ modifiers: winit::event::ModifiersState,
+ application: PhantomData<A>,
+}
+
+impl<A: Application> State<A> {
+ /// Creates a new [`State`] for the provided [`Application`] and window.
+ pub fn new(application: &A, window: &Window) -> Self {
+ let title = application.title();
+ let menu = application.menu();
+ let mode = application.mode();
+ let background_color = application.background_color();
+ let scale_factor = application.scale_factor();
+
+ let viewport = {
+ let physical_size = window.inner_size();
+
+ Viewport::with_physical_size(
+ Size::new(physical_size.width, physical_size.height),
+ window.scale_factor() * scale_factor,
+ )
+ };
+
+ Self {
+ title,
+ menu,
+ mode,
+ background_color,
+ scale_factor,
+ viewport,
+ viewport_version: 0,
+ // TODO: Encode cursor availability in the type-system
+ cursor_position: winit::dpi::PhysicalPosition::new(-1.0, -1.0),
+ modifiers: winit::event::ModifiersState::default(),
+ application: PhantomData,
+ }
+ }
+
+ /// Returns the current [`Menu`] of the [`State`].
+ pub fn menu(&self) -> &Menu<A::Message> {
+ &self.menu
+ }
+
+ /// Returns the current background [`Color`] of the [`State`].
+ pub fn background_color(&self) -> Color {
+ self.background_color
+ }
+
+ /// Returns the current [`Viewport`] of the [`State`].
+ pub fn viewport(&self) -> &Viewport {
+ &self.viewport
+ }
+
+ /// Returns the version of the [`Viewport`] of the [`State`].
+ ///
+ /// The version is incremented every time the [`Viewport`] changes.
+ pub fn viewport_version(&self) -> usize {
+ self.viewport_version
+ }
+
+ /// Returns the physical [`Size`] of the [`Viewport`] of the [`State`].
+ pub fn physical_size(&self) -> Size<u32> {
+ self.viewport.physical_size()
+ }
+
+ /// Returns the logical [`Size`] of the [`Viewport`] of the [`State`].
+ pub fn logical_size(&self) -> Size<f32> {
+ self.viewport.logical_size()
+ }
+
+ /// Returns the current scale factor of the [`Viewport`] of the [`State`].
+ pub fn scale_factor(&self) -> f64 {
+ self.viewport.scale_factor()
+ }
+
+ /// Returns the current cursor position of the [`State`].
+ pub fn cursor_position(&self) -> Point {
+ conversion::cursor_position(
+ self.cursor_position,
+ self.viewport.scale_factor(),
+ )
+ }
+
+ /// Returns the current keyboard modifiers of the [`State`].
+ pub fn modifiers(&self) -> winit::event::ModifiersState {
+ self.modifiers
+ }
+
+ /// Processes the provided window event and updates the [`State`]
+ /// accordingly.
+ pub fn update(
+ &mut self,
+ window: &Window,
+ event: &WindowEvent<'_>,
+ _debug: &mut Debug,
+ ) {
+ match event {
+ WindowEvent::Resized(new_size) => {
+ let size = Size::new(new_size.width, new_size.height);
+
+ self.viewport = Viewport::with_physical_size(
+ size,
+ window.scale_factor() * self.scale_factor,
+ );
+
+ self.viewport_version = self.viewport_version.wrapping_add(1);
+ }
+ WindowEvent::ScaleFactorChanged {
+ scale_factor: new_scale_factor,
+ new_inner_size,
+ } => {
+ let size =
+ Size::new(new_inner_size.width, new_inner_size.height);
+
+ self.viewport = Viewport::with_physical_size(
+ size,
+ new_scale_factor * self.scale_factor,
+ );
+
+ self.viewport_version = self.viewport_version.wrapping_add(1);
+ }
+ WindowEvent::CursorMoved { position, .. }
+ | WindowEvent::Touch(Touch {
+ location: position, ..
+ }) => {
+ self.cursor_position = *position;
+ }
+ WindowEvent::CursorLeft { .. } => {
+ // TODO: Encode cursor availability in the type-system
+ self.cursor_position =
+ winit::dpi::PhysicalPosition::new(-1.0, -1.0);
+ }
+ WindowEvent::ModifiersChanged(new_modifiers) => {
+ self.modifiers = *new_modifiers;
+ }
+ #[cfg(feature = "debug")]
+ WindowEvent::KeyboardInput {
+ input:
+ winit::event::KeyboardInput {
+ virtual_keycode: Some(winit::event::VirtualKeyCode::F12),
+ state: winit::event::ElementState::Pressed,
+ ..
+ },
+ ..
+ } => _debug.toggle(),
+ _ => {}
+ }
+ }
+
+ /// Synchronizes the [`State`] with its [`Application`] and its respective
+ /// window.
+ ///
+ /// Normally an [`Application`] should be synchronized with its [`State`]
+ /// and window after calling [`Application::update`].
+ ///
+ /// [`Application::update`]: crate::Program::update
+ pub fn synchronize(&mut self, application: &A, window: &Window) {
+ // Update window title
+ let new_title = application.title();
+
+ if self.title != new_title {
+ window.set_title(&new_title);
+
+ self.title = new_title;
+ }
+
+ // Update window mode
+ let new_mode = application.mode();
+
+ if self.mode != new_mode {
+ window.set_fullscreen(conversion::fullscreen(
+ window.current_monitor(),
+ new_mode,
+ ));
+
+ window.set_visible(conversion::visible(new_mode));
+
+ self.mode = new_mode;
+ }
+
+ // Update background color
+ self.background_color = application.background_color();
+
+ // Update scale factor
+ let new_scale_factor = application.scale_factor();
+
+ if self.scale_factor != new_scale_factor {
+ let size = window.inner_size();
+
+ self.viewport = Viewport::with_physical_size(
+ Size::new(size.width, size.height),
+ window.scale_factor() * new_scale_factor,
+ );
+
+ self.scale_factor = new_scale_factor;
+ }
+
+ // Update menu
+ let new_menu = application.menu();
+
+ if self.menu != new_menu {
+ window.set_menu(Some(conversion::menu(&new_menu)));
+
+ self.menu = new_menu;
+ }
+ }
+}
diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs
index 1ff029ab..ca25c065 100644
--- a/winit/src/clipboard.rs
+++ b/winit/src/clipboard.rs
@@ -1,19 +1,54 @@
/// A buffer for short-term storage and transfer within and between
/// applications.
#[allow(missing_debug_implementations)]
-pub struct Clipboard(window_clipboard::Clipboard);
+pub struct Clipboard {
+ state: State,
+}
+
+enum State {
+ Connected(window_clipboard::Clipboard),
+ Unavailable,
+}
impl Clipboard {
/// Creates a new [`Clipboard`] for the given window.
- ///
- /// [`Clipboard`]: struct.Clipboard.html
- pub fn new(window: &winit::window::Window) -> Option<Clipboard> {
- window_clipboard::Clipboard::new(window).map(Clipboard).ok()
+ pub fn connect(window: &winit::window::Window) -> Clipboard {
+ let state = window_clipboard::Clipboard::connect(window)
+ .ok()
+ .map(State::Connected)
+ .unwrap_or(State::Unavailable);
+
+ Clipboard { state }
+ }
+
+ /// Reads the current content of the [`Clipboard`] as text.
+ pub fn read(&self) -> Option<String> {
+ match &self.state {
+ State::Connected(clipboard) => clipboard.read().ok(),
+ State::Unavailable => None,
+ }
+ }
+
+ /// Writes the given text contents to the [`Clipboard`].
+ pub fn write(&mut self, contents: String) {
+ match &mut self.state {
+ State::Connected(clipboard) => match clipboard.write(contents) {
+ Ok(()) => {}
+ Err(error) => {
+ log::warn!("error writing to clipboard: {}", error)
+ }
+ },
+ State::Unavailable => {}
+ }
}
}
impl iced_native::Clipboard for Clipboard {
- fn content(&self) -> Option<String> {
- self.0.read().ok()
+ fn read(&self) -> Option<String> {
+ self.read()
+ }
+
+ fn write(&mut self, contents: String) {
+ self.write(contents)
}
}
diff --git a/winit/src/conversion.rs b/winit/src/conversion.rs
index 638787ab..e0934f43 100644
--- a/winit/src/conversion.rs
+++ b/winit/src/conversion.rs
@@ -2,10 +2,12 @@
//!
//! [`winit`]: https://github.com/rust-windowing/winit
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
-use crate::{
- keyboard::{self, KeyCode, ModifiersState},
- mouse, window, Event, Mode, Point,
-};
+use crate::keyboard;
+use crate::menu::{self, Menu};
+use crate::mouse;
+use crate::touch;
+use crate::window;
+use crate::{Event, Mode, Point, Position};
/// Converts a winit window event into an iced event.
pub fn window_event(
@@ -32,12 +34,14 @@ pub fn window_event(
height: logical_size.height,
}))
}
+ WindowEvent::CloseRequested => {
+ Some(Event::Window(window::Event::CloseRequested))
+ }
WindowEvent::CursorMoved { position, .. } => {
let position = position.to_logical::<f64>(scale_factor);
Some(Event::Mouse(mouse::Event::CursorMoved {
- x: position.x as f32,
- y: position.y as f32,
+ position: Point::new(position.x as f32, position.y as f32),
}))
}
WindowEvent::CursorEntered { .. } => {
@@ -89,7 +93,7 @@ pub fn window_event(
..
} => Some(Event::Keyboard({
let key_code = key_code(*virtual_keycode);
- let modifiers = modifiers_state(modifiers);
+ let modifiers = self::modifiers(modifiers);
match state {
winit::event::ElementState::Pressed => {
@@ -107,8 +111,13 @@ pub fn window_event(
}
})),
WindowEvent::ModifiersChanged(new_modifiers) => Some(Event::Keyboard(
- keyboard::Event::ModifiersChanged(modifiers_state(*new_modifiers)),
+ keyboard::Event::ModifiersChanged(self::modifiers(*new_modifiers)),
)),
+ WindowEvent::Focused(focused) => Some(Event::Window(if *focused {
+ window::Event::Focused
+ } else {
+ window::Event::Unfocused
+ })),
WindowEvent::HoveredFile(path) => {
Some(Event::Window(window::Event::FileHovered(path.clone())))
}
@@ -118,26 +127,189 @@ pub fn window_event(
WindowEvent::HoveredFileCancelled => {
Some(Event::Window(window::Event::FilesHoveredLeft))
}
+ WindowEvent::Touch(touch) => {
+ Some(Event::Touch(touch_event(*touch, scale_factor)))
+ }
+ WindowEvent::Moved(position) => {
+ let winit::dpi::LogicalPosition { x, y } =
+ position.to_logical(scale_factor);
+
+ Some(Event::Window(window::Event::Moved { x, y }))
+ }
_ => None,
}
}
+/// Converts a [`Position`] to a [`winit`] logical position for a given monitor.
+///
+/// [`winit`]: https://github.com/rust-windowing/winit
+pub fn position(
+ monitor: Option<&winit::monitor::MonitorHandle>,
+ (width, height): (u32, u32),
+ position: Position,
+) -> Option<winit::dpi::Position> {
+ match position {
+ Position::Default => None,
+ Position::Specific(x, y) => {
+ Some(winit::dpi::Position::Logical(winit::dpi::LogicalPosition {
+ x: f64::from(x),
+ y: f64::from(y),
+ }))
+ }
+ Position::Centered => {
+ if let Some(monitor) = monitor {
+ let start = monitor.position();
+
+ let resolution: winit::dpi::LogicalSize<f64> =
+ monitor.size().to_logical(monitor.scale_factor());
+
+ let centered: winit::dpi::PhysicalPosition<i32> =
+ winit::dpi::LogicalPosition {
+ x: (resolution.width - f64::from(width)) / 2.0,
+ y: (resolution.height - f64::from(height)) / 2.0,
+ }
+ .to_physical(monitor.scale_factor());
+
+ Some(winit::dpi::Position::Physical(
+ winit::dpi::PhysicalPosition {
+ x: start.x + centered.x,
+ y: start.y + centered.y,
+ },
+ ))
+ } else {
+ None
+ }
+ }
+ }
+}
+
/// Converts a [`Mode`] to a [`winit`] fullscreen mode.
///
-/// [`Mode`]: ../enum.Mode.html
/// [`winit`]: https://github.com/rust-windowing/winit
pub fn fullscreen(
- monitor: winit::monitor::MonitorHandle,
+ monitor: Option<winit::monitor::MonitorHandle>,
mode: Mode,
) -> Option<winit::window::Fullscreen> {
match mode {
- Mode::Windowed => None,
+ Mode::Windowed | Mode::Hidden => None,
Mode::Fullscreen => {
Some(winit::window::Fullscreen::Borderless(monitor))
}
}
}
+/// Converts a [`Mode`] to a visibility flag.
+pub fn visible(mode: Mode) -> bool {
+ match mode {
+ Mode::Windowed | Mode::Fullscreen => true,
+ Mode::Hidden => false,
+ }
+}
+
+/// Converts a `Hotkey` from [`iced_native`] to a [`winit`] Hotkey.
+///
+/// [`winit`]: https://github.com/rust-windowing/winit
+/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
+fn hotkey(hotkey: keyboard::Hotkey) -> winit::window::Hotkey {
+ use winit::event::ModifiersState;
+
+ let mut modifiers = ModifiersState::empty();
+ modifiers.set(ModifiersState::CTRL, hotkey.modifiers.control());
+ modifiers.set(ModifiersState::SHIFT, hotkey.modifiers.shift());
+ modifiers.set(ModifiersState::ALT, hotkey.modifiers.alt());
+ modifiers.set(ModifiersState::LOGO, hotkey.modifiers.logo());
+
+ winit::window::Hotkey::new(modifiers, to_virtual_keycode(hotkey.key))
+}
+
+/// Converts a `Menu` from [`iced_native`] to a [`winit`] menu.
+///
+/// [`winit`]: https://github.com/rust-windowing/winit
+/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
+pub fn menu<Message>(menu: &Menu<Message>) -> winit::window::Menu {
+ fn menu_i<Message>(
+ converted: &mut winit::window::Menu,
+ starting_id: usize,
+ menu: &Menu<Message>,
+ ) -> usize {
+ let mut id = starting_id;
+
+ for item in menu.iter() {
+ match item {
+ menu::Entry::Item { title, hotkey, .. } => {
+ converted.add_item(id, title, hotkey.map(self::hotkey));
+
+ id += 1;
+ }
+ menu::Entry::Dropdown { title, submenu } => {
+ let mut converted_submenu = winit::window::Menu::new();
+ let n_children =
+ menu_i(&mut converted_submenu, id, submenu);
+
+ converted.add_dropdown(title, converted_submenu);
+
+ id += n_children;
+ }
+ menu::Entry::Separator => {
+ converted.add_separator();
+ }
+ }
+ }
+
+ id - starting_id
+ }
+
+ let mut converted = winit::window::Menu::default();
+ let _ = menu_i(&mut converted, 0, menu);
+
+ converted
+}
+
+/// Given a [`Menu`] and an identifier of a [`menu::Entry`], it returns the
+/// `Message` that should be produced when that entry is activated.
+pub fn menu_message<Message>(menu: &Menu<Message>, id: u32) -> Option<Message>
+where
+ Message: Clone,
+{
+ fn find_message<Message>(
+ target: u32,
+ starting_id: u32,
+ menu: &Menu<Message>,
+ ) -> Result<Message, u32>
+ where
+ Message: Clone,
+ {
+ let mut id = starting_id;
+
+ for entry in menu.iter() {
+ match entry {
+ menu::Entry::Item { on_activation, .. } => {
+ if id == target {
+ return Ok(on_activation.clone());
+ }
+
+ id += 1;
+ }
+ menu::Entry::Dropdown { submenu, .. } => {
+ match find_message(target, id, submenu) {
+ Ok(message) => {
+ return Ok(message);
+ }
+ Err(n_children) => {
+ id += n_children;
+ }
+ }
+ }
+ menu::Entry::Separator => {}
+ }
+ }
+
+ Err(id - starting_id)
+ }
+
+ find_message(id, 0, menu).ok()
+}
+
/// Converts a `MouseCursor` from [`iced_native`] to a [`winit`] cursor icon.
///
/// [`winit`]: https://github.com/rust-windowing/winit
@@ -171,7 +343,9 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button {
winit::event::MouseButton::Left => mouse::Button::Left,
winit::event::MouseButton::Right => mouse::Button::Right,
winit::event::MouseButton::Middle => mouse::Button::Middle,
- winit::event::MouseButton::Other(other) => mouse::Button::Other(other),
+ winit::event::MouseButton::Other(other) => {
+ mouse::Button::Other(other as u8)
+ }
}
}
@@ -180,15 +354,17 @@ pub fn mouse_button(mouse_button: winit::event::MouseButton) -> mouse::Button {
///
/// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
-pub fn modifiers_state(
+pub fn modifiers(
modifiers: winit::event::ModifiersState,
-) -> ModifiersState {
- ModifiersState {
- shift: modifiers.shift(),
- control: modifiers.ctrl(),
- alt: modifiers.alt(),
- logo: modifiers.logo(),
- }
+) -> keyboard::Modifiers {
+ let mut result = keyboard::Modifiers::empty();
+
+ result.set(keyboard::Modifiers::SHIFT, modifiers.shift());
+ result.set(keyboard::Modifiers::CTRL, modifiers.ctrl());
+ result.set(keyboard::Modifiers::ALT, modifiers.alt());
+ result.set(keyboard::Modifiers::LOGO, modifiers.logo());
+
+ result
}
/// Converts a physical cursor position to a logical `Point`.
@@ -201,11 +377,223 @@ pub fn cursor_position(
Point::new(logical_position.x, logical_position.y)
}
+/// Converts a `Touch` from [`winit`] to an [`iced_native`] touch event.
+///
+/// [`winit`]: https://github.com/rust-windowing/winit
+/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
+pub fn touch_event(
+ touch: winit::event::Touch,
+ scale_factor: f64,
+) -> touch::Event {
+ let id = touch::Finger(touch.id);
+ let position = {
+ let location = touch.location.to_logical::<f64>(scale_factor);
+
+ Point::new(location.x as f32, location.y as f32)
+ };
+
+ match touch.phase {
+ winit::event::TouchPhase::Started => {
+ touch::Event::FingerPressed { id, position }
+ }
+ winit::event::TouchPhase::Moved => {
+ touch::Event::FingerMoved { id, position }
+ }
+ winit::event::TouchPhase::Ended => {
+ touch::Event::FingerLifted { id, position }
+ }
+ winit::event::TouchPhase::Cancelled => {
+ touch::Event::FingerLost { id, position }
+ }
+ }
+}
+
+/// Converts a `KeyCode` from [`iced_native`] to an [`winit`] key code.
+///
+/// [`winit`]: https://github.com/rust-windowing/winit
+/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
+fn to_virtual_keycode(
+ keycode: keyboard::KeyCode,
+) -> winit::event::VirtualKeyCode {
+ use keyboard::KeyCode;
+ use winit::event::VirtualKeyCode;
+
+ match keycode {
+ KeyCode::Key1 => VirtualKeyCode::Key1,
+ KeyCode::Key2 => VirtualKeyCode::Key2,
+ KeyCode::Key3 => VirtualKeyCode::Key3,
+ KeyCode::Key4 => VirtualKeyCode::Key4,
+ KeyCode::Key5 => VirtualKeyCode::Key5,
+ KeyCode::Key6 => VirtualKeyCode::Key6,
+ KeyCode::Key7 => VirtualKeyCode::Key7,
+ KeyCode::Key8 => VirtualKeyCode::Key8,
+ KeyCode::Key9 => VirtualKeyCode::Key9,
+ KeyCode::Key0 => VirtualKeyCode::Key0,
+ KeyCode::A => VirtualKeyCode::A,
+ KeyCode::B => VirtualKeyCode::B,
+ KeyCode::C => VirtualKeyCode::C,
+ KeyCode::D => VirtualKeyCode::D,
+ KeyCode::E => VirtualKeyCode::E,
+ KeyCode::F => VirtualKeyCode::F,
+ KeyCode::G => VirtualKeyCode::G,
+ KeyCode::H => VirtualKeyCode::H,
+ KeyCode::I => VirtualKeyCode::I,
+ KeyCode::J => VirtualKeyCode::J,
+ KeyCode::K => VirtualKeyCode::K,
+ KeyCode::L => VirtualKeyCode::L,
+ KeyCode::M => VirtualKeyCode::M,
+ KeyCode::N => VirtualKeyCode::N,
+ KeyCode::O => VirtualKeyCode::O,
+ KeyCode::P => VirtualKeyCode::P,
+ KeyCode::Q => VirtualKeyCode::Q,
+ KeyCode::R => VirtualKeyCode::R,
+ KeyCode::S => VirtualKeyCode::S,
+ KeyCode::T => VirtualKeyCode::T,
+ KeyCode::U => VirtualKeyCode::U,
+ KeyCode::V => VirtualKeyCode::V,
+ KeyCode::W => VirtualKeyCode::W,
+ KeyCode::X => VirtualKeyCode::X,
+ KeyCode::Y => VirtualKeyCode::Y,
+ KeyCode::Z => VirtualKeyCode::Z,
+ KeyCode::Escape => VirtualKeyCode::Escape,
+ KeyCode::F1 => VirtualKeyCode::F1,
+ KeyCode::F2 => VirtualKeyCode::F2,
+ KeyCode::F3 => VirtualKeyCode::F3,
+ KeyCode::F4 => VirtualKeyCode::F4,
+ KeyCode::F5 => VirtualKeyCode::F5,
+ KeyCode::F6 => VirtualKeyCode::F6,
+ KeyCode::F7 => VirtualKeyCode::F7,
+ KeyCode::F8 => VirtualKeyCode::F8,
+ KeyCode::F9 => VirtualKeyCode::F9,
+ KeyCode::F10 => VirtualKeyCode::F10,
+ KeyCode::F11 => VirtualKeyCode::F11,
+ KeyCode::F12 => VirtualKeyCode::F12,
+ KeyCode::F13 => VirtualKeyCode::F13,
+ KeyCode::F14 => VirtualKeyCode::F14,
+ KeyCode::F15 => VirtualKeyCode::F15,
+ KeyCode::F16 => VirtualKeyCode::F16,
+ KeyCode::F17 => VirtualKeyCode::F17,
+ KeyCode::F18 => VirtualKeyCode::F18,
+ KeyCode::F19 => VirtualKeyCode::F19,
+ KeyCode::F20 => VirtualKeyCode::F20,
+ KeyCode::F21 => VirtualKeyCode::F21,
+ KeyCode::F22 => VirtualKeyCode::F22,
+ KeyCode::F23 => VirtualKeyCode::F23,
+ KeyCode::F24 => VirtualKeyCode::F24,
+ KeyCode::Snapshot => VirtualKeyCode::Snapshot,
+ KeyCode::Scroll => VirtualKeyCode::Scroll,
+ KeyCode::Pause => VirtualKeyCode::Pause,
+ KeyCode::Insert => VirtualKeyCode::Insert,
+ KeyCode::Home => VirtualKeyCode::Home,
+ KeyCode::Delete => VirtualKeyCode::Delete,
+ KeyCode::End => VirtualKeyCode::End,
+ KeyCode::PageDown => VirtualKeyCode::PageDown,
+ KeyCode::PageUp => VirtualKeyCode::PageUp,
+ KeyCode::Left => VirtualKeyCode::Left,
+ KeyCode::Up => VirtualKeyCode::Up,
+ KeyCode::Right => VirtualKeyCode::Right,
+ KeyCode::Down => VirtualKeyCode::Down,
+ KeyCode::Backspace => VirtualKeyCode::Back,
+ KeyCode::Enter => VirtualKeyCode::Return,
+ KeyCode::Space => VirtualKeyCode::Space,
+ KeyCode::Compose => VirtualKeyCode::Compose,
+ KeyCode::Caret => VirtualKeyCode::Caret,
+ KeyCode::Numlock => VirtualKeyCode::Numlock,
+ KeyCode::Numpad0 => VirtualKeyCode::Numpad0,
+ KeyCode::Numpad1 => VirtualKeyCode::Numpad1,
+ KeyCode::Numpad2 => VirtualKeyCode::Numpad2,
+ KeyCode::Numpad3 => VirtualKeyCode::Numpad3,
+ KeyCode::Numpad4 => VirtualKeyCode::Numpad4,
+ KeyCode::Numpad5 => VirtualKeyCode::Numpad5,
+ KeyCode::Numpad6 => VirtualKeyCode::Numpad6,
+ KeyCode::Numpad7 => VirtualKeyCode::Numpad7,
+ KeyCode::Numpad8 => VirtualKeyCode::Numpad8,
+ KeyCode::Numpad9 => VirtualKeyCode::Numpad9,
+ KeyCode::AbntC1 => VirtualKeyCode::AbntC1,
+ KeyCode::AbntC2 => VirtualKeyCode::AbntC2,
+ KeyCode::NumpadAdd => VirtualKeyCode::NumpadAdd,
+ KeyCode::Plus => VirtualKeyCode::Plus,
+ KeyCode::Apostrophe => VirtualKeyCode::Apostrophe,
+ KeyCode::Apps => VirtualKeyCode::Apps,
+ KeyCode::At => VirtualKeyCode::At,
+ KeyCode::Ax => VirtualKeyCode::Ax,
+ KeyCode::Backslash => VirtualKeyCode::Backslash,
+ KeyCode::Calculator => VirtualKeyCode::Calculator,
+ KeyCode::Capital => VirtualKeyCode::Capital,
+ KeyCode::Colon => VirtualKeyCode::Colon,
+ KeyCode::Comma => VirtualKeyCode::Comma,
+ KeyCode::Convert => VirtualKeyCode::Convert,
+ KeyCode::NumpadDecimal => VirtualKeyCode::NumpadDecimal,
+ KeyCode::NumpadDivide => VirtualKeyCode::NumpadDivide,
+ KeyCode::Equals => VirtualKeyCode::Equals,
+ KeyCode::Grave => VirtualKeyCode::Grave,
+ KeyCode::Kana => VirtualKeyCode::Kana,
+ KeyCode::Kanji => VirtualKeyCode::Kanji,
+ KeyCode::LAlt => VirtualKeyCode::LAlt,
+ KeyCode::LBracket => VirtualKeyCode::LBracket,
+ KeyCode::LControl => VirtualKeyCode::LControl,
+ KeyCode::LShift => VirtualKeyCode::LShift,
+ KeyCode::LWin => VirtualKeyCode::LWin,
+ KeyCode::Mail => VirtualKeyCode::Mail,
+ KeyCode::MediaSelect => VirtualKeyCode::MediaSelect,
+ KeyCode::MediaStop => VirtualKeyCode::MediaStop,
+ KeyCode::Minus => VirtualKeyCode::Minus,
+ KeyCode::NumpadMultiply => VirtualKeyCode::NumpadMultiply,
+ KeyCode::Mute => VirtualKeyCode::Mute,
+ KeyCode::MyComputer => VirtualKeyCode::MyComputer,
+ KeyCode::NavigateForward => VirtualKeyCode::NavigateForward,
+ KeyCode::NavigateBackward => VirtualKeyCode::NavigateBackward,
+ KeyCode::NextTrack => VirtualKeyCode::NextTrack,
+ KeyCode::NoConvert => VirtualKeyCode::NoConvert,
+ KeyCode::NumpadComma => VirtualKeyCode::NumpadComma,
+ KeyCode::NumpadEnter => VirtualKeyCode::NumpadEnter,
+ KeyCode::NumpadEquals => VirtualKeyCode::NumpadEquals,
+ KeyCode::OEM102 => VirtualKeyCode::OEM102,
+ KeyCode::Period => VirtualKeyCode::Period,
+ KeyCode::PlayPause => VirtualKeyCode::PlayPause,
+ KeyCode::Power => VirtualKeyCode::Power,
+ KeyCode::PrevTrack => VirtualKeyCode::PrevTrack,
+ KeyCode::RAlt => VirtualKeyCode::RAlt,
+ KeyCode::RBracket => VirtualKeyCode::RBracket,
+ KeyCode::RControl => VirtualKeyCode::RControl,
+ KeyCode::RShift => VirtualKeyCode::RShift,
+ KeyCode::RWin => VirtualKeyCode::RWin,
+ KeyCode::Semicolon => VirtualKeyCode::Semicolon,
+ KeyCode::Slash => VirtualKeyCode::Slash,
+ KeyCode::Sleep => VirtualKeyCode::Sleep,
+ KeyCode::Stop => VirtualKeyCode::Stop,
+ KeyCode::NumpadSubtract => VirtualKeyCode::NumpadSubtract,
+ KeyCode::Sysrq => VirtualKeyCode::Sysrq,
+ KeyCode::Tab => VirtualKeyCode::Tab,
+ KeyCode::Underline => VirtualKeyCode::Underline,
+ KeyCode::Unlabeled => VirtualKeyCode::Unlabeled,
+ KeyCode::VolumeDown => VirtualKeyCode::VolumeDown,
+ KeyCode::VolumeUp => VirtualKeyCode::VolumeUp,
+ KeyCode::Wake => VirtualKeyCode::Wake,
+ KeyCode::WebBack => VirtualKeyCode::WebBack,
+ KeyCode::WebFavorites => VirtualKeyCode::WebFavorites,
+ KeyCode::WebForward => VirtualKeyCode::WebForward,
+ KeyCode::WebHome => VirtualKeyCode::WebHome,
+ KeyCode::WebRefresh => VirtualKeyCode::WebRefresh,
+ KeyCode::WebSearch => VirtualKeyCode::WebSearch,
+ KeyCode::WebStop => VirtualKeyCode::WebStop,
+ KeyCode::Yen => VirtualKeyCode::Yen,
+ KeyCode::Copy => VirtualKeyCode::Copy,
+ KeyCode::Paste => VirtualKeyCode::Paste,
+ KeyCode::Cut => VirtualKeyCode::Cut,
+ KeyCode::Asterisk => VirtualKeyCode::Asterisk,
+ }
+}
+
/// Converts a `VirtualKeyCode` from [`winit`] to an [`iced_native`] key code.
///
/// [`winit`]: https://github.com/rust-windowing/winit
/// [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
-pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode {
+pub fn key_code(
+ virtual_keycode: winit::event::VirtualKeyCode,
+) -> keyboard::KeyCode {
+ use keyboard::KeyCode;
+
match virtual_keycode {
winit::event::VirtualKeyCode::Key1 => KeyCode::Key1,
winit::event::VirtualKeyCode::Key2 => KeyCode::Key2,
@@ -299,7 +687,8 @@ pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode {
winit::event::VirtualKeyCode::Numpad9 => KeyCode::Numpad9,
winit::event::VirtualKeyCode::AbntC1 => KeyCode::AbntC1,
winit::event::VirtualKeyCode::AbntC2 => KeyCode::AbntC2,
- winit::event::VirtualKeyCode::Add => KeyCode::Add,
+ winit::event::VirtualKeyCode::NumpadAdd => KeyCode::NumpadAdd,
+ winit::event::VirtualKeyCode::Plus => KeyCode::Plus,
winit::event::VirtualKeyCode::Apostrophe => KeyCode::Apostrophe,
winit::event::VirtualKeyCode::Apps => KeyCode::Apps,
winit::event::VirtualKeyCode::At => KeyCode::At,
@@ -310,8 +699,8 @@ pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode {
winit::event::VirtualKeyCode::Colon => KeyCode::Colon,
winit::event::VirtualKeyCode::Comma => KeyCode::Comma,
winit::event::VirtualKeyCode::Convert => KeyCode::Convert,
- winit::event::VirtualKeyCode::Decimal => KeyCode::Decimal,
- winit::event::VirtualKeyCode::Divide => KeyCode::Divide,
+ winit::event::VirtualKeyCode::NumpadDecimal => KeyCode::NumpadDecimal,
+ winit::event::VirtualKeyCode::NumpadDivide => KeyCode::NumpadDivide,
winit::event::VirtualKeyCode::Equals => KeyCode::Equals,
winit::event::VirtualKeyCode::Grave => KeyCode::Grave,
winit::event::VirtualKeyCode::Kana => KeyCode::Kana,
@@ -325,7 +714,7 @@ pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode {
winit::event::VirtualKeyCode::MediaSelect => KeyCode::MediaSelect,
winit::event::VirtualKeyCode::MediaStop => KeyCode::MediaStop,
winit::event::VirtualKeyCode::Minus => KeyCode::Minus,
- winit::event::VirtualKeyCode::Multiply => KeyCode::Multiply,
+ winit::event::VirtualKeyCode::NumpadMultiply => KeyCode::NumpadMultiply,
winit::event::VirtualKeyCode::Mute => KeyCode::Mute,
winit::event::VirtualKeyCode::MyComputer => KeyCode::MyComputer,
winit::event::VirtualKeyCode::NavigateForward => {
@@ -353,7 +742,7 @@ pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode {
winit::event::VirtualKeyCode::Slash => KeyCode::Slash,
winit::event::VirtualKeyCode::Sleep => KeyCode::Sleep,
winit::event::VirtualKeyCode::Stop => KeyCode::Stop,
- winit::event::VirtualKeyCode::Subtract => KeyCode::Subtract,
+ winit::event::VirtualKeyCode::NumpadSubtract => KeyCode::NumpadSubtract,
winit::event::VirtualKeyCode::Sysrq => KeyCode::Sysrq,
winit::event::VirtualKeyCode::Tab => KeyCode::Tab,
winit::event::VirtualKeyCode::Underline => KeyCode::Underline,
@@ -372,6 +761,7 @@ pub fn key_code(virtual_keycode: winit::event::VirtualKeyCode) -> KeyCode {
winit::event::VirtualKeyCode::Copy => KeyCode::Copy,
winit::event::VirtualKeyCode::Paste => KeyCode::Paste,
winit::event::VirtualKeyCode::Cut => KeyCode::Cut,
+ winit::event::VirtualKeyCode::Asterisk => KeyCode::Asterisk,
}
}
diff --git a/winit/src/lib.rs b/winit/src/lib.rs
index 8ca8eec1..1707846a 100644
--- a/winit/src/lib.rs
+++ b/winit/src/lib.rs
@@ -1,6 +1,6 @@
//! A windowing shell for Iced, on top of [`winit`].
//!
-//! ![`iced_winit` crate graph](https://github.com/hecrj/iced/blob/cae26cb7bc627f4a5b3bcf1cd023a0c552e8c65e/docs/graphs/winit.png?raw=true)
+//! ![The native path of the Iced ecosystem](https://github.com/hecrj/iced/blob/0525d76ff94e828b7b21634fa94a747022001c83/docs/graphs/native.png?raw=true)
//!
//! `iced_winit` offers some convenient abstractions on top of [`iced_native`]
//! to quickstart development when using [`winit`].
@@ -13,8 +13,7 @@
//!
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
//! [`winit`]: https://github.com/rust-windowing/winit
-//! [`Application`]: trait.Application.html
-//! [`conversion`]: conversion
+//! [`conversion`]: crate::conversion
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![deny(unused_results)]
@@ -32,12 +31,14 @@ pub mod settings;
mod clipboard;
mod error;
mod mode;
+mod position;
mod proxy;
pub use application::Application;
pub use clipboard::Clipboard;
pub use error::Error;
pub use mode::Mode;
+pub use position::Position;
pub use proxy::Proxy;
pub use settings::Settings;
diff --git a/winit/src/mode.rs b/winit/src/mode.rs
index 37464711..fdce8e23 100644
--- a/winit/src/mode.rs
+++ b/winit/src/mode.rs
@@ -6,4 +6,7 @@ pub enum Mode {
/// The application takes the whole screen of its current monitor.
Fullscreen,
+
+ /// The application is hidden
+ Hidden,
}
diff --git a/winit/src/position.rs b/winit/src/position.rs
new file mode 100644
index 00000000..c260c29e
--- /dev/null
+++ b/winit/src/position.rs
@@ -0,0 +1,22 @@
+/// The position of a window in a given screen.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum Position {
+ /// The platform-specific default position for a new window.
+ Default,
+ /// The window is completely centered on the screen.
+ Centered,
+ /// The window is positioned with specific coordinates: `(X, Y)`.
+ ///
+ /// When the decorations of the window are enabled, Windows 10 will add some
+ /// invisible padding to the window. This padding gets included in the
+ /// position. So if you have decorations enabled and want the window to be
+ /// at (0, 0) you would have to set the position to
+ /// `(PADDING_X, PADDING_Y)`.
+ Specific(i32, i32),
+}
+
+impl Default for Position {
+ fn default() -> Self {
+ Self::Default
+ }
+}
diff --git a/winit/src/proxy.rs b/winit/src/proxy.rs
index 532f8c56..7b9074d7 100644
--- a/winit/src/proxy.rs
+++ b/winit/src/proxy.rs
@@ -21,8 +21,6 @@ impl<Message: 'static> Clone for Proxy<Message> {
impl<Message: 'static> Proxy<Message> {
/// Creates a new [`Proxy`] from an `EventLoopProxy`.
- ///
- /// [`Proxy`]: struct.Proxy.html
pub fn new(raw: winit::event_loop::EventLoopProxy<Message>) -> Self {
Self { raw }
}
diff --git a/winit/src/settings.rs b/winit/src/settings.rs
index 92541e7d..72a1a24a 100644
--- a/winit/src/settings.rs
+++ b/winit/src/settings.rs
@@ -9,22 +9,30 @@ mod platform;
pub use platform::PlatformSpecific;
use crate::conversion;
-use crate::Mode;
+use crate::{Mode, Position};
use winit::monitor::MonitorHandle;
use winit::window::WindowBuilder;
/// The settings of an application.
#[derive(Debug, Clone, Default)]
pub struct Settings<Flags> {
- /// The [`Window`] settings
+ /// The identifier of the application.
///
- /// [`Window`]: struct.Window.html
+ /// If provided, this identifier may be used to identify the application or
+ /// communicate with it through the windowing system.
+ pub id: Option<String>,
+
+ /// The [`Window`] settings
pub window: Window,
/// The data needed to initialize an [`Application`].
///
- /// [`Application`]: trait.Application.html
+ /// [`Application`]: crate::Application
pub flags: Flags,
+
+ /// Whether the [`Application`] should exit when the user requests the
+ /// window to close (e.g. the user presses the close button).
+ pub exit_on_close_request: bool,
}
/// The window settings of an application.
@@ -33,6 +41,9 @@ pub struct Window {
/// The size of the window.
pub size: (u32, u32),
+ /// The position of the window.
+ pub position: Position,
+
/// The minimum size of the window.
pub min_size: Option<(u32, u32)>,
@@ -45,9 +56,12 @@ pub struct Window {
/// Whether the window should have a border, a title bar, etc.
pub decorations: bool,
- /// Whether the window should be transparent
+ /// Whether the window should be transparent.
pub transparent: bool,
+ /// Whether the window will always be on top of other windows.
+ pub always_on_top: bool,
+
/// The window icon, which is also usually used in the taskbar
pub icon: Option<winit::window::Icon>,
@@ -61,7 +75,8 @@ impl Window {
self,
title: &str,
mode: Mode,
- primary_monitor: MonitorHandle,
+ primary_monitor: Option<MonitorHandle>,
+ _id: Option<String>,
) -> WindowBuilder {
let mut window_builder = WindowBuilder::new();
@@ -74,7 +89,16 @@ impl Window {
.with_decorations(self.decorations)
.with_transparent(self.transparent)
.with_window_icon(self.icon)
- .with_fullscreen(conversion::fullscreen(primary_monitor, mode));
+ .with_always_on_top(self.always_on_top)
+ .with_visible(conversion::visible(mode));
+
+ if let Some(position) = conversion::position(
+ primary_monitor.as_ref(),
+ self.size,
+ self.position,
+ ) {
+ window_builder = window_builder.with_position(position);
+ }
if let Some((width, height)) = self.min_size {
window_builder = window_builder
@@ -86,6 +110,21 @@ impl Window {
.with_max_inner_size(winit::dpi::LogicalSize { width, height });
}
+ #[cfg(any(
+ target_os = "linux",
+ target_os = "dragonfly",
+ target_os = "freebsd",
+ target_os = "netbsd",
+ target_os = "openbsd"
+ ))]
+ {
+ use ::winit::platform::unix::WindowBuilderExtUnix;
+
+ if let Some(id) = _id {
+ window_builder = window_builder.with_app_id(id);
+ }
+ }
+
#[cfg(target_os = "windows")]
{
use winit::platform::windows::WindowBuilderExtWindows;
@@ -93,8 +132,14 @@ impl Window {
if let Some(parent) = self.platform_specific.parent {
window_builder = window_builder.with_parent_window(parent);
}
+
+ window_builder = window_builder
+ .with_drag_and_drop(self.platform_specific.drag_and_drop);
}
+ window_builder = window_builder
+ .with_fullscreen(conversion::fullscreen(primary_monitor, mode));
+
window_builder
}
}
@@ -103,11 +148,13 @@ impl Default for Window {
fn default() -> Window {
Window {
size: (1024, 768),
+ position: Position::default(),
min_size: None,
max_size: None,
resizable: true,
decorations: true,
transparent: false,
+ always_on_top: false,
icon: None,
platform_specific: Default::default(),
}
diff --git a/winit/src/settings/windows.rs b/winit/src/settings/windows.rs
index 76b8d067..fc26acd7 100644
--- a/winit/src/settings/windows.rs
+++ b/winit/src/settings/windows.rs
@@ -2,8 +2,20 @@
//! Platform specific settings for Windows.
/// The platform specific window settings of an application.
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PlatformSpecific {
- /// Parent Window
+ /// Parent window
pub parent: Option<winapi::shared::windef::HWND>,
+
+ /// Drag and drop support
+ pub drag_and_drop: bool,
+}
+
+impl Default for PlatformSpecific {
+ fn default() -> Self {
+ Self {
+ parent: None,
+ drag_and_drop: true,
+ }
+ }
}