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
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use crate::{
    types::{Rect, Vector2},
    ui::WindowContext,
    Id, Ui,
};

#[derive(Debug, Clone)]
pub struct Window {
    id: Id,
    position: Vector2,
    size: Vector2,
    close_button: bool,
    enabled: bool,
    force_focus: bool,
    label: Option<String>,
}

impl Window {
    pub fn new(id: Id, position: Vector2, size: Vector2) -> Window {
        Window {
            id,
            position,
            size,
            close_button: false,
            enabled: true,
            force_focus: false,
            label: None,
        }
    }

    pub fn label(self, label: &str) -> Window {
        Window {
            label: Some(label.to_string()),
            ..self
        }
    }

    pub fn close_button(self, close_button: bool) -> Window {
        Window {
            close_button,
            ..self
        }
    }

    pub fn enabled(self, enabled: bool) -> Window {
        Window { enabled, ..self }
    }

    pub fn force_focus(self, force_focus: bool) -> Window {
        Window {
            force_focus,
            ..self
        }
    }

    pub fn ui<F: FnOnce(&mut Ui)>(self, ui: &mut Ui, f: F) -> bool {
        let mut context = ui.begin_window(self.id, None, self.position, self.size);

        self.draw_window_frame(&mut context);
        if self.close_button && self.draw_close_button(&mut context) {
            context.close();
        }

        let clip_rect = context.window.content_rect();
        context.window.draw_list.clip(clip_rect);
        context.scroll_area();

        f(ui);

        let context = ui.get_active_window_context();
        context.window.draw_list.clip(None);

        let opened = context.window.want_close == false;

        ui.end_window();

        opened
    }

    fn draw_close_button(&self, context: &mut WindowContext) -> bool {
        let button_rect = Rect::new(
            context.window.position.x + context.window.size.x - 15.,
            context.window.position.y,
            20.,
            20.,
        );
        context.window.draw_list.draw_label(
            "X",
            Vector2::new(
                context.window.position.x + context.window.size.x - 10.,
                context.window.position.y + 3.,
            ),
            Some(context.global_style.title(context.focused)),
        );
        context.focused
            && button_rect.contains(context.input.mouse_position)
            && context.input.click_up
    }

    fn draw_window_frame(&self, context: &mut WindowContext) {
        let focused = context.focused;
        let style = context.global_style;
        let position = context.window.position;
        let size = context.window.size;

        context.window.draw_list.draw_rect(
            Rect::new(position.x, position.y, size.x, size.y),
            style.window_border(focused),
            style.background(focused),
        );

        if let Some(label) = &self.label {
            context.window.draw_list.draw_label(
                &label,
                Vector2::new(position.x + style.margin, position.y + style.margin),
                context.global_style.title(focused),
            );
        }
        context.window.draw_list.draw_line(
            Vector2::new(position.x, position.y + style.title_height),
            Vector2::new(position.x + size.x, position.y + style.title_height),
            style.window_border(focused),
        );
    }
}

impl Ui {
    pub fn window<F: FnOnce(&mut Ui)>(
        &mut self,
        id: Id,
        position: Vector2,
        size: Vector2,
        f: F,
    ) -> bool {
        Window::new(id, position, size).ui(self, f)
    }
}