rename components

This commit is contained in:
yggverse 2024-10-17 05:06:22 +03:00
parent cf86cc2a14
commit 06f2e0e499
14 changed files with 18 additions and 18 deletions

View file

@ -0,0 +1,50 @@
mod counter;
mod send;
mod widget;
use counter::Counter;
use send::Send;
use widget::Widget;
use gtk::{gio::SimpleAction, Box};
use std::sync::Arc;
pub struct Control {
counter: Arc<Counter>,
send: Arc<Send>,
widget: Arc<Widget>,
}
impl Control {
// Construct
pub fn new_arc(action_send: Arc<SimpleAction>) -> Arc<Self> {
// Init components
let counter = Counter::new_arc();
let send = Send::new_arc(action_send);
// Init widget
let widget = Widget::new_arc(counter.gobject(), send.gobject());
// Return activated struct
Arc::new(Self {
counter,
send,
widget,
})
}
// Actions
pub fn update(&self, chars_left: Option<i32>) {
// Update children components
self.counter.update(chars_left);
self.send.update(match chars_left {
Some(left) => left > 0,
None => false,
});
}
// Getters
pub fn gobject(&self) -> &Box {
&self.widget.gobject()
}
}

View file

@ -0,0 +1,31 @@
mod widget;
use widget::Widget;
use gtk::Label;
use std::sync::Arc;
pub struct Counter {
widget: Arc<Widget>,
}
impl Counter {
// Construct
pub fn new_arc() -> Arc<Self> {
// Init widget
let widget = Widget::new_arc();
// Result
Arc::new(Self { widget })
}
// Actions
pub fn update(&self, chars_left: Option<i32>) {
self.widget.update(chars_left);
}
// Getters
pub fn gobject(&self) -> &Label {
&self.widget.gobject()
}
}

View file

@ -0,0 +1,38 @@
use gtk::{prelude::WidgetExt, Label};
use std::sync::Arc;
pub struct Widget {
gobject: Label,
}
impl Widget {
// Construct
pub fn new_arc() -> Arc<Self> {
let gobject = Label::builder().build();
Arc::new(Self { gobject })
}
// Actions
pub fn update(&self, chars_left: Option<i32>) {
match chars_left {
Some(value) => {
// Update color on chars left reached
self.gobject
.set_css_classes(&[if value > 0 { "success" } else { "error" }]); // @TODO add warning step?
// Update text
self.gobject.set_label(&value.to_string());
// Toggle visibility on chars left provided
self.gobject.set_visible(true);
}
None => self.gobject.set_visible(false),
}
}
// Getters
pub fn gobject(&self) -> &Label {
&self.gobject
}
}

View file

@ -0,0 +1,31 @@
mod widget;
use widget::Widget;
use gtk::{gio::SimpleAction, Button};
use std::sync::Arc;
pub struct Send {
widget: Arc<Widget>,
}
impl Send {
// Construct
pub fn new_arc(action_send: Arc<SimpleAction>) -> Arc<Self> {
// Init widget
let widget = Widget::new_arc(action_send);
// Result
Arc::new(Self { widget })
}
// Actions
pub fn update(&self, is_sensitive: bool) {
self.widget.update(is_sensitive);
}
// Getters
pub fn gobject(&self) -> &Button {
&self.widget.gobject()
}
}

View file

@ -0,0 +1,41 @@
use gtk::{
gio::SimpleAction,
prelude::{ActionExt, ButtonExt, WidgetExt},
Button,
};
use std::sync::Arc;
pub struct Widget {
gobject: Button,
}
impl Widget {
// Construct
pub fn new_arc(action_send: Arc<SimpleAction>) -> Arc<Self> {
// Init gobject
let gobject = Button::builder()
//.css_classes(["accent"])
.label("Send")
.build();
// Init events
gobject.connect_clicked({
move |_| {
action_send.activate(None);
}
});
// Return activated struct
Arc::new(Self { gobject })
}
// Actions
pub fn update(&self, is_sensitive: bool) {
self.gobject.set_sensitive(is_sensitive);
}
// Getters
pub fn gobject(&self) -> &Button {
&self.gobject
}
}

View file

