diff --git a/src/app/browser/window/tab/item/page/input/titan.rs b/src/app/browser/window/tab/item/page/input/titan.rs index c0c7369d..a286b59d 100644 --- a/src/app/browser/window/tab/item/page/input/titan.rs +++ b/src/app/browser/window/tab/item/page/input/titan.rs @@ -17,7 +17,7 @@ pub trait Titan { impl Titan for gtk::Box { fn titan(callback: impl Fn(Header, Bytes, Box) + '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}; // Init components @@ -26,9 +26,8 @@ impl Titan for gtk::Box { token: None, })); let control = Rc::new(Control::build(&header)); - + let file = Rc::new(File::build(&control)); let text = TextView::text(&control); - let file = File::build(&control); let notebook = { let notebook = Notebook::builder() @@ -41,6 +40,7 @@ impl Titan for gtk::Box { notebook.connect_switch_page({ let control = control.clone(); + let file = file.clone(); let text = text.clone(); move |_, _, i| { if i == 0 { @@ -57,7 +57,7 @@ impl Titan for gtk::Box { // Init main widget let g_box = { - use gtk::{prelude::BoxExt, Orientation}; + use gtk::{prelude::BoxExt, Box, Orientation}; let g_box = { const MARGIN: i32 = 8; @@ -75,17 +75,22 @@ impl Titan for gtk::Box { }; // Init events - /*control.upload.connect_clicked(move |this| { + control.upload.connect_clicked(move |this| { + use control::Upload; this.set_uploading(); callback( - header.take(), - Bytes::from(form.text().as_bytes()), + header.take(), // @TODO copy? + match notebook.current_page().unwrap() { + 0 => text.to_bytes(), + 1 => file.to_bytes().unwrap(), + _ => panic!(), + }, Box::new({ let this = this.clone(); move || this.set_resend() // on failure }), ) - });*/ + }); g_box } diff --git a/src/app/browser/window/tab/item/page/input/titan/file.rs b/src/app/browser/window/tab/item/page/input/titan/file.rs index df78163c..fdc3bc8c 100644 --- a/src/app/browser/window/tab/item/page/input/titan/file.rs +++ b/src/app/browser/window/tab/item/page/input/titan/file.rs @@ -80,6 +80,17 @@ impl File { 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 { + self.buffer.borrow_mut().take() + } */ + + pub fn to_bytes(&self) -> Option { + self.buffer.borrow().as_ref().map(|bytes| bytes.clone()) + } + pub fn size(&self) -> Option { self.buffer.borrow().as_ref().map(|bytes| bytes.len()) } diff --git a/src/app/browser/window/tab/item/page/input/titan/text.rs b/src/app/browser/window/tab/item/page/input/titan/text.rs index 4878e075..2f72591f 100644 --- a/src/app/browser/window/tab/item/page/input/titan/text.rs +++ b/src/app/browser/window/tab/item/page/input/titan/text.rs @@ -2,6 +2,7 @@ mod form; use super::Control; use gtk::{ + glib::{Bytes, GString}, prelude::{TextBufferExt, TextViewExt}, TextView, }; @@ -9,6 +10,8 @@ use std::rc::Rc; pub trait Text { fn text(control: &Rc) -> Self; + fn to_bytes(&self) -> Bytes; + fn to_gstring(&self) -> GString; fn len(&self) -> usize; fn count(&self) -> i32; } @@ -28,6 +31,16 @@ impl Text for TextView { 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 { self.buffer().char_count() }