diff --git a/src/app/browser/window/tab/item/page/input.rs b/src/app/browser/window/tab/item/page/input.rs index c30af99e..18be5baa 100644 --- a/src/app/browser/window/tab/item/page/input.rs +++ b/src/app/browser/window/tab/item/page/input.rs @@ -57,9 +57,7 @@ impl Input { title: Option<&str>, size_limit: Option, ) { - self.update(Some( - &Response::build(action, base, title, size_limit).g_box, - )); + self.update(Some(>k::Box::response(action, base, title, size_limit))); } pub fn set_new_sensitive( @@ -69,9 +67,7 @@ impl Input { title: Option<&str>, max_length: Option, ) { - self.update(Some( - &Sensitive::build(action, base, title, max_length).g_box, - )); + self.update(Some(>k::Box::sensitive(action, base, title, max_length))); } pub fn set_new_titan(&self, on_send: impl Fn(&[u8], Box) + 'static) { diff --git a/src/app/browser/window/tab/item/page/input/response.rs b/src/app/browser/window/tab/item/page/input/response.rs index 90d15964..1221f3ad 100644 --- a/src/app/browser/window/tab/item/page/input/response.rs +++ b/src/app/browser/window/tab/item/page/input/response.rs @@ -11,23 +11,27 @@ use gtk::{ gio::SimpleAction, glib::{uuid_string_random, Uri, UriHideFlags}, prelude::BoxExt, - Box, Orientation, + Box, Label, Orientation, TextView, }; use std::rc::Rc; const MARGIN: i32 = 6; const SPACING: i32 = 8; -pub struct Response { - // Components - pub g_box: Box, +pub trait Response { + fn response( + item_action: Rc, + base: Uri, + title: Option<&str>, + size_limit: Option, + ) -> Self; } -impl Response { +impl Response for Box { // Constructors /// Build new `Self` - pub fn build( + fn response( item_action: Rc, base: Uri, title: Option<&str>, @@ -39,8 +43,8 @@ impl Response { // Init components let control = Rc::new(Control::build(action_send.clone())); - let form = Rc::new(Form::build(action_update.clone())); - let title = Rc::new(Title::build(title)); + let form = TextView::form(action_update.clone()); + let title = Label::title(title); // Init main widget let g_box = Box::builder() @@ -52,8 +56,8 @@ impl Response { .orientation(Orientation::Vertical) .build(); - g_box.append(&title.label); - g_box.append(&form.text_view); + g_box.append(&title); + g_box.append(&form); g_box.append(&control.g_box); // Init events @@ -88,7 +92,7 @@ impl Response { } }); - // Return activated struct - Self { g_box } + // Return activated `Self` + g_box } } diff --git a/src/app/browser/window/tab/item/page/input/response/control.rs b/src/app/browser/window/tab/item/page/input/response/control.rs index a49587a7..8fa33af8 100644 --- a/src/app/browser/window/tab/item/page/input/response/control.rs +++ b/src/app/browser/window/tab/item/page/input/response/control.rs @@ -2,16 +2,14 @@ mod counter; mod send; use counter::Counter; +use gtk::{gio::SimpleAction, prelude::BoxExt, Align, Box, Button, Label, Orientation}; use send::Send; -use gtk::{gio::SimpleAction, prelude::BoxExt, Align, Box, Orientation}; -use std::rc::Rc; - const SPACING: i32 = 8; pub struct Control { - pub counter: Rc, - pub send: Rc, + pub counter: Label, + pub send: Button, pub g_box: Box, } @@ -21,8 +19,8 @@ impl Control { /// Build new `Self` pub fn build(action_send: SimpleAction) -> Self { // Init components - let counter = Rc::new(Counter::new()); - let send = Rc::new(Send::build(action_send)); + let counter = Label::counter(); + let send = Button::send(action_send); // Init main widget let g_box = Box::builder() @@ -31,8 +29,8 @@ impl Control { .spacing(SPACING) .build(); - g_box.append(&counter.label); - g_box.append(&send.button); + g_box.append(&counter); + g_box.append(&send); // Return activated struct Self { diff --git a/src/app/browser/window/tab/item/page/input/response/control/counter.rs b/src/app/browser/window/tab/item/page/input/response/control/counter.rs index 2f1cb4a9..1dd9b28e 100644 --- a/src/app/browser/window/tab/item/page/input/response/control/counter.rs +++ b/src/app/browser/window/tab/item/page/input/response/control/counter.rs @@ -1,47 +1,38 @@ use gtk::{prelude::WidgetExt, Label}; use plurify::Plurify; -pub struct Counter { - pub label: Label, +pub trait Counter { + fn counter() -> Self; + fn update(&self, is_empty: bool, bytes_left: Option); } -impl Default for Counter { - fn default() -> Self { - Self::new() - } -} - -impl Counter { - // Construct - pub fn new() -> Self { - Self { - label: Label::builder().build(), - } +impl Counter for Label { + fn counter() -> Self { + Label::builder().build() } - // Actions - pub fn update(&self, is_empty: bool, bytes_left: Option) { + fn update(&self, is_empty: bool, bytes_left: Option) { match bytes_left { Some(value) => { // Update color on chars left reached - self.label.set_css_classes(&[if value.is_positive() { + self.set_css_classes(&[if value.is_positive() { "success" } else { "error" }]); // @TODO add warning step? // Update text - self.label.set_label(&value.to_string()); + self.set_label(&value.to_string()); // Toggle visibility on chars left provided - self.label.set_visible(!is_empty); + self.set_visible(!is_empty); - self.label.set_tooltip_text(Some(&format!( + self.set_tooltip_text(Some(&format!( "{value} {} left", (value as usize).plurify(&["byte", "bytes", "bytes"]) ))); } - None => self.label.set_visible(false), + None => self.set_visible(false), } } } diff --git a/src/app/browser/window/tab/item/page/input/response/control/send.rs b/src/app/browser/window/tab/item/page/input/response/control/send.rs index 6eefdaa3..a8354701 100644 --- a/src/app/browser/window/tab/item/page/input/response/control/send.rs +++ b/src/app/browser/window/tab/item/page/input/response/control/send.rs @@ -4,15 +4,16 @@ use gtk::{ Button, }; -pub struct Send { - pub button: Button, +pub trait Send { + fn send(action_send: SimpleAction) -> Self; + fn update(&self, is_sensitive: bool); } -impl Send { +impl Send for Button { // Constructors /// Build new `Self` - pub fn build(action_send: SimpleAction) -> Self { + fn send(action_send: SimpleAction) -> Self { // Init main widget let button = Button::builder() .css_classes(["accent"]) // | `suggested-action` @@ -30,11 +31,11 @@ impl Send { }); // Return activated `Self` - Self { button } + button } // Actions - pub fn update(&self, is_sensitive: bool) { - self.button.set_sensitive(is_sensitive); + fn update(&self, is_sensitive: bool) { + self.set_sensitive(is_sensitive); } } diff --git a/src/app/browser/window/tab/item/page/input/response/form.rs b/src/app/browser/window/tab/item/page/input/response/form.rs index b5ed8d73..25d6b178 100644 --- a/src/app/browser/window/tab/item/page/input/response/form.rs +++ b/src/app/browser/window/tab/item/page/input/response/form.rs @@ -9,15 +9,16 @@ use sourceview::Buffer; const MARGIN: i32 = 8; -pub struct Form { - pub text_view: TextView, +pub trait Form { + fn form(action_update: SimpleAction) -> Self; + fn text(&self) -> GString; } -impl Form { +impl Form for TextView { // Constructors /// Build new `Self` - pub fn build(action_update: SimpleAction) -> Self { + fn form(action_update: SimpleAction) -> Self { // Init [SourceView](https://gitlab.gnome.org/GNOME/gtksourceview) type buffer let buffer = Buffer::builder().build(); @@ -52,13 +53,13 @@ impl Form { }); // Return activated `Self` - Self { text_view } + text_view } // Getters - pub fn text(&self) -> GString { - let buffer = self.text_view.buffer(); + fn text(&self) -> GString { + let buffer = self.buffer(); buffer.text(&buffer.start_iter(), &buffer.end_iter(), true) } } diff --git a/src/app/browser/window/tab/item/page/input/response/title.rs b/src/app/browser/window/tab/item/page/input/response/title.rs index c5c91a62..417d3121 100644 --- a/src/app/browser/window/tab/item/page/input/response/title.rs +++ b/src/app/browser/window/tab/item/page/input/response/title.rs @@ -1,20 +1,15 @@ use gtk::{Align, Label}; -pub struct Title { - pub label: Label, +pub trait Title { + fn title(title: Option<&str>) -> Self; } -impl Title { - // Constructors - - /// Build new `Self` - pub fn build(title: Option<&str>) -> Self { - let label = Label::builder() +impl Title for Label { + fn title(title: Option<&str>) -> Self { + Label::builder() .css_classes(["heading"]) .halign(Align::Start) .label(title.unwrap_or("Input expected")) - .build(); - - Self { label } + .build() } } diff --git a/src/app/browser/window/tab/item/page/input/sensitive.rs b/src/app/browser/window/tab/item/page/input/sensitive.rs index c6cc9b36..58dca2e4 100644 --- a/src/app/browser/window/tab/item/page/input/sensitive.rs +++ b/src/app/browser/window/tab/item/page/input/sensitive.rs @@ -13,15 +13,20 @@ use std::rc::Rc; const MARGIN: i32 = 6; const SPACING: i32 = 8; -pub struct Sensitive { - pub g_box: Box, +pub trait Sensitive { + fn sensitive( + item_action: Rc, + base: Uri, + title: Option<&str>, + max_length: Option, + ) -> Self; } -impl Sensitive { +impl Sensitive for Box { // Constructors /// Build new `Self` - pub fn build( + fn sensitive( item_action: Rc, base: Uri, title: Option<&str>, @@ -69,7 +74,7 @@ impl Sensitive { form.password_entry_row.grab_focus(); }); - // Return activated struct - Self { g_box } + // Return activated `Self` + g_box } }