install_framework_gui/renderer/
processing.rs

1// Copyright 2021 Yuri6037
2
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19// IN THE SOFTWARE.
20
21use std::string::String;
22use async_channel::Sender;
23use async_channel::SendError;
24use iced::Element;
25use iced::Row;
26use iced::Column;
27use iced::Text;
28use iced::Align;
29use iced::button;
30use iced::ProgressBar;
31use iced::text_input;
32
33use super::window::Page;
34use super::window::ConstructiblePage;
35use super::window::PageMessage;
36use crate::messages::RenderMessage;
37use crate::messages::ThreadMessage;
38use crate::messages;
39use crate::ext::SenderSync;
40
41pub struct Progress
42{
43    message: String,
44    value: f32
45}
46
47pub struct ProcessingPage
48{
49    step: Option<Progress>,
50    substep: Option<Progress>,
51    user_input_msg: Option<String>,
52    user_input_text: String,
53    useless: button::State,
54    useless1: text_input::State
55}
56
57impl ConstructiblePage for ProcessingPage
58{
59    type PageType = ProcessingPage;
60
61    fn new(msg: &messages::Page) -> ProcessingPage
62    {
63        if let messages::Page::Processing = msg
64        {
65            return ProcessingPage
66            {
67                step: None,
68                substep: None,
69                user_input_msg: None,
70                user_input_text: String::new(),
71                useless: button::State::new(),
72                useless1: text_input::State::new()
73            };
74        }
75        panic!("The impossible happened: incorrect message received");
76    }
77}
78
79impl Page for ProcessingPage
80{
81    fn handle_message(&mut self, msg: &RenderMessage, _: &mut Sender<ThreadMessage>) -> Result<(), SendError<ThreadMessage>>
82    {
83        match msg
84        {
85            RenderMessage::UserInput(s) => self.user_input_msg = Some(s.clone()),
86            RenderMessage::BeginStep(s) =>
87            {
88                self.step = Some(Progress
89                {
90                    message: s.clone(),
91                    value: 0.0
92                });
93            },
94            RenderMessage::BeginSubstep(s) =>
95            {
96                self.substep = Some(Progress
97                {
98                    message: s.clone(),
99                    value: 0.0
100                });
101            },
102            RenderMessage::UpdateSubstep(v) =>
103            {
104                if let Some(progress) = &mut self.substep
105                {
106                    progress.value = *v;
107                }
108            },
109            RenderMessage::UpdateStep(v) =>
110            {
111                if let Some(progress) = &mut self.step
112                {
113                    progress.value = *v;
114                }
115            },
116            RenderMessage::EndSubstep => self.substep = None,
117            _ => ()
118        }
119        return Ok(());
120    }
121
122    fn update(&mut self, msg: &PageMessage, sender: &mut Sender<ThreadMessage>) -> Result<bool, SendError<ThreadMessage>>
123    {
124        match msg
125        {
126            PageMessage::Next =>
127            {
128                self.user_input_msg = None;
129                let s = std::mem::replace(&mut self.user_input_text, String::new());
130                sender.send_sync(ThreadMessage::UserInput(s))?;
131            },
132            PageMessage::TextChanged(s) => self.user_input_text = s.clone(),
133            _ => ()
134        }
135        return Ok(false);
136    }
137
138    fn view(&mut self) -> Element<PageMessage>
139    {
140        let mut col = Column::new().spacing(5);
141        if let Some(progress) = &self.step
142        {
143            col = col.push(Text::new(&progress.message));
144            col = col.push(
145                Row::new()
146                    .align_items(Align::Center)
147                    .push(ProgressBar::new(0.0..=1.0, progress.value))
148                    .push(Text::new(format!("({:.2}%)", progress.value * 100.0)))
149            );
150            col = col.push(Text::new(" "));
151        }
152        if let Some(progress) = &self.substep
153        {
154            col = col.push(Text::new(&progress.message));
155            col = col.push(
156                Row::new()
157                    .align_items(Align::Center)
158                    .push(ProgressBar::new(0.0..=1.0, progress.value))
159                    .push(Text::new(format!("({:.2}%)", progress.value * 100.0)))
160            );
161            col = col.push(Text::new(" "));
162        }
163        if let Some(msg) = &self.user_input_msg
164        {
165            col = col.push(Text::new(msg));
166            col = col.push(
167                Row::new()
168                    .align_items(Align::Center)
169                    .push(text_input::TextInput::new(&mut self.useless1, "", &self.user_input_text, PageMessage::TextChanged))
170                    .push(button::Button::new(&mut self.useless, Text::new("OK")).on_press(PageMessage::Next))
171            );
172        }
173        return col.into();
174    }
175}