@ -0,0 +1,31 @@
use gtk::{prelude::BoxExt, Align, Box, Button, Label, Orientation};
use std::sync::Arc;
const SPACING: i32 = 8;
pub struct Widget {
gobject: Box,
}
impl Widget {
// Construct
pub fn new_arc(limit: &Label, send: &Button) -> Arc<Self> {
// Init gobject
let gobject = Box::builder()
.halign(Align::End)
.orientation(Orientation::Horizontal)
.spacing(SPACING)
.build();
gobject.append(limit);
gobject.append(send);
// Return new struct
Arc::new(Self { gobject })
}
// Getters
pub fn gobject(&self) -> &Box {
&self.gobject
}
}

View file

@ -0,0 +1,35 @@
mod widget;
use widget::Widget;
use gtk::{gio::SimpleAction, glib::GString, TextView};
use std::sync::Arc;
pub struct Form {
widget: Arc<Widget>,
}
impl Form {
// Construct
pub fn new_arc(action_update: Arc<SimpleAction>) -> Arc<Self> {
// Init widget
let widget = Widget::new_arc(action_update);
// Result
Arc::new(Self { widget })
}
// Actions
pub fn focus(&self) {
self.widget.focus();
}
// Getters
pub fn text(&self) -> GString {
self.widget.text()
}
pub fn gobject(&self) -> &TextView {
&self.widget.gobject()
}
}

View file

@ -0,0 +1,45 @@
use gtk::{
gio::SimpleAction, glib::GString, prelude::{ActionExt, TextBufferExt, TextViewExt, WidgetExt}, TextView, WrapMode
};
use std::sync::Arc;
pub struct Widget {
gobject: TextView,
}
impl Widget {
// Construct
pub fn new_arc(action_update: Arc<SimpleAction>) -> Arc<Self> {
// Init gobject
let gobject = TextView::builder()
.left_margin(8)
.pixels_above_lines(8)
.pixels_below_lines(8)
.right_margin(8)
.wrap_mode(WrapMode::Word)
.build();
// Init events
gobject.buffer().connect_changed(move |_| {
action_update.activate(None);
});
// Return activated struct
Arc::new(Self { gobject })
}
// Actions
pub fn focus(&self) {
self.gobject.grab_focus();
}
// Getters
pub fn text(&self) -> GString {
let buffer = self.gobject.buffer();
buffer.text(&buffer.start_iter(), &buffer.end_iter(), true)
}
pub fn gobject(&self) -> &TextView {
&self.gobject
}
}

View file

@ -0,0 +1,26 @@
mod widget;
use widget::Widget;
use gtk::Label;
use std::sync::Arc;
pub struct Title {
widget: Arc<Widget>,
}
impl Title {
// Construct
pub fn new_arc(title: Option<&str>) -> Arc<Self> {
// Init widget
let widget = Widget::new_arc(title);
// Result
Arc::new(Self { widget })
}
// Getters
pub fn gobject(&self) -> &Label {
&self.widget.gobject()
}
}

View file

@ -0,0 +1,31 @@
use gtk::{prelude::WidgetExt, Align, Label};
use std::sync::Arc;
pub struct Widget {
gobject: Label,
}
impl Widget {
// Construct
pub fn new_arc(title: Option<&str>) -> Arc<Self> {
let gobject = Label::builder()
.css_classes(["heading"])
.halign(Align::Start)
.margin_end(8)
.margin_start(8)
.visible(false)
.build();
match title {
Some(value) => gobject.set_label(value),
None => gobject.set_visible(false),
}
Arc::new(Self { gobject })
}
// Getters
pub fn gobject(&self) -> &Label {
&self.gobject
}
}

View file

@ -0,0 +1,34 @@
use gtk::{prelude::BoxExt, Box, Label, Orientation, TextView};
use std::sync::Arc;
const MARGIN: i32 = 6;
const SPACING: i32 = 8;
pub struct Widget {
gobject: Box,
}
impl Widget {
// Construct
pub fn new_arc(title: &Label, response: &TextView, control: &Box) -> Arc<Self> {
let gobject = Box::builder()
.margin_bottom(MARGIN)
.margin_end(MARGIN)
.margin_start(MARGIN)
.margin_top(MARGIN)
.spacing(SPACING)
.orientation(Orientation::Vertical)
.build();
gobject.append(title);
gobject.append(response);
gobject.append(control);
Arc::new(Self { gobject })
}
// Getters
pub fn gobject(&self) -> &Box {
&self.gobject
}
}