refactor: rewrite "dirty" code into normal

This commit is contained in:
doryan 2024-08-10 00:42:59 +04:00
parent 2ce75224fa
commit 656e75ff35

View File

@ -1,60 +1,32 @@
use gtk4 as gtk; use gtk4 as gtk;
use crate::{ use crate::{
controller::{ model::builder_traits::Product,
controller::*, view::{
event_handlers::{button_event_handlers::*, switch_event_handlers::*}, components::{info_bar::InfoBar, input::Input, wrapper::*},
view_utils::{hamming_code_input_utils::*, input_utils::*}, properties::*,
}, },
model::models::*, view_utils::{hamming_code_input_utils::start_hamming_algorithm, input_utils::clearing},
view::{components::wrapper::*, properties::*},
}; };
use gtk::{prelude::*, *}; use gtk::{glib::clone, prelude::*, *};
pub fn hamming_code_page(wrapper: &Box) { pub fn hamming_code_page(wrapper: &Box) {
// input let info_bar = InfoBar::get_instance();
let hamming_text_view_input_label = Label::builder() let input_code = Input::builder()
.halign(Align::Start) .label("Поле ввода для кода:")
.set_margin(MarginData::MultipleMargin((10, 5, 0, 0))) .margins(MarginData::EqualsMargin(6))
.label(String::from("Поле ввода для кода:")) .align(Alignment::new(Align::Start, Align::Start))
.build(); .build(true, WrapMode::Word, 64);
let hamming_text_view_input = TextView::builder() let output_code = Input::builder()
.monospace(true) .label("Результат:")
.set_text_view_margin(MarginData::EqualsMargin(6)) .margins(MarginData::EqualsMargin(6))
.wrap_mode(WrapMode::Word) .align(Alignment::new(Align::Start, Align::Start))
.build(); .build(true, WrapMode::Word, 64);
let hamming_text_view_input_frame = Frame::builder() output_code.get_input().set_editable(false);
.child(&hamming_text_view_input)
.height_request(64)
.set_margin(MarginData::MultipleMargin((0, 5, 0, 5)))
.build();
// output
let hamming_text_view_output_label = Label::builder()
.halign(Align::Start)
.set_margin(MarginData::MultipleMargin((10, 5, 0, 0)))
.label(String::from("Результат:"))
.build();
let hamming_text_view_output = TextView::builder()
.monospace(true)
.editable(false)
.set_text_view_margin(MarginData::EqualsMargin(6))
.wrap_mode(WrapMode::Word)
.build();
let hamming_text_view_output_frame = Frame::builder()
.child(&hamming_text_view_output)
.height_request(64)
.set_margin(MarginData::MultipleMargin((0, 5, 0, 5)))
.build();
// interactive panel
let clear_input_button = Button::builder() let clear_input_button = Button::builder()
.set_align(Alignment::new(Align::Fill, Align::Fill)) .set_align(Alignment::new(Align::Fill, Align::Fill))
@ -70,67 +42,72 @@ pub fn hamming_code_page(wrapper: &Box) {
let crypt_mode_label = Label::builder().label("Режим: кодирование").build(); let crypt_mode_label = Label::builder().label("Режим: кодирование").build();
// references for binding actions
let clear_input_button_to_handle = clear_input_button.clone();
let crypt_mode_label_to_handle = crypt_mode_label.clone();
let crypt_mode_switch_to_handle = crypt_mode_switch.clone();
let text_view_input_for_parse = hamming_text_view_input.clone();
let text_view_output_for_output = hamming_text_view_output.clone();
let text_view_input_for_clearing = hamming_text_view_input.clone();
let text_view_output_for_clearing = hamming_text_view_output.clone();
// actions
EventHandler::new(clear_input_button_to_handle, move |_| {
clearing(
&text_view_input_for_clearing,
&text_view_output_for_clearing,
);
})
.on_click();
EventHandler::new(hamming_crypt_button.clone(), move |_button: &Button| {
start_hamming_algorithm(
&text_view_input_for_parse,
&text_view_output_for_output,
crypt_mode_switch_to_handle.state(),
)
})
.on_click();
EventHandler::new(crypt_mode_switch.clone(), move |s: &Switch| {
state_controller(s, &crypt_mode_label_to_handle);
})
.on_toggle();
// wrappers
let crypt_mode_wrapper = Wrapper::col_builder() let crypt_mode_wrapper = Wrapper::col_builder()
.set_align(Alignment::new(Align::Fill, Align::Center)) .set_align(Alignment::new(Align::Fill, Align::Center))
.hexpand(true) .hexpand(true)
.spacing(10) .spacing(10)
.build(); .build();
let interactive_components_wrapper = Wrapper::col_builder() let action_components_wrapper = Wrapper::col_builder()
.set_align(Alignment::new(Align::Fill, Align::Fill)) .set_align(Alignment::new(Align::Fill, Align::Fill))
.set_margin(MarginData::MultipleMargin((0, 5, 0, 5))) .set_margin(MarginData::MultipleMargin((0, 5, 0, 5)))
.spacing(10) .spacing(10)
.build(); .build();
crypt_mode_switch.connect_state_set(clone!(
#[strong]
crypt_mode_label,
move |_, toggled| {
if toggled {
crypt_mode_label.set_label("Режим: проверка");
} else {
crypt_mode_label.set_label("Режим: кодирование");
}
glib::Propagation::Proceed
}
));
clear_input_button.connect_clicked(clone!(
#[strong]
input_code,
#[strong]
output_code,
move |_| {
clearing(input_code.get_input(), output_code.get_input());
}
));
hamming_crypt_button.connect_clicked(clone!(
#[strong]
input_code,
#[strong]
output_code,
#[strong]
crypt_mode_switch,
#[strong]
info_bar,
move |_| {
let result = start_hamming_algorithm(input_code.get_input(), crypt_mode_switch.state());
match result {
Ok(result) => {
output_code.get_input().buffer().set_text(result.trim_end());
}
Err(reject) => {
info_bar.set_text_label(Some(&*format!("{reject}")));
info_bar.show_infobar(5);
}
}
}
));
crypt_mode_wrapper.append(&crypt_mode_switch); crypt_mode_wrapper.append(&crypt_mode_switch);
crypt_mode_wrapper.append(&crypt_mode_label); crypt_mode_wrapper.append(&crypt_mode_label);
interactive_components_wrapper.append(&clear_input_button); action_components_wrapper.append(&clear_input_button);
interactive_components_wrapper.append(&crypt_mode_wrapper); action_components_wrapper.append(&crypt_mode_wrapper);
interactive_components_wrapper.append(&hamming_crypt_button); action_components_wrapper.append(&hamming_crypt_button);
wrapper.append(&hamming_text_view_input_label); wrapper.append(input_code.get());
wrapper.append(&hamming_text_view_input_frame); wrapper.append(&action_components_wrapper);
wrapper.append(&interactive_components_wrapper); wrapper.append(output_code.get());
wrapper.append(&hamming_text_view_output_label);
wrapper.append(&hamming_text_view_output_frame);
} }