Scroll to selection on selecting relationship

This commit is contained in:
Graeme Arthur
2026-05-15 22:47:25 +10:00
parent b5abc3fc93
commit 20a0e34811
@@ -43,41 +43,54 @@ struct InstalledPackagesView: View {
}
private func installedList(_ content: InstalledPackagesContent) -> some View {
List {
if content.shouldShowFormulaeSection {
Section("Formulae") {
ForEach(content.formulaPackages) { package in
listRow(for: package)
.contentShape(Rectangle())
.onTapGesture {
viewModel.setSelection(package.id)
}
.listRowBackground(
viewModel.activeSelectedPackageID == package.id ? Color.brewBrandTint : Color.clear,
)
ScrollViewReader { proxy in
List {
if content.shouldShowFormulaeSection {
Section("Formulae") {
ForEach(content.formulaPackages) { package in
listRow(for: package)
.id(package.id)
.contentShape(Rectangle())
.onTapGesture {
viewModel.setSelection(package.id)
}
.listRowBackground(
viewModel.activeSelectedPackageID == package.id ? Color.brewBrandTint : Color.clear,
)
}
}
}
}
if content.shouldShowCasksSection {
Section("Casks") {
ForEach(content.caskPackages) { package in
listRow(for: package)
.contentShape(Rectangle())
.onTapGesture {
viewModel.setSelection(package.id)
}
.listRowBackground(
viewModel.activeSelectedPackageID == package.id ? Color.brewBrandTint : Color.clear,
)
if content.shouldShowCasksSection {
Section("Casks") {
ForEach(content.caskPackages) { package in
listRow(for: package)
.id(package.id)
.contentShape(Rectangle())
.onTapGesture {
viewModel.setSelection(package.id)
}
.listRowBackground(
viewModel.activeSelectedPackageID == package.id ? Color.brewBrandTint : Color.clear,
)
}
}
}
}
}
.listStyle(.plain)
.accessibilityLabel("Installed packages")
.onExitCommand {
viewModel.clearSelection()
.listStyle(.plain)
.accessibilityLabel("Installed packages")
.onAppear {
scrollToSelection(viewModel.activeSelectedPackageID, in: content, with: proxy)
}
.onChange(of: viewModel.activeSelectedPackageID) { _, selectedID in
scrollToSelection(selectedID, in: content, with: proxy)
}
.onChange(of: content.packages.map(\.id)) { _, _ in
scrollToSelection(viewModel.activeSelectedPackageID, in: content, with: proxy)
}
.onExitCommand {
viewModel.clearSelection()
}
}
}
@@ -85,6 +98,19 @@ struct InstalledPackagesView: View {
InstalledListRowRoot(package: package)
}
private func scrollToSelection(
_ selectedID: BrewPackage.ID?,
in content: InstalledPackagesContent,
with proxy: ScrollViewProxy,
) {
guard let selectedID, content.packages.contains(where: { $0.id == selectedID }) else {
return
}
withAnimation(.brewFast) {
proxy.scrollTo(selectedID, anchor: .center)
}
}
private var loadingSkeletonList: some View {
ScrollView {
LazyVStack(alignment: .leading, spacing: 0) {