cursive-image 0.0.6

Image view for the Cursive TUI library
Documentation
use super::{format::*, source::*, stream::*};

use {
    cursive::*,
    std::{cell::*, io, path::*, sync::*},
};

//
// Image
//

/// Image.
pub struct Image {
    /// Source.
    pub source: ImageSource,

    /// Format.
    pub format: ImageFormat,

    /// Size (in pixels).
    pub size: Vec2,

    pub(crate) id: Mutex<RefCell<Option<usize>>>,
    pub(crate) placement: Mutex<RefCell<Option<usize>>>,
}

impl Image {
    /// Constructor.
    pub fn new_owned<SizeT>(data: Vec<u8>, compressed: bool, format: ImageFormat, size: SizeT) -> Self
    where
        SizeT: Into<Vec2>,
    {
        Self {
            source: ImageSource::Owned(data, compressed),
            format,
            size: size.into(),
            id: Default::default(),
            placement: Default::default(),
        }
    }

    /// Constructor.
    pub fn new_file<PathT, SizeT>(path: PathT, compressed: bool, format: ImageFormat, size: SizeT) -> Self
    where
        PathT: Into<PathBuf>,
        SizeT: Into<Vec2>,
    {
        Self {
            source: ImageSource::LocalFile(path.into(), compressed),
            format,
            size: size.into(),
            id: Default::default(),
            placement: Default::default(),
        }
    }

    /// Constructor.
    pub fn new_stream<ImageStreamT, SizeT>(stream: ImageStreamT, format: ImageFormat, size: SizeT) -> Self
    where
        ImageStreamT: 'static + ImageStream + Send + Sync,
        SizeT: Into<Vec2>,
    {
        Self {
            source: ImageSource::Stream(Box::new(stream)),
            format,
            size: size.into(),
            id: Default::default(),
            placement: Default::default(),
        }
    }

    /// Into owned.
    pub fn into_owned(&mut self) -> io::Result<()> {
        self.source.into_owned()
    }

    pub(crate) fn get_id(&self) -> Option<usize> {
        *self.id.lock().unwrap().borrow()
    }

    pub(crate) fn set_id(&self, id: Option<usize>) {
        *self.id.lock().unwrap().borrow_mut() = id;
    }

    pub(crate) fn get_placement(&self) -> Option<usize> {
        *self.placement.lock().unwrap().borrow()
    }

    pub(crate) fn set_placement(&self, placement: Option<usize>) {
        *self.placement.lock().unwrap().borrow_mut() = placement;
    }
}