cfait 1.1.6

Powerful, fast and elegant task / TODO manager. (GUI & TUI, CalDAV & local)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
// File: ./src/gui/update/common.rs
// SPDX-License-Identifier: GPL-3.0-or-later
//! Common utilities for GUI update handlers.
//!
//! This module implements shared helper routines used by the GUI update flow:
//!   - building the filtered task list for the UI,
//!   - saving configuration back to disk,
//!   - applying alias changes retroactively,
//!   - and helper scrolling/focus utilities used by the view layer.
//!
//! Key design notes:
//!   - The filter pipeline is intentionally split so that most predicate checks
//!     operate on &Task references (no clones). Only the final, visible tasks are
//!     cloned for UI rendering. This keeps memory churn low on frequent refreshes.
//!   - We maintain a small parent-attribute cache here (tags/location) to avoid
//!     repeated lookups while rendering task rows.
//!   - The scrolling helpers attempt a bounds-aware snap (pixel-accurate) when a
//!     widget Id and its layout bounds are available; they fall back to index-based
//!     heuristics when bounds are not yet registered.

use crate::config::Config;
use crate::gui::message::Message;
use crate::gui::state::GuiApp;
use crate::gui::view::focusable::{clear_focus_bounds, get_all_focus_bounds, get_focus_bounds};

use crate::system::SystemEvent;

use iced::Task;
use iced::widget::operation;
use iced::widget::scrollable::RelativeOffset;
use std::time::Duration as StdDuration;

/// Build the visible task list and update UI caches.
///
/// Strategy:
/// 1) Clear any previously stored focus bounds (these can be stale after major updates).
/// 2) Build an effective set of hidden calendars (user-hidden + disabled).
/// 3) Run `store.filter(...)` which operates on references and returns the final cloned list.
/// 4) Notify the alarm actor with the full task set (for alarm scheduling).
pub fn refresh_filtered_tasks(app: &mut GuiApp) {
    // Clear focus bounds before rebuilding the list.
    clear_focus_bounds();

    app.sort_calendars();

    let config = &app.core_config;

    // Sync specific Iced state to SessionState
    app.session.active_calendar_href = app.active_cal_href.clone();
    app.session.search_term = app.search_value.text();

    // Delegate entirely to session state
    let filter_res = app.session.get_filtered_view(&app.store, config);

    app.tasks = filter_res.items;
    app.cached_categories = filter_res.categories;
    app.cached_locations = filter_res.locations;
    app.cached_journal_pages = filter_res.journal_pages;

    for item in &mut app.tasks {
        if let crate::store::TaskListItem::Task(task) = item {
            app.task_ids
                .entry(task.uid.clone())
                .or_insert_with(iced::widget::Id::unique);
        }
    }

    let mut goals_progress = std::collections::HashMap::new();
    for (key, goal) in &app.core_config.goals {
        let prog = app.store.calculate_goal_progress(key, goal);
        let history = app.store.calculate_goal_history(key, goal, 7);
        goals_progress.insert(key.clone(), (prog, history));
    }
    app.cached_goals_progress = goals_progress;

    let mut task_goals = Vec::new();
    if config.show_task_goals_in_sidebar {
        for (href, map) in app.store.calendars.iter() {
            if app.hidden_calendars.contains(href)
                || app.disabled_calendars.contains(href)
                || href == crate::storage::LOCAL_TRASH_HREF
                || href == "local://recovery"
            {
                continue;
            }
            for t in map.values() {
                if t.unmapped_properties
                    .iter()
                    .any(|p| p.key == "X-CFAIT-HISTORY-OF")
                {
                    continue;
                }
                if let Some(goal) = &t.goal {
                    let progress = app
                        .store
                        .calculate_goal_progress(&format!("task:{}", t.uid), goal);
                    let history =
                        app.store
                            .calculate_goal_history(&format!("task:{}", t.uid), goal, 7);
                    task_goals.push((
                        t.uid.clone(),
                        t.summary.clone(),
                        goal.clone(),
                        progress,
                        history,
                    ));
                }
            }
        }
    }
    task_goals.sort_by(|a, b| a.1.cmp(&b.1));
    app.cached_task_goals = task_goals;

    if let Some(tx) = &app.alarm_tx {
        let all_tasks: Vec<crate::model::Task> = app
            .store
            .calendars
            .values()
            .flat_map(|m| m.values())
            .cloned()
            .collect();
        let _ = tx.try_send(SystemEvent::UpdateTasks(all_tasks));
    }

    // Update search highlight regex
    let search_text_str = app.search_value.text();
    app.search_highlight_regex = if !search_text_str.trim().is_empty() {
        let terms = crate::model::matcher::extract_highlight_terms(&search_text_str);
        if terms.is_empty() {
            None
        } else {
            let pattern = format!("(?i)({})", terms.join("|"));
            regex::Regex::new(&pattern).ok().map(std::rc::Rc::new)
        }
    } else {
        None
    };
}

