Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,17 @@ jobs:
!contains(matrix.platform.target, 'redox') &&
matrix.rust_version != '1.60.0'
run: cargo $CMD test --verbose --target ${{ matrix.platform.target }} $OPTIONS --features serde,$FEATURES

# derived from https://github.com/psychon/x11rb/blob/15153e514a4e3e4c116279f76ab1c3635abc05e5/.github/workflows/CI.yml#L141-L159
- name: Run examples
shell: bash
if: >
contains(matrix.platform.target, 'linux') &&
contains(matrix.platform.features, 'x11') &&
(contains(matrix.rust_version, 'stable') || contains(matrix.rust_version, 'nightly'))
run: |
for example in examples/*.rs; do
example=${example/examples\//}
example=${example/.rs/}
WINIT_EXAMPLE_TIMEOUT=1 xvfb-run -a cargo run --example "$example" || exit 1
done
7 changes: 7 additions & 0 deletions examples/child_window.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(any(x11_platform, macos_platform, windows_platform))]
include!("it_util/timeout.rs");

#[cfg(any(x11_platform, macos_platform, windows_platform))]
fn main() {
use std::collections::HashMap;
Expand Down Expand Up @@ -39,6 +42,7 @@ fn main() {
.with_inner_size(LogicalSize::new(640.0f32, 480.0f32))
.build(&event_loop)
.unwrap();
util::start_timeout_thread(&event_loop, ());

println!("parent window: {parent_window:?})");

Expand Down Expand Up @@ -70,6 +74,9 @@ fn main() {
}
_ => (),
}
} else if let Event::UserEvent(()) = event {
windows.clear();
*control_flow = ControlFlow::Exit;
}
})
}
Expand Down
7 changes: 7 additions & 0 deletions examples/control_flow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(clippy::single_match)]

include!("it_util/timeout.rs");

use std::{thread, time};

use simple_logger::SimpleLogger;
Expand Down Expand Up @@ -34,6 +36,8 @@ fn main() {
.build(&event_loop)
.unwrap();

util::start_timeout_thread(&event_loop, ());

let mut mode = Mode::Wait;
let mut request_redraw = false;
let mut wait_cancelled = false;
Expand Down Expand Up @@ -108,6 +112,9 @@ fn main() {
}
};
}
Event::UserEvent(()) => {
control_flow.set_exit();
}
_ => (),
}
});
Expand Down
6 changes: 6 additions & 0 deletions examples/cursor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(clippy::single_match)]

include!("it_util/timeout.rs");

use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, KeyboardInput, WindowEvent},
Expand All @@ -10,6 +12,7 @@ use winit::{
fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();
util::start_timeout_thread(&event_loop, ());

let window = WindowBuilder::new().build(&event_loop).unwrap();
window.set_title("A fantastic window!");
Expand Down Expand Up @@ -46,6 +49,9 @@ fn main() {
} => {
control_flow.set_exit();
}
Event::UserEvent(()) => {
control_flow.set_exit();
}
_ => (),
}
});
Expand Down
4 changes: 4 additions & 0 deletions examples/cursor_grab.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(clippy::single_match)]

include!("it_util/timeout.rs");

use simple_logger::SimpleLogger;
use winit::{
event::{DeviceEvent, ElementState, Event, KeyboardInput, ModifiersState, WindowEvent},
Expand All @@ -10,6 +12,7 @@ use winit::{
fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();
util::start_timeout_thread(&event_loop, ());

let window = WindowBuilder::new()
.with_title("Super Cursor Grab'n'Hide Simulator 9000")
Expand Down Expand Up @@ -64,6 +67,7 @@ fn main() {
},
_ => (),
},
Event::UserEvent(()) => control_flow.set_exit(),
_ => (),
}
});
Expand Down
11 changes: 10 additions & 1 deletion examples/custom_events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![allow(clippy::single_match)]

#[cfg(not(wasm_platform))]
include!("it_util/timeout.rs");

#[cfg(not(wasm_platform))]
fn main() {
use simple_logger::SimpleLogger;
Expand All @@ -12,10 +15,12 @@ fn main() {
#[derive(Debug, Clone, Copy)]
enum CustomEvent {
Timer,
Timeout,
}

SimpleLogger::new().init().unwrap();
let event_loop = EventLoopBuilder::<CustomEvent>::with_user_event().build();
util::start_timeout_thread(&event_loop, CustomEvent::Timeout);

let _window = WindowBuilder::new()
.with_title("A fantastic window!")
Expand All @@ -39,7 +44,11 @@ fn main() {
control_flow.set_wait();

match event {
Event::UserEvent(event) => println!("user event: {event:?}"),
Event::UserEvent(CustomEvent::Timer) => println!("user event: {event:?}"),
Event::UserEvent(CustomEvent::Timeout) => {
println!("user event: {event:?}");
control_flow.set_exit();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
Expand Down
4 changes: 4 additions & 0 deletions examples/drag_window.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(clippy::single_match)]

include!("it_util/timeout.rs");

use simple_logger::SimpleLogger;
use winit::{
event::{
Expand All @@ -12,6 +14,7 @@ use winit::{
fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();
util::start_timeout_thread(&event_loop, ());

let window_1 = WindowBuilder::new().build(&event_loop).unwrap();
let window_2 = WindowBuilder::new().build(&event_loop).unwrap();
Expand Down Expand Up @@ -59,6 +62,7 @@ fn main() {
}
_ => (),
},
Event::UserEvent(()) => control_flow.set_exit(),
_ => (),
});
}
Expand Down
4 changes: 4 additions & 0 deletions examples/fullscreen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(clippy::single_match)]

include!("it_util/timeout.rs");

use simple_logger::SimpleLogger;
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::event_loop::EventLoop;
Expand All @@ -8,6 +10,7 @@ use winit::window::{Fullscreen, WindowBuilder};
fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();
util::start_timeout_thread(&event_loop, ());

let mut decorations = true;
let mut minimized = false;
Expand Down Expand Up @@ -107,6 +110,7 @@ fn main() {
},
_ => (),
},
Event::UserEvent(()) => control_flow.set_exit(),
_ => {}
}
});
Expand Down
4 changes: 4 additions & 0 deletions examples/handling_close.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(clippy::single_match)]

include!("it_util/timeout.rs");

use simple_logger::SimpleLogger;
use winit::{
event::{Event, KeyboardInput, WindowEvent},
Expand All @@ -10,6 +12,7 @@ use winit::{
fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();
util::start_timeout_thread(&event_loop, ());

let _window = WindowBuilder::new()
.with_title("Your faithful window")
Expand Down Expand Up @@ -80,6 +83,7 @@ fn main() {
_ => (),
}
}
Event::UserEvent(()) => control_flow.set_exit(),
_ => (),
}
});
Expand Down
6 changes: 6 additions & 0 deletions examples/ime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(clippy::single_match)]

include!("it_util/timeout.rs");

use log::LevelFilter;
use simple_logger::SimpleLogger;
use winit::{
Expand All @@ -21,6 +23,7 @@ fn main() {
println!("Press F3 to cycle through IME purposes.");

let event_loop = EventLoop::new();
util::start_timeout_thread(&event_loop, ());

let window = WindowBuilder::new()
.with_inner_size(winit::dpi::LogicalSize::new(256f64, 128f64))
Expand Down Expand Up @@ -106,6 +109,9 @@ fn main() {
println!("\nIME purpose: {ime_purpose:?}\n");
}
}
Event::UserEvent(()) => {
control_flow.set_exit();
}
_ => (),
}
});
Expand Down
42 changes: 42 additions & 0 deletions examples/it_util/timeout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Spawns a timeout thread that will close the event loop after one second.

#[cfg(not(wasm_platform))]
mod util {
use std::env;
use std::thread;
use winit::event_loop::EventLoop;

pub(super) fn start_timeout_thread<T: Send + 'static>(event_loop: &EventLoop<T>, msg_to_send: T) {
// If the WINIT_EXAMPLE_TIMEOUT environment variable is set, get the number of seconds
// to wait before closing the window.
let secs = match env::var("WINIT_EXAMPLE_TIMEOUT")
.ok()
.and_then(|s| s.parse::<u64>().ok())
{
Some(secs) => secs,
None => return,
};

// Spawn a thread that will close the window after `secs` seconds.
thread::Builder::new()
.name("winit example timeout".to_string())
.spawn({
let proxy = event_loop.create_proxy();
move || {
thread::sleep(std::time::Duration::from_secs(secs));
println!("Closing window due to timeout");
proxy.send_event(msg_to_send).unwrap_or_else(|_| panic!("Failed to send event to event loop"));
}
})
.expect("failed to spawn timeout thread");
}
}

#[cfg(wasm_platform)]
mod util {
use winit::event_loop::EventLoop;

pub(super) fn start_timeout_thread<T: Send + 'static>(_event_loop: &EventLoop<T>, _msg_to_send: T) {
// Not supported on web.
}
}
6 changes: 6 additions & 0 deletions examples/mouse_wheel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(clippy::single_match)]

include!("it_util/timeout.rs");

use simple_logger::SimpleLogger;
use winit::{
event::{Event, WindowEvent},
Expand All @@ -10,6 +12,7 @@ use winit::{
fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();
util::start_timeout_thread(&event_loop, ());

let window = WindowBuilder::new()
.with_title("Mouse Wheel events")
Expand Down Expand Up @@ -56,6 +59,9 @@ In other words, the deltas indicate the direction in which to move the content (
},
_ => (),
},
Event::UserEvent(()) => {
control_flow.set_exit();
}
_ => (),
}
});
Expand Down
9 changes: 9 additions & 0 deletions examples/multithreaded.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![allow(clippy::single_match)]

#[cfg(not(wasm_platform))]
include!("it_util/timeout.rs");

#[cfg(not(wasm_platform))]
fn main() {
use std::{collections::HashMap, sync::mpsc, thread, time::Duration};
Expand All @@ -17,6 +20,8 @@ fn main() {

SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();
util::start_timeout_thread(&event_loop, ());

let mut window_senders = HashMap::with_capacity(WINDOW_COUNT);
for _ in 0..WINDOW_COUNT {
let window = WindowBuilder::new()
Expand Down Expand Up @@ -188,6 +193,10 @@ fn main() {
}
}
},
Event::UserEvent(()) => {
window_senders.clear();
control_flow.set_exit();
}
_ => {}
}
})
Expand Down
6 changes: 6 additions & 0 deletions examples/multiwindow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![allow(clippy::single_match)]

include!("it_util/timeout.rs");

use std::collections::HashMap;

use simple_logger::SimpleLogger;
Expand All @@ -12,6 +14,7 @@ use winit::{
fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();
util::start_timeout_thread(&event_loop, ());

let mut windows = HashMap::new();
for _ in 0..3 {
Expand Down Expand Up @@ -55,6 +58,9 @@ fn main() {
_ => (),
}
}
Event::UserEvent(()) => {
control_flow.set_exit();
}
_ => (),
}
})
Expand Down
Loading