[Bf-blender-cvs] [b0a6a388237] asset-browser-poselib: Asset: Keep assets active after renaming, make sure they are scrolled into view

Julian Eisel noreply at git.blender.org
Thu Apr 29 18:29:51 CEST 2021


Commit: b0a6a38823729a1010db4408d1e60ad6f97ba377
Author: Julian Eisel
Date:   Thu Apr 29 18:19:45 2021 +0200
Branches: asset-browser-poselib
https://developer.blender.org/rBb0a6a38823729a1010db4408d1e60ad6f97ba377

Asset: Keep assets active after renaming, make sure they are scrolled into view

I want to clean this up a bit still, but it seems to work now.

Also includes (to be split off):
* Don't allow renaming assets from the file list for now (e.g. right-click >
  Rename). This doesn't work yet and I'm not sure it should even. If an asset
  is a local data-block it can be renamed in the sidebar.
* Fix broken logic to scroll a preview into view.
* Partially includes D9994. These changes were needed for this to work.

Differential Revision: https://developer.blender.org/D11119

===================================================================

M	source/blender/blenkernel/intern/screen.c
M	source/blender/editors/space_file/file_draw.c
M	source/blender/editors/space_file/file_intern.h
M	source/blender/editors/space_file/file_ops.c
M	source/blender/editors/space_file/filelist.c
M	source/blender/editors/space_file/filelist.h
M	source/blender/editors/space_file/filesel.c
M	source/blender/editors/space_file/space_file.c
M	source/blender/makesdna/DNA_space_types.h

===================================================================

diff --git a/source/blender/blenkernel/intern/screen.c b/source/blender/blenkernel/intern/screen.c
index d0d63192ebf..9b9d961c268 100644
--- a/source/blender/blenkernel/intern/screen.c
+++ b/source/blender/blenkernel/intern/screen.c
@@ -1723,6 +1723,14 @@ static void direct_link_area(BlendDataReader *reader, ScrArea *area)
       sfile->runtime = NULL;
       BLO_read_data_address(reader, &sfile->params);
       BLO_read_data_address(reader, &sfile->asset_params);
+      /* XXX No access to filelist_uuid_unset() here. Should be used once direct-linking happens
+       * via a space-type callback. */
+      if (sfile->params) {
+        sfile->params->renamefile_uuid[0] = FILE_UUID_UNSET;
+      }
+      if (sfile->asset_params) {
+        sfile->asset_params->base_params.renamefile_uuid[0] = FILE_UUID_UNSET;
+      }
     }
     else if (sl->spacetype == SPACE_CLIP) {
       SpaceClip *sclip = (SpaceClip *)sl;
diff --git a/source/blender/editors/space_file/file_draw.c b/source/blender/editors/space_file/file_draw.c
index c1dcf2e56d3..cd7f44cc147 100644
--- a/source/blender/editors/space_file/file_draw.c
+++ b/source/blender/editors/space_file/file_draw.c
@@ -506,6 +506,9 @@ static void renamebutton_cb(bContext *C, void *UNUSED(arg1), char *oldname)
   ARegion *region = CTX_wm_region(C);
   FileSelectParams *params = ED_fileselect_get_active_params(sfile);
 
+  /* Renaming asset files is not supported from the file list. */
+  BLI_assert(!ED_fileselect_is_asset_browser(sfile));
+
   BLI_join_dirfile(orgname, sizeof(orgname), params->dir, oldname);
   BLI_strncpy(filename, params->renamefile, sizeof(filename));
   BLI_filename_make_safe(filename);
@@ -521,13 +524,7 @@ static void renamebutton_cb(bContext *C, void *UNUSED(arg1), char *oldname)
       else {
         /* If rename is successful, scroll to newly renamed entry. */
         BLI_strncpy(params->renamefile, filename, sizeof(params->renamefile));
-        params->rename_flag = FILE_PARAMS_RENAME_POSTSCROLL_PENDING;
-
-        if (sfile->smoothscroll_timer != NULL) {
-          WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), sfile->smoothscroll_timer);
-        }
-        sfile->smoothscroll_timer = WM_event_add_timer(wm, CTX_wm_window(C), TIMER1, 1.0 / 1000.0);
-        sfile->scroll_offset = 0;
+        file_params_invoke_rename_postscroll(wm, CTX_wm_window(C), sfile);
       }
 
       /* to make sure we show what is on disk */
@@ -957,6 +954,8 @@ void file_draw_list(const bContext *C, ARegion *region)
                               textwidth :
                               layout->attribute_columns[COLUMN_NAME].width -
                                   ATTRIBUTE_COLUMN_PADDING;
