From a2baf50242a315961fc7bc4d202a610442e813ec Mon Sep 17 00:00:00 2001 From: Julian Eisel Date: Tue, 26 May 2020 20:32:21 +0200 Subject: Cleanup/refactor: Workspace API, boilerplate code, early exit * Simplify workspace API a bit * Comment on behavior of workspace-layout relations where exposed in API * Remove annoying getters/setters * Avoid lookups if we can early exit * A NULL check is removed in `direct_link_workspace()` that I don't see a need for. Am not 100% sure though, fingers crossed. In general these changes should improve readability and make things easier to reason about. --- source/blender/blenkernel/intern/workspace.c | 78 +++++++++++++++------------- 1 file changed, 43 insertions(+), 35 deletions(-) (limited to 'source/blender/blenkernel/intern/workspace.c') diff --git a/source/blender/blenkernel/intern/workspace.c b/source/blender/blenkernel/intern/workspace.c index 3a69b95c114..4625fd76293 100644 --- a/source/blender/blenkernel/intern/workspace.c +++ b/source/blender/blenkernel/intern/workspace.c @@ -69,17 +69,9 @@ static void workspace_free_data(ID *id) static void workspace_foreach_id(ID *id, LibraryForeachIDData *data) { WorkSpace *workspace = (WorkSpace *)id; - ListBase *layouts = BKE_workspace_layouts_get(workspace); - LISTBASE_FOREACH (WorkSpaceLayout *, layout, layouts) { - bScreen *screen = BKE_workspace_layout_screen_get(layout); - - /* CALLBACK_INVOKE expects an actual pointer, not a variable holding the pointer. - * However we can't access layout->screen here - * since we are outside the workspace project. */ - BKE_LIB_FOREACHID_PROCESS(data, screen, IDWALK_CB_USER); - /* allow callback to set a different screen */ - BKE_workspace_layout_screen_set(layout, screen); + LISTBASE_FOREACH (WorkSpaceLayout *, layout, &workspace->layouts) { + BKE_LIB_FOREACHID_PROCESS(data, layout->screen, IDWALK_CB_USER); } } @@ -228,7 +220,7 @@ WorkSpaceInstanceHook *BKE_workspace_instance_hook_create(const Main *bmain) /* set an active screen-layout for each possible window/workspace combination */ for (WorkSpace *workspace = bmain->workspaces.first; workspace; workspace = workspace->id.next) { - BKE_workspace_hook_layout_for_workspace_set(hook, workspace, workspace->layouts.first); + BKE_workspace_active_layout_set(hook, workspace, workspace->layouts.first); } return hook; @@ -433,6 +425,10 @@ WorkSpace *BKE_workspace_active_get(WorkSpaceInstanceHook *hook) } void BKE_workspace_active_set(WorkSpaceInstanceHook *hook, WorkSpace *workspace) { + if (hook->active == workspace) { + return; + } + hook->active = workspace; if (workspace) { WorkSpaceLayout *layout = workspace_relation_get_data_matching_parent( @@ -443,13 +439,47 @@ void BKE_workspace_active_set(WorkSpaceInstanceHook *hook, WorkSpace *workspace) } } +/** + * Get the layout that is active for \a hook (which is the visible layout for the active workspace + * in \a hook). + */ WorkSpaceLayout *BKE_workspace_active_layout_get(const WorkSpaceInstanceHook *hook) { return hook->act_layout; } -void BKE_workspace_active_layout_set(WorkSpaceInstanceHook *hook, WorkSpaceLayout *layout) + +/** + * Get the layout to be activated should \a workspace become or be the active workspace in \a hook. + */ +WorkSpaceLayout *BKE_workspace_active_layout_for_workspace_get(const WorkSpaceInstanceHook *hook, + const WorkSpace *workspace) +{ + /* If the workspace is active, the active layout can be returned, no need for a lookup. */ + if (hook->active == workspace) { + return hook->act_layout; + } + + /* Inactive workspace */ + return workspace_relation_get_data_matching_parent(&workspace->hook_layout_relations, hook); +} + +/** + * \brief Activate a layout + * + * Sets \a layout as active for \a workspace when activated through or already active in \a hook. + * So when the active workspace of \a hook is \a workspace, \a layout becomes the active layout of + * \a hook too. See #BKE_workspace_active_set(). + * + * \a workspace does not need to be active for this. + * + * WorkSpaceInstanceHook.act_layout should only be modified directly to update the layout pointer. + */ +void BKE_workspace_active_layout_set(WorkSpaceInstanceHook *hook, + WorkSpace *workspace, + WorkSpaceLayout *layout) { hook->act_layout = layout; + workspace_relation_ensure_updated(&workspace->hook_layout_relations, hook, layout); } bScreen *BKE_workspace_active_screen_get(const WorkSpaceInstanceHook *hook) @@ -462,12 +492,7 @@ void BKE_workspace_active_screen_set(WorkSpaceInstanceHook *hook, { /* we need to find the WorkspaceLayout that wraps this screen */ WorkSpaceLayout *layout = BKE_workspace_layout_find(hook->active, screen); - BKE_workspace_hook_layout_for_workspace_set(hook, workspace, layout); -} - -ListBase *BKE_workspace_layouts_get(WorkSpace *workspace) -{ - return &workspace->layouts; + BKE_workspace_active_layout_set(hook, workspace, layout); } const char *BKE_workspace_layout_name_get(const WorkSpaceLayout *layout) @@ -485,22 +510,5 @@ bScreen *BKE_workspace_layout_screen_get(const WorkSpaceLayout *layout) { return layout->screen; } -void BKE_workspace_layout_screen_set(WorkSpaceLayout *layout, bScreen *screen) -{ - layout->screen = screen; -} - -WorkSpaceLayout *BKE_workspace_hook_layout_for_workspace_get(const WorkSpaceInstanceHook *hook, - const WorkSpace *workspace) -{ - return workspace_relation_get_data_matching_parent(&workspace->hook_layout_relations, hook); -} -void BKE_workspace_hook_layout_for_workspace_set(WorkSpaceInstanceHook *hook, - WorkSpace *workspace, - WorkSpaceLayout *layout) -{ - hook->act_layout = layout; - workspace_relation_ensure_updated(&workspace->hook_layout_relations, hook, layout); -} /** \} */ -- cgit v1.2.3