Cleanup: Move common File Browser renaming code into functions

Code would manually do the same things in a couple of places, obvious case of
unnecessary code duplication.
This commit is contained in:
Julian Eisel 2021-07-05 13:30:47 +02:00
parent 2c6c1b6cc0
commit 2eca9c7ed4
4 changed files with 52 additions and 24 deletions

View File

@ -542,13 +542,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 */

View File

@ -113,6 +113,13 @@ 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_smoothscroll_timer_clear(struct wmWindowManager *wm,
struct wmWindow *win,
SpaceFile *sfile);
void file_params_renamefile_clear(struct FileSelectParams *params);
void file_params_invoke_rename_postscroll(struct wmWindowManager *wm,
struct wmWindow *win,
SpaceFile *sfile);
void file_params_renamefile_activate(struct SpaceFile *sfile, struct FileSelectParams *params);
typedef void *onReloadFnData;

View File

@ -2059,13 +2059,15 @@ static int file_smoothscroll_invoke(bContext *C, wmOperator *UNUSED(op), const w
}
}
wmWindowManager *wm = CTX_wm_manager(C);
wmWindow *win = CTX_wm_window(C);
/* if we are not editing, we are done */
if (edit_idx == -1) {
/* Do not invalidate timer if filerename is still pending,
* we might still be building the filelist and yet have to find edited entry. */
if (params->rename_flag == 0) {
WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), sfile->smoothscroll_timer);
sfile->smoothscroll_timer = NULL;
file_params_smoothscroll_timer_clear(wm, win, sfile);
}
return OPERATOR_PASS_THROUGH;
}
@ -2073,8 +2075,7 @@ static int file_smoothscroll_invoke(bContext *C, wmOperator *UNUSED(op), const w
/* we need the correct area for scrolling */
region = BKE_area_find_region_type(area, RGN_TYPE_WINDOW);
if (!region || region->regiontype != RGN_TYPE_WINDOW) {
WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), sfile->smoothscroll_timer);
sfile->smoothscroll_timer = NULL;
file_params_smoothscroll_timer_clear(wm, win, sfile);
return OPERATOR_PASS_THROUGH;
}
@ -2131,13 +2132,11 @@ static int file_smoothscroll_invoke(bContext *C, wmOperator *UNUSED(op), const w
(max_middle_offset - middle_offset < items_block_size));
if (is_ready && (is_centered || is_full_start || is_full_end)) {
WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), sfile->smoothscroll_timer);
sfile->smoothscroll_timer = NULL;
file_params_smoothscroll_timer_clear(wm, win, sfile);
/* Post-scroll (after rename has been validated by user) is done,
* rename process is totally finished, cleanup. */
if ((params->rename_flag & FILE_PARAMS_RENAME_POSTSCROLL_ACTIVE) != 0) {
params->renamefile[0] = '\0';
params->rename_flag = 0;
file_params_renamefile_clear(params);
}
return OPERATOR_FINISHED;
}
@ -2346,18 +2345,16 @@ static int file_directory_new_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
eFileSel_Params_RenameFlag rename_flag = params->rename_flag;
/* If we don't enter the directory directly, remember file to jump into editing. */
if (do_diropen == false) {
BLI_strncpy(params->renamefile, name, FILE_MAXFILE);
params->rename_flag = FILE_PARAMS_RENAME_PENDING;
rename_flag = FILE_PARAMS_RENAME_PENDING;
}
/* Set timer to smoothly view newly generated file. */
if (sfile->smoothscroll_timer != NULL) {
WM_event_remove_timer(wm, 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);
params->rename_flag = rename_flag;
/* reload dir to make sure we're seeing what's in the directory */
ED_fileselect_clear(wm, sfile);

View File

@ -1227,6 +1227,37 @@ void ED_fileselect_exit(wmWindowManager *wm, SpaceFile *sfile)
}
}
void file_params_smoothscroll_timer_clear(wmWindowManager *wm, wmWindow *win, SpaceFile *sfile)
{
WM_event_remove_timer(wm, win, sfile->smoothscroll_timer);
sfile->smoothscroll_timer = NULL;
}
/**
* Set the renaming-state to #FILE_PARAMS_RENAME_POSTSCROLL_PENDING and trigger the smooth-scroll
* timer. To be used right after a file was renamed.
* Note that the caller is responsible for setting the correct rename-file info
* (#FileSelectParams.renamefile or #FileSelectParams.renamefile_uuid).
*/
void file_params_invoke_rename_postscroll(wmWindowManager *wm, wmWindow *win, SpaceFile *sfile)
{
FileSelectParams *params = ED_fileselect_get_active_params(sfile);
params->rename_flag = FILE_PARAMS_RENAME_POSTSCROLL_PENDING;
if (sfile->smoothscroll_timer != NULL) {
file_params_smoothscroll_timer_clear(wm, win, sfile);
}
sfile->smoothscroll_timer = WM_event_add_timer(wm, win, TIMER1, 1.0 / 1000.0);
sfile->scroll_offset = 0;
}
void file_params_renamefile_clear(FileSelectParams *params)
{
params->renamefile[0] = '\0';
params->rename_flag = 0;
}
/**
* Helper used by both main update code, and smooth-scroll timer,
* to try to enable rename editing from #FileSelectParams.renamefile name.
@ -1260,8 +1291,7 @@ void file_params_renamefile_activate(SpaceFile *sfile, FileSelectParams *params)
/* File listing is now async, only reset renaming if matching entry is not found
* when file listing is not done. */
else if (filelist_is_ready(sfile->files)) {
params->renamefile[0] = '\0';
params->rename_flag = 0;
file_params_renamefile_clear(params);
}
}