move header preset init to the target widget impl

This commit is contained in:
yggverse 2025-02-06 18:37:11 +02:00
parent f6fb73c241
commit ffb6929bec
3 changed files with 40 additions and 60 deletions

View File

@ -6,7 +6,7 @@ mod title;
use file::File; use file::File;
use gtk::{ use gtk::{
glib::{uuid_string_random, Bytes}, glib::{uuid_string_random, Bytes},
Label, Notebook, Notebook,
}; };
pub use header::Header; pub use header::Header;
use text::Text; use text::Text;
@ -18,17 +18,14 @@ pub trait Titan {
impl Titan for Notebook { impl Titan for Notebook {
fn titan(callback: impl Fn(Header, Bytes, Box<dyn Fn()>) + 'static) -> Self { fn titan(callback: impl Fn(Header, Bytes, Box<dyn Fn()>) + 'static) -> Self {
use gtk::Box; use gtk::{Box, Label};
use std::{cell::Cell, rc::Rc};
let notebook = Notebook::builder() let notebook = Notebook::builder()
.name(format!("s{}", uuid_string_random())) .name(format!("s{}", uuid_string_random()))
.show_border(false) .show_border(false)
.build(); .build();
let header = Rc::new(Cell::new(Header::new())); notebook.append_page(&Box::text(callback), Some(&Label::title("Text")));
notebook.append_page(&Box::text(&header, callback), Some(&Label::title("Text")));
notebook.append_page(&Box::file(), Some(&Label::title("File"))); notebook.append_page(&Box::file(), Some(&Label::title("File")));
notebook_css_patch(&notebook); notebook_css_patch(&notebook);

View File

@ -9,13 +9,6 @@ pub struct Header {
} }
impl Header { impl Header {
pub fn new() -> Self {
Self {
mime: None,
token: None,
}
}
/// Show header options dialog for the referrer `widget` /// Show header options dialog for the referrer `widget`
/// * takes ownership of `Self`, return new updated copy in `callback` function /// * takes ownership of `Self`, return new updated copy in `callback` function
pub fn dialog(self, widget: Option<&impl IsA<Widget>>, callback: impl Fn(Self) + 'static) { pub fn dialog(self, widget: Option<&impl IsA<Widget>>, callback: impl Fn(Self) + 'static) {

View File

@ -13,69 +13,59 @@ use gtk::{
use std::cell::Cell; use std::cell::Cell;
use std::rc::Rc; use std::rc::Rc;
const MARGIN: i32 = 8;
const SPACING: i32 = 8;
pub trait Text { pub trait Text {
fn text( fn text(callback: impl Fn(Header, Bytes, Box<dyn Fn()>) + 'static) -> Self;
header: &Rc<Cell<Header>>,
callback: impl Fn(Header, Bytes, Box<dyn Fn()>) + 'static,
) -> Self;
} }
impl Text for gtk::Box { impl Text for gtk::Box {
fn text( fn text(callback: impl Fn(Header, Bytes, Box<dyn Fn()>) + 'static) -> Self {
header: &Rc<Cell<Header>>,
callback: impl Fn(Header, Bytes, Box<dyn Fn()>) + 'static,
) -> Self {
// Init components // Init components
let control = Rc::new(Control::build(header)); let header = Rc::new(Cell::new(Header {
mime: Some("text/plain".into()), // some servers require not empty content type
token: None,
}));
let control = Rc::new(Control::build(&header));
let form = TextView::form(); let form = TextView::form();
//header.take().dialog();
// Init widget // Init widget
let g_box = gtk::Box::builder() let g_box = {
.margin_bottom(MARGIN) const MARGIN: i32 = 8;
.margin_end(MARGIN) const SPACING: i32 = 8;
.margin_start(MARGIN)
.orientation(Orientation::Vertical)
.spacing(SPACING)
//.margin_top(MARGIN)
.build();
g_box.append(&form); let g_box = gtk::Box::builder()
g_box.append(&control.g_box); .margin_bottom(MARGIN)
.margin_end(MARGIN)
.margin_start(MARGIN)
.orientation(Orientation::Vertical)
.spacing(SPACING)
.build();
g_box.append(&form);
g_box.append(&control.g_box);
g_box
};
// Connect events // Connect events
form.buffer().connect_changed({ form.buffer().connect_changed({
let control = control.clone(); let control = control.clone();
let form = form.clone(); move |this| {
move |this| control.update(this.char_count(), form.text().len()) control.update(this.char_count(), {
this.text(&this.start_iter(), &this.end_iter(), true).len()
})
}
}); });
control.upload.connect_clicked({ control.upload.connect_clicked(move |this| {
let form = form.clone(); this.set_uploading();
let header = header.clone(); callback(
move |this| { header.take(),
this.set_uploading(); Bytes::from(form.text().as_bytes()),
let header = header.take(); Box::new({
callback( let this = this.clone();
Header { move || this.set_resend() // on failure
mime: match header.mime { }),
Some(mime) => Some(mime), )
None => Some("text/plain".into()), // server may reject the request without content type
},
token: header.token,
},
Bytes::from(form.text().as_bytes()),
Box::new({
let this = this.clone();
move || this.set_resend() // on failure
}),
)
}
}); });
g_box g_box