added pages builder, to do universal traits for builders

This commit is contained in:
2024-03-12 23:49:20 +04:00
parent 4c230c090d
commit a9626cda4d
7 changed files with 137 additions and 12 deletions

View File

@@ -14,7 +14,7 @@ use view::view::*;
fn main() {
let app = Application::builder()
let app = adw::Application::builder()
.application_id("com.github.gtk-rs.examples.basic")
.build();

View File

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

View File

@@ -0,0 +1,88 @@
use gtk4 as gtk;
use gtk::{Stack, StackSidebar, Box};
use gtk4::{Orientation, StackSwitcher, StackTransitionType, Widget};
use gtk4::prelude::{BoxExt, IsA};
pub struct Pages{
all_pages: Box
}
pub struct PagesBuilder{
pages_content: Stack,
}
impl Pages {
pub fn builder() -> PagesBuilder{
PagesBuilder {
pages_content: Stack::new(),
}
}
pub fn get(self) -> Box {
self.all_pages
}
}
impl PagesBuilder {
fn append_page_private(
&mut self,
page_name: &str,
page_title: &str,
content: &impl IsA<Widget>
) {
self.pages_content.add_titled(content, Some(page_name), page_title);
}
pub fn set_transition(
mut self,
type_transition : StackTransitionType,
duration: u32
) -> Self {
self.pages_content.set_transition_type(type_transition);
self.pages_content.set_transition_duration(duration);
self
}
pub fn add_page(
mut 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(
mut 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

@@ -24,7 +24,6 @@ impl Tabs {
pub fn get(self) -> Notebook {
self.tabs_wrapper
}
}
impl TabsBuilder {

View File

@@ -1,6 +1,7 @@
use gtk4 as gtk;
use gtk::{*, prelude::*};
use gtk4::StackTransitionType::SlideLeftRight;
use crate::{
model::model::*,
@@ -22,6 +23,7 @@ use crate::{
},
}
};
use crate::view::components::pages::Pages;
pub fn laboratory_work_first_section(wrapper: &Box) -> (){
@@ -152,7 +154,7 @@ pub fn laboratory_work_first_section(wrapper: &Box) -> (){
}
pub fn ui(application: &Application) {
pub fn ui(application: &adw::Application) {
let mutual_wrapper = Wrapper::row_builder()
.set_align(Alignment::new(Align::Fill, Align::Fill))
@@ -170,12 +172,13 @@ pub fn ui(application: &Application) {
second_wrapper.append(&Label::new(Some("Код Хафмана")));
let notebook = Tabs::builder()
.add_tabs(
vec!["Код Хэмминга", "Код Хафмана"],
vec![mutual_wrapper, second_wrapper]
)
.build("Tabs")
let pages = Pages::builder()
.set_transition(SlideLeftRight, 200)
.add_pages(vec![
("Код Хэмминга", "Код Хэмминга", &mutual_wrapper),
("Код Хафмана", "Код Хафмана", &second_wrapper)
])
.build(5)
.get();
let window = ApplicationWindow::builder()
@@ -183,7 +186,7 @@ pub fn ui(application: &Application) {
.width_request(650)
.height_request(400)
.application(application)
.child(&notebook)
.child(&pages)
.build();
window.show();