implement show_with_delay option

This commit is contained in:
yggverse 2024-10-30 03:55:39 +02:00
parent 0912f2c68d
commit a336b18ac3
5 changed files with 51 additions and 9 deletions

View file

@ -29,7 +29,7 @@ use gtk::{
Box, Box,
}; };
use sqlite::Transaction; use sqlite::Transaction;
use std::{cell::RefCell, sync::Arc}; use std::{cell::RefCell, sync::Arc, time::Duration};
pub struct Page { pub struct Page {
id: GString, id: GString,
@ -548,7 +548,8 @@ impl Page {
// Final image size unknown, show loading widget // Final image size unknown, show loading widget
let status = content.set_status_loading( let status = content.set_status_loading(
Some(&"Loading.."), Some(&"Loading.."),
None None,
Some(Duration::from_secs(1)) // show if download time > 1 second
); );
// Asynchronously move `InputStream` data from `SocketConnection` into the local `MemoryInputStream` // Asynchronously move `InputStream` data from `SocketConnection` into the local `MemoryInputStream`

View file

@ -13,6 +13,8 @@ use gtk::{
prelude::{BoxExt, WidgetExt}, prelude::{BoxExt, WidgetExt},
Box, Orientation, Box, Orientation,
}; };
use std::time::Duration;
pub struct Content { pub struct Content {
// GTK // GTK
gobject: Box, gobject: Box,
@ -46,9 +48,14 @@ impl Content {
} }
/// Loading placeholder /// Loading placeholder
pub fn set_status_loading(&self, title: Option<&str>, description: Option<&str>) -> Status { pub fn set_status_loading(
&self,
title: Option<&str>,
description: Option<&str>,
show_with_delay: Option<Duration>,
) -> Status {
self.clean(); self.clean();
let status = Status::new_loading(title, description); let status = Status::new_loading(title, description, show_with_delay);
self.gobject.append(status.gobject()); self.gobject.append(status.gobject());
status status
} }

View file

@ -5,6 +5,7 @@ use failure::Failure;
use loading::Loading; use loading::Loading;
use adw::StatusPage; use adw::StatusPage;
use std::time::Duration;
pub struct Status { pub struct Status {
gobject: StatusPage, gobject: StatusPage,
@ -25,9 +26,15 @@ impl Status {
/// Create new loading preset /// Create new loading preset
/// ///
/// Useful as placeholder widget for async operations /// Useful as placeholder widget for async operations
pub fn new_loading(title: Option<&str>, description: Option<&str>) -> Self { pub fn new_loading(
title: Option<&str>,
description: Option<&str>,
show_with_delay: Option<Duration>,
) -> Self {
Self { Self {
gobject: Loading::new(title, description).gobject().clone(), gobject: Loading::new(title, description, show_with_delay)
.gobject()
.clone(),
} }
} }

View file

@ -2,15 +2,20 @@ mod widget;
use widget::Widget; use widget::Widget;
use adw::StatusPage; use adw::StatusPage;
use std::time::Duration;
pub struct Loading { pub struct Loading {
widget: Widget, widget: Widget,
} }
impl Loading { impl Loading {
pub fn new(title: Option<&str>, description: Option<&str>) -> Self { pub fn new(
title: Option<&str>,
description: Option<&str>,
show_with_delay: Option<Duration>,
) -> Self {
Self { Self {
widget: Widget::new(title, description), widget: Widget::new(title, description, show_with_delay),
} }
} }

View file

@ -1,4 +1,9 @@
use adw::{Spinner, StatusPage}; use adw::{Spinner, StatusPage};
use gtk::{
glib::{timeout_add_local, ControlFlow},
prelude::WidgetExt,
};
use std::time::Duration;
/// 16-64 (px) /// 16-64 (px)
const SPINNER_SIZE: i32 = 64; const SPINNER_SIZE: i32 = 64;
@ -11,7 +16,13 @@ impl Widget {
// Constructors // Constructors
/// Create new default widget configuration with options /// Create new default widget configuration with options
pub fn new(title: Option<&str>, description: Option<&str>) -> Self { ///
/// * use `show_with_delay` option on loading not take a while
pub fn new(
title: Option<&str>,
description: Option<&str>,
show_with_delay: Option<Duration>,
) -> Self {
let gobject = StatusPage::builder() let gobject = StatusPage::builder()
.child( .child(
&Spinner::builder() &Spinner::builder()
@ -27,6 +38,17 @@ impl Widget {
gobject.set_description(description); gobject.set_description(description);
if let Some(duration) = show_with_delay {
gobject.set_visible(false);
timeout_add_local(duration, {
let this = gobject.clone();
move || {
this.set_visible(true);
ControlFlow::Break
}
});
}
Self { gobject } Self { gobject }
} }