From d571c734cc3c9b972e0e3bcc1fbc77cb640355cd Mon Sep 17 00:00:00 2001 From: Austin Condiff Date: Thu, 7 Mar 2024 12:50:44 -0600 Subject: [PATCH] Invalidating flattenedEditors cache when editors change. --- .../Editor/Models/EditorManager.swift | 3 ++- .../PathBar/Views/EditorPathBarView.swift | 2 +- .../EditorTabBarTrailingAccessories.swift | 1 + .../Features/Editor/Views/EditorView.swift | 24 +++++++++---------- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/CodeEdit/Features/Editor/Models/EditorManager.swift b/CodeEdit/Features/Editor/Models/EditorManager.swift index e0b501a0b..063e30a70 100644 --- a/CodeEdit/Features/Editor/Models/EditorManager.swift +++ b/CodeEdit/Features/Editor/Models/EditorManager.swift @@ -37,7 +37,7 @@ class EditorManager: ObservableObject { var cancellable: AnyCancellable? // This caching mechanism is a temporary solution and is not optimized - var updateCachedFlattenedEditors: Bool = true + @Published var updateCachedFlattenedEditors: Bool = true var cachedFlettenedEditors: [Editor] = [] var flattenedEditors: [Editor] { if updateCachedFlattenedEditors { @@ -120,6 +120,7 @@ class EditorManager: ObservableObject { flatten() objectWillChange.send() + updateCachedFlattenedEditors = true } /// Set a new active editor. diff --git a/CodeEdit/Features/Editor/PathBar/Views/EditorPathBarView.swift b/CodeEdit/Features/Editor/PathBar/Views/EditorPathBarView.swift index 32a5ed72d..3b488d4cc 100644 --- a/CodeEdit/Features/Editor/PathBar/Views/EditorPathBarView.swift +++ b/CodeEdit/Features/Editor/PathBar/Views/EditorPathBarView.swift @@ -67,7 +67,7 @@ struct EditorPathBarView: View { } } } - .padding(.horizontal, shouldShowTabBar ? file == nil ? 10 : 4 : 0) + .padding(.horizontal, shouldShowTabBar ? (file == nil ? 10 : 4) : 0) .safeAreaInset(edge: .leading, spacing: 0) { if !shouldShowTabBar { EditorTabBarLeadingAccessories() diff --git a/CodeEdit/Features/Editor/TabBar/Views/EditorTabBarTrailingAccessories.swift b/CodeEdit/Features/Editor/TabBar/Views/EditorTabBarTrailingAccessories.swift index 7b8b68507..eb93488a1 100644 --- a/CodeEdit/Features/Editor/TabBar/Views/EditorTabBarTrailingAccessories.swift +++ b/CodeEdit/Features/Editor/TabBar/Views/EditorTabBarTrailingAccessories.swift @@ -74,6 +74,7 @@ struct EditorTabBarTrailingAccessories: View { newEditor = .init() } splitEditor(edge, newEditor) + editorManager.updateCachedFlattenedEditors = true editorManager.activeEditor = newEditor } } diff --git a/CodeEdit/Features/Editor/Views/EditorView.swift b/CodeEdit/Features/Editor/Views/EditorView.swift index 3942cf73f..6ca74ae35 100644 --- a/CodeEdit/Features/Editor/Views/EditorView.swift +++ b/CodeEdit/Features/Editor/Views/EditorView.swift @@ -23,21 +23,21 @@ struct EditorView: View { @EnvironmentObject private var editorManager: EditorManager - var shouldShowTabBar: Bool { - return navigationStyle == .openInTabs - || editorManager.flattenedEditors.contains { editor in - (editor.temporaryTab == nil && !editor.tabs.isEmpty) - || (editor.temporaryTab != nil && editor.tabs.count > 1) + var body: some View { + var shouldShowTabBar: Bool { + return navigationStyle == .openInTabs + || editorManager.flattenedEditors.contains { editor in + (editor.temporaryTab == nil && !editor.tabs.isEmpty) + || (editor.temporaryTab != nil && editor.tabs.count > 1) + } } - } - var editorInsetAmount: Double { - let tabBarHeight = shouldShowTabBar ? (EditorTabBarView.height + 1) : 0 - let pathBarHeight = showEditorPathBar ? (EditorPathBarView.height + 1) : 0 - return tabBarHeight + pathBarHeight - } + var editorInsetAmount: Double { + let tabBarHeight = shouldShowTabBar ? (EditorTabBarView.height + 1) : 0 + let pathBarHeight = showEditorPathBar ? (EditorPathBarView.height + 1) : 0 + return tabBarHeight + pathBarHeight + } - var body: some View { VStack { if let selected = editor.selectedTab { WorkspaceCodeFileView(