Skip to main content

b3_core/
icon.rs

1//! This module contains image type definitions.
2
3use crate::{
4    platform::{IconApi, Wrapper},
5    platform_impl::IconImpl,
6    ContextOwner,
7    Error,
8};
9
10/// Icon types.
11#[derive(Debug)]
12pub enum IconType {
13    /// GIF.
14    Gif,
15    /// JPEG.
16    Jpeg,
17    /// PNG.
18    Png,
19    /// TIFF.
20    Tiff,
21}
22
23/// System icon.
24#[derive(Debug)]
25pub struct Icon(IconImpl);
26
27impl Icon {
28    /// Creates a new icon from bytes.
29    ///
30    /// # Parameters:
31    /// * `ctx` - Context owner.
32    /// * `icon_data` - Icon data in bytes.
33    /// * `icon_type` - Icon type.
34    pub fn from_data(
35        ctx: &impl ContextOwner,
36        icon_data: &Vec<u8>,
37        icon_type: IconType,
38    ) -> Result<Self, Error> {
39        Ok(Self(IconImpl::from_data(ctx, icon_data, icon_type)?))
40    }
41
42    /// Creates a new image from built-in system icons.
43    ///
44    /// # Parameters:
45    /// * `ctx` - Context owner.
46    /// * `title` - Built-in system icon title.
47    pub fn from_str<S>(ctx: &impl ContextOwner, title: S) -> Result<Self, Error>
48    where
49        S: Into<String>,
50    {
51        Ok(Self(IconImpl::from_str(ctx, &title.into())?))
52    }
53}
54
55impl Wrapper<IconImpl> for Icon {
56    #[inline]
57    fn get_impl(&self) -> &IconImpl { &self.0 }
58
59    #[inline]
60    fn get_impl_mut(&mut self) -> &mut IconImpl { &mut self.0 }
61}