-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix Navigator Key Navigation Explosion #1803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thecoolwinter
merged 1 commit into
CodeEditApp:main
from
thecoolwinter:fix/history-explosion
Jul 14, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.