Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
dev_history_gui.c
Go to the documentation of this file.
1/*
2 This file is part of Ansel,
3 Copyright (C) 2026 Aurélien PIERRE.
4 Copyright (C) 2026 Guillaume Stutin.
5
6 Ansel is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Ansel is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Ansel. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include "develop/imageop_gui.h"
23
24#include "develop/blend_gui.h"
25#include "develop/dev_history.h"
27#include "develop/develop.h"
28#include "develop/imageop.h"
29#include "develop/masks_gui.h"
31#include "system/macros.h"
32
33#include <gtk/gtk.h>
34
35/* The module widgets after a backend history reload. */
37{
38 if(!dev->gui_attached) return;
39
40 // Match the live module instances to the reloaded history before touching GTK.
41 // This loop may remove obsolete instances or expose instances newly created
42 // while reading a style/history from the database.
43 GList *removed = NULL;
44 dt_pthread_rwlock_wrlock(&dev->history_mutex);
45 dt_dev_history_refresh_nodes_ext(dev, &dev->iop, dev->history, &removed);
46 dt_pthread_rwlock_unlock(&dev->history_mutex);
47
49
50 // Destroy the widgets of the instances the reconciliation removed -- AFTER the lock,
51 // which is the point of the removed-list protocol (see dev_history.h); the modules are
52 // parked in dev->alliop and already unlinked, so nothing else touches them.
53 for(GList *l = removed; l; l = g_list_next(l))
54 {
55 dt_iop_module_t *mod = (dt_iop_module_t *)l->data;
56 if(dev->gui_module == mod) dt_iop_request_focus(NULL);
57 if(!dt_iop_is_hidden(mod) && mod->gui && mod->gui->expander)
58 {
59 // hide first to avoid a burst of gtk critical warnings from the live container
60 gtk_widget_hide(mod->gui->expander);
61 // frees mod->gui and destroys the whole expander/header/widget tree itself
63 }
64 }
65 g_list_free(removed);
66
67 for(GList *module = g_list_first(dev->iop); module; module = g_list_next(module))
68 {
69 dt_iop_module_t *mod = (dt_iop_module_t *)(module->data);
70
71 // History reload is backend-only and creates new multi-instances without
72 // GTK state. Attach every missing GUI here, after releasing history_mutex,
73 // so styles and global history actions expose their complete module set.
74 if(!dt_iop_is_hidden(mod) && (IS_NULL_PTR(mod->gui) || IS_NULL_PTR(mod->gui->expander)))
75 {
76 // a backend-created instance has no gui struct at all -- that IS "never initialised"
77 if(IS_NULL_PTR(mod->gui) || IS_NULL_PTR(mod->gui->widget)) dt_iop_gui_init(mod);
79 }
80
81 // Parameters, enabled state, headers and blending controls may all have
82 // changed, therefore refresh every module rather than only history entries.
84 }
85
88
90}
91
92/* Undoing an edit also undoes what the user was LOOKING at: the recorded mask-edit view for the
93 * focused module. Undo and redo refresh the widgets here rather than through
94 * dt_dev_history_gui_update(), because they keep a shape creation session armed. */
95static void _undo_restore_gui(dt_develop_t *dev, const int mask_edit_mode,
96 const int request_mask_display)
97{
99
100 dt_iop_module_t *module = dev->gui_module;
101 if(IS_NULL_PTR(module)) return;
102
103 // Changing the edit mode would end an armed creation session.
104 if(IS_NULL_PTR(dev->form_gui) || !dev->form_gui->creation)
105 dt_masks_set_edit_mode(module, mask_edit_mode);
106 module->request_mask_display = request_mask_display;
107
109 dt_iop_gui_blend_data_t *bd = module->gui ? (dt_iop_gui_blend_data_t *)module->gui->blend_data : NULL;
110 if(bd)
111 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(bd->showmask),
112 request_mask_display == DT_DEV_PIXELPIPE_DISPLAY_MASK);
113}
114
116{
117 // A history reload is an edit made off the canvas: it ends a shape creation session, as Esc does.
118 if(dev->gui_attached) dt_masks_form_exit_creation(NULL, dev->form_gui);
120}
121/* ---------------------------------------------------------------------------------------
122 * Throttling the history commit, at the one place every widget already goes through.
123 *
124 * A history commit is not cheap: it records an undo step, takes history_mutex as writer,
125 * rehashes the whole history, writes the image cache, rebuilds the masks list, schedules a
126 * DB+XMP write, and finally tells the pipes their history changed -- which also raises their
127 * `shutdown` atomic, so the worker abandons the frame it is rendering. Running that per step
128 * of a slider drag or a combobox scroll blocks the GUI thread often enough that the widget
129 * cannot even repaint its own value, and aborts each render before it can finish.
130 *
131 * Widgets used to dodge that by deferring their own `value-changed` emission through
132 * gui_throttle (bauhaus, and a copy of the same dance in nine curve modules). That put the
133 * policy in every widget that wanted it and left every other widget without it. The commit
134 * is deferred here instead, where all of them already arrive.
135 *
136 * TWO RULES, and the difference between them matters:
137 *
138 * 1. NOTHING IS MERGED. A tempting optimisation is to collapse pending requests into one
139 * carrying the union of their flags; do not. Drawn masks, raster masks, module
140 * enable/disable, the mask manager and plain parameter edits each commit differently, and
141 * any merging rule is somewhere to silently drop one of them. The queue is FIFO and every
142 * distinct request is kept, in order.
143 *
144 * 2. A REPEAT OF THE PENDING TAIL IS NOT A NEW REQUEST. If the last thing queued is already
145 * "commit `module`, enable=`enable`, switched=`switched`" and that is exactly what is being
146 * asked for again, there is nothing to add: the queued request reads module->params when it
147 * drains, so it necessarily commits the newest value. This is consecutive-duplicate
148 * suppression against the TAIL only -- never a search of the queue, never a combination of
149 * two different requests -- so ordering is exactly preserved. It is what keeps a 300-tick
150 * scroll from queueing 300 commits, which is the whole point: a transient intermediate value
151 * of an ongoing gesture is not a history event.
152 *
153 * This is a batching window, not a debounce: the timer is armed by the first request and is
154 * NOT re-armed by the ones that follow, so a sustained drag keeps committing once per window
155 * instead of freezing until the user stops moving.
156 *
157 * A request queued while GTK dispatches a key press -- a slider step, a typed value, a shortcut --
158 * is `switched`: the pipes keep the outputs it replaces (dt_dev_pixelpipe_t.keep_outputs), since
159 * going back is the likely next keystroke. So is a choice between discrete states: a request
160 * queued while a combobox announces its value (dt_gui_widgets_choice_announced(), whether
161 * from its popup, a scroll or a key), or while GTK dispatches a click on a check box or a toggle
162 * button. A choice less than the user's timeout after the previous one (a held key, a scroll
163 * through a combobox) is not, nor is a slider drag or click. See doc/pipeline-cache.md.
164 *
165 * dt_dev_add_history_item_ext() is NOT throttled and never was. Anything that must land
166 * before the next statement (bulk history loads, style application, image duplication)
167 * already goes through it.
168 *
169 * GUI thread only, like the commit path it serves ("always called from GUI controls" below),
170 * hence no lock -- same contract as gui_throttle's own queue.
171 */
172
180
181static GQueue _pending_commits = G_QUEUE_INIT;
182static guint _pending_commit_source = 0;
183static guint32 _last_choice_time = 0;
184
185
186static gboolean _drain_pending_commits(gpointer user_data)
187{
188 (void)user_data;
189
191
192 // Detach the queue before draining: a commit can trigger another one (a module reacting in
193 // post_history_commit), which would push onto the queue we are iterating.
194 GQueue ready = G_QUEUE_INIT;
195 ready.head = _pending_commits.head;
196 ready.tail = _pending_commits.tail;
197 ready.length = _pending_commits.length;
198 g_queue_init(&_pending_commits);
199
200 while(!g_queue_is_empty(&ready))
201 {
202 dt_dev_pending_commit_t *request = (dt_dev_pending_commit_t *)g_queue_pop_head(&ready);
203 dt_dev_history_commit_item_now(request->dev, request->module, request->enable, request->switched);
204 dt_free(request);
205 }
206
207 return G_SOURCE_REMOVE;
208}
209
210// Whether the commit requested now is `switched` (see above): a key press, a combobox choice or
211// a click on a check box or toggle button, unless it follows the previous one too closely. A
212// choice made outside any event (a combobox set by code) has no stream to follow. Event times
213// are in ms and wrap, hence the unsigned difference. Records the choice's time: call it once
214// per request.
215static gboolean _request_is_switch(void)
216{
217 GdkEvent *event = gtk_get_current_event();
218 const GdkEventType type = IS_NULL_PTR(event) ? GDK_NOTHING : gdk_event_get_event_type(event);
219 const gboolean toggle_click = (type == GDK_BUTTON_PRESS || type == GDK_BUTTON_RELEASE)
220 && GTK_IS_TOGGLE_BUTTON(gtk_get_event_widget(event));
221 const gboolean choice = type == GDK_KEY_PRESS || toggle_click || dt_gui_widgets_choice_announced();
222 const guint32 choice_time = IS_NULL_PTR(event) ? 0 : gdk_event_get_time(event);
223 g_clear_pointer(&event, gdk_event_free);
224 const gboolean switched = choice
225 && (type == GDK_NOTHING
227 if(choice && type != GDK_NOTHING) _last_choice_time = choice_time;
228 return switched;
229}
230
231static void _queue_pending_commit(dt_develop_t *dev, dt_iop_module_t *module, const gboolean enable)
232{
233 const guint timeout_ms = dt_gui_throttle_get_timeout_ms();
234 const gboolean switched = _request_is_switch();
235
236 // A zero timeout means the user turned throttling off in the preferences (and is also what
237 // a headless run gets, since nothing ever sets one there). Same call, same place, just now.
238 if(timeout_ms == 0)
239 {
240 dt_dev_history_commit_item_now(dev, module, enable, switched);
241 return;
242 }
243
244 // Rule 2 above: a repeat of what is already queued at the tail adds nothing.
246 ? (const dt_dev_pending_commit_t *)_pending_commits.tail->data
247 : NULL;
248 if(!IS_NULL_PTR(tail) && tail->dev == dev && tail->module == module && tail->enable == enable
249 && tail->switched == switched)
250 return;
251
252 dt_dev_pending_commit_t *queued = (dt_dev_pending_commit_t *)calloc(1, sizeof(*queued));
253 if(IS_NULL_PTR(queued))
254 {
255 // Out of memory for a 24-byte record: commit now rather than lose the edit.
256 dt_dev_history_commit_item_now(dev, module, enable, switched);
257 return;
258 }
259
260 queued->dev = dev;
261 queued->module = module;
262 queued->enable = enable;
263 queued->switched = switched;
264 g_queue_push_tail(&_pending_commits, queued);
265
267 _pending_commit_source = g_timeout_add(timeout_ms, _drain_pending_commits, NULL);
268}
269
271{
272 // The edits queued before this one land first, in their order.
275}
276
278{
279 // Run them, do not drop them: a pending request IS the user's last edit, and dropping it
280 // would lose the value they left a slider on. Callers invoke this while `dev` is still
281 // whole -- before any pipe node, iop or history teardown -- so committing here is safe,
282 // and it is the only moment at which it still is. A request draining after teardown is the
283 // race CLAUDE.md documents, which corrupts the heap and crashes somewhere unrelated.
285 {
286 g_source_remove(_pending_commit_source);
288 }
289
290 GList *iter = _pending_commits.head;
291 while(iter)
292 {
293 GList *next = g_list_next(iter);
294 dt_dev_pending_commit_t *request = (dt_dev_pending_commit_t *)iter->data;
295 if(IS_NULL_PTR(dev) || request->dev == dev)
296 {
297 g_queue_delete_link(&_pending_commits, iter);
298 dt_dev_history_commit_item_now(request->dev, request->module, request->enable, request->switched);
299 dt_free(request);
300 }
301 iter = next;
302 }
303
304 // Something else's requests may remain (another dev): give them their timer back.
305 if(!g_queue_is_empty(&_pending_commits))
306 {
307 const guint timeout_ms = dt_gui_throttle_get_timeout_ms();
308 if(timeout_ms > 0) _pending_commit_source = g_timeout_add(timeout_ms, _drain_pending_commits, NULL);
309 else _drain_pending_commits(NULL);
310 }
311}
312
314{
315 // Last-resort counterpart to the flush, for a `dev` that is already too far gone to commit
316 // to. Nothing should normally be left here by the time this runs.
317 GList *iter = _pending_commits.head;
318 while(iter)
319 {
320 GList *next = g_list_next(iter);
321 dt_dev_pending_commit_t *request = (dt_dev_pending_commit_t *)iter->data;
322 if(IS_NULL_PTR(dev) || request->dev == dev)
323 {
324 g_queue_delete_link(&_pending_commits, iter);
325 dt_free(request);
326 }
327 iter = next;
328 }
329
330 if(_pending_commit_source && g_queue_is_empty(&_pending_commits))
331 {
332 g_source_remove(_pending_commit_source);
334 }
335}
336
337
338// The next 2 functions are always called from GUI controls setting parameters
339// This is why they directly start a pipeline recompute.
340// Otherwise, please keep GUI and pipeline fully separated.
341
342void dt_dev_add_history_item_real(dt_develop_t *dev, dt_iop_module_t *module, gboolean enable, gboolean redraw)
343{
344 (void)redraw;
345 // A shape creation session is a tool for the canvas: an edit made anywhere else ends it, as Esc
346 // does. The session's own strokes commit through dt_dev_history_commit_item_now() instead.
347 dt_masks_form_exit_creation(NULL, dev->form_gui);
349}
350
351/* After each immediate commit: keep the viewport geometry and the enable toggle honest. */
353{
354 // If module params change the geometry of the ROI, update immediately so we avoid
355 // drawing glitches.
356 if(module->modify_roi_in || module->modify_roi_out)
358
359 // Changing a parameter of a disabled module enables it, so update the GUI toggle state
360 // to reflect it -- frozen, so setting the state does not run its callbacks.
364}
365
371
372// clang-format off
373// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
374// vim: shiftwidth=2 expandtab tabstop=2 cindent
375// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
376// clang-format on
void dt_iop_gui_update_blendif(dt_iop_module_t *module)
Definition blend_gui.c:3439
The blending panel's GUI state and API, cut out of develop/blend.h.
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
void dt_dev_history_set_commit_gui_handler(dt_dev_history_commit_gui_handler_t handler)
void dt_dev_history_commit_item_now(dt_develop_t *dev, dt_iop_module_t *module, gboolean enable, gboolean switched)
Commit one history item immediately – record undo, take history_mutex as writer, write through,...
void dt_dev_history_set_undo_restore_gui_handler(dt_dev_history_undo_restore_gui_handler_t handler)
int dt_dev_history_refresh_nodes_ext(dt_develop_t *dev, GList **iop, GList *history, GList **removed_gui_modules)
Refresh GUI module nodes to match history state.
void dt_dev_add_history_item_real(dt_develop_t *dev, dt_iop_module_t *module, gboolean enable, gboolean redraw)
Thread-safe wrapper around dt_dev_add_history_item_ext().
void dt_dev_history_drop_pending_commits(dt_develop_t *dev)
Discard every history commit still queued for dev, without running it.
static gboolean _drain_pending_commits(gpointer user_data)
void dt_dev_history_flush_pending_commits(dt_develop_t *dev)
Run every history commit still queued for dev, now.
static GQueue _pending_commits
static guint32 _last_choice_time
void dt_dev_history_gui_init(void)
Install the history engine's GUI handlers. Called once from dt_init() beside the other handler instal...
static guint _pending_commit_source
static void _history_gui_refresh(dt_develop_t *dev)
static void _commit_gui(dt_develop_t *dev, dt_iop_module_t *module)
static void _queue_pending_commit(dt_develop_t *dev, dt_iop_module_t *module, const gboolean enable)
void dt_dev_history_gui_update(dt_develop_t *dev)
Apply history-loaded params to module GUIs.
static gboolean _request_is_switch(void)
static void _undo_restore_gui(dt_develop_t *dev, const int mask_edit_mode, const int request_mask_display)
void dt_dev_history_commit_item_from_gui(dt_develop_t *dev, dt_iop_module_t *module, const gboolean enable)
Commit one history item from a GUI control now, after the commits queued before it.
The GTK half of the history engine, following the blend.c/blend_gui.c pattern.
dt_atomic_set_uint64 & dev
void dt_dev_signal_modules_moved(dt_develop_t *dev)
Definition develop.c:1915
void dt_dev_masks_list_change(dt_develop_t *dev)
Definition develop.c:1407
int dt_dev_get_thumbnail_size(dt_develop_t *dev)
Definition develop.c:356
@ DT_DEV_PIXELPIPE_DISPLAY_MASK
Definition develop.h:123
static int dt_pthread_rwlock_wrlock(dt_pthread_rwlock_t *rwlock) ACQUIRE(rwlock) NO_THREAD_SAFETY_ANALYSIS
Definition dtpthread.h:299
static int dt_pthread_rwlock_unlock(dt_pthread_rwlock_t *rwlock) RELEASE_GENERIC(rwlock) NO_THREAD_SAFETY_ANALYSIS
Definition dtpthread.h:217
guint dt_gui_throttle_get_user_timeout_ms(void)
guint dt_gui_throttle_get_timeout_ms(void)
static gboolean enable(const dt_image_t *image)
Definition highlights.c:938
gboolean dt_iop_is_hidden(dt_iop_module_t *module)
Definition imageop.c:736
void dt_iop_gui_cleanup_module(dt_iop_module_t *module)
void dt_iop_gui_set_expander(dt_iop_module_t *module)
void dt_iop_gui_init(dt_iop_module_t *module)
void dt_iop_gui_update(dt_iop_module_t *module)
void dt_iop_gui_set_enable_button(dt_iop_module_t *module)
void dt_iop_request_focus(dt_iop_module_t *module)
Move darkroom focus to module, or clear it with NULL.
_lib_location_type_t type
Definition location.c:1
#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
void dt_masks_set_edit_mode(struct dt_iop_module_t *module, dt_masks_edit_mode_t value)
Definition masks_gui.c:5852
gboolean dt_masks_form_exit_creation(dt_iop_module_t *module, dt_masks_form_gui_t *mask_gui)
Definition masks_gui.c:2280
The interactive half of the masks subsystem: the editing state (dt_masks_form_gui_t),...
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
Definition mem_alloc.h:171
dt_iop_module_t * module
GtkWidget * expander
Definition imageop_gui.h:57
GtkWidget * widget
Definition imageop_gui.h:47
struct dt_iop_module_gui_t * gui
Definition imageop.h:346
void(* modify_roi_out)(struct dt_iop_module_t *self, const struct dt_dev_pixelpipe_t *pipe, struct dt_dev_pixelpipe_iop_t *piece, struct dt_iop_roi_t *roi_out, const struct dt_iop_roi_t *roi_in)
Definition imageop.h:241
void(* modify_roi_in)(struct dt_iop_module_t *self, const struct dt_dev_pixelpipe_t *pipe, struct dt_dev_pixelpipe_iop_t *piece, const struct dt_iop_roi_t *roi_out, struct dt_iop_roi_t *roi_in)
Definition imageop.h:238
gboolean dt_gui_widgets_choice_announced(void)
#define dt_gui_freeze_begin()
#define dt_gui_freeze_end()