Blender V5.3
interface_template_recent_files.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cmath>
10
11#include <fmt/format.h>
12
13#include "BLI_fileops.hh"
14#include "BLI_listbase.hh"
15#include "BLI_path_utils.hh"
16#include "BLI_set.hh"
17#include "BLI_string.hh"
18#include "BLI_string_utf8.hh"
19
20#include "BLO_readfile.hh"
21
22#include "BLT_date_string.hh"
23#include "BLT_lang.hh"
24#include "BLT_translation.hh"
25
26#include "BKE_autoexec.hh"
28#include "BKE_blendfile.hh"
29#include "BKE_global.hh"
30#include "BKE_main.hh"
31
32#include "IMB_imbuf.hh"
33#include "IMB_metadata.hh"
34#include "IMB_thumbs.hh"
35
36#include "RNA_access.hh"
37
39
40#include "UI_resources.hh"
41#include "WM_api.hh"
42#include "WM_types.hh"
43
44#include "interface_intern.hh"
45
46namespace blender::ui {
47
49 TooltipData &tip,
50 Button * /*but*/,
51 void *argN)
52{
53 char *path = static_cast<char *>(argN);
54
55 /* File name and path. */
56 char dirname[FILE_MAX];
57 char filename[FILE_MAX];
61
63
64 if (!BLI_exists(path)) {
65 tooltip_text_field_add(tip, TIP_("File not found"), {}, TIP_STYLE_NORMAL, TIP_LC_ALERT);
66 return;
67 }
68
69 /* Blender version. */
70 char version_str[128] = {0};
71 /* Load the thumbnail from cache if existing, but don't create if not. */
72 ImBuf *thumb = IMB_thumb_read(path, THB_LARGE);
73 if (thumb) {
74 /* Look for version in existing thumbnail if available. */
76 thumb->metadata(), "Thumb::Blender::Version", version_str, sizeof(version_str));
77 }
78
79 eFileAttributes attributes = BLI_file_attributes(path);
80 if (!version_str[0] && !(attributes & FILE_ATTR_OFFLINE)) {
81 /* Load Blender version directly from the file. */
82 short version = BLO_version_from_file(path);
83 if (version != 0) {
84 SNPRINTF_UTF8(version_str, "%d.%01d", version / 100, version % 100);
85 }
86 }
87
88 if (version_str[0]) {
90 tip, fmt::format("Blender {}", version_str), {}, TIP_STYLE_NORMAL, TIP_LC_VALUE);
91 }
92
93 BLI_stat_t status;
94 if (BLI_stat(path, &status) != -1) {
95 if (status.st_size > 0) {
96 char size[16];
97 BLI_filelist_entry_size_to_string(nullptr, status.st_size, false, size);
99 fmt::format(fmt::runtime(TIP_("Size: {}")), size),
100 {},
103 }
104
105 const tm mod_time = date_string::localtime_safe(status.st_mtime);
106 const time_t ts_now = time(nullptr);
107 const tm now = date_string::localtime_safe(ts_now);
108 const char *lang = BLT_lang_get();
109 std::string modified_s = date_string::datetime(mod_time,
110 lang,
111 date_string::DateFormat(U.date_format),
112 date_string::TimeFormat(U.time_format),
113 &now,
114 TIP_("Today"),
115 TIP_("Yesterday"));
117 fmt::format(fmt::runtime(TIP_("Modified: {}")), modified_s),
118 {},
121 }
122
123 if (!thumb) {
124 /* try to load from the blend file itself. */
126 thumb = BKE_main_thumbnail_to_imbuf(nullptr, data);
127 if (data) {
128 MEM_delete(data);
129 }
130 }
131
132 if (thumb) {
135
136 TooltipImage image_data;
137 float scale = (72.0f * UI_SCALE_FAC) / float(std::max(thumb->x, thumb->y));
138 image_data.ibuf = thumb;
139 image_data.width = short(float(thumb->x) * scale);
140 image_data.height = short(float(thumb->y) * scale);
141 image_data.border = true;
142 image_data.background = TooltipImageBackground::Checkerboard_Themed;
143 image_data.premultiplied = true;
144 tooltip_image_field_add(tip, image_data);
145 IMB_freeImBuf(thumb);
146 }
147}
148
153
154static RecentFilesList recent_files_get(const std::optional<std::string> &project_root,
155 const int rows,
156 const int project_rows)
157{
158 /* In a project, reserve rows for the most recent project files. */
160 if (project_root) {
161 for (const RecentFile &recent : G.recent_files) {
162 if (shown.size() >= project_rows) {
163 break;
164 }
165 if (BLI_path_contains(project_root->c_str(), recent.filepath)) {
166 shown.add(&recent);
167 }
168 }
169 }
170
171 /* Fill remaining rows with the most recent other files, in the project or not. */
172 int64_t remaining = rows - shown.size();
173 for (const RecentFile &recent : G.recent_files) {
174 if (remaining <= 0) {
175 break;
176 }
177 if (shown.add(&recent)) {
178 remaining--;
179 }
180 }
181
182 /* Project files first, each group most recent first. */
184 for (const RecentFile &recent : G.recent_files) {
185 if (!shown.contains(&recent)) {
186 continue;
187 }
188 if (project_root && BLI_path_contains(project_root->c_str(), recent.filepath)) {
189 list.project_files.append(recent.filepath);
190 }
191 else {
192 list.other_files.append(recent.filepath);
193 }
194 }
195 return list;
196}
197
198static void template_recent_file(Layout *layout, const std::string &filepath)
199{
200 const char *filename = BLI_path_basename(filepath.c_str());
201 PointerRNA ptr = layout->op("WM_OT_open_mainfile",
202 filename,
203 BKE_blendfile_extension_check(filename) ? ICON_FILE_BLEND :
204 ICON_FILE_BACKUP,
207 RNA_string_set(&ptr, "filepath", filepath.c_str());
208 RNA_boolean_set(&ptr, "display_file_selector", false);
209 RNA_boolean_set(&ptr,
210 "use_scripts",
211 BKE_autoexec_default_trust_source(filepath.c_str(),
212 {
213 .skip_overrides = false,
214 .canonicalize = true,
215 .strip_filename = true,
216 }));
217
218 Block *block = layout->block();
219 Button *but = button_last(block);
222}
223
225 int rows,
227 const float project_ratio)
228{
229 const std::optional<std::string> project_root = BKE_blender_project_read_callback(
230 G_MAIN, [](const bke::BlenderProject *project) -> std::optional<std::string> {
231 if (project == nullptr) {
232 return std::nullopt;
233 }
234 return project->get_root_path();
235 });
236
237 const int project_rows = int(std::ceil(rows * project_ratio));
238 const RecentFilesList list = recent_files_get(project_root, rows, project_rows);
239
240 int found = 0;
242 for (const std::string &filepath : list.project_files) {
243 template_recent_file(layout, filepath);
244 found++;
245 }
246 }
248 for (const std::string &filepath : list.other_files) {
249 template_recent_file(layout, filepath);
250 found++;
251 }
252 }
253 return found;
254}
255
257 TooltipData &tip,
258 Button * /*but*/,
259 void *argN)
260{
261 const char *path = static_cast<const char *>(argN);
263
266 tooltip_text_field_add(tip, TIP_("Project Not Found"), {}, TIP_STYLE_NORMAL, TIP_LC_ALERT);
267 }
268}
269
270int template_recent_projects(Layout *layout, int rows)
271{
272 wmOperatorType *ot = WM_operatortype_find("PROJECT_OT_open", false);
273
274 const std::string active_path = BKE_blender_project_read_callback(
275 G_MAIN, [](const bke::BlenderProject *project) -> std::string {
276 return project ? project->get_root_path() : "";
277 });
278
279 int i = 0;
281 if (i >= rows) {
282 break;
283 }
284
285 if (ot) {
286 const bool is_active = !active_path.empty() &&
287 BLI_path_cmp_normalized(project.path.c_str(), active_path.c_str()) ==
288 0;
289 PointerRNA ptr = layout->op(ot,
290 project.name,
291 is_active ? ICON_PROJECT : ICON_NONE,
294 RNA_string_set(&ptr, "directory", project.path.c_str());
295
296 Block *block = layout->block();
297 Button *but = button_last(block);
300 BLI_strdup(project.path.c_str()),
302 if (is_active) {
303 button_disable(but, TIP_("Project is already open"));
304 }
305 }
306 else {
307 layout->label(project.name, ICON_NONE);
308 }
309 i++;
310 }
311
312 return i;
313}
314
315} // namespace blender::ui
#define G_MAIN
File and directory operations.
#define FILE_MAX
#define SNPRINTF_UTF8(dst, format,...)
const char * dirname(char *path)
external readfile function prototypes.
#define TIP_(msgid)
#define UI_SCALE_FAC
#define UI_ITEM_NONE
int64_t size() const
Definition BLI_set.hh:606
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
float3x3 scale(float3x3 mat, float2 scale)
std::string datetime(const std::tm &date_time, StringRef locale_iso={}, DateFormat date_format=DateFormat::Default, TimeFormat time_format=TimeFormat::H24, const std::tm *now=nullptr, StringRef today={}, StringRef yesterday={})
std::tm localtime_safe(time_t time)
std::string filename(const std::string_view &str, size_t pos)
static void template_recent_projects_tooltip_func(bContext &, TooltipData &tip, Button *, void *argN)
static void template_recent_file(Layout *layout, const std::string &filepath)
static void template_recent_files_tooltip_func(bContext &, TooltipData &tip, Button *, void *argN)
int template_recent_projects(Layout *layout, int rows)
int template_recent_files(Layout *layout, int rows, RecentFilesFilter filter=RecentFilesFilter::All, float project_ratio=0.5f)
static RecentFilesList recent_files_get(const std::optional< std::string > &project_root, const int rows, const int project_rows)
void button_func_tooltip_custom_set(Button *but, ButtonToolTipCustomFunc func, void *arg, FreeArgFunc free_arg)
void button_disable(Button *but, const char *disabled_hint)
void tooltip_image_field_add(TooltipData &data, const TooltipImage &image_data)
void tooltip_text_field_add(TooltipData &data, std::string text, std::string suffix, const TooltipStyle style, const TooltipColorID color_id, const bool is_pad=false)
Button * button_last(Block *block) ATTR_WARN_UNUSED_RESULT
UserDef U
Definition blender.cc:58
__int64 int64_t
Definition BKE_text.h:90
Vector< RecentProject > BKE_blender_project_recent_get()
ImBuf * BKE_main_thumbnail_to_imbuf(Main *bmain, BlendThumbnail *data)
Definition main.cc:1029
short BLO_version_from_file(const char *filepath)
Definition readfile.cc:1485
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
bool BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition storage.cc:404
BlendThumbnail * BLO_thumbnail_from_file(const char *filepath)
Definition readfile.cc:1458
ImBuf * IMB_thumb_read(const char *file_or_lib_path, ThumbSize size)
Definition thumbs.cc:567
auto BKE_blender_project_read_callback(const Main *bmain, Fn lambda)
eFileAttributes BLI_file_attributes(const char *path)
Definition storage.cc:239
void MEM_delete_void(void *vmemh)
Global G
Definition blender.cc:57
@ FILE_ATTR_OFFLINE
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void IMB_freeImBuf(ImBuf *ibuf)
BMesh const char void * data
int BLI_stat(const char *path, BLI_stat_t *buffer) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
wmOperatorType * WM_operatortype_find(const char *idname, bool quiet)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
bool BKE_autoexec_default_trust_source(const char *path, const AutoExec_Params &params)
Definition autoexec.cc:103
bool BLI_path_contains(const char *container_path, const char *containee_path) ATTR_NONNULL(1
const char * BLT_lang_get()
Definition blt_lang.cc:268
void BLI_path_split_dir_file(const char *filepath, char *dir, size_t dir_maxncpy, char *file, size_t file_maxncpy) ATTR_NONNULL(1
bool IMB_metadata_get_field(const IDProperty *metadata, const char *key, char *value, size_t value_maxncpy)
bool BKE_blendfile_extension_check(const char *str)
Definition blendfile.cc:88
bool BKE_blender_project_dir_is_project(StringRef path)
struct stat BLI_stat_t
void BLI_filelist_entry_size_to_string(const struct stat *st, uint64_t st_size_fallback, bool compact, char r_size[FILELIST_DIRENTRY_SIZE_LEN])
const IDProperty * metadata() const
void label(StringRef name, int icon)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)