Skip to content

Commit

Permalink
Fix outline inspector selection
Browse files Browse the repository at this point in the history
  • Loading branch information
1024jp committed Dec 15, 2024
1 parent a733a6c commit 53dd3bf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

- Fix an issue that the shortcut key for cycling through windows was not functioning correctly for certain input methods.
- Fix an issue that the end point of a selection shifted after a hanging indent on a wrapped line when the line endings are CRLF and the selection is expanded using the `⇧↑` or `⇧↓` shortcuts.
- Fix an issue that the insertion point rarely jumped to the nearest outline item when the outline inspector is shown.



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct OutlineInspectorView: View {


var items: [Item] = []
var selection: Item.ID?
var selection: Item.ID? { didSet { self.selectItem(id: selection) } }

var document: Document? { didSet { self.invalidateObservation() } }

Expand Down Expand Up @@ -119,7 +119,8 @@ struct OutlineInspectorView: View {
.foregroundStyle(.secondary)
.accessibilityRemoveTraits(.isHeader)

let items = self.model.items.compactMap { $0.filter(self.filterString, keyPath: \.title) }
let items = self.model.items
.compactMap { $0.filter(self.filterString, keyPath: \.title) }

List(items, selection: $model.selection) { item in
OutlineRowView(item: item, fontSize: self.fontSize)
Expand All @@ -133,9 +134,6 @@ struct OutlineInspectorView: View {
.controlSize(.regular)
}
}
.onChange(of: self.model.selection) { (_, id) in
self.model.selectItem(id: id)
}
.contextMenu {
Menu(String(localized: "Text Size", table: "MainMenu")) {
Button(String(localized: "Bigger", table: "MainMenu"), action: self.biggerFont)
Expand Down Expand Up @@ -211,23 +209,7 @@ private struct OutlineRowView: View {

private extension OutlineInspectorView.Model {

/// Selects correspondence range of the item in the editor.
///
/// - Parameter id: The `id` of the item to select.
func selectItem(id: Item.ID?) {

guard
!self.isOwnSelectionChange,
let item = self.items[id: id],
let textView = self.document?.textView,
textView.string.length >= item.range.upperBound
else { return }

textView.selectedRange = item.range
textView.centerSelectionInVisibleArea(self)
}


/// Updates observations.
func invalidateObservation() {

if let document {
Expand Down Expand Up @@ -262,9 +244,28 @@ private extension OutlineInspectorView.Model {
}


/// Selects correspondence range of the item in the editor.
///
/// - Parameter id: The `id` of the item to select.
private func selectItem(id: Item.ID?) {

guard
!self.isOwnSelectionChange,
let item = self.items[id: id],
let textView = self.document?.textView,
textView.string.length >= item.range.upperBound
else { return }

textView.selectedRange = item.range
textView.centerSelectionInVisibleArea(self)
}


/// Updates row selection to synchronize with the editor's cursor location.
///
/// - Parameter textView: The text view to apply the selection. when nil, the current focused editor will be used (the document can have multiple editors).
/// - Parameters:
/// - textView: The text view to apply the selection. when `nil`,
/// the current focused editor will be used (the document can have multiple editors).
private func invalidateCurrentItem(in textView: NSTextView? = nil) {

guard
Expand All @@ -274,11 +275,7 @@ private extension OutlineInspectorView.Model {

self.isOwnSelectionChange = true
self.selection = item.id
// adjust the timing to restore flag for selectItem(id:)
Task {
await Task.yield()
self.isOwnSelectionChange = false
}
self.isOwnSelectionChange = false
}
}

Expand Down

0 comments on commit 53dd3bf

Please sign in to comment.