use std::{ptr, fmt, hash::{Hash, Hasher}};
use ncursesw::{SCREEN, WINDOW};
use crate::{Screen, newscr, gen::*};
pub struct Window {
screen: Option<SCREEN>, handle: WINDOW, free_on_drop: bool }
impl HasHandle<WINDOW> for Window {
fn _from(screen: Option<SCREEN>, handle: WINDOW, free_on_drop: bool) -> Self {
assert!(screen.map_or_else(|| true, |screen| !screen.is_null()), "Window::_from() : screen.is_null()");
assert!(!handle.is_null(), "Window::_from() : handle.is_null()");
Self { screen, handle, free_on_drop }
}
fn _screen(&self) -> Option<SCREEN> {
self.screen
}
fn _handle(&self) -> WINDOW {
self.handle
}
}
impl Window {
pub fn screen(&self) -> Option<Screen> {
self.screen.map(|screen| Screen::_from(screen, false))
}
}
impl NCurseswWindow for Window { }
impl IsWindow for Window { }
impl BaseCanvas for Window { }
impl CanSubWindow for Window { }
impl Mouseable for Window { }
impl Moveable for Window { }
impl Derivable for Window { }
impl Scrollable for Window { }
impl HasYAxis for Window { }
impl HasYXAxis for Window { }
impl HasXAxis for Window { }
impl GraphicsTransform for Window { }
impl HasGraphicFunctions for Window { }
impl HasBackground for Window { }
impl HasAttributes for Window { }
impl HasMvAttributes for Window { }
impl HasAddFunctions for Window { }
impl HasMvAddFunctions for Window { }
impl HasDelFunctions for Window { }
impl HasMvDelFunctions for Window { }
impl HasInFunctions for Window { }
impl HasMvInFunctions for Window { }
impl HasInsFunctions for Window { }
impl HasMvInsFunctions for Window { }
impl HasNonBlocking for Window { }
impl HasGetFunctions for Window { }
impl HasMvGetFunctions for Window { }
impl Drop for Window {
fn drop(&mut self) {
if self.free_on_drop {
if let Err(source) = ncursesw::delwin(self.handle) {
panic!("{} @ {:?}", source, self)
}
}
}
}
impl Default for Window {
fn default() -> Self {
newscr()
}
}
unsafe impl Send for Window { } unsafe impl Sync for Window { }
impl PartialEq for Window {
fn eq(&self, rhs: &Self) -> bool {
ptr::eq(self.handle, rhs.handle)
}
}
impl Eq for Window { }
impl Hash for Window {
fn hash<H: Hasher>(&self, state: &mut H) {
self.handle.hash(state);
}
}
impl AsRef<Window> for Window {
fn as_ref(&self) -> &Self {
self
}
}
impl Clone for Window {
fn clone(&self) -> Self {
Self::_from(self.screen.clone(), self.handle.clone(), false)
}
}
impl fmt::Debug for Window {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Window {{ screen: {:?}, handle: {:p}, free_on_drop: {} }}", self.screen, self.handle, self.free_on_drop)
}
}