Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion App/Settings/DashboardPane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import SwiftUI

struct DashboardPane: View {
@Bindable var preferences: Preferences
@State private var draggedSection: DashboardSection?

var body: some View {
Form {
Expand Down Expand Up @@ -35,11 +36,11 @@ struct DashboardPane: View {
)
}
.toggleStyle(.switch)
.reorderable(section, in: order, dragging: $draggedSection, move: preferences.moveDashboardSections)
.contextMenu {
MoveCommands(index: index, count: order.count, move: preferences.moveDashboardSections)
}
}
.onMove(perform: preferences.moveDashboardSections)
} header: {
Text("Sections")
} footer: {
Expand Down
8 changes: 4 additions & 4 deletions App/Settings/MenuBarPane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SwiftUI
struct MenuBarPane: View {
@Bindable var preferences: Preferences
let statusItems: StatusItemController
@State private var draggedModule: MenuBarModule?

var body: some View {
Form {
Expand Down Expand Up @@ -52,7 +53,9 @@ struct MenuBarPane: View {
.toggleStyle(.switch)
// The last module that's always shown stays so there's always an item to click.
.disabled(!selection.isAvailable(module) || !selection.canHide(module))
.moveDisabled(!isMovable)
.reorderable(module, in: selection.order, dragging: $draggedModule, isEnabled: isMovable) { source, destination in
preferences.menuBarModules.move(fromOffsets: source, toOffset: destination)
}
.contextMenu {
if isMovable {
MoveCommands(index: index, count: selection.order.count) { source, destination in
Expand All @@ -61,9 +64,6 @@ struct MenuBarPane: View {
}
}
}
.onMove { source, destination in
preferences.menuBarModules.move(fromOffsets: source, toOffset: destination)
}
} header: {
Text("Modules")
} footer: {
Expand Down
66 changes: 66 additions & 0 deletions App/Settings/SettingsComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,72 @@ struct MoveCommands: View {
}
}

extension View {
/// Drag-to-reorder for a row in a `Form`, where `onMove` does nothing on macOS.
/// Rows move live as the dragged one passes over them.
func reorderable<Item: Hashable>(
_ item: Item,
in order: [Item],
dragging: Binding<Item?>,
isEnabled: Bool = true,
move: @escaping (IndexSet, Int) -> Void
) -> some View {
modifier(Reorderable(item: item, order: order, dragging: dragging, isEnabled: isEnabled, move: move))
}
}

private struct Reorderable<Item: Hashable>: ViewModifier {
let item: Item
let order: [Item]
@Binding var dragging: Item?
let isEnabled: Bool
let move: (IndexSet, Int) -> Void

func body(content: Content) -> some View {
if isEnabled {
content
.onDrag {
dragging = item
return NSItemProvider(object: String(describing: item) as NSString)
}
.onDrop(of: [.text], delegate: ReorderDropDelegate(item: item, order: order, dragging: $dragging, move: move))
} else {
content
}
}
}

private struct ReorderDropDelegate<Item: Hashable>: DropDelegate {
let item: Item
let order: [Item]
@Binding var dragging: Item?
let move: (IndexSet, Int) -> Void

func validateDrop(info: DropInfo) -> Bool {
dragging != nil
}

func dropEntered(info: DropInfo) {
guard let dragging, dragging != item,
let source = order.firstIndex(of: dragging),
let target = order.firstIndex(of: item)
else { return }
// `onMove` offsets: landing below a row means inserting after it.
withAnimation {
move([source], target > source ? target + 1 : target)
}
}

func dropUpdated(info: DropInfo) -> DropProposal? {
DropProposal(operation: .move)
}

func performDrop(info: DropInfo) -> Bool {
dragging = nil
return true
}
}

extension MenuBarModule {
var symbol: String {
switch self {
Expand Down
Loading