implemented trait-interface for builders

This commit is contained in:
doryan04 2024-03-17 00:21:20 +04:00
parent 1d62f3810c
commit 5bd783b75b
5 changed files with 111 additions and 78 deletions

View File

@ -0,0 +1,17 @@
pub trait Product<B, T> {
fn builder() -> B;
fn get(self) -> T;
}
pub trait Builder<T, I, P> {
fn build(&self, build_arg: P) -> T;
fn append_item(self, item: I) -> Self;
fn append_items(self, items: Vec<I>) -> Self;
}

View File

@ -2,3 +2,4 @@ pub mod tabs;
pub mod pages;
pub mod switch;
pub mod wrapper;
pub mod builder_pattern_traits;

View File

@ -1,26 +1,71 @@
use std::ops::Deref;
use gtk4 as gtk;
use gtk::{Stack, StackSidebar, Box};
use gtk4::{Orientation, StackSwitcher, StackTransitionType, Widget};
use gtk4::prelude::{BoxExt, IsA};
use gtk::{
Box,
Stack,
Widget,
Orientation,
StackSidebar,
StackSwitcher,
StackTransitionType,
prelude::{BoxExt, IsA},
};
use crate::view::components::builder_pattern_traits::{Builder, Product};
pub type Page<'a> = (&'a str, &'a str, &'a Box);
pub struct Pages{
all_pages: Box
wrapper: Box
}
pub struct PagesBuilder{
pages_content: Stack,
}
impl Pages {
pub fn builder() -> PagesBuilder{
impl Product<PagesBuilder, Box> for Pages {
fn builder() -> PagesBuilder {
PagesBuilder {
pages_content: Stack::new(),
}
}
pub fn get(self) -> Box {
self.all_pages
fn get(self) -> Box {
self.wrapper
}
}
impl Builder<Pages, Page<'_>, i32> for PagesBuilder{
fn build(&self, build_param: i32) -> Pages {
let stack_sidebar = StackSidebar::new();
let stack_switcher = StackSwitcher::new();
stack_sidebar.set_stack(&self.pages_content);
stack_switcher.set_stack(Some(&self.pages_content));
let wrapper = Box::new(Orientation::Horizontal, build_param);
wrapper.append(&stack_sidebar);
wrapper.append(&self.pages_content);
Pages{
wrapper
}
}
fn append_item(self, item: Page) -> Self {
self.append_page_private(item);
self
}
fn append_items(self, items: Vec<Page>) -> Self {
items.iter()
.for_each(|&item| {
self.append_page_private(item);
});
self
}
}
@ -28,11 +73,9 @@ impl PagesBuilder {
fn append_page_private(
&self,
page_name: &str,
page_title: &str,
content: &impl IsA<Widget>
item: Page
) {
self.pages_content.add_titled(content, Some(page_name), page_title);
self.pages_content.add_titled(item.2, Some(item.1), item.0);
}
pub fn set_transition(
@ -46,43 +89,4 @@ impl PagesBuilder {
self
}
pub fn add_page(
self,
page_name: &str,
page_title: &str,
content: &impl IsA<Widget>
) -> Self {
self.append_page_private(page_name, page_title, content);
self
}
pub fn add_pages(
self,
pages: Vec<(&str, &str, &impl IsA<Widget>)>,
) -> Self {
pages.iter()
.for_each(|(name, title, content)| {
self.append_page_private(*name, *title, *content);
});
self
}
pub fn build(&self, spacing: i32) -> Pages {
let stack_sidebar = StackSidebar::new();
let stack_switcher = StackSwitcher::new();
stack_sidebar.set_stack(&self.pages_content);
stack_switcher.set_stack(Some(&self.pages_content));
let wrapper = Box::new(Orientation::Horizontal, spacing);
wrapper.append(&stack_sidebar);
wrapper.append(&self.pages_content);
Pages{
all_pages: wrapper
}
}
}

View File

@ -1,9 +1,12 @@
use gtk4 as gtk;
use std::boxed::Box as BoxPtr;
use gtk::{Notebook, Label, Box};
use super::builder_pattern_traits::*;
pub type TabLabel = Label;
pub type TabContent = Box;
pub type TabPage = (BoxPtr<str>, TabContent);
#[derive(Clone)]
pub struct TabsBuilder {
@ -14,42 +17,25 @@ pub struct Tabs {
tabs_wrapper: Notebook
}
impl Tabs {
pub fn builder() -> TabsBuilder {
impl Product<TabsBuilder, Notebook> for Tabs {
fn builder() -> TabsBuilder {
TabsBuilder {
tabs: Vec::new(),
}
}
pub fn get(self) -> Notebook {
fn get(self) -> Notebook {
self.tabs_wrapper
}
}
impl TabsBuilder {
impl Builder<Tabs, TabPage, &str> for 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(&self, group_name: &str) -> Tabs {
fn build(&self, build_param: &str) -> Tabs {
let tabs_wrapper = Notebook::builder()
.group_name(group_name)
.group_name(build_param)
.build();
self.tabs
@ -63,4 +49,28 @@ impl TabsBuilder {
}
}
fn append_item(mut self, item: TabPage) -> Self {
self.append_tab_private(&item.0, item.1);
self
}
fn append_items(mut self, items: Vec<TabPage>) -> Self {
for item in items{
self.append_tab_private(&item.0, item.1);
}
self
}
}
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));
}
}

View File

@ -1,3 +1,4 @@
use crate::view::components::builder_pattern_traits::{Builder, Product};
use gtk4 as gtk;
use gtk::{*, prelude::*};
@ -174,7 +175,7 @@ pub fn ui(application: &adw::Application) {
let pages = Pages::builder()
.set_transition(SlideLeftRight, 200)
.add_pages(vec![
.append_items(vec![
("Код Хэмминга", "Код Хэмминга", &mutual_wrapper),
("Код Хафмана", "Код Хафмана", &second_wrapper)
])