Repository navigation
Expand file tree
/
Copy pathapp_commands.cpp
More file actions
2675 lines (2342 loc) · 95.4 KB
/
Copy pathapp_commands.cpp
File metadata and controls
2675 lines (2342 loc) · 95.4 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: Command handlers for all user actions. Implements file operations, editing,
// sharing, metadata updates, and other application commands, and registers what each command does.
// The dialogs that are features in their own right are in app_dialogs.cpp.
#include "pch.h"
#include "util_top.h"
#include "model.h"
#include "model_index.h"
#include "model_db.h"
#include "model_tokenizer.h"
#include "ui_controls.h"
#include "ui_dialog.h"
#include "ui_controllers.h"
#include "view_edit.h"
#include "view_movie.h"
#include "view_items.h"
#include "app_match.h"
#include "app_text.h"
#include "app_command_status.h"
#include "app_util.h"
#include "app.h"
#include "view_import.h"
#include "view_locate.h"
#include "view_rename.h"
#include "view_batch.h"
#include "view_sync.h"
#include "view_tags.h"
#include "app_dialogs.h"
extern bool toggle_details_state;
bool can_process_selection_or_explain(const view_state& s, const view_host_base_ptr& view,
const ui::control_frame_ptr& parent, const std::string_view title,
const df::process_items_type type)
{
const auto can_process = s.can_process_selection_and_mark_errors(view, type);
if (!can_process.fail()) return true;
make_dlg(parent)->show_message(icon_index::error, title, can_process.to_string());
return false;
}
static void zoom_invoke(const view_state& s, const ui::control_frame_ptr& parent)
{
const auto display = s.display_state();
if (display && display->can_zoom())
{
display->toggle_zoom();
}
};
static void zoom_fit_invoke(const view_state& s)
{
if (const auto display = s.display_state(); display && display->can_zoom()) display->zoom(false);
}
static void zoom_fit_variant_invoke(const view_state& s, const df::zoom_scale_mode mode)
{
if (const auto display = s.display_state(); display && display->can_zoom()) display->zoom_fit_variant(mode);
}
static void zoom_100_invoke(const view_state& s)
{
if (const auto display = s.display_state(); display && display->can_zoom()) display->zoom_100();
}
static void zoom_toggle_fit_invoke(const view_state& s)
{
if (const auto display = s.display_state(); display && display->can_zoom()) display->toggle_zoom_fit();
}
static void zoom_step_invoke(const view_state& s, const int direction)
{
if (const auto display = s.display_state(); display && display->can_zoom()) display->adjust_zoom_scale(direction);
}
static void pin_invoke(view_state& s)
{
// Only one item is ever held, so the command that reports a pin is held must also release it.
// Re-pointing it at whatever has focus instead would leave no way back to plain browsing
// without walking focus to the pinned item first.
if (s.has_pin())
{
s._pin_item.reset();
}
else
{
s._pin_item = s.focus_item();
}
s.invalidate_view(view_invalid::view_layout | view_invalid::view_redraw | view_invalid::command_state);
}
static void edit_invoke(view_state& s)
{
if (s.can_edit_media())
{
s.view_mode(view_type::edit);
}
}
static void containing_folder_invoke(view_state& s, const ui::control_frame_ptr& parent, const view_host_base_ptr& view)
{
const auto i = s.command_item();
if (i)
{
df::unique_paths selection;
selection.emplace(i->path());
s.open(view, i->containing(), selection);
}
}
static void open_in_file_browser_invoke(const view_state& s, const ui::control_frame_ptr& parent,
const view_host_base_ptr& view)
{
if (can_process_selection_or_explain(s, view, parent, tt.open_in_browser_title,
df::process_items_type::local_file_or_folder))
{
const auto f = s.command_item();
if (f)
{
const auto file = std::dynamic_pointer_cast<df::item_element>(f);
if (file)
{
platform::show_in_file_browser(file->path());
}
}
}
}
static void new_folder_invoke(view_state& s, const ui::control_frame_ptr& parent, const view_host_base_ptr& view)
{
if (!s.search().is_showing_folder()) return;
int i = 2;
const auto folder = s.save_path();
auto new_name = std::string(tt.new_folder_name);
auto new_path = folder.combine(new_name);
while (platform::exists(new_path) && i < 100)
{
new_name = std::format("{} {}", tt.new_folder_name, i++);
new_path = folder.combine(new_name);
}
const auto create_folder_result = platform::create_folder(new_path);
if (create_folder_result.failed())
{
const auto dlg = make_dlg(parent);
dlg->show_message(icon_index::folder, tt.new_folder_title, create_folder_result.format_error());
}
else
{
df::unique_paths selection;
selection.emplace(new_path);
s.open(view, s.search(), selection);
}
}
static void burn_command_invoke(view_state& s, const ui::control_frame_ptr& parent,
const view_host_base_ptr& view)
{
const auto dlg = make_dlg(parent);
const auto title = tt.burn_title;
pause_media pause(s);
if (can_process_selection_or_explain(s, view, parent, title, df::process_items_type::local_file_or_folder))
{
const auto items = s.selected_items();
auto title_control = std::make_shared<ui::title_control2>(
dlg->_frame, icon_index::disk, title, format_plural_text(tt.burn_info_fmt, items),
std::vector<ui::const_surface_ptr>{}, items.size());
title_control->selection_async(items.thumbs(), items.size(), s._async);
const std::vector<view_element_ptr> controls = {
set_margin(title_control),
std::make_shared<divider_element>(),
set_margin(std::make_shared<text_element>(tt.burn_help)),
std::make_shared<divider_element>(),
std::make_shared<ui::ok_cancel_control>(dlg->_frame, tt.burn_stage),
};
if (dlg->show_modal(controls) == ui::close_result::ok)
{
record_feature_use(features::burn_to_disk);
if (!platform::burn_to_cd(items.file_paths(true), items.folder_paths()))
{
dlg->show_message(icon_index::error, title, tt.burn_failed);
}
}
}
}
static void print_invoke(const view_state& s, const ui::control_frame_ptr& parent, const view_host_base_ptr& view)
{
if (can_process_selection_or_explain(s, view, parent, tt.print_title, df::process_items_type::local_file))
{
const auto items = s.selected_items();
record_feature_use(features::print);
platform::print(items.file_paths(false), items.folder_paths());
}
}
quadd rotate_command_crop_bounds(const sizei dimensions, const ui::orientation stored_orientation,
const simple_transform transform, const bool show_rotated)
{
const auto current_orientation = show_rotated ? stored_orientation : ui::orientation::top_left;
return quadd(dimensions).transform(to_simple_transform_inv(current_orientation)).transform(transform);
}
static void rename_invoke(view_state& s, const ui::control_frame_ptr& parent, const view_host_base_ptr& view)
{
const auto title = tt.command_rename;
constexpr auto icon = icon_index::rename;
const auto dlg = make_dlg(parent);
pause_media pause(s);
if (can_process_selection_or_explain(s, view, parent, title, df::process_items_type::local_file_or_folder))
{
const auto& items = s.selected_items();
if (items.size() > 1)
{
s.view_mode(view_type::rename);
}
else if (items.size() == 1)
{
const auto& file_system_items = items.items();
const auto i = file_system_items[0];
auto name = i->base_name();
auto title_control = std::make_shared<ui::title_control2>(
dlg->_frame, icon, title, format_plural_text(tt.rename_fmt, items),
std::vector<ui::const_surface_ptr>{}, items.size());
title_control->selection_async(items.thumbs(), items.size(), s._async);
const std::vector<view_element_ptr> controls{
set_margin(title_control),
std::make_shared<divider_element>(),
set_margin(std::make_shared<text_element>(tt.rename_label)),
set_margin(std::make_shared<ui::edit_control>(dlg->_frame, name)),
std::make_shared<divider_element>(),
std::make_shared<ui::ok_cancel_control>(dlg->_frame)
};
if (dlg->show_modal(controls) == ui::close_result::ok)
{
detach_file_handles detach(s);
const auto result = i->rename(s.item_index, name);
if (result.failed())
{
dlg->show_message(icon_index::error, title, result.format_error(tt.error_rename_failed, {}));
}
}
}
s.invalidate_view(
view_invalid::view_layout | view_invalid::group_layout | view_invalid::media_elements |
view_invalid::app_layout);
}
}
static void file_properties_invoke(const view_state& s, const ui::control_frame_ptr& parent,
const view_host_base_ptr& view)
{
if (can_process_selection_or_explain(s, view, parent, tt.open_properties_title,
df::process_items_type::local_file_or_folder))
{
const auto items = s.selected_items();
platform::show_file_properties(items.file_paths(false), items.folder_paths());
}
}
static void edit_paste_invoke(view_state& s, const ui::control_frame_ptr& parent,
const std::shared_ptr<view_frame>& view)
{
const auto dlg = make_dlg(parent);
const auto data = platform::clipboard();
platform::file_op_result result;
if (data->has_drop_files())
{
detach_file_handles detach(s);
shell_file_operation_ui processing(*view, parent);
result = data->drop_files(s.save_path(), platform::drop_effect::none);
if (result.success() && (!result.created_files.files.empty() || !result.created_files.folders.empty()))
detach.keep_display_closed();
}
else if (data->has_bitmap())
{
auto bitmap = ui_owned(s._async, data->capture_bitmap());
if (!bitmap)
{
result.error_message = tt.platform_text.error_save_image;
}
else
{
const auto save_path = s.save_path();
const auto file_name = std::string(tt.pasted_file_name);
const auto results = std::make_shared<command_status>(s._async, dlg, icon_index::edit_paste,
tt.command_edit_paste, 1);
s.queue_async(async_queue::work,
[&s, view = ui_owned(s._async, view), results, bitmap = std::move(bitmap), save_path,
file_name]
{
auto save_result = bitmap->save(save_path, file_name, true);
if (save_result.success())
{
s.queue_ui([&s, view, results, created_files = std::move(save_result.created_files)]
{
results->complete();
if (!created_files.files.empty() || !created_files.folders.empty())
{
detach_file_handles detach(s);
detach.keep_display_closed();
s.open(view.shared(), s.search(), make_unique_paths(created_files));
}
});
}
else
{
results->abort(str::is_empty(save_result.error_message)
? std::string(tt.platform_text.error_unknown)
: save_result.error_message);
}
});
results->wait_for_complete();
return;
}
}
if (result.failed())
{
dlg->show_message(icon_index::error, tt.command_edit_paste,
str::is_empty(result.error_message) ? tt.platform_text.error_unknown.sv() : result.error_message);
}
else
{
s.open(view, s.search(), make_unique_paths(result.created_files));
}
}
static void show_flatten_invoke(view_state& s, const ui::control_frame_ptr& parent, const view_host_base_ptr& view)
{
const auto current_search = s.search();
if (current_search.has_selector())
{
const auto first_selector = current_search.selectors().front();
if (first_selector.is_recursive())
{
auto a = current_search;
a.remove_recursive();
s.open(view, a, {});
}
else
{
auto a = current_search;
a.set_recursive(true);
s.open(view, a, {});
}
}
}
static void setting_invoke(const view_state& s, bool& val, const bool new_val)
{
val = new_val;
s.invalidate_view(view_invalid::view_layout | view_invalid::group_layout | view_invalid::app_layout);
}
static void zoom_navigator_mode_invoke(const view_state& state, const zoom_navigator_mode mode)
{
setting.zoom_navigator = mode;
if (const auto display = state.display_state()) display->mark_zoom_activity();
state.invalidate_view(view_invalid::view_redraw | view_invalid::controller | view_invalid::options_save);
}
static void rotate_invoke(view_state& s, const ui::control_frame_ptr& parent, const view_host_base_ptr& view,
const simple_transform t)
{
// Rotation rewrites each item in place, so it never needs a save folder. The only
// precondition is that the selection can be written back, which can_process checks below
// and which is also what enables the command.
auto dlg = make_dlg(parent);
const auto title = t == simple_transform::rot_90 ? tt.command_rotate_clockwise : tt.command_rotate_anticlockwise;
const auto icon = t == simple_transform::rot_90 ? icon_index::rotate_clockwise : icon_index::rotate_anticlockwise;
pause_media pause(s);
if (can_process_selection_or_explain(s, view, parent, title, df::process_items_type::can_save_pixels))
{
const auto& items = s.selected_items();
const auto is_single = items.size() == 1;
// A single-item rotation is reviewed only while the user asks for it; rotating several items
// overwrites several originals at once and is always reviewed.
if (!is_single || setting.confirm_rotations)
{
const auto surfaces = items.thumbnail_surfaces(1);
bool confirm_single = setting.confirm_rotations;
auto title_control = std::make_shared<ui::title_control2>(
dlg->_frame, icon, title, format_plural_text(tt.rotate_info_fmt, items),
std::vector<ui::const_surface_ptr>{}, items.size());
title_control->selection_async(items.thumbs(), items.size(), s._async);
std::vector<view_element_ptr> controls;
controls.emplace_back(set_margin(title_control));
controls.emplace_back(std::make_shared<divider_element>());
if (!surfaces.empty())
{
controls.emplace_back(std::make_shared<ui::before_after_control>(surfaces.front(), t));
}
if (is_single)
{
controls.emplace_back(set_margin(std::make_shared<ui::check_control>(
dlg->_frame, tt.rotate_confirm_single, confirm_single)));
}
controls.emplace_back(std::make_shared<divider_element>());
controls.emplace_back(std::make_shared<ui::ok_cancel_control>(dlg->_frame, tt.button_rotate));
if (dlg->show_modal(controls) != ui::close_result::ok) return;
if (is_single && confirm_single != setting.confirm_rotations)
{
setting.confirm_rotations = confirm_single;
s.invalidate_view(view_invalid::options_save);
}
}
{
record_feature_use(features::rotate);
const auto results = std::make_shared<command_status>(s._async, dlg, icon, title, items.size());
batch_edit_spec spec;
spec.process_type = df::process_items_type::can_save_pixels;
spec.encode_params = make_file_encode_params();
// A rotation always changes what is drawn, whether it is stored as an orientation tag or
// as rotated pixels.
spec.changes_presentation = true;
spec.make_edits = [t](const df::file_path path) -> std::optional<item_edits>
{
// Only the frame size and stored orientation are needed, and load_image_file derives
// both from this same scan, so read the header instead of the whole file.
sizei dimensions{0, 0};
auto stored_orientation = ui::orientation::top_left;
file_read_stream stream;
if (stream.open(path))
{
const auto info = scan_photo(stream);
dimensions = info.dimensions();
stored_orientation = info.orientation;
}
if (dimensions.cx <= 0 || dimensions.cy <= 0)
{
// Bytes the header scan cannot describe, such as a container carrying a saveable
// extension; the full load still decodes them.
files ff;
const auto load_result = ff.load(path, false);
if (!load_result.success) return {};
dimensions = load_result.dimensions();
stored_orientation = load_result.orientation();
}
item_edits result;
const auto current_orientation = setting.show_rotated
? stored_orientation
: ui::orientation::top_left;
const auto crop = rotate_command_crop_bounds(dimensions, stored_orientation, t, setting.show_rotated);
if (current_orientation != ui::orientation::top_left)
{
result.metadata.orientation = ui::orientation::top_left;
}
result.image.crop_bounds(crop);
return result;
};
s.modify_items(results, items.items(), spec, view);
}
}
}
static void desktop_background_invoke(view_state& s, const ui::control_frame_ptr& parent)
{
s.capture_display([&s, parent](const file_load_result& loaded)
{
const auto title = tt.command_desktop_background;
auto dlg = make_dlg(parent);
pause_media pause(s);
const std::vector<view_element_ptr> controls = {
set_margin(std::make_shared<ui::title_control2>(dlg->_frame, icon_index::wallpaper, title,
tt.desktop_background_info)),
std::make_shared<divider_element>(),
std::make_shared<wallpaper_control>(dlg->_frame, loaded, setting.desktop_background.maximize),
set_margin(std::make_shared<ui::check_control>(dlg->_frame, tt.maximize_image,
setting.desktop_background.maximize, false, false,
[d = dlg->_frame](bool checked) { d->invalidate(); })),
std::make_shared<divider_element>(),
std::make_shared<ui::ok_cancel_control>(dlg->_frame),
};
if (dlg->show_modal(controls) == ui::close_result::ok)
{
constexpr auto write_extension = ".png";
const auto write_path = df::file_path(known_path(platform::known_folder::app_data), "wallpaper",
write_extension);
const auto path_temp = platform::temp_file(write_extension);
const auto bounds = ui::desktop_bounds(true);
const auto screen_extent = bounds.extent();
const auto max_dim = std::max(screen_extent.cx, screen_extent.cy);
const auto dimensions = setting.desktop_background.maximize ? sizei(max_dim, max_dim) : screen_extent;
const auto encode_params = make_file_encode_params();
const auto results = std::make_shared<command_status>(s._async, dlg, icon_index::wallpaper, title, 1);
// Writing a screen-sized PNG twice is far too slow to run on the UI thread. Only the
// wallpaper switch comes back, and a failure is now reported instead of being
// described as a failed update.
s.queue_async(async_queue::work,
[&s, results, loaded, write_path, path_temp, dimensions, encode_params]
{
files ff;
const image_edits edits(dimensions);
std::string error;
if (!ff.save(path_temp, loaded))
{
error = str_format(tt.error_create_file_failed_fmt.sv(), path_temp);
}
else
{
const auto update_result = ff.update(path_temp, write_path, {}, edits, encode_params,
false, {});
if (update_result.failed())
{
error = update_result.format_error(
str_format(tt.error_create_file_failed_fmt.sv(), write_path));
}
}
if (path_temp.exists())
{
platform::delete_file(path_temp);
}
if (error.empty())
{
s.queue_ui([results, write_path]
{
platform::set_desktop_wallpaper(write_path);
results->complete();
});
}
else
{
results->abort(error);
}
});
results->wait_for_complete();
}
});
}
static void capture_invoke(view_state& s, const ui::control_frame_ptr& parent)
{
// Read now, not when the frame arrives: the frame is of this item, so this is the name it has
// to be offered under. Reading it in the callback named the capture after whatever the display
// had moved on to. Only the path is kept - the callback waits on the media read thread, which
// must not end up holding the item itself.
const auto item = s.command_item();
if (!item) return;
s.capture_display([&s, parent, source_path = item->path()](const file_load_result& lr)
{
if (!lr.is_empty())
{
auto i = 1;
const auto save_ext = lr.is_jpeg() ? "jpg" : "png";
const auto save_folder = s.save_path();
const auto save_name = source_path.file_name_without_extension();
auto save_path = df::file_path(save_folder, save_name, save_ext);
while (save_path.exists())
{
save_path = df::file_path(save_folder, std::format("{}-{}", save_name, i++), save_ext);
}
if (platform::prompt_for_save_path(save_path))
{
files ff;
if (!ff.save(save_path, lr))
{
const auto dlg = make_dlg(parent);
dlg->show_message(icon_index::error, tt.command_capture, tt.platform_text.error_save_image);
}
}
}
});
}
static void related_invoke(view_state& s, const ui::control_frame_ptr& parent, const view_host_base_ptr& view)
{
const auto i = s.command_item();
if (i)
{
df::related_info r;
r.load(i);
s.open(view, df::search_t().related(r), {});
}
}
static void favorite_tags_invoke(view_state& s, const ui::control_frame_ptr& parent)
{
auto dlg = make_dlg(parent);
auto favorite_tags = setting.favorite_tags;
auto edit = std::make_shared<ui::multi_line_edit_control>(dlg->_frame, favorite_tags);
auto content = std::make_shared<ui::group_control>();
const auto toolbar_refresh = std::make_shared<ui::recommended_words_control::refresh_group::element_type>();
const auto unique_favorites = [&favorite_tags]
{
std::vector<std::string> result;
df::hash_set<std::string, df::ihash, df::ieq> seen;
for (const auto tag : str::split(favorite_tags, true))
{
if (seen.emplace(tag).second) result.emplace_back(tag);
}
return result;
};
const auto contains_favorite = [&unique_favorites](const std::string_view tag)
{
const auto favorites = unique_favorites();
return std::ranges::find_if(favorites, [tag](const std::string_view favorite)
{
return str::icmp(favorite, tag) == 0;
}) != favorites.end();
};
const auto toggle_favorite = [&favorite_tags, edit, unique_favorites](const std::string_view tag)
{
auto favorites = unique_favorites();
const auto found = std::ranges::find_if(favorites, [tag](const std::string_view favorite)
{
return str::icmp(favorite, tag) == 0;
});
if (found == favorites.end())
favorites.emplace_back(tag);
else
favorites.erase(found);
favorite_tags = str::combine(favorites);
edit->text(favorite_tags);
};
favorite_tags = str::combine(unique_favorites());
edit->text(favorite_tags);
content->add(set_margin(std::make_shared<text_element>(tt.customise_tags_help)));
content->add(set_margin(std::make_shared<text_element>(tt.help_tag1)));
content->add(set_margin(std::make_shared<text_element>(tt.help_tag2)));
content->add(edit);
df::string_counts common_counts;
for (const auto& tag : s.item_index.distinct_tags())
{
++common_counts[tag.first];
}
s.recent_tags.count_strings(common_counts, 1000000);
const auto common_tags = top_map(common_counts, 20);
if (!common_tags.empty())
{
content->add(std::make_shared<ui::title_control>(tt.tags_common_label));
content->add(set_margin(std::make_shared<ui::recommended_words_control>(
dlg->_frame, common_tags, toggle_favorite, contains_favorite, toolbar_refresh)));
}
static constexpr std::array<std::string_view, 12> workflow_tags = {
"Approved", "Archive", "Client", "Draft", "Final", "Progress", "Review", "Published",
"Rejected", "Rights", "Select", "Todo"
};
std::vector<std::string_view> workflow_suggestions;
df::hash_set<std::string_view, df::ihash, df::ieq> workflow_seen;
const auto append_workflow_tag = [&workflow_suggestions, &workflow_seen](const std::string_view tag)
{
if (workflow_seen.emplace(tag).second) workflow_suggestions.emplace_back(tag);
};
for (const auto tag : str::split(tt.default_favorite_tags, true)) append_workflow_tag(tag);
for (const auto tag : workflow_tags) append_workflow_tag(tag);
content->add(std::make_shared<ui::title_control>(tt.tags_workflow_label));
content->add(set_margin(std::make_shared<ui::recommended_words_control>(
dlg->_frame, workflow_suggestions, toggle_favorite, contains_favorite, toolbar_refresh)));
std::vector<view_element_ptr> controls;
controls.emplace_back(set_margin(std::make_shared<ui::title_control2>(
dlg->_frame, icon_index::tag, tt.customise_tags_title, tt.customise_tags_help)));
controls.emplace_back(std::make_shared<divider_element>());
controls.emplace_back(content);
controls.emplace_back(std::make_shared<divider_element>());
controls.emplace_back(std::make_shared<ui::ok_cancel_control>(dlg->_frame));
pause_media pause(s);
// The edit box and the suggestion toolbars all write to the local copy, so Cancel and
// Escape leave the saved favourites alone.
if (dlg->show_modal(controls, {54}, {66}) != ui::close_result::ok) return;
favorite_tags = str::combine(unique_favorites());
setting.favorite_tags = favorite_tags;
s.invalidate_view(view_invalid::sidebar | view_invalid::options_save | view_invalid::command_state |
view_invalid::tooltip);
}
static void rate_items_invoke(view_state& s, const ui::control_frame_ptr& parent, const view_host_base_ptr& view, int r)
{
auto dlg = make_dlg(parent);
metadata_edits edits;
edits.rating = r;
constexpr auto icon = icon_index::star;
const auto title = tt.prop_name_rating;
const auto results = std::make_shared<command_status>(s._async, dlg, icon, title, s.selected_count());
s.modify_items(results, icon, title, s.selected_items().items(), edits, view);
}
static void label_items_invoke(view_state& s, const ui::control_frame_ptr& parent, const view_host_base_ptr& view,
const std::string_view label)
{
const auto selected = s.selected_items();
const auto& items = selected.items();
// Applying a label the whole selection already carries removes it, so a key repeats the toggle
// the grading control performs with the pointer and clearing never needs a separate command.
const auto is_set = !label.empty() && !items.empty() &&
std::ranges::all_of(items, [label](const df::item_element_ptr& i)
{
return str::icmp(i->label(), label) == 0;
});
auto dlg = make_dlg(parent);
metadata_edits edits;
edits.label = is_set ? std::string_view{} : label;
constexpr auto icon = icon_index::flag;
const auto title = tt.prop_name_label;
const auto results = std::make_shared<command_status>(s._async, dlg, icon, title, items.size());
s.modify_items(results, icon, title, items, edits, view);
}
static void cut_copy_invoke(const view_state& s, const ui::control_frame_ptr& parent, const view_host_base_ptr& view,
const bool is_move)
{
const auto title = is_move ? tt.command_edit_cut : tt.command_edit_copy;
if (can_process_selection_or_explain(s, view, parent, title, df::process_items_type::local_file_or_folder))
{
const auto items = s.selected_items();
const auto item_paths = items.file_paths(true);
const auto folder_paths = items.folder_paths();
s.capture_display([item_paths, folder_paths, is_move = is_move](const file_load_result& loaded)
{
platform::set_clipboard(item_paths, folder_paths, clipboard_image_of(loaded), is_move);
});
}
}
static void copy_item_path_invoke(const view_state& s)
{
const auto items = s.selected_items().items();
std::string paths;
for (const auto& item : items)
{
if (!paths.empty()) paths += "\r\n";
paths += item->path().pack();
}
if (!paths.empty()) platform::set_clipboard(paths);
}
class folder_auto_complete final : public std::enable_shared_from_this<folder_auto_complete>,
public ui::complete_strategy_t
{
view_state& _state;
std::shared_ptr<const df::folder_counts> _folders;
std::shared_ptr<std::atomic_uint64_t> _latest_request = std::make_shared<std::atomic_uint64_t>(0);
destination_folder_enumerator _enumerate_child_folders;
std::vector<df::folder_path> _recents;
ui::control_frame_ptr _parent;
ui::auto_complete_match_ptr _selected;
uint64_t _request_id = 0;
std::weak_ptr<void> _picker_lifetime;
public:
std::string no_results_message() override
{
return std::string(tt.type_to_search);
}
std::weak_ptr<ui::search_control> _search_control;
folder_auto_complete(view_state& s, ui::control_frame_ptr parent,
std::shared_ptr<void> picker_lifetime = {},
std::shared_ptr<const df::folder_counts> folders = {},
destination_folder_enumerator enumerate_child_folders = {}) :
_state(s),
_folders(folders ? std::move(folders) : std::make_shared<df::folder_counts>(_state.known_folders())),
_enumerate_child_folders(std::move(enumerate_child_folders)),
_parent(std::move(parent)),
_picker_lifetime(std::move(picker_lifetime))
{
resize_to_show_results = false;
auto_select_first = false;
folder_select_button = true;
max_predictions = 12u;
if (!_enumerate_child_folders)
{
_enumerate_child_folders = [](const df::folder_path folder)
{
df::folder_paths result;
const df::item_selector selector(folder);
for (const auto& fi : platform::select_folders(selector, setting.show_hidden))
{
result.emplace_back(folder.combine(fi.name));
}
return result;
};
}
auto recents = _state.recent_folders.items();
std::ranges::reverse(recents);
if (recents.size() > max_predictions)
{
recents.resize(max_predictions);
}
for (const auto& i : recents)
{
_recents.emplace_back(df::folder_path(i));
}
}
void bind_search_control(const std::shared_ptr<ui::search_control>& search_control)
{
_search_control = search_control;
_picker_lifetime = search_control;
}
void initialise(std::function<void(const ui::auto_complete_results&)> complete) override
{
search({}, std::move(complete));
}
static bool compare_weight(const ui::auto_complete_match_ptr& l, const ui::auto_complete_match_ptr& r)
{
const auto diff = l->weight - r->weight;
return diff == 0 ? str::icmp(l->edit_text(), r->edit_text()) < 0 : diff > 0;
}
struct folder_completion_candidate
{
df::folder_path folder;
ui::match_highlights match;
int weight = 0;
};
static void add_candidate(df::hash_map<df::folder_path, folder_completion_candidate, df::ihash, df::ieq>& results,
const df::folder_path folder, ui::match_highlights m, const int weight)
{
const auto found = results.find(folder);
if (found != results.end())
{
found->second.weight += weight;
return;
}
results.emplace(folder, folder_completion_candidate{folder, std::move(m), weight});
}
static std::vector<folder_completion_candidate> find_matches(const std::string& query,
const df::folder_counts& folders,
const destination_folder_enumerator& enumerate_child_folders,
const size_t max_results,
const std::function<bool()>& is_stale = {})
{
df::hash_map<df::folder_path, folder_completion_candidate, df::ihash, df::ieq> candidates;
const auto query_parts = str::split(query, true);
for (const auto& folder : folders)
{
ui::match_highlights m;
if (find_auto_complete(query_parts, folder.first.text(), true, m))
{
add_candidate(candidates, folder.first, std::move(m), folder.second);
}
}
if (is_stale && is_stale()) return {};
if (df::is_path(query))
{
const df::folder_path folder(query);
for (const auto& path : enumerate_child_folders(folder))
{
ui::match_highlights m;
if (find_auto_complete(query_parts, path.text(), true, m))
{
const auto known = folders.find(path);
const auto known_weight = candidates.contains(path) || known == folders.end() ? 0 : known->second;
add_candidate(candidates, path, std::move(m), 10 + known_weight);
}
}
}
std::vector<folder_completion_candidate> results;
results.reserve(candidates.size());
for (auto&& candidate : candidates) results.emplace_back(std::move(candidate.second));
std::ranges::sort(results, [](const auto& l, const auto& r)
{
const auto diff = l.weight - r.weight;
return diff == 0 ? str::icmp(l.folder.text(), r.folder.text()) < 0 : diff > 0;
});
if (results.size() > max_results) results.resize(max_results);
return results;
}
void search(const std::string& query,
const std::function<void(const ui::auto_complete_results&)> complete) override
{
df::assert_true(ui::is_ui_thread());
const auto request_id = ++_request_id;
_latest_request->store(request_id, std::memory_order_release);
if (query.empty())
{
ui::auto_complete_results results;
for (const auto& folder : _recents)
{
results.emplace_back(std::make_shared<folder_match>(*this, folder, ui::match_highlights{}, 1, true));
}
complete(results);
}
else
{
auto folders = _folders;
auto latest_request = _latest_request;
auto enumerate_child_folders = _enumerate_child_folders;
const auto max_results = max_predictions;
auto weak = weak_from_this();
auto complete_ui = ui_owned(
_state._async, std::make_shared<std::function<void(const ui::auto_complete_results&)>>(complete));
_state.queue_async(async_queue::work,
[&s = _state, weak, query, folders = std::move(folders),