Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation comments to bevy_winit #8115

Merged
merged 8 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions crates/bevy_winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use crate::web_resize::{CanvasParentResizeEventChannel, CanvasParentResizePlugin
#[cfg(target_os = "android")]
pub static ANDROID_APP: once_cell::sync::OnceCell<AndroidApp> = once_cell::sync::OnceCell::new();

/// A [`Plugin`] that utilizes [`winit`] for window creation and event loop management.
#[derive(Default)]
pub struct WinitPlugin;

Expand Down Expand Up @@ -270,6 +271,7 @@ impl Default for WinitPersistentState {
}
}

/// The default [`App::runner`] for the [`WinitPlugin`] plugin.
pub fn winit_runner(mut app: App) {
// We remove this so that we have ownership over it.
let mut event_loop = app
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_winit/src/winit_config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy_ecs::system::Resource;
use bevy_utils::Duration;

/// A resource for configuring usage of the `rust_winit` library.
/// A resource for configuring usage of the [`winit`] library.
#[derive(Debug, Resource)]
pub struct WinitSettings {
/// Configures `winit` to return control to the caller after exiting the
Expand Down
14 changes: 14 additions & 0 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![warn(missing_docs)]
use std::sync::atomic::Ordering;

use accesskit_winit::Adapter;
Expand All @@ -20,18 +21,24 @@ use crate::{
converters::convert_window_level,
};

/// A reource which maps window entities to [`winit`] library windows.
beeryt marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Debug, Default)]
pub struct WinitWindows {
/// Stores [`winit`] windows by window identifier.
pub windows: HashMap<winit::window::WindowId, winit::window::Window>,
/// Maps entities to `winit` window identifiers.
pub entity_to_winit: HashMap<Entity, winit::window::WindowId>,
/// Maps `winit` window identifiers to entities.
pub winit_to_entity: HashMap<winit::window::WindowId, Entity>,

// Some winit functions, such as `set_window_icon` can only be used from the main thread. If
// they are used in another thread, the app will hang. This marker ensures `WinitWindows` is
// only ever accessed with bevy's non-send functions and in NonSend systems.
_not_send_sync: core::marker::PhantomData<*const ()>,
}

impl WinitWindows {
/// Creates a `winit` window and associates it with our entity.
pub fn create_window(
&mut self,
event_loop: &winit::event_loop::EventLoopWindowTarget<()>,
Expand Down Expand Up @@ -227,6 +234,9 @@ impl WinitWindows {
}
}

/// Gets the "best" video mode which fits the given dimensions.
///
/// The heuristic for "best" prioritizes width, height, and refresh rate in that order.
pub fn get_fitting_videomode(
monitor: &winit::monitor::MonitorHandle,
width: u32,
Expand Down Expand Up @@ -259,6 +269,9 @@ pub fn get_fitting_videomode(
modes.first().unwrap().clone()
}

/// Gets the "best" videomode from a monitor.
///
/// The heuristic for "best" prioritizes width, height, and refresh rate in that order.
pub fn get_best_videomode(monitor: &winit::monitor::MonitorHandle) -> winit::monitor::VideoMode {
let mut modes = monitor.video_modes().collect::<Vec<_>>();
modes.sort_by(|a, b| {
Expand Down Expand Up @@ -300,6 +313,7 @@ pub(crate) fn attempt_grab(winit_window: &winit::window::Window, grab_mode: Curs
}
}

/// Compute the physical window position for a given [`WindowPosition`].
// Ideally we could generify this across window backends, but we only really have winit atm
// so whatever.
pub fn winit_window_position(
Expand Down