Fix menu in connections & Fix sidebar status

This commit is contained in:
世界
2026-01-06 01:06:51 +08:00
parent 995b6742c9
commit 9d5e4a4e30
3 changed files with 177 additions and 41 deletions
+96 -32
View File
@@ -2,49 +2,28 @@ import ApplicationLibrary
import Library
import SwiftUI
public struct SidebarView: View {
private struct SidebarContentView: View {
@Binding var selection: NavigationPage
@EnvironmentObject private var environments: ExtensionEnvironments
@State private var localSelection: NavigationPage = .dashboard
@Binding var localSelection: NavigationPage
@ObservedObject var profile: ExtensionProfile
var environments: ExtensionEnvironments
public init(selection: Binding<NavigationPage>) {
_selection = selection
private var hasGroups: Bool {
environments.commandClient.groups?.isEmpty == false
}
public var body: some View {
if ApplicationLibrary.inPreview {
sidebarContent(isConnected: true, profile: nil)
} else if environments.extensionProfileLoading {
ProgressView()
} else if let profile = environments.extensionProfile {
sidebarContent(isConnected: profile.status.isConnectedStrict, profile: profile)
.onReceive(profile.$status) { _ in }
.onChangeCompat(of: profile.status) {
if !localSelection.visible(profile) {
Task { @MainActor in
localSelection = .dashboard
}
}
}
} else {
sidebarContent(isConnected: false, profile: nil)
}
}
@ViewBuilder
private func sidebarContent(isConnected: Bool, profile: ExtensionProfile?) -> some View {
var body: some View {
List(selection: $localSelection) {
if isConnected {
if profile.status.isConnectedStrict {
Section(NavigationPage.dashboard.title) {
Label("Overview", systemImage: "text.and.command.macwindow")
.tint(.textColor)
.tag(NavigationPage.dashboard)
NavigationPage.groups.label.tag(NavigationPage.groups)
if Variant.isBeta {
NavigationPage.connections.label.tag(NavigationPage.connections)
if hasGroups {
NavigationPage.groups.label.tag(NavigationPage.groups)
}
NavigationPage.connections.label.tag(NavigationPage.connections)
}
Divider()
ForEach(NavigationPage.macosDefaultPages, id: \.self) { it in
it.label
}
@@ -71,5 +50,90 @@ public struct SidebarView: View {
}
}
}
.onChangeCompat(of: profile.status) {
if !localSelection.visible(profile) {
Task { @MainActor in
localSelection = .dashboard
}
}
}
.onReceive(environments.commandClient.$groups) { groups in
if localSelection == .groups, groups?.isEmpty != false {
Task { @MainActor in
localSelection = .dashboard
}
}
}
}
}
public struct SidebarView: View {
@Binding var selection: NavigationPage
@EnvironmentObject private var environments: ExtensionEnvironments
@State private var localSelection: NavigationPage = .dashboard
public init(selection: Binding<NavigationPage>) {
_selection = selection
}
public var body: some View {
if ApplicationLibrary.inPreview {
previewContent
} else if environments.extensionProfileLoading {
ProgressView()
} else if let profile = environments.extensionProfile {
SidebarContentView(
selection: $selection,
localSelection: $localSelection,
profile: profile,
environments: environments
)
} else {
disconnectedContent
}
}
@ViewBuilder
private var previewContent: some View {
List(selection: $localSelection) {
Section(NavigationPage.dashboard.title) {
Label("Overview", systemImage: "text.and.command.macwindow")
.tint(.textColor)
.tag(NavigationPage.dashboard)
NavigationPage.groups.label.tag(NavigationPage.groups)
NavigationPage.connections.label.tag(NavigationPage.connections)
}
ForEach(NavigationPage.macosDefaultPages, id: \.self) { it in
it.label
}
}
.listStyle(.sidebar)
.scrollDisabled(true)
}
@ViewBuilder
private var disconnectedContent: some View {
List(selection: $localSelection) {
ForEach(NavigationPage.allCases.filter { $0.visible(nil) }, id: \.self) { it in
it.label
}
}
.listStyle(.sidebar)
.scrollDisabled(true)
.onAppear {
localSelection = selection
}
.onChangeCompat(of: selection) { newValue in
if localSelection != newValue {
localSelection = newValue
}
}
.onChangeCompat(of: localSelection) { newValue in
if selection != newValue {
Task { @MainActor in
selection = newValue
}
}
}
}
}