remove extra getters, give name to gobjects

This commit is contained in:
yggverse 2024-12-05 21:17:32 +02:00
parent 36d4673d44
commit 2799ce37fe
16 changed files with 76 additions and 180 deletions

View File

@ -38,7 +38,7 @@ impl Input {
size_limit: Option<usize>, size_limit: Option<usize>,
) { ) {
self.widget.update(Some( self.widget.update(Some(
Response::new(action, base, title, size_limit).gobject(), &Response::new(action, base, title, size_limit).widget.g_box,
)); ));
} }

View File

@ -13,13 +13,12 @@ use gtk::{
gio::SimpleAction, gio::SimpleAction,
glib::{uuid_string_random, Uri, UriHideFlags}, glib::{uuid_string_random, Uri, UriHideFlags},
prelude::WidgetExt, prelude::WidgetExt,
Box,
}; };
use std::rc::Rc; use std::rc::Rc;
pub struct Response { pub struct Response {
// Components // Components
widget: Rc<Widget>, pub widget: Rc<Widget>,
} }
impl Response { impl Response {
@ -41,9 +40,9 @@ impl Response {
// Init widget // Init widget
let widget = Rc::new(Widget::new( let widget = Rc::new(Widget::new(
title.gobject(), &title.widget.label,
form.gobject(), &form.widget.text_view,
control.gobject(), &control.widget.g_box,
)); ));
// Init events // Init events
@ -55,7 +54,7 @@ impl Response {
control.update(size_limit.map(|limit| { control.update(size_limit.map(|limit| {
limit as i32 limit as i32
- (base.to_string_partial(UriHideFlags::QUERY).len() as i32 - (base.to_string_partial(UriHideFlags::QUERY).len() as i32
+ Uri::escape_string(form.text().as_str(), None, false).len() as i32) + Uri::escape_string(&form.widget.text(), None, false).len() as i32)
})); }));
} }
}); });
@ -67,21 +66,18 @@ impl Response {
Some(&format!( Some(&format!(
"{}?{}", "{}?{}",
base.to_string_partial(UriHideFlags::QUERY), base.to_string_partial(UriHideFlags::QUERY),
Uri::escape_string(form.text().as_str(), None, false), Uri::escape_string(&form.widget.text(), None, false),
)), )),
true, true,
); );
} }
}); });
widget.gobject().connect_realize(move |_| form.focus()); widget.g_box.connect_realize(move |_| {
form.widget.text_view.grab_focus();
});
// Return activated struct // Return activated struct
Self { widget } Self { widget }
} }
// Getters
pub fn gobject(&self) -> &Box {
self.widget.gobject()
}
} }

View File

@ -6,13 +6,13 @@ use counter::Counter;
use send::Send; use send::Send;
use widget::Widget; use widget::Widget;
use gtk::{gio::SimpleAction, Box}; use gtk::gio::SimpleAction;
use std::rc::Rc; use std::rc::Rc;
pub struct Control { pub struct Control {
counter: Rc<Counter>, pub counter: Rc<Counter>,
send: Rc<Send>, pub send: Rc<Send>,
widget: Rc<Widget>, pub widget: Rc<Widget>,
} }
impl Control { impl Control {
@ -23,7 +23,7 @@ impl Control {
let send = Rc::new(Send::new(action_send)); let send = Rc::new(Send::new(action_send));
// Init widget // Init widget
let widget = Rc::new(Widget::new(counter.gobject(), send.gobject())); let widget = Rc::new(Widget::new(&counter.widget.label, &send.widget.button));
// Return activated struct // Return activated struct
Self { Self {
@ -42,9 +42,4 @@ impl Control {
None => false, None => false,
}); });
} }
// Getters
pub fn gobject(&self) -> &Box {
self.widget.gobject()
}
} }

View File

