Skip to content

Commit c7e2a8a

Browse files
committed
issue-1031: Small changes, documentation, comments
1 parent d6eb2f5 commit c7e2a8a

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

crates/bevy_render/src/lib.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,7 @@ impl Plugin for RenderPlugin {
209209
.label(RenderSystem::VisibleEntities)
210210
.after(TransformSystem::TransformPropagate),
211211
)
212-
.add_system_to_stage(
213-
CoreStage::PostUpdate,
214-
window_icon_changed.system(), /* TODO: label? */
215-
)
212+
.add_system_to_stage(CoreStage::PostUpdate, window_icon_changed.system())
216213
.add_system_to_stage(
217214
RenderStage::RenderResource,
218215
shader::shader_update_system.system(),
@@ -274,8 +271,8 @@ fn window_icon_changed(
274271
if let Some(handle_path) = asset_server.get_handle_path(o.get()) {
275272
if handle_path.path() != path {
276273
o.insert(asset_server.load(path.clone()));
277-
}
278-
}
274+
} /* else we are still attempting to load the initial asset */
275+
} /* else the path from the asset is not available yet */
279276
}
280277
Entry::Vacant(v) => {
281278
v.insert(asset_server.load(path.clone()));
@@ -290,9 +287,7 @@ fn window_icon_changed(
290287
LoadState::Loaded => {
291288
let texture = textures.get(handle).unwrap(); /* Safe to unwrap here, because loadstate==loaded is checked */
292289

293-
/* TODO: Not actually sure if we need to check the error here
294-
Whatever Texture gives us might be fine */
295-
let window_icon_bytes = WindowIconBytes::new(
290+
let window_icon_bytes = WindowIconBytes::from_rgba(
296291
texture.data.clone(),
297292
texture.size.width,
298293
texture.size.height,

crates/bevy_window/src/window.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,18 @@ impl WindowResizeConstraints {
9191
}
9292
}
9393

94+
/// Generic icon buffer for a window.
95+
/// Replicates the struct from winit.
96+
///
97+
/// Only allows rgba images.
9498
#[derive(Debug, Clone)]
9599
pub struct WindowIconBytes {
96100
bytes: Vec<u8>,
97101
width: u32,
98102
height: u32,
99103
}
100104

105+
/// Errors that occur while constructing window icons.
101106
#[derive(Error, Debug)]
102107
pub enum WindowIconBytesError {
103108
#[error("32bpp RGBA image buffer expected, but {bytes_length} is not divisible by 4")]
@@ -109,6 +114,10 @@ pub enum WindowIconBytesError {
109114
},
110115
}
111116

117+
/// The icon on a window.
118+
/// The path buffer in the `Path` variant will be passed to the asset server and will automatically trigger the call to the window backend.
119+
///
120+
/// Make sure that the source image is reasonably sized. Refer to winit's `set_window_icon` function.
112121
#[derive(Debug, Clone)]
113122
pub enum WindowIcon {
114123
Path(PathBuf),
@@ -131,7 +140,14 @@ impl From<WindowIconBytes> for WindowIcon {
131140
}
132141

133142
impl WindowIconBytes {
134-
pub fn new(bytes: Vec<u8>, width: u32, height: u32) -> Result<Self, WindowIconBytesError> {
143+
/// Create a window icon from a rgba image.
144+
///
145+
/// Returns a `WindowIconBytesError` if `bytes` do not add up to a rgba image or the size does not match the specified width and height.
146+
pub fn from_rgba(
147+
bytes: Vec<u8>,
148+
width: u32,
149+
height: u32,
150+
) -> Result<Self, WindowIconBytesError> {
135151
let pixel_count = (width * height) as usize;
136152
let pixel_bytes_length = pixel_count * 4;
137153
let bytes_length = bytes.len();
@@ -152,14 +168,17 @@ impl WindowIconBytes {
152168
}
153169
}
154170

171+
/// Bytes of the rgba icon.
155172
pub fn bytes(&self) -> &[u8] {
156173
&self.bytes
157174
}
158175

176+
/// Width of the icon.
159177
pub fn width(&self) -> u32 {
160178
self.width
161179
}
162180

181+
/// Height if the icon.
163182
pub fn height(&self) -> u32 {
164183
self.height
165184
}
@@ -200,9 +219,9 @@ pub struct Window {
200219
cursor_position: Option<Vec2>,
201220
focused: bool,
202221
mode: WindowMode,
222+
icon: Option<WindowIcon>,
203223
#[cfg(target_arch = "wasm32")]
204224
pub canvas: Option<String>,
205-
icon: Option<WindowIcon>,
206225
command_queue: Vec<WindowCommand>,
207226
}
208227

crates/bevy_winit/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ fn change_window(world: &mut World) {
163163
bevy_window::WindowCommand::SetIcon { window_icon_bytes } => {
164164
let window = winit_windows.get_window(id).unwrap();
165165

166-
/* Failures should already be covered in WindowIconBytes constructor */
166+
/* Winit errors are replicated in the WindowIconBytes constructor, so it is safe to ignore here */
167167
window.set_window_icon(
168168
Icon::from_rgba(
169169
window_icon_bytes.bytes().to_vec(),

crates/bevy_winit/src/winit_windows.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl WinitWindows {
149149
);
150150

151151
if let Some(icon_path) = &window_descriptor.icon_path {
152-
window.set_icon(icon_path); /* This will queue up SetWindowIcon until the asset has loaded */
152+
window.set_icon(icon_path); /* This will queue up loading the asset and subsequently set the window icon */
153153
}
154154

155155
window

examples/window/window_settings.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ fn toggle_cursor(input: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
3636
}
3737
}
3838

39+
/// This system toggles the windows' icon (on/off) when I is pressed
3940
fn toggle_icon(input: Res<Input<KeyCode>>, mut windows: ResMut<Windows>) {
4041
let window = windows.get_primary_mut().unwrap();
4142
if input.just_pressed(KeyCode::I) {
4243
match window.icon() {
4344
None => {
45+
/* Alternatively you can construct a "buffer-based" WindowIcon and bypass the asset server */
4446
window.set_icon("android-res/mipmap-mdpi/ic_launcher.png");
4547
}
4648
_ => window.clear_icon(),

0 commit comments

Comments
 (0)