implement resend form ability on request failure

This commit is contained in:
yggverse 2025-01-31 20:06:52 +02:00
parent c4e8f2ff61
commit afc33c1a03
8 changed files with 406 additions and 445 deletions

View file

@ -73,13 +73,14 @@ impl Gemini {
self.redirects.clone(), self.redirects.clone(),
feature, feature,
cancellable, cancellable,
None,
), ),
"titan" => { "titan" => {
self.page.input.set_new_titan({ self.page.input.set_new_titan({
let client = self.client.clone(); let client = self.client.clone();
let page = self.page.clone(); let page = self.page.clone();
let redirects = self.redirects.clone(); let redirects = self.redirects.clone();
move |data, _label| { move |data, on_failure| {
handle( handle(
Request::Titan { Request::Titan {
uri: uri.clone(), uri: uri.clone(),
@ -93,27 +94,8 @@ impl Gemini {
redirects.clone(), redirects.clone(),
feature.clone(), feature.clone(),
cancellable.clone(), cancellable.clone(),
Some(on_failure),
) )
// init data to send
/* @TODO
use plurify::ns as plural;
const CHUNK: usize = 0x400;
let bytes_sent = 0;
let bytes_total = data.len();
// send by chunks for large content size
if bytes_total > CHUNK {
label.set_label(&format!(
"sent {}/{} {}",
format_bytes(bytes_sent),
format_bytes(bytes_total),
plural(bytes_sent, &["byte", "bytes", "bytes"])
));
} else {
label.set_visible(false);
}
todo!()*/
} }
}); });
self.page.set_title("Titan input"); self.page.set_title("Titan input");
@ -131,6 +113,7 @@ fn handle(
redirects: Rc<Cell<usize>>, redirects: Rc<Cell<usize>>,
feature: Rc<Feature>, feature: Rc<Feature>,
cancellable: Cancellable, cancellable: Cancellable,
on_failure: Option<Box<dyn Fn()>>,
) { ) {
let uri = request.uri().clone(); let uri = request.uri().clone();
client.request_async( client.request_async(
@ -153,13 +136,7 @@ fn handle(
{ {
let page = page.clone(); let page = page.clone();
let redirects = redirects.clone(); let redirects = redirects.clone();
move |result| { move |result| match result {
// Remove input forms when redirection expected has not been applied (e.g. failure status)
// @TODO implement input data recovery on error (it's also available before unset, but reference lost at this point)
page.input.unset();
// Begin result handle
match result {
Ok(response) => { Ok(response) => {
match response.meta.status { match response.meta.status {
// https://geminiprotocol.net/docs/protocol-specification.gmi#input-expected // https://geminiprotocol.net/docs/protocol-specification.gmi#input-expected
@ -450,6 +427,7 @@ fn handle(
} }
error => { error => {
let status = page.content.to_status_failure(); let status = page.content.to_status_failure();
status.set_description( status.set_description(
Some(&match response.meta.data { Some(&match response.meta.data {
Some(message) => message.to_string(), Some(message) => message.to_string(),
@ -459,6 +437,10 @@ fn handle(
page.set_progress(0.0); page.set_progress(0.0);
page.set_title(&status.title()); page.set_title(&status.title());
redirects.replace(0); // reset redirects.replace(0); // reset
if let Some(callback) = on_failure {
callback()
}
}, },
} }
} }
@ -470,7 +452,6 @@ fn handle(
redirects.replace(0); // reset redirects.replace(0); // reset
} }
} }
}
}, },
) )
} }

View file

@ -4,7 +4,7 @@ mod titan;
use super::ItemAction; use super::ItemAction;
use adw::Clamp; use adw::Clamp;
use gtk::{glib::Uri, prelude::WidgetExt, Box, Label}; use gtk::{glib::Uri, prelude::WidgetExt};
use response::Response; use response::Response;
use sensitive::Sensitive; use sensitive::Sensitive;
use std::rc::Rc; use std::rc::Rc;
@ -40,7 +40,7 @@ impl Input {
self.update(None); self.update(None);
} }
pub fn update(&self, child: Option<&Box>) { pub fn update(&self, child: Option<&gtk::Box>) {
if child.is_some() { if child.is_some() {
self.clamp.set_visible(true); // widget may be hidden, make it visible to child redraw self.clamp.set_visible(true); // widget may be hidden, make it visible to child redraw
self.clamp.set_child(child); self.clamp.set_child(child);
@ -74,7 +74,7 @@ impl Input {
)); ));
} }
pub fn set_new_titan(&self, on_send: impl Fn(&[u8], &Label) + 'static) { pub fn set_new_titan(&self, on_send: impl Fn(&[u8], Box<dyn Fn()>) + 'static) {
self.update(Some(&Titan::build(on_send).g_box)); self.update(Some(&gtk::Box::titan(on_send)));
} }
} }

View file

@ -3,36 +3,31 @@ mod form;
mod title; mod title;
use control::Control; use control::Control;
use control::Send;
use form::Form; use form::Form;
use title::Title; use gtk::{
prelude::{BoxExt, ButtonExt, TextBufferExt, TextViewExt},
use gtk::{gio::SimpleAction, glib::uuid_string_random, prelude::BoxExt, Box, Label, Orientation}; Label, Orientation, TextView,
};
use std::rc::Rc; use std::rc::Rc;
use title::Title;
const MARGIN: i32 = 6; const MARGIN: i32 = 6;
const SPACING: i32 = 8; const SPACING: i32 = 8;
pub struct Titan { pub trait Titan {
// Components fn titan(callback: impl Fn(&[u8], Box<dyn Fn()>) + 'static) -> Self;
pub g_box: Box,
} }
impl Titan { impl Titan for gtk::Box {
// Constructors fn titan(callback: impl Fn(&[u8], Box<dyn Fn()>) + 'static) -> Self {
/// Build new `Self`
pub fn build(on_send: impl Fn(&[u8], &Label) + 'static) -> Self {
// Init local actions
let action_update = SimpleAction::new(&uuid_string_random(), None);
let action_send = SimpleAction::new(&uuid_string_random(), None);
// Init components // Init components
let control = Rc::new(Control::build(action_send.clone())); let control = Rc::new(Control::build());
let form = Rc::new(Form::build(action_update.clone())); let form = TextView::form();
let title = Title::build(None); let title = Label::title(None);
// Init widget // Init widget
let g_box = Box::builder() let g_box = gtk::Box::builder()
.margin_bottom(MARGIN) .margin_bottom(MARGIN)
.margin_end(MARGIN) .margin_end(MARGIN)
.margin_start(MARGIN) .margin_start(MARGIN)
@ -41,21 +36,31 @@ impl Titan {
.orientation(Orientation::Vertical) .orientation(Orientation::Vertical)
.build(); .build();
g_box.append(&title.label); g_box.append(&title);
g_box.append(&form.text_view); g_box.append(&form);
g_box.append(&control.g_box); g_box.append(&control.g_box);
// Init events // Connect events
action_update.connect_activate({ control.send.connect_clicked({
let control = control.clone();
let form = form.clone(); let form = form.clone();
move |_, _| control.update(Some(form.text().len())) move |this| {
this.set_sending();
callback(
form.text().as_bytes(),
Box::new({
let this = this.clone();
move || this.set_resend() // on failure
}),
)
}
}); });
action_send form.buffer().connect_changed({
.connect_activate(move |_, _| on_send(form.text().as_bytes(), &control.counter.label)); let control = control.clone();
move |this| control.update(Some(this.char_count()))
});
// Return activated struct // Return activated `Self`
Self { g_box } g_box
} }
} }

View file

@ -2,16 +2,17 @@ mod counter;
mod send; mod send;
use counter::Counter; use counter::Counter;
use gtk::gio::SimpleAction; use gtk::{
use gtk::{prelude::BoxExt, Align, Box, Orientation}; prelude::{BoxExt, WidgetExt},
use send::Send; Align, Box, Button, Label, Orientation,
use std::rc::Rc; };
pub use send::Send;
const SPACING: i32 = 8; const SPACING: i32 = 8;
pub struct Control { pub struct Control {
pub counter: Rc<Counter>, pub counter: Label,
pub send: Rc<Send>, pub send: Button,
pub g_box: Box, pub g_box: Box,
} }
@ -19,10 +20,10 @@ impl Control {
// Constructors // Constructors
/// Build new `Self` /// Build new `Self`
pub fn build(action_send: SimpleAction) -> Self { pub fn build() -> Self {
// Init components // Init components
let counter = Rc::new(Counter::new()); let counter = Label::counter();
let send = Rc::new(Send::build(action_send)); let send = Button::send();
// Init main widget // Init main widget
let g_box = Box::builder() let g_box = Box::builder()
@ -31,8 +32,8 @@ impl Control {
.spacing(SPACING) .spacing(SPACING)
.build(); .build();
g_box.append(&counter.label); g_box.append(&counter);
g_box.append(&send.button); g_box.append(&send);
// Return activated struct // Return activated struct
Self { Self {
@ -43,10 +44,10 @@ impl Control {
} }
// Actions // Actions
pub fn update(&self, bytes_total: Option<usize>) { pub fn update(&self, char_count: Option<i32>) {
// Update children components // Update children components
self.counter.update(bytes_total); self.counter.update(char_count);
self.send.update(match bytes_total { self.send.set_sensitive(match char_count {
Some(total) => total > 0, Some(total) => total > 0,
None => false, None => false,
}); });

View file

@ -1,31 +1,26 @@
use gtk::{prelude::WidgetExt, Label}; use gtk::{prelude::WidgetExt, Label};
pub struct Counter { pub trait Counter {
pub label: Label, fn counter() -> Self;
fn update(&self, char_count: Option<i32>);
} }
impl Default for Counter { impl Counter for Label {
fn default() -> Self { // Constructors
Self::new()
}
}
impl Counter { fn counter() -> Self {
// Construct Label::builder().css_classes(["dim-label"]).build() // @TODO use `dimmed` in Adw 1.6,
pub fn new() -> Self {
Self {
label: Label::builder().css_classes(["dim-label"]).build(), // @TODO use `dimmed` in Adw 1.6,
}
} }
// Actions // Actions
pub fn update(&self, bytes_total: Option<usize>) {
match bytes_total { fn update(&self, char_count: Option<i32>) {
match char_count {
Some(value) => { Some(value) => {
self.label.set_label(&value.to_string()); self.set_label(&value.to_string());
self.label.set_visible(value > 0); self.set_visible(value > 0);
} }
None => self.label.set_visible(false), None => self.set_visible(false),
} }
} }
} }

View file

@ -1,40 +1,28 @@
use gtk::{ use gtk::{
gio::SimpleAction, prelude::{ButtonExt, WidgetExt},
prelude::{ActionExt, ButtonExt, WidgetExt},
Button, Button,
}; };
pub struct Send { pub trait Send {
pub button: Button, fn send() -> Self;
fn set_sending(&self);
fn set_resend(&self);
} }
impl Send { impl Send for Button {
// Constructors fn send() -> Self {
Button::builder()
/// Build new `Self`
pub fn build(action_send: SimpleAction) -> Self {
// Init main widget
let button = Button::builder()
.css_classes(["accent"]) // | `suggested-action` .css_classes(["accent"]) // | `suggested-action`
.label("Send") .label("Send")
.sensitive(false) .sensitive(false)
.build(); .build()
// Init events
button.connect_clicked({
move |this| {
this.set_sensitive(false);
this.set_label("sending..");
action_send.activate(None);
} }
}); fn set_sending(&self) {
self.set_sensitive(false);
// Return activated `Self` self.set_label("sending..");
Self { button }
} }
fn set_resend(&self) {
// Actions self.set_sensitive(true);
pub fn update(&self, is_sensitive: bool) { self.set_label("Resend");
self.button.set_sensitive(is_sensitive);
} }
} }

View file

@ -1,7 +1,6 @@
use gtk::{ use gtk::{
gio::SimpleAction,
glib::GString, glib::GString,
prelude::{ActionExt, TextBufferExt, TextViewExt, WidgetExt}, prelude::{TextBufferExt, TextViewExt, WidgetExt},
TextView, WrapMode, TextView, WrapMode,
}; };
use libspelling::{Checker, TextBufferAdapter}; use libspelling::{Checker, TextBufferAdapter};
@ -9,15 +8,16 @@ use sourceview::Buffer;
const MARGIN: i32 = 8; const MARGIN: i32 = 8;
pub struct Form { pub trait Form {
pub text_view: TextView, fn form() -> Self;
fn text(&self) -> GString;
} }
impl Form { impl Form for TextView {
// Constructors // Constructors
/// Build new `Self` /// Build new `Self`
pub fn build(action_update: SimpleAction) -> Self { fn form() -> Self {
// Init [SourceView](https://gitlab.gnome.org/GNOME/gtksourceview) type buffer // Init [SourceView](https://gitlab.gnome.org/GNOME/gtksourceview) type buffer
let buffer = Buffer::builder().build(); let buffer = Buffer::builder().build();
@ -43,22 +43,18 @@ impl Form {
text_view.set_size_request(-1, 38); // @TODO [#635](https://gitlab.gnome.org/GNOME/pygobject/-/issues/635) text_view.set_size_request(-1, 38); // @TODO [#635](https://gitlab.gnome.org/GNOME/pygobject/-/issues/635)
// Init events // Init events
text_view.buffer().connect_changed(move |_| {
action_update.activate(None);
});
text_view.connect_realize(|this| { text_view.connect_realize(|this| {
this.grab_focus(); this.grab_focus();
}); });
// Return activated `Self` // Return activated `Self`
Self { text_view } text_view
} }
// Getters // Getters
pub fn text(&self) -> GString { fn text(&self) -> GString {
let buffer = self.text_view.buffer(); let buffer = self.buffer();
buffer.text(&buffer.start_iter(), &buffer.end_iter(), true) buffer.text(&buffer.start_iter(), &buffer.end_iter(), true)
} }
} }

View file

@ -1,20 +1,15 @@
use gtk::{Align, Label}; use gtk::{Align, Label};
pub struct Title { pub trait Title {
pub label: Label, fn title(title: Option<&str>) -> Self;
} }
impl Title { impl Title for Label {
// Constructors fn title(title: Option<&str>) -> Self {
Label::builder()
/// Build new `Self`
pub fn build(title: Option<&str>) -> Self {
Self {
label: Label::builder()
.css_classes(["heading"]) .css_classes(["heading"])
.halign(Align::Start) .halign(Align::Start)
.label(title.unwrap_or("Titan input")) .label(title.unwrap_or("Titan input"))
.build(), .build()
}
} }
} }