@ -2,11 +2,10 @@ mod widget;
use widget::Widget; use widget::Widget;
use gtk::Label;
use std::rc::Rc; use std::rc::Rc;
pub struct Counter { pub struct Counter {
widget: Rc<Widget>, pub widget: Rc<Widget>,
} }
impl Counter { impl Counter {
@ -21,9 +20,4 @@ impl Counter {
pub fn update(&self, chars_left: Option<i32>) { pub fn update(&self, chars_left: Option<i32>) {
self.widget.update(chars_left); self.widget.update(chars_left);
} }
// Getters
pub fn gobject(&self) -> &Label {
self.widget.gobject()
}
} }

View File

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

View File

@ -1,11 +1,11 @@
mod widget; mod widget;
use widget::Widget; use widget::Widget;
use gtk::{gio::SimpleAction, Button}; use gtk::gio::SimpleAction;
use std::rc::Rc; use std::rc::Rc;
pub struct Send { pub struct Send {
widget: Rc<Widget>, pub widget: Rc<Widget>,
} }
impl Send { impl Send {
@ -22,9 +22,4 @@ impl Send {
pub fn update(&self, is_sensitive: bool) { pub fn update(&self, is_sensitive: bool) {
self.widget.update(is_sensitive); self.widget.update(is_sensitive);
} }
// Getters
pub fn gobject(&self) -> &Button {
self.widget.gobject()
}
} }

View File

@ -5,36 +5,31 @@ use gtk::{
}; };
pub struct Widget { pub struct Widget {
gobject: Button, pub button: Button,
} }
impl Widget { impl Widget {
// Construct // Construct
pub fn new(action_send: SimpleAction) -> Self { pub fn new(action_send: SimpleAction) -> Self {
// Init gobject // Init main widget
let gobject = Button::builder() let button = Button::builder()
//.css_classes(["accent"]) //.css_classes(["accent"])
.label("Send") .label("Send")
.build(); .build();
// Init events // Init events
gobject.connect_clicked({ button.connect_clicked({
move |_| { move |_| {
action_send.activate(None); action_send.activate(None);
} }
}); });
// Return activated `Self` // Return activated `Self`
Self { gobject } Self { button }
} }
// Actions // Actions
pub fn update(&self, is_sensitive: bool) { pub fn update(&self, is_sensitive: bool) {
self.gobject.set_sensitive(is_sensitive); self.button.set_sensitive(is_sensitive);
}
// Getters
pub fn gobject(&self) -> &Button {
&self.gobject
} }
} }

View File

@ -3,28 +3,23 @@ use gtk::{prelude::BoxExt, Align, Box, Button, Label, Orientation};
const SPACING: i32 = 8; const SPACING: i32 = 8;
pub struct Widget { pub struct Widget {
gobject: Box, pub g_box: Box,
} }
impl Widget { impl Widget {
// Construct // Construct
pub fn new(limit: &Label, send: &Button) -> Self { pub fn new(limit: &Label, send: &Button) -> Self {
// Init gobject // Init main widget
let gobject = Box::builder() let g_box = Box::builder()
.halign(Align::End) .halign(Align::End)
.orientation(Orientation::Horizontal) .orientation(Orientation::Horizontal)
.spacing(SPACING) .spacing(SPACING)
.build(); .build();
gobject.append(limit); g_box.append(limit);
gobject.append(send); g_box.append(send);
// Return new `Self` // Return new `Self`
Self { gobject } Self { g_box }
}
// Getters
pub fn gobject(&self) -> &Box {
&self.gobject
} }
} }

View File

@ -2,11 +2,11 @@ mod widget;
use widget::Widget; use widget::Widget;
use gtk::{gio::SimpleAction, glib::GString, TextView}; use gtk::gio::SimpleAction;
use std::rc::Rc; use std::rc::Rc;
pub struct Form { pub struct Form {
widget: Rc<Widget>, pub widget: Rc<Widget>,
} }
impl Form { impl Form {
@ -16,18 +16,4 @@ impl Form {
widget: Rc::new(Widget::new(action_update)), widget: Rc::new(Widget::new(action_update)),
} }
} }
// 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

