Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
closing.c
Go to the documentation of this file.
1/*
2 This file is part of Ansel,
3 Copyright (C) 2026 Guillaume Stutin.
4
5 Ansel is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 Ansel is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Ansel. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include "gui/closing.h"
20
21#include "common/logging.h"
22#include "control/control.h"
23#include "control/jobs.h"
24#include "control/progress.h"
25#include "gui/alert.h"
26#include "gui/application.h"
27#include "system/macros.h"
28#include "system/mem_alloc.h"
29#include "widgets/dialog.h"
32
33#include <glib/gi18n.h>
34#include <gtk/gtk.h>
35
36// A quit that is over within this long shows nothing: a window that flashes says less than none.
37#define DT_CLOSING_NOTICE_DELAY (G_USEC_PER_SEC)
38// How often the workers are counted, and the window brought up to date, in milliseconds.
39#define DT_CLOSING_POLL_INTERVAL 100
40// A job's kind, as the details name it, is the queue it was added to. The reserved worker, which
41// only the darkroom uses, comes after the queues.
42#define DT_CLOSING_KIND_DARKROOM DT_JOB_QUEUE_MAX
43
44// The same window serves twice. Before the quit, it asks whether to quit while background tasks
45// are running; once the quit is under way, it waits for the running jobs.
51
52enum
53{
54 DT_CLOSING_COL_KIND, // the job's kind
55 DT_CLOSING_COL_DESCRIPTION, // the description it was created with
57};
58
59typedef struct dt_closing_t
60{
62 gboolean quit; // the answer, in DT_CLOSING_CONFIRM
63 GMainLoop *loop;
64 gint64 start;
65 dt_gui_alert_t *alert; // NULL until the window shows
66 GtkWidget *count; // how many jobs are still running
67 GtkWidget *names; // what the ones that publish a progress say they are doing
68 GtkWidget *details; // the expander that lists the running jobs, hidden while there is nothing in it
69 GtkListStore *store; // its rows, owned by the view inside it
70 gchar *listed; // what the list shows, so that it is rebuilt only when that changes
71 GtkWidget *dropped; // under the list, how many queued jobs the quit drops
72 int32_t queued; // what it says, -1 before it says anything
74
75typedef struct dt_closing_job_t
76{
77 int kind;
78 gchar *description; // translated when the catalog knows it
80
81static void _closing_answer(dt_closing_t *closing, const gboolean quit)
82{
83 closing->quit = quit;
84 g_main_loop_quit(closing->loop);
85}
86
87static void _closing_quit_clicked(dt_gui_alert_t *alert __attribute__((unused)), void *data)
88{
90}
91
92// Going back is also what closing the question, by its title bar or by Escape, answers.
93static void _closing_back_clicked(dt_gui_alert_t *alert __attribute__((unused)), void *data)
94{
96}
97
98static void _closing_window_new(dt_closing_t *closing)
99{
100 const gboolean confirm = closing->mode == DT_CLOSING_CONFIRM;
101 // Before the quit, a question over the main window. During it, a progress, which the user cannot
102 // close: closing it would stop nothing.
103 closing->alert = confirm ? dt_gui_alert_new(DT_GUI_ALERT_QUESTION, _("Quit Ansel?"), _("Tasks are still running"))
104 : dt_gui_alert_new(DT_GUI_ALERT_PROGRESS, _("Closing Ansel..."),
105 _("Ansel is finishing its work before closing"));
106 if(confirm) dt_gui_alert_set_cancel(closing->alert, _closing_back_clicked, closing);
107
108 closing->count = dt_gui_alert_add_text(closing->alert, NULL);
109
110 closing->names = dt_gui_alert_add_text(closing->alert, NULL);
111 gtk_widget_set_no_show_all(closing->names, TRUE);
112
113 // Nothing is cancelled by a quit: a running job is finished, a queued one is dropped.
114 dt_gui_alert_add_text(closing->alert, confirm ? _("If you quit now, the tasks not started yet are dropped, and Ansel "
115 "closes once the running ones are done.")
116 : _("This window closes by itself as soon as it is done."));
117
118 closing->details = gtk_expander_new(_("Details"));
119 gtk_expander_set_resize_toplevel(GTK_EXPANDER(closing->details), TRUE);
120 gtk_widget_set_no_show_all(closing->details, TRUE);
121 dt_gui_alert_add_widget(closing->alert, closing->details);
122
123 closing->store = gtk_list_store_new(DT_CLOSING_COLS, G_TYPE_STRING, G_TYPE_STRING);
124 GtkWidget *view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(closing->store));
125 g_object_unref(closing->store);
126 gtk_tree_view_set_enable_search(GTK_TREE_VIEW(view), FALSE);
127 gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)), GTK_SELECTION_NONE);
128
129 GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
130 GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes(C_("closing jobs", "Type"), renderer,
131 "text", DT_CLOSING_COL_KIND, NULL);
132 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
133
134 renderer = gtk_cell_renderer_text_new();
135 g_object_set(renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
136 column = gtk_tree_view_column_new_with_attributes(C_("closing jobs", "Description"), renderer, "text",
138 gtk_tree_view_column_set_expand(column, TRUE);
139 gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
140
141 // As tall as its rows up to a point, then it scrolls: there is one row per worker at most. The
142 // tree view is transparent, and the recessed frame of our other lists puts it on a dark ground.
143 GtkWidget *scroll = gtk_scrolled_window_new(NULL, NULL);
144 dt_gui_add_class(scroll, "dt_recessed_scroll");
145 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
146 gtk_scrolled_window_set_min_content_width(GTK_SCROLLED_WINDOW(scroll), DT_PIXEL_APPLY_DPI(420));
147 gtk_scrolled_window_set_max_content_height(GTK_SCROLLED_WINDOW(scroll), DT_PIXEL_APPLY_DPI(300));
148 gtk_scrolled_window_set_propagate_natural_height(GTK_SCROLLED_WINDOW(scroll), TRUE);
149 gtk_container_add(GTK_CONTAINER(scroll), view);
150
151 closing->dropped = gtk_label_new(NULL);
152 gtk_label_set_xalign(GTK_LABEL(closing->dropped), 0.0);
153 gtk_widget_set_no_show_all(closing->dropped, TRUE);
154
155 GtkWidget *content = gtk_box_new(GTK_ORIENTATION_VERTICAL, DT_PIXEL_APPLY_DPI(6));
156 gtk_widget_set_margin_top(content, DT_PIXEL_APPLY_DPI(6));
157 gtk_box_pack_start(GTK_BOX(content), scroll, FALSE, FALSE, 0);
158 gtk_box_pack_start(GTK_BOX(content), closing->dropped, FALSE, FALSE, 0);
159 gtk_container_add(GTK_CONTAINER(closing->details), content);
160 // Shown by hand: the expander's no_show_all keeps show_all from reaching them. The count of
161 // dropped jobs keeps its own, and shows only when there are some.
162 gtk_widget_show_all(content);
163
164 if(confirm)
165 {
166 // Going back is the default, added first: Enter does not quit.
167 dt_gui_alert_add_button(closing->alert, _("Go back"), _closing_back_clicked, closing);
168 dt_gui_alert_add_button(closing->alert, _("Quit anyway"), _closing_quit_clicked, closing);
169 }
170
171 dt_gui_alert_show(closing->alert);
172}
173
174// Pango markup: the names are in italics, set apart from the sentences around them.
175static void _closing_append_name(const gchar *message, void *data)
176{
177 GString *names = (GString *)data;
178 if(names->len > 0) g_string_append_c(names, '\n');
179 gchar *name = g_markup_printf_escaped("<i>%s</i>", message);
180 g_string_append(names, name);
181 dt_free(name);
182}
183
184static const char *_closing_kind_name(const int kind)
185{
186 switch(kind)
187 {
189 return C_("job type", "Image operation");
191 return C_("job type", "Thumbnail");
193 return C_("job type", "Background task");
194 case DT_JOB_QUEUE_USER_EXPORT: // prints share this queue: their description says so
195 return C_("job type", "Export");
197 return C_("job type", "Maintenance");
198 default:
199 return C_("job type", "Darkroom rendering");
200 }
201}
202
203static void _closing_job_clear(gpointer data)
204{
206 dt_free(job->description);
207}
208
209static void _closing_collect_job(const char *description, const dt_job_queue_t queue, const gboolean reserved,
210 void *data)
211{
212 GArray *running = (GArray *)data;
213 // Descriptions are written for the debug log, in English. Some are also the catalog's
214 // strings, those the generic image jobs show on their progress bar.
215 const dt_closing_job_t job = { .kind = reserved ? DT_CLOSING_KIND_DARKROOM : (int)queue,
216 .description = g_strdup(_(description)) };
217 g_array_append_val(running, job);
218}
219
220// One row per running job. The queued ones are only counted, under the list: a quit drops them,
221// unrun.
223{
224 GArray *running = g_array_new(FALSE, FALSE, sizeof(dt_closing_job_t));
225 g_array_set_clear_func(running, _closing_job_clear);
227
228 // Before the quit, the list says what the question counts: not the darkroom's service loop,
229 // which is not counted (see _closing_count_job()). Walk backwards so removals keep the indices.
230 if(closing->mode == DT_CLOSING_CONFIRM)
231 for(guint i = running->len; i > 0; i--)
232 if(g_array_index(running, dt_closing_job_t, i - 1).kind == DT_CLOSING_KIND_DARKROOM)
233 g_array_remove_index(running, i - 1);
234
235 GString *listed = g_string_new(NULL);
236 for(guint i = 0; i < running->len; i++)
237 {
238 const dt_closing_job_t *job = &g_array_index(running, dt_closing_job_t, i);
239 g_string_append_printf(listed, "%d\t%s\n", job->kind, job->description);
240 }
241
242 if(g_strcmp0(listed->str, closing->listed) != 0)
243 {
244 gtk_list_store_clear(closing->store);
245 for(guint i = 0; i < running->len; i++)
246 {
247 const dt_closing_job_t *job = &g_array_index(running, dt_closing_job_t, i);
248 gtk_list_store_insert_with_values(closing->store, NULL, -1, DT_CLOSING_COL_KIND, _closing_kind_name(job->kind),
250 }
251 dt_free(closing->listed);
252 closing->listed = g_string_free(listed, FALSE);
253 }
254 else
255 g_string_free(listed, TRUE);
256
257 const int32_t queued = dt_control_queued_jobs_count(dt_control_get_global());
258 if(queued != closing->queued)
259 {
260 gchar *dropped = (closing->mode == DT_CLOSING_CONFIRM)
261 ? g_strdup_printf(ngettext("%d other task, not started, would be dropped",
262 "%d other tasks, not started, would be dropped", queued),
263 queued)
264 : g_strdup_printf(ngettext("%d other task, not started, is dropped",
265 "%d other tasks, not started, are dropped", queued),
266 queued);
267 gtk_label_set_text(GTK_LABEL(closing->dropped), dropped);
268 dt_free(dropped);
269 gtk_widget_set_visible(closing->dropped, queued > 0);
270 closing->queued = queued;
271 }
272
273 gtk_widget_set_visible(closing->details, running->len > 0 || queued > 0);
274 g_array_free(running, TRUE);
275}
276
277static void _closing_count_job(const char *description __attribute__((unused)),
278 const dt_job_queue_t queue __attribute__((unused)), const gboolean reserved,
279 void *data)
280{
281 // The reserved worker's job is the darkroom's service loop: it runs from entering the view to
282 // leaving it, idle or not, and leaving the view is part of the quit. Counted here, it would hold
283 // the question up for as long as the darkroom is open. It is the only reserved job there is
284 // (DT_CTL_WORKER_RESERVED is 1); a second one that does real work would need telling apart.
285 if(reserved) return;
286 (*(int32_t *)data)++;
287}
288
289// The jobs running now that the quit would wait for, thumbnails included, but not the darkroom's
290// service loop, which the quit ends itself. Only the question before the quit counts them so.
291static int32_t _closing_running_jobs(void)
292{
293 int32_t running = 0;
295 return running;
296}
297
298static void _closing_refresh(dt_closing_t *closing)
299{
300 // Before the quit, the workers are all alive, idle or not: the running jobs are counted one by
301 // one. During it, the workers still alive are those jobs.
302 const int32_t running
304 gchar *count = g_strdup_printf(ngettext("%d task is still running", "%d tasks are still running", running),
305 running);
306 gtk_label_set_text(GTK_LABEL(closing->count), count);
307 dt_free(count);
308
309 GString *names = g_string_new(NULL);
311 gtk_label_set_markup(GTK_LABEL(closing->names), names->str);
312 gtk_widget_set_visible(closing->names, names->len > 0);
313 g_string_free(names, TRUE);
314
316}
317
318static gboolean _closing_poll(gpointer user_data)
319{
320 dt_closing_t *closing = (dt_closing_t *)user_data;
321 if(dt_control_workers_alive() == 0)
322 {
323 g_main_loop_quit(closing->loop);
324 return G_SOURCE_REMOVE;
325 }
326
327 if(IS_NULL_PTR(closing->alert))
328 {
329 if(g_get_monotonic_time() - closing->start < DT_CLOSING_NOTICE_DELAY) return G_SOURCE_CONTINUE;
330 _closing_window_new(closing);
331 }
332
333 _closing_refresh(closing);
334 return G_SOURCE_CONTINUE;
335}
336
337// Once nothing runs and nothing is queued, there is nothing left to ask about: the quit asked for
338// goes on, as if Quit anyway had been clicked.
339static gboolean _closing_nothing_pending(void)
340{
342}
343
344static gboolean _closing_confirm_poll(gpointer user_data)
345{
346 dt_closing_t *closing = (dt_closing_t *)user_data;
348 {
349 _closing_answer(closing, TRUE);
350 return G_SOURCE_CONTINUE; // removed by _closing_confirm()
351 }
352 _closing_refresh(closing);
353 return G_SOURCE_CONTINUE;
354}
355
356// Ask, while jobs are running or queued, whether to quit all the same. TRUE to quit. The running
357// ones are what the closing window would wait for; the queued ones, what the quit would drop.
358static gboolean _closing_confirm(void)
359{
360 if(_closing_nothing_pending()) return TRUE;
361
362 // A second request while the question is open -- the shortcut again, the dock -- does not ask
363 // it twice: the window that is up answers it.
364 static gboolean asking = FALSE;
365 if(asking) return FALSE;
366 asking = TRUE;
367
368 dt_closing_t closing = { .mode = DT_CLOSING_CONFIRM, .loop = g_main_loop_new(NULL, FALSE), .queued = -1 };
369 _closing_window_new(&closing);
370 _closing_refresh(&closing);
371 const guint poll = g_timeout_add(DT_CLOSING_POLL_INTERVAL, _closing_confirm_poll, &closing);
372 g_main_loop_run(closing.loop);
373 g_source_remove(poll);
374 g_main_loop_unref(closing.loop);
375
377 dt_free(closing.listed);
378 if(!closing.quit) dt_gui_refocus_parent(GTK_WINDOW(dt_gui_main_window()));
379
380 asking = FALSE;
381 return closing.quit;
382}
383
385{
387}
388
390{
391 if(dt_control_workers_alive() == 0) return;
392
393 // The main window is hidden and its view left, but the windows it may have left on screen are
394 // not, and nothing behind them is in a state to answer. The grab takes every input event
395 // away from them, close buttons included, for as long as this loop turns. It holds the
396 // default window group, theirs; the closing window has one of its own, and escapes it. Its
397 // widget is realised, since GTK delivers events to realised widgets only, but never shown:
398 // mapped, it would be a window on screen -- on macOS, an NSWindow of its own.
399 GtkWidget *grab = gtk_invisible_new();
400 gtk_widget_realize(grab);
401 gtk_grab_add(grab);
402
403 dt_closing_t closing = { .mode = DT_CLOSING_WAIT, .loop = g_main_loop_new(NULL, FALSE),
404 .start = g_get_monotonic_time(), .queued = -1 };
405 g_timeout_add(DT_CLOSING_POLL_INTERVAL, _closing_poll, &closing);
406 g_main_loop_run(closing.loop);
407 g_main_loop_unref(closing.loop);
408
409 gtk_grab_remove(grab);
410 gtk_widget_destroy(grab);
411
412 dt_print(DT_DEBUG_CONTROL, "[closing] waited %.2f s for the running jobs\n",
413 (g_get_monotonic_time() - closing.start) / (double)G_USEC_PER_SEC);
414
415 // Off the screen at the drain of the main context that dt_cleanup() does right after
416 // dt_control_shutdown(), whose joins no longer wait for anything.
417 if(!IS_NULL_PTR(closing.alert)) dt_gui_alert_destroy(closing.alert);
418 dt_free(closing.listed);
419}
420
421// clang-format off
422// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
423// vim: shiftwidth=2 expandtab tabstop=2 cindent
424// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
425// clang-format on
void dt_gui_alert_destroy(dt_gui_alert_t *alert)
Definition alert.c:253
dt_gui_alert_t * dt_gui_alert_new(const dt_gui_alert_kind_t kind, const char *title, const char *heading)
Definition alert.c:107
void dt_gui_alert_add_widget(dt_gui_alert_t *alert, GtkWidget *widget)
Definition alert.c:194
GtkWidget * dt_gui_alert_add_text(dt_gui_alert_t *alert, const char *text)
Definition alert.c:184
void dt_gui_alert_set_cancel(dt_gui_alert_t *alert, dt_gui_alert_callback_t callback, void *data)
Definition alert.c:230
void dt_gui_alert_add_button(dt_gui_alert_t *alert, const char *label, dt_gui_alert_callback_t callback, void *data)
Definition alert.c:206
void dt_gui_alert_show(dt_gui_alert_t *alert)
Definition alert.c:236
@ DT_GUI_ALERT_PROGRESS
Definition alert.h:46
@ DT_GUI_ALERT_QUESTION
Definition alert.h:42
GtkWidget * dt_gui_main_window(void)
const char ** description(struct dt_iop_module_t *self)
Definition ashift.c:168
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
static const dt_adaptation_t kind
static void _closing_details_update(dt_closing_t *closing)
Definition closing.c:222
dt_closing_mode_t
Definition closing.c:47
@ DT_CLOSING_CONFIRM
Definition closing.c:48
@ DT_CLOSING_WAIT
Definition closing.c:49
#define DT_CLOSING_KIND_DARKROOM
Definition closing.c:42
static gboolean _closing_nothing_pending(void)
Definition closing.c:339
static void _closing_refresh(dt_closing_t *closing)
Definition closing.c:298
void dt_gui_closing_quit(void)
Definition closing.c:384
static void _closing_job_clear(gpointer data)
Definition closing.c:203
static void _closing_append_name(const gchar *message, void *data)
Definition closing.c:175
static gboolean _closing_confirm(void)
Definition closing.c:358
static void _closing_window_new(dt_closing_t *closing)
Definition closing.c:98
void dt_gui_closing_wait(void)
Definition closing.c:389
static gboolean _closing_poll(gpointer user_data)
Definition closing.c:318
static void _closing_count_job(const char *description __attribute__((unused)), const dt_job_queue_t queue __attribute__((unused)), const gboolean reserved, void *data)
Definition closing.c:277
#define DT_CLOSING_NOTICE_DELAY
Definition closing.c:37
static gboolean _closing_confirm_poll(gpointer user_data)
Definition closing.c:344
static void _closing_back_clicked(dt_gui_alert_t *alert __attribute__((unused)), void *data)
Definition closing.c:93
@ DT_CLOSING_COL_KIND
Definition closing.c:54
@ DT_CLOSING_COLS
Definition closing.c:56
@ DT_CLOSING_COL_DESCRIPTION
Definition closing.c:55
static void _closing_answer(dt_closing_t *closing, const gboolean quit)
Definition closing.c:81
static const char * _closing_kind_name(const int kind)
Definition closing.c:184
static int32_t _closing_running_jobs(void)
Definition closing.c:291
static void _closing_quit_clicked(dt_gui_alert_t *alert __attribute__((unused)), void *data)
Definition closing.c:87
static void _closing_collect_job(const char *description, const dt_job_queue_t queue, const gboolean reserved, void *data)
Definition closing.c:209
#define DT_CLOSING_POLL_INTERVAL
Definition closing.c:39
struct _GtkWidget GtkWidget
GtkWidget, opaque, spelled exactly as GTK spells it.
Definition colorspaces.h:98
void dt_control_quit()
Definition control.c:452
struct dt_control_t * dt_control_get_global(void)
Definition darktable.c:654
void dt_gui_refocus_parent(GtkWindow *parent)
Definition dialog.c:64
GtkWidget * view
the tree view
gchar ** names
Definition geometry.c:491
void * data
Definition geometry.h:111
void dt_control_running_jobs_foreach(dt_control_t *control, dt_control_running_jobs_foreach_callback_t callback, void *data)
Definition jobs.c:549
int32_t dt_control_queued_jobs_count(dt_control_t *control)
Definition jobs.c:571
int32_t dt_control_workers_alive(void)
Definition jobs.c:544
dt_job_queue_t
Definition jobs.h:54
@ DT_JOB_QUEUE_USER_FG
Definition jobs.h:55
@ DT_JOB_QUEUE_USER_BG
Definition jobs.h:57
@ DT_JOB_QUEUE_USER_EXPORT
Definition jobs.h:58
@ DT_JOB_QUEUE_SYSTEM_BG
Definition jobs.h:59
@ DT_JOB_QUEUE_SYSTEM_FG
Definition jobs.h:56
@ DT_DEBUG_CONTROL
Definition logging.h:52
void dt_print(dt_debug_thread_t thread, const char *msg,...) __attribute__((format(printf
Print to stdout when thread is enabled, prefixed with seconds since startup.
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition macros.h:96
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
Definition mem_alloc.h:171
const char * name
Definition pdf.h:90
void dt_control_progress_foreach(dt_control_t *control, dt_progress_foreach_callback_t callback, void *data)
Definition progress.c:424
float dt_aligned_pixel_simd_t __attribute__((vector_size(16), aligned(16)))
Apply one channel's tone curve to each of the three colour channels, or pass the channel through unto...
Definition simd.h:55
gchar * description
Definition closing.c:78
GMainLoop * loop
Definition closing.c:63
dt_gui_alert_t * alert
Definition closing.c:65
gchar * listed
Definition closing.c:70
gint64 start
Definition closing.c:64
int32_t queued
Definition closing.c:72
GtkWidget * dropped
Definition closing.c:71
GtkWidget * names
Definition closing.c:67
GtkListStore * store
Definition closing.c:69
GtkWidget * count
Definition closing.c:66
dt_closing_mode_t mode
Definition closing.c:61
GtkWidget * details
Definition closing.c:68
gboolean quit
Definition closing.c:62
#define DT_PIXEL_APPLY_DPI(value)
void dt_gui_add_class(GtkWidget *widget, const gchar *class_name)