use std::rc::Rc;
use mitsuami_core::{AnyView, CurrentWindow, Modality, NodeId, Prop, Size, Ui, UiEvent, View, WidgetKind, WindowSize};
use mitsuami_reactive::{IntoValue, Signal, Value, computed, effect, inject, on_cleanup, provide, untrack};
pub struct Window<T = Value<String>> {
title: T,
size: Value<WindowSize>,
modality: Value<Option<Modality>>,
open: Value<bool>,
full_screen: Option<Signal<bool>>,
maximized: Option<Signal<bool>>,
resizable: Option<Value<bool>>,
min_size: Option<Value<Size>>,
on_open: Option<Rc<dyn Fn()>>,
on_close_request: Option<Rc<dyn Fn()>>,
content: Option<Rc<dyn Fn() -> AnyView>>,
}
impl Window {
pub fn new(title: impl IntoValue<String>) -> Window {
Window {
title: title.into_value(),
size: Value::Static(WindowSize::FitHeight(480.0)),
modality: Value::Static(None),
open: Value::Static(true),
full_screen: None,
maximized: None,
resizable: None,
min_size: None,
on_open: None,
on_close_request: None,
content: None,
}
}
}
impl<T> Window<T> {
pub fn size(mut self, size: impl IntoValue<WindowSize>) -> Window<T> {
self.size = size.into_value();
self
}
pub fn modal(mut self, modality: Modality) -> Window<T> {
self.modality = Value::Static(Some(modality));
self
}
pub fn modality(mut self, modality: impl IntoValue<Option<Modality>>) -> Window<T> {
self.modality = modality.into_value();
self
}
pub fn open(mut self, open: impl IntoValue<bool>) -> Window<T> {
self.open = open.into_value();
self
}
pub fn bind(self, open: Signal<bool>) -> Window<T> {
self.open(open).on_close_request(move || open.set(false))
}
pub fn full_screen(mut self, full_screen: Signal<bool>) -> Window<T> {
self.full_screen = Some(full_screen);
self
}
pub fn maximized(mut self, maximized: Signal<bool>) -> Window<T> {
self.maximized = Some(maximized);
self
}
pub fn resizable(mut self, resizable: impl IntoValue<bool>) -> Window<T> {
self.resizable = Some(resizable.into_value());
self
}
pub fn min_size(mut self, size: impl IntoValue<Size>) -> Window<T> {
self.min_size = Some(size.into_value());
self
}
pub fn on_open(mut self, handler: impl Fn() + 'static) -> Window<T> {
self.on_open = Some(Rc::new(handler));
self
}
pub fn on_close_request(mut self, handler: impl Fn() + 'static) -> Window<T> {
self.on_close_request = Some(Rc::new(handler));
self
}
pub fn content<V: View>(mut self, content: impl Fn() -> V + 'static) -> Window<T> {
self.content = Some(Rc::new(move || AnyView::new(content())));
self
}
}
impl View for Window {
fn build(self, ui: &Ui) -> NodeId {
let placeholder = ui.create(WidgetKind::Fragment, Vec::new());
let Window {
title,
size,
modality,
open,
full_screen,
maximized,
resizable,
min_size,
on_open,
on_close_request,
content,
} = self;
let owner = inject::<CurrentWindow>().map(|w| w.0);
let ui = ui.clone();
let show = move || {
let window = ui.create_window(String::new(), size.get());
if let Some(modality) = modality.get() {
let modality = if owner.is_some() { modality } else { Modality::Application };
ui.set_prop(window, Prop::Modal { owner, modality });
}
let title = title.clone();
let titled = ui.clone();
effect(move || titled.set_prop(window, Prop::Title(title.get())));
if let Some(min_size) = min_size.clone() {
let ui = ui.clone();
effect(move || ui.set_prop(window, Prop::MinSize(min_size.get())));
}
if let Some(full_screen) = full_screen {
let filled = ui.clone();
effect(move || filled.set_prop(window, Prop::FullScreen(full_screen.get())));
ui.on_event(window, move |event| {
if let UiEvent::FullScreenChanged(on) = event {
full_screen.set(*on);
}
});
}
if let Some(maximized) = maximized {
let zoomed = ui.clone();
effect(move || zoomed.set_prop(window, Prop::Maximized(maximized.get())));
ui.on_event(window, move |event| {
if let UiEvent::MaximizedChanged(on) = event {
maximized.set(*on);
}
});
}
if let Some(resizable) = resizable.clone() {
let ui = ui.clone();
effect(move || ui.set_prop(window, Prop::Resizable(resizable.get())));
}
provide(CurrentWindow(window));
if let Some(handler) = &on_open {
handler();
}
if let Some(content) = &content {
let root = content().build(&ui);
ui.append_child(window, root);
}
if let Some(handler) = on_close_request.clone() {
ui.on_event(window, move |event| {
if *event == UiEvent::WindowCloseRequested {
handler();
}
});
}
let ui = ui.clone();
on_cleanup(move || ui.destroy(window));
};
let open = computed(move || open.get());
effect(move || {
if open.get() {
untrack(&show);
}
});
placeholder
}
}
impl Window {
#[doc(hidden)]
pub fn __tag() -> Window<()> {
Window::new(String::new()).retitled(())
}
#[doc(hidden)]
pub fn __children<V: View>(self, content: impl Fn() -> V + 'static) -> Window {
self.content(content)
}
}
impl Window<()> {
#[doc(hidden)]
pub fn title(self, title: impl IntoValue<String>) -> Window {
self.retitled(title.into_value())
}
}
impl<T> Window<T> {
fn retitled<U>(self, title: U) -> Window<U> {
let Window {
title: _,
size,
modality,
open,
full_screen,
maximized,
resizable,
min_size,
on_open,
on_close_request,
content,
} = self;
Window {
title,
size,
modality,
open,
full_screen,
maximized,
resizable,
min_size,
on_open,
on_close_request,
content,
}
}
}