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

@ -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 }
}