1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use std::ops::{Deref, DerefMut};

use crate::{Context, Inspectable};
use bevy_egui::egui::{self, Id};
use pretty_type_name::pretty_type_name;

/// The attributes for [`InNewWindow`]
#[allow(missing_docs)]
pub struct WindowAttributes<T: Inspectable> {
    pub title: Option<&'static str>,
    pub title_bar: bool,
    pub scrollable: bool,
    pub resizable: bool,
    pub collapsible: bool,
    pub inner_attributes: <T as Inspectable>::Attributes,
}
impl<T: Inspectable> Clone for WindowAttributes<T> {
    fn clone(&self) -> Self {
        Self {
            title: self.title,
            title_bar: self.title_bar,
            scrollable: self.scrollable,
            resizable: self.resizable,
            collapsible: self.collapsible,
            inner_attributes: self.inner_attributes.clone(),
        }
    }
}
impl<T: Inspectable> Default for WindowAttributes<T> {
    fn default() -> Self {
        WindowAttributes {
            title: None,
            title_bar: true,
            scrollable: false,
            resizable: false,
            collapsible: true,
            inner_attributes: Default::default(),
        }
    }
}

#[derive(Default)]
/// Wrapper type which displays the inner value inside another egui window.
///
/// Can be configured using [`WindowAttributes`].
pub struct InNewWindow<T>(pub T);

impl<T> Deref for InNewWindow<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl<T> DerefMut for InNewWindow<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T: Inspectable + 'static> Inspectable for InNewWindow<T> {
    type Attributes = WindowAttributes<T>;

    fn ui(&mut self, ui: &mut egui::Ui, options: Self::Attributes, context: &Context) -> bool {
        let ui_ctx = match context.ui_ctx {
            Some(ctx) => ctx,
            None => {
                ui.label(
                    "Need `context.ui_ctx` for the `InNewWindow<T>` inspectable implementation",
                );
                return false;
            }
        };
        ui.label("<shown in another window>");

        let window_title = options
            .title
            .map(|title| title.to_string())
            .unwrap_or_else(pretty_type_name::<T>);

        let mut changed = false;

        let id = Id::new(context.id()).with(context.id);
        egui::Window::new(window_title)
            .id(id)
            .resizable(options.resizable)
            .scroll(options.scrollable)
            .title_bar(options.title_bar)
            .collapsible(options.collapsible)
            .default_pos([400., 100.])
            .show(ui_ctx, |ui| {
                changed |=
                    <T as Inspectable>::ui(&mut self.0, ui, options.inner_attributes, context);
            });

        changed
    }
}