I'm trying to Build a Mac-App using SwiftUI and I want to replicate the double sidebar feel of XCode, so collapsable sidebar on the left and collapsable inspector on the right.
I achieved this following the answer to this question.
But my ToolBarItem
is behaving weird in some cases, where it starts jumping to other parts of the NavigationView
especially when the right sidebar is open, while collapsing and opening the left one.
The toolbar and window styles are set on app level:
WindowGroup {
ContentView()
}
.windowToolbarStyle(UnifiedWindowToolbarStyle(showsTitle: false))
.windowStyle(HiddenTitleBarWindowStyle())
and the toolbars are set using:
struct LeftToolBarItems: ToolbarContent {
@ObservedObject var mbvm = MenuBarViewModel.shared
var body: some ToolbarContent {
ToolbarItemGroup(placement: .primaryAction) {
Button {
mbvm.leftExtended.toggle()
} label: {
Image(systemName: "sidebar.leading")
.font(.system(size: 17))
}
}
}
}
I'm using a singleton ObservableObject to keep track of the status of the sidebar panes and in the contentView I'm just switching Views based on that status
if (mbvm.leftExtended == true && mbvm.rightExtended == true){
BothSidebars()
} else if (mbvm.leftExtended == true && mbvm.rightExtended == false){
LeftSidebars()
} else if (mbvm.leftExtended == false && mbvm.rightExtended == true){
RightSidebars()
} else {
NoSidebars()
}
And those Views are just different alignments of the same underlying views:
struct NoSidebars: View {
@ObservedObject var mbvm = MenuBarViewModel.shared
var body: some View {
GeometryReader{ window in
NavigationView{
PreView()
.frame(width: window.size.width)
.toolbar {
LeftToolBarItems()
RightToolBarItems()
}
}
}
}
}
struct LeftSidebars: View {
@ObservedObject var mbvm = MenuBarViewModel.shared
var body: some View {
GeometryReader{ window in
NavigationView{
ButtonToolbarView()
.toolbar {
LeftToolBarItems()
}
PreView()
.frame(width: window.size.width-100)
.toolbar {
RightToolBarItems()
}
}
}
}
}
TLDR: How do I prevent my ToolbarItems
from switching the panes (the not expected way)?
full code, ready to compile after adding windowStyle
EDIT: updated Pastebin link, weird formatting