Skip to content

armgit / cra

Public Imported from GitHub
0

The open-source Arc replacement for macOS. Spaces, pinned tabs, split view, Little Windows and Arc's shortcuts on Chromium, with your Arc sidebar imported in one go.

Fix sidebar drag and drop outside the pinned list

Victor269a24e8f689
diff --git a/Sources/Cra/Services/SidebarStore.swift b/Sources/Cra/Services/SidebarStore.swift
index cd73bb8..7f3d9ab 100644
--- a/Sources/Cra/Services/SidebarStore.swift
+++ b/Sources/Cra/Services/SidebarStore.swift
@@ -155,6 +155,14 @@ final class SidebarStore {
return out
}
+ /// The top-level pinned rows a space's list draws. Collapsed, only the
+ /// pinned item holding the open tab stays.
+ func listedPinnedItems(in space: Space) -> [SidebarItem] {
+ guard isPinnedCollapsed(space.id) else { return children(of: space.pinnedContainerID) }
+ let selected = selectedTabID(in: space.id).flatMap { splitContaining($0) ?? item($0) }
+ return selected.flatMap { rootContainer(of: $0.id) == .pinned(spaceID: space.id) ? [$0] : nil } ?? []
+ }
+
/// Favorites + pinned + today for a space, in sidebar order. This is what
/// ⌘1…⌘9 index into (Arc: "Go to Sidebar Item N").
func visibleTabs(in spaceID: UUID) -> [SidebarItem] {
diff --git a/Sources/Cra/UI/ShellView.swift b/Sources/Cra/UI/ShellView.swift
index 25250f6..f06c2f7 100644
--- a/Sources/Cra/UI/ShellView.swift
+++ b/Sources/Cra/UI/ShellView.swift
@@ -612,16 +612,10 @@ final class ShellView: NSView {
}
}
-/// Hosts the SwiftUI sidebar. The window is movable by its background, and
-/// AppKit asks the hit view at mouse-down whether that press may move the
-/// window; NSHostingView says yes for the whole sidebar, so a press on a tab
-/// row started a window drag before SwiftUI's `.draggable` could begin. Rows
-/// and tiles flag the pointer while hovered; then the press stays with SwiftUI.
-final class SidebarHostingView: NSHostingView<AnyView> {
- /// Returns true while the pointer is over something draggable (a row, a tile).
- var pointerOverDraggable: () -> Bool = { false }
- override var mouseDownCanMoveWindow: Bool { !pointerOverDraggable() }
-}
+/// Hosts the SwiftUI sidebar. The window is movable by its background; while
+/// the pointer is over a draggable row or tile the window controller turns that
+/// off, so a press starts SwiftUI's `.draggable` instead of a window drag.
+final class SidebarHostingView: NSHostingView<AnyView> {}
/// Thin vertical strip between sidebar and content that drags the width.
final class ResizeHandleView: NSView {
diff --git a/Sources/Cra/UI/Sidebar/FavoritesGrid.swift b/Sources/Cra/UI/Sidebar/FavoritesGrid.swift
index 0067dbd..96d38e8 100644
--- a/Sources/Cra/UI/Sidebar/FavoritesGrid.swift
+++ b/Sources/Cra/UI/Sidebar/FavoritesGrid.swift
@@ -84,7 +84,7 @@ struct FavoriteCell: View {
}
.frame(height: 47)
.contentShape(RoundedRectangle(cornerRadius: 13))
- .onHover { hover = $0; ui.pointerOverDraggable = $0 }
+ .onHover { hover = $0; ui.hoverDraggable(item.id, $0) }
.onTapGesture { ui.keyboardFocusID = nil; store.selectTab(item.id) }
.help(item.displayTitle)
.draggable(SidebarDragItem.begin(item.id, session))
diff --git a/Sources/Cra/UI/Sidebar/SidebarItemView.swift b/Sources/Cra/UI/Sidebar/SidebarItemView.swift
index fa79e73..410f773 100644
--- a/Sources/Cra/UI/Sidebar/SidebarItemView.swift
+++ b/Sources/Cra/UI/Sidebar/SidebarItemView.swift
@@ -135,7 +135,7 @@ struct TabRow: View {
)
.overlay { if ui.keyboardFocusID == item.id { FocusRing(radius: CraMetrics.rowCorner) } }
.contentShape(Rectangle())
- .onHover { hover = $0; ui.pointerOverDraggable = $0 }
+ .onHover { hover = $0; ui.hoverDraggable(item.id, $0) }
.onTapGesture {
let event = NSApp.currentEvent
let flags = event?.modifierFlags.intersection([.command, .shift]) ?? []
@@ -292,7 +292,7 @@ struct FolderRow: View {
.frame(height: CraMetrics.rowHeight)
.background(RoundedRectangle(cornerRadius: CraMetrics.rowCorner, style: .continuous).fill(hover ? palette.hoverFill : .clear))
.contentShape(Rectangle())
- .onHover { hover = $0; ui.pointerOverDraggable = $0 }
+ .onHover { hover = $0; ui.hoverDraggable(item.id, $0) }
// Every click toggles, however fast they come: double-click-to-rename made
// quick open/close clicks start a rename that then swallowed the clicks.
// Rename stays in the context menu. No count: 2 gesture either: it makes
diff --git a/Sources/Cra/UI/Sidebar/SidebarListDrop.swift b/Sources/Cra/UI/Sidebar/SidebarListDrop.swift
index a453f70..e7f1e56 100644
--- a/Sources/Cra/UI/Sidebar/SidebarListDrop.swift
+++ b/Sources/Cra/UI/Sidebar/SidebarListDrop.swift
@@ -33,6 +33,75 @@ struct SidebarListDrop: Equatable {
var rowBelow: UUID?
}
+/// Scrolls a space's list while a drag hovers near its top or bottom edge.
+/// SwiftUI's ScrollView has no autoscroll for custom drops, so with a long
+/// pinned list everything past the fold (Today included) was out of reach.
+@MainActor
+final class DragAutoscroller {
+ /// Edge band, and the speed at the very edge in points per second.
+ static let band: CGFloat = 48
+ static let maxSpeed: CGFloat = 900
+
+ fileprivate weak var anchor: NSView?
+ private var timer: Timer?
+ private var lastTick: CFTimeInterval = 0
+
+ /// Starts or stops scrolling for where the pointer is now.
+ func update() {
+ guard speed() != 0 else { stop(); return }
+ guard timer == nil else { return }
+ lastTick = CACurrentMediaTime()
+ let t = Timer(timeInterval: 1 / 60, repeats: true) { [weak self] _ in
+ MainActor.assumeIsolated { self?.tick() }
+ }
+ // A drag keeps the run loop in event-tracking mode.
+ RunLoop.main.add(t, forMode: .common)
+ timer = t
+ }
+
+ func stop() {
+ timer?.invalidate()
+ timer = nil
+ }
+
+ private func speed() -> CGFloat {
+ guard NSEvent.pressedMouseButtons != 0, let sv = anchor?.enclosingScrollView, let window = sv.window else { return 0 }
+ let p = sv.convert(window.mouseLocationOutsideOfEventStream, from: nil)
+ let fromTop = sv.isFlipped ? p.y : sv.bounds.height - p.y
+ let fromBottom = sv.bounds.height - fromTop
+ guard p.x >= 0, p.x <= sv.bounds.width, fromTop >= 0, fromBottom >= 0 else { return 0 }
+ if fromTop < Self.band { return -Self.maxSpeed * (1 - fromTop / Self.band) }
+ if fromBottom < Self.band { return Self.maxSpeed * (1 - fromBottom / Self.band) }
+ return 0
+ }
+
+ private func tick() {
+ let now = CACurrentMediaTime()
+ let dt = min(now - lastTick, 0.05)
+ lastTick = now
+ let v = speed()
+ guard v != 0, let sv = anchor?.enclosingScrollView else { stop(); return }
+ let clip = sv.contentView
+ var origin = clip.bounds.origin
+ origin.y += v * dt * (sv.documentView?.isFlipped ?? true ? 1 : -1)
+ let r = clip.constrainBoundsRect(NSRect(origin: origin, size: clip.bounds.size))
+ guard r.origin != clip.bounds.origin else { return }
+ clip.scroll(to: r.origin)
+ sv.reflectScrolledClipView(clip)
+ }
+
+ /// Placed inside the list so the scroller can find the scroll view around it.
+ struct Anchor: NSViewRepresentable {
+ let scroller: DragAutoscroller
+ func makeNSView(context: Context) -> NSView { PassThroughView() }
+ func updateNSView(_ view: NSView, context: Context) { scroller.anchor = view }
+ }
+
+ private final class PassThroughView: NSView {
+ override func hitTest(_ point: NSPoint) -> NSView? { nil }
+ }
+}
+
/// One drop delegate for the whole pinned + today list. Per-row drop
/// destinations only report entered/exited, so the list hit-tests the pointer
/// against the rows' frames itself on every move.
@@ -44,6 +113,7 @@ struct SidebarListDropDelegate: DropDelegate {
let space: Space
let store: SidebarStore
let ui: SidebarUIState
+ let autoscroller: DragAutoscroller
func validateDrop(info: DropInfo) -> Bool { info.hasItemsConforming(to: [.craSidebarItem]) }
@@ -65,10 +135,12 @@ struct SidebarListDropDelegate: DropDelegate {
}
func dropExited(info: DropInfo) {
+ autoscroller.stop()
withAnimation(Self.animation) { ui.listDrop = nil }
}
func performDrop(info: DropInfo) -> Bool {
+ autoscroller.stop()
guard let id = ui.draggingID, store.item(id) != nil else { return false }
let drop = ui.listDrop ?? resolve(dragged: id, at: info.location)
withAnimation(Self.animation) {
@@ -84,6 +156,7 @@ struct SidebarListDropDelegate: DropDelegate {
}
private func update(_ info: DropInfo) {
+ autoscroller.update()
guard let id = ui.draggingID, store.item(id) != nil else { return }
let drop = resolve(dragged: id, at: info.location)
guard drop != ui.listDrop else { return }
@@ -132,18 +205,21 @@ struct SidebarListDropDelegate: DropDelegate {
let frame: CGRect
}
- /// Rows as drawn: pinned (expanded folders open), then today.
+ /// Rows as drawn: pinned (expanded folders open), then today. Frames stay
+ /// recorded for rows that are no longer drawn (a collapsed pinned list),
+ /// so only drawn rows may be hit.
private func visibleRows() -> [Row] {
var out: [Row] = []
- func walk(_ parent: UUID, _ depth: Int) {
- for (i, c) in store.children(of: parent).enumerated() {
- guard let f = ui.rowFrames[c.id] else { continue }
+ func walk(_ rows: [SidebarItem], _ depth: Int) {
+ for c in rows {
+ guard let f = ui.rowFrames[c.id], let parent = c.parentID,
+ let i = store.item(parent)?.children.firstIndex(of: c.id) else { continue }
out.append(Row(item: c, parent: parent, index: i, depth: depth, frame: f))
- if c.isFolder, store.isExpanded(c.id) { walk(c.id, depth + 1) }
+ if c.isFolder, store.isExpanded(c.id) { walk(store.children(of: c.id), depth + 1) }
}
}
- walk(space.pinnedContainerID, 0)
- walk(space.todayContainerID, 0)
+ walk(store.listedPinnedItems(in: space), 0)
+ walk(store.children(of: space.todayContainerID), 0)
return out
}
@@ -212,9 +288,9 @@ struct SidebarListDropDelegate: DropDelegate {
guard case .insert(let parent, let index)? = target else { return drop }
let kids = store.item(parent)?.children ?? []
var y: CGFloat?
- if index < kids.count, let f = ui.rowFrames[kids[index]] {
+ if index < kids.count, let f = rows.first(where: { $0.item.id == kids[index] })?.frame {
y = f.minY - half
- } else if let last = kids.last, let r = rows.last(where: { $0.item.id == last || isInside($0.item.id, last) }) {
+ } else if index > 0, let r = rows.last(where: { $0.item.id == kids[index - 1] || isInside($0.item.id, kids[index - 1]) }) {
y = r.frame.maxY + half
} else if store.item(parent)?.isFolder == true, let f = ui.rowFrames[parent] {
y = f.maxY + half
diff --git a/Sources/Cra/UI/Sidebar/SidebarRootView.swift b/Sources/Cra/UI/Sidebar/SidebarRootView.swift
index 9ffdc20..b9e5a9b 100644
--- a/Sources/Cra/UI/Sidebar/SidebarRootView.swift
+++ b/Sources/Cra/UI/Sidebar/SidebarRootView.swift
@@ -274,6 +274,7 @@ struct SpaceSectionView: View {
@State private var hoverDivider = false
@State private var rowsFrame = CGRect.zero
@State private var viewportHeight: CGFloat = 0
+ @State private var autoscroller = DragAutoscroller()
/// Arc 1.170 draws a full-width 1pt hairline (~5% black over the light theme) at an edge of
/// the list only while rows are hidden past it, fading over ~0.25s.
@@ -292,8 +293,9 @@ struct SpaceSectionView: View {
// Arc leaves ~121pt of empty list after the last row at full scroll.
.padding(.bottom, 121)
.overlay(alignment: .topLeading) { dropLine }
+ .background(DragAutoscroller.Anchor(scroller: autoscroller))
.contentShape(Rectangle())
- .onDrop(of: [.craSidebarItem], delegate: SidebarListDropDelegate(space: space, store: store, ui: ui))
+ .onDrop(of: [.craSidebarItem], delegate: SidebarListDropDelegate(space: space, store: store, ui: ui, autoscroller: autoscroller))
// Safety net: a drag that ended without an exit callback leaves no preview behind.
.onContinuousHover { _ in if ui.listDrop != nil, NSEvent.pressedMouseButtons == 0 { ui.listDrop = nil } }
.coordinateSpace(name: SidebarListDropDelegate.coordinateSpace)
@@ -366,19 +368,16 @@ struct SpaceSectionView: View {
private var rows: some View {
let todayItems = store.children(of: space.todayContainerID)
- var pinnedItems = store.children(of: space.pinnedContainerID)
- // Collapsed, only the pinned tab you are on stays in the list.
- if store.isPinnedCollapsed(space.id) {
- let selected = store.selectedTabID(in: space.id).flatMap { store.splitContaining($0) ?? store.item($0) }
- pinnedItems = selected.flatMap { store.rootContainer(of: $0.id) == .pinned(spaceID: space.id) ? [$0] : nil } ?? []
- }
+ let pinnedItems = store.listedPinnedItems(in: space)
return VStack(alignment: .leading, spacing: 0) {
VStack(spacing: CraMetrics.rowSpacing) {
ForEach(pinnedItems) { item in
SidebarItemView(item: item, depth: 0, palette: palette, section: .pinned)
}
// With nothing pinned the list is 8pt tall; a drag opens a real target.
- if pinnedItems.isEmpty && ui.listDrop != nil { pinPlaceholder }
+ // Not when collapsed: the placeholder would claim an empty list and
+ // open under the pointer, taking drops meant for the top of Today.
+ if store.children(of: space.pinnedContainerID).isEmpty && ui.listDrop != nil { pinPlaceholder }
}
.onGeometryChange(for: CGRect.self) { $0.frame(in: .named(SidebarListDropDelegate.coordinateSpace)) } action: { ui.rowFrames[space.pinnedContainerID] = $0 }
.padding(.horizontal, 10)
diff --git a/Sources/Cra/UI/Sidebar/SpaceDock.swift b/Sources/Cra/UI/Sidebar/SpaceDock.swift
index b6ca2f7..e1274a9 100644
--- a/Sources/Cra/UI/Sidebar/SpaceDock.swift
+++ b/Sources/Cra/UI/Sidebar/SpaceDock.swift
@@ -47,7 +47,15 @@ struct SpaceDockButton: View {
guard let d = items.first else { return false }
store.moveTab(d.id, toSpace: space.id)
return true
- } isTargeted: { targeted = $0 }
+ } isTargeted: { t in
+ targeted = t
+ // Arc: holding a dragged item over a space switches to it, so the
+ // item can be carried into that space's list and dropped in place.
+ guard t, !selected else { return }
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
+ if targeted { store.focusSpace(space.id) }
+ }
+ }
.contextMenu {
Button("Edit Space…") { actions.editSpace(space) }
Divider()
diff --git a/Sources/Cra/UI/SidebarUIState.swift b/Sources/Cra/UI/SidebarUIState.swift
index 8b0053d..5bae083 100644
--- a/Sources/Cra/UI/SidebarUIState.swift
+++ b/Sources/Cra/UI/SidebarUIState.swift
@@ -24,10 +24,21 @@ final class SidebarUIState {
/// Tab chosen from the keyboard; Arc draws its yellow/black dashed ring
/// around it until the mouse picks another tab.
var keyboardFocusID: UUID?
- /// The pointer is over a draggable sidebar row or tile. The sidebar's
- /// hosting view then refuses window-background drags so SwiftUI's
- /// `.draggable` gets the press (the window is movable by its background).
- var pointerOverDraggable = false
+ /// The pointer is over a draggable sidebar row or tile. The window is
+ /// movable by its background; the window controller turns that off while
+ /// this is true so a press starts SwiftUI's `.draggable`, not a window drag.
+ var pointerOverDraggable: Bool { draggableUnderPointer != nil }
+ @ObservationIgnored var onPointerOverDraggableChange: (Bool) -> Void = { _ in }
+ /// Moving between rows, the next row's hover can arrive before the last
+ /// one's exit; a shared flag then ended up false over a row and the press
+ /// dragged the window instead of the tab. Each row clears only itself.
+ @ObservationIgnored private var draggableUnderPointer: UUID? {
+ didSet { if (oldValue != nil) != (draggableUnderPointer != nil) { onPointerOverDraggableChange(pointerOverDraggable) } }
+ }
+
+ func hoverDraggable(_ id: UUID, _ inside: Bool) {
+ if inside { draggableUnderPointer = id } else if draggableUnderPointer == id { draggableUnderPointer = nil }
+ }
/// Per-tab navigation state for split panes (each pane has its own toolbar).
var navStates: [UUID: TabNavState] = [:]
/// Panes currently shown in the window (empty when a single page shows).
diff --git a/Sources/Cra/UI/SplitView/SplitRow.swift b/Sources/Cra/UI/SplitView/SplitRow.swift
index 6b3e196..95f4e6e 100644
--- a/Sources/Cra/UI/SplitView/SplitRow.swift
+++ b/Sources/Cra/UI/SplitView/SplitRow.swift
@@ -87,7 +87,7 @@ struct SplitChildCell: View {
.padding(.trailing, 4)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.contentShape(Rectangle())
- .onHover { hover = $0; ui.pointerOverDraggable = $0 }
+ .onHover { hover = $0; ui.hoverDraggable(item.id, $0) }
.onTapGesture { store.selectTab(item.id) }
.draggable(SidebarDragItem.begin(item.id, session)) {
HStack(spacing: 8) {
diff --git a/Sources/Cra/UI/Windows/MainWindowController.swift b/Sources/Cra/UI/Windows/MainWindowController.swift
index 4b4b7f2..9026a56 100644
--- a/Sources/Cra/UI/Windows/MainWindowController.swift
+++ b/Sources/Cra/UI/Windows/MainWindowController.swift
@@ -77,7 +77,10 @@ final class MainWindowController: NSWindowController, NSWindowDelegate, BrowserM
browsers = BrowserManager(store: store, favicons: app.favicons, container: shell.parkingView)
browsers.delegate = self
- shell.sidebarHost.pointerOverDraggable = { [weak self] in self?.sidebarUI.pointerOverDraggable ?? false }
+ // AppKit asks the view under a press whether it may move the window. In the
+ // sidebar's list that is SwiftUI's scroll container, which always says yes,
+ // so the window's own flag is what keeps a row press for the row's drag.
+ sidebarUI.onPointerOverDraggableChange = { [weak window] over in window?.isMovableByWindowBackground = !over }
browsers.paneHost = { [weak self] id in self?.shell.card(for: id)?.webArea }
shell.makeToolbar = { [weak self] id in
guard let self else { return AnyView(EmptyView()) }