[Bf-blender-cvs] [e9e28055730] master: Cleanup: Deduplicate file closing dialog logic

Julian Eisel noreply at git.blender.org
Wed Apr 21 15:18:12 CEST 2021


Commit: e9e280557302c461bee220be1871692c8094bcd6
Author: Julian Eisel
Date:   Wed Apr 21 15:04:44 2021 +0200
Branches: master
https://developer.blender.org/rBe9e280557302c461bee220be1871692c8094bcd6

Cleanup: Deduplicate file closing dialog logic

Was doing almost the same thing in two places, plus I need the same for
a third case. So better have a helper function for it.

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

M	source/blender/windowmanager/intern/wm_files.c
M	source/blender/windowmanager/wm_files.h

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

diff --git a/source/blender/windowmanager/intern/wm_files.c b/source/blender/windowmanager/intern/wm_files.c
index bbcb0669cce..c1fdb005232 100644
--- a/source/blender/windowmanager/intern/wm_files.c
+++ b/source/blender/windowmanager/intern/wm_files.c
@@ -2157,21 +2157,9 @@ static void wm_homefile_read_after_dialog_callback(bContext *C, void *user_data)
       C, "WM_OT_read_homefile", WM_OP_EXEC_DEFAULT, (IDProperty *)user_data);
 }
 
-static void wm_free_operator_properties_callback(void *user_data)
-{
-  IDProperty *properties = (IDProperty *)user_data;
-  IDP_FreeProperty(properties);
-}
-
 static int wm_homefile_read_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
 {
-  if (U.uiflag & USER_SAVE_PROMPT &&
-      wm_file_or_image_is_modified(CTX_data_main(C), CTX_wm_manager(C))) {
-    wmGenericCallback *callback = MEM_callocN(sizeof(*callback), __func__);
-    callback->exec = wm_homefile_read_after_dialog_callback;
-    callback->user_data = IDP_CopyProperty(op->properties);
-    callback->free_user_data = wm_free_operator_properties_callback;
-    wm_close_file_dialog(C, callback);
+  if (wm_operator_close_file_dialog_if_needed(C, op, wm_homefile_read_after_dialog_callback)) {
     return OPERATOR_INTERFACE;
   }
   return wm_homefile_read_exec(C, op);
@@ -2331,13 +2319,7 @@ static int wm_open_mainfile__discard_changes(bContext *C, wmOperator *op)
     set_next_operator_state(op, OPEN_MAINFILE_STATE_OPEN);
   }
 
-  if (U.uiflag & USER_SAVE_PROMPT &&
-      wm_file_or_image_is_modified(CTX_data_main(C), CTX_wm_manager(C))) {
-    wmGenericCallback *callback = MEM_callocN(sizeof(*callback), __func__);
-    callback->exec = wm_open_mainfile_after_dialog_callback;
-    callback->user_data = IDP_CopyProperty(op->properties);
-    callback->free_user_data = wm_free_operator_properties_callback;
-    wm_close_file_dialog(C, callback);
+  if (wm_operator_close_file_dialog_if_needed(C, op, wm_open_mainfile_after_dialog_callback)) {
     return OPERATOR_INTERFACE;
   }
   return wm_open_mainfile_dispatch(C, op);
@@ -3456,4 +3438,31 @@ void wm_close_file_dialog(bContext *C, wmGenericCallback *post_action)
   }
 }
 
+static void wm_free_operator_properties_callback(void *user_data)
+{
+  IDProperty *properties = (IDProperty *)user_data;
+  IDP_FreeProperty(properties);
+}
+
+/**
+ * \return True if the dialog was created, the calling operator should return #OPERATOR_INTERFACE
+ *         then.
+ */
+bool wm_operator_close_file_dialog_if_needed(bContext *C,
+                                             wmOperator *op,
+                                             wmGenericCallbackFn post_action_fn)
+{
+  if (U.uiflag & USER_SAVE_PROMPT &&
+      wm_file_or_image_is_modified(CTX_data_main(C), CTX_wm_manager(C))) {
+    wmGenericCallback *callback = MEM_callocN(sizeof(*callback), __func__);
+    callback->exec = post_action_fn;
+    callback->user_data = IDP_CopyProperty(op->properties);
+    callback->free_user_data = wm_free_operator_properties_callback;
+    wm_close_file_dialog(C, callback);
+    return true;
+  }
+
+  return false;
+}
+
 /** \} */
diff --git a/source/blender/windowmanager/wm_files.h b/source/blender/windowmanager/wm_files.h
index d54090a6025..533084e96e8 100644
--- a/source/blender/windowmanager/wm_files.h
+++ b/source/blender/windowmanager/wm_files.h
@@ -45,6 +45,7 @@ void wm_homefile_read(struct bContext *C,
 void wm_file_read_report(bContext *C, struct Main *bmain);
 
 void wm_close_file_dialog(bContext *C, struct wmGenericCallback *post_action);
+bool wm_operator_close_file_dialog_if_needed(bContext *C, wmOperator *op, wmGenericCallbackFn exec_fn);
 bool wm_file_or_image_is_modified(const Main *bmain, const wmWindowManager *wm);
 
 void WM_OT_save_homefile(struct wmOperatorType *ot);



More information about the Bf-blender-cvs mailing list