@ -1,21 +1,21 @@
use gtk::{ use gtk::{
gio::SimpleAction, gio::SimpleAction,
glib::GString, glib::GString,
prelude::{ActionExt, TextBufferExt, TextViewExt, WidgetExt}, prelude::{ActionExt, TextBufferExt, TextViewExt},
TextView, WrapMode, TextView, WrapMode,
}; };
const MARGIN: i32 = 8; const MARGIN: i32 = 8;
pub struct Widget { pub struct Widget {
gobject: TextView, pub text_view: TextView,
} }
impl Widget { impl Widget {
// Construct // Construct
pub fn new(action_update: SimpleAction) -> Self { pub fn new(action_update: SimpleAction) -> Self {
// Init gobject // Init main widget
let gobject = TextView::builder() let text_view = TextView::builder()
.bottom_margin(MARGIN) .bottom_margin(MARGIN)
.left_margin(MARGIN) .left_margin(MARGIN)
.right_margin(MARGIN) .right_margin(MARGIN)
@ -24,26 +24,18 @@ impl Widget {
.build(); .build();
// Init events // Init events
gobject.buffer().connect_changed(move |_| { text_view.buffer().connect_changed(move |_| {
action_update.activate(None); action_update.activate(None);
}); });
// Return activated `Self` // Return activated `Self`
Self { gobject } Self { text_view }
}
// Actions
pub fn focus(&self) {
self.gobject.grab_focus();
} }
// Getters // Getters
pub fn text(&self) -> GString { pub fn text(&self) -> GString {
let buffer = self.gobject.buffer(); let buffer = self.text_view.buffer();
buffer.text(&buffer.start_iter(), &buffer.end_iter(), true) buffer.text(&buffer.start_iter(), &buffer.end_iter(), true)
} }
pub fn gobject(&self) -> &TextView {
&self.gobject
}
} }

View File

@ -2,11 +2,10 @@ mod widget;
use widget::Widget; use widget::Widget;
use gtk::Label;
use std::rc::Rc; use std::rc::Rc;
pub struct Title { pub struct Title {
widget: Rc<Widget>, pub widget: Rc<Widget>,
} }
impl Title { impl Title {
@ -16,9 +15,4 @@ impl Title {
widget: Rc::new(Widget::new(title)), widget: Rc::new(Widget::new(title)),
} }
} }
// Getters
pub fn gobject(&self) -> &Label {
self.widget.gobject()
}
} }

View File

@ -1,13 +1,13 @@
use gtk::{prelude::WidgetExt, Align, Label}; use gtk::{prelude::WidgetExt, Align, Label};
pub struct Widget { pub struct Widget {
gobject: Label, pub label: Label,
} }
impl Widget { impl Widget {
// Construct // Construct
pub fn new(title: Option<&str>) -> Self { pub fn new(title: Option<&str>) -> Self {
let gobject = Label::builder() let label = Label::builder()
.css_classes(["heading"]) .css_classes(["heading"])
.halign(Align::Start) .halign(Align::Start)
.margin_end(8) .margin_end(8)
@ -15,18 +15,13 @@ impl Widget {
.visible(false) .visible(false)
.build(); .build();
if let Some(label) = title { if let Some(value) = title {
if !label.is_empty() { if !value.is_empty() {
gobject.set_label(label); label.set_label(value);
gobject.set_visible(true) label.set_visible(true)
} }
} }
Self { gobject } Self { label }
}
// Getters
pub fn gobject(&self) -> &Label {
&self.gobject
} }
} }

View File

@ -4,13 +4,13 @@ const MARGIN: i32 = 6;
const SPACING: i32 = 8; const SPACING: i32 = 8;
pub struct Widget { pub struct Widget {
gobject: Box, pub g_box: Box,
} }
impl Widget { impl Widget {
// Construct // Construct
pub fn new(title: &Label, response: &TextView, control: &Box) -> Self { pub fn new(title: &Label, response: &TextView, control: &Box) -> Self {
let gobject = Box::builder() let g_box = Box::builder()
.margin_bottom(MARGIN) .margin_bottom(MARGIN)
.margin_end(MARGIN) .margin_end(MARGIN)
.margin_start(MARGIN) .margin_start(MARGIN)
@ -19,15 +19,10 @@ impl Widget {
.orientation(Orientation::Vertical) .orientation(Orientation::Vertical)
.build(); .build();
gobject.append(title); g_box.append(title);
gobject.append(response); g_box.append(response);
gobject.append(control); g_box.append(control);
Self { gobject } Self { g_box }
}
// Getters
pub fn gobject(&self) -> &Box {
&self.gobject
} }
} }

View File

@ -8,7 +8,7 @@ use crate::app::browser::window::tab::item::action::Action as TabAction;
use gtk::{ use gtk::{
gio::SimpleAction, gio::SimpleAction,
glib::{uuid_string_random, Uri, UriHideFlags}, glib::{uuid_string_random, Uri, UriHideFlags},
prelude::WidgetExt, prelude::{EditableExt, WidgetExt},
Box, Box,
}; };
use std::rc::Rc; use std::rc::Rc;
@ -38,7 +38,7 @@ impl Sensitive {
)); ));
// Init widget // Init widget
let widget = Rc::new(Widget::new(form.gobject())); let widget = Rc::new(Widget::new(&form.widget.password_entry_row));
// Init events // Init events
action_send.connect_activate({ action_send.connect_activate({
@ -48,14 +48,16 @@ impl Sensitive {
Some(&format!( Some(&format!(
"{}?{}", "{}?{}",
base.to_string_partial(UriHideFlags::QUERY), base.to_string_partial(UriHideFlags::QUERY),
Uri::escape_string(form.text().as_str(), None, false), Uri::escape_string(&form.widget.password_entry_row.text(), None, false),
)), )),
true, true,
); );
} }
}); });
widget.gobject().connect_realize(move |_| form.focus()); widget.gobject().connect_realize(move |_| {
form.widget.password_entry_row.grab_focus();
});
// Return activated struct // Return activated struct
Self { widget } Self { widget }

View File

@ -2,12 +2,11 @@ mod widget;
use widget::Widget; use widget::Widget;
use adw::PasswordEntryRow; use gtk::gio::SimpleAction;
use gtk::{gio::SimpleAction, glib::GString};
use std::rc::Rc; use std::rc::Rc;
pub struct Form { pub struct Form {
widget: Rc<Widget>, pub widget: Rc<Widget>,
} }
impl Form { impl Form {
@ -19,18 +18,4 @@ impl Form {
// Result // Result
Self { widget } Self { widget }
} }
// Actions
pub fn focus(&self) {
self.widget.focus();
}
// Getters
pub fn text(&self) -> GString {
self.widget.text()
}
pub fn gobject(&self) -> &PasswordEntryRow {
self.widget.gobject()
}
} }

View File

@ -2,51 +2,33 @@ use adw::{
prelude::{EntryRowExt, PreferencesRowExt}, prelude::{EntryRowExt, PreferencesRowExt},
PasswordEntryRow, PasswordEntryRow,
}; };
use gtk::{ use gtk::{gio::SimpleAction, prelude::ActionExt};
gio::SimpleAction,
glib::GString,
prelude::{ActionExt, EditableExt, WidgetExt},
};
pub struct Widget { pub struct Widget {
gobject: PasswordEntryRow, pub password_entry_row: PasswordEntryRow,
} }
impl Widget { impl Widget {
// Construct // Construct
pub fn new(action_send: SimpleAction, title: Option<&str>, _max_length: Option<i32>) -> Self { pub fn new(action_send: SimpleAction, title: Option<&str>, _max_length: Option<i32>) -> Self {
// Init gobject // Init main widget
let gobject = PasswordEntryRow::builder().show_apply_button(true).build(); let password_entry_row = PasswordEntryRow::builder().show_apply_button(true).build();
if let Some(value) = title { if let Some(value) = title {
gobject.set_title(value); password_entry_row.set_title(value);
} }
/* @TODO adw 1.6 / ubuntu 24.10+ /* @TODO adw 1.6 / ubuntu 24.10+
if let Some(value) = max_length { if let Some(value) = max_length {
gobject.set_max_length(value); password_entry_row.set_max_length(value);
} */ } */
// Init events // Init events
gobject.connect_apply(move |_| { password_entry_row.connect_apply(move |_| {
action_send.activate(None); action_send.activate(None);
}); });
// Return activated struct // Return activated struct
Self { gobject } Self { password_entry_row }
}
// Actions
pub fn focus(&self) {
self.gobject.grab_focus();
}
// Getters
pub fn text(&self) -> GString {
self.gobject.text()
}
pub fn gobject(&self) -> &PasswordEntryRow {
&self.gobject
} }
} }