open-timeline-gui 0.1.0

OpenTimeline GUI
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
// SPDX-License-Identifier: GPL-3.0-or-later

//!
//! Full GUI search
//!

use crate::app::{ActionRequest, EntityOrTimelineActionRequest};
use crate::components::OpenTimelineButton;
use crate::components::{BooleanExpressionGui, HintText};
use crate::config::SharedConfig;
use crate::consts::{EDIT_BUTTON_WIDTH, VIEW_BUTTON_WIDTH};
use crate::spawn_transaction_no_commit_send_result;
use bool_tag_expr::BoolTagExpr;
use eframe::egui::{self, Align, Context, Layout, ScrollArea, TextEdit, Ui, Vec2};
use egui_extras::{Column, TableBuilder};
use open_timeline_core::{
    IsReducedCollection, IsReducedType, OpenTimelineId, ReducedEntities, ReducedEntity,
    ReducedTimeline, ReducedTimelines,
};
use open_timeline_crud::{CrudError, FetchByPartialNameAndBoolTagExpr, Limit};
use open_timeline_gui_core::{
    Draw, EmptyConsideredInvalid, Reload, ShowRemoveButton, body_text_height, widget_x_spacing,
};
use std::sync::Arc;
use std::u32;
use tokio::sync::mpsc::{Receiver, UnboundedSender};

/// The maximum number of search results shown for each results section
const SEARCH_LIMIT: u32 = 75;

/// The search GUI panel in the main window
#[derive(Debug)]
pub struct SearchGui {
    /// Entity search terms & results
    entity_search: SearchPartialNameAndBoolTagExpr<ReducedEntities>,

    /// Timeline search terms & results
    timeline_search: SearchPartialNameAndBoolTagExpr<ReducedTimelines>,

    /// Used request new windows for editing and viewing timelines and entities
    tx_action_request: UnboundedSender<ActionRequest>,
}

impl SearchGui {
    /// Create a new search GUI panel manager
    pub fn new(
        shared_config: SharedConfig,
        tx_action_request: UnboundedSender<ActionRequest>,
    ) -> Self {
        let mut search = Self {
            entity_search: SearchPartialNameAndBoolTagExpr::<ReducedEntities>::new(Arc::clone(
                &shared_config,
            )),
            timeline_search: SearchPartialNameAndBoolTagExpr::<ReducedTimelines>::new(Arc::clone(
                &shared_config,
            )),
            tx_action_request,
        };
        search.request_reload();
        search
    }

    /// Display the entity search results fetched by partial name
    fn show_entity_search_results(&mut self, ui: &mut Ui, ctx: &Context) {
        let clicked = self.entity_search.show(ctx, ui);
        match clicked {
            None => (),
            Some(SearchResultButtonClicked::View(entity)) => {
                self.request_view_entity(ctx, ui, &entity)
            }
            Some(SearchResultButtonClicked::Edit(entity)) => {
                self.request_edit_entity(ctx, ui, &entity)
            }
        }
    }

    /// Display the timeline search results fetched by partial name
    fn show_timeline_search_results(&mut self, ui: &mut Ui, ctx: &Context) {
        let clicked = self.timeline_search.show(ctx, ui);
        match clicked {
            None => (),
            Some(SearchResultButtonClicked::View(timeline)) => {
                self.request_view_timeline(ctx, ui, &timeline)
            }
            Some(SearchResultButtonClicked::Edit(timeline)) => {
                self.request_edit_timeline(ctx, ui, &timeline)
            }
        }
    }

    /// Inform the main control loop that the user wants to edit an entity
    fn request_edit_entity(&mut self, _ctx: &Context, _ui: &mut Ui, entity: &ReducedEntity) {
        self.send_action_request(ActionRequest::Entity(
            EntityOrTimelineActionRequest::EditExisting(entity.id()),
        ));
    }

    /// Inform the main control loop that the user wants to view an entity
    fn request_view_entity(&mut self, _ctx: &Context, _ui: &mut Ui, entity: &ReducedEntity) {
        self.send_action_request(ActionRequest::Entity(
            EntityOrTimelineActionRequest::ViewExisting(entity.id()),
        ));
    }

