mirror of
https://github.com/danbulant/appit
synced 2026-06-24 17:11:54 +00:00
wip
This commit is contained in:
parent
e7d01c02bb
commit
272d1c7462
2 changed files with 53 additions and 58 deletions
101
src/lib.rs
101
src/lib.rs
|
|
@ -11,11 +11,10 @@ pub use winit;
|
||||||
use raw_window_handle::HasRawWindowHandle;
|
use raw_window_handle::HasRawWindowHandle;
|
||||||
pub use window::{RunningWindow, Window, WindowAttributes, WindowBehavior, WindowBuilder};
|
pub use window::{RunningWindow, Window, WindowAttributes, WindowBehavior, WindowBuilder};
|
||||||
|
|
||||||
use winit::error::OsError;
|
use winit::error::{EventLoopError, OsError};
|
||||||
use winit::window::WindowId;
|
use winit::window::WindowId;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::process::exit;
|
|
||||||
use std::sync::{mpsc, Arc, Mutex, PoisonError};
|
use std::sync::{mpsc, Arc, Mutex, PoisonError};
|
||||||
use winit::event_loop::{ControlFlow, EventLoopBuilder, EventLoopProxy, EventLoopWindowTarget};
|
use winit::event_loop::{ControlFlow, EventLoopBuilder, EventLoopProxy, EventLoopWindowTarget};
|
||||||
use winit::{event::Event, event_loop::EventLoop};
|
use winit::{event::Event, event_loop::EventLoop};
|
||||||
|
|
@ -82,58 +81,54 @@ where
|
||||||
/// Begins running the application. This function will never return.
|
/// Begins running the application. This function will never return.
|
||||||
///
|
///
|
||||||
/// Internally this runs the [`EventLoop`].
|
/// Internally this runs the [`EventLoop`].
|
||||||
pub fn run(mut self) -> ! {
|
pub fn run(mut self) -> Result<(), EventLoopError> {
|
||||||
self.event_loop
|
self.event_loop.run(move |event, target, control_flow| {
|
||||||
.run(move |event, target, control_flow| {
|
*control_flow = ControlFlow::Wait;
|
||||||
*control_flow = ControlFlow::Wait;
|
match event {
|
||||||
match event {
|
Event::WindowEvent { window_id, event } => {
|
||||||
Event::WindowEvent { window_id, event } => {
|
let event = WindowEvent::from(event);
|
||||||
let event = WindowEvent::from(event);
|
self.running
|
||||||
self.running
|
.windows
|
||||||
.windows
|
.send(window_id, WindowMessage::Event(event));
|
||||||
.send(window_id, WindowMessage::Event(event));
|
|
||||||
}
|
|
||||||
Event::RedrawRequested(window_id) => {
|
|
||||||
self.running.windows.send(window_id, WindowMessage::Redraw);
|
|
||||||
}
|
|
||||||
Event::UserEvent(message) => match message {
|
|
||||||
EventLoopMessage::CloseWindow(window_id) => {
|
|
||||||
if self.running.windows.close(window_id) {
|
|
||||||
*control_flow = ControlFlow::ExitWithCode(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EventLoopMessage::WindowPanic(window_id) => {
|
|
||||||
if self.running.windows.close(window_id) {
|
|
||||||
*control_flow = ControlFlow::ExitWithCode(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
EventLoopMessage::OpenWindow {
|
|
||||||
attrs,
|
|
||||||
sender,
|
|
||||||
open_sender,
|
|
||||||
} => {
|
|
||||||
let result = self.running.windows.open(target, attrs, sender);
|
|
||||||
let _result = open_sender.send(result);
|
|
||||||
}
|
|
||||||
EventLoopMessage::User {
|
|
||||||
message,
|
|
||||||
response_sender,
|
|
||||||
} => {
|
|
||||||
let _result = response_sender
|
|
||||||
.send((self.message_callback)(message, &self.running.windows));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Event::NewEvents(_)
|
|
||||||
| Event::DeviceEvent { .. }
|
|
||||||
| Event::Suspended
|
|
||||||
| Event::Resumed
|
|
||||||
| Event::LoopExiting
|
|
||||||
| Event::AboutToWait => {}
|
|
||||||
}
|
}
|
||||||
})
|
Event::RedrawRequested(window_id) => {
|
||||||
.unwrap();
|
self.running.windows.send(window_id, WindowMessage::Redraw);
|
||||||
// TODO do we want to forward the change to a result
|
}
|
||||||
exit(0);
|
Event::UserEvent(message) => match message {
|
||||||
|
EventLoopMessage::CloseWindow(window_id) => {
|
||||||
|
if self.running.windows.close(window_id) {
|
||||||
|
*control_flow = ControlFlow::ExitWithCode(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EventLoopMessage::WindowPanic(window_id) => {
|
||||||
|
if self.running.windows.close(window_id) {
|
||||||
|
*control_flow = ControlFlow::ExitWithCode(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
EventLoopMessage::OpenWindow {
|
||||||
|
attrs,
|
||||||
|
sender,
|
||||||
|
open_sender,
|
||||||
|
} => {
|
||||||
|
let result = self.running.windows.open(target, attrs, sender);
|
||||||
|
let _result = open_sender.send(result);
|
||||||
|
}
|
||||||
|
EventLoopMessage::User {
|
||||||
|
message,
|
||||||
|
response_sender,
|
||||||
|
} => {
|
||||||
|
let _result = response_sender
|
||||||
|
.send((self.message_callback)(message, &self.running.windows));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Event::NewEvents(_)
|
||||||
|
| Event::DeviceEvent { .. }
|
||||||
|
| Event::Suspended
|
||||||
|
| Event::Resumed
|
||||||
|
| Event::LoopExiting
|
||||||
|
| Event::AboutToWait => {}
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ use std::thread;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use winit::dpi::{PhysicalPosition, PhysicalSize, Position, Size};
|
use winit::dpi::{PhysicalPosition, PhysicalSize, Position, Size};
|
||||||
use winit::error::OsError;
|
use winit::error::{EventLoopError, OsError};
|
||||||
use winit::event::{
|
use winit::event::{
|
||||||
AxisId, DeviceId, ElementState, Ime, KeyEvent, Modifiers, MouseButton, MouseScrollDelta, Touch,
|
AxisId, DeviceId, ElementState, Ime, KeyEvent, Modifiers, MouseButton, MouseScrollDelta, Touch,
|
||||||
TouchPhase,
|
TouchPhase,
|
||||||
|
|
@ -774,7 +774,7 @@ where
|
||||||
fn run_with_event_callback(
|
fn run_with_event_callback(
|
||||||
app_callback: impl FnMut(AppMessage, &Windows<AppMessage::Window>) -> AppMessage::Response
|
app_callback: impl FnMut(AppMessage, &Windows<AppMessage::Window>) -> AppMessage::Response
|
||||||
+ 'static,
|
+ 'static,
|
||||||
) -> !
|
) -> Result<(), EventLoopError>
|
||||||
where
|
where
|
||||||
Self::Context: Default,
|
Self::Context: Default,
|
||||||
{
|
{
|
||||||
|
|
@ -795,7 +795,7 @@ where
|
||||||
context: Self::Context,
|
context: Self::Context,
|
||||||
app_callback: impl FnMut(AppMessage, &Windows<AppMessage::Window>) -> AppMessage::Response
|
app_callback: impl FnMut(AppMessage, &Windows<AppMessage::Window>) -> AppMessage::Response
|
||||||
+ 'static,
|
+ 'static,
|
||||||
) -> ! {
|
) -> Result<(), EventLoopError> {
|
||||||
let app = PendingApp::new_with_event_callback(app_callback);
|
let app = PendingApp::new_with_event_callback(app_callback);
|
||||||
Self::open_with(&app, context).expect("error opening initial window");
|
Self::open_with(&app, context).expect("error opening initial window");
|
||||||
app.run()
|
app.run()
|
||||||
|
|
@ -1020,7 +1020,7 @@ pub trait Run: WindowBehavior<()> {
|
||||||
///
|
///
|
||||||
/// This function is shorthand for creating a [`PendingApp`], opening this
|
/// This function is shorthand for creating a [`PendingApp`], opening this
|
||||||
/// window inside of it, and running the pending app.
|
/// window inside of it, and running the pending app.
|
||||||
fn run() -> !
|
fn run() -> Result<(), EventLoopError>
|
||||||
where
|
where
|
||||||
Self::Context: Default,
|
Self::Context: Default,
|
||||||
{
|
{
|
||||||
|
|
@ -1033,7 +1033,7 @@ pub trait Run: WindowBehavior<()> {
|
||||||
///
|
///
|
||||||
/// This function is shorthand for creating a [`PendingApp`], opening this
|
/// This function is shorthand for creating a [`PendingApp`], opening this
|
||||||
/// window inside of it, and running the pending app.
|
/// window inside of it, and running the pending app.
|
||||||
fn run_with(context: Self::Context) -> ! {
|
fn run_with(context: Self::Context) -> Result<(), EventLoopError> {
|
||||||
let app = PendingApp::new();
|
let app = PendingApp::new();
|
||||||
Self::open_with(&app, context).expect("error opening initial window");
|
Self::open_with(&app, context).expect("error opening initial window");
|
||||||
app.run()
|
app.run()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue