added pages builder, to do universal traits for builders

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

33
Cargo.lock generated
View File

@ -488,9 +488,42 @@ version = "0.1.0"
dependencies = [ dependencies = [
"bitvec", "bitvec",
"gtk4", "gtk4",
"libadwaita",
"tokio", "tokio",
] ]
[[package]]
name = "libadwaita"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91b4990248b9e1ec5e72094a2ccaea70ec3809f88f6fd52192f2af306b87c5d9"
dependencies = [
"gdk-pixbuf",
"gdk4",
"gio",
"glib",
"gtk4",
"libadwaita-sys",
"libc",
"pango",
]
[[package]]
name = "libadwaita-sys"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23a748e4e92be1265cd9e93d569c0b5dfc7814107985aa6743d670ab281ea1a8"
dependencies = [
"gdk4-sys",
"gio-sys",
"glib-sys",
"gobject-sys",
"gtk4-sys",
"libc",
"pango-sys",
"system-deps",
]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.150" version = "0.2.150"

View File

@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
adw = { version = "0.6.0", package = "libadwaita", features = ["v1_4"] }
bitvec = "1.0.1" bitvec = "1.0.1"
gtk4 = "0.8.1" gtk4 = "0.8.1"
tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] } tokio = { version = "1.0.0", features = ["rt", "rt-multi-thread", "macros"] }

View File

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

View File

@ -1,3 +1,4 @@
pub mod wrapper;
pub mod switch;
pub mod tabs; 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 { pub fn get(self) -> Notebook {
self.tabs_wrapper self.tabs_wrapper
} }
} }
impl TabsBuilder { impl TabsBuilder {

View File

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