/// Persist GUI-level config values back to the central Config object and save to disk.
///
/// This function collects the various UI-bound fields, converts them into the
/// Config structure and saves. It's intentionally permissive because the GUI may
/// leave some fields empty while the user types; callers should ensure validation
/// where necessary.
pub fn save_config(app: &mut GuiApp) -> Config {
    let mut cfg = app.core_config.clone();

    cfg.url = app.ob_url.clone();
    cfg.username = app.ob_user.clone();
    cfg.password = app.ob_pass.clone();
    cfg.tls_client_cert_path = if app.ob_tls_client_cert_path.trim().is_empty() {
        None
    } else {
        Some(app.ob_tls_client_cert_path.trim().to_string())
    };
    cfg.tls_client_key_path = if app.ob_tls_client_key_path.trim().is_empty() {
        None
    } else {
        Some(app.ob_tls_client_key_path.trim().to_string())
    };
    cfg.default_calendar = app.ob_default_cal.clone();
    cfg.allow_insecure_certs = app.ob_insecure;
    cfg.hidden_calendars = app.hidden_calendars.iter().cloned().collect();
    cfg.disabled_calendars = app.disabled_calendars.iter().cloned().collect();
    cfg.hide_completed = app.hide_completed;
    cfg.hide_fully_completed_tags = app.hide_fully_completed_tags;
    cfg.hide_aliases_in_sidebar = app.hide_aliases_in_sidebar;
    cfg.show_inline_descriptions = app.show_inline_descriptions;
    cfg.sort_standard_by_priority = app.sort_standard_by_priority;
    cfg.paused_sort_behavior = app.paused_sort_behavior;
    cfg.sort_tiebreak_recent = app.sort_tiebreak_recent;
    cfg.sort_preset = app.sort_preset;
    cfg.ui_scale = app.ui_scale;
    cfg.show_priority_numbers = app.show_priority_numbers;
    cfg.tag_aliases = app.tag_aliases.clone();
    cfg.sort_cutoff_days = app.sort_cutoff_days;
    cfg.theme = app.current_theme;
    cfg.urgent_days_horizon = app.urgent_days;
    cfg.urgent_priority_threshold = app.urgent_prio;
    cfg.default_priority = app.default_priority;
    cfg.start_grace_period_days = app.start_grace_period_days;
    cfg.auto_reminders = app.auto_reminders;
    cfg.default_reminder_time = app.default_reminder_time.clone();
    cfg.snooze_short_mins = app.snooze_short_mins;
    cfg.snooze_long_mins = app.snooze_long_mins;
    cfg.create_events_for_tasks = app.create_events_for_tasks;
    cfg.delete_events_on_completion = app.delete_events_on_completion;
    cfg.strikethrough_completed = app.strikethrough_completed;
    cfg.auto_refresh_interval_mins = app.auto_refresh_interval_mins;
    cfg.trash_retention_days = app.trash_retention_days;
    cfg.pinned_actions = app.pinned_actions.clone();

    cfg.max_done_roots = app.ob_max_done_roots_input.parse().unwrap_or(20);
    cfg.max_done_subtasks = app.ob_max_done_subtasks_input.parse().unwrap_or(5);

    cfg.quick_filter_term = app.quick_filter_term.clone();
    cfg.quick_filter_icon = app.quick_filter_icon.clone();
    cfg.show_quick_filter = app.show_quick_filter;
    cfg.show_calendars_tab = app.show_calendars_tab;
    cfg.show_tags_tab = app.show_tags_tab;
    cfg.show_locations_tab = app.show_locations_tab;
    cfg.show_goals_tab = app.show_goals_tab;
    cfg.sidebar_is_hidden = app.sidebar_is_hidden;
    cfg.blur_when_unfocused = app.blur_when_unfocused;
    cfg.sort_collections_by_size = app.sort_collections_by_size;
    cfg.log_level = app.log_level;
    cfg.sync_settings = app.sync_settings;
    cfg.first_day_of_week = app.first_day_of_week;

    cfg.expanded_tags = app.session.expanded_tags.clone();
    cfg.expanded_locations = app.session.expanded_locations.clone();

    // BUMP TIMESTAMP IF CHANGED
    let old_timestamp = cfg.settings_updated_at;
    cfg.update_sync_timestamp_if_changed(&app.core_config);
    let timestamp_changed = cfg.settings_updated_at != old_timestamp;

    // Cache the updated config in memory
    app.core_config = cfg.clone();

    if timestamp_changed && let Some(tx) = &app.bg_tx {
        let _ = tx.try_send(crate::gui::async_ops::WorkerCommand::SyncNow);
    }

    // --- ASYNC SAVE FIX ---
    let ctx_clone = app.ctx.clone();
    let cfg_clone = cfg.clone();
    std::thread::spawn(move || {
        let _ = cfg_clone.save_with_credentials(ctx_clone.as_ref());
    });
    // ----------------------

    cfg
}

/// Apply a new alias mapping retroactively across tasks.
///
/// If there are modified tasks, refresh the filtered view and optionally issue
/// network update commands to persist changes to a remote server.
pub fn apply_alias_retroactively(
    app: &mut GuiApp,
    alias_key: &str,
    target_tags: &[String],
) -> Vec<crate::model::Task> {
    let modified_tasks = app.store.apply_alias_retroactively(alias_key, target_tags);

    if modified_tasks.is_empty() {
        return Vec::new();
    }

    refresh_filtered_tasks(app);
    modified_tasks
}

/// Scroll the main list to the selected task.
///
/// This helper prefers a bounds-aware pixel-accurate scroll when we have a widget Id
/// and the view has registered layout bounds for it. When bounds are not available
/// (e.g. the widget hasn't rendered yet) it falls back to an index-based heuristic
/// using an average row height.
pub fn scroll_to_selected(app: &GuiApp, focus: bool) -> Task<Message> {
    if let Some(uid) = &app.selected_uid {
        let id_opt = app.task_ids.get(uid).cloned();
        let idx_opt = app.find_task_index_by_uid(uid);

        // If the task was completely filtered out (e.g. search didn't match), do NOT try to scroll to it.
        // Doing so would query a floating `Id` that is not attached to the layout tree!
        if idx_opt.is_none() {
            return Task::none();
        }

        if let (Some(id), Some(idx)) = (id_opt.clone(), idx_opt) {
            if let Some(rect) = get_focus_bounds(&id) {
                // When available, use the union of all registered bounds to compute
                // an absolute content height and derive a fractional scroll offset.
                let all = get_all_focus_bounds();
                let mut min_y: f32 = f32::INFINITY;
                let mut max_y: f32 = 0.0;
                for r in all.values() {
                    min_y = min_y.min(r.y);
                    max_y = max_y.max(r.y + r.height);
                }

                if min_y.is_finite() && max_y > min_y {
                    let content_h = max_y - min_y;
                    let viewport_h = (app.current_window_size.height - 180.0).max(100.0);

                    // If the item is fully visible in the viewport, do not snap the scrollable.
                    // This prevents the view from jumping when clicking or navigating to a visible task.
                    let viewport_top = 80.0;
                    let viewport_bottom = app.current_window_size.height - 100.0;
                    let top_visible = rect.y >= viewport_top && rect.y <= viewport_bottom;
                    let bottom_visible = (rect.y + rect.height) >= viewport_top
                        && (rect.y + rect.height) <= viewport_bottom;
                    let spans_viewport =
                        rect.y <= viewport_top && (rect.y + rect.height) >= viewport_bottom;
                    let is_visible = top_visible || bottom_visible || spans_viewport;

                    if is_visible {
                        if focus {
                            return operation::focus(id);
                        } else {
                            return Task::none();
                        }
                    }

                    // Item center relative to content top (min_y)
                    let item_center_rel = (rect.y - min_y) + rect.height / 2.0;

                    // Maximum scroll = content_h - viewport_h
                    let max_scroll = (content_h - viewport_h).max(0.0);
                    // Desired offset so the item center is positioned in the middle of the viewport.
                    let desired_offset_px =
                        (item_center_rel - viewport_h / 2.0).clamp(0.0, max_scroll);

                    // Convert desired pixel offset into a relative fraction for snap_to.
                    let max_scroll_px = (content_h - viewport_h).max(0.0);
                    let y = if max_scroll_px > 0.0 {
                        (desired_offset_px / max_scroll_px).clamp(0.0, 1.0)
                    } else {
                        0.0
                    };

                    let snap =
                        operation::snap_to(app.scrollable_id.clone(), RelativeOffset { x: 0.0, y });

                    if focus {
                        return Task::batch(vec![snap, operation::focus(id)]);
                    } else {
                        return snap;
                    }
                }
            }

            // Fallback: index-centered heuristic (estimate pixels using avg row height).
            // Tuned default row height is used when bounds are not available.
            let avg_item_h: f32 = 34.0;
            let total_items = app.tasks.len() as f32;
            let content_h = (avg_item_h * total_items).max(1.0);
            let viewport_h = (app.current_window_size.height - 180.0).max(100.0);
            let item_center = (idx as f32 + 0.5) * avg_item_h;
            let max_scroll = (content_h - viewport_h).max(0.0);
            let desired_offset_px = (item_center - viewport_h / 2.0).clamp(0.0, max_scroll);

            let max_scroll_px = (content_h - viewport_h).max(0.0);
            let y = if max_scroll_px > 0.0 {
                (desired_offset_px / max_scroll_px).clamp(0.0, 1.0)
            } else {
                0.0
            };

            let snap = operation::snap_to(app.scrollable_id.clone(), RelativeOffset { x: 0.0, y });

            if focus {
                return Task::batch(vec![snap, operation::focus(id)]);
            } else {
                return snap;
            }
        }

        if let Some(idx) = idx_opt {
            // Index-only centering as a last resort, using dynamic height estimation.
            let mut content_h = 0.0;
            let mut item_center = 0.0;

            for (i, item) in app.tasks.iter().enumerate() {
                let mut h = 36.0; // Base row height

                if let crate::store::TaskListItem::Task(t) = item {
                    // Add height for wrapped summary (approx 60 chars per line)
                    h += (t.summary.len().saturating_sub(60) as f32 / 60.0).floor() * 20.0;

                    // Add height for expanded details
                    if app.expanded_tasks.contains(&t.uid) {
                        h += 15.0; // Margin
                        if !t.description.is_empty() {
                            h += t.description.lines().count() as f32 * 18.0;
                        }
                        h += t.dependencies.len() as f32 * 24.0;
                        h += t.related_to.len() as f32 * 24.0;
                        h += app.store.get_tasks_blocking(&t.uid).len() as f32 * 24.0;
                        h += app.store.get_tasks_related_to(&t.uid).len() as f32 * 24.0;
                        if !t.sessions.is_empty() || app.adding_session_uid.as_ref() == Some(&t.uid)
                        {
                            h += 30.0 + (t.sessions.len().min(3) as f32 * 20.0);
                        }
                    }
                } else {
                    h = 28.0; // Expand/Collapse virtual row height
                }

                if i < idx {
                    item_center += h;
                } else if i == idx {
                    item_center += h / 2.0;
                }
                content_h += h;
            }

            content_h = content_h.max(1.0);
            let viewport_h = (app.current_window_size.height - 180.0).max(100.0);
            let max_scroll = (content_h - viewport_h).max(0.0);
            let desired_offset_px = (item_center - viewport_h / 2.0).clamp(0.0, max_scroll);

            let max_scroll_px = (content_h - viewport_h).max(0.0);
            let y = if max_scroll_px > 0.0 {
                (desired_offset_px / max_scroll_px).clamp(0.0, 1.0)
            } else {
                0.0
            };

            return operation::snap_to(app.scrollable_id.clone(), RelativeOffset { x: 0.0, y });
        }

        if let Some(id) = id_opt
            && focus
        {
            // If we only have a widget Id, focus it as a last step.
            return operation::focus(id);
        }
    }

    Task::none()
}

/// Try to focus the selected row after a short delay. This function emits a small
/// batch of delayed attempts to increase the chance the focusable row has been
/// registered by the view traversal on slower platforms or complex layouts.
pub fn scroll_to_selected_delayed(_app: &GuiApp, focus: bool) -> Task<Message> {
    if let Some(uid) = &_app.selected_uid
        && let Some(id) = _app.task_ids.get(uid).cloned()
    {
        return Task::perform(
            async move {
                let mut attempts = 0u8;
                loop {
                    if crate::gui::view::focusable::get_focus_bounds(&id).is_some() {
                        break;
                    }
                    attempts = attempts.saturating_add(1);
                    if attempts >= 20 {
                        break;
                    }
                    tokio::time::sleep(StdDuration::from_millis(50)).await;
                }
            },
            move |_| Message::SnapToSelected { focus },
        );
    }

    // Batch a few increasing delays to give the UI multiple chances to register the row.
    Task::batch(vec![
        Task::perform(
            async {
                tokio::time::sleep(StdDuration::from_millis(120)).await;
            },
            move |_| Message::SnapToSelected { focus },
        ),
        Task::perform(
            async {
                tokio::time::sleep(StdDuration::from_millis(360)).await;
            },
            move |_| Message::SnapToSelected { focus },
        ),
        Task::perform(
            async {
                tokio::time::sleep(StdDuration::from_millis(720)).await;
            },
            move |_| Message::SnapToSelected { focus },
        ),
    ])
}

use crate::model::AppIntent;

pub fn dispatch_intent(app: &mut GuiApp, intent: AppIntent) {
    let config = &app.core_config;

    // 1. Update UI filters synchronously
    app.session.apply_session_intent(&intent);

    // 2. Mutate in-memory store synchronously & extract persistence actions
    let (actions, reverse, desc, primary_uid) = app.store.apply_task_intent(&intent, config);
    if !actions.is_empty() {
        app.undo_history.push(crate::journal::UndoRecord {
            description: desc,
            primary_uid,
            forward: actions.clone(),
            reverse,
        });
        app.info_msg = None; // clear generic info msg on a new action
    }

    // 3. Update the UI rendering
    refresh_filtered_tasks(app);

    // 4. Send actions to background thread for disk/network persistence
    if !actions.is_empty()
        && let Some(tx) = &app.bg_tx
    {
        let _ = tx.try_send(crate::gui::async_ops::WorkerCommand::Batch(actions));
    }
}

/// Reloads the journal from disk and updates the unsynced UI state & tooltip.
pub fn update_journal_state(app: &mut GuiApp) {
    let journal = crate::journal::Journal::load(app.ctx.as_ref());
    app.unsynced_changes = !journal.is_empty();

    if app.unsynced_changes {
        let mut lines = vec![rust_i18n::t!("unsynced").to_string()];
        for (i, action) in journal.queue.iter().enumerate() {
            if i >= 10 {
                lines.push(
                    rust_i18n::t!("unsynced_and_more", count = journal.queue.len() - 10)
                        .to_string(),
                );
                break;
            }
            let (verb, summary) = match action {
                crate::journal::Action::Create(t) => (
                    rust_i18n::t!("unsynced_action_create").to_string(),
                    &t.summary,
                ),
                crate::journal::Action::Update(t) => (
                    rust_i18n::t!("unsynced_action_update").to_string(),
                    &t.summary,
                ),
                crate::journal::Action::Delete(t) => (
                    rust_i18n::t!("calendar_action_deleted").to_string(),
                    &t.summary,
                ),
                crate::journal::Action::Move(t, _) => (
                    rust_i18n::t!("unsynced_action_move").to_string(),
                    &t.summary,
                ),
            };
            let trunc_summary = if summary.chars().count() > 40 {
                format!("{}...", summary.chars().take(37).collect::<String>())
            } else {
                summary.clone()
            };
            lines.push(format!("{}: {}", verb, trunc_summary));
        }
        app.unsynced_tooltip = lines.join("\n");
    } else {
        app.unsynced_tooltip = rust_i18n::t!("force_sync").to_string();
    }
}