Repository navigation
Expand file tree
/
Copy pathapp.cpp
More file actions
4128 lines (3532 loc) · 136 KB
/
Copy pathapp.cpp
File metadata and controls
4128 lines (3532 loc) · 136 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: The application frame. Startup and shutdown, settings load and save, the frame layout and
// the status bar it paints, draining view invalidation in dependency order, keyboard routing,
// switching views, update checks and crash reporting. Command behavior, toolbars, workers and the
// address box live in app_commands.cpp, app_toolbar.cpp, app_workers.cpp and app_search.cpp.
#include "pch.h"
#ifdef _WIN32
#include "libdesktop/platform/win32.h"
#endif
#include "app_text.h"
#include "util_zip.h"
#include "model_location.h"
#include "model_db.h"
#include "model_index.h"
#include "model.h"
#include "ui_dialog.h"
#include "util_crash_files_db.h"
#include "libdesktop/core/log.h"
#include "files.h"
#include "av_format.h"
#include "view_items.h"
#include "view_edit.h"
#include "view_movie.h"
#include "view_media.h"
#include "ui_sid.h"
#include "view_selector.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 "view_options.h"
#include "app_sidebar.h"
#include "app_commands.h"
#include "app_dialogs.h"
#include "app_command_line.h"
#include "app.h"
#include <utility>
#include "app_command_status.h"
#include "util_spell.h"
std::atomic_int df::file_handles_detached = 0;
std::atomic_int df::jobs_running = 0;
std::atomic_int df::loading_media = 0;
std::atomic_int df::dragging_items = 0;
std::atomic_int df::handling_crash = 0;
std::atomic<core::file_path> df::last_loaded_path;
const std::string_view s_app_version = "127.4";
const std::string_view g_app_build = "1315";
extern const unsigned char diffractor_resource_icon_font[];
extern const unsigned long diffractor_resource_icon_font_size;
void register_diffractor_icon_font()
{
render::set_icon_font({diffractor_resource_icon_font,
static_cast<size_t>(diffractor_resource_icon_font_size)});
}
static constexpr auto s_search = "search";
extern void start_worker(platform::task_queue& q, std::string_view name);
std::string df::format_version(const bool short_text)
{
if (short_text)
{
return std::format("{}.{}", s_app_version, g_app_build);
}
return std::format("{}: {}.{} | {}", tt.version, s_app_version, g_app_build, core::utf8_cast(__DATE__));
};
bool is_app_installed()
{
const auto running_folder = platform::running_app_path().folder();
const auto install_folder = known_path(platform::known_folder::app_data);
return running_folder == install_folder;
}
struct app_updates_and_location_params
{
gps_coordinate gps = setting.default_location;
std::string version;
std::string test_version = setting.available_test_version;
void apply(const app_frame_ptr& app, const view_state& s) const
{
core::assert_true(platform::is_ui_thread());
setting.default_location = gps;
setting.available_version = version;
setting.available_test_version = test_version;
// settings_t is plain UI-owned state, so the report's own side effect is applied here rather
// than on the web worker that observed it.
setting.first_run_ever = false;
s.invalidate_view(view_invalid::app_layout);
app->save_options(true);
}
};
static gps_coordinate parse_coordinates(const std::string_view text, const gps_coordinate def_coords)
{
const auto splits = core::split(text, true);
if (splits.size() == 2)
{
const auto latitude = splits[0];
const auto longitude = splits[1];
if (!longitude.empty() && !latitude.empty())
{
return {core::to_double(latitude), core::to_double(longitude)};
}
}
return def_coords;
}
// Runs on a shared worker thread (which already owns its own name and COM
// initialisation) and only queues the actual network request, so it must not
// rename the thread or re-initialise it.
static void check_for_updates_and_location(const app_frame_ptr& app, view_state& s)
{
log_func lf(__FUNCTION__);
spell().configure_async(&s._async);
if (platform::is_online())
{
#ifndef WINSTORE
if (setting.first_run_today && setting.check_for_updates)
{
s.queue_async(async_queue::web, [app, &s]
{
const auto reported_features = features_used_since_last_report();
platform::web_request req;
req.path = "/ver";
req.query = platform::web_params{
{"v"s, std::string(s_app_version)},
{"b"s, std::string(g_app_build)},
{"f"s, setting.first_run_ever ? "1"s : "0"s},
{"ft"s, core::to_hex(reported_features)},
{"i"s, core::to_hex(setting.instantiations)},
{"os"s, platform::OS()},
// The packed platform identity. It ships alongside `os` for one release rather
// than replacing it on the same commit, so the ingest can change without a gap
// in the daily series. It is its own parameter and never merged into `ft`: a
// feature bit means "at least once since the last report" and is cleared once
// sent, a system value means "right now" and is recomputed every time, and
// OR-accumulating an architecture would make anyone who once ran the 32-bit
// build look like a permanent 32-bit user.
{"s"s, core::to_hex(platform::pack_environment(platform::environment()))},
};
const auto con = platform::connect_to_host("diffractor.com");
const auto response = send_request(con, req);
if (response.status_code == 200)
{
core::json::json_doc json;
json.Parse(response.body);
app_updates_and_location_params params;
params.version = core::json::safe_string(json, "current_version");
params.test_version = core::json::safe_string(json, "test_version");
params.gps = parse_coordinates(core::json::safe_string(json, "latlon"), params.gps);
clear_reported_feature_use(reported_features);
s.queue_ui([params, app, &s]
{
params.apply(app, s);
});
}
});
}
#endif
spell().lazy_download(s._async);
}
}
std::vector<std::pair<std::string_view, std::string>> calc_app_info(const index_state& index,
const bool include_state)
{
std::vector<std::pair<std::string_view, std::string>> result;
auto arch = "32-bit";
auto config = "release";
#ifdef _M_X64
arch = "64-bit";
#endif
#ifdef _DEBUG
config = "debug";
#endif //_DEBUG
const auto seconds_running = core::utc_now().to_seconds() - ui::start_time.to_seconds();
result.reserve(include_state ? 25_z : 19_z);
result.emplace_back("Version:", df::format_version(true));
result.emplace_back("Windows:", std::format("{} {} {}", platform::OS(), arch, config));
result.emplace_back("Id:", core::to_hex(core::crc32c(platform::user_name()), false));
result.emplace_back("Running:", std::format("{} seconds", seconds_running));
platform::memory_usage_t mem;
if (platform::memory_usage(mem))
{
// Private working set first: it is what Task Manager's Memory column reports, so a user
// comparing the two sees the same number. Shared pages are mapped data files, which cost
// this process nothing private.
result.emplace_back("Memory:",
std::format("{} (commit {}, shared {})", core::file_size(mem.private_working_set),
core::file_size(mem.commit), core::file_size(mem.shared_working_set)));
result.emplace_back("Peak memory:", platform::format_size(core::file_size(mem.peak_working_set)));
}
result.emplace_back("Static Memory:",
platform::format_size(core::file_size(core::static_memory_usage.load(std::memory_order_relaxed))));
result.emplace_back("GPU:", render::gpu_desc);
result.emplace_back("GPU Id:", render::gpu_id);
result.emplace_back("D3D:", render::d3d_info);
result.emplace_back("Indexed items:", core::to_string(index.stats.index_item_count));
result.emplace_back("Indexed folders:", core::to_string(index.stats.index_folder_count));
result.emplace_back("Duplicates:",
std::format("g={} mcomp={}", index.stats.indexed_dup_folder_count,
index.stats.indexed_max_compare_count));
result.emplace_back("Hashes:",
std::format("crc={} phash={} declined={} uninvited={}", index.stats.indexed_crc_count,
index.stats.indexed_phash_count, index.stats.indexed_phash_declined_count,
index.stats.indexed_phash_uninvited_count));
result.emplace_back("DB size:", platform::format_size(index.stats.database_size));
result.emplace_back("Saved:", std::format("{} items | {} thumbs", index.stats.items_saved,
index.stats.thumbs_saved));
result.emplace_back("Index load:", std::format("{} ms", index.stats.index_load_ms));
result.emplace_back("Predictions:", std::format("{} ms", index.stats.predictions_ms));
result.emplace_back("Count Matches:", std::format("{} ms", index.stats.count_matches_ms));
if (include_state)
{
result.emplace_back("Jobs running: ", core::to_string(df::jobs_running));
result.emplace_back("Is indexing: ", core::to_string(index.indexing));
result.emplace_back("Is searching: ", core::to_string(index.searching));
result.emplace_back("Is command active: ", core::to_string(ui::command_active));
result.emplace_back("Is closing: ", core::to_string(platform::is_closing));
result.emplace_back("Rendering function: ", core::utf8_cast(render::rendering_func.load(std::memory_order_relaxed)));
}
return result;
}
void app_logo_element::tooltip(view_hover_element& hover, const pointi loc, const pointi element_offset) const
{
hover.elements->add(std::make_shared<text_element>(_text, render::font_face::title,
render::text_style::single_line,
flex_item::line_break));
hover.elements->add(std::make_shared<text_element>(df::format_version(false), render::font_face::dialog,
render::text_style::single_line,
flex_item::line_break));
if (setting.show_debug_info)
{
const auto table = std::make_shared<ui::table_element>(flex_item::center);
for (const auto& i : calc_app_info(_state.item_index, true))
{
table->add(i.first, i.second);
}
hover.elements->add(table);
}
hover.elements->add(std::make_shared<action_element>(tt.help_more_info));
hover.active_bounds = hover.window_bounds = bounds.offset(element_offset);
}
view_element_ptr create_app_logo_element(view_state& s, const render::font_face font, const bool interactive,
const bool show_plasma, const double logo_scale,
const view_element_options& options)
{
return std::make_shared<app_logo_element>(s, font, interactive, show_plasma, logo_scale, options);
}
void app_frame::fail(const std::string_view message, const std::string_view more_text)
{
auto message_s = std::string(message);
auto more_text_s = std::string(more_text);
// A failure during startup has no window to parent a dialog and no message loop to run one:
// wWinMain returns as soon as init() fails, so a queued dialog would never be shown and the
// process would simply disappear. Say it synchronously instead. Restricted to the UI thread so
// a worker reporting a failure after the window has gone still just queues and is dropped.
if (!_app_frame && platform::is_ui_thread())
{
auto text = message_s;
if (!more_text_s.empty()) text += "\n\n" + more_text_s;
platform::show_startup_failure(text);
return;
}
queue_ui([message_s, more_text_s, parent = _app_frame]
{
const auto dlg = make_dlg(parent);
std::vector<view_element_ptr> controls;
controls.emplace_back(set_margin(std::make_shared<ui::title_control>(icon_index::error, tt.platform_text.title_error)));
controls.emplace_back(set_margin(std::make_shared<text_element>(tt.platform_text.error_cannot_continue)));
if (!message_s.empty()) controls.emplace_back(set_margin(std::make_shared<text_element>(message_s)));
if (!more_text_s.empty()) controls.emplace_back(set_margin(std::make_shared<text_element>(more_text_s)));
controls.emplace_back(std::make_shared<ui::close_control>(dlg->_frame));
dlg->show_modal(controls);
if (parent)
{
parent->close();
}
});
}
void media_view::update_media_elements()
{
core::assert_true(platform::is_ui_thread());
const auto display = _state.display_state();
_display = display;
view_element_ptr media_element;
if (display)
{
if (display->is_one())
{
const auto& item = display->_item1;
const auto* const file_type = item->file_type();
// Arrowing through fullscreen lands on whatever is next, including a file the decoder
// could not open. A player over nothing is what makes that look like a broken video
// rather than a file that was never media, so it falls through to no media element and
// the panel states what the file is.
const auto media_unavailable = display->_av_open_failed;
if (file_type->has_trait(file_traits::bitmap))
{
media_element = std::make_shared<photo_control>(_state, display, _host);
}
else if (file_type->has_trait(file_traits::sid_tune) && !media_unavailable)
{
media_element = std::make_shared<sid_monitor_control>(_state, display, _host);
}
else if (file_type->has_trait(file_traits::visualize_audio) && !media_unavailable)
{
media_element = std::make_shared<audio_control>(_state, display, _host, true);
}
else if (file_type->has_trait(file_traits::av) && !media_unavailable)
{
media_element = std::make_shared<video_control>(_state, display, _host);
}
else if (file_type->has_trait(file_traits::archive))
{
media_element = std::make_shared<file_list_control>(display, flex_item::center);
}
else
{
}
}
else if (display->is_two())
{
media_element = std::make_shared<side_by_side_control>(_state, display, _host);
}
else if (display->is_multi())
{
media_element = std::make_shared<images_control2>(_state, display);
}
}
std::swap(_media_element, media_element);
_controls_element = _state.create_selection_controls(true);
_description_element = _state.create_selection_description();
if (_controls_element)
{
_controls_element->set_style_bit(view_element_style::dark_background, true);
}
if (_description_element)
{
_description_element->set_style_bit(view_element_style::dark_background, true);
}
}
// Case-insensitive exact match for a switch name.
static bool is_switch(const std::string_view op, const std::string_view name)
{
return core::icmp(op, name) == 0;
}
// Case-insensitive match for "<prefix><value>" where value is non-empty.
static bool switch_value(const std::string_view op, const std::string_view prefix, std::string_view& value)
{
if (op.size() > prefix.size() && core::icmp(op.substr(0, prefix.size()), prefix) == 0)
{
value = op.substr(prefix.size());
return true;
}
return false;
}
void command_line_t::parse(const std::string_view command_line_text)
{
// core::split with detect_quotes strips the surrounding quotes, so a quoted
// path containing spaces arrives here as a single part.
for (const auto part : core::split(core::trim(command_line_text), true, core::is_white_space))
{
if (part.empty())
{
continue;
}
// A leading '/' introduces a switch only where it cannot begin an absolute path. Everywhere
// else '/home/zac' would be read as a switch and never reach the path branch below.
const auto starts_switch = part[0] == '-' || (core::windows_path_semantics && part[0] == '/');
if (starts_switch && part.size() > 1)
{
const auto op = part.substr(part[1] == '-' ? 2 : 1);
std::string_view value;
if (is_switch(op, "no-gpu")) no_gpu = true;
else if (is_switch(op, "no-indexing")) no_indexing = true;
else if (is_switch(op, "run-tests") || is_switch(op, "test")) console_test = true;
else if (switch_value(op, "test:", value))
{
console_test = true;
test_filter = value;
}
else if (switch_value(op, "test-temp:", value)) test_temp_folder = value;
else if (is_switch(op, "gen-docs")) gen_docs = true;
else if (switch_value(op, "gen-docs:", value))
{
gen_docs = true;
docs_path = value;
}
else if (is_switch(op, "validate-po")) validate_po = true;
else if (switch_value(op, "dup-report:", value))
{
dup_report = true;
dup_report_folder = value;
}
else if (switch_value(op, "dup-report-out:", value)) dup_report_output = value;
else if (switch_value(op, "movie-check:", value))
{
movie_batch.projects = value;
movie_batch.mode = movie_batch_mode::check;
}
else if (switch_value(op, "movie-import:", value))
{
movie_batch.projects = value;
movie_batch.mode = movie_batch_mode::import;
}
else if (switch_value(op, "movie-render:", value))
{
movie_batch.projects = value;
movie_batch.mode = movie_batch_mode::render;
}
else if (switch_value(op, "movie-media:", value)) movie_batch.media = value;
else if (switch_value(op, "movie-out:", value)) movie_batch.output = value;
else if (is_switch(op, "movie-overwrite")) movie_batch.overwrite = true;
else if (is_switch(op, "movie-skip-missing")) movie_batch.skip_missing = true;
#ifdef _DEBUG
else if (is_switch(op, "test-reset-graphics")) test_action = "reset-graphics";
else if (is_switch(op, "test-crash")) test_action = "crash";
else if (is_switch(op, "test-send-crash-report")) test_action = "send-crash-report";
else if (is_switch(op, "test-new-version")) test_action = "new-version";
else if (switch_value(op, "screenshot:", value)) screenshot_scene = value;
else if (switch_value(op, "screenshot-output:", value)) screenshot_output = value;
#endif
}
else if (part.ends_with("**"))
{
// A trailing "**" asks for the folder with its subfolders, as it does in the address box.
// AutoPlay opens an inserted card that way, and may name a drive without its separator.
auto text = std::string(part);
if (core::windows_path_semantics && text.size() == 4 && text[1] == ':') text.insert(2, 1, core::preferred_path_sep);
if (core::is_path(text))
{
const core::item_selector selector(text);
if (selector.is_recursive() && !selector.has_wildcard() && platform::exists(selector.folder()))
{
folder_path = selector;
selection = {};
}
}
}
else if (core::is_path(part))
{
const auto folder = core::folder_path(part);
if (platform::exists(folder))
{
folder_path = folder;
selection = {};
}
else
{
const auto file = core::file_path(part);
if (platform::exists(file))
{
folder_path = file.folder();
selection = file;
}
}
}
}
}
std::string command_line_t::format_restart_cmd_line() const
{
std::string result;
if (no_gpu) result += " -no-gpu";
if (no_indexing) result += " -no-indexing";
return result;
}
void view_frame::update_status(const std::string_view title, const std::string_view text, const int padding)
{
if (_status_title != title || _status_text != text || _status_padding != padding)
{
_status_title = title;
_status_text = text;
_status_padding = padding;
_state.invalidate_view(view_invalid::view_redraw);
}
}
void view_frame::clear_status()
{
update_status({}, {});
}
void view_frame::draw_status(render::draw_context& dc) const
{
if (!_status_title.empty() || !_status_text.empty())
{
constexpr auto title_font = render::font_face::title;
constexpr auto text_font = render::font_face::dialog;
const auto text_color = core::color(dc.colors.foreground, dc.colors.alpha);
const auto bg_color = core::color(ui::style::colors.important_background, dc.colors.alpha);
const sizei avail_extent{_view_bounds.width() / 2, _view_bounds.height() / 2};
const auto title_extent = dc.measure_text(_status_title, title_font, render::text_style::multiline_center,
avail_extent.cx);
const auto text_extent = dc.measure_text(_status_text, text_font, render::text_style::multiline_center,
avail_extent.cx);
const auto text_spacing = !_status_title.empty() && !_status_text.empty() ? _status_padding : 0;
const auto extent = sizei(std::max(title_extent.cx, text_extent.cx),
title_extent.cy + text_extent.cy + text_spacing);
const auto bounds = center_rect(extent, _view_bounds);
auto title_bounds = bounds;
title_bounds.bottom = title_bounds.top + title_extent.cy;
auto text_bounds = bounds;
text_bounds.top = text_bounds.bottom - text_extent.cy;
dc.draw_rect(bounds.inflate(_status_padding), bg_color);
dc.draw_text(_status_title, title_bounds, title_font, render::text_style::multiline_center, text_color, {});
dc.draw_text(_status_text, text_bounds, text_font, render::text_style::multiline_center, text_color, {});
}
}
shell_file_operation_ui::shell_file_operation_ui(view_frame& view, ui::frame_ptr main_frame) :
_view(view), _main_frame(std::move(main_frame))
{
_view.update_status(tt.processing_files, {}, 20);
_view.redraw_now();
_main_frame->enable(false);
}
shell_file_operation_ui::~shell_file_operation_ui()
{
_main_frame->enable(true);
_view.clear_status();
}
// Both panes stay usable, so a splitter drag can never collapse either one out of reach.
static int clamp_pane_width(const int width, const int available, const int min_pane)
{
const auto lo = std::min(min_pane, available / 2);
return std::clamp(width, lo, std::max(lo, available - min_pane));
}
// The strip between a view and its panel: dragged, it shares the width between them, and the width the
// panel took is kept for the view. Internal to this file: the Items view has a splitter_controller of its own,
// and two classes of one name in two files are one class to the linker, which keeps one vtable for both.
namespace
{
class panel_splitter_controller final : public view_controller
{
std::shared_ptr<view_frame> _view;
public:
explicit panel_splitter_controller(const std::shared_ptr<view_frame>& view) :
view_controller(view, view->_splitter.bounds), _view(view)
{
}
~panel_splitter_controller() override
{
_view->_splitter.tracking = false;
_view->frame()->invalidate(_view->_splitter.bounds);
}
platform::cursor cursor() const override
{
return platform::cursor::left_right;
}
void draw(render::draw_context& dc) override
{
const auto& s = _view->_splitter;
draw_splitter_handle(dc, s.bounds, s.width, true, s.tracking);
}
void on_mouse_left_button_down(const pointi loc, const platform::key_state keys) override
{
_view->_splitter.tracking = true;
_view->drag_splitter(loc);
}
void on_mouse_move(const pointi loc) override
{
if (_view->_splitter.tracking) _view->drag_splitter(loc);
}
void on_mouse_left_button_up(const pointi loc, const platform::key_state keys) override
{
if (!_view->_splitter.tracking) return;
_view->drag_splitter(loc);
_view->_splitter.tracking = false;
_view->frame()->invalidate(_view->_splitter.bounds);
_view->invalidate_view(view_invalid::options_save);
}
};
}
void view_frame::panel(view_controls_host_ptr panel, const bool fills)
{
if (_panel == panel && _panel_fills == fills) return;
_panel = std::move(panel);
_panel_fills = fills;
if (_panel) _panel->_frame = frame();
_splitter.tracking = false;
}
bool view_frame::in_panel(const pointi loc) const
{
return _panel && _panel->_bounds.contains(loc);
}
void view_frame::drag_splitter(const pointi loc)
{
const auto& s = _splitter;
const auto available = std::max(0, _extent.cx - s.width);
if (available <= 0) return;
// The pointer holds the middle of the strip, so the grab point does not jump on the first move.
const auto panel_width = clamp_pane_width(_extent.cx - loc.x - s.width / 2, available, s.min_pane);
const auto pos = std::max(1, core::mul_div(panel_width, settings_t::view_splitter_max, available));
if (setting.set_view_splitter(_state.view_mode(), pos))
{
// Direct pointer input, so lay out and repaint inside this message. Deferring to idle would
// paint the strip at its new position while the panes are still at the old one. The setting
// is written once the drag ends rather than on every move.
frame()->layout();
frame()->redraw_now();
}
}
// The view takes what the panel, the splitter and the strip leave: the panel the share of the width the
// view keeps for it - all of it when it is the whole frame - and the strip a band along the base.
void view_frame::on_window_layout(render::measure_context& mc, const sizei extent, const bool is_minimized)
{
_extent = extent;
const auto scale_factor = mc.scale_factor;
const auto has_panel = _panel != nullptr;
const auto fills = has_panel && _panel_fills;
const auto splitter_width = has_panel && !fills ? core::mul_div(mc.scroll_width, 3, 5) : 0;
const auto min_pane = core::round(200 * scale_factor);
const auto available = std::max(0, extent.cx - splitter_width);
const auto stored = setting.view_splitter(_state.view_mode());
const auto preferred = stored > 0
? core::mul_div(available, stored, settings_t::view_splitter_max)
: std::max(extent.cx / 4, core::round(400 * scale_factor));
const auto panel_width = fills ? extent.cx : has_panel ? clamp_pane_width(preferred, available, min_pane) : 0;
const auto content_right = std::max(0, extent.cx - panel_width - splitter_width);
const auto strip_height = _strip_wanted ? std::min(core::round(150 * scale_factor), extent.cy / 3) : 0;
_view_bounds = recti(0, 0, content_right, extent.cy - strip_height);
_strip->bounds = recti(0, extent.cy - strip_height, content_right, extent.cy);
_strip->is_visible(strip_height > 0 && content_right > 0);
_splitter.bounds = splitter_width > 0
? recti(content_right, 0, content_right + splitter_width, extent.cy)
: recti{};
_splitter.width = splitter_width;
_splitter.min_pane = min_pane;
_view->layout(mc, _view_bounds.extent());
if (_panel) _panel->layout_panel(mc, recti(content_right + splitter_width, 0, extent.cx, extent.cy));
}
void view_frame::on_window_paint(render::draw_context& dc)
{
dc.time_now = core::now();
dc.colors.alpha = 1.0;
dc.colors.overlay_alpha = 1.0;
if (!_view_bounds.is_empty())
{
render::scoped_clip clip(dc, _view_bounds);
_view->render(dc, _active_controller);
}
if (_panel) _panel->render_panel(dc);
if (!_splitter.bounds.is_empty())
{
// In the panel's colour, so the strip reads as part of it until the user touches it.
dc.draw_rect(_splitter.bounds, core::color(ui::style::colors.dialog_background, dc.colors.alpha));
}
if (_active_controller)
{
_active_controller->draw(dc);
}
draw_status(dc);
if (setting.show_debug_info && _active_controller)
{
const auto c = core::color(1.0f, 0.0f, 0.0f, 1.0f);
const auto pad = core::round(2 * dc.scale_factor);
dc.draw_border(_controller_bounds, _controller_bounds.inflate(pad), c, c);
}
}
void view_frame::visit_elements(const std::function<void(const view_element_ptr&, pointi)>& visit)
{
visit(_strip, {});
if (_panel) _panel->visit_elements(visit);
}
void view_frame::on_mouse_wheel(const pointi loc, const int delta, const platform::key_state keys, bool& was_handled)
{
if (in_panel(loc)) _panel->on_mouse_wheel(loc, delta, keys, was_handled);
else if (in_view(loc)) was_handled = dispatch_wheel(loc, delta, keys, platform::wheel_axis::vertical, _wheel_vertical);
update_controller(loc);
}
void view_frame::focus_changed(const bool has_focus, const ui::control_ptr& child)
{
core::trace(std::format("render_window::focus {}", has_focus));
_view->focus(has_focus && !child);
if (child && _panel) _panel->focus_changed(has_focus, child);
if (focus_listener) focus_listener(has_focus, child);
frame()->invalidate();
}
view_controller_ptr view_frame::controller_from_location(const pointi loc, hit_test_context& ctx)
{
if (ctx.occluded(_splitter.bounds))
{
return std::make_shared<panel_splitter_controller>(shared_from_this());
}
if (in_panel(loc))
{
ctx.confine(_panel->_bounds);
return _panel->controller_from_location(loc, ctx);
}
if (!in_view(loc)) return nullptr;
ctx.confine(_view_bounds);
return _view->controller_from_location(shared_from_this(), loc, ctx);
}
void bar_frame::init(const ui::frame_ptr& owner)
{
ui::frame_style fs;
fs.colors = {
ui::style::colors.toolbar_background, ui::style::colors.view_text, ui::style::colors.view_selected_background
};
_frame = owner->create_frame(weak_from_this(), fs);
}
void bar_frame::on_window_layout(render::measure_context& mc, const sizei extent, const bool is_minimized)
{
if (is_minimized) return;
_extent = extent;
std::vector<ui::control_ptr> shown;
if (_kind == kind::title) _app.layout_title_bar(mc, extent, shown);
else _app.layout_status_bar(mc, extent, shown);
_controls = std::move(shown);
frame()->invalidate();
}
void bar_frame::on_window_paint(render::draw_context& dc)
{
if (_kind == kind::title) _app.paint_title_bar(dc);
else _app.draw_status_bar(dc);
for (const auto& c : _controls)
{
if (c->is_visible()) c->render(dc, {});
}
}
void bar_frame::visit_elements(const std::function<void(const view_element_ptr&, pointi)>& visit)
{
const auto controls = _controls;
for (const auto& c : controls) visit(c, {});
}
// The title bar moves the window by any part of it that is neither the title nor a control.
bool bar_frame::is_caption_area(const pointi loc) const
{
return _kind == kind::title && !_app._title_bounds.contains(loc);
}
void bar_frame::on_mouse_move(const pointi loc, const bool is_tracking)
{
if (_kind == kind::title) _app.title_bar_mouse_move(loc);
else _app.update_status_hover(loc);
}
void bar_frame::on_mouse_leave(const pointi loc)
{
if (_kind == kind::title) _app.title_bar_mouse_leave();
else _app.update_status_hover({-1, -1});
}
void bar_frame::on_mouse_left_button_up(const pointi loc, const platform::key_state keys)
{
if (_kind == kind::title) _app.title_bar_click(loc);
}
void bar_frame::focus_changed(const bool has_focus, const ui::control_ptr& child)
{
_app.focus_changed(has_focus, child);
}
void bar_frame::command_hover(const ui::command_ptr& c, const recti window_bounds)
{
_app.command_hover(c, window_bounds);
}
std::vector<ui::command_ptr> bar_frame::menu(const pointi loc)
{
return _app.context_menu(menu_type::sidebar);
}
app_frame::app_frame() :
_item_index(*this, _locations),
_player(std::make_shared<av_player>(*this, [&index = _item_index](const core::file_path path, const double position)
{
index.save_media_position(path, position);
})),
_state(*this, *this, _item_index, _player),
_edit_view_state(_state),
_db(_state.item_index)
{
_title_bar = std::make_shared<bar_frame>(*this, bar_frame::kind::title);
_view_frame = std::make_shared<view_frame>(_state);
_status_bar = std::make_shared<bar_frame>(*this, bar_frame::kind::status);
_title_region = std::make_shared<ui::frame_element>();
_body_region = std::make_shared<ui::frame_element>();
_status_region = std::make_shared<ui::frame_element>();
_regions = std::make_shared<ui::elements>(std::vector<view_element_ptr>{_title_region, _body_region, _status_region});
_app_logo = std::make_shared<app_logo_element>(_state);
_selector_frame = std::make_shared<view_frame>(_state);
_view_sync = std::make_shared<sync_view>(_state, _view_frame);
_view_tags = std::make_shared<tags_view>(_state, _view_frame);
_view_options = std::make_shared<options_view>(_state, _view_frame, options_view_actions{
[this](const std::string_view code) { select_language(code); },
[this] { index_maintenance_invoke(_state, _app_frame); },
[this]
{
return std::static_pointer_cast<void>(
std::make_shared<pause_media>(_state));
},
[this] { return ask_to_save_options(_app_frame); }
});
_view_import = std::make_shared<import_view>(_state, _view_frame);
_view_locate = std::make_shared<locate_view>(_state, _view_frame);
_view_rename = std::make_shared<rename_view>(_state, _view_frame);
_view_batch = std::make_shared<batch_tool_view>(_state, _view_frame);
_view_items = std::make_shared<items_view>(_state, _view_frame);
_view_selector = std::make_shared<selector_view>(_state, _selector_frame,
[this](const df::item_element_ptr& item, const platform::key_state keys)
{
select_from_selector(item, keys);
});
_view_edit = std::make_shared<edit_view>(_state, _view_frame, _edit_view_state);
_view_movie = std::make_shared<movie_view>(_state, _view_frame, _movie_view_state);
_selector_frame->view(_view_selector);
_view_media = std::make_shared<media_view>(_state, _view_frame);
// Both frames carry a view from construction. init() creates their windows before the first
// view_changed assigns one, and only some of view_frame's handlers test _view, so a paint or
// pointer message arriving in that window would otherwise dereference null. Items is what
// view_mode(view_type::items) selects moments later, and view_changed replaces it either way.
_view_frame->view(_view_items);
}
app_frame::~app_frame()
{
_threads.clear();
// Order matters: workers truncate their queues as they exit, and a truncated task holding the last
// reference to UI-owned state hands it to _ui_queue rather than destroying it on the worker (see
// ui_owned_ptr). Joining first and draining second is what makes those hand-backs land here, on the
// UI thread, while the objects they reference are still alive.
(void)_ui_queue.dequeue_all();
_state.close();
core::log(__FUNCTION__, "destruct");
}
void app_frame::frame_tick()
{
core::assert_true(platform::is_ui_thread());
core::bump(render::ui_perf.frame_prepares);
const auto vf = _view_frame;
// _view is only assigned by view_changed, which init() reaches after the frame exists.
if (!platform::is_closing && vf && _view)
{
const auto time_now = core::now();
const auto display_frequency = platform::display_frequency();
const auto animation_delay_ms = 1000 / display_frequency;
constexpr auto idle_delay_ms = 1000 / render::default_ticks_per_second;
// Clamped so a stall, a breakpoint, or a first frame cannot snap every fade to its target.
const auto prepare_us = core::now_us();
const auto elapsed_s = _last_prepare_us == 0
? 1.0 / display_frequency
: std::clamp((prepare_us - _last_prepare_us) / 1'000'000.0, 1.0 / 240.0,
1.0 / 15.0);
_last_prepare_us = prepare_us;
ui::animation_step_factor = static_cast<float>(1.0 - std::exp(-ui::alpha_fade_rate * elapsed_s));
std::vector<void*> removals;
auto is_animating = false;
auto is_animating2 = false;
for (const auto& i : ui::animations)
{
if (i.second())
{
is_animating2 = true;
}
else
{
removals.emplace_back(i.first);
}
}
for (const auto& i : removals)
{