use std::path::PathBuf;
use image;
use qualia::{AestheticsConfig, Buffer, PixelFormat, SurfaceId, AestheticsCoordinationTrait};
pub struct Background<C>
where C: AestheticsCoordinationTrait
{
background_sid: SurfaceId,
background_path: Option<PathBuf>,
buffer: Buffer,
coordinator: C,
}
impl<C> Background<C>
where C: AestheticsCoordinationTrait
{
pub fn new(coordinator: C, config: AestheticsConfig) -> Self {
Background {
background_sid: SurfaceId::invalid(),
background_path: config.background_path,
buffer: Buffer::empty(),
coordinator: coordinator,
}
}
pub fn set_background(&mut self) {
if let Some(ref path) = self.background_path {
match image::open(&path) {
Ok(img) => {
let rgba = img.to_rgba();
let f = PixelFormat::ABGR8888;
let w = rgba.width() as usize;
let h = rgba.height() as usize;
let s = w * f.get_size();
let d = rgba.into_raw();
self.buffer = Buffer::new(f, w, h, s, d);
let memory = unsafe { self.buffer.as_memory() };
let background_sid = self.coordinator.create_surface();
let bid = self.coordinator.create_memory_pool(memory);
if let Some(mvid) = self.coordinator.create_memory_view(bid, f, 0, w, h, s) {
self.coordinator.attach_shm(mvid, background_sid);
self.coordinator.commit_surface(background_sid);
self.coordinator.set_surface_as_background(background_sid);
} else {
log_warn1!("Failed to create memory view for background");
}
}
Err(err) => log_warn1!("Failed to open background file: {:?}", err),
}
}
}
}
impl<C> Background<C>
where C: AestheticsCoordinationTrait
{
pub fn on_surface_change(&mut self, sid: SurfaceId) {
self.background_sid = sid;
}
pub fn on_display_created(&mut self) {
if !self.background_sid.is_valid() {
self.set_background();
}
}
}