-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Navigator Key Navigation Explosion (#1803)
- Loading branch information
1 parent
f3524cc
commit 451d4c2
Showing
8 changed files
with
189 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// Editor+History.swift | ||
// CodeEdit | ||
// | ||
// Created by Khan Winter on 7/9/24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Methods for modifying the history list on the editor. | ||
extension Editor { | ||
/// Add the tab to the history list. | ||
/// - Parameter tab: The tab to add to the history. | ||
func addToHistory(_ tab: Tab) { | ||
if history.first != tab { | ||
history.prepend(tab) | ||
} | ||
} | ||
|
||
/// Clear any tabs in the "future" on the history list. Resets the history offset and removes any tabs that were | ||
/// available to navigate forwards to. | ||
func clearFuture() { | ||
guard historyOffset > 0 else { return } // nothing to clear, avoid an out of bounds error | ||
history.removeFirst(historyOffset) | ||
historyOffset = 0 | ||
} | ||
|
||
/// Move backwards in the history list by one place. | ||
func goBackInHistory() { | ||
if canGoBackInHistory { | ||
historyOffset += 1 | ||
} | ||
} | ||
|
||
/// Move forwards in the history list by one place. | ||
func goForwardInHistory() { | ||
if canGoForwardInHistory { | ||
historyOffset -= 1 | ||
} | ||
} | ||
|
||
// TODO: move to @Observable so this works better | ||
/// Warning: NOT published! | ||
var canGoBackInHistory: Bool { | ||
historyOffset != history.count - 1 && !history.isEmpty | ||
} | ||
|
||
// TODO: move to @Observable so this works better | ||
/// Warning: NOT published! | ||
var canGoForwardInHistory: Bool { | ||
historyOffset != 0 | ||
} | ||
|
||
/// Called by the ``Editor`` class when the history offset is changed. | ||
/// | ||
/// This method updates the selected tab to the current tab in the history offset. | ||
/// If the tab is not opened, it is opened without modifying the history list. | ||
/// - Warning: Do not use except in the ``historyOffset``'s `didSet`. | ||
func historyOffsetDidChange() { | ||
let tab = history[historyOffset] | ||
|
||
if !tabs.contains(tab) { | ||
if let temporaryTab, tabs.contains(temporaryTab) { | ||
closeTab(file: temporaryTab.file, fromHistory: true) | ||
} | ||
temporaryTab = tab | ||
openTab(file: tab.file, fromHistory: true) | ||
} | ||
selectedTab = tab | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
CodeEdit/Features/Editor/TabBar/Views/EditorHistoryMenus.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// EditorHistoryMenus.swift | ||
// CodeEdit | ||
// | ||
// Created by Khan Winter on 7/8/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct EditorHistoryMenus: View { | ||
@EnvironmentObject private var editorManager: EditorManager | ||
@EnvironmentObject private var editor: Editor | ||
|
||
var body: some View { | ||
Group { | ||
Menu { | ||
ForEach( | ||
Array(editor.history.dropFirst(editor.historyOffset+1).enumerated()), | ||
id: \.offset | ||
) { index, tab in | ||
Button { | ||
editorManager.activeEditor = editor | ||
editor.historyOffset += index + 1 | ||
} label: { | ||
HStack { | ||
tab.file.icon | ||
Text(tab.file.name) | ||
} | ||
} | ||
} | ||
} label: { | ||
Image(systemName: "chevron.left") | ||
.opacity(editor.historyOffset == editor.history.count - 1 || editor.history.isEmpty ? 0.5 : 1) | ||
.frame(height: EditorTabBarView.height - 2) | ||
.padding(.horizontal, 4) | ||
} primaryAction: { | ||
editorManager.activeEditor = editor | ||
editor.goBackInHistory() | ||
} | ||
.disabled(editor.historyOffset == editor.history.count - 1 || editor.history.isEmpty) | ||
.help("Navigate back") | ||
|
||
Menu { | ||
ForEach( | ||
Array(editor.history.prefix(editor.historyOffset).reversed().enumerated()), | ||
id: \.offset | ||
) { index, tab in | ||
Button { | ||
editorManager.activeEditor = editor | ||
editor.historyOffset -= index + 1 | ||
} label: { | ||
HStack { | ||
tab.file.icon | ||
Text(tab.file.name) | ||
} | ||
} | ||
} | ||
} label: { | ||
Image(systemName: "chevron.right") | ||
.opacity(editor.historyOffset == 0 ? 0.5 : 1) | ||
.frame(height: EditorTabBarView.height - 2) | ||
.padding(.horizontal, 4) | ||
} primaryAction: { | ||
editorManager.activeEditor = editor | ||
editor.goForwardInHistory() | ||
} | ||
.disabled(editor.historyOffset == 0) | ||
.help("Navigate forward") | ||
} | ||
.buttonStyle(.icon) | ||
.controlSize(.small) | ||
.font(EditorTabBarAccessoryIcon.iconFont) | ||
} | ||
} | ||
|
||
#Preview { | ||
EditorHistoryMenus() | ||
} |
Oops, something went wrong.