winit-core: move icon

This commit is contained in:
Kirill Chibisov 2025-05-01 19:59:29 +09:00
parent a491c2abed
commit cbb29ab526
8 changed files with 60 additions and 39 deletions

View file

@ -1,7 +1,18 @@
#![allow(clippy::assertions_on_constants)]
use super::*;
use crate::icon::{Pixel, RgbaIcon, PIXEL_SIZE};
use crate::icon::RgbaIcon;
pub(crate) const PIXEL_SIZE: usize = mem::size_of::<Pixel>();
#[repr(C)]
#[derive(Debug)]
pub(crate) struct Pixel {
pub(crate) r: u8,
pub(crate) g: u8,
pub(crate) b: u8,
pub(crate) a: u8,
}
impl Pixel {
pub fn to_packed_argb(&self) -> Cardinal {
@ -18,19 +29,17 @@ impl Pixel {
}
}
impl RgbaIcon {
pub(crate) fn to_cardinals(&self) -> Vec<Cardinal> {
assert_eq!(self.rgba.len() % PIXEL_SIZE, 0);
let pixel_count = self.rgba.len() / PIXEL_SIZE;
assert_eq!(pixel_count, (self.width * self.height) as usize);
let mut data = Vec::with_capacity(pixel_count);
data.push(self.width as Cardinal);
data.push(self.height as Cardinal);
let pixels = self.rgba.as_ptr() as *const Pixel;
for pixel_index in 0..pixel_count {
let pixel = unsafe { &*pixels.add(pixel_index) };
data.push(pixel.to_packed_argb());
}
data
pub(crate) fn rgba_to_cardinals(icon: &RgbaIcon) -> Vec<Cardinal> {
assert_eq!(icon.buffer().len() % PIXEL_SIZE, 0);
let pixel_count = icon.buffer().len() / PIXEL_SIZE;
assert_eq!(pixel_count, (icon.width() * icon.height()) as usize);
let mut data = Vec::with_capacity(pixel_count);
data.push(icon.width() as Cardinal);
data.push(icon.height() as Cardinal);
let pixels = icon.buffer().as_ptr() as *const Pixel;
for pixel_index in 0..pixel_count {
let pixel = unsafe { &*pixels.add(pixel_index) };
data.push(pixel.to_packed_argb());
}
data
}

View file

@ -25,6 +25,7 @@ use x11rb::protocol::xproto::{self, ConnectionExt as _};
pub use self::cursor::*;
pub use self::geometry::*;
pub use self::hint::*;
pub(crate) use self::icon::rgba_to_cardinals;
pub use self::input::*;
pub use self::mouse::*;
pub use self::window_property::*;

View file

@ -35,6 +35,7 @@ use crate::monitor::{
use crate::platform::x11::WindowType;
use crate::platform_impl::common;
use crate::platform_impl::x11::atoms::*;
use crate::platform_impl::x11::util::rgba_to_cardinals;
use crate::platform_impl::x11::{
xinput_fp1616_to_float, MonitorHandle as X11MonitorHandle, WakeSender, X11Error,
};
@ -205,7 +206,7 @@ impl CoreWindow for Window {
fn set_window_icon(&self, window_icon: Option<crate::window::Icon>) {
let icon = match window_icon.as_ref() {
Some(icon) => icon.0.cast_ref::<RgbaIcon>(),
Some(icon) => icon.cast_ref::<RgbaIcon>(),
None => None,
};
self.0.set_window_icon(icon)
@ -771,7 +772,7 @@ impl UnownedWindow {
// Set window icons
if let Some(icon) =
window_attrs.window_icon.as_ref().and_then(|icon| icon.0.cast_ref::<RgbaIcon>())
window_attrs.window_icon.as_ref().and_then(|icon| icon.cast_ref::<RgbaIcon>())
{
leap!(window.set_icon_inner(icon)).ignore_error();
}
@ -1414,7 +1415,7 @@ impl UnownedWindow {
fn set_icon_inner(&self, icon: &RgbaIcon) -> Result<VoidCookie<'_>, X11Error> {
let atoms = self.xconn.atoms();
let icon_atom = atoms[_NET_WM_ICON];
let data = icon.to_cardinals();
let data = rgba_to_cardinals(icon);
self.xconn.change_property(
self.xwindow,
icon_atom,