TachyonUI 1.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_file_thumbnails.h
Go to the documentation of this file.
1#pragma once
2
3// Pictures of what's in files, as the OS shows them in its file views: a photo's, a frame of
4// a video, a document's first page. Read off the UI thread.
5
8
9#include <chrono>
10#include <condition_variable>
11#include <cstdint>
12#include <filesystem>
13#include <functional>
14#include <list>
15#include <mutex>
16#include <string>
17#include <thread>
18#include <unordered_map>
19#include <vector>
20
21namespace tachyon
22{
23 class ITextSystem;
24
27
40 {
41 public:
43 struct Picture
44 {
46 std::vector<uint8_t> rgba;
47 };
48
50 using Reader = std::function<bool(const std::filesystem::path& file, uint32_t pixels, Picture& picture)>;
51
52 explicit FileThumbnails(ITextSystem& textSystem, size_t capacity = 512, Reader reader = ShellReader(),
53 std::chrono::milliseconds inUse = std::chrono::milliseconds(500));
57
62 IconID Get(const std::filesystem::path& file, std::filesystem::file_time_type modified, uint32_t pixels);
63
67 void SetOnRead(std::function<void()> onRead);
68
70 size_t Count() const { return m_entries.size(); }
72 size_t Reading();
73
75 static std::string Key(const std::filesystem::path& file, std::filesystem::file_time_type modified, uint32_t pixels);
79
80 private:
81 struct Entry
82 {
84 bool pending = true;
85 std::list<std::string>::iterator used;
86 int64_t asked = 0;
87 };
88 struct Job
89 {
90 std::string key;
91 std::filesystem::path file;
92 uint32_t pixels = 0;
93 };
94 struct Done
95 {
96 std::string key;
97 bool ok = false;
98 Picture picture;
99 };
100
101 void Collect();
102 void Forget(const std::string& key);
103 void Work();
104
105 ITextSystem& m_textSystem;
106 size_t m_capacity;
107 Reader m_reader;
108 std::chrono::milliseconds m_inUse;
109 std::unordered_map<std::string, Entry> m_entries;
110 std::list<std::string> m_used;
111
112 // Shared with the reading thread.
113 std::mutex m_mutex;
114 std::condition_variable m_wake;
115 std::vector<Job> m_jobs;
116 std::vector<Done> m_done;
117 std::function<void()> m_onRead;
118 bool m_stop = false;
119 std::thread m_thread;
120 };
121
122#ifdef _WIN32
123 namespace detail
124 {
127 bool BitmapPixels(void* bitmap, uint32_t& width, uint32_t& height, std::vector<uint8_t>& rgba);
128 }
129#endif
130
132}
void SetOnRead(std::function< void()> onRead)
Called on the reading thread each time a file was read, with a thumbnail or without: ask for a frame ...
FileThumbnails & operator=(const FileThumbnails &)=delete
static std::string Key(const std::filesystem::path &file, std::filesystem::file_time_type modified, uint32_t pixels)
What a thumbnail is kept by: the same for the same file, time and size.
FileThumbnails(ITextSystem &textSystem, size_t capacity=512, Reader reader=ShellReader(), std::chrono::milliseconds inUse=std::chrono::milliseconds(500))
size_t Reading()
How many of those are still being read.
FileThumbnails(const FileThumbnails &)=delete
std::function< bool(const std::filesystem::path &file, uint32_t pixels, Picture &picture)> Reader
Reads file's thumbnail, at most pixels square, keeping its shape.
Definition gui_file_thumbnails.h:50
IconID Get(const std::filesystem::path &file, std::filesystem::file_time_type modified, uint32_t pixels)
The thumbnail of the file file on the disk, last written at modified, at most pixels square (the icon...
static Reader ShellReader()
The Windows shell's thumbnails (IShellItemImageFactory, thumbnails only, never the type's icon); an e...
size_t Count() const
How many are kept, read or still being read.
Definition gui_file_thumbnails.h:70
Text and icon registration, measurement, rendering and editing.
Definition gui_text_system.h:189
IconID
Handle for an icon registered with ITextSystem::RegisterIconFromSVG / RegisterIconFromSVGFile.
Definition gui_style_text.h:15
@ Invalid
Definition gui_style_text.h:15
Definition gui_logging.h:24
RGBA pixels, rows from the top, straight alpha.
Definition gui_file_thumbnails.h:44
uint2 size
Definition gui_file_thumbnails.h:45
std::vector< uint8_t > rgba
Definition gui_file_thumbnails.h:46
Definition gui_base_types.h:74