use crate::controls::UIElement;
use crate::error::Result;
use parking_lot::RwLock;
use std::sync::Arc;
use windows::Win32::Foundation::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Stretch {
None,
Fill,
Uniform,
UniformToFill,
}
#[derive(Clone)]
pub struct Image {
element: UIElement,
inner: Arc<ImageInner>,
}
struct ImageInner {
source: RwLock<Option<String>>,
stretch: RwLock<Stretch>,
}
impl Image {
pub fn new() -> Result<Self> {
let inner = Arc::new(ImageInner {
source: RwLock::new(None),
stretch: RwLock::new(Stretch::Uniform),
});
Ok(Image {
element: UIElement::empty(),
inner,
})
}
pub fn source(&self) -> Option<String> {
self.inner.source.read().clone()
}
pub fn set_source(&self, source: Option<String>) {
*self.inner.source.write() = source;
}
pub fn stretch(&self) -> Stretch {
*self.inner.stretch.read()
}
pub fn set_stretch(&self, stretch: Stretch) {
*self.inner.stretch.write() = stretch;
}
pub fn with_stretch(self, stretch: Stretch) -> Self {
self.set_stretch(stretch);
self
}
pub fn element(&self) -> &UIElement {
&self.element
}
pub fn hwnd(&self) -> HWND {
self.element.hwnd()
}
}
impl Default for Image {
fn default() -> Self {
Self::new().expect("Failed to create image")
}
}
impl From<Image> for UIElement {
fn from(image: Image) -> Self {
image.element.clone()
}
}