From 1f52521ecd6bd03e96055bdb37a0f0ebd7ba020b Mon Sep 17 00:00:00 2001 From: mg <102437792+mg-dev25@users.noreply.github.com> Date: Thu, 4 Dec 2025 22:44:08 +0100 Subject: [PATCH] feat(timeout): Add --timeout-elapsed for seamless dialog chaining Adds a new --timeout-elapsed flag that allows progress bar continuation across chained dialogs by specifying how much time has already elapsed. Changes: - Add timeout_elapsed field to YadData struct (yad.h) - Register --timeout-elapsed CLI option (option.c) - Update timeout_cb to initialize from elapsed value (main.c) - Calculate initial progress bar fraction from elapsed time - Update remaining time label to account for elapsed time Usage: # First dialog yad --timeout=30 --timeout-indicator=bottom # Second dialog continuing same timeout (10s elapsed) yad --timeout=30 --timeout-elapsed=10 --timeout-indicator=bottom The progress bar and remaining time label will start at the correct position based on the elapsed time, enabling smooth visual continuity when chaining multiple yad dialogs with a shared timeout budget. --- src/main.c | 27 ++++++++++++++++++++++++--- src/option.c | 3 +++ src/yad.h | 1 + 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index edf6ebf..d518f11 100644 --- a/src/main.c +++ b/src/main.c @@ -129,9 +129,17 @@ btn_cb (GtkWidget *b, gchar *cmd) static gboolean timeout_cb (gpointer data) { - static guint count = 1; + static guint count = 0; + static gboolean initialized = FALSE; GtkWidget *w = (GtkWidget *) data; + /* Initialize count only once, accounting for elapsed time from previous dialog */ + if (!initialized) + { + count = options.data.timeout_elapsed + 1; + initialized = TRUE; + } + if (options.data.timeout < count) { yad_exit (YAD_RESPONSE_TIMEOUT); @@ -463,8 +471,15 @@ create_dialog (void) if (G_LIKELY (options.data.to_indicator) && strcasecmp (options.data.to_indicator, "none") != 0) { + gdouble initial_fraction = 1.0; + topb = gtk_progress_bar_new (); - gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (topb), 1.0); + + /* Calculate initial fraction accounting for elapsed time */ + if (options.data.timeout_elapsed > 0 && options.data.timeout_elapsed < options.data.timeout) + initial_fraction = (gdouble)(options.data.timeout - options.data.timeout_elapsed) / (gdouble)options.data.timeout; + + gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (topb), initial_fraction); gtk_widget_set_name (topb, "yad-timeout-indicator"); } @@ -508,7 +523,13 @@ create_dialog (void) if (SHOW_REMAIN) #endif { - gchar *lbl = g_strdup_printf (_("%d sec"), options.data.timeout); + guint remaining = options.data.timeout; + + /* Account for elapsed time in initial label */ + if (options.data.timeout_elapsed > 0 && options.data.timeout_elapsed < options.data.timeout) + remaining = options.data.timeout - options.data.timeout_elapsed; + + gchar *lbl = g_strdup_printf (_("%d sec"), remaining); gtk_progress_bar_set_show_text (GTK_PROGRESS_BAR (topb), TRUE); gtk_progress_bar_set_text (GTK_PROGRESS_BAR (topb), lbl); g_free (lbl); diff --git a/src/option.c b/src/option.c index 11e48e5..c4de235 100644 --- a/src/option.c +++ b/src/option.c @@ -111,6 +111,8 @@ static GOptionEntry general_options[] = { N_("Set dialog timeout in seconds"), N_("TIMEOUT") }, { "timeout-indicator", 0, 0, G_OPTION_ARG_STRING, &options.data.to_indicator, N_("Show remaining time indicator (top, bottom, left, right)"), N_("POS") }, + { "timeout-elapsed", 0, 0, G_OPTION_ARG_INT, &options.data.timeout_elapsed, + N_("Set elapsed time for timeout continuation"), N_("SECONDS") }, { "text", 0, G_OPTION_FLAG_NOALIAS, G_OPTION_ARG_STRING, &options.data.dialog_text, N_("Set the dialog text"), N_("TEXT") }, { "text-width", 0, G_OPTION_FLAG_NOALIAS, G_OPTION_ARG_INT, &options.data.text_width, @@ -1667,6 +1669,7 @@ yad_options_init (void) options.data.icon_theme = NULL; options.data.expander = NULL; options.data.timeout = 0; + options.data.timeout_elapsed = 0; options.data.to_indicator = NULL; options.data.hscroll_policy = GTK_POLICY_AUTOMATIC; options.data.vscroll_policy = GTK_POLICY_AUTOMATIC; diff --git a/src/yad.h b/src/yad.h index 649ba4f..565c20f 100644 --- a/src/yad.h +++ b/src/yad.h @@ -238,6 +238,7 @@ typedef struct { gboolean negy; gchar *geometry; guint timeout; + guint timeout_elapsed; /* Time already elapsed from previous dialog */ gchar *to_indicator; gchar *dialog_text; guint text_width;