Repository navigation
Expand file tree
/
Copy pathview_items.cpp
More file actions
3557 lines (3004 loc) · 116 KB
/
Copy pathview_items.cpp
File metadata and controls
3557 lines (3004 loc) · 116 KB
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of the Diffractor photo and video organizer
// Copyright 2026 Zac Walker
//
// This program is free software; you can redistribute it and / or modify it
// under the terms of the LGPL License either version 2.1 or later.
// License details are available at https://www.gnu.org/licenses/lgpl-2.1.html
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
// Purpose: Items grid/list view. Displays thumbnails in grid layout, handles
// selection, keyboard navigation, context menus, and item interactions. Also
// builds the selection detail pane, including the verbose metadata block tree.
#include "pch.h"
#include "model.h"
#include "model_index.h"
#include "model_db.h"
#include "ui_view.h"
#include "ui_dialog.h"
#include "ui_controls.h"
#include "ui_document.h"
#include "ui_sid.h"
#include "view_items.h"
#include "app_sidebar.h"
static view_element_ptr find_center_scroll_element(const std::vector<view_element_ptr>& elements,
const view_scroller& scroller)
{
const auto logical_bounds = scroller.client_bounds().offset(0, scroller.scroll_offset().y);
const auto center_y = logical_bounds.center().y;
auto closest_distance = INT_MAX;
view_element_ptr result;
for (const auto& element : elements)
{
if (!element->is_visible() || !element->bounds.intersects(logical_bounds)) continue;
const auto distance = std::abs(element->bounds.center().y - center_y);
if (distance < closest_distance)
{
closest_distance = distance;
result = element;
}
}
return result;
}
// Shared pointer behaviour for the item grid. Both grid gestures track the item under the pointer,
// scrub preview-video frames while hovering and open on double click; only what happens between
// button down and button up differs.
class item_pointer_controller : public view_controller
{
protected:
items_view& _parent;
view_scroller& _scroller;
view_state& _state;
bool _tracking = false;
bool _cancel = false;
pointi _last_logical_loc;
pointi _start_logical_loc;
df::item_element_ptr _hover_item;
item_pointer_controller(const view_host_ptr& host, items_view& parent, view_state& s, view_scroller& scroller,
df::item_element_ptr i, const pointi loc) :
view_controller(host, {}), _parent(parent), _scroller(scroller), _state(s),
_last_logical_loc(scroller.device_to_logical(loc)), _start_logical_loc(_last_logical_loc),
_hover_item(std::move(i))
{
if (_hover_item)
{
_state.hover_item(_host, _hover_item, true);
// The item's own region, expanded caption and all. Keeping it inside the grid, and keeping
// the pin badge reachable, are the hit test's business rather than this constructor's.
_bounds = _scroller.logical_to_device(_hover_item->interactive_bounds());
}
}
~item_pointer_controller() override
{
if (_hover_item)
{
_state.hover_item(_host, _hover_item, false);
}
}
// Scrubs the preview frame of a hovered video from the pointer x position.
void update_hover_preview(const pointi loc) const
{
if (_tracking || !_hover_item || !_hover_item->file_type()->has_trait(file_traits::preview_video)) return;
const auto item_bounds = _hover_item->bounds;
if (item_bounds.contains(_scroller.device_to_logical(loc)))
{
_state.load_hover_thumb(_hover_item, loc.x - item_bounds.left, item_bounds.width());
}
}
void open_item(const df::item_element_ptr& i)
{
if (!i) return;
_state.open(_parent._host, i);
if (i->file_type()->is_media() && !_state.is_full_screen)
{
_state.toggle_full_screen();
}
_state.stop_slideshow();
_cancel = true;
}
public:
bool escape() override
{
if (!_tracking) return false;
_cancel = true;
return true;
}
};
class item_select_controller final : public item_pointer_controller
{
bool _selecting = false;
public:
item_select_controller(const view_host_ptr& host, items_view& parent, view_state& s, view_scroller& scroller,
df::item_element_ptr i, const pointi loc) :
item_pointer_controller(host, parent, s, scroller, std::move(i), loc)
{
}
~item_select_controller() override
{
// Ending clears the highlight bit across every group the selection rectangle touched,
// so there is no need to walk all items here.
if (_tracking)
{
end_selecting();
}
}
platform::cursor cursor() const override
{
return _selecting ? platform::cursor::select : platform::cursor::normal;
}
recti calc_logical_selection_bounds() const
{
return _selecting ? recti(_start_logical_loc, _last_logical_loc).normalise() : recti();
}
void draw(render::draw_context& rc) override
{
if (_selecting)
{
// Drawn from the logical rectangle rather than the device one, because auto-scroll moves
// the content under a stationary pointer: a device-space marquee then sat still over items
// that were not going to be selected. Clipped to the grid so it never paints over the
// preview pane or the sidebar.
const auto selection = _scroller.logical_to_device(calc_logical_selection_bounds())
.intersection(_scroller.client_bounds());
if (!selection.is_empty())
{
rc.draw_rect(selection, core::color(ui::style::colors.dialog_selected_background, 0.5));
}
}
}
void on_mouse_left_button_down(const pointi loc, const platform::key_state keys) override
{
_tracking = true;
_cancel = false;
_last_loc = _start_loc = loc;
_last_logical_loc = _start_logical_loc = _scroller.device_to_logical(loc);
update_state();
}
void on_mouse_move(const pointi loc) override
{
_last_loc = loc;
update_state();
update_hover_preview(loc);
}
void update_state()
{
const auto previous_sel_bounds = calc_logical_selection_bounds();
const auto start_area = center_rect({8, 8}, _start_loc);
_selecting = _tracking && !_cancel && !start_area.contains(_last_loc);
_last_logical_loc = _scroller.device_to_logical(_last_loc);
apply_highlight(previous_sel_bounds);
}
// The highlight bit is transient rubber-band paint that is drawn like selection but is not
// selection, so the gesture that set it also clears it. Left behind it makes an unselected item
// keep a selected background that the selection list contradicts and no click can clear.
void end_selecting()
{
const auto previous_sel_bounds = calc_logical_selection_bounds();
_selecting = false;
_tracking = false;
apply_highlight(previous_sel_bounds);
}
void apply_highlight(const recti previous_sel_bounds) const
{
const auto logical_selection_bounds = calc_logical_selection_bounds();
const auto changed_bounds = logical_selection_bounds.make_union(previous_sel_bounds);
for (const auto& group : _state.groups())
{
if (group->bounds.intersects(changed_bounds))
{
for (const auto& i : group->items())
{
i->set_style_bit(view_element_style::highlight, i->bounds.intersects(logical_selection_bounds),
_host, i);
}
}
}
_host->frame()->invalidate();
}
void invoke_select(const platform::key_state keys, const bool perform_open)
{
if (_cancel) return;
if (_selecting)
{
_state.select(_parent._host, calc_logical_selection_bounds(), keys.control);
_state.stop_slideshow();
return;
}
const auto i = _state.item_from_location(_last_logical_loc);
if (i)
{
_state.select(_parent._host, i, keys.control, keys.shift, false);
_state.stop_slideshow();
if (perform_open)
{
open_item(i);
}
}
}
void on_mouse_left_button_up(const pointi loc, const platform::key_state keys) override
{
_last_loc = loc;
update_state();
invoke_select(keys, false);
end_selecting();
}
void on_mouse_left_button_double_click(const pointi loc, const platform::key_state keys) override
{
_last_loc = loc;
update_state();
invoke_select(keys, true);
end_selecting();
}
bool escape() override
{
if (!_tracking) return false;
_cancel = true;
update_state();
return true;
}
void tick() override
{
if (_selecting)
{
constexpr auto offset = 32;
const auto client_bounds = _scroller.client_bounds();
constexpr auto scroll_zone = 64;
if (_last_loc.y < client_bounds.top + scroll_zone)
{
_scroller.offset(_host, 0, -offset);
}
else if (_last_loc.y > client_bounds.bottom - scroll_zone)
{
_scroller.offset(_host, 0, offset);
}
else
{
return;
}
// The pointer is stationary while the content moves, so the logical end of the rectangle
// only follows if it is recomputed here. Without this the highlight froze during the
// scroll and the whole scrolled-past run appeared in the selection at the release.
update_state();
}
}
};
class item_drag_controller final : public item_pointer_controller
{
bool _drag_started = false;
public:
item_drag_controller(const view_host_ptr& host, items_view& parent, view_state& s, view_scroller& scroller,
df::item_element_ptr i, const pointi loc) :
item_pointer_controller(host, parent, s, scroller, std::move(i), loc)
{
}
void on_mouse_left_button_down(const pointi loc, const platform::key_state keys) override
{
_tracking = true;
_cancel = false;
_last_loc = _start_loc = loc;
_last_logical_loc = _start_logical_loc = _scroller.device_to_logical(loc);
_drag_started = false;
}
void on_mouse_move(const pointi loc) override
{
_last_loc = loc;
// Leaving the tile is not enough on its own: the tiles abut, so a press near an edge turned a
// one-pixel twitch into a drag of the whole selection. The same start area the rubber band
// uses has to be cleared as well, so one surface does not hold two thresholds.
if (_tracking && !_cancel && !_bounds.contains(loc) && !center_rect({8, 8}, _start_loc).contains(loc))
{
start_drag();
}
else
{
update_hover_preview(loc);
}
}
void on_mouse_left_button_up(const pointi loc, const platform::key_state keys) override
{
_last_loc = loc;
_tracking = false;
if (!_drag_started && !_cancel && _hover_item)
{
_state.select(_parent._host, _hover_item, keys.control, keys.shift, false);
_state.stop_slideshow();
}
}
void on_mouse_left_button_double_click(const pointi loc, const platform::key_state keys) override
{
_last_loc = loc;
open_item(_hover_item);
}
void start_drag()
{
// Letting go of the displayed media waits, handling input meanwhile, and a move handled there
// came back in here. One press starts one drag.
if (!_hover_item || _drag_started) return;
_drag_started = true;
core::scope_locked_inc l(df::dragging_items);
detach_file_handles detach(_state);
// The release or Escape that ends the gesture can be handled in that wait too, and a drag begun
// without the button held drops wherever the next click lands.
if (_tracking && !_cancel)
{
_host->perform_drag(_state.selected_items().file_paths(), _state.selected_items().folder_paths());
}
_tracking = false;
_state.invalidate_view(view_invalid::controller);
}
};
void items_view::activate(const sizei extent)
{
_client_extent = extent;
_visible_items_valid = false;
update_media_elements();
}
void items_view::deactivate()
{
// A view that is not on screen claims no item visibility and queues no thumbnail work.
// app_frame::tick drives retry_visible_thumbnails whichever view is current, so a retained
// working set would keep scanning for a grid the user can no longer see.
for (const auto& i : _visible_items) i.i->is_visible(false);
_visible_items.clear();
_draw_items.clear();
_visible_items_valid = false;
// Nor does it keep the keyboard for a document no longer on screen: Copy in the view that
// replaces it acts on what that view shows.
release_document();
}
// The panel's document gives Copy, Cut and Select all back to the items.
void items_view::release_document() const
{
if (_display && _display->_document && _display->_document->focused)
{
_display->_document->focused = false;
if (_host) _host->invalidate(ui::host_invalid::redraw);
}
}
void items_view::refresh()
{
_state.open(_host, _state.search(), {});
}
items_view::items_view(view_state& s, view_host_ptr host) :
_state(s),
_host(std::move(host)),
_sidebar(std::make_shared<sidebar_host>(s))
{
_items_scroller.popup_func = [this](view_hover_element& hover, const pointi loc)
{
items_scroll_popup(hover, loc);
};
_items_scroller.changed_func = [this] { update_visible_items_list(); };
_media_scroller.changed_func = [this] { _state.invalidate_view(view_invalid::view_redraw); };
_metadata_tree->invalidate = [this]
{
_state.invalidate_view(view_invalid::view_layout | view_invalid::view_redraw | view_invalid::controller);
};
}
items_view::~items_view()
{
// A listing held by a latched controller can outlive the view; without this its next toggle
// would call back into a destroyed view.
_metadata_tree->invalidate = nullptr;
ui::animations.erase(this);
}
bool items_view::sidebar_visible() const
{
return setting.show_sidebar && _sidebar_width > 0 && _display && !_display->is_zoom_mode() && !_display->
comparing();
}
int items_view::content_left() const
{
return sidebar_visible() ? _sidebar_width + _sidebar_splitter_width : 0;
}
recti items_view::sidebar_bounds() const
{
return sidebar_visible()
? recti{0, 0, _sidebar_width + _sidebar_splitter_width, _client_extent.cy}
: recti{};
}
recti items_view::sidebar_splitter_bounds() const
{
return sidebar_visible()
? recti{
_sidebar_width, 0, _sidebar_width + _sidebar_splitter_width,
_sidebar->can_scroll() ? _client_extent.cy / 2 : _client_extent.cy
}
: recti{};
}
void items_view::sidebar_width(const int x)
{
const auto min_width = std::max(_scroll_width * 3, core::round(64 * _scale_factor));
const auto max_width = std::max(min_width, _client_extent.cx / 3);
const auto width = std::clamp(x, min_width, max_width);
if (width == _sidebar_width) return;
setting.sidebar.width = core::round(width / _scale_factor);
_state.invalidate_view(view_invalid::view_layout);
}
class sidebar_splitter_controller final : public view_controller
{
items_view& _view;
int _start_width = 0;
bool _tracking = false;
public:
sidebar_splitter_controller(const view_host_ptr& host, items_view& view, const recti bounds) :
view_controller(host, bounds), _view(view)
{
_view._sidebar_splitter_active++;
}
~sidebar_splitter_controller() override
{
_view._sidebar_splitter_active--;
}
void draw(render::draw_context& dc) override
{
_view.draw_splitter(dc, _view.sidebar_splitter_bounds(), true, _tracking);
}
platform::cursor cursor() const override
{
return platform::cursor::left_right;
}
void on_mouse_left_button_down(const pointi loc, const platform::key_state keys) override
{
_tracking = true;
_start_width = _view._sidebar_width;
_view.sidebar_width(loc.x);
}
void on_mouse_move(const pointi loc) override
{
if (_tracking) _view.sidebar_width(loc.x);
}
void on_mouse_left_button_up(const pointi loc, const platform::key_state keys) override
{
if (_tracking) _view.sidebar_width(loc.x);
_tracking = false;
// The width is persisted once here rather than on every move, which would write the whole
// settings store to the registry for each pointer message.
_view._state.invalidate_view(view_invalid::options_save);
}
bool escape() override
{
if (!_tracking) return false;
_tracking = false;
_view.sidebar_width(_start_width);
_view._state.invalidate_view(view_invalid::options_save);
return true;
}
};
bool items_view::is_caption_area(const pointi loc) const
{
return false;
}
// The list's own controls sit at the end of the status bar, where they stay on screen however far
// the list scrolls: size and filter first, then the type filters and subfolders, then the totals
// beside the grouping they are not part of. The bar starts under the sidebar with one toolbar: the
// Options cog, beside the overview of the collection Options opens on; Tools and Playback options, as
// icons; then Open, labelled with what is selected, where a file browser's status bar says it.
view_bars items_view::bars() const
{
return {
.navigation = true,
.bottom = {
bar_control::item_size, bar_control::item_filter, commands::filter_photos, commands::filter_videos,
commands::filter_audio, commands::browse_recursive, bar_control::item_totals,
commands::menu_group_toolbar
},
.bottom_leading = {
commands::options, commands::menu_tools, commands::playback_menu, commands::menu_open_status,
},
};
}
namespace
{
std::shared_ptr<link_element> make_chip(std::string text, std::function<void()> invoke,
std::function<void(view_hover_element&)> tooltip, const bool latched)
{
auto chip = std::make_shared<link_element>(std::move(text), std::move(invoke), std::move(tooltip),
render::font_face::dialog,
render::text_style::multiline_center,
flex_item::center);
chip->full_background(true);
chip->padding = {6, 4};
chip->margin = {3, 0};
chip->set_style_bit(view_element_style::selected, latched);
return chip;
}
// locations.md 7.3: a collapsed row still has to say what it is and how much it holds, or the
// user cannot tell whether opening it is worth the height it will take.
view_element_ptr make_row_header(const std::string_view title, const uint32_t count,
std::function<void()> expand)
{
auto header = std::make_shared<link_element>(
std::format("{}|{}", title, core::format_count(count)), std::move(expand),
render::font_face::dialog, render::text_style::single_line, flex_item::center);
header->padding = {6, 4};
return header;
}
}
// locations.md 7.3: the breakdown row is the only optional row, and it collapses to a one-line
// header when it would otherwise bury the items the user came to see. It never holds a captured
// control while it is asked to collapse.
void items_view::apply_control_row_budget(render::measure_context& mc, const int width, const int budget)
{
if (!_breakdown_row.element) return;
_breakdown_row.collapsed(false);
const auto natural_height = _breakdown_row.element->measure(mc, width).cy;
_breakdown_row.collapsed(natural_height > budget && !_breakdown_expanded);
}
// locations.md 7.2: the top places inside the current results. Hidden when the grouping is
// already by location, because the group headers already say this.
// A related search answers about one item, so the view says which item and how that item stands
// against the collection. That explanation used to be reachable only by hovering a badge, where it
// could not be read alongside the answer it belongs to (docs/collections.md section 7).
view_element_ptr items_view::build_related_header_row() const
{
const auto& search = _state.search();
if (!search.has_related()) return {};
const auto& related = search.related();
const auto name = related.path.name();
if (name.is_empty()) return {};
auto row = std::make_shared<view_elements>(flex_item::center);
auto title = std::make_shared<text_element>(std::format("{} | {}", tt.command_related.sv(), name.sv()),
render::font_face::dialog,
render::text_style::single_line_center, flex_item::center);
title->padding = {6, 4};
row->add(std::move(title));
// The anchor is part of its own answer, so its presence is read from the results rather than
// recomputed here. It stays absent while presence is still resolving.
for (const auto& item : _state.search_items().items())
{
if (item->path() != related.path) continue;
if (item->presence() == item_presence::unknown) break;
auto presence = std::make_shared<text_element>(item_presence_text(item->presence(), false),
render::font_face::dialog,
render::text_style::single_line_center,
flex_item::center);
presence->padding = {6, 4};
row->add(std::move(presence));
break;
}
return row;
}
view_element_ptr items_view::build_location_breakdown_row()
{
_breakdown_row.reset();
const auto& timeline = _state.visits();
const auto current = _state.search();
const auto has_location_context = current.has_term_type(df::search_term_type::location) ||
current.has_term_type(df::search_term_type::area);
const auto want_places = has_location_context && _state.group_order() != group_by::location &&
timeline.places.size() > 1;
if (!want_places) return {};
auto row = std::make_shared<view_elements>(flex_item::stretch);
auto total_count = 0u;
if (want_places)
{
for (const auto& place : timeline.places)
{
total_count += place.count;
auto text = std::format("{}|{}", place.name, core::format_count(place.count));
// The bubble names the place in full, because the chip is only as specific as it can
// be in the width it has.
const auto action = df::visit_place_detail(place);
_breakdown_row.content.emplace_back(make_chip(std::move(text), [this, place]
{
_state.open(
_host, df::visit_place_search(_state.search(), place),
{});
}, [action](const view_hover_element& hover)
{
hover.elements->add(
std::make_shared<action_element>(action));
}, false));
}
}
if (_breakdown_row.content.empty())
{
_breakdown_row.reset();
return {};
}
_breakdown_row.header = make_row_header(tt.places_title.sv(), total_count, [this]
{
_breakdown_expanded = true;
_state.invalidate_view(view_invalid::group_layout_complete);
});
row->add(_breakdown_row.content);
row->add(view_element_ptr(_breakdown_row.header));
_breakdown_row.element = row;
_breakdown_row.collapsed(false);
return row;
}
void items_view::items_changed(const bool path_changed)
{
if (!path_changed)
{
const auto focus = _state.focus_item();
const auto anchor = focus && is_visible(focus) ? focus : _layout_center_item;
_pending_items_anchor = _items_scroller.capture_anchor(anchor);
}
else
{
_pending_items_anchor = {};
}
if (path_changed)
{
_items_scroller.reset();
_media_scroller.reset();
}
std::vector<view_element_ptr> elements;
elements.reserve(_state.groups().size() * 3 + 2);
const auto has_items = _state.has_display_items();
const auto total_count = _state.search_items().size();
const auto shown_count = _state.display_items().size();
if (auto related_row = build_related_header_row())
{
elements.emplace_back(std::move(related_row));
}
// locations.md 7.3: the breakdown strip sits above the timeline, so the rows read from
// "where within this" down to "when within this".
if (auto breakdown_row = build_location_breakdown_row())
{
elements.emplace_back(std::move(breakdown_row));
}
for (const auto& g : _state.groups())
{
elements.emplace_back(build_group_title(_state, _host, g));
elements.emplace_back(g);
}
// The footer is one flex column: the status lines stack and centre, and every action sits in a
// single wrapping row below them. Gaps do the spacing, so no growing spacer can push the lines
// apart when the window is taller than the results.
constexpr auto footer_message_width = 420; // logical units; keeps a message to a readable measure
std::vector<view_element_ptr> status_elements;
std::vector<view_element_ptr> action_elements;
const auto is_searching = _state.item_index.searching > 0;
const auto has_filtered_out = !is_searching && total_count > shown_count;
const auto add_status = [&status_elements](const std::string_view text, const render::font_face font)
{
auto element = std::make_shared<text_element>(text, font, render::text_style::multiline_center,
flex_item::center);
element->flex.max_size.cx = footer_message_width;
element->padding = {8, 2};
status_elements.emplace_back(std::move(element));
};
const auto add_action = [&action_elements]<typename action_t>(const std::string_view text, action_t action)
{
auto link = std::make_shared<link_element>(text, std::move(action), render::font_face::dialog,
render::text_style::single_line_center,
flex_item::center, true);
link->flex.max_size.cx = footer_message_width;
link->padding = {8, 6};
action_elements.emplace_back(std::move(link));
};
if (is_searching)
{
add_status(tt.searching_text, render::font_face::title);
}
else if (!has_items && (_state.search().has_selector() || _state.search().has_terms()))
{
// Give feedback when a committed search / path resolves to no items, instead of silently
// showing an empty view. A folder browse just shows "Empty Folder"; an actual search shows
// "Nothing found" plus guidance on widening the index. A folder emptied by the type filters
// is not empty, so it says "Nothing found" and leaves the recovery to the filter action.
const auto browsing_folder = _state.search().is_folder();
add_status(browsing_folder && !has_filtered_out ? tt.empty_folder : tt.nothing_found1,
render::font_face::title);
if (!browsing_folder && !has_filtered_out)
{
// locations.md 3.7: a location query that finds nothing must explain itself and offer
// the next action. That moment is where trust in the search is won or lost.
if (const auto* const place_term = _state.search().single_place_term())
{
const auto typed = place_term->text;
const auto km = place_term->float_val;
add_status(km > 0.0
? str_format(tt.nothing_within_fmt.sv(), format_distance_km(km), typed)
: str_format(tt.nothing_at_place_fmt.sv(), typed),
render::font_face::dialog);
if (km > 0.0)
{
if (const auto wider = location_distance_at_detent(
location_nearest_distance_detent(km) + 1); wider > km)
{
add_action(str_format(tt.widen_to_fmt.sv(), format_distance_km(wider)),
std::function<void()>([this, wider]
{
auto search = _state.search();
search.set_place_distance(wider);
_state.open(_host, search, {});
}));
}
}
// A qualified name that resolved to the wrong namesake -- offer all of them.
if (const auto comma = typed.find(','); comma != std::string::npos)
{
auto bare = typed.substr(0, comma);
while (!bare.empty() && bare.back() == ' ') bare.pop_back();
add_action(str_format(tt.search_all_named_fmt.sv(), bare),
std::function<void()>([this, bare]
{
auto search = _state.search();
search.set_place_name(bare);
_state.open(_host, search, {});
}));
}
// So a user never mistakes "not placed" for "not present".
add_action(tt.show_without_location.sv(),
std::function<void()>([this]
{
auto search = _state.search();
search.clear_terms();
search.without(df::search_term_type::has_location);
_state.open(_host, search, {});
}));
}
// State the collection's edge, then offer the one move that widens it.
add_status(tt.nothing_found2, render::font_face::dialog);
add_action(tt.nothing_found_add_folders.sv(),
std::function<void()>([this] { _state.invoke(commands::options); }));
}
}
if (has_filtered_out)
{
add_action(str_format(tt.some_items_filtered_fmt.sv(), total_count - shown_count),
std::function<void()>([this] { _state.clear_filters(); }));
}
if (has_items && is_date_group_order(_state.effective_group_order()))
{
add_action(setting.sort_dates_descending
? tt.command_sort_dates_ascending.sv()
: tt.command_sort_dates_descending.sv(),
setting.sort_dates_descending
? commands::sort_dates_ascending
: commands::sort_dates_descending);
}
if (!status_elements.empty() || !action_elements.empty())
{
if (has_items)
{
auto divider = std::make_shared<divider_element>();
divider->margin = {0, 8};
elements.emplace_back(std::move(divider));
}
auto footer = std::make_shared<view_elements>(flex_item::stretch);
footer->flex_container.direction = flex_direction::column;
footer->flex_container.wrap = flex_wrap::no_wrap;
footer->flex_container.align_items = flex_align::center;
footer->flex_container.gap = {0, 8};
footer->flex_container.padding = {8, has_items ? 8 : 32};
footer->add(status_elements);
if (!action_elements.empty())
{
auto actions = std::make_shared<view_elements>(flex_item::stretch);
actions->flex_container.justify = flex_justify::center;
actions->flex_container.gap = {8, 8};
actions->add(action_elements);
footer->add(std::move(actions));
}
elements.emplace_back(std::move(footer));
}
elements.emplace_back(std::make_shared<padding_element>(32));
std::swap(_item_elements, elements);
_state.invalidate_view(view_invalid::view_layout);
}
void items_view::draw_splitter(render::draw_context& dc, const recti bounds, const bool active,
const bool tracking) const
{
draw_splitter_handle(dc, bounds, _sidebar_splitter_width, active, tracking);
}
class media_splitter_controller final : public view_controller
{
public:
items_view& _view;
int _start = 0;
pointi _start_loc;
bool _tracking = false;
media_splitter_controller(const view_host_ptr& host, items_view& view,
const recti bounds) : view_controller(host, bounds), _view(view)
{
_view._splitter_active++;
}
~media_splitter_controller() override
{
if (_tracking)
{
escape();
}
_view._splitter_active--;
}
void draw(render::draw_context& rc) override
{
_view.draw_splitter(rc, _view.calc_spliter_bounds(), true, _tracking);
}
platform::cursor cursor() const override
{
return platform::cursor::left_right;
}
void update_pos(const pointi loc) const
{
_view.splitter_pos(loc.x, _tracking);
}
void on_mouse_left_button_down(const pointi loc, const platform::key_state keys) override
{
_tracking = true;
_start = _view.splitter_pos();
_start_loc = loc;
update_pos(loc);
}
void on_mouse_move(const pointi loc) override
{
if (_tracking)
{
update_pos(loc);
}
}
void on_mouse_left_button_up(const pointi loc, const platform::key_state keys) override
{
if (_tracking)
{
_tracking = false;
update_pos(loc);
}
}
bool escape() override
{
if (!_tracking) return false;
_tracking = false;
_view.splitter_pos(_start, false);
return true;
}
};
// design.md L5: Escape peels one layer. With a rectangle on the picture it clears it, and with text
// selected in the panel's document it clears that; without either it falls through to the unwind
// ladder view_state::escape owns, so there is one order rather than two.
bool items_view::escape()
{
if (const auto photo = std::dynamic_pointer_cast<photo_control>(_media_element); photo && photo->has_region())
{