    /// Inform the main control loop that the user wants to edit a timeline
    fn request_edit_timeline(&mut self, _ctx: &Context, _ui: &mut Ui, timeline: &ReducedTimeline) {
        self.send_action_request(ActionRequest::Timeline(
            EntityOrTimelineActionRequest::EditExisting(timeline.id()),
        ));
    }

    /// Inform the main control loop that the user wants to view a timeline
    fn request_view_timeline(&mut self, _ctx: &Context, _ui: &mut Ui, timeline: &ReducedTimeline) {
        self.send_action_request(ActionRequest::Timeline(
            EntityOrTimelineActionRequest::ViewExisting(timeline.id()),
        ));
    }

    /// Send an [`ActionRequest`] request to the main control loop
    fn send_action_request(&mut self, request: ActionRequest) {
        let _ = self.tx_action_request.send(request);
    }
}

impl Draw for SearchGui {
    fn draw(&mut self, ctx: &Context, ui: &mut Ui) {
        self.timeline_search.check_reload_response();
        self.entity_search.check_reload_response();

        ui.columns(2, |columns| {
            // Timeline Column
            columns[0].vertical(|ui| {
                open_timeline_gui_core::Label::sub_heading(ui, "Timelines");

                // Button to create a new timeline
                if open_timeline_gui_core::Button::open_new(ui).clicked() {
                    self.send_action_request(ActionRequest::Timeline(
                        EntityOrTimelineActionRequest::CreateNew,
                    ));
                }

                ui.separator();
                draw_search_bars(ctx, ui, &mut self.timeline_search);
                ui.separator();
                self.show_timeline_search_results(ui, ctx);
            });

            // Entity column
            columns[1].vertical(|ui| {
                open_timeline_gui_core::Label::sub_heading(ui, "Entities");

                // Button to create a new entity
                if open_timeline_gui_core::Button::open_new(ui).clicked() {
                    self.send_action_request(ActionRequest::Entity(
                        EntityOrTimelineActionRequest::CreateNew,
                    ));
                }

                ui.separator();
                draw_search_bars(ctx, ui, &mut self.entity_search);
                ui.separator();
                self.show_entity_search_results(ui, ctx);
            });
        });
    }
}

/// Draw search bars
fn draw_search_bars<T>(
    ctx: &Context,
    ui: &mut Ui,
    search_info: &mut SearchPartialNameAndBoolTagExpr<T>,
) where
    T: FetchByPartialNameAndBoolTagExpr + IsReducedCollection + Default + 'static,
{
    let changed = {
        // Search bar for searching by entity name
        let name_search_input = ui.add(
            TextEdit::singleline(&mut search_info.name_search)
                .desired_width(f32::INFINITY)
                .hint_text("Name"),
        );
        if name_search_input.changed() {
            search_info.name_search_active = true;
        }
        ui.add_space(5.0);

        // Search bar for searching by entity tag bool expr
        search_info.tag_boolean_expr_search.draw(ctx, ui);
        if search_info.tag_boolean_expr_search.changed() {
            search_info.tag_boolean_expr_search_active =
                !search_info.tag_boolean_expr_search.expr().trim().is_empty();
        }
        search_info.tag_boolean_expr_search.changed() || name_search_input.changed()
    };

    // Refresh search if needed
    if changed {
        search_info.request_reload();
    }
}

impl Reload for SearchGui {
    fn request_reload(&mut self) {
        self.entity_search.request_reload();
        self.timeline_search.request_reload();
    }

    fn check_reload_response(&mut self) {
        self.entity_search.check_reload_response();
        self.timeline_search.check_reload_response();
    }
}

/// Holds everything needed for searching by partial name and bool exprs
#[derive(Debug)]
struct SearchPartialNameAndBoolTagExpr<T>
where
    T: FetchByPartialNameAndBoolTagExpr + IsReducedCollection,
{
    /// Used to derive an ID for the GUI component
    gui_component_id_source: OpenTimelineId,

    /// Whether to search by partial name
    name_search_active: bool,

    /// The partial name to search by (if active)
    name_search: String,

    /// Whether to search by bool tag expr
    tag_boolean_expr_search_active: bool,

    /// The bool tag expr to search by (if active)
    tag_boolean_expr_search: BooleanExpressionGui,

    /// The search results
    search_results: T,

    /// Receive the search results
    rx_search_results: Option<Receiver<Result<T, CrudError>>>,

    /// Database pool
    shared_config: SharedConfig,
}

impl<T> SearchPartialNameAndBoolTagExpr<T>
where
    T: FetchByPartialNameAndBoolTagExpr + IsReducedCollection + Send + Default + 'static,
{
    /// Create a new `SearchPartialNameAndBoolTagExpr`
    fn new(shared_config: SharedConfig) -> Self {
        Self {
            gui_component_id_source: OpenTimelineId::new(),
            name_search_active: true,
            name_search: String::new(),
            tag_boolean_expr_search_active: false,
            tag_boolean_expr_search: BooleanExpressionGui::new(
                ShowRemoveButton::No,
                EmptyConsideredInvalid::No,
                HintText::Default,
            ),
            search_results: T::default(),
            rx_search_results: None,
            shared_config,
        }
    }

    /// Request a new search by just partial name
    fn request_new_search_by_partial_name(&mut self) {
        let partial_name = self.name_search.clone();
        let (tx, rx) = tokio::sync::mpsc::channel(1);
        self.rx_search_results = Some(rx);
        let shared_config = Arc::clone(&self.shared_config);
        spawn_transaction_no_commit_send_result!(
            shared_config,
            bounded,
            tx,
            |transaction| async move {
                T::fetch_by_partial_name(transaction, Limit(SEARCH_LIMIT), &partial_name).await
            }
        );
    }

    /// Request a new search by just bool tag expr
    fn request_new_search_by_bool_tag_expr(&mut self) {
        let bool_tag_expr_result = BoolTagExpr::from(self.tag_boolean_expr_search.expr());
        let (tx, rx) = tokio::sync::mpsc::channel(1);
        self.rx_search_results = Some(rx);
        let shared_config = Arc::clone(&self.shared_config);
        // TODO: can we use our spawn_block_needs_transaction_send_block_result_down_tx!() macro here? (add other with extra preamble arg?)
        tokio::spawn(async move {
            let bool_tag_expr = match bool_tag_expr_result {
                Ok(expr) => expr,
                Err(error) => {
                    let _ = tx.send(Err(CrudError::BoolExprParse(error))).await;
                    return;
                }
            };
            let result = async {
                let mut transaction = shared_config.read().await.db_pool.begin().await?;
                T::fetch_by_bool_tag_expr(&mut transaction, Limit(SEARCH_LIMIT), bool_tag_expr)
                    .await
            }
            .await;
            let _ = tx.send(result).await;
        });
    }

    /// Request a new search by both partial name & bool tag expr
    fn request_new_search_by_partial_name_and_bool_tag_expr(&mut self) {
        // Setup
        let (tx, rx) = tokio::sync::mpsc::channel(1);
        self.rx_search_results = Some(rx);
        let shared_config = Arc::clone(&self.shared_config);

        // Partial name & bool tag expr
        let partial_name = self.name_search.clone();
        let bool_tag_expr_result = BoolTagExpr::from(self.tag_boolean_expr_search.expr());

        // TODO: can we use our spawn_block_needs_transaction_send_block_result_down_tx!() macro here? (add other with extra preamble arg?)
        tokio::spawn(async move {
            let bool_tag_expr = match bool_tag_expr_result {
                Ok(expr) => expr,
                Err(error) => {
                    let _ = tx.send(Err(CrudError::BoolExprParse(error))).await;
                    return;
                }
            };
            let result = async {
                let mut transaction = shared_config.read().await.db_pool.begin().await?;
                T::fetch_by_partial_name_and_bool_tag_expr(
                    &mut transaction,
                    Limit(SEARCH_LIMIT),
                    &partial_name,
                    bool_tag_expr,
                )
                .await
            }
            .await;
            let _ = tx.send(result).await;
        });
    }
}

impl<T> SearchPartialNameAndBoolTagExpr<T>
where
    T: FetchByPartialNameAndBoolTagExpr + IsReducedCollection + Clone + Default + 'static,
    <T as IsReducedCollection>::Item: Clone,
{
    // TODO: show matches count?
    // TODO: impl Draw?
    /// Draw search results to a table
    pub fn show(
        &mut self,
        _ctx: &Context,
        ui: &mut Ui,
    ) -> Option<SearchResultButtonClicked<<T as IsReducedCollection>::Item>> {
        ui.horizontal(|ui| {
            open_timeline_gui_core::Label::strong(ui, "Search By");
            let name_checkbox = ui.checkbox(&mut self.name_search_active, "Name");
            let expr_checkbox =
                ui.checkbox(&mut self.tag_boolean_expr_search_active, "Tag Bool Expr");
            if name_checkbox.changed() || expr_checkbox.changed() {
                self.request_reload();
            }
        });
        ui.separator();

        let available_width = ui.available_width();
        let table_height = ui.available_height();

        // Marshall search results
        let search_results = match (self.name_search_active, self.tag_boolean_expr_search_active) {
            (false, false) => None,
            _ => (!self.search_results.collection().is_empty())
                .then_some(self.search_results.clone()),
        };

        // Results
        match search_results {
            // If there are no search results
            None => {
                // Maintain the sizing of the search result area
                ui.allocate_ui(Vec2::from([available_width, table_height]), |ui| {
                    ui.set_min_size(Vec2::from([available_width, table_height]));
                    open_timeline_gui_core::Label::none(ui);
                });
                None
            }
            // If there are results to draw
            Some(search_results) => {
                // Sizes
                let row_height = body_text_height(ui);
                let spacing = widget_x_spacing(ui);
                let text_width =
                    available_width - VIEW_BUTTON_WIDTH - EDIT_BUTTON_WIDTH - (2.0 * spacing);
                let text_width = text_width.max(0.0);

                // Which to view or edit if there is one
                let mut to_view_or_edit = None;

                // Show the results in a table in a scrollable area
                ScrollArea::vertical()
                    .max_height(table_height)
                    .id_salt(format!("{}_scroll_area", self.gui_component_id_source))
                    .show(ui, |ui| {
                        ui.set_min_size(Vec2::from([available_width, table_height]));
                        TableBuilder::new(ui)
                            .id_salt(format!("{}_table", self.gui_component_id_source))
                            .striped(true)
                            .column(Column::exact(text_width).clip(true))
                            .column(Column::exact(EDIT_BUTTON_WIDTH))
                            .column(Column::exact(VIEW_BUTTON_WIDTH))
                            .body(|mut body| {
                                for (index, reduced_entity) in
                                    search_results.collection().iter().enumerate()
                                {
                                    if index as u32 > SEARCH_LIMIT {
                                        break;
                                    }
                                    body.row(row_height, |mut row| {
                                        // Name
                                        row.col(|ui| {
                                            let layout = Layout::left_to_right(Align::Center);
                                            let label =
                                                egui::Label::new(reduced_entity.name().as_str());
                                            ui.with_layout(layout, |ui| {
                                                ui.add(label.truncate());
                                            });
                                        });
                                        // Edit
                                        row.col(|ui| {
                                            if OpenTimelineButton::edit(ui).clicked() {
                                                to_view_or_edit =
                                                    Some(SearchResultButtonClicked::Edit(
                                                        reduced_entity.clone(),
                                                    ));
                                            }
                                        });
                                        // View
                                        row.col(|ui| {
                                            if OpenTimelineButton::view(ui).clicked() {
                                                to_view_or_edit =
                                                    Some(SearchResultButtonClicked::View(
                                                        reduced_entity.clone(),
                                                    ));
                                            }
                                        });
                                    });
                                }
                            });
                    });
                to_view_or_edit
            }
        }
    }
}

/// Used to indicate whether the edit or the view button was clicked.
pub enum SearchResultButtonClicked<T> {
    View(T),
    Edit(T),
}

impl<T> Reload for SearchPartialNameAndBoolTagExpr<T>
where
    T: FetchByPartialNameAndBoolTagExpr + IsReducedCollection + Default + 'static,
{
    fn request_reload(&mut self) {
        match (self.name_search_active, self.tag_boolean_expr_search_active) {
            (false, false) => {
                self.search_results.collection_mut().clear();
                self.rx_search_results = None;
            }
            (true, false) => self.request_new_search_by_partial_name(),
            (false, true) => self.request_new_search_by_bool_tag_expr(),
            (true, true) => self.request_new_search_by_partial_name_and_bool_tag_expr(),
        };
    }

    fn check_reload_response(&mut self) {
        if let Some(rx) = self.rx_search_results.as_mut() {
            if let Ok(data) = rx.try_recv() {
                match data {
                    Ok(results) => self.search_results = results,
                    Err(_) => (),
                }
            }
        }
    }
}