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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

use yew::prelude::*;

use super::style::LocalStyle;
use crate::renderer::*;
use crate::session::*;
use crate::utils::*;
use crate::*;

#[derive(Properties)]
pub struct RenderWarningProps {
    pub dimensions: Option<(usize, usize, Option<usize>, Option<usize>)>,
    pub renderer: Renderer,
    pub session: Session,
}

impl PartialEq for RenderWarningProps {
    fn eq(&self, other: &Self) -> bool {
        self.dimensions == other.dimensions
    }
}

pub enum RenderWarningMsg {
    DismissWarning,
}

pub struct RenderWarning {
    col_warn: Option<(usize, usize)>,
    row_warn: Option<(usize, usize)>,
}

impl RenderWarning {
    fn update_warnings(&mut self, ctx: &Context<Self>) {
        if let Some((num_cols, num_rows, max_cols, max_rows)) = ctx.props().dimensions {
            let count = num_cols * num_rows;
            if max_cols.map_or(false, |x| x < num_cols) {
                self.col_warn = Some((max_cols.unwrap(), num_cols));
            } else {
                self.col_warn = None;
            }

            if max_rows.map_or(false, |x| x < num_rows) {
                self.row_warn = Some((num_cols * max_rows.unwrap(), count));
            } else {
                self.row_warn = None;
            }
        } else {
            self.col_warn = None;
            self.row_warn = None;
        }
    }
}

impl Component for RenderWarning {
    type Message = RenderWarningMsg;
    type Properties = RenderWarningProps;

    fn create(ctx: &Context<Self>) -> Self {
        // enable_weak_link_test!(props, link);
        let mut elem = Self {
            col_warn: None,
            row_warn: None,
        };

        elem.update_warnings(ctx);
        elem
    }

    fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
        match msg {
            RenderWarningMsg::DismissWarning => {
                clone!(ctx.props().renderer, ctx.props().session);
                ApiFuture::spawn(async move {
                    renderer.disable_active_plugin_render_warning();
                    renderer.update(&session).await
                });
            },
        };
        true
    }

    fn changed(&mut self, ctx: &Context<Self>, _old: &Self::Properties) -> bool {
        self.update_warnings(ctx);
        true
    }

    fn view(&self, ctx: &Context<Self>) -> Html {
        if self.col_warn.is_some() || self.row_warn.is_some() {
            let warning = match (self.col_warn, self.row_warn) {
                (Some((x, y)), Some((a, b))) => html! {
                    <span style="white-space:nowrap">
                        { "Rendering" }
                        { render_pair(x, y) }
                        { "of columns and" }
                        { render_pair(a, b) }
                        { "of points." }
                    </span>
                },
                (Some((x, y)), None) => html! {
                    <span style="white-space:nowrap">
                        { "Rendering" }
                        { render_pair(x, y) }
                        { "of columns." }
                    </span>
                },
                (None, Some((x, y))) => html! {
                    <span style="white-space:nowrap">
                        { "Rendering" }
                        { render_pair(x, y) }
                        { "of points." }
                    </span>
                },
                _ => html! { <div /> },
            };

            let onclick = ctx.link().callback(|_| RenderWarningMsg::DismissWarning);

            html! {
                <>
                    <LocalStyle href={css!("render-warning")} />
                    <div
                        class="plugin_information plugin_information--warning"
                        id="plugin_information--size"
                    >
                        <span class="plugin_information__icon" />
                        <span class="plugin_information__text" id="plugin_information_count">
                            { warning }
                        </span>
                        <span class="plugin_information__actions">
                            <span class="plugin_information__action" onmousedown={onclick}>
                                { "Render all points" }
                            </span>
                        </span>
                    </div>
                </>
            }
        } else {
            html! {}
        }
    }
}

fn pretty_print_int(i: usize) -> String {
    let mut s = String::new();
    let i_str = i.to_string();
    let a = i_str.chars().rev().enumerate();
    for (idx, val) in a {
        if idx != 0 && idx % 3 == 0 {
            s.insert(0, ',');
        }
        s.insert(0, val);
    }
    s
}

fn render_pair(n: usize, d: usize) -> Html {
    let x = pretty_print_int(n);
    let y = pretty_print_int(d);
    let total = ((n as f64 / d as f64) * 100_f64).floor() as usize;
    html! {
        <span title={format!("${} / ${}", x, y)} class="plugin_information--overflow-hint">
            { "\u{00a0}" }
            <span class="plugin_information--overflow-hint-percent">{ format!("{}%", total) }</span>
            { "\u{00a0}" }
        </span>
    }
}