Make first and single item non-collapsible

This commit is contained in:
Balazs Perlaki-Horvath 2025-04-20 11:21:02 +02:00 committed by Kelson
parent 25bba5a23a
commit 1164425624
3 changed files with 14 additions and 6 deletions

View File

@ -86,20 +86,28 @@ struct Language: Identifiable, Comparable {
}
}
class OutlineItem: ObservableObject, Identifiable {
final class OutlineItem: ObservableObject, Identifiable {
let id: String
let index: Int
let text: String
let level: Int
let isCollapsible: Bool
private(set) var children: [OutlineItem]?
@Published var isExpanded = true
func asNonCollapsible() -> OutlineItem {
let copy = OutlineItem(id: id, index: index, text: text, level: level, isCollapsible: false)
copy.children = children
return copy
}
init(id: String, index: Int, text: String, level: Int) {
init(id: String, index: Int, text: String, level: Int, isCollapsible: Bool = true) {
self.id = id
self.index = index
self.text = text
self.level = level
self.isCollapsible = isCollapsible
}
convenience init(index: Int, text: String, level: Int) {

View File

@ -760,10 +760,9 @@ final class BrowserViewModel: NSObject, ObservableObject,
}
}
// if there is only one h1, flatten one level
// if there is only one h1, make the first item non collapsible
if let rootChildren = root.children, rootChildren.count == 1, let rootFirstChild = rootChildren.first {
let children = rootFirstChild.removeAllChildren()
self.outlineItemTree = [rootFirstChild] + children
self.outlineItemTree = [rootFirstChild.asNonCollapsible()]
} else {
self.outlineItemTree = root.children ?? []
}

View File

@ -90,7 +90,8 @@ struct OutlineButton: View {
var body: some View {
if let children = item.children {
DisclosureGroup(isExpanded: $item.isExpanded) {
let isExpanded = item.isCollapsible ? $item.isExpanded : .constant(true)
DisclosureGroup(isExpanded: isExpanded) {
ForEach(children) { child in
OutlineNode(item: child, action: action)
}