minifb/
icon.rs

1#[cfg(target_os = "linux")]
2use std::convert::TryFrom;
3#[cfg(target_os = "windows")]
4use std::{ffi::OsStr, os::windows::prelude::OsStrExt, str::FromStr};
5
6/// Represents a window icon
7///
8/// Different under Windows, Linux and MacOS
9///
10/// **Windows**: Icon can be created from a relative path string
11///
12/// **Linux / X11:** Icon can be created from an ARGB buffer
13#[derive(Clone, Copy, Debug)]
14pub enum Icon {
15    Path(*const u16),
16    Buffer(*const u64, u32),
17}
18
19#[cfg(target_os = "windows")]
20impl FromStr for Icon {
21    type Err = &'static str;
22
23    fn from_str(s: &str) -> Result<Self, Self::Err> {
24        if s.is_empty() {
25            return Err("Path to icon cannot be empty!");
26        }
27
28        let path = OsStr::new(s)
29            .encode_wide()
30            //.chain(Some(0).into_iter())
31            .chain(Some(0))
32            .collect::<Vec<u16>>();
33
34        Ok(Icon::Path(path.as_ptr()))
35    }
36}
37
38#[cfg(target_os = "linux")]
39impl TryFrom<&[u64]> for Icon {
40    type Error = &'static str;
41
42    fn try_from(value: &[u64]) -> Result<Self, Self::Error> {
43        if value.is_empty() {
44            return Err("ARGB buffer cannot be empty!");
45        }
46
47        Ok(Icon::Buffer(value.as_ptr(), value.len() as u32))
48    }
49}