+      /* Assets don't support renaming via the file list. */
+      BLI_assert((file->typeflag & FILE_TYPE_ASSET) == 0);
 
       uiBut *but = uiDefBut(block,
                             UI_BTYPE_TEXT,
diff --git a/source/blender/editors/space_file/file_intern.h b/source/blender/editors/space_file/file_intern.h
index 309b280177c..1350b3d2982 100644
--- a/source/blender/editors/space_file/file_intern.h
+++ b/source/blender/editors/space_file/file_intern.h
@@ -111,6 +111,9 @@ int file_select_match(struct SpaceFile *sfile, const char *pattern, char *matche
 int autocomplete_directory(struct bContext *C, char *str, void *arg_v);
 int autocomplete_file(struct bContext *C, char *str, void *arg_v);
 
+void file_params_invoke_rename_postscroll(struct wmWindowManager *wm,
+                                          struct wmWindow *win,
+                                          struct SpaceFile *sfile);
 void file_params_renamefile_activate(struct SpaceFile *sfile, struct FileSelectParams *params);
 
 typedef void *onReloadFnData;
diff --git a/source/blender/editors/space_file/file_ops.c b/source/blender/editors/space_file/file_ops.c
index 856bd5b1bc3..d432aee5940 100644
--- a/source/blender/editors/space_file/file_ops.c
+++ b/source/blender/editors/space_file/file_ops.c
@@ -2057,11 +2057,7 @@ static int file_smoothscroll_invoke(bContext *C, wmOperator *UNUSED(op), const w
       sfile->layout, (int)region->v2d.cur.xmin, (int)-region->v2d.cur.ymax);
   const int last_visible_item = first_visible_item + numfiles_layout + 1;
 
-  /* Note: the special case for vertical layout is because filename is at the bottom of items then,
-   * so we artificially move current row back one step, to ensure we show bottom of
-   * active item rather than its top (important in case visible height is low). */
-  const int middle_offset = max_ii(
-      0, (first_visible_item + last_visible_item) / 2 - (is_horizontal ? 0 : items_block_size));
+  const int middle_offset = max_ii(0, (first_visible_item + last_visible_item) / 2);
 
   const int min_middle_offset = numfiles_layout / 2;
   const int max_middle_offset = ((numfiles / items_block_size) * items_block_size +
@@ -2101,6 +2097,7 @@ static int file_smoothscroll_invoke(bContext *C, wmOperator *UNUSED(op), const w
      * rename process is totally finished, cleanup. */
     if ((params->rename_flag & FILE_PARAMS_RENAME_POSTSCROLL_ACTIVE) != 0) {
       params->renamefile[0] = '\0';
+      filelist_uuid_unset(params->renamefile_uuid);
       params->rename_flag = 0;
     }
     return OPERATOR_FINISHED;
@@ -2311,6 +2308,7 @@ static int file_directory_new_exec(bContext *C, wmOperator *op)
 
   /* If we don't enter the directory directly, remember file to jump into editing. */
   if (do_diropen == false) {
+    BLI_assert(filelist_uuid_is_set(params->renamefile_uuid) == false);
     BLI_strncpy(params->renamefile, name, FILE_MAXFILE);
     params->rename_flag = FILE_PARAMS_RENAME_PENDING;
   }
@@ -2687,6 +2685,8 @@ static void file_rename_state_activate(SpaceFile *sfile, int file_idx, bool requ
 
   if ((file_idx >= 0) && (file_idx < numfiles)) {
     FileDirEntry *file = filelist_file(sfile->files, file_idx);
+    /* Renaming assets is not supported here. Caller should check. */
+    BLI_assert((file->typeflag & FILE_TYPE_ASSET) == 0);
 
     if ((require_selected == false) ||
         (filelist_entry_select_get(sfile->files, file, CHECK_ALL) & FILE_SEL_SELECTED)) {
@@ -2730,6 +2730,11 @@ static int file_rename_exec(bContext *C, wmOperator *UNUSED(op))
   return OPERATOR_FINISHED;
 }
 
+static bool file_rename_poll(bContext *C)
+{
+  return ED_operator_file_active(C) && !ED_fileselect_is_asset_browser(CTX_wm_space_file(C));
+}
+
 void FILE_OT_rename(struct wmOperatorType *ot)
 {
   /* identifiers */
@@ -2740,7 +2745,7 @@ void FILE_OT_rename(struct wmOperatorType *ot)
   /* api callbacks */
   ot->invoke = file_rename_invoke;
   ot->exec = file_rename_exec;
-  ot->poll = ED_operator_file_active;
+  ot->poll = file_rename_poll;
 }
 
 /** \} */
diff --git a/source/blender/editors/space_file/filelist.c b/source/blender/editors/space_file/filelist.c
index c2b3a7ac0ee..783f32892a5 100644
--- a/source/blender/editors/space_file/filelist.c
+++ b/source/blender/editors/space_file/filelist.c
@@ -2043,19 +2043,17 @@ FileDirEntry *filelist_file(struct FileList *filelist, int index)
   return filelist_file_ex(filelist, index, true);
 }
 
-int filelist_file_findpath(struct FileList *filelist, const char *filename)
+int filelist_file_find_path(struct FileList *filelist, const char *filename)
 {
-  int fidx = -1;
-
   if (filelist->filelist.nbr_entries_filtered == FILEDIR_NBR_ENTRIES_UNSET) {
-    return fidx;
+    return -1;
   }
 
   /* XXX TODO Cache could probably use a ghash on paths too? Not really urgent though.
    *          This is only used to find again renamed entry,
    *          annoying but looks hairy to get rid of it currently. */
 
-  for (fidx = 0; fidx < filelist->filelist.nbr_entries_filtered; fidx++) {
+  for (int fidx = 0; fidx < filelist->filelist.nbr_entries_filtered; fidx++) {
     FileListInternEntry *entry = filelist->filelist_intern.filtered[fidx];
     if (STREQ(entry->relpath, filename)) {
       return fidx;
@@ -2065,6 +2063,22 @@ int filelist_file_findpath(struct FileList *filelist, const char *filename)
   return -1;
 }
 
+int filelist_file_find_uuid(struct FileList *filelist, const uint32_t uuid[4])
+{
+  if (filelist->filelist.nbr_entries_filtered == FILEDIR_NBR_ENTRIES_UNSET) {
+    return -1;
+  }
+
+  for (int fidx = 0; fidx < filelist->filelist.nbr_entries_filtered; fidx++) {
+    FileListInternEntry *entry = filelist->filelist_intern.filtered[fidx];
+    if (memcmp(entry->uuid, uuid, sizeof(entry->uuid)) == 0) {
+      return fidx;
+    }
+  }
+
+  return -1;
+}
+
 /**
  * Get the ID a file represents (if any). For #FILE_MAIN, #FILE_MAIN_ASSET.
  */
@@ -2073,31 +2087,30 @@ ID *filelist_file_get_id(const FileDirEntry *file)
   return file->id;
 }
 
-FileDirEntry *filelist_entry_find_uuid(struct FileList *filelist, const int uuid[4])
+static void filelist_uuid_from_uint(const uint32_t value, uint32_t r_uuid[4])
 {
-  if (filelist->filelist.nbr_entries_filtered == FILEDIR_NBR_ENTRIES_UNSET) {
-    return NULL;
-  }
-
-  if (filelist->filelist_cache.uuids) {
-    FileDirEntry *entry = BLI_ghash_lookup(filelist->filelist_cache.uuids, uuid);
-    if (entry) {
-      return entry;
-    }
-  }
+  /* Ugly uint32 to uuid (uint32[4]) abuse. */
+  r_uuid[0] = value;
+}
 
-  {
-    int fidx;
+bool filelist_uuid_is_set(const uint32_t uuid[4])
+{
+  uint32_t unset_uuid[4];
+  filelist_uuid_unset(unset_uuid);
+  return memcmp(uuid, unset_uuid, sizeof(unset_uuid)) != 0;
+}
 
-    for (fidx = 0; fidx < filelist->filelist.nbr_entries_filtered; fidx++) {
-      FileListInternEntry *entry = filelist->filelist_intern.filtered[fidx];
-      if (memcmp(entry->uuid, uuid, sizeof(entry->uuid)) == 0) {
-        return filelist_file(filelist, fidx);
-      }
-    }
-  }
+void filelist_uuid_unset(uint32_t r_uuid[4])
+{
+  filelist_uuid_from_uint(FILE_UUID_UNSET, r_uuid);
+}
 
-  return NULL;
+/**
+ * \warning: The UUID will only be valid for the current session. Use as runtime data only!
+ */
+void filelist_uuid_from_id(const ID *id, uint32_t r_uuid[4])
+{
+  filelist_uuid_from_uint(id->session_uuid, r_uuid);
 }
 
 void filelist_file_cache_slidingwindow_set(FileList *filelist, size_t window_size)
@@ -3184,8 +3197,9 @@ static void filelist_readjob_do(const bool do_lib,
        * 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list