mirror of
https://github.com/rust-windowing/winit.git
synced 2026-08-29 04:40:04 -04:00
Reduce the use of custom cfgs in examples
The `x11_platform`/`wayland_platform` cfgs remain (those are a lot more involved, so it might still makes sense to use them for clarity).
This commit is contained in:
@@ -10,7 +10,7 @@ use std::fmt::Debug;
|
|||||||
use std::num::NonZeroU32;
|
use std::num::NonZeroU32;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::mpsc::{self, Receiver, Sender};
|
use std::sync::mpsc::{self, Receiver, Sender};
|
||||||
#[cfg(not(web_platform))]
|
#[cfg(not(target_family = "wasm"))]
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use std::{fmt, mem};
|
use std::{fmt, mem};
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ use cursor_icon::CursorIcon;
|
|||||||
use rwh_06::{DisplayHandle, HasDisplayHandle};
|
use rwh_06::{DisplayHandle, HasDisplayHandle};
|
||||||
use softbuffer::{Context, Surface};
|
use softbuffer::{Context, Surface};
|
||||||
use tracing::{error, info};
|
use tracing::{error, info};
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
use web_time::Instant;
|
use web_time::Instant;
|
||||||
use winit::application::ApplicationHandler;
|
use winit::application::ApplicationHandler;
|
||||||
use winit::cursor::{Cursor, CustomCursor, CustomCursorSource};
|
use winit::cursor::{Cursor, CustomCursor, CustomCursorSource};
|
||||||
@@ -29,13 +29,15 @@ use winit::event_loop::{ActiveEventLoop, EventLoop};
|
|||||||
use winit::icon::{Icon, RgbaIcon};
|
use winit::icon::{Icon, RgbaIcon};
|
||||||
use winit::keyboard::{Key, ModifiersState};
|
use winit::keyboard::{Key, ModifiersState};
|
||||||
use winit::monitor::Fullscreen;
|
use winit::monitor::Fullscreen;
|
||||||
#[cfg(macos_platform)]
|
#[cfg(all(target_vendor = "apple", not(target_os = "macos")))]
|
||||||
|
use winit::platform::ios::WindowExtIOS;
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
use winit::platform::macos::{OptionAsAlt, WindowAttributesMacOS, WindowExtMacOS};
|
use winit::platform::macos::{OptionAsAlt, WindowAttributesMacOS, WindowExtMacOS};
|
||||||
#[cfg(any(x11_platform, wayland_platform))]
|
#[cfg(any(x11_platform, wayland_platform))]
|
||||||
use winit::platform::startup_notify::{self, EventLoopExtStartupNotify, WindowExtStartupNotify};
|
use winit::platform::startup_notify::{self, EventLoopExtStartupNotify, WindowExtStartupNotify};
|
||||||
#[cfg(wayland_platform)]
|
#[cfg(wayland_platform)]
|
||||||
use winit::platform::wayland::{ActiveEventLoopExtWayland, WindowAttributesWayland};
|
use winit::platform::wayland::{ActiveEventLoopExtWayland, WindowAttributesWayland};
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
use winit::platform::web::{ActiveEventLoopExtWeb, WindowAttributesWeb};
|
use winit::platform::web::{ActiveEventLoopExtWeb, WindowAttributesWeb};
|
||||||
#[cfg(x11_platform)]
|
#[cfg(x11_platform)]
|
||||||
use winit::platform::x11::{ActiveEventLoopExtX11, WindowAttributesX11};
|
use winit::platform::x11::{ActiveEventLoopExtX11, WindowAttributesX11};
|
||||||
@@ -52,7 +54,7 @@ mod fill;
|
|||||||
const BORDER_SIZE: f64 = 20.;
|
const BORDER_SIZE: f64 = 20.;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
console_error_panic_hook::set_once();
|
console_error_panic_hook::set_once();
|
||||||
|
|
||||||
tracing_init::init();
|
tracing_init::init();
|
||||||
@@ -61,7 +63,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let (sender, receiver) = mpsc::channel();
|
let (sender, receiver) = mpsc::channel();
|
||||||
|
|
||||||
// Wire the user event from another thread.
|
// Wire the user event from another thread.
|
||||||
#[cfg(not(web_platform))]
|
#[cfg(not(target_family = "wasm"))]
|
||||||
{
|
{
|
||||||
let event_loop_proxy = event_loop.create_proxy();
|
let event_loop_proxy = event_loop.create_proxy();
|
||||||
let sender = sender.clone();
|
let sender = sender.clone();
|
||||||
@@ -143,34 +145,68 @@ impl Application {
|
|||||||
|
|
||||||
#[cfg(x11_platform)]
|
#[cfg(x11_platform)]
|
||||||
if event_loop.is_x11() {
|
if event_loop.is_x11() {
|
||||||
window_attributes = window_attributes
|
let mut attrs = WindowAttributesX11::default();
|
||||||
.with_platform_attributes(Box::new(window_attributes_x11(event_loop)?));
|
|
||||||
|
if let Some(token) = event_loop.read_token_from_env() {
|
||||||
|
startup_notify::reset_activation_token_env();
|
||||||
|
info!("Using token {:?} to activate a window", token);
|
||||||
|
attrs = attrs.with_activation_token(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
match std::env::var("X11_VISUAL_ID") {
|
||||||
|
Ok(visual_id_str) => {
|
||||||
|
info!("Using X11 visual id {visual_id_str}");
|
||||||
|
let visual_id = visual_id_str.parse().expect("invalid X11_VISUAL_ID");
|
||||||
|
attrs = attrs.with_x11_visual(visual_id);
|
||||||
|
},
|
||||||
|
Err(_) => {
|
||||||
|
info!("Set the X11_VISUAL_ID env var to request specific X11 visual")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
match std::env::var("X11_SCREEN_ID") {
|
||||||
|
Ok(screen_id_str) => {
|
||||||
|
info!("Placing the window on X11 screen {screen_id_str}");
|
||||||
|
let screen_id = screen_id_str.parse().expect("invalid X11_SCREEN_ID");
|
||||||
|
attrs = attrs.with_x11_screen(screen_id);
|
||||||
|
},
|
||||||
|
Err(_) => {
|
||||||
|
info!("Set the X11_SCREEN_ID env var to place the window on non-default screen")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
window_attributes = window_attributes.with_platform_attributes(Box::new(attrs));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(wayland_platform)]
|
#[cfg(wayland_platform)]
|
||||||
if event_loop.is_wayland() {
|
if event_loop.is_wayland() {
|
||||||
window_attributes = window_attributes
|
let mut attrs = WindowAttributesWayland::default();
|
||||||
.with_platform_attributes(Box::new(window_attributes_wayland(event_loop)));
|
|
||||||
|
if let Some(token) = event_loop.read_token_from_env() {
|
||||||
|
startup_notify::reset_activation_token_env();
|
||||||
|
info!("Using token {:?} to activate a window", token);
|
||||||
|
attrs = attrs.with_activation_token(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
window_attributes = window_attributes.with_platform_attributes(Box::new(attrs));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
if let Some(tab_id) = _tab_id {
|
if let Some(tab_id) = _tab_id {
|
||||||
let window_attributes_macos =
|
let attrs = WindowAttributesMacOS::default().with_tabbing_identifier(&tab_id);
|
||||||
Box::new(WindowAttributesMacOS::default().with_tabbing_identifier(&tab_id));
|
window_attributes = window_attributes.with_platform_attributes(Box::new(attrs));
|
||||||
window_attributes = window_attributes.with_platform_attributes(window_attributes_macos);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
{
|
{
|
||||||
window_attributes =
|
let attrs = WindowAttributesWeb::default().with_append(true);
|
||||||
window_attributes.with_platform_attributes(Box::new(window_attributes_web()));
|
window_attributes = window_attributes.with_platform_attributes(Box::new(attrs));
|
||||||
}
|
}
|
||||||
|
|
||||||
let window = event_loop.create_window(window_attributes)?;
|
let window = event_loop.create_window(window_attributes)?;
|
||||||
|
|
||||||
#[cfg(ios_platform)]
|
#[cfg(all(target_vendor = "apple", not(target_os = "macos")))]
|
||||||
{
|
{
|
||||||
use winit::platform::ios::WindowExtIOS;
|
|
||||||
window.recognize_doubletap_gesture(true);
|
window.recognize_doubletap_gesture(true);
|
||||||
window.recognize_pinch_gesture(true);
|
window.recognize_pinch_gesture(true);
|
||||||
window.recognize_rotation_gesture(true);
|
window.recognize_rotation_gesture(true);
|
||||||
@@ -186,7 +222,7 @@ impl Application {
|
|||||||
|
|
||||||
fn handle_action_from_proxy(&mut self, _event_loop: &dyn ActiveEventLoop, action: Action) {
|
fn handle_action_from_proxy(&mut self, _event_loop: &dyn ActiveEventLoop, action: Action) {
|
||||||
match action {
|
match action {
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
Action::DumpMonitors => self.dump_monitors(_event_loop),
|
Action::DumpMonitors => self.dump_monitors(_event_loop),
|
||||||
Action::Message => {
|
Action::Message => {
|
||||||
info!("User wake up");
|
info!("User wake up");
|
||||||
@@ -234,11 +270,11 @@ impl Application {
|
|||||||
Action::ToggleResizable => window.toggle_resizable(),
|
Action::ToggleResizable => window.toggle_resizable(),
|
||||||
Action::ToggleDecorations => window.toggle_decorations(),
|
Action::ToggleDecorations => window.toggle_decorations(),
|
||||||
Action::ToggleFullscreen => window.toggle_fullscreen(),
|
Action::ToggleFullscreen => window.toggle_fullscreen(),
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
Action::ToggleSimpleFullscreen => {
|
Action::ToggleSimpleFullscreen => {
|
||||||
window.window.set_simple_fullscreen(!window.window.simple_fullscreen());
|
window.window.set_simple_fullscreen(!window.window.simple_fullscreen());
|
||||||
},
|
},
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
Action::ToggleBorderlessGame => {
|
Action::ToggleBorderlessGame => {
|
||||||
let current = window.window.is_borderless_game();
|
let current = window.window.is_borderless_game();
|
||||||
window.window.set_borderless_game(!current);
|
window.window.set_borderless_game(!current);
|
||||||
@@ -253,13 +289,13 @@ impl Application {
|
|||||||
error!("Error creating custom cursor: {err}");
|
error!("Error creating custom cursor: {err}");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
Action::UrlCustomCursor => {
|
Action::UrlCustomCursor => {
|
||||||
if let Err(err) = window.url_custom_cursor(event_loop) {
|
if let Err(err) = window.url_custom_cursor(event_loop) {
|
||||||
error!("Error creating custom cursor from URL: {err}");
|
error!("Error creating custom cursor from URL: {err}");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
Action::AnimationCustomCursor => {
|
Action::AnimationCustomCursor => {
|
||||||
if let Err(err) = self
|
if let Err(err) = self
|
||||||
.custom_cursors
|
.custom_cursors
|
||||||
@@ -274,7 +310,7 @@ impl Application {
|
|||||||
Action::DragResizeWindow => window.drag_resize_window(),
|
Action::DragResizeWindow => window.drag_resize_window(),
|
||||||
Action::ShowWindowMenu => window.show_menu(),
|
Action::ShowWindowMenu => window.show_menu(),
|
||||||
Action::PrintHelp => self.print_help(),
|
Action::PrintHelp => self.print_help(),
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
Action::CycleOptionAsAlt => window.cycle_option_as_alt(),
|
Action::CycleOptionAsAlt => window.cycle_option_as_alt(),
|
||||||
Action::SetTheme(theme) => {
|
Action::SetTheme(theme) => {
|
||||||
window.window.set_theme(theme);
|
window.window.set_theme(theme);
|
||||||
@@ -282,7 +318,7 @@ impl Application {
|
|||||||
let actual_theme = theme.or_else(|| window.window.theme()).unwrap_or(Theme::Dark);
|
let actual_theme = theme.or_else(|| window.window.theme()).unwrap_or(Theme::Dark);
|
||||||
window.set_draw_theme(actual_theme);
|
window.set_draw_theme(actual_theme);
|
||||||
},
|
},
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
Action::CreateNewTab => {
|
Action::CreateNewTab => {
|
||||||
let tab_id = window.window.tabbing_identifier();
|
let tab_id = window.window.tabbing_identifier();
|
||||||
if let Err(err) = self.create_window(event_loop, Some(tab_id)) {
|
if let Err(err) = self.create_window(event_loop, Some(tab_id)) {
|
||||||
@@ -290,7 +326,7 @@ impl Application {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
Action::RequestResize => window.swap_dimensions(),
|
Action::RequestResize => window.swap_dimensions(),
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
Action::DumpMonitors => {
|
Action::DumpMonitors => {
|
||||||
let future = event_loop.request_detailed_monitor_permission();
|
let future = event_loop.request_detailed_monitor_permission();
|
||||||
let proxy = event_loop.create_proxy();
|
let proxy = event_loop.create_proxy();
|
||||||
@@ -304,7 +340,7 @@ impl Application {
|
|||||||
proxy.wake_up();
|
proxy.wake_up();
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
#[cfg(not(web_platform))]
|
#[cfg(not(target_family = "wasm"))]
|
||||||
Action::DumpMonitors => self.dump_monitors(event_loop),
|
Action::DumpMonitors => self.dump_monitors(event_loop),
|
||||||
Action::Message => {
|
Action::Message => {
|
||||||
self.sender.send(Action::Message).unwrap();
|
self.sender.send(Action::Message).unwrap();
|
||||||
@@ -635,7 +671,7 @@ struct WindowState {
|
|||||||
/// The amount of pan of the window.
|
/// The amount of pan of the window.
|
||||||
panned: PhysicalPosition<f32>,
|
panned: PhysicalPosition<f32>,
|
||||||
|
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
option_as_alt: OptionAsAlt,
|
option_as_alt: OptionAsAlt,
|
||||||
|
|
||||||
// Cursor states.
|
// Cursor states.
|
||||||
@@ -659,7 +695,7 @@ impl WindowState {
|
|||||||
|
|
||||||
let size = window.surface_size();
|
let size = window.surface_size();
|
||||||
let mut state = Self {
|
let mut state = Self {
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
option_as_alt: window.option_as_alt(),
|
option_as_alt: window.option_as_alt(),
|
||||||
custom_idx: app.custom_cursors.as_ref().map(Vec::len).unwrap_or(1) - 1,
|
custom_idx: app.custom_cursors.as_ref().map(Vec::len).unwrap_or(1) - 1,
|
||||||
cursor_grab: CursorGrabMode::None,
|
cursor_grab: CursorGrabMode::None,
|
||||||
@@ -758,7 +794,7 @@ impl WindowState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
fn cycle_option_as_alt(&mut self) {
|
fn cycle_option_as_alt(&mut self) {
|
||||||
self.option_as_alt = match self.option_as_alt {
|
self.option_as_alt = match self.option_as_alt {
|
||||||
OptionAsAlt::None => OptionAsAlt::OnlyLeft,
|
OptionAsAlt::None => OptionAsAlt::OnlyLeft,
|
||||||
@@ -804,7 +840,7 @@ impl WindowState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Custom cursor from an URL.
|
/// Custom cursor from an URL.
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
fn url_custom_cursor(
|
fn url_custom_cursor(
|
||||||
&mut self,
|
&mut self,
|
||||||
event_loop: &dyn ActiveEventLoop,
|
event_loop: &dyn ActiveEventLoop,
|
||||||
@@ -817,7 +853,7 @@ impl WindowState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Custom cursor from a URL.
|
/// Custom cursor from a URL.
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
fn animation_custom_cursor(
|
fn animation_custom_cursor(
|
||||||
&mut self,
|
&mut self,
|
||||||
event_loop: &dyn ActiveEventLoop,
|
event_loop: &dyn ActiveEventLoop,
|
||||||
@@ -1003,27 +1039,27 @@ enum Action {
|
|||||||
ToggleDecorations,
|
ToggleDecorations,
|
||||||
ToggleResizable,
|
ToggleResizable,
|
||||||
ToggleFullscreen,
|
ToggleFullscreen,
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
ToggleSimpleFullscreen,
|
ToggleSimpleFullscreen,
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
ToggleBorderlessGame,
|
ToggleBorderlessGame,
|
||||||
ToggleMaximize,
|
ToggleMaximize,
|
||||||
Minimize,
|
Minimize,
|
||||||
NextCursor,
|
NextCursor,
|
||||||
NextCustomCursor,
|
NextCustomCursor,
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
UrlCustomCursor,
|
UrlCustomCursor,
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
AnimationCustomCursor,
|
AnimationCustomCursor,
|
||||||
CycleCursorGrab,
|
CycleCursorGrab,
|
||||||
PrintHelp,
|
PrintHelp,
|
||||||
DragWindow,
|
DragWindow,
|
||||||
DragResizeWindow,
|
DragResizeWindow,
|
||||||
ShowWindowMenu,
|
ShowWindowMenu,
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
CycleOptionAsAlt,
|
CycleOptionAsAlt,
|
||||||
SetTheme(Option<Theme>),
|
SetTheme(Option<Theme>),
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
CreateNewTab,
|
CreateNewTab,
|
||||||
RequestResize,
|
RequestResize,
|
||||||
DumpMonitors,
|
DumpMonitors,
|
||||||
@@ -1042,35 +1078,35 @@ impl Action {
|
|||||||
Action::ToggleDecorations => "Toggle decorations",
|
Action::ToggleDecorations => "Toggle decorations",
|
||||||
Action::ToggleResizable => "Toggle window resizable state",
|
Action::ToggleResizable => "Toggle window resizable state",
|
||||||
Action::ToggleFullscreen => "Toggle fullscreen",
|
Action::ToggleFullscreen => "Toggle fullscreen",
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
Action::ToggleSimpleFullscreen => "Toggle simple fullscreen",
|
Action::ToggleSimpleFullscreen => "Toggle simple fullscreen",
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
Action::ToggleBorderlessGame => "Toggle borderless game mode",
|
Action::ToggleBorderlessGame => "Toggle borderless game mode",
|
||||||
Action::ToggleMaximize => "Maximize",
|
Action::ToggleMaximize => "Maximize",
|
||||||
Action::Minimize => "Minimize",
|
Action::Minimize => "Minimize",
|
||||||
Action::ToggleResizeIncrements => "Use resize increments when resizing window",
|
Action::ToggleResizeIncrements => "Use resize increments when resizing window",
|
||||||
Action::NextCursor => "Advance the cursor to the next value",
|
Action::NextCursor => "Advance the cursor to the next value",
|
||||||
Action::NextCustomCursor => "Advance custom cursor to the next value",
|
Action::NextCustomCursor => "Advance custom cursor to the next value",
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
Action::UrlCustomCursor => "Custom cursor from an URL",
|
Action::UrlCustomCursor => "Custom cursor from an URL",
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
Action::AnimationCustomCursor => "Custom cursor from an animation",
|
Action::AnimationCustomCursor => "Custom cursor from an animation",
|
||||||
Action::CycleCursorGrab => "Cycle through cursor grab mode",
|
Action::CycleCursorGrab => "Cycle through cursor grab mode",
|
||||||
Action::PrintHelp => "Print help",
|
Action::PrintHelp => "Print help",
|
||||||
Action::DragWindow => "Start window drag",
|
Action::DragWindow => "Start window drag",
|
||||||
Action::DragResizeWindow => "Start window drag-resize",
|
Action::DragResizeWindow => "Start window drag-resize",
|
||||||
Action::ShowWindowMenu => "Show window menu",
|
Action::ShowWindowMenu => "Show window menu",
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
Action::CycleOptionAsAlt => "Cycle option as alt mode",
|
Action::CycleOptionAsAlt => "Cycle option as alt mode",
|
||||||
Action::SetTheme(None) => "Change to the system theme",
|
Action::SetTheme(None) => "Change to the system theme",
|
||||||
Action::SetTheme(Some(Theme::Light)) => "Change to a light theme",
|
Action::SetTheme(Some(Theme::Light)) => "Change to a light theme",
|
||||||
Action::SetTheme(Some(Theme::Dark)) => "Change to a dark theme",
|
Action::SetTheme(Some(Theme::Dark)) => "Change to a dark theme",
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
Action::CreateNewTab => "Create new tab",
|
Action::CreateNewTab => "Create new tab",
|
||||||
Action::RequestResize => "Request a resize",
|
Action::RequestResize => "Request a resize",
|
||||||
#[cfg(not(web_platform))]
|
#[cfg(not(target_family = "wasm"))]
|
||||||
Action::DumpMonitors => "Dump monitor information",
|
Action::DumpMonitors => "Dump monitor information",
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
Action::DumpMonitors => {
|
Action::DumpMonitors => {
|
||||||
"Request permission to query detailed monitor information and dump monitor \
|
"Request permission to query detailed monitor information and dump monitor \
|
||||||
information"
|
information"
|
||||||
@@ -1097,7 +1133,7 @@ fn decode_cursor(bytes: &[u8]) -> CustomCursorSource {
|
|||||||
CustomCursorSource::from_rgba(samples.samples, w, h, w / 2, h / 2).unwrap()
|
CustomCursorSource::from_rgba(samples.samples, w, h, w / 2, h / 2).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
fn url_custom_cursor() -> CustomCursorSource {
|
fn url_custom_cursor() -> CustomCursorSource {
|
||||||
use std::sync::atomic::{AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicU64, Ordering};
|
||||||
|
|
||||||
@@ -1162,60 +1198,6 @@ fn mouse_button_to_string(button: MouseButton) -> Cow<'static, str> {
|
|||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(web_platform)]
|
|
||||||
fn window_attributes_web() -> WindowAttributesWeb {
|
|
||||||
WindowAttributesWeb::default().with_append(true)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(wayland_platform)]
|
|
||||||
fn window_attributes_wayland(event_loop: &dyn ActiveEventLoop) -> WindowAttributesWayland {
|
|
||||||
let mut window_attributes = WindowAttributesWayland::default();
|
|
||||||
|
|
||||||
if let Some(token) = event_loop.read_token_from_env() {
|
|
||||||
startup_notify::reset_activation_token_env();
|
|
||||||
info!("Using token {:?} to activate a window", token);
|
|
||||||
window_attributes = window_attributes.with_activation_token(token);
|
|
||||||
}
|
|
||||||
|
|
||||||
window_attributes
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(x11_platform)]
|
|
||||||
fn window_attributes_x11(
|
|
||||||
event_loop: &dyn ActiveEventLoop,
|
|
||||||
) -> Result<WindowAttributesX11, Box<dyn Error>> {
|
|
||||||
let mut window_attributes = WindowAttributesX11::default();
|
|
||||||
|
|
||||||
#[cfg(any(x11_platform, wayland_platform))]
|
|
||||||
if let Some(token) = event_loop.read_token_from_env() {
|
|
||||||
startup_notify::reset_activation_token_env();
|
|
||||||
info!("Using token {:?} to activate a window", token);
|
|
||||||
window_attributes = window_attributes.with_activation_token(token);
|
|
||||||
}
|
|
||||||
|
|
||||||
match std::env::var("X11_VISUAL_ID") {
|
|
||||||
Ok(visual_id_str) => {
|
|
||||||
info!("Using X11 visual id {visual_id_str}");
|
|
||||||
let visual_id = visual_id_str.parse()?;
|
|
||||||
window_attributes = window_attributes.with_x11_visual(visual_id);
|
|
||||||
},
|
|
||||||
Err(_) => info!("Set the X11_VISUAL_ID env variable to request specific X11 visual"),
|
|
||||||
}
|
|
||||||
|
|
||||||
match std::env::var("X11_SCREEN_ID") {
|
|
||||||
Ok(screen_id_str) => {
|
|
||||||
info!("Placing the window on X11 screen {screen_id_str}");
|
|
||||||
let screen_id = screen_id_str.parse()?;
|
|
||||||
window_attributes = window_attributes.with_x11_screen(screen_id);
|
|
||||||
},
|
|
||||||
Err(_) => {
|
|
||||||
info!("Set the X11_SCREEN_ID env variable to place the window on non-default screen")
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(window_attributes)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Cursor list to cycle through.
|
/// Cursor list to cycle through.
|
||||||
const CURSORS: &[CursorIcon] = &[
|
const CURSORS: &[CursorIcon] = &[
|
||||||
CursorIcon::Default,
|
CursorIcon::Default,
|
||||||
@@ -1259,7 +1241,7 @@ const KEY_BINDINGS: &[Binding<&'static str>] = &[
|
|||||||
Binding::new("H", ModifiersState::CONTROL, Action::PrintHelp),
|
Binding::new("H", ModifiersState::CONTROL, Action::PrintHelp),
|
||||||
Binding::new("F", ModifiersState::SHIFT, Action::ToggleAnimatedFillColor),
|
Binding::new("F", ModifiersState::SHIFT, Action::ToggleAnimatedFillColor),
|
||||||
Binding::new("F", ModifiersState::CONTROL, Action::ToggleFullscreen),
|
Binding::new("F", ModifiersState::CONTROL, Action::ToggleFullscreen),
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
Binding::new("F", ModifiersState::ALT, Action::ToggleSimpleFullscreen),
|
Binding::new("F", ModifiersState::ALT, Action::ToggleSimpleFullscreen),
|
||||||
Binding::new("D", ModifiersState::CONTROL, Action::ToggleDecorations),
|
Binding::new("D", ModifiersState::CONTROL, Action::ToggleDecorations),
|
||||||
Binding::new("L", ModifiersState::CONTROL, Action::CycleCursorGrab),
|
Binding::new("L", ModifiersState::CONTROL, Action::CycleCursorGrab),
|
||||||
@@ -1276,13 +1258,13 @@ const KEY_BINDINGS: &[Binding<&'static str>] = &[
|
|||||||
// C.
|
// C.
|
||||||
Binding::new("C", ModifiersState::CONTROL, Action::NextCursor),
|
Binding::new("C", ModifiersState::CONTROL, Action::NextCursor),
|
||||||
Binding::new("C", ModifiersState::ALT, Action::NextCustomCursor),
|
Binding::new("C", ModifiersState::ALT, Action::NextCustomCursor),
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
Binding::new(
|
Binding::new(
|
||||||
"C",
|
"C",
|
||||||
ModifiersState::CONTROL.union(ModifiersState::SHIFT),
|
ModifiersState::CONTROL.union(ModifiersState::SHIFT),
|
||||||
Action::UrlCustomCursor,
|
Action::UrlCustomCursor,
|
||||||
),
|
),
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
Binding::new(
|
Binding::new(
|
||||||
"C",
|
"C",
|
||||||
ModifiersState::ALT.union(ModifiersState::SHIFT),
|
ModifiersState::ALT.union(ModifiersState::SHIFT),
|
||||||
@@ -1293,11 +1275,11 @@ const KEY_BINDINGS: &[Binding<&'static str>] = &[
|
|||||||
Binding::new("K", ModifiersState::empty(), Action::SetTheme(None)),
|
Binding::new("K", ModifiersState::empty(), Action::SetTheme(None)),
|
||||||
Binding::new("K", ModifiersState::META, Action::SetTheme(Some(Theme::Light))),
|
Binding::new("K", ModifiersState::META, Action::SetTheme(Some(Theme::Light))),
|
||||||
Binding::new("K", ModifiersState::CONTROL, Action::SetTheme(Some(Theme::Dark))),
|
Binding::new("K", ModifiersState::CONTROL, Action::SetTheme(Some(Theme::Dark))),
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
Binding::new("T", ModifiersState::META, Action::CreateNewTab),
|
Binding::new("T", ModifiersState::META, Action::CreateNewTab),
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
Binding::new("O", ModifiersState::CONTROL, Action::CycleOptionAsAlt),
|
Binding::new("O", ModifiersState::CONTROL, Action::CycleOptionAsAlt),
|
||||||
#[cfg(macos_platform)]
|
#[cfg(target_os = "macos")]
|
||||||
Binding::new("B", ModifiersState::CONTROL, Action::ToggleBorderlessGame),
|
Binding::new("B", ModifiersState::CONTROL, Action::ToggleBorderlessGame),
|
||||||
Binding::new("S", ModifiersState::ALT, Action::EmitSurfaceSize),
|
Binding::new("S", ModifiersState::ALT, Action::EmitSurfaceSize),
|
||||||
Binding::new("S", ModifiersState::CONTROL, Action::Message),
|
Binding::new("S", ModifiersState::CONTROL, Action::Message),
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#[cfg(any(x11_platform, macos_platform, windows_platform))]
|
#[cfg(any(x11_platform, target_os = "macos", target_os = "windows"))]
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
fn main() -> Result<(), impl std::error::Error> {
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@@ -121,7 +121,7 @@ fn main() -> Result<(), impl std::error::Error> {
|
|||||||
event_loop.run_app(Application::default())
|
event_loop.run_app(Application::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(any(x11_platform, macos_platform, windows_platform)))]
|
#[cfg(not(any(x11_platform, target_os = "macos", target_os = "windows")))]
|
||||||
fn main() {
|
fn main() {
|
||||||
panic!(
|
panic!(
|
||||||
"This example is supported only on x11, macOS, and Windows, with the `rwh_06` feature \
|
"This example is supported only on x11, macOS, and Windows, with the `rwh_06` feature \
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
#![allow(clippy::single_match)]
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
#[cfg(not(web_platform))]
|
use std::time::Duration;
|
||||||
use std::time;
|
#[cfg(not(target_family = "wasm"))]
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
use web_time as time;
|
use web_time::Instant;
|
||||||
use winit::application::ApplicationHandler;
|
use winit::application::ApplicationHandler;
|
||||||
use winit::event::{ElementState, KeyEvent, StartCause, WindowEvent};
|
use winit::event::{ElementState, KeyEvent, StartCause, WindowEvent};
|
||||||
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
|
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
|
||||||
@@ -18,8 +19,8 @@ mod fill;
|
|||||||
#[path = "util/tracing.rs"]
|
#[path = "util/tracing.rs"]
|
||||||
mod tracing;
|
mod tracing;
|
||||||
|
|
||||||
const WAIT_TIME: time::Duration = time::Duration::from_millis(100);
|
const WAIT_TIME: Duration = Duration::from_millis(100);
|
||||||
const POLL_SLEEP_TIME: time::Duration = time::Duration::from_millis(100);
|
const POLL_SLEEP_TIME: Duration = Duration::from_millis(100);
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
enum Mode {
|
enum Mode {
|
||||||
@@ -30,7 +31,7 @@ enum Mode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), impl std::error::Error> {
|
fn main() -> Result<(), impl std::error::Error> {
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
console_error_panic_hook::set_once();
|
console_error_panic_hook::set_once();
|
||||||
|
|
||||||
tracing::init();
|
tracing::init();
|
||||||
@@ -129,8 +130,7 @@ impl ApplicationHandler for ControlFlowDemo {
|
|||||||
Mode::Wait => event_loop.set_control_flow(ControlFlow::Wait),
|
Mode::Wait => event_loop.set_control_flow(ControlFlow::Wait),
|
||||||
Mode::WaitUntil => {
|
Mode::WaitUntil => {
|
||||||
if !self.wait_cancelled {
|
if !self.wait_cancelled {
|
||||||
event_loop
|
event_loop.set_control_flow(ControlFlow::WaitUntil(Instant::now() + WAIT_TIME));
|
||||||
.set_control_flow(ControlFlow::WaitUntil(time::Instant::now() + WAIT_TIME));
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Mode::Poll => {
|
Mode::Poll => {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ use winit::application::ApplicationHandler;
|
|||||||
use winit::event::{Ime, WindowEvent};
|
use winit::event::{Ime, WindowEvent};
|
||||||
use winit::event_loop::{ActiveEventLoop, EventLoop};
|
use winit::event_loop::{ActiveEventLoop, EventLoop};
|
||||||
use winit::keyboard::{Key, ModifiersState, NamedKey};
|
use winit::keyboard::{Key, ModifiersState, NamedKey};
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
use winit::platform::web::WindowAttributesWeb;
|
use winit::platform::web::WindowAttributesWeb;
|
||||||
use winit::window::{
|
use winit::window::{
|
||||||
ImeCapabilities, ImeEnableRequest, ImeHint, ImePurpose, ImeRequest, ImeRequestData,
|
ImeCapabilities, ImeEnableRequest, ImeHint, ImePurpose, ImeRequest, ImeRequestData,
|
||||||
@@ -68,9 +68,9 @@ impl TextInputState {
|
|||||||
|
|
||||||
impl ApplicationHandler for App {
|
impl ApplicationHandler for App {
|
||||||
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
|
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
|
||||||
#[cfg(not(web_platform))]
|
#[cfg(not(target_family = "wasm"))]
|
||||||
let window_attributes = WindowAttributes::default();
|
let window_attributes = WindowAttributes::default();
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
let window_attributes = WindowAttributes::default()
|
let window_attributes = WindowAttributes::default()
|
||||||
.with_platform_attributes(Box::new(WindowAttributesWeb::default().with_append(true)));
|
.with_platform_attributes(Box::new(WindowAttributesWeb::default().with_append(true)));
|
||||||
self.window = match event_loop.create_window(window_attributes) {
|
self.window = match event_loop.create_window(window_attributes) {
|
||||||
@@ -339,7 +339,7 @@ fn preedit_with_cursor_checked(text: &str, cursor: usize, anchor: usize) -> Resu
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
console_error_panic_hook::set_once();
|
console_error_panic_hook::set_once();
|
||||||
|
|
||||||
tracing_init::init();
|
tracing_init::init();
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
#![allow(clippy::single_match)]
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
// Limit this example to only compatible platforms.
|
// Limit this example to only compatible platforms.
|
||||||
#[cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform, android_platform,))]
|
#[cfg(any(
|
||||||
|
target_os = "windows",
|
||||||
|
target_os = "macos",
|
||||||
|
x11_platform,
|
||||||
|
wayland_platform,
|
||||||
|
target_os = "android",
|
||||||
|
))]
|
||||||
fn main() -> std::process::ExitCode {
|
fn main() -> std::process::ExitCode {
|
||||||
use std::process::ExitCode;
|
use std::process::ExitCode;
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
@@ -77,7 +83,13 @@ fn main() -> std::process::ExitCode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(ios_platform, web_platform, orbital_platform))]
|
#[cfg(not(any(
|
||||||
|
target_os = "windows",
|
||||||
|
target_os = "macos",
|
||||||
|
x11_platform,
|
||||||
|
wayland_platform,
|
||||||
|
target_os = "android",
|
||||||
|
)))]
|
||||||
fn main() {
|
fn main() {
|
||||||
panic!("This platform doesn't support pump_events.")
|
panic!("This platform doesn't support pump_events.")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
#![allow(clippy::single_match)]
|
#![allow(clippy::single_match)]
|
||||||
|
|
||||||
// Limit this example to only compatible platforms.
|
// Limit this example to only compatible platforms.
|
||||||
#[cfg(any(windows_platform, macos_platform, x11_platform, wayland_platform, orbital_platform))]
|
#[cfg(any(
|
||||||
|
target_os = "windows",
|
||||||
|
target_os = "macos",
|
||||||
|
x11_platform,
|
||||||
|
wayland_platform,
|
||||||
|
target_os = "redox"
|
||||||
|
))]
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
@@ -90,11 +96,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(any(
|
#[cfg(not(any(
|
||||||
windows_platform,
|
target_os = "windows",
|
||||||
macos_platform,
|
target_os = "macos",
|
||||||
x11_platform,
|
x11_platform,
|
||||||
wayland_platform,
|
wayland_platform,
|
||||||
orbital_platform
|
target_os = "redox"
|
||||||
)))]
|
)))]
|
||||||
fn main() {
|
fn main() {
|
||||||
panic!("This example is not supported on this platform")
|
panic!("This example is not supported on this platform")
|
||||||
|
|||||||
@@ -12,11 +12,11 @@ use std::collections::HashMap;
|
|||||||
use std::mem;
|
use std::mem;
|
||||||
use std::mem::ManuallyDrop;
|
use std::mem::ManuallyDrop;
|
||||||
use std::num::NonZeroU32;
|
use std::num::NonZeroU32;
|
||||||
#[cfg(not(web_platform))]
|
#[cfg(not(target_family = "wasm"))]
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use softbuffer::{Context, Surface};
|
use softbuffer::{Context, Surface};
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
use web_time::Instant;
|
use web_time::Instant;
|
||||||
use winit::window::{Window, WindowId};
|
use winit::window::{Window, WindowId};
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#[cfg(not(web_platform))]
|
#[cfg(not(target_family = "wasm"))]
|
||||||
pub fn init() {
|
pub fn init() {
|
||||||
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
|
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
|
||||||
|
|
||||||
@@ -9,7 +9,7 @@ pub fn init() {
|
|||||||
.init();
|
.init();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
pub fn init() {
|
pub fn init() {
|
||||||
use tracing_subscriber::layer::SubscriberExt;
|
use tracing_subscriber::layer::SubscriberExt;
|
||||||
use tracing_subscriber::util::SubscriberInitExt;
|
use tracing_subscriber::util::SubscriberInitExt;
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ use tracing::{error, info};
|
|||||||
use winit::application::ApplicationHandler;
|
use winit::application::ApplicationHandler;
|
||||||
use winit::event::WindowEvent;
|
use winit::event::WindowEvent;
|
||||||
use winit::event_loop::{ActiveEventLoop, EventLoop};
|
use winit::event_loop::{ActiveEventLoop, EventLoop};
|
||||||
#[cfg(web_platform)]
|
|
||||||
use winit::platform::web::WindowAttributesWeb;
|
|
||||||
use winit::window::{Window, WindowAttributes, WindowId};
|
use winit::window::{Window, WindowAttributes, WindowId};
|
||||||
|
|
||||||
#[path = "util/fill.rs"]
|
#[path = "util/fill.rs"]
|
||||||
@@ -22,11 +20,11 @@ struct App {
|
|||||||
|
|
||||||
impl ApplicationHandler for App {
|
impl ApplicationHandler for App {
|
||||||
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
|
fn can_create_surfaces(&mut self, event_loop: &dyn ActiveEventLoop) {
|
||||||
#[cfg(not(web_platform))]
|
|
||||||
let window_attributes = WindowAttributes::default();
|
let window_attributes = WindowAttributes::default();
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
let window_attributes = WindowAttributes::default()
|
let window_attributes = window_attributes.with_platform_attributes(Box::new(
|
||||||
.with_platform_attributes(Box::new(WindowAttributesWeb::default().with_append(true)));
|
winit::platform::web::WindowAttributesWeb::default().with_append(true),
|
||||||
|
));
|
||||||
self.window = match event_loop.create_window(window_attributes) {
|
self.window = match event_loop.create_window(window_attributes) {
|
||||||
Ok(window) => Some(window),
|
Ok(window) => Some(window),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -71,7 +69,7 @@ impl ApplicationHandler for App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn Error>> {
|
fn main() -> Result<(), Box<dyn Error>> {
|
||||||
#[cfg(web_platform)]
|
#[cfg(target_family = "wasm")]
|
||||||
console_error_panic_hook::set_once();
|
console_error_panic_hook::set_once();
|
||||||
|
|
||||||
tracing::init();
|
tracing::init();
|
||||||
|
|||||||
Reference in New Issue
Block a user