tao 0.16.11

Cross-platform window manager library.
Documentation
// Copyright 2014-2021 The winit contributors
// Copyright 2021-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0

use crate::icon::{BadIcon, RgbaIcon};
use std::io::Cursor;

#[derive(Debug, Clone)]
pub struct PlatformIcon(RgbaIcon);

impl PlatformIcon {
  pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> {
    Ok(PlatformIcon(RgbaIcon::from_rgba(rgba, width, height)?))
  }

  pub fn get_size(&self) -> (u32, u32) {
    (self.0.width, self.0.height)
  }

  pub fn to_png(&self) -> Result<Vec<u8>, crate::error::OsError> {
    let mut png = Vec::new();

    {
      let mut encoder =
        png::Encoder::new(Cursor::new(&mut png), self.0.width as _, self.0.height as _);
      encoder.set_color(png::ColorType::Rgba);
      encoder.set_depth(png::BitDepth::Eight);

      let mut writer = encoder.write_header().map_err(|e| os_error!(e.into()))?;
      writer
        .write_image_data(&self.0.rgba)
        .map_err(|e| os_error!(e.into()))?;
    }

    Ok(png)
  }
}