connect upload action

This commit is contained in:
yggverse 2025-02-07 20:16:09 +02:00
parent 41d30f82fd
commit c90ec34ccc
3 changed files with 37 additions and 8 deletions

View file

@ -17,7 +17,7 @@ pub trait Titan {
impl Titan for gtk::Box { impl Titan for gtk::Box {
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::{glib::uuid_string_random, Box, Label, TextView}; use gtk::{glib::uuid_string_random, prelude::ButtonExt, Label, TextView};
use std::{cell::Cell, rc::Rc}; use std::{cell::Cell, rc::Rc};
// Init components // Init components
@ -26,9 +26,8 @@ impl Titan for gtk::Box {
token: None, token: None,
})); }));
let control = Rc::new(Control::build(&header)); let control = Rc::new(Control::build(&header));
let file = Rc::new(File::build(&control));
let text = TextView::text(&control); let text = TextView::text(&control);
let file = File::build(&control);
let notebook = { let notebook = {
let notebook = Notebook::builder() let notebook = Notebook::builder()
@ -41,6 +40,7 @@ impl Titan for gtk::Box {
notebook.connect_switch_page({ notebook.connect_switch_page({
let control = control.clone(); let control = control.clone();
let file = file.clone();
let text = text.clone(); let text = text.clone();
move |_, _, i| { move |_, _, i| {
if i == 0 { if i == 0 {
@ -57,7 +57,7 @@ impl Titan for gtk::Box {
// Init main widget // Init main widget
let g_box = { let g_box = {
use gtk::{prelude::BoxExt, Orientation}; use gtk::{prelude::BoxExt, Box, Orientation};
let g_box = { let g_box = {
const MARGIN: i32 = 8; const MARGIN: i32 = 8;
@ -75,17 +75,22 @@ impl Titan for gtk::Box {
}; };
// Init events // Init events
/*control.upload.connect_clicked(move |this| { control.upload.connect_clicked(move |this| {
use control::Upload;
this.set_uploading(); this.set_uploading();
callback( callback(
header.take(), header.take(), // @TODO copy?
Bytes::from(form.text().as_bytes()), match notebook.current_page().unwrap() {
0 => text.to_bytes(),
1 => file.to_bytes().unwrap(),
_ => panic!(),
},
Box::new({ Box::new({
let this = this.clone(); let this = this.clone();
move || this.set_resend() // on failure move || this.set_resend() // on failure
}), }),
) )
});*/ });
g_box g_box
} }

View file

@ -80,6 +80,17 @@ impl File {
Self { buffer, button } Self { buffer, button }
} }
/* this method is less-expensive but not useful as user
will not able re-upload existing form on failure @TODO
pub fn take_bytes(&self) -> Option<Bytes> {
self.buffer.borrow_mut().take()
} */
pub fn to_bytes(&self) -> Option<Bytes> {
self.buffer.borrow().as_ref().map(|bytes| bytes.clone())
}
pub fn size(&self) -> Option<usize> { pub fn size(&self) -> Option<usize> {
self.buffer.borrow().as_ref().map(|bytes| bytes.len()) self.buffer.borrow().as_ref().map(|bytes| bytes.len())
} }

View file

@ -2,6 +2,7 @@ mod form;
use super::Control; use super::Control;
use gtk::{ use gtk::{
glib::{Bytes, GString},
prelude::{TextBufferExt, TextViewExt}, prelude::{TextBufferExt, TextViewExt},
TextView, TextView,
}; };
@ -9,6 +10,8 @@ use std::rc::Rc;
pub trait Text { pub trait Text {
fn text(control: &Rc<Control>) -> Self; fn text(control: &Rc<Control>) -> Self;
fn to_bytes(&self) -> Bytes;
fn to_gstring(&self) -> GString;
fn len(&self) -> usize; fn len(&self) -> usize;
fn count(&self) -> i32; fn count(&self) -> i32;
} }
@ -28,6 +31,16 @@ impl Text for TextView {
text_view text_view
} }
fn to_bytes(&self) -> Bytes {
Bytes::from(self.to_gstring().as_bytes())
}
fn to_gstring(&self) -> GString {
let buffer = self.buffer();
self.buffer()
.text(&buffer.start_iter(), &buffer.end_iter(), true)
}
fn count(&self) -> i32 { fn count(&self) -> i32 {
self.buffer().char_count() self.buffer().char_count()
} }