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

View File

@ -9,13 +9,6 @@ pub struct Header {
}
impl Header {
pub fn new() -> Self {
Self {
mime: None,
token: None,
}
}
/// Show header options dialog for the referrer `widget`
/// * 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) {

View File

@ -13,69 +13,59 @@ use gtk::{
use std::cell::Cell;
use std::rc::Rc;
const MARGIN: i32 = 8;
const SPACING: i32 = 8;
pub trait Text {
fn text(
header: &Rc<Cell<Header>>,
callback: impl Fn(Header, Bytes, Box<dyn Fn()>) + 'static,
) -> Self;
fn text(callback: impl Fn(Header, Bytes, Box<dyn Fn()>) + 'static) -> Self;
}
impl Text for gtk::Box {
fn text(
header: &Rc<Cell<Header>>,
callback: impl Fn(Header, Bytes, Box<dyn Fn()>) + 'static,
) -> Self {
fn text(callback: impl Fn(Header, Bytes, Box<dyn Fn()>) + 'static) -> Self {
// 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();
//header.take().dialog();
// Init widget
let g_box = gtk::Box::builder()
.margin_bottom(MARGIN)
.margin_end(MARGIN)
.margin_start(MARGIN)
.orientation(Orientation::Vertical)
.spacing(SPACING)
//.margin_top(MARGIN)
.build();
let g_box = {
const MARGIN: i32 = 8;
const SPACING: i32 = 8;
g_box.append(&form);
g_box.append(&control.g_box);
let g_box = gtk::Box::builder()
.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
form.buffer().connect_changed({
let control = control.clone();
let form = form.clone();
move |this| control.update(this.char_count(), form.text().len())
move |this| {
control.update(this.char_count(), {
this.text(&this.start_iter(), &this.end_iter(), true).len()
})
}
});
control.upload.connect_clicked({
let form = form.clone();
let header = header.clone();
move |this| {
this.set_uploading();
let header = header.take();
callback(
Header {
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
}),
)
}
control.upload.connect_clicked(move |this| {
this.set_uploading();
callback(
header.take(),
Bytes::from(form.text().as_bytes()),
Box::new({
let this = this.clone();
move || this.set_resend() // on failure
}),
)
});
g_box