From 42e4129f1d9492c873b7c66c2cfaacd317e574ba Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Wed, 20 Sep 2023 17:18:51 -0700 Subject: [PATCH 1/6] WIP: UI: Recent Menu Previews Showing details, with preview image, for Recent menu. --- source/blender/blenloader/BLO_readfile.h | 8 ++ source/blender/blenloader/intern/readfile.cc | 11 ++ .../editors/interface/interface_templates.cc | 105 +++++++++++++++++- .../editors/space_topbar/space_topbar.cc | 20 +--- 4 files changed, 125 insertions(+), 19 deletions(-) diff --git a/source/blender/blenloader/BLO_readfile.h b/source/blender/blenloader/BLO_readfile.h index 91311bfbcf6..830276f485c 100644 --- a/source/blender/blenloader/BLO_readfile.h +++ b/source/blender/blenloader/BLO_readfile.h @@ -532,6 +532,14 @@ void BLO_sanitize_experimental_features_userpref_blend(struct UserDef *userdef); */ struct BlendThumbnail *BLO_thumbnail_from_file(const char *filepath); +/** + * Does a very light reading of given .blend file to extract its version. + * + * \param filepath: The path of the blend file to extract version from. + * \return The file version + */ +short BLO_version_from_file(const char *filepath); + /** Default theme, see: `release/datafiles/userdef/userdef_default_theme.c`. */ extern const struct bTheme U_theme_default; /** Default preferences, defined by: `release/datafiles/userdef/userdef_default.c`. */ diff --git a/source/blender/blenloader/intern/readfile.cc b/source/blender/blenloader/intern/readfile.cc index 52ed617748a..6311aabdc1a 100644 --- a/source/blender/blenloader/intern/readfile.cc +++ b/source/blender/blenloader/intern/readfile.cc @@ -1409,6 +1409,17 @@ BlendThumbnail *BLO_thumbnail_from_file(const char *filepath) return data; } +short BLO_version_from_file(const char *filepath) +{ + short version = 0; + FileData *fd = blo_filedata_from_file_minimal(filepath); + if (fd) { + version = fd->fileversion; + blo_filedata_free(fd); + } + return version; +} + /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/editors/interface/interface_templates.cc b/source/blender/editors/interface/interface_templates.cc index 4fd1e434afa..6090566eafe 100644 --- a/source/blender/editors/interface/interface_templates.cc +++ b/source/blender/editors/interface/interface_templates.cc @@ -27,6 +27,7 @@ #include "DNA_texture_types.h" #include "BLI_alloca.h" +#include "BLI_fileops.h" #include "BLI_fnmatch.h" #include "BLI_listbase.h" #include "BLI_math_color.h" @@ -69,6 +70,8 @@ #include "BKE_screen.h" #include "BKE_shader_fx.h" +#include "BLO_readfile.h" + #include "DEG_depsgraph.h" #include "DEG_depsgraph_build.h" #include "DEG_depsgraph_query.h" @@ -80,6 +83,11 @@ #include "ED_screen.hh" #include "ED_undo.hh" +#include "IMB_imbuf.h" +#include "IMB_imbuf_types.h" +#include "IMB_metadata.h" +#include "IMB_thumbs.h" + #include "RE_engine.h" #include "RNA_access.hh" @@ -7174,10 +7182,99 @@ void uiTemplateCacheFile(uiLayout *layout, /* -------------------------------------------------------------------- */ /** \name Recent Files Template * \{ */ +static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, + struct uiTooltipData *tip, + void *argN) +{ + char *path = (char *)argN; + eFileAttributes attributes = BLI_file_attributes(path); + + /* Filename. */ + UI_tooltip_text_field_add( + tip, BLI_strdup(path), nullptr, UI_TIP_STYLE_HEADER, UI_TIP_LC_MAIN); + + /* Add pad variable to the very next field only. */ + bool pad = true; + + /* Blender version. */ + char version_st[128] = {0}; + ImBuf *thumb = IMB_thumb_read(path, THB_LARGE); + if (thumb) { + /* Look for version in existing thumbnail if available. */ + IMB_metadata_get_field( + thumb->metadata, "Thumb::Blender::Version", version_st, sizeof(version_st)); + } + + if (!version_st[0] && !(attributes & FILE_ATTR_OFFLINE)) { + /* Load Blender version directly from the file. */ + short version = BLO_version_from_file(path); + if (version != 0) { + SNPRINTF(version_st, "%d.%01d", version / 100, version % 100); + } + } + + if (version_st[0]) { + UI_tooltip_text_field_add(tip, + BLI_sprintfN("Blender: %s", version_st), + nullptr, + UI_TIP_STYLE_NORMAL, + UI_TIP_LC_NORMAL, pad); + pad = false; + } + + BLI_stat_t status; + if (BLI_stat(path, &status) != -1) { + char date_st[FILELIST_DIRENTRY_DATE_LEN], time_st[FILELIST_DIRENTRY_TIME_LEN]; + bool is_today, is_yesterday; + std::string day_string = (""); + BLI_filelist_entry_datetime_to_string( + nullptr, int64_t(status.st_mtime), false, time_st, date_st, &is_today, &is_yesterday); + if (is_today || is_yesterday) { + day_string = (is_today ? N_("Today") : N_("Yesterday")) + std::string(" "); + } + UI_tooltip_text_field_add( + tip, + BLI_sprintfN("%s: %s%s %s", N_("Modified"), day_string.c_str(), date_st, time_st), + nullptr, + UI_TIP_STYLE_NORMAL, + UI_TIP_LC_NORMAL, pad); + pad = false; + + if (status.st_size > 0) { + char size[16]; + char size_full[16]; + BLI_filelist_entry_size_to_string(NULL, status.st_size, false, size); + BLI_str_format_uint64_grouped(size_full, status.st_size); + UI_tooltip_text_field_add( + tip, + BLI_sprintfN("%s: %s (%s %s)", N_("Size"), size, size_full, N_("bytes")), + nullptr, + UI_TIP_STYLE_NORMAL, + UI_TIP_LC_NORMAL); + } + } + + if (!thumb) { + /* try to load from the blend file itself. */ + BlendThumbnail *data = BLO_thumbnail_from_file(path); + thumb = BKE_main_thumbnail_to_imbuf(nullptr, data); + if (data) { + MEM_freeN(data); + } + } + + if (thumb) { + float scale = (256.0f * UI_SCALE_FAC) / float(MAX2(thumb->x, thumb->y)); + short size[2] = {short(float(thumb->x) * scale), short(float(thumb->y) * scale)}; + UI_tooltip_image_field_add(tip, thumb, size); + IMB_freeImBuf(thumb); + } + +} int uiTemplateRecentFiles(uiLayout *layout, int rows) { - int i; + int i = 0; LISTBASE_FOREACH_INDEX (RecentFile *, recent, &G.recent_files, i) { if (i >= rows) { break; @@ -7193,8 +7290,14 @@ int uiTemplateRecentFiles(uiLayout *layout, int rows) WM_OP_INVOKE_DEFAULT, UI_ITEM_NONE, &ptr); + RNA_string_set(&ptr, "filepath", recent->filepath); RNA_boolean_set(&ptr, "display_file_selector", false); + + uiBlock *block = uiLayoutGetBlock(layout); + uiBut *but = ui_but_last(block); + UI_but_func_tooltip_custom_set( + but, uiTemplateRecentFiles_tooltip_func, BLI_strdup(recent->filepath), MEM_freeN); } return i; diff --git a/source/blender/editors/space_topbar/space_topbar.cc b/source/blender/editors/space_topbar/space_topbar.cc index fce425ac86d..bad7e6492d9 100644 --- a/source/blender/editors/space_topbar/space_topbar.cc +++ b/source/blender/editors/space_topbar/space_topbar.cc @@ -190,24 +190,8 @@ static void recent_files_menu_draw(const bContext * /*C*/, Menu *menu) { uiLayout *layout = menu->layout; uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_DEFAULT); - if (!BLI_listbase_is_empty(&G.recent_files)) { - LISTBASE_FOREACH (RecentFile *, recent, &G.recent_files) { - const char *file = BLI_path_basename(recent->filepath); - const int icon = BKE_blendfile_extension_check(file) ? ICON_FILE_BLEND : ICON_FILE_BACKUP; - PointerRNA ptr; - uiItemFullO(layout, - "WM_OT_open_mainfile", - file, - icon, - nullptr, - WM_OP_INVOKE_DEFAULT, - UI_ITEM_NONE, - &ptr); - RNA_string_set(&ptr, "filepath", recent->filepath); - RNA_boolean_set(&ptr, "display_file_selector", false); - } - } - else { + + if (uiTemplateRecentFiles(layout, U.recent_files) == 0) { uiItemL(layout, IFACE_("No Recent Files"), ICON_NONE); } } -- 2.30.2 From 8ef61ab4c98a134041b548f7ff2e0feb8271b22f Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Sun, 12 Nov 2023 11:41:56 -0800 Subject: [PATCH 2/6] Formatting changes, checker bg, outline. --- .../interface/interface_region_tooltip.cc | 27 ++++++++++++++++++- .../editors/interface/interface_templates.cc | 8 +++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/source/blender/editors/interface/interface_region_tooltip.cc b/source/blender/editors/interface/interface_region_tooltip.cc index 6b3dfa9c047..338f0d162e2 100644 --- a/source/blender/editors/interface/interface_region_tooltip.cc +++ b/source/blender/editors/interface/interface_region_tooltip.cc @@ -265,6 +265,13 @@ static void ui_tooltip_region_draw_cb(const bContext * /*C*/, ARegion *region) bbox.ymax -= field->image_size[1]; GPU_blend(GPU_BLEND_ALPHA_PREMULT); + + /* Draw checker pattern behind the image in case is has transparency. */ + imm_draw_box_checker_2d(float(bbox.xmin), + float(bbox.ymax), + float(bbox.xmin + field->image_size[0]), + float(bbox.ymax + field->image_size[1])); + IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_3D_IMAGE_COLOR); immDrawPixelsTexScaledFullSize(&state, bbox.xmin, @@ -279,8 +286,26 @@ static void ui_tooltip_region_draw_cb(const bContext * /*C*/, ARegion *region) float(field->image_size[0]) / float(field->image->x), float(field->image_size[1]) / float(field->image->y), nullptr); - GPU_blend(GPU_BLEND_ALPHA); + + GPUVertFormat *format = immVertexFormat(); + uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); + immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + float border_color[4] = {1.0f, 1.0f, 1.0f, 0.15f}; + float bgcolor[4]; + UI_GetThemeColor4fv(TH_BACK, bgcolor); + if (rgb_to_grayscale(bgcolor) > 0.5f) { + border_color[0] = 0.0f; + border_color[1] = 0.0f; + border_color[2] = 0.0f; + } + immUniformColor4fv(border_color); + imm_draw_box_wire_2d(pos, + float(bbox.xmin), + float(bbox.ymax), + float(bbox.xmin + field->image_size[0]), + float(bbox.ymax + field->image_size[1])); + immUnbindProgram(); } else if (field->format.style == UI_TIP_STYLE_SPACER) { bbox.ymax -= data->lineh * UI_TIP_SPACER; diff --git a/source/blender/editors/interface/interface_templates.cc b/source/blender/editors/interface/interface_templates.cc index 3eaa682f136..d100d7a7106 100644 --- a/source/blender/editors/interface/interface_templates.cc +++ b/source/blender/editors/interface/interface_templates.cc @@ -7191,9 +7191,9 @@ static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, eFileAttributes attributes = BLI_file_attributes(path); /* Filename. */ - UI_tooltip_text_field_add( - tip, BLI_strdup(path), nullptr, UI_TIP_STYLE_HEADER, UI_TIP_LC_MAIN); - UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + //UI_tooltip_text_field_add( + // tip, BLI_strdup(path), nullptr, UI_TIP_STYLE_HEADER, UI_TIP_LC_MAIN); + //UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); /* Blender version. */ char version_st[128] = {0}; @@ -7261,7 +7261,7 @@ static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, } if (thumb) { - float scale = (64.0f * UI_SCALE_FAC) / float(MAX2(thumb->x, thumb->y)); + float scale = (72.0f * UI_SCALE_FAC) / float(MAX2(thumb->x, thumb->y)); short size[2] = {short(float(thumb->x) * scale), short(float(thumb->y) * scale)}; UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); -- 2.30.2 From 56a8f0d9b87565db2cc44cbf6f58d7ce0813059e Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Sun, 12 Nov 2023 13:28:46 -0800 Subject: [PATCH 3/6] Show file path --- .../blender/editors/interface/interface_region_tooltip.cc | 1 + source/blender/editors/interface/interface_templates.cc | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/source/blender/editors/interface/interface_region_tooltip.cc b/source/blender/editors/interface/interface_region_tooltip.cc index 338f0d162e2..8f77e7e38d5 100644 --- a/source/blender/editors/interface/interface_region_tooltip.cc +++ b/source/blender/editors/interface/interface_region_tooltip.cc @@ -288,6 +288,7 @@ static void ui_tooltip_region_draw_cb(const bContext * /*C*/, ARegion *region) nullptr); GPU_blend(GPU_BLEND_ALPHA); + /* Draw border around it. */ GPUVertFormat *format = immVertexFormat(); uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT); immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); diff --git a/source/blender/editors/interface/interface_templates.cc b/source/blender/editors/interface/interface_templates.cc index d100d7a7106..b619fdc9e96 100644 --- a/source/blender/editors/interface/interface_templates.cc +++ b/source/blender/editors/interface/interface_templates.cc @@ -7188,13 +7188,18 @@ static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, void *argN) { char *path = (char *)argN; - eFileAttributes attributes = BLI_file_attributes(path); /* Filename. */ //UI_tooltip_text_field_add( // tip, BLI_strdup(path), nullptr, UI_TIP_STYLE_HEADER, UI_TIP_LC_MAIN); //UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + /* File path. */ + char root[FILE_MAX]; + BLI_path_split_dir_part(path, root, FILE_MAX); + UI_tooltip_text_field_add(tip, BLI_strdup(root), nullptr, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + /* Blender version. */ char version_st[128] = {0}; ImBuf *thumb = IMB_thumb_read(path, THB_LARGE); @@ -7204,6 +7209,7 @@ static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, thumb->metadata, "Thumb::Blender::Version", version_st, sizeof(version_st)); } + eFileAttributes attributes = BLI_file_attributes(path); if (!version_st[0] && !(attributes & FILE_ATTR_OFFLINE)) { /* Load Blender version directly from the file. */ short version = BLO_version_from_file(path); -- 2.30.2 From d4fe22e2487bc51a1f84e9284462b9cb6661326f Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Thu, 7 Dec 2023 17:07:22 -0800 Subject: [PATCH 4/6] Updated to be close to #104547 --- .../editors/interface/interface_templates.cc | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/source/blender/editors/interface/interface_templates.cc b/source/blender/editors/interface/interface_templates.cc index 7ee95c2f3dc..3de4beba5b3 100644 --- a/source/blender/editors/interface/interface_templates.cc +++ b/source/blender/editors/interface/interface_templates.cc @@ -7198,10 +7198,11 @@ static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, char root[FILE_MAX]; BLI_path_split_dir_part(path, root, FILE_MAX); UI_tooltip_text_field_add(tip, BLI_strdup(root), nullptr, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); - UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); /* Blender version. */ char version_st[128] = {0}; + /* Load the thumbnail from cache if existing, but don't create if not. */ ImBuf *thumb = IMB_thumb_read(path, THB_LARGE); if (thumb) { /* Look for version in existing thumbnail if available. */ @@ -7220,10 +7221,11 @@ static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, if (version_st[0]) { UI_tooltip_text_field_add(tip, - BLI_sprintfN("Blender: %s", version_st), + BLI_sprintfN("Blender %s", version_st), nullptr, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); } BLI_stat_t status; @@ -7236,24 +7238,24 @@ static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, if (is_today || is_yesterday) { day_string = (is_today ? N_("Today") : N_("Yesterday")) + std::string(" "); } - UI_tooltip_text_field_add( - tip, - BLI_sprintfN("%s: %s%s %s", N_("Modified"), day_string.c_str(), date_st, time_st), - nullptr, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, + BLI_sprintfN("%s: %s%s%s", + N_("Modified"), + day_string.c_str(), + (is_today || is_yesterday) ? "" : date_st, + (is_today || is_yesterday) ? time_st : ""), + nullptr, + UI_TIP_STYLE_NORMAL, + UI_TIP_LC_NORMAL); if (status.st_size > 0) { char size[16]; - char size_full[16]; BLI_filelist_entry_size_to_string(NULL, status.st_size, false, size); - BLI_str_format_uint64_grouped(size_full, status.st_size); - UI_tooltip_text_field_add( - tip, - BLI_sprintfN("%s: %s (%s %s)", N_("Size"), size, size_full, N_("bytes")), - nullptr, - UI_TIP_STYLE_NORMAL, - UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, + BLI_sprintfN("%s: %s", N_("Size"), size), + nullptr, + UI_TIP_STYLE_NORMAL, + UI_TIP_LC_NORMAL); } } -- 2.30.2 From b172f69b87193b7dff6899bb05f899233aa1a3e8 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Wed, 3 Jan 2024 10:32:01 -0800 Subject: [PATCH 5/6] Small changes. --- source/blender/editors/interface/interface_templates.cc | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/source/blender/editors/interface/interface_templates.cc b/source/blender/editors/interface/interface_templates.cc index 51c5eac2cb2..0a8dcb74ed7 100644 --- a/source/blender/editors/interface/interface_templates.cc +++ b/source/blender/editors/interface/interface_templates.cc @@ -7199,15 +7199,10 @@ static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, { char *path = (char *)argN; - /* Filename. */ - //UI_tooltip_text_field_add( - // tip, BLI_strdup(path), nullptr, UI_TIP_STYLE_HEADER, UI_TIP_LC_MAIN); - //UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); - /* File path. */ char root[FILE_MAX]; BLI_path_split_dir_part(path, root, FILE_MAX); - UI_tooltip_text_field_add(tip, BLI_strdup(root), nullptr, UI_TIP_STYLE_NORMAL, UI_TIP_LC_NORMAL); + UI_tooltip_text_field_add(tip, BLI_strdup(root), nullptr, UI_TIP_STYLE_HEADER, UI_TIP_LC_NORMAL); UI_tooltip_text_field_add(tip, nullptr, nullptr, UI_TIP_STYLE_SPACER, UI_TIP_LC_NORMAL); /* Blender version. */ -- 2.30.2 From 9be6d2ed86dfafb8b622e33bd623697d5a2a15c1 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Wed, 3 Jan 2024 14:57:41 -0800 Subject: [PATCH 6/6] small formatting changes --- source/blender/editors/interface/interface_templates.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/blender/editors/interface/interface_templates.cc b/source/blender/editors/interface/interface_templates.cc index 555576d1575..a664b484e3b 100644 --- a/source/blender/editors/interface/interface_templates.cc +++ b/source/blender/editors/interface/interface_templates.cc @@ -7281,7 +7281,6 @@ static void uiTemplateRecentFiles_tooltip_func(bContext * /*C*/, UI_tooltip_image_field_add(tip, thumb, size); IMB_freeImBuf(thumb); } - } int uiTemplateRecentFiles(uiLayout *layout, int rows) @@ -7302,7 +7301,6 @@ int uiTemplateRecentFiles(uiLayout *layout, int rows) WM_OP_INVOKE_DEFAULT, UI_ITEM_NONE, &ptr); - RNA_string_set(&ptr, "filepath", recent->filepath); RNA_boolean_set(&ptr, "display_file_selector", false); -- 2.30.2