reorganize children widgets

This commit is contained in:
yggverse 2025-01-22 08:28:27 +02:00
parent ba965814eb
commit 972fa6c3db
21 changed files with 271 additions and 488 deletions

View File

@ -64,7 +64,7 @@ impl Page {
&navigation.widget.g_box,
&content.g_box,
&search.g_box,
&input.widget.clamp,
&input.clamp,
));
// Done

View File

@ -1,18 +1,19 @@
mod response;
mod sensitive;
mod titan;
mod widget;
use super::TabAction;
use gtk::{glib::Uri, Label};
use adw::Clamp;
use gtk::{glib::Uri, prelude::WidgetExt, Box, Label};
use response::Response;
use sensitive::Sensitive;
use std::rc::Rc;
use titan::Titan;
use widget::Widget;
const MARGIN: i32 = 6;
pub struct Input {
pub widget: Rc<Widget>,
pub clamp: Clamp,
}
impl Default for Input {
@ -24,16 +25,28 @@ impl Default for Input {
impl Input {
// Construct
pub fn new() -> Self {
// Init widget
let widget = Rc::new(Widget::new());
let clamp = Clamp::builder()
.margin_bottom(MARGIN)
.margin_top(MARGIN)
.maximum_size(800)
.visible(false)
.build();
// Result
Self { widget }
Self { clamp }
}
// Actions
pub fn unset(&self) {
self.widget.update(None);
self.update(None);
}
pub fn update(&self, child: Option<&Box>) {
if child.is_some() {
self.clamp.set_visible(true); // widget may be hidden, make it visible to child redraw
self.clamp.set_child(child);
} else {
self.clamp.set_visible(false)
}
}
// Setters
@ -44,10 +57,8 @@ impl Input {
title: Option<&str>,
size_limit: Option<usize>,
) {
self.widget.update(Some(
&Response::build(action, base, title, size_limit)
.widget
.g_box,
self.update(Some(
&Response::build(action, base, title, size_limit).g_box,
));
}
@ -58,15 +69,12 @@ impl Input {
title: Option<&str>,
max_length: Option<i32>,
) {
self.widget.update(Some(
&Sensitive::build(action, base, title, max_length)
.widget
.g_box,
self.update(Some(
&Sensitive::build(action, base, title, max_length).g_box,
));
}
pub fn set_new_titan(&self, on_send: impl Fn(&[u8], &Label) + 'static) {
self.widget
.update(Some(&Titan::build(on_send).widget.g_box));
self.update(Some(&Titan::build(on_send).g_box));
}
}

View File

@ -1,23 +1,26 @@
mod control;
mod form;
mod title;
mod widget;
use control::Control;
use form::Form;
use title::Title;
use widget::Widget;
use super::TabAction;
use gtk::{
gio::SimpleAction,
glib::{uuid_string_random, Uri, UriHideFlags},
prelude::BoxExt,
Box, Orientation,
};
use std::rc::Rc;
const MARGIN: i32 = 6;
const SPACING: i32 = 8;
pub struct Response {
// Components
pub widget: Rc<Widget>,
pub g_box: Box,
}
impl Response {
@ -39,12 +42,19 @@ impl Response {
let form = Rc::new(Form::build(action_update.clone()));
let title = Rc::new(Title::build(title));
// Init widget
let widget = Rc::new(Widget::build(
&title.widget.label,
&form.widget.text_view,
&control.widget.g_box,
));
// Init main widget
let g_box = Box::builder()
.margin_bottom(MARGIN)
.margin_end(MARGIN)
.margin_start(MARGIN)
.margin_top(MARGIN)
.spacing(SPACING)
.orientation(Orientation::Vertical)
.build();
g_box.append(&title.label);
g_box.append(&form.text_view);
g_box.append(&control.g_box);
// Init events
action_update.connect_activate({
@ -53,11 +63,11 @@ impl Response {
let form = form.clone();
move |_, _| {
control.update(
form.widget.text().is_empty(),
form.text().is_empty(),
size_limit.map(|limit| {
limit as isize
- ((base.to_string_partial(UriHideFlags::QUERY).len()
+ Uri::escape_string(&form.widget.text(), None, false).len())
+ Uri::escape_string(&form.text(), None, false).len())
as isize)
}),
)
@ -71,7 +81,7 @@ impl Response {
Some(&format!(
"{}?{}",
base.to_string_partial(UriHideFlags::QUERY),
Uri::escape_string(&form.widget.text(), None, false),
Uri::escape_string(&form.text(), None, false),
)),
true,
);
@ -79,6 +89,6 @@ impl Response {
});
// Return activated struct
Self { widget }
Self { g_box }
}
}

View File

@ -1,18 +1,18 @@
mod counter;
mod send;
mod widget;
use counter::Counter;
use send::Send;
use widget::Widget;
use gtk::gio::SimpleAction;
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 widget: Rc<Widget>,
pub g_box: Box,
}
impl Control {
@ -24,14 +24,21 @@ impl Control {
let counter = Rc::new(Counter::new());
let send = Rc::new(Send::build(action_send));
// Init widget
let widget = Rc::new(Widget::build(&counter.label, &send.button));
// Init main widget
let g_box = Box::builder()
.halign(Align::End)
.orientation(Orientation::Horizontal)
.spacing(SPACING)
.build();
g_box.append(&counter.label);
g_box.append(&send.button);
// Return activated struct
Self {
counter,
send,
widget,
g_box,
}
}

View File

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

View File

@ -1,12 +1,16 @@
mod widget;
use gtk::{
gio::SimpleAction,
glib::GString,
prelude::{ActionExt, TextBufferExt, TextViewExt, WidgetExt},
TextView, WrapMode,
};
use libspelling::{Checker, TextBufferAdapter};
use sourceview::Buffer;
use widget::Widget;
use gtk::gio::SimpleAction;
use std::rc::Rc;
const MARGIN: i32 = 8;
pub struct Form {
pub widget: Rc<Widget>,
pub text_view: TextView,
}
impl Form {
@ -14,8 +18,46 @@ impl Form {
/// Build new `Self`
pub fn build(action_update: SimpleAction) -> Self {
Self {
widget: Rc::new(Widget::build(action_update)),
}
// Init [SourceView](https://gitlab.gnome.org/GNOME/gtksourceview) type buffer
let buffer = Buffer::builder().build();
// Init [libspelling](https://gitlab.gnome.org/GNOME/libspelling)
let checker = Checker::default();
let adapter = TextBufferAdapter::new(&buffer, &checker);
adapter.set_enabled(true);
// Init main widget
let text_view = TextView::builder()
.bottom_margin(MARGIN)
.buffer(&buffer)
.css_classes(["frame", "view"])
.extra_menu(&adapter.menu_model())
.left_margin(MARGIN)
.margin_bottom(MARGIN / 4)
.right_margin(MARGIN)
.top_margin(MARGIN)
.wrap_mode(WrapMode::Word)
.build();
text_view.insert_action_group("spelling", Some(&adapter));
// Init events
text_view.buffer().connect_changed(move |_| {
action_update.activate(None);
});
text_view.connect_realize(move |this| {
this.grab_focus();
});
// Return activated `Self`
Self { text_view }
}
// Getters
pub fn text(&self) -> GString {
let buffer = self.text_view.buffer();
buffer.text(&buffer.start_iter(), &buffer.end_iter(), true)
}
}

View File

@ -1,63 +0,0 @@
use gtk::{
gio::SimpleAction,
glib::GString,
prelude::{ActionExt, TextBufferExt, TextViewExt, WidgetExt},
TextView, WrapMode,
};
use libspelling::{Checker, TextBufferAdapter};
use sourceview::Buffer;
const MARGIN: i32 = 8;
pub struct Widget {
pub text_view: TextView,
}
impl Widget {
// Constructors
/// Build new `Self`
pub fn build(action_update: SimpleAction) -> Self {
// Init [SourceView](https://gitlab.gnome.org/GNOME/gtksourceview) type buffer
let buffer = Buffer::builder().build();
// Init [libspelling](https://gitlab.gnome.org/GNOME/libspelling)
let checker = Checker::default();
let adapter = TextBufferAdapter::new(&buffer, &checker);
adapter.set_enabled(true);
// Init main widget
let text_view = TextView::builder()
.bottom_margin(MARGIN)
.buffer(&buffer)
.css_classes(["frame", "view"])
.extra_menu(&adapter.menu_model())
.left_margin(MARGIN)
.margin_bottom(MARGIN / 4)
.right_margin(MARGIN)
.top_margin(MARGIN)
.wrap_mode(WrapMode::Word)
.build();
text_view.insert_action_group("spelling", Some(&adapter));
// Init events
text_view.buffer().connect_changed(move |_| {
action_update.activate(None);
});
text_view.connect_realize(move |this| {
this.grab_focus();
});
// Return activated `Self`
Self { text_view }
}
// Getters
pub fn text(&self) -> GString {
let buffer = self.text_view.buffer();
buffer.text(&buffer.start_iter(), &buffer.end_iter(), true)
}
}

View File

@ -1,11 +1,7 @@
mod widget;
use widget::Widget;
use std::rc::Rc;
use gtk::{prelude::WidgetExt, Align, Label};
pub struct Title {
pub widget: Rc<Widget>,
pub label: Label,
}
impl Title {
@ -13,8 +9,19 @@ impl Title {
/// Build new `Self`
pub fn build(title: Option<&str>) -> Self {
Self {
widget: Rc::new(Widget::build(title)),
let label = Label::builder()
.css_classes(["heading"])
.halign(Align::Start)
.visible(false)
.build();
if let Some(value) = title {
if !value.is_empty() {
label.set_label(value);
label.set_visible(true)
}
}
Self { label }
}
}

View File

@ -1,27 +0,0 @@
use gtk::{prelude::WidgetExt, Align, Label};
pub struct Widget {
pub label: Label,
}
impl Widget {
// Constructors
/// Build new `Self`
pub fn build(title: Option<&str>) -> Self {
let label = Label::builder()
.css_classes(["heading"])
.halign(Align::Start)
.visible(false)
.build();
if let Some(value) = title {
if !value.is_empty() {
label.set_label(value);
label.set_visible(true)
}
}
Self { label }
}
}

View File

@ -1,30 +0,0 @@
use gtk::{prelude::BoxExt, Box, Label, Orientation, TextView};
const MARGIN: i32 = 6;
const SPACING: i32 = 8;
pub struct Widget {
pub g_box: Box,
}
impl Widget {
// Constructors
/// Build new `Self`
pub fn build(title: &Label, response: &TextView, control: &Box) -> Self {
let g_box = Box::builder()
.margin_bottom(MARGIN)
.margin_end(MARGIN)
.margin_start(MARGIN)
.margin_top(MARGIN)
.spacing(SPACING)
.orientation(Orientation::Vertical)
.build();
g_box.append(title);
g_box.append(response);
g_box.append(control);
Self { g_box }
}
}

View File

@ -1,19 +1,20 @@
mod form;
mod widget;
use form::Form;
use widget::Widget;
use super::TabAction;
use form::Form;
use gtk::{
gio::SimpleAction,
glib::{uuid_string_random, Uri, UriHideFlags},
prelude::{EditableExt, WidgetExt},
prelude::{BoxExt, EditableExt, WidgetExt},
Box, Orientation,
};
use std::rc::Rc;
const MARGIN: i32 = 6;
const SPACING: i32 = 8;
pub struct Sensitive {
pub widget: Rc<Widget>,
pub g_box: Box,
}
impl Sensitive {
@ -38,7 +39,16 @@ impl Sensitive {
));
// Init widget
let widget = Rc::new(Widget::build(&form.widget.password_entry_row));
let g_box = Box::builder()
.margin_bottom(MARGIN)
.margin_end(MARGIN)
.margin_start(MARGIN)
.margin_top(MARGIN)
.spacing(SPACING)
.orientation(Orientation::Vertical)
.build();
g_box.append(&form.password_entry_row);
// Init events
action_send.connect_activate({
@ -48,18 +58,18 @@ impl Sensitive {
Some(&format!(
"{}?{}",
base.to_string_partial(UriHideFlags::QUERY),
Uri::escape_string(&form.widget.password_entry_row.text(), None, false),
Uri::escape_string(&form.password_entry_row.text(), None, false),
)),
true,
);
}
});
widget.g_box.connect_realize(move |_| {
form.widget.password_entry_row.grab_focus();
g_box.connect_realize(move |_| {
form.password_entry_row.grab_focus();
});
// Return activated struct
Self { widget }
Self { g_box }
}
}

View File

@ -1,23 +1,43 @@
mod widget;
use widget::Widget;
use gtk::gio::SimpleAction;
use std::rc::Rc;
use adw::{
prelude::{EntryRowExt, PreferencesRowExt},
PasswordEntryRow,
};
use gtk::{
gio::SimpleAction,
prelude::{ActionExt, WidgetExt},
};
pub struct Form {
pub widget: Rc<Widget>,
pub password_entry_row: PasswordEntryRow,
}
impl Form {
// Constructors
/// Build new `Self`
pub fn build(action_send: SimpleAction, title: Option<&str>, max_length: Option<i32>) -> Self {
// Init widget
let widget = Rc::new(Widget::build(action_send, title, max_length));
pub fn build(action_send: SimpleAction, title: Option<&str>, _max_length: Option<i32>) -> Self {
// Init main widget
let password_entry_row = PasswordEntryRow::builder().show_apply_button(true).build();
// Result
Self { widget }
if let Some(value) = title {
password_entry_row.set_title(value);
}
/* @TODO adw 1.6 / ubuntu 24.10+
if let Some(value) = max_length {
password_entry_row.set_max_length(value);
} */
// Init events
password_entry_row.connect_apply(move |_| {
action_send.activate(None);
});
password_entry_row.connect_realize(move |this| {
this.grab_focus();
});
// Return activated struct
Self { password_entry_row }
}
}

View File

@ -1,43 +0,0 @@
use adw::{
prelude::{EntryRowExt, PreferencesRowExt},
PasswordEntryRow,
};
use gtk::{
gio::SimpleAction,
prelude::{ActionExt, WidgetExt},
};
pub struct Widget {
pub password_entry_row: PasswordEntryRow,
}
impl Widget {
// Constructors
/// Build new `Self`
pub fn build(action_send: SimpleAction, title: Option<&str>, _max_length: Option<i32>) -> Self {
// Init main widget
let password_entry_row = PasswordEntryRow::builder().show_apply_button(true).build();
if let Some(value) = title {
password_entry_row.set_title(value);
}
/* @TODO adw 1.6 / ubuntu 24.10+
if let Some(value) = max_length {
password_entry_row.set_max_length(value);
} */
// Init events
password_entry_row.connect_apply(move |_| {
action_send.activate(None);
});
password_entry_row.connect_realize(move |this| {
this.grab_focus();
});
// Return activated struct
Self { password_entry_row }
}
}

View File

@ -1,29 +0,0 @@
use adw::PasswordEntryRow;
use gtk::{prelude::BoxExt, Box, Orientation};
const MARGIN: i32 = 6;
const SPACING: i32 = 8;
pub struct Widget {
pub g_box: Box,
}
impl Widget {
// Constructors
/// Build new `Self`
pub fn build(response: &PasswordEntryRow) -> Self {
let g_box = Box::builder()
.margin_bottom(MARGIN)
.margin_end(MARGIN)
.margin_start(MARGIN)
.margin_top(MARGIN)
.spacing(SPACING)
.orientation(Orientation::Vertical)
.build();
g_box.append(response);
Self { g_box }
}
}

View File

@ -1,19 +1,20 @@
mod control;
mod form;
mod title;
mod widget;
use control::Control;
use form::Form;
use title::Title;
use widget::Widget;
use gtk::{gio::SimpleAction, glib::uuid_string_random, Label};
use gtk::{gio::SimpleAction, glib::uuid_string_random, prelude::BoxExt, Box, Label, Orientation};
use std::rc::Rc;
const MARGIN: i32 = 6;
const SPACING: i32 = 8;
pub struct Titan {
// Components
pub widget: Rc<Widget>,
pub g_box: Box,
}
impl Titan {
@ -31,24 +32,30 @@ impl Titan {
let title = Title::build(None);
// Init widget
let widget = Rc::new(Widget::build(
&title.label,
&form.widget.text_view,
&control.widget.g_box,
));
let g_box = Box::builder()
.margin_bottom(MARGIN)
.margin_end(MARGIN)
.margin_start(MARGIN)
.margin_top(MARGIN)
.spacing(SPACING)
.orientation(Orientation::Vertical)
.build();
g_box.append(&title.label);
g_box.append(&form.text_view);
g_box.append(&control.g_box);
// Init events
action_update.connect_activate({
let control = control.clone();
let form = form.clone();
move |_, _| control.update(Some(form.widget.text().as_bytes().len()))
move |_, _| control.update(Some(form.text().as_bytes().len()))
});
action_send.connect_activate(move |_, _| {
on_send(form.widget.text().as_bytes(), &control.counter.label)
});
action_send
.connect_activate(move |_, _| on_send(form.text().as_bytes(), &control.counter.label));
// Return activated struct
Self { widget }
Self { g_box }
}
}

View File

@ -1,18 +1,18 @@
mod counter;
mod send;
mod widget;
use counter::Counter;
use send::Send;
use widget::Widget;
use gtk::gio::SimpleAction;
use gtk::{prelude::BoxExt, Align, Box, Orientation};
use send::Send;
use std::rc::Rc;
const SPACING: i32 = 8;
pub struct Control {
pub counter: Rc<Counter>,
pub send: Rc<Send>,
pub widget: Rc<Widget>,
pub g_box: Box,
}
impl Control {
@ -24,14 +24,21 @@ impl Control {
let counter = Rc::new(Counter::new());
let send = Rc::new(Send::build(action_send));
// Init widget
let widget = Rc::new(Widget::build(&counter.label, &send.button));
// Init main widget
let g_box = Box::builder()
.halign(Align::End)
.orientation(Orientation::Horizontal)
.spacing(SPACING)
.build();
g_box.append(&counter.label);
g_box.append(&send.button);
// Return activated struct
Self {
counter,
send,
widget,
g_box,
}
}

View File

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

View File

@ -1,12 +1,16 @@
mod widget;
use gtk::{
gio::SimpleAction,
glib::GString,
prelude::{ActionExt, TextBufferExt, TextViewExt, WidgetExt},
TextView, WrapMode,
};
use libspelling::{Checker, TextBufferAdapter};
use sourceview::Buffer;
use widget::Widget;
use gtk::gio::SimpleAction;
use std::rc::Rc;
const MARGIN: i32 = 8;
pub struct Form {
pub widget: Rc<Widget>,
pub text_view: TextView,
}
impl Form {
@ -14,8 +18,46 @@ impl Form {
/// Build new `Self`
pub fn build(action_update: SimpleAction) -> Self {
Self {
widget: Rc::new(Widget::build(action_update)),
}
// Init [SourceView](https://gitlab.gnome.org/GNOME/gtksourceview) type buffer
let buffer = Buffer::builder().build();
// Init [libspelling](https://gitlab.gnome.org/GNOME/libspelling)
let checker = Checker::default();
let adapter = TextBufferAdapter::new(&buffer, &checker);
adapter.set_enabled(true);
// Init main widget
let text_view = TextView::builder()
.bottom_margin(MARGIN)
.buffer(&buffer)
.css_classes(["frame", "view"])
.extra_menu(&adapter.menu_model())
.left_margin(MARGIN)
.margin_bottom(MARGIN / 4)
.right_margin(MARGIN)
.top_margin(MARGIN)
.wrap_mode(WrapMode::Word)
.build();
text_view.insert_action_group("spelling", Some(&adapter));
// Init events
text_view.buffer().connect_changed(move |_| {
action_update.activate(None);
});
text_view.connect_realize(move |this| {
this.grab_focus();
});
// Return activated `Self`
Self { text_view }
}
// Getters
pub fn text(&self) -> GString {
let buffer = self.text_view.buffer();
buffer.text(&buffer.start_iter(), &buffer.end_iter(), true)
}
}

View File

@ -1,63 +0,0 @@
use gtk::{
gio::SimpleAction,
glib::GString,
prelude::{ActionExt, TextBufferExt, TextViewExt, WidgetExt},
TextView, WrapMode,
};
use libspelling::{Checker, TextBufferAdapter};
use sourceview::Buffer;
const MARGIN: i32 = 8;
pub struct Widget {
pub text_view: TextView,
}
impl Widget {
// Constructors
/// Build new `Self`
pub fn build(action_update: SimpleAction) -> Self {
// Init [SourceView](https://gitlab.gnome.org/GNOME/gtksourceview) type buffer
let buffer = Buffer::builder().build();
// Init [libspelling](https://gitlab.gnome.org/GNOME/libspelling)
let checker = Checker::default();
let adapter = TextBufferAdapter::new(&buffer, &checker);
adapter.set_enabled(true);
// Init main widget
let text_view = TextView::builder()
.bottom_margin(MARGIN)
.buffer(&buffer)
.css_classes(["frame", "view"])
.extra_menu(&adapter.menu_model())
.left_margin(MARGIN)
.margin_bottom(MARGIN / 4)
.right_margin(MARGIN)
.top_margin(MARGIN)
.wrap_mode(WrapMode::Word)
.build();
text_view.insert_action_group("spelling", Some(&adapter));
// Init events
text_view.buffer().connect_changed(move |_| {
action_update.activate(None);
});
text_view.connect_realize(move |this| {
this.grab_focus();
});
// Return activated `Self`
Self { text_view }
}
// Getters
pub fn text(&self) -> GString {
let buffer = self.text_view.buffer();
buffer.text(&buffer.start_iter(), &buffer.end_iter(), true)
}
}

View File

@ -1,30 +0,0 @@
use gtk::{prelude::BoxExt, Box, Label, Orientation, TextView};
const MARGIN: i32 = 6;
const SPACING: i32 = 8;
pub struct Widget {
pub g_box: Box,
}
impl Widget {
// Constructors
/// Build new `Self`
pub fn build(title: &Label, response: &TextView, control: &Box) -> Self {
let g_box = Box::builder()
.margin_bottom(MARGIN)
.margin_end(MARGIN)
.margin_start(MARGIN)
.margin_top(MARGIN)
.spacing(SPACING)
.orientation(Orientation::Vertical)
.build();
g_box.append(title);
g_box.append(response);
g_box.append(control);
Self { g_box }
}
}

View File

@ -1,38 +0,0 @@
use adw::Clamp;
use gtk::{prelude::WidgetExt, Box};
const MARGIN: i32 = 6;
pub struct Widget {
pub clamp: Clamp,
}
impl Default for Widget {
fn default() -> Self {
Self::new()
}
}
impl Widget {
// Construct
pub fn new() -> Self {
let clamp = Clamp::builder()
.margin_bottom(MARGIN)
.margin_top(MARGIN)
.maximum_size(800)
.visible(false)
.build();
Self { clamp }
}
// Actions
pub fn update(&self, child: Option<&Box>) {
if child.is_some() {
self.clamp.set_visible(true); // widget may be hidden, make it visible to child redraw
self.clamp.set_child(child);
} else {
self.clamp.set_visible(false)
}
}
}