From aa8cae7551bb9beae2128362a14022afd3c63d64 Mon Sep 17 00:00:00 2001 From: Bude Date: Fri, 28 May 2021 11:40:40 +0200 Subject: [PATCH 01/17] issue-1031: Add base functionality to change window icon at runtime --- crates/bevy_window/Cargo.toml | 1 + crates/bevy_window/src/window.rs | 25 +++++++++++++++++++++++++ crates/bevy_winit/src/lib.rs | 14 +++++++++----- examples/window/window_settings.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 5 deletions(-) diff --git a/crates/bevy_window/Cargo.toml b/crates/bevy_window/Cargo.toml index b7064f986612e..1939fd326a252 100644 --- a/crates/bevy_window/Cargo.toml +++ b/crates/bevy_window/Cargo.toml @@ -18,6 +18,7 @@ bevy_app = { path = "../bevy_app", version = "0.5.0" } bevy_ecs = { path = "../bevy_ecs", version = "0.5.0" } bevy_math = { path = "../bevy_math", version = "0.5.0" } bevy_utils = { path = "../bevy_utils", version = "0.5.0" } +bevy_asset = { path = "../bevy_asset", version = "0.5.0" } # other diff --git a/crates/bevy_window/src/window.rs b/crates/bevy_window/src/window.rs index be3a6c0f99230..57dbcfdf0a2a3 100644 --- a/crates/bevy_window/src/window.rs +++ b/crates/bevy_window/src/window.rs @@ -1,5 +1,6 @@ use bevy_math::{IVec2, Vec2}; use bevy_utils::{tracing::warn, Uuid}; +use bevy_asset::Handle; #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] pub struct WindowId(Uuid); @@ -127,6 +128,7 @@ pub struct Window { mode: WindowMode, #[cfg(target_arch = "wasm32")] pub canvas: Option, + icon: Option, command_queue: Vec, } @@ -176,6 +178,9 @@ pub enum WindowCommand { SetResizeConstraints { resize_constraints: WindowResizeConstraints, }, + SetWindowIcon { + icon: Option, + }, } /// Defines the way a window is displayed @@ -220,6 +225,7 @@ impl Window { mode: window_descriptor.mode, #[cfg(target_arch = "wasm32")] canvas: window_descriptor.canvas.clone(), + icon: window_descriptor.icon, command_queue: Vec::new(), } } @@ -511,6 +517,23 @@ impl Window { pub fn is_focused(&self) -> bool { self.focused } + + #[inline] + pub fn icon(&self) -> Option { + self.icon + } + + pub fn set_icon(&mut self, icon: Option) { + self.icon = icon; + self.command_queue.push(WindowCommand::SetWindowIcon { + icon, + }); + } +} + +#[derive(Debug, Clone, Copy)] +pub struct WindowIcon { + /* icon: Handle, */ } #[derive(Debug, Clone)] @@ -528,6 +551,7 @@ pub struct WindowDescriptor { pub mode: WindowMode, #[cfg(target_arch = "wasm32")] pub canvas: Option, + pub icon: Option, } impl Default for WindowDescriptor { @@ -546,6 +570,7 @@ impl Default for WindowDescriptor { mode: WindowMode::Windowed, #[cfg(target_arch = "wasm32")] canvas: None, + icon: None, } } } diff --git a/crates/bevy_winit/src/lib.rs b/crates/bevy_winit/src/lib.rs index 3ba197c05c9f2..a5f4ab6e2fc5d 100644 --- a/crates/bevy_winit/src/lib.rs +++ b/crates/bevy_winit/src/lib.rs @@ -14,15 +14,12 @@ use bevy_app::{App, AppBuilder, AppExit, CoreStage, Events, ManualEventReader, P use bevy_ecs::{system::IntoExclusiveSystem, world::World}; use bevy_math::{ivec2, Vec2}; use bevy_utils::tracing::{error, trace, warn}; -use bevy_window::{ - CreateWindow, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter, - WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCreated, WindowFocused, - WindowMoved, WindowResized, WindowScaleFactorChanged, Windows, -}; +use bevy_window::{CreateWindow, CursorEntered, CursorLeft, CursorMoved, FileDragAndDrop, ReceivedCharacter, WindowBackendScaleFactorChanged, WindowCloseRequested, WindowCommand, WindowCreated, WindowFocused, WindowMoved, WindowResized, WindowScaleFactorChanged, Windows}; use winit::{ dpi::PhysicalPosition, event::{self, DeviceEvent, Event, WindowEvent}, event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget}, + window::Icon, }; use winit::dpi::LogicalSize; @@ -158,6 +155,13 @@ fn change_window(world: &mut World) { window.set_max_inner_size(Some(max_inner_size)); } } + bevy_window::WindowCommand::SetWindowIcon { icon } => { + let window = winit_windows.get_window(id).unwrap(); + //let winit_icon = Icon::default; + let winit_icon = None; + + window.set_window_icon(winit_icon); + } } } } diff --git a/examples/window/window_settings.rs b/examples/window/window_settings.rs index 3c7719916ac2e..a357e2570ba27 100644 --- a/examples/window/window_settings.rs +++ b/examples/window/window_settings.rs @@ -10,12 +10,28 @@ fn main() { vsync: true, ..Default::default() }) + .insert_resource(IconResource::default()) .add_plugins(DefaultPlugins) + .add_startup_system(setup.system()) .add_system(change_title.system()) .add_system(toggle_cursor.system()) + .add_system(toggle_icon.system()) .run(); } +#[derive(Debug, Clone, Default)] +struct IconResource { + handle: Handle, +} + +fn setup( + asset_server: Res, + mut icon_resource: ResMut, +) { + let icon: /* TODO */ Handle = asset_server.load("android-res/mipmap-mdpi/ic_launcher.png"); + (*icon_resource).handle = icon; +} + /// This system will then change the title during execution fn change_title(time: Res