Skip to content

Screenshots, hung out to dry. A tiny native macOS app that hangs every screenshot on a line at the top of your screen.

Swift

Keep Tendedero's folder from filling up with screenshots off the line (#36)

nijataliyev76b382b0d1ebf
diff --git a/Sources/Tendedero/Line.swift b/Sources/Tendedero/Line.swift
index 98eaf0a..9487c7e 100644
--- a/Sources/Tendedero/Line.swift
+++ b/Sources/Tendedero/Line.swift
@@ -62,9 +62,11 @@ final class Line: ObservableObject {
var item = Pegged(url: url, thumb: thumb)
item.flying = flying
items.append(item)
- // A full line lets the oldest photo fall off the far end.
- while liveCount > maxItems, let oldest = items.first(where: { !$0.falling }) {
- drop(oldest.id, quietly: true)
+ // A full line lets the oldest photo fall off the far end. Only one: a
+ // line hung on a wider screen keeps its length here instead of losing
+ // several photos to a single capture.
+ if liveCount > maxItems, let oldest = items.first(where: { !$0.falling }) {
+ letGo(oldest.id)
}
save()
if !quietly { play("Tink", volume: 0.35) }
@@ -93,11 +95,12 @@ final class Line: ObservableObject {
}
}
+ /// "Take everything down": every photo goes the way of its corner cross.
func clear() {
let live = items.filter { !$0.falling }
for (n, item) in live.enumerated() {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.06 * Double(n)) { [weak self] in
- self?.drop(item.id, quietly: n > 0)
+ self?.discard(item.id, quietly: n > 0)
}
}
}
@@ -134,16 +137,19 @@ final class Line: ObservableObject {
/// Moves the file to the Trash and takes the photo off the line. When a
/// drag ends on the Dock's Trash, macOS only reports it: deleting the file
/// is the source app's job, as Finder does.
- func trash(_ id: UUID) {
- guard let item = items.first(where: { $0.id == id }) else { return }
+ @discardableResult
+ func trash(_ id: UUID, quietly: Bool = false) -> Bool {
+ guard let item = items.first(where: { $0.id == id }) else { return false }
do {
try FileManager.default.trashItem(at: item.url, resultingItemURL: nil)
log.notice("Trashed \(item.url.lastPathComponent, privacy: .public)")
- if soundOn { Line.trashSound?.play() }
+ if soundOn && !quietly { Line.trashSound?.play() }
drop(id, quietly: true)
+ return true
} catch {
log.error("Could not trash \(item.url.path, privacy: .public): \(error.localizedDescription, privacy: .public)")
- NSSound.beep()
+ if !quietly { NSSound.beep() }
+ return false
}
}
@@ -160,8 +166,16 @@ final class Line: ObservableObject {
}
/// The corner cross and "Take down" both end up here.
- func discard(_ id: UUID) {
- if isInInbox(id) { trash(id) } else { drop(id) }
+ func discard(_ id: UUID, quietly: Bool = false) {
+ if isInInbox(id) { trash(id, quietly: quietly) } else { drop(id, quietly: quietly) }
+ }
+
+ /// The oldest photo falling off a full line. Like the cross, a file from
+ /// Tendedero's folder goes to the Trash, or nothing would ever take it out
+ /// of that folder. If the Trash refuses it, it still leaves the line.
+ private func letGo(_ id: UUID) {
+ if isInInbox(id), trash(id, quietly: true) { return }
+ drop(id, quietly: true)
}
/// Inbox mode: keep a screenshot by moving it to the Desktop.
@@ -227,10 +241,15 @@ final class Line: ObservableObject {
UserDefaults.standard.set(paths, forKey: storeKey)
}
+ /// Everything comes back as it was. This runs before the line knows its
+ /// screen, so the capacity is still the default one: hanging through
+ /// `hang` would let photos fall off a wide screen's line at every launch.
private func restore() {
let paths = UserDefaults.standard.stringArray(forKey: storeKey) ?? []
for path in paths where FileManager.default.fileExists(atPath: path) {
- hang(URL(fileURLWithPath: path), quietly: true)
+ let url = URL(fileURLWithPath: path)
+ guard !items.contains(where: { $0.url == url }), let thumb = makeThumbnail(url) else { continue }
+ items.append(Pegged(url: url, thumb: thumb))
}
}
diff --git a/Sources/Tendedero/LineView.swift b/Sources/Tendedero/LineView.swift
index 8304b5b..9de27f1 100644
--- a/Sources/Tendedero/LineView.swift
+++ b/Sources/Tendedero/LineView.swift
@@ -16,9 +16,13 @@ enum Layout {
return ropeTop + 4 * sag(width: width) * f * (1 - f)
}
+ /// Photos hang `spacing` apart. A line hung on a wider screen can hold
+ /// more than this one has room for, and then they move closer together
+ /// so that none ends up past the edge.
static func x(index: Int, count: Int, width: CGFloat) -> CGFloat {
- let total = CGFloat(max(count - 1, 0)) * spacing
- return width / 2 - total / 2 + CGFloat(index) * spacing
+ let gaps = CGFloat(max(count - 1, 0))
+ let step = gaps > 0 ? min(spacing, max(0, width - 200) / gaps) : spacing
+ return width / 2 - gaps * step / 2 + CGFloat(index) * step
}
}