Skip to content

Commit

Permalink
Fix: handle IconData::default() without crashing
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 19, 2024
1 parent 76ec982 commit cc25a4a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
27 changes: 19 additions & 8 deletions crates/egui-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1338,10 +1338,8 @@ fn process_viewport_command(
egui::viewport::WindowLevel::Normal => WindowLevel::Normal,
}),
ViewportCommand::Icon(icon) => {
window.set_window_icon(icon.map(|icon| {
winit::window::Icon::from_rgba(icon.rgba.clone(), icon.width, icon.height)
.expect("Invalid ICON data!")
}));
let winit_icon = icon.and_then(|icon| to_winit_icon(&icon));
window.set_window_icon(winit_icon);
}
ViewportCommand::IMERect(rect) => {
window.set_ime_cursor_area(
Expand Down Expand Up @@ -1546,10 +1544,8 @@ pub fn create_winit_window_builder<T>(
}

if let Some(icon) = icon {
window_builder = window_builder.with_window_icon(Some(
winit::window::Icon::from_rgba(icon.rgba.clone(), icon.width, icon.height)
.expect("Invalid Icon Data!"),
));
let winit_icon = to_winit_icon(&icon);
window_builder = window_builder.with_window_icon(winit_icon);
}

#[cfg(all(feature = "wayland", target_os = "linux"))]
Expand Down Expand Up @@ -1577,6 +1573,21 @@ pub fn create_winit_window_builder<T>(
window_builder
}

fn to_winit_icon(icon: &egui::IconData) -> Option<winit::window::Icon> {
if icon.is_empty() {
None
} else {
crate::profile_function!();
match winit::window::Icon::from_rgba(icon.rgba.clone(), icon.width, icon.height) {
Ok(winit_icon) => Some(winit_icon),
Err(err) => {
log::warn!("Invalid IconData: {err}");
None
}
}
}
}

/// Applies what `create_winit_window_builder` couldn't
pub fn apply_viewport_builder_to_window(
egui_ctx: &egui::Context,
Expand Down
7 changes: 7 additions & 0 deletions crates/egui/src/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ pub struct IconData {
pub height: u32,
}

impl IconData {
#[inline]
pub fn is_empty(&self) -> bool {
self.rgba.is_empty()
}
}

impl std::fmt::Debug for IconData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("IconData")
Expand Down

0 comments on commit cc25a4a

Please sign in to comment.