From 6d5d632c9642467e393279af4e5014cc55fc2065 Mon Sep 17 00:00:00 2001 From: Jade Date: Thu, 11 Feb 2021 20:47:27 -0800 Subject: [PATCH] Add an icon to the SDL2 window (#462) * Add an icon to the SDL2 window * Use the native endian RGBA32 so we don't have wrong icons on BE --- src/window/sdl2/mod.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/window/sdl2/mod.rs b/src/window/sdl2/mod.rs index b264ded..33a6ee0 100644 --- a/src/window/sdl2/mod.rs +++ b/src/window/sdl2/mod.rs @@ -7,6 +7,7 @@ use crate::{ redraw_scheduler::REDRAW_SCHEDULER, renderer::Renderer, settings::SETTINGS, }; use crossfire::mpsc::TxUnbounded; +use image::load_from_memory_with_format; use layouts::handle_qwerty_layout; use skulpin::{ ash::prelude::VkResult, @@ -391,6 +392,28 @@ fn allow_compositing() { sdl2::hint::set(name, "0"); } +fn set_icon(win: &mut sdl2::video::Window) { + let icon_data = Asset::get("nvim.ico").expect("Failed to read icon data"); + let icon = load_from_memory_with_format(&icon_data, image::ImageFormat::ICO) + .expect("Failed to parse icon data"); + + let icon = icon.into_rgba(); + let width = icon.width(); + let height = icon.height(); + let mut icon = icon.into_raw(); + + let surf = sdl2::surface::Surface::from_data( + &mut icon, + width, + height, + 4 * width, + sdl2::pixels::PixelFormatEnum::RGBA32, + ) + .expect("Failed to create icon surface"); + + win.set_icon(surf); +} + pub fn start_loop( window_command_receiver: Receiver, ui_command_sender: TxUnbounded, @@ -419,6 +442,8 @@ pub fn start_loop( .expect("Failed to create window"); log::info!("window created"); + set_icon(&mut sdl_window); + if std::env::args().any(|arg| arg == "--maximized") { sdl_window.maximize(); }