Add name to bevy:🪟:Window (#7650)

# Objective
- Fixes  #4188, make users could set application ID for bevy apps.

## Solution

- Add `name` field to `bevy:🪟:Window`. Specifying this field adds
different properties to the window: application ID on `Wayland`,
`WM_CLASS` on `X11`, or window class name on Windows. It has no effect
on other platforms.
---

## Changelog

### Added
- Add `name` to `bevy:🪟:Window`.

## Migration Guide

- Set the `bevy_window::Window`'s `name` field when needed:
```rust
App::new()
        .add_plugins(DefaultPlugins.set(WindowPlugin {
            primary_window: Some(Window {
                title: "I am a window!".into(),
                name: Some("SpaceGameCompany.SpaceShooter".into()),
                ..default()
            }),
            ..default()
        }))
        .run();
```

---------

Co-authored-by: François <mockersf@gmail.com>
This commit is contained in:
VitalyR 2024-02-05 21:35:35 +08:00 committed by GitHub
parent dd15890c6a
commit 7705c1dd6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 71 additions and 0 deletions

View file

@ -138,6 +138,21 @@ pub struct Window {
pub resolution: WindowResolution, pub resolution: WindowResolution,
/// Stores the title of the window. /// Stores the title of the window.
pub title: String, pub title: String,
/// Stores the application ID (on **`Wayland`**), `WM_CLASS` (on **`X11`**) or window class name (on **`Windows`**) of the window.
///
/// For details about application ID conventions, see the [Desktop Entry Spec](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#desktop-file-id).
/// For details about `WM_CLASS`, see the [X11 Manual Pages](https://www.x.org/releases/current/doc/man/man3/XAllocClassHint.3.xhtml).
/// For details about **`Windows`**'s window class names, see [About Window Classes](https://learn.microsoft.com/en-us/windows/win32/winmsg/about-window-classes).
///
/// ## Platform-specific
///
/// - **`Windows`**: Can only be set while building the window, setting the window's window class name.
/// - **`Wayland`**: Can only be set while building the window, setting the window's application ID.
/// - **`X11`**: Can only be set while building the window, setting the window's `WM_CLASS`.
/// - **`macOS`**, **`iOS`**, **`Android`**, and **`Web`**: not applicable.
///
/// Notes: Changing this field during runtime will have no effect for now.
pub name: Option<String>,
/// How the alpha channel of textures should be handled while compositing. /// How the alpha channel of textures should be handled while compositing.
pub composite_alpha_mode: CompositeAlphaMode, pub composite_alpha_mode: CompositeAlphaMode,
/// The limits of the window's logical size /// The limits of the window's logical size
@ -242,6 +257,7 @@ impl Default for Window {
fn default() -> Self { fn default() -> Self {
Self { Self {
title: "App".to_owned(), title: "App".to_owned(),
name: None,
cursor: Default::default(), cursor: Default::default(),
present_mode: Default::default(), present_mode: Default::default(),
mode: Default::default(), mode: Default::default(),

View file

@ -96,6 +96,60 @@ impl WinitWindows {
.with_transparent(window.transparent) .with_transparent(window.transparent)
.with_visible(window.visible); .with_visible(window.visible);
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "windows"
))]
if let Some(name) = &window.name {
#[cfg(all(
feature = "wayland",
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
)
))]
{
winit_window_builder = winit::platform::wayland::WindowBuilderExtWayland::with_name(
winit_window_builder,
name.clone(),
"",
);
}
#[cfg(all(
feature = "x11",
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
)
))]
{
winit_window_builder = winit::platform::x11::WindowBuilderExtX11::with_name(
winit_window_builder,
name.clone(),
"",
);
}
#[cfg(target_os = "windows")]
{
winit_window_builder =
winit::platform::windows::WindowBuilderExtWindows::with_class_name(
winit_window_builder,
name.clone(),
);
}
}
let constraints = window.resize_constraints.check_constraints(); let constraints = window.resize_constraints.check_constraints();
let min_inner_size = LogicalSize { let min_inner_size = LogicalSize {
width: constraints.min_width, width: constraints.min_width,

View file

@ -14,6 +14,7 @@ fn main() {
DefaultPlugins.set(WindowPlugin { DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window { primary_window: Some(Window {
title: "I am a window!".into(), title: "I am a window!".into(),
name: Some("bevy.app".into()),
resolution: (500., 300.).into(), resolution: (500., 300.).into(),
present_mode: PresentMode::AutoVsync, present_mode: PresentMode::AutoVsync,
// Tells wasm not to override default event handling, like F5, Ctrl+R etc. // Tells wasm not to override default event handling, like F5, Ctrl+R etc.