summaryrefslogtreecommitdiffstats
path: root/glutin/src/application.rs
blob: 63d41573f5f12a71230c9d9b8e606e627f7a84cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! Create interactive, native cross-platform applications.
use crate::{mouse, Executor, Runtime, Size};
use iced_graphics::window;
use iced_graphics::Viewport;
use iced_winit::application;
use iced_winit::conversion;
use iced_winit::{Clipboard, Debug, Proxy, Settings};

pub use iced_winit::Application;
pub use iced_winit::{program, Program};

/// 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,
) where
    A: Application + 'static,
    E: Executor + 'static,
    C: window::GLCompositor<Renderer = A::Renderer> + 'static,
{
    use glutin::{
        event,
        event_loop::{ControlFlow, EventLoop},
        ContextBuilder,
    };

    let mut debug = Debug::new();
    debug.startup_started();

    let event_loop = EventLoop::with_user_event();
    let mut runtime = {
        let executor = E::new().expect("Create executor");
        let proxy = Proxy::new(event_loop.create_proxy());

        Runtime::new(executor, proxy)
    };

    let flags = settings.flags;
    let (application, init_command) = runtime.enter(|| A::new(flags));
    runtime.spawn(init_command);

    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 context = {
        let builder = settings.window.into_builder(
            &title,
            mode,
            event_loop.primary_monitor(),
        );

        let context = ContextBuilder::new()
            .with_vsync(true)
            .with_multisampling(C::sample_count(&compositor_settings) as u16)
            .build_windowed(builder, &event_loop)
            .expect("Open window");

        #[allow(unsafe_code)]
        unsafe {
            context.make_current().expect("Make OpenGL context current")
        }
    };

    let clipboard = Clipboard::new(&context.window());
    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(),
    );
    let mut resized = false;

    #[allow(unsafe_code)]
    let (mut compositor, mut renderer) = unsafe {
        C::new(compositor_settings, |address| {
            context.get_proc_address(address)
        })
    };

    let mut state = program::State::new(
        application,
        viewport.logical_size(),
        &mut renderer,
        &mut debug,
    );
    debug.startup_finished();

    event_loop.run(move |event, _, control_flow| match event {
        event::Event::MainEventsCleared => {
            if state.is_queue_empty() {
                return;
            }

            let command = runtime.enter(|| {
                state.update(
                    clipboard.as_ref().map(|c| c as _),
                    viewport.logical_size(),
                    &mut renderer,
                    &mut debug,
                )
            });

            // If the application was updated
            if let Some(command) = command {
                runtime.spawn(command);

                let program = state.program();

                // Update subscriptions
                let subscription = program.subscription();
                runtime.track(subscription);

                // Update window title
                let new_title = program.title();

                if title != new_title {
                    context.window().set_title(&new_title);

                    title = new_title;
                }

                // Update window mode
                let new_mode = program.mode();

                if mode != new_mode {
                    context.window().set_fullscreen(conversion::fullscreen(
                        context.window().current_monitor(),
                        new_mode,
                    ));

                    mode = new_mode;
                }

                // Update background color
                background_color = program.background_color();
            }

            context.window().request_redraw();
        }
        event::Event::UserEvent(message) => {
            state.queue_message(message);
        }
        event::Event::RedrawRequested(_) => {
            debug.render_started();

            if resized {
                let physical_size = viewport.physical_size();

                context.resize(glutin::dpi::PhysicalSize::new(
                    physical_size.width,
                    physical_size.height,
                ));

                compositor.resize_viewport(physical_size);

                resized = false;
            }

            let new_mouse_interaction = compositor.draw(
                &mut renderer,
                &viewport,
                background_color,
                state.primitive(),
                &debug.overlay(),
            );

            context.swap_buffers().expect("Swap buffers");

            debug.render_finished();

            if new_mouse_interaction != mouse_interaction {
                context.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.
        }
        event::Event::WindowEvent {
            event: window_event,
            ..
        } => {
            application::handle_window_event(
                &window_event,
                context.window(),
                control_flow,
                &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);
            }
        }
        _ => {
            *control_flow = ControlFlow::Wait;
        }
    })
}