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,
};
use sqlite::Transaction;
use std::{cell::RefCell, sync::Arc};
use std::{cell::RefCell, sync::Arc, time::Duration};
pub struct Page {
id: GString,
@ -548,7 +548,8 @@ impl Page {
// Final image size unknown, show loading widget
let status = content.set_status_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`

View File

@ -13,6 +13,8 @@ use gtk::{
prelude::{BoxExt, WidgetExt},
Box, Orientation,
};
use std::time::Duration;
pub struct Content {
// GTK
gobject: Box,
@ -46,9 +48,14 @@ impl Content {
}
/// 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();
let status = Status::new_loading(title, description);
let status = Status::new_loading(title, description, show_with_delay);
self.gobject.append(status.gobject());
status
}

View File

@ -5,6 +5,7 @@ use failure::Failure;
use loading::Loading;
use adw::StatusPage;
use std::time::Duration;
pub struct Status {
gobject: StatusPage,
@ -25,9 +26,15 @@ impl Status {
/// Create new loading preset
///
/// 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 {
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 adw::StatusPage;
use std::time::Duration;
pub struct Loading {
widget: Widget,
}
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 {
widget: Widget::new(title, description),
widget: Widget::new(title, description, show_with_delay),
}
}

View File

@ -1,4 +1,9 @@
use adw::{Spinner, StatusPage};
use gtk::{
glib::{timeout_add_local, ControlFlow},
prelude::WidgetExt,
};
use std::time::Duration;
/// 16-64 (px)
const SPINNER_SIZE: i32 = 64;
@ -11,7 +16,13 @@ impl Widget {
// Constructors
/// 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()
.child(
&Spinner::builder()
@ -27,6 +38,17 @@ impl Widget {
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 }
}