Skip to content

Commit

Permalink
Platform is called Web not Wasm (#3393)
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda authored Jan 14, 2024
1 parent 2ee4424 commit 16d8607
Show file tree
Hide file tree
Showing 19 changed files with 57 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ targets = [
"x86_64-apple-ios",
# Android
"aarch64-linux-android",
# WebAssembly
# Web
"wasm32-unknown-unknown",
]
rustdoc-args = ["--cfg", "docsrs"]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ same MSRV policy.

Note that windows don't appear on Wayland until you draw/present to them.

#### WebAssembly
#### Web

To run the web example: `cargo run-wasm --example web`

Expand All @@ -87,7 +87,7 @@ either [provide Winit with a `<canvas>` element][web with_canvas], or [let Winit
create a `<canvas>` element which you can then retrieve][web canvas getter] and
insert it into the DOM yourself.

For the example code using Winit with WebAssembly, check out the [web example]. For
For the example code using Winit on Web, check out the [web example]. For
information on using Rust on WebAssembly, check out the [Rust and WebAssembly
book].

Expand Down
6 changes: 3 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
cfg_aliases! {
// Systems.
android_platform: { target_os = "android" },
wasm_platform: { all(target_family = "wasm", not(target_os = "emscripten")) },
web_platform: { all(target_family = "wasm", target_os = "unknown") },
macos_platform: { target_os = "macos" },
ios_platform: { target_os = "ios" },
windows_platform: { target_os = "windows" },
Expand All @@ -17,8 +17,8 @@ fn main() {
redox: { target_os = "redox" },

// Native displays.
x11_platform: { all(feature = "x11", free_unix, not(wasm), not(redox)) },
wayland_platform: { all(feature = "wayland", free_unix, not(wasm), not(redox)) },
x11_platform: { all(feature = "x11", free_unix, not(redox)) },
wayland_platform: { all(feature = "wayland", free_unix, not(redox)) },
orbital_platform: { redox },
}
}
4 changes: 2 additions & 2 deletions examples/control_flow.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![allow(clippy::single_match)]

use std::thread;
#[cfg(not(wasm_platform))]
#[cfg(not(web_platform))]
use std::time;
#[cfg(wasm_platform)]
#[cfg(web_platform)]
use web_time as time;

use simple_logger::SimpleLogger;
Expand Down
22 changes: 11 additions & 11 deletions examples/custom_cursors.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#![allow(clippy::single_match, clippy::disallowed_methods)]

#[cfg(not(wasm_platform))]
#[cfg(not(web_platform))]
use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{EventLoop, EventLoopWindowTarget},
keyboard::Key,
window::{CursorIcon, CustomCursor, WindowBuilder},
};
#[cfg(wasm_platform)]
#[cfg(web_platform)]
use {
std::sync::atomic::{AtomicU64, Ordering},
std::time::Duration,
winit::platform::web::CustomCursorExtWebSys,
};

#[cfg(wasm_platform)]
#[cfg(web_platform)]
static COUNTER: AtomicU64 = AtomicU64::new(0);

fn decode_cursor(bytes: &[u8], window_target: &EventLoopWindowTarget) -> CustomCursor {
Expand All @@ -28,22 +28,22 @@ fn decode_cursor(bytes: &[u8], window_target: &EventLoopWindowTarget) -> CustomC
builder.build(window_target)
}

#[cfg(not(wasm_platform))]
#[cfg(not(web_platform))]
#[path = "util/fill.rs"]
mod fill;

fn main() -> Result<(), impl std::error::Error> {
#[cfg(not(wasm_platform))]
#[cfg(not(web_platform))]
SimpleLogger::new()
.with_level(log::LevelFilter::Info)
.init()
.unwrap();
#[cfg(wasm_platform)]
#[cfg(web_platform)]
console_log::init_with_level(log::Level::Debug).unwrap();

let event_loop = EventLoop::new().unwrap();
let builder = WindowBuilder::new().with_title("A fantastic window!");
#[cfg(wasm_platform)]
#[cfg(web_platform)]
let builder = {
use winit::platform::web::WindowBuilderExtWebSys;
builder.with_append(true)
Expand Down Expand Up @@ -83,7 +83,7 @@ fn main() -> Result<(), impl std::error::Error> {
log::debug!("Setting cursor visibility to {:?}", cursor_visible);
window.set_cursor_visible(cursor_visible);
}
#[cfg(wasm_platform)]
#[cfg(web_platform)]
Key::Character("4") => {
log::debug!("Setting cursor to a random image from an URL");
window.set_cursor(
Expand All @@ -98,7 +98,7 @@ fn main() -> Result<(), impl std::error::Error> {
.build(_elwt),
);
}
#[cfg(wasm_platform)]
#[cfg(web_platform)]
Key::Character("5") => {
log::debug!("Setting cursor to an animation");
window.set_cursor(
Expand All @@ -125,11 +125,11 @@ fn main() -> Result<(), impl std::error::Error> {
_ => {}
},
WindowEvent::RedrawRequested => {
#[cfg(not(wasm_platform))]
#[cfg(not(web_platform))]
fill::fill_window(&window);
}
WindowEvent::CloseRequested => {
#[cfg(not(wasm_platform))]
#[cfg(not(web_platform))]
_elwt.exit();
}
_ => (),
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::single_match)]

#[cfg(not(wasm_platform))]
#[cfg(not(web_platform))]
fn main() -> Result<(), impl std::error::Error> {
use simple_logger::SimpleLogger;
use winit::{
Expand Down Expand Up @@ -56,7 +56,7 @@ fn main() -> Result<(), impl std::error::Error> {
})
}

#[cfg(wasm_platform)]
#[cfg(web_platform)]
fn main() {
panic!("This example is not supported on web.");
}
4 changes: 2 additions & 2 deletions examples/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
//! Example for focusing a window.

use simple_logger::SimpleLogger;
#[cfg(not(wasm_platform))]
#[cfg(not(web_platform))]
use std::time;
#[cfg(wasm_platform)]
#[cfg(web_platform)]
use web_time as time;
use winit::{
event::{Event, StartCause, WindowEvent},
Expand Down
6 changes: 3 additions & 3 deletions examples/multithreaded.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::single_match)]

#[cfg(not(wasm_platform))]
#[cfg(not(web_platform))]
fn main() -> Result<(), impl std::error::Error> {
use std::{collections::HashMap, sync::mpsc, thread, time::Duration};

Expand Down Expand Up @@ -205,7 +205,7 @@ fn main() -> Result<(), impl std::error::Error> {
})
}

#[cfg(wasm_platform)]
#[cfg(web_platform)]
fn main() {
panic!("Example not supported on Wasm");
panic!("Example not supported on Web");
}
4 changes: 2 additions & 2 deletions examples/request_redraw_threaded.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::single_match)]

#[cfg(not(wasm_platform))]
#[cfg(not(web_platform))]
fn main() -> Result<(), impl std::error::Error> {
use std::{sync::Arc, thread, time};

Expand Down Expand Up @@ -53,7 +53,7 @@ fn main() -> Result<(), impl std::error::Error> {
})
}

#[cfg(wasm_platform)]
#[cfg(web_platform)]
fn main() {
unimplemented!() // `Window` can't be sent between threads
}
4 changes: 2 additions & 2 deletions examples/timer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![allow(clippy::single_match)]

use std::time::Duration;
#[cfg(not(wasm_platform))]
#[cfg(not(web_platform))]
use std::time::Instant;
#[cfg(wasm_platform)]
#[cfg(web_platform)]
use web_time::Instant;

use simple_logger::SimpleLogger;
Expand Down
8 changes: 4 additions & 4 deletions examples/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ pub fn main() -> Result<(), impl std::error::Error> {
let event_loop = EventLoop::new().unwrap();

let builder = WindowBuilder::new().with_title("A fantastic window!");
#[cfg(wasm_platform)]
#[cfg(web_platform)]
let builder = {
use winit::platform::web::WindowBuilderExtWebSys;
builder.with_append(true)
};
let window = builder.build(&event_loop).unwrap();

#[cfg(wasm_platform)]
#[cfg(web_platform)]
let log_list = wasm::insert_canvas_and_create_log_list(&window);

event_loop.run(move |event, elwt| {
#[cfg(wasm_platform)]
#[cfg(web_platform)]
wasm::log_event(&log_list, &event);

match event {
Expand Down Expand Up @@ -57,7 +57,7 @@ pub fn main() -> Result<(), impl std::error::Error> {
})
}

#[cfg(wasm_platform)]
#[cfg(web_platform)]
mod wasm {
use std::num::NonZeroU32;

Expand Down
2 changes: 1 addition & 1 deletion examples/web_aspect_ratio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn main() {
println!("This example must be run with cargo run-wasm --example web_aspect_ratio")
}

#[cfg(wasm_platform)]
#[cfg(web_platform)]
mod wasm {
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
Expand Down
2 changes: 1 addition & 1 deletion examples/window_pump_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn main() -> std::process::ExitCode {
}
}

#[cfg(any(ios_platform, wasm_platform, orbital_platform))]
#[cfg(any(ios_platform, web_platform, orbital_platform))]
fn main() {
println!("This platform doesn't support pump_events.");
}
4 changes: 2 additions & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@
//! [`ControlFlow::WaitUntil`]: crate::event_loop::ControlFlow::WaitUntil
use std::path::PathBuf;
use std::sync::{Mutex, Weak};
#[cfg(not(wasm_platform))]
#[cfg(not(web_platform))]
use std::time::Instant;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use smol_str::SmolStr;
#[cfg(wasm_platform)]
#[cfg(web_platform)]
use web_time::Instant;

use crate::error::ExternalError;
Expand Down
14 changes: 7 additions & 7 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::{error, fmt};

#[cfg(not(wasm_platform))]
#[cfg(not(web_platform))]
use std::time::{Duration, Instant};
#[cfg(wasm_platform)]
#[cfg(web_platform)]
use web_time::{Duration, Instant};

use crate::error::EventLoopError;
Expand Down Expand Up @@ -130,7 +130,7 @@ impl<T> EventLoopBuilder<T> {
})
}

#[cfg(wasm_platform)]
#[cfg(web_platform)]
pub(crate) fn allow_event_loop_recreation() {
EVENT_LOOP_CREATED.store(false, Ordering::Relaxed);
}
Expand Down Expand Up @@ -223,10 +223,10 @@ impl<T> EventLoop<T> {
///
/// Web applications are recommended to use
#[cfg_attr(
wasm_platform,
web_platform,
doc = "[`EventLoopExtWebSys::spawn()`][crate::platform::web::EventLoopExtWebSys::spawn()]"
)]
#[cfg_attr(not(wasm_platform), doc = "`EventLoopExtWebSys::spawn()`")]
#[cfg_attr(not(web_platform), doc = "`EventLoopExtWebSys::spawn()`")]
/// [^1] instead of [`run()`] to avoid the need
/// for the Javascript exception trick, and to make it clearer that the event loop runs
/// asynchronously (via the browser's own, internal, event loop) and doesn't block the
Expand All @@ -236,9 +236,9 @@ impl<T> EventLoop<T> {
///
/// [`set_control_flow()`]: EventLoopWindowTarget::set_control_flow()
/// [`run()`]: Self::run()
/// [^1]: `EventLoopExtWebSys::spawn()` is only available on WASM.
/// [^1]: `EventLoopExtWebSys::spawn()` is only available on Web.
#[inline]
#[cfg(not(all(wasm_platform, target_feature = "exception-handling")))]
#[cfg(not(all(web_platform, target_feature = "exception-handling")))]
pub fn run<F>(self, event_handler: F) -> Result<(), EventLoopError>
where
F: FnMut(Event<T>, &EventLoopWindowTarget),
Expand Down
2 changes: 1 addition & 1 deletion src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub mod orbital;
pub mod startup_notify;
#[cfg(any(wayland_platform, docsrs))]
pub mod wayland;
#[cfg(any(wasm_platform, docsrs))]
#[cfg(any(web_platform, docsrs))]
pub mod web;
#[cfg(any(windows_platform, docsrs))]
pub mod windows;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/run_on_demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub trait EventLoopExtRunOnDemand {
/// - **iOS:** It's not possible to stop and start an `NSApplication` repeatedly on iOS.
///
#[cfg_attr(
not(wasm_platform),
not(web_platform),
doc = "[^1]: `spawn()` is only available on `wasm` platforms."
)]
///
Expand Down
Loading

0 comments on commit 16d8607

Please sign in to comment.