Screenshots, hung out to dry. A tiny native macOS app that hangs every screenshot on a line at the top of your screen.
Swift
Pinned photos stay put while the line scrolls
Alejandro Buján13c47b32c92f
diff --git a/Sources/Tendedero/AppDelegate.swift b/Sources/Tendedero/AppDelegate.swiftindex 005c0ef..a0a4be3 100644--- a/Sources/Tendedero/AppDelegate.swift+++ b/Sources/Tendedero/AppDelegate.swift@@ -356,7 +356,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {guard let index = line.items.firstIndex(where: { $0.id == id }) else { return nil }let width = panel.frame.widthlet x = Layout.x(index: index, count: line.items.count, width: width,- visible: line.visibleCount, scroll: line.scroll)+ visible: line.visibleCount, scroll: line.scroll, pinned: line.pinnedLead)let viewTop = Layout.ropeY(x: x, width: width) - Layout.pinAbovelet cardTop = viewTop + PeggedView.cardOffsetBelowToplet size = PeggedView.cardSize(for: line.items[index].thumb.size)diff --git a/Sources/Tendedero/Line.swift b/Sources/Tendedero/Line.swiftindex 408f4c2..0fecef6 100644--- a/Sources/Tendedero/Line.swift+++ b/Sources/Tendedero/Line.swift@@ -78,6 +78,10 @@ final class Line: ObservableObject {/// before the knot, and never fall to make room.var keptCount: Int { items.filter { !$0.falling && $0.kept }.count }+ /// How many photos at the start of the line are pinned, the empty+ /// places of pinned ones that just came down included.+ var pinnedLead: Int { items.prefix { $0.kept }.count }+/// The oldest photo that can fall: the first one past the knot.private var oldestLoose: Pegged? { items.first { !$0.falling && !$0.kept && !$0.held } }diff --git a/Sources/Tendedero/LineView.swift b/Sources/Tendedero/LineView.swiftindex f78d57f..0b0f0e2 100644--- a/Sources/Tendedero/LineView.swift+++ b/Sources/Tendedero/LineView.swift@@ -31,6 +31,18 @@ enum Layout {/// The rope hangs as a parabola from edge to edge of the screen.static func sag(width: CGFloat) -> CGFloat { min(30, width * 0.018) }+ static func anchorsPinned(_ pinned: Int, visible: Int) -> Bool {+ pinned > 0 && pinned < visible - 1+ }++ /// On a line that scrolls with pinned photos anchored, where the photos+ /// scrolling behind them should have faded out. Nil otherwise.+ static func pinnedEdge(count: Int, width: CGFloat, visible: Int, pinned: Int) -> CGFloat? {+ guard Line.keepOnLine != nil, count > visible, anchorsPinned(pinned, visible: visible) else { return nil }+ let newest = width / 2 + CGFloat(visible - 1) * spacing / 2+ return newest - CGFloat(visible - pinned) * spacing+ }+static func ropeY(x: CGFloat, width: CGFloat) -> CGFloat {guard width > 0 else { return ropeTop }let f = x / width@@ -42,9 +54,17 @@ enum Layout {/// the newest stays where the last one would hang on a full line, and/// `scroll` brings older ones in from the left. Otherwise (a line hung/// on a wider screen) they move closer together so none is past the edge.- static func x(index: Int, count: Int, width: CGFloat, visible: Int, scroll: CGFloat) -> CGFloat {+ ///+ /// The first `pinned` photos are the pinned ones. On a line that+ /// scrolls they stay put at its left end, and the rest scroll behind+ /// them, unless they would leave almost no room for the rest.+ static func x(index: Int, count: Int, width: CGFloat, visible: Int, scroll: CGFloat,+ pinned: Int = 0) -> CGFloat {if Line.keepOnLine != nil && count > visible {let newest = width / 2 + CGFloat(visible - 1) * spacing / 2+ if anchorsPinned(pinned, visible: visible), index < pinned {+ return newest - CGFloat(visible - 1 - index) * spacing+ }return newest - CGFloat(count - 1 - index) * spacing + scroll}let gaps = CGFloat(max(count - 1, 0))@@ -76,12 +96,27 @@ struct LineView: View {ForEach(Array(line.items.enumerated()), id: \.element.id) { index, item inlet x = Layout.x(index: index, count: line.items.count, width: width,- visible: line.visibleCount, scroll: line.scroll)+ visible: line.visibleCount, scroll: line.scroll, pinned: line.pinnedLead)+ // On a line that scrolls, a photo reaching the edge of the+ // screen, or the knot it slips behind, dims and stops taking+ // clicks as soon as it starts to.+ let bounds = index >= line.pinnedLead ? scrollBounds(width: width) : nil+ let cardLeft = x - Layout.cardWidth / 2, cardRight = x + Layout.cardWidth / 2+ let out = bounds.map { cardLeft < $0.lowerBound || cardRight > $0.upperBound } == true+ // On the left it fades gradually: from a little before the+ // limit until it has gone halfway behind it.+ let fadeIn = bounds.map {+ min(1, max(0, (cardLeft - ($0.lowerBound - Layout.cardWidth / 2)) / (Layout.cardWidth / 2 + 60)))+ } ?? 1let ropeY = Layout.ropeY(x: x, width: width)PeggedView(item: item, line: line).frame(width: Layout.cardWidth, height: Layout.panelHeight - ropeY, alignment: .top)// Photos scrolled towards an edge fade out before they reach it.- .opacity(min(1, max(0, min(x, width - x) / 90)))+ .opacity(min(1, max(0, min(x, width - x) / 90)) * fadeIn+ * (bounds.map { cardRight > $0.upperBound } == true ? 0.35 : 1))+ .animation(.easeOut(duration: 0.2), value: out)+ .allowsHitTesting(!out)+ .zIndex(index < line.pinnedLead ? 1 : 0).position(x: x, y: ropeY - Layout.pinAbove + (Layout.panelHeight - ropeY) / 2)}}@@ -99,13 +134,29 @@ struct LineView: View {}}+ /// Where a photo on a scrolling line stops being whole in view: the+ /// knot when pinned photos stay put, or else the left edge of the screen,+ /// and the right edge. Nil on a line that does not scroll.+ private func scrollBounds(width: CGFloat) -> ClosedRange<CGFloat>? {+ guard Line.keepOnLine != nil, line.items.count > line.visibleCount else { return nil }+ let pinnedStay = Layout.pinnedEdge(count: line.items.count, width: width,+ visible: line.visibleCount, pinned: line.pinnedLead) != nil+ let start = pinnedStay ? knotX(width: width) ?? 0 : 0+ return start...width+ }+/// Halfway between the last pinned photo and the next one, or half a/// step past it when nothing hangs loose. The empty place a pinned photo/// leaves still counts, so the knot stays put until the gaps close.private func knotX(width: CGFloat) -> CGFloat? {guard let last = line.items.lastIndex(where: \.kept) else { return nil }let x = { (i: Int) in Layout.x(index: i, count: line.items.count, width: width,- visible: line.visibleCount, scroll: line.scroll) }+ visible: line.visibleCount, scroll: line.scroll, pinned: line.pinnedLead) }+ // On a line that scrolls, the pinned photos stay put, and the knot with them.+ if Layout.pinnedEdge(count: line.items.count, width: width,+ visible: line.visibleCount, pinned: line.pinnedLead) != nil {+ return x(last) + Layout.spacing / 2+ }let next = last + 1 < line.items.count ? x(last + 1) : x(last) + Layout.spacingreturn (x(last) + next) / 2}