Fully refactored project architecture, imports and etc.
This commit is contained in:
3
src/view/components/mod.rs
Normal file
3
src/view/components/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod wrapper;
|
||||
pub mod switch;
|
||||
pub mod tabs;
|
||||
153
src/view/components/switch.rs
Normal file
153
src/view/components/switch.rs
Normal file
@@ -0,0 +1,153 @@
|
||||
pub mod switch_module {
|
||||
|
||||
use gtk4 as gtk;
|
||||
|
||||
use gtk::{*, prelude::*};
|
||||
|
||||
use glib::{
|
||||
signal::{connect_raw, SignalHandlerId},
|
||||
translate::*,
|
||||
};
|
||||
|
||||
use std::boxed::Box as Box_;
|
||||
|
||||
mod sealed {
|
||||
pub trait Sealed {}
|
||||
impl<T: super::IsA<super::Switch>> Sealed for T {}
|
||||
}
|
||||
|
||||
pub trait SwitchExt: IsA<Switch> + sealed::Sealed + 'static {
|
||||
#[doc(alias = "gtk_switch_get_active")]
|
||||
#[doc(alias = "get_active")]
|
||||
fn is_active(&self) -> bool {
|
||||
unsafe { from_glib(ffi::gtk_switch_get_active(self.as_ref().to_glib_none().0)) }
|
||||
}
|
||||
|
||||
#[doc(alias = "gtk_switch_get_state")]
|
||||
#[doc(alias = "get_state")]
|
||||
fn state(&self) -> bool {
|
||||
unsafe { from_glib(ffi::gtk_switch_get_state(self.as_ref().to_glib_none().0)) }
|
||||
}
|
||||
|
||||
#[doc(alias = "gtk_switch_set_active")]
|
||||
fn set_active(&self, is_active: bool) {
|
||||
unsafe {
|
||||
ffi::gtk_switch_set_active(self.as_ref().to_glib_none().0, is_active.into_glib());
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(alias = "gtk_switch_set_state")]
|
||||
fn set_state(&self, state: bool) {
|
||||
unsafe {
|
||||
ffi::gtk_switch_set_state(self.as_ref().to_glib_none().0, state.into_glib());
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(alias = "activate")]
|
||||
fn connect_activate<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
|
||||
unsafe extern "C" fn activate_trampoline<P: IsA<Switch>, F: Fn(&P) + 'static>(
|
||||
this: *mut ffi::GtkSwitch,
|
||||
f: glib::ffi::gpointer,
|
||||
) {
|
||||
let f: &F = &*(f as *const F);
|
||||
f(Switch::from_glib_borrow(this).unsafe_cast_ref())
|
||||
}
|
||||
unsafe {
|
||||
let f: Box_<F> = Box_::new(f);
|
||||
connect_raw(
|
||||
self.as_ptr() as *mut _,
|
||||
b"activate\0".as_ptr() as *const _,
|
||||
Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
|
||||
activate_trampoline::<Self, F> as *const (),
|
||||
)),
|
||||
Box_::into_raw(f),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn emit_activate(&self) {
|
||||
self.emit_by_name::<()>("activate", &[]);
|
||||
}
|
||||
|
||||
#[doc(alias = "state-set")]
|
||||
fn connect_state_set<F: Fn(&Self, bool) -> glib::Propagation + 'static>(
|
||||
&self,
|
||||
f: F,
|
||||
) -> SignalHandlerId {
|
||||
unsafe extern "C" fn state_set_trampoline<
|
||||
P: IsA<Switch>,
|
||||
F: Fn(&P, bool) -> glib::Propagation + 'static,
|
||||
>(
|
||||
this: *mut ffi::GtkSwitch,
|
||||
state: glib::ffi::gboolean,
|
||||
f: glib::ffi::gpointer,
|
||||
) -> glib::ffi::gboolean {
|
||||
let f: &F = &*(f as *const F);
|
||||
f(
|
||||
Switch::from_glib_borrow(this).unsafe_cast_ref(),
|
||||
from_glib(state),
|
||||
)
|
||||
.into_glib()
|
||||
}
|
||||
unsafe {
|
||||
let f: Box_<F> = Box_::new(f);
|
||||
connect_raw(
|
||||
self.as_ptr() as *mut _,
|
||||
b"state-set\0".as_ptr() as *const _,
|
||||
Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
|
||||
state_set_trampoline::<Self, F> as *const (),
|
||||
)),
|
||||
Box_::into_raw(f),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(alias = "active")]
|
||||
fn connect_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
|
||||
unsafe extern "C" fn notify_active_trampoline<P: IsA<Switch>, F: Fn(&P) + 'static>(
|
||||
this: *mut ffi::GtkSwitch,
|
||||
_param_spec: glib::ffi::gpointer,
|
||||
f: glib::ffi::gpointer,
|
||||
) {
|
||||
let f: &F = &*(f as *const F);
|
||||
f(Switch::from_glib_borrow(this).unsafe_cast_ref())
|
||||
}
|
||||
unsafe {
|
||||
let f: Box_<F> = Box_::new(f);
|
||||
connect_raw(
|
||||
self.as_ptr() as *mut _,
|
||||
b"notify::active\0".as_ptr() as *const _,
|
||||
Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
|
||||
notify_active_trampoline::<Self, F> as *const (),
|
||||
)),
|
||||
Box_::into_raw(f),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(alias = "state")]
|
||||
fn connect_state_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
|
||||
unsafe extern "C" fn notify_state_trampoline<P: IsA<Switch>, F: Fn(&P) + 'static>(
|
||||
this: *mut ffi::GtkSwitch,
|
||||
_param_spec: glib::ffi::gpointer,
|
||||
f: glib::ffi::gpointer,
|
||||
) {
|
||||
let f: &F = &*(f as *const F);
|
||||
f(Switch::from_glib_borrow(this).unsafe_cast_ref())
|
||||
}
|
||||
unsafe {
|
||||
let f: Box_<F> = Box_::new(f);
|
||||
connect_raw(
|
||||
self.as_ptr() as *mut _,
|
||||
b"notify::state\0".as_ptr() as *const _,
|
||||
Some(std::mem::transmute::<_, unsafe extern "C" fn()>(
|
||||
notify_state_trampoline::<Self, F> as *const (),
|
||||
)),
|
||||
Box_::into_raw(f),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<O: IsA<Switch>> SwitchExt for O {}
|
||||
}
|
||||
71
src/view/components/tabs.rs
Normal file
71
src/view/components/tabs.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
pub mod tabs_module {
|
||||
|
||||
use gtk4 as gtk;
|
||||
|
||||
use gtk::{Notebook, Label, Box};
|
||||
|
||||
pub type TabLabel = Label;
|
||||
pub type TabContent = Box;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct TabsBuilder {
|
||||
tabs: Vec<(TabLabel, TabContent)>
|
||||
}
|
||||
|
||||
pub struct Tabs {
|
||||
tabs_wrapper: Notebook
|
||||
}
|
||||
|
||||
impl Tabs {
|
||||
pub fn builder() -> TabsBuilder {
|
||||
TabsBuilder{
|
||||
tabs: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(self) -> Notebook {
|
||||
self.tabs_wrapper
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl TabsBuilder {
|
||||
|
||||
fn append_tab_private(&mut self, label: &str, page: TabContent) {
|
||||
let tab_label = Label::new(Some(label));
|
||||
self.tabs.push((tab_label, page));
|
||||
}
|
||||
|
||||
pub fn add_tab(mut self, label: &str, page: TabContent) -> Self {
|
||||
self.append_tab_private(label, page);
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_tabs(mut self, labels: Vec<&str>, pages: Vec<TabContent>) -> Self {
|
||||
for (label, page) in labels.iter().zip(pages) {
|
||||
self.append_tab_private(label, page);
|
||||
}
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(&mut self, group_name: &str) -> Tabs {
|
||||
let tabs_wrapper = Notebook::builder()
|
||||
.group_name(group_name)
|
||||
.build();
|
||||
|
||||
self.tabs
|
||||
.iter()
|
||||
.for_each(|(label, content)| {
|
||||
tabs_wrapper.append_page(content, Some(label));
|
||||
});
|
||||
|
||||
Tabs {
|
||||
tabs_wrapper
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
23
src/view/components/wrapper.rs
Normal file
23
src/view/components/wrapper.rs
Normal file
@@ -0,0 +1,23 @@
|
||||
pub mod wrapper_module {
|
||||
|
||||
use gtk4 as gtk;
|
||||
|
||||
use gtk::{Orientation, builders::BoxBuilder, Box};
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct Wrapper;
|
||||
|
||||
impl Wrapper{
|
||||
|
||||
pub fn row_builder() -> BoxBuilder {
|
||||
Box::builder().orientation(Orientation::Vertical)
|
||||
}
|
||||
|
||||
pub fn col_builder() -> BoxBuilder {
|
||||
Box::builder().orientation(Orientation::Horizontal)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
3
src/view/mod.rs
Normal file
3
src/view/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
pub mod view;
|
||||
pub mod components;
|
||||
pub mod properties;
|
||||
120
src/view/properties.rs
Normal file
120
src/view/properties.rs
Normal file
@@ -0,0 +1,120 @@
|
||||
pub mod properties_module{
|
||||
use gtk4 as gtk;
|
||||
|
||||
use gtk::{Align};
|
||||
use gtk::builders::*;
|
||||
|
||||
/**
|
||||
* Types
|
||||
*/
|
||||
|
||||
pub type Margin = (i32, i32, i32, i32);
|
||||
|
||||
/**
|
||||
* Enums
|
||||
*/
|
||||
|
||||
pub enum MarginData{
|
||||
EqualsMargin(i32),
|
||||
MultipleMargin(Margin),
|
||||
}
|
||||
|
||||
/**
|
||||
* Structs
|
||||
*/
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
||||
pub struct Size {
|
||||
pub width: i32,
|
||||
pub height: i32
|
||||
}
|
||||
|
||||
pub struct Alignment {
|
||||
pub horizontal: Align,
|
||||
pub vertical : Align
|
||||
}
|
||||
|
||||
/**
|
||||
* Traits
|
||||
*/
|
||||
|
||||
pub trait Setters{
|
||||
fn set_margin(self, margin: MarginData) -> Self;
|
||||
fn set_align(self, align: Alignment) -> Self;
|
||||
}
|
||||
|
||||
pub trait TextViewSetters{
|
||||
fn set_text_view_margin(self, margin: MarginData) -> Self;
|
||||
}
|
||||
|
||||
impl TextViewSetters for TextViewBuilder{
|
||||
fn set_text_view_margin(self, margin: MarginData) -> Self{
|
||||
match margin{
|
||||
MarginData::EqualsMargin(margin) =>
|
||||
self.top_margin(margin)
|
||||
.left_margin(margin)
|
||||
.bottom_margin(margin)
|
||||
.right_margin(margin),
|
||||
MarginData::MultipleMargin(margins) =>
|
||||
self.top_margin(margins.0)
|
||||
.left_margin(margins.1)
|
||||
.bottom_margin(margins.2)
|
||||
.right_margin(margins.3),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Macros
|
||||
*/
|
||||
|
||||
macro_rules! impl_setters {
|
||||
($($t:ty),+) => {
|
||||
$(
|
||||
impl Setters for $t {
|
||||
fn set_margin(self, margin: MarginData) -> Self{
|
||||
match margin{
|
||||
MarginData::EqualsMargin(margin) =>
|
||||
self.margin_top(margin)
|
||||
.margin_start(margin)
|
||||
.margin_bottom(margin)
|
||||
.margin_end(margin),
|
||||
MarginData::MultipleMargin(margins) =>
|
||||
self.margin_top(margins.0)
|
||||
.margin_start(margins.1)
|
||||
.margin_bottom(margins.2)
|
||||
.margin_end(margins.3),
|
||||
}
|
||||
}
|
||||
fn set_align(self, align: Alignment) -> Self {
|
||||
self.halign(align.horizontal)
|
||||
.valign(align.vertical)
|
||||
}
|
||||
}
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
||||
impl_setters!{ButtonBuilder, EntryBuilder, TextViewBuilder,
|
||||
BoxBuilder, SwitchBuilder, FrameBuilder, LabelBuilder}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl Size{
|
||||
pub fn new(w: i32, h: i32) -> Size{
|
||||
Size{
|
||||
width: w,
|
||||
height: h,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Alignment{
|
||||
pub fn new(horizontal: Align, vertical : Align) -> Alignment{
|
||||
Alignment{
|
||||
horizontal,
|
||||
vertical,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
195
src/view/view.rs
Normal file
195
src/view/view.rs
Normal file
@@ -0,0 +1,195 @@
|
||||
pub mod view_module{
|
||||
|
||||
use gtk4 as gtk;
|
||||
|
||||
use gtk::{*, prelude::*};
|
||||
|
||||
use crate::{
|
||||
model::model::model_module::*,
|
||||
view::{
|
||||
properties::properties_module::*,
|
||||
components::{
|
||||
*,
|
||||
tabs::tabs_module::*,
|
||||
switch::switch_module::*,
|
||||
wrapper::wrapper_module::*,
|
||||
}
|
||||
},
|
||||
controller::{
|
||||
controller::*,
|
||||
view_utils::input_utils::input_utils_module::*,
|
||||
event_handlers::{
|
||||
button_event_handlers::button_event_handlers_module::*,
|
||||
switch_event_handlers::switch_event_handlers_module::*,
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
pub fn laboratory_work_first_section(wrapper: &Box) -> (){
|
||||
|
||||
// input
|
||||
|
||||
let hamming_text_view_input_label = Label::builder()
|
||||
.halign(Align::Start)
|
||||
.set_margin(MarginData::MultipleMargin((10, 5, 0, 0)))
|
||||
.label(String::from("Поле ввода для кода:"))
|
||||
.build();
|
||||
|
||||
let hamming_text_view_input = TextView::builder()
|
||||
.monospace(true)
|
||||
.set_text_view_margin(MarginData::EqualsMargin(6))
|
||||
.wrap_mode(WrapMode::Word)
|
||||
.build();
|
||||
|
||||
let hamming_text_view_input_frame = Frame::builder()
|
||||
.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()
|
||||
.set_align(Alignment::new(Align::Fill, Align::Fill))
|
||||
.label("Очистка полей")
|
||||
.build();
|
||||
|
||||
let hamming_crypt_button = Button::builder()
|
||||
.set_align(Alignment::new(Align::Fill, Align::Fill))
|
||||
.label("Выполнить")
|
||||
.build();
|
||||
|
||||
let crypt_mode_switch = Switch::new();
|
||||
|
||||
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_input.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| {
|
||||
parse_input(
|
||||
&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()
|
||||
.set_align(Alignment::new(Align::Fill, Align::Center))
|
||||
.hexpand(true)
|
||||
.spacing(10)
|
||||
.build();
|
||||
|
||||
let interactive_components_wrapper = Wrapper::col_builder()
|
||||
.set_align(Alignment::new(Align::Fill, Align::Fill))
|
||||
.set_margin(MarginData::MultipleMargin((0, 5, 0, 5)))
|
||||
.spacing(10)
|
||||
.build();
|
||||
|
||||
crypt_mode_wrapper.append(&crypt_mode_switch);
|
||||
crypt_mode_wrapper.append(&crypt_mode_label);
|
||||
|
||||
interactive_components_wrapper.append(&clear_input_button);
|
||||
interactive_components_wrapper.append(&crypt_mode_wrapper);
|
||||
interactive_components_wrapper.append(&hamming_crypt_button);
|
||||
|
||||
wrapper.append(&hamming_text_view_input_label);
|
||||
wrapper.append(&hamming_text_view_input_frame);
|
||||
wrapper.append(&interactive_components_wrapper);
|
||||
wrapper.append(&hamming_text_view_output_label);
|
||||
wrapper.append(&hamming_text_view_output_frame);
|
||||
|
||||
}
|
||||
|
||||
pub fn ui(application: &Application) {
|
||||
|
||||
let mutual_wrapper = Wrapper::row_builder()
|
||||
.set_align(Alignment::new(Align::Fill, Align::Fill))
|
||||
.set_margin(MarginData::EqualsMargin(15))
|
||||
.spacing(10)
|
||||
.build();
|
||||
|
||||
laboratory_work_first_section(&mutual_wrapper);
|
||||
|
||||
let second_wrapper = Wrapper::row_builder()
|
||||
.set_align(Alignment::new(Align::Fill, Align::Fill))
|
||||
.set_margin(MarginData::EqualsMargin(15))
|
||||
.spacing(10)
|
||||
.build();
|
||||
|
||||
second_wrapper.append(&Label::new(Some("Код Хафмана")));
|
||||
|
||||
let notebook = Tabs::builder()
|
||||
.add_tabs(
|
||||
vec!["Код Хэмминга", "Код Хафмана"],
|
||||
vec![mutual_wrapper, second_wrapper]
|
||||
)
|
||||
.build("Tabs")
|
||||
.get();
|
||||
|
||||
let window = ApplicationWindow::builder()
|
||||
.title("Комплексная программа для лаб. работ")
|
||||
.width_request(650)
|
||||
.height_request(400)
|
||||
.application(application)
|
||||
.child(¬ebook)
|
||||
.build();
|
||||
|
||||
window.show();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user