use std::cell::{Cell, RefCell};
use std::rc::{Rc, Weak};
use std::time::Duration;
use waft_embedder::{
HostWindow, IconImage, RawWindowHandle, Rect, RequestId, ScreenId, Size, ViewId, ViewRef,
WindowConfig, WindowSizeLimits,
};
use crate::TestView;
const FIRST_VIEW_ID: u64 = 1000;
pub(crate) fn view_of_request(id: RequestId) -> ViewId {
ViewId(FIRST_VIEW_ID + id.0)
}
pub struct TestWindows {
made: RefCell<Vec<Rc<TestWindow>>>,
screen: Option<ScreenId>,
}
impl TestWindows {
pub fn new() -> TestWindows {
TestWindows {
made: RefCell::default(),
screen: None,
}
}
pub fn on_screen(mut self, screen: ScreenId) -> TestWindows {
self.screen = Some(screen);
self
}
pub fn made(&self) -> Vec<Rc<TestWindow>> {
self.made.borrow().clone()
}
pub(crate) fn request(
&self,
id: RequestId,
config: WindowConfig,
initial_data: Option<String>,
) {
let [width, height] = config.size;
let mut view = TestView::new(width, height)
.with_id(view_of_request(id))
.with_request(id, initial_data);
if let Some(screen) = self.screen {
view = view.on_screen(screen);
}
self.made
.borrow_mut()
.push(TestWindow::around(view, config));
}
pub(crate) fn open_views(&self) -> Vec<ViewRef> {
self.made
.borrow()
.iter()
.filter(|window| window.closes() == 0)
.map(|window| Rc::clone(&window.view) as ViewRef)
.collect()
}
}
impl Default for TestWindows {
fn default() -> TestWindows {
TestWindows::new()
}
}
pub struct TestWindow {
view: Rc<TestView>,
config: WindowConfig,
frame: Cell<Rect>,
title: RefCell<String>,
icon: RefCell<Option<IconImage>>,
shown: Cell<bool>,
closes: Cell<u32>,
drags: Cell<u32>,
minimized: Cell<bool>,
maximized: Cell<bool>,
occluded: Cell<bool>,
min_size: Cell<Option<Size>>,
max_size: Cell<Option<Size>>,
}
impl TestWindow {
pub fn around(view: TestView, config: WindowConfig) -> Rc<TestWindow> {
let [width, height] = config.size;
let [left, top] = config.position.unwrap_or([0.0, 0.0]);
Rc::new_cyclic(|window: &Weak<TestWindow>| {
let window: Weak<dyn HostWindow> = window.clone();
view.set_window(Some(window));
TestWindow {
view: Rc::new(view),
frame: Cell::new(Rect::from_ltwh(left, top, width, height)),
title: RefCell::new(config.title.clone()),
icon: RefCell::new(config.icon.clone()),
shown: Cell::new(config.visible),
config,
closes: Cell::new(0),
drags: Cell::new(0),
minimized: Cell::new(false),
maximized: Cell::new(false),
occluded: Cell::new(false),
min_size: Cell::new(None),
max_size: Cell::new(None),
}
})
}
pub fn test_view(&self) -> &Rc<TestView> {
&self.view
}
pub fn config(&self) -> &WindowConfig {
&self.config
}
pub fn title(&self) -> String {
self.title.borrow().clone()
}
pub fn icon(&self) -> Option<IconImage> {
self.icon.borrow().clone()
}
pub fn is_shown(&self) -> bool {
self.shown.get()
}
pub fn closes(&self) -> u32 {
self.closes.get()
}
pub fn drags(&self) -> u32 {
self.drags.get()
}
pub fn set_minimized(&self, minimized: bool) {
self.minimized.set(minimized);
}
pub fn set_occluded(&self, occluded: bool) {
self.occluded.set(occluded);
}
pub fn min_size(&self) -> Option<Size> {
self.min_size.get()
}
pub fn max_size(&self) -> Option<Size> {
self.max_size.get()
}
}
impl HostWindow for TestWindow {
fn view(&self) -> ViewRef {
Rc::clone(&self.view) as ViewRef
}
fn frame(&self) -> Rect {
self.frame.get()
}
fn set_frame(&self, frame: Rect, _animate: Option<Duration>) {
self.frame.set(frame);
}
fn set_title(&self, title: &str) {
*self.title.borrow_mut() = title.to_owned();
}
fn set_icon(&self, icon: Option<&IconImage>) {
*self.icon.borrow_mut() = icon.cloned();
}
fn show(&self) {
self.shown.set(true);
}
fn hide(&self) {
self.shown.set(false);
}
fn is_minimized(&self) -> bool {
self.minimized.get()
}
fn is_occluded(&self) -> bool {
self.occluded.get()
}
fn close(&self) {
self.closes.set(self.closes.get() + 1);
self.view.set_window(None);
}
fn size_limits(&self) -> Option<&dyn WindowSizeLimits> {
Some(self)
}
fn drag(&self) {
self.drags.set(self.drags.get() + 1);
}
fn is_maximized(&self) -> bool {
self.maximized.get()
}
fn set_maximized(&self, maximized: bool) {
self.maximized.set(maximized);
}
fn set_minimized(&self, minimized: bool) {
self.minimized.set(minimized);
}
fn native_handle(&self) -> Option<RawWindowHandle> {
None
}
}
impl WindowSizeLimits for TestWindow {
fn set_min_size(&self, size: Option<Size>) {
self.min_size.set(size);
}
fn set_max_size(&self, size: Option<Size>) {
self.max_size.set(size);
}
}