implement some components as traits

This commit is contained in:
yggverse 2025-01-31 22:02:53 +02:00
parent d1c05b6469
commit 1ad3e5006e
8 changed files with 70 additions and 79 deletions

View file

@ -57,9 +57,7 @@ impl Input {
title: Option<&str>,
size_limit: Option<usize>,
) {
self.update(Some(
&Response::build(action, base, title, size_limit).g_box,
));
self.update(Some(&gtk::Box::response(action, base, title, size_limit)));
}
pub fn set_new_sensitive(
@ -69,9 +67,7 @@ impl Input {
title: Option<&str>,
max_length: Option<i32>,
) {
self.update(Some(
&Sensitive::build(action, base, title, max_length).g_box,
));
self.update(Some(&gtk::Box::sensitive(action, base, title, max_length)));
}
pub fn set_new_titan(&self, on_send: impl Fn(&[u8], Box<dyn Fn()>) + 'static) {

View file

@ -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<ItemAction>,
base: Uri,
title: Option<&str>,
size_limit: Option<usize>,
) -> Self;
}
impl Response {
impl Response for Box {
// Constructors
/// Build new `Self`
pub fn build(
fn response(
item_action: Rc<ItemAction>,
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
}
}

View file

@ -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<Counter>,
pub send: Rc<Send>,
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 {

View file

@ -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<isize>);
}
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<isize>) {
fn update(&self, is_empty: bool, bytes_left: Option<isize>) {
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),
}
}
}

View file

@ -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);
}
}

View file

@ -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)
}
}

View file

@ -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()
}
}

View file

@ -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<ItemAction>,
base: Uri,
title: Option<&str>,
max_length: Option<i32>,
) -> Self;
}
impl Sensitive {
impl Sensitive for Box {
// Constructors
/// Build new `Self`
pub fn build(
fn sensitive(
item_action: Rc<ItemAction>,
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
}
}