summaryrefslogtreecommitdiffstats
path: root/glutin
diff options
context:
space:
mode:
Diffstat (limited to 'glutin')
-rw-r--r--glutin/Cargo.toml14
-rw-r--r--glutin/README.md22
-rw-r--r--glutin/src/application.rs429
-rw-r--r--glutin/src/lib.rs4
4 files changed, 282 insertions, 187 deletions
diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml
index 4652112c..b2a7f307 100644
--- a/glutin/Cargo.toml
+++ b/glutin/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "iced_glutin"
-version = "0.1.0"
+version = "0.2.0"
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
edition = "2018"
description = "A glutin runtime for Iced"
@@ -13,18 +13,20 @@ categories = ["gui"]
[features]
debug = ["iced_winit/debug"]
-[dependencies]
-glutin = "0.24"
+[dependencies.glutin]
+version = "0.27"
+git = "https://github.com/iced-rs/glutin"
+rev = "03437d8a1826d83c62017b2bb7bf18bfc9e352cc"
[dependencies.iced_native]
-version = "0.2"
+version = "0.4"
path = "../native"
[dependencies.iced_winit]
-version = "0.1"
+version = "0.3"
path = "../winit"
[dependencies.iced_graphics]
-version = "0.1"
+version = "0.2"
path = "../graphics"
features = ["opengl"]
diff --git a/glutin/README.md b/glutin/README.md
index 34dec1b3..fcae157e 100644
--- a/glutin/README.md
+++ b/glutin/README.md
@@ -1,24 +1,26 @@
-# `iced_winit`
-[![Documentation](https://docs.rs/iced_winit/badge.svg)][documentation]
-[![Crates.io](https://img.shields.io/crates/v/iced_winit.svg)](https://crates.io/crates/iced_winit)
-[![License](https://img.shields.io/crates/l/iced_winit.svg)](https://github.com/hecrj/iced/blob/master/LICENSE)
+# `iced_glutin`
+[![Documentation](https://docs.rs/iced_glutin/badge.svg)][documentation]
+[![Crates.io](https://img.shields.io/crates/v/iced_glutin.svg)](https://crates.io/crates/iced_glutin)
+[![License](https://img.shields.io/crates/l/iced_glutin.svg)](https://github.com/hecrj/iced/blob/master/LICENSE)
[![project chat](https://img.shields.io/badge/chat-on_zulip-brightgreen.svg)](https://iced.zulipchat.com)
-`iced_winit` offers some convenient abstractions on top of [`iced_native`] to quickstart development when using [`winit`].
+`iced_glutin` offers some convenient abstractions on top of [`iced_native`] to quickstart development when using [`glutin`].
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
+[documentation]: https://docs.rs/iced_glutin
[`iced_native`]: ../native
-[`winit`]: https://github.com/rust-windowing/winit
+[`glutin`]: https://github.com/rust-windowing/glutin
## Installation
-Add `iced_winit` as a dependency in your `Cargo.toml`:
+Add `iced_glutin` as a dependency in your `Cargo.toml`:
```toml
-iced_winit = "0.1"
+iced_glutin = "0.2"
```
__Iced moves fast and the `master` branch can contain breaking changes!__ If
diff --git a/glutin/src/application.rs b/glutin/src/application.rs
index fe6ad99d..4a5f4bd2 100644
--- a/glutin/src/application.rs
+++ b/glutin/src/application.rs
@@ -1,18 +1,20 @@
//! Create interactive, native cross-platform applications.
-use crate::{mouse, Error, Executor, Runtime, Size};
+use crate::{mouse, Error, Executor, Runtime};
+
+pub use iced_winit::Application;
+
use iced_graphics::window;
-use iced_graphics::Viewport;
use iced_winit::application;
use iced_winit::conversion;
-use iced_winit::{Clipboard, Debug, Proxy, Settings};
+use iced_winit::futures;
+use iced_winit::futures::channel::mpsc;
+use iced_winit::{Cache, Clipboard, Debug, Proxy, Settings};
-pub use iced_winit::Application;
-pub use iced_winit::{program, Program};
+use glutin::window::Window;
+use std::mem::ManuallyDrop;
/// 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,
@@ -22,11 +24,10 @@ where
E: Executor + 'static,
C: window::GLCompositor<Renderer = A::Renderer> + 'static,
{
- use glutin::{
- event,
- event_loop::{ControlFlow, EventLoop},
- ContextBuilder,
- };
+ use futures::task;
+ use futures::Future;
+ use glutin::event_loop::EventLoop;
+ use glutin::ContextBuilder;
let mut debug = Debug::new();
debug.startup_started();
@@ -39,24 +40,27 @@ where
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 context = {
- let builder = settings.window.into_builder(
- &title,
- mode,
- event_loop.primary_monitor(),
- );
+ let builder = settings
+ .window
+ .into_builder(
+ &application.title(),
+ application.mode(),
+ event_loop.primary_monitor(),
+ settings.id,
+ )
+ .with_menu(Some(conversion::menu(&application.menu())));
let context = ContextBuilder::new()
.with_vsync(true)
@@ -79,189 +83,274 @@ where
}
};
- let clipboard = Clipboard::new(&context.window());
- let mut cursor_position = glutin::dpi::PhysicalPosition::new(-1.0, -1.0);
- let mut mouse_interaction = mouse::Interaction::default();
- let mut modifiers = glutin::event::ModifiersState::default();
-
- let physical_size = context.window().inner_size();
- let mut viewport = Viewport::with_physical_size(
- Size::new(physical_size.width, physical_size.height),
- context.window().scale_factor() * scale_factor,
- );
- let mut resized = false;
-
#[allow(unsafe_code)]
- let (mut compositor, mut renderer) = unsafe {
+ let (compositor, renderer) = unsafe {
C::new(compositor_settings, |address| {
context.get_proc_address(address)
})?
};
- let mut state = program::State::new(
+ let (mut sender, receiver) = mpsc::unbounded();
+
+ 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,
+ context,
+ settings.exit_on_close_request,
+ ));
+
+ let mut context = task::Context::from_waker(task::noop_waker_ref());
+
+ event_loop.run(move |event, _, control_flow| {
+ use glutin::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 {
+ glutin::event::Event::WindowEvent {
+ event:
+ glutin::event::WindowEvent::ScaleFactorChanged {
+ new_inner_size,
+ ..
+ },
+ window_id,
+ } => Some(glutin::event::Event::WindowEvent {
+ event: glutin::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<glutin::event::Event<'_, A::Message>>,
+ mut context: glutin::ContextWrapper<glutin::PossiblyCurrent, Window>,
+ exit_on_close_request: bool,
+) where
+ A: Application + 'static,
+ E: Executor + 'static,
+ C: window::GLCompositor<Renderer = A::Renderer> + 'static,
+{
+ use glutin::event;
+ use iced_winit::futures::stream::StreamExt;
+
+ let mut clipboard = Clipboard::connect(context.window());
+
+ let mut state = application::State::new(&application, context.window());
+ let mut viewport_version = state.viewport_version();
+ let mut user_interface =
+ ManuallyDrop::new(application::build_user_interface(
+ &mut application,
+ Cache::default(),
+ &mut renderer,
+ state.logical_size(),
+ &mut debug,
+ ));
+
+ let mut primitive =
+ user_interface.draw(&mut renderer, state.cursor_position());
+ let mut mouse_interaction = mouse::Interaction::default();
- // If the application was updated
- if let Some(command) = command {
- runtime.spawn(command);
+ let mut events = Vec::new();
+ let mut messages = Vec::new();
- let program = state.program();
+ debug.startup_finished();
+
+ while let Some(event) = receiver.next().await {
+ match event {
+ event::Event::MainEventsCleared => {
+ if events.is_empty() && messages.is_empty() {
+ continue;
+ }
- // Update subscriptions
- let subscription = program.subscription();
- runtime.track(subscription);
+ debug.event_processing_started();
- // Update window title
- let new_title = program.title();
+ let statuses = user_interface.update(
+ &events,
+ state.cursor_position(),
+ &mut renderer,
+ &mut clipboard,
+ &mut messages,
+ );
- if title != new_title {
- context.window().set_title(&new_title);
+ debug.event_processing_finished();
- title = new_title;
+ for event in events.drain(..).zip(statuses.into_iter()) {
+ runtime.broadcast(event);
}
- // Update window mode
- let new_mode = program.mode();
+ if !messages.is_empty() {
+ let cache =
+ ManuallyDrop::into_inner(user_interface).into_cache();
- if mode != new_mode {
- context.window().set_fullscreen(conversion::fullscreen(
- context.window().current_monitor(),
- new_mode,
- ));
+ // Update application
+ application::update(
+ &mut application,
+ &mut runtime,
+ &mut debug,
+ &mut clipboard,
+ &mut messages,
+ );
- mode = new_mode;
+ // Update window
+ state.synchronize(&application, context.window());
+
+ let should_exit = application.should_exit();
+
+ user_interface =
+ ManuallyDrop::new(application::build_user_interface(
+ &mut application,
+ cache,
+ &mut renderer,
+ state.logical_size(),
+ &mut debug,
+ ));
+
+ if should_exit {
+ break;
+ }
}
- // Update background color
- background_color = program.background_color();
+ debug.draw_started();
+ primitive =
+ user_interface.draw(&mut renderer, state.cursor_position());
+ debug.draw_finished();
- // Update scale factor
- let new_scale_factor = program.scale_factor();
+ context.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(_) => {
+ debug.render_started();
+
+ #[allow(unsafe_code)]
+ unsafe {
+ if !context.is_current() {
+ context = context
+ .make_current()
+ .expect("Make OpenGL context current");
+ }
+ }
- if scale_factor != new_scale_factor {
- let size = context.window().inner_size();
+ let current_viewport_version = state.viewport_version();
- viewport = Viewport::with_physical_size(
- Size::new(size.width, size.height),
- context.window().scale_factor() * new_scale_factor,
- );
+ if viewport_version != current_viewport_version {
+ let physical_size = state.physical_size();
+ let logical_size = state.logical_size();
- // 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 _),
- &mut renderer,
- &mut debug,
+ debug.layout_started();
+ user_interface = ManuallyDrop::new(
+ ManuallyDrop::into_inner(user_interface)
+ .relayout(logical_size, &mut renderer),
);
+ debug.layout_finished();
- scale_factor = new_scale_factor;
- }
- }
-
- context.window().request_redraw();
- }
- event::Event::UserEvent(message) => {
- state.queue_message(message);
- }
- event::Event::RedrawRequested(_) => {
- debug.render_started();
+ debug.draw_started();
+ primitive = user_interface
+ .draw(&mut renderer, state.cursor_position());
+ debug.draw_finished();
- if resized {
- let physical_size = viewport.physical_size();
+ context.resize(glutin::dpi::PhysicalSize::new(
+ physical_size.width,
+ physical_size.height,
+ ));
- context.resize(glutin::dpi::PhysicalSize::new(
- physical_size.width,
- physical_size.height,
- ));
+ compositor.resize_viewport(physical_size);
- compositor.resize_viewport(physical_size);
+ viewport_version = current_viewport_version;
+ }
- resized = false;
- }
+ let new_mouse_interaction = compositor.draw(
+ &mut renderer,
+ state.viewport(),
+ state.background_color(),
+ &primitive,
+ &debug.overlay(),
+ );
- let new_mouse_interaction = compositor.draw(
- &mut renderer,
- &viewport,
- background_color,
- state.primitive(),
- &debug.overlay(),
- );
+ context.swap_buffers().expect("Swap buffers");
- context.swap_buffers().expect("Swap buffers");
+ debug.render_finished();
- debug.render_finished();
+ if new_mouse_interaction != mouse_interaction {
+ context.window().set_cursor_icon(
+ conversion::mouse_interaction(new_mouse_interaction),
+ );
- if new_mouse_interaction != mouse_interaction {
- context.window().set_cursor_icon(
- conversion::mouse_interaction(new_mouse_interaction),
- );
+ mouse_interaction = new_mouse_interaction;
+ }
- mouse_interaction = new_mouse_interaction;
+ // TODO: Handle animations!
+ // Maybe we can use `ControlFlow::WaitUntil` for this.
}
+ 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 application::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,
- ..
- } => {
- application::handle_window_event(
- &window_event,
- context.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(context.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));
}
diff --git a/glutin/src/lib.rs b/glutin/src/lib.rs
index 49bc2a33..107d9556 100644
--- a/glutin/src/lib.rs
+++ b/glutin/src/lib.rs
@@ -1,5 +1,7 @@
//! A windowing shell for [`iced`], on top of [`glutin`].
//!
+//! ![The native path of the Iced ecosystem](https://github.com/hecrj/iced/blob/0525d76ff94e828b7b21634fa94a747022001c83/docs/graphs/native.png?raw=true)
+//!
//! [`iced`]: https://github.com/hecrj/iced
//! [`glutin`]: https://github.com/rust-windowing/glutin
#![deny(missing_docs)]
@@ -15,7 +17,7 @@ pub use iced_native::*;
pub mod application;
pub use iced_winit::settings;
-pub use iced_winit::{Error, Mode};
+pub use iced_winit::{Clipboard, Error, Mode};
#[doc(no_inline)]
pub use application::Application;