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
141 changes: 127 additions & 14 deletions BarWidget.qml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ Panel {
property var selectedPathList: []
// Where a Shift-click measures its range from.
property int selectionAnchor: -1
// Same reason as selectionCount: walking the model from a binding would
// go stale the moment setProperty flipped `pinned`.
property int pinnedCount: 0

function syncSelection() {
const paths = []
Expand All @@ -167,6 +170,36 @@ Panel {
root.selectionCount = paths.length
if (paths.length === 0)
root.selectionAnchor = -1
root.syncPinnedCount()
}

function syncPinnedCount() {
let n = 0
for (let i = 0; i < ledgeModel.count; i++) {
if (ledgeModel.get(i).pinned === true)
n++
}
root.pinnedCount = n
}

// Pinned chips sit at the top, keeping the order they already had inside
// each group. ListModel has no sort, so this is a stable partition with
// `move` — the same rule `Model.pinnedFirst` describes for the tests.
function compactPinned() {
let insertAt = 0
let moved = false
for (let i = 0; i < ledgeModel.count; i++) {
if (ledgeModel.get(i).pinned === true) {
if (i !== insertAt) {
ledgeModel.move(i, insertAt, 1)
moved = true
}
insertAt++
}
}
if (moved)
root.selectionAnchor = -1
syncSelection()
}

function toggleSelection(index) {
Expand Down Expand Up @@ -220,6 +253,17 @@ Panel {
persist()
}

function setPinnedAt(index, pinned) {
if (index < 0 || index >= ledgeModel.count)
return
if ((ledgeModel.get(index).pinned === true) === pinned)
return
ledgeModel.setProperty(index, "pinned", pinned)
compactPinned()
persist()
showToast(pinned ? "Pinned — kept on clear" : "Unpinned")
}

// Everything the buttons in the header act on: the selection when there is
// one, the whole ledge otherwise.
function targetPaths() {
Expand All @@ -245,8 +289,9 @@ Panel {
if (indexOfPath(item.path) !== -1)
continue
// The role has to exist from the first append or setProperty()
// cannot add it later.
ledgeModel.append(Object.assign({ selected: false }, item))
// cannot add it later — that includes `pinned`, which is flipped
// after the chip is already in the list.
ledgeModel.append(Object.assign({ selected: false, pinned: false }, item))
added++
}
if (added > 0) {
Expand Down Expand Up @@ -293,10 +338,20 @@ Panel {
function clearLedge() {
if (ledgeModel.count === 0)
return
ledgeModel.clear()
let removed = 0
for (let i = ledgeModel.count - 1; i >= 0; i--) {
if (ledgeModel.get(i).pinned !== true) {
ledgeModel.remove(i)
removed++
}
}
if (removed === 0) {
showToast(ledgeModel.count === 1 ? "Pinned file kept" : "Pinned files kept")
return
}
syncSelection()
persist()
showToast("Ledge cleared")
showToast(ledgeModel.count > 0 ? "Cleared — pinned files kept" : "Ledge cleared")
}

// --- clipboard / open ----------------------------------------------------
Expand Down Expand Up @@ -424,7 +479,11 @@ Panel {
const items = []
for (let i = 0; i < ledgeModel.count; i++) {
const entry = ledgeModel.get(i)
items.push({ path: entry.path, addedAt: entry.addedAt })
items.push({
path: entry.path,
addedAt: entry.addedAt,
pinned: entry.pinned === true
})
}
stateFile.setText(Model.serialize(items))
}
Expand All @@ -438,12 +497,17 @@ Panel {
if (!items.length)
return
const raced = ledgeModel.count > 0
const existing = root.allPaths()
for (const item of items) {
if (existing.indexOf(item.path) === -1)
ledgeModel.append(Object.assign({ selected: false }, item))
const index = indexOfPath(item.path)
if (index === -1) {
ledgeModel.append(Object.assign({ selected: false, pinned: false }, item))
} else if (item.pinned === true && ledgeModel.get(index).pinned !== true) {
// A drop that won the startup race still picks up the pin
// the state file remembered for the same path.
ledgeModel.setProperty(index, "pinned", true)
}
}
syncSelection()
compactPinned()
// Only when the two lists actually met. On an ordinary start the file
// on disk is already exactly what was just read out of it.
if (raced)
Expand Down Expand Up @@ -477,7 +541,7 @@ Panel {
// across the method's parameters, so a bare JSON array never survives the
// trip. An object does, and it keeps file names with spaces, commas,
// quotes or newlines in one piece.
function addFromArgument(argument) {
function entriesFromArgument(argument) {
const text = String(argument)
let entries = null
try {
Expand All @@ -489,7 +553,40 @@ Panel {
} catch (e) {
// Not JSON: treated as plain text below.
}
return root.addPaths(entries ? entries : text.split("\n"))
return entries ? entries : text.split("\n")
}

function addFromArgument(argument) {
return root.addPaths(root.entriesFromArgument(argument))
}

// Pins files that are already on the ledge, and puts missing ones there
// already pinned — the CLI path for "I always re-add these". Unpin of a
// path that is not on the ledge is a no-op.
function pinFromArgument(argument, pinned) {
const items = Model.itemsFromDrop(root.entriesFromArgument(argument), Date.now())
let changed = 0
for (const item of items) {
const index = indexOfPath(item.path)
if (index === -1) {
if (!pinned)
continue
ledgeModel.append(Object.assign({ selected: false }, item, { pinned: true }))
changed++
} else if ((ledgeModel.get(index).pinned === true) !== pinned) {
ledgeModel.setProperty(index, "pinned", pinned)
changed++
}
}
if (changed > 0) {
compactPinned()
persist()
if (pinned)
showToast(changed === 1 ? "Pinned 1 file" : "Pinned " + changed + " files")
else
showToast(changed === 1 ? "Unpinned 1 file" : "Unpinned " + changed + " files")
}
return changed
}

IpcHandler {
Expand All @@ -515,7 +612,15 @@ Panel {

function list(): string { return root.allPaths().join("\n") }
function count(): string { return String(ledgeModel.count) }
function clear(): string { root.clearLedge(); return "0" }
function clear(): string { root.clearLedge(); return String(ledgeModel.count) }
function pin(paths: string): string {
const n = root.pinFromArgument(paths, true)
root.open()
return String(n)
}
function unpin(paths: string): string {
return String(root.pinFromArgument(paths, false))
}
}

LedgeTheme {
Expand Down Expand Up @@ -707,10 +812,16 @@ Panel {
LedgeIconButton {
theme: ledgeTheme
icon: root.glyphTrash
tooltip: root.selectionCount > 0 ? "Remove selected" : "Clear ledge"
tooltip: root.selectionCount > 0
? "Remove selected"
: (root.pinnedCount > 0 ? "Clear unpinned" : "Clear ledge")
tooltipEdge: "bottom"
danger: true
visible: ledgeModel.count > 0 && !root.settingsOpen
// Hidden when every remaining file is pinned and
// nothing is selected: clear would not take any of them.
visible: !root.settingsOpen
&& (root.selectionCount > 0
|| root.pinnedCount < ledgeModel.count)
onClicked: root.selectionCount > 0 ? root.removeSelected() : root.clearLedge()
}

Expand Down Expand Up @@ -764,6 +875,7 @@ Panel {
icon: model.icon
isImage: model.isImage
selected: model.selected === true
pinned: model.pinned === true
selectionCount: root.selectionCount
// Dragging a selected chip carries the whole selection;
// dragging an unselected one carries just itself and leaves
Expand All @@ -777,6 +889,7 @@ Panel {
onCopyPathRequested: root.copyPath(chipDelegate.path)
onOpenRequested: root.openPath(chipDelegate.path)
onRemoveRequested: root.removeAt(chipDelegate.index)
onPinRequested: root.setPinnedAt(chipDelegate.index, !chipDelegate.pinned)
onSelectToggleRequested: root.toggleSelection(chipDelegate.index)
onSelectRangeRequested: additive => root.selectRangeTo(chipDelegate.index, additive)
onDragStarted: {
Expand Down
29 changes: 29 additions & 0 deletions LedgeChip.qml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Rectangle {
property string ext: ""
property bool isImage: false
property bool selected: false
property bool pinned: false
// How many chips are selected in total, so a selected chip can say what it
// is part of. Zero when nothing is selected.
property int selectionCount: 0
Expand All @@ -31,6 +32,7 @@ Rectangle {
signal copyFileRequested()
signal copyPathRequested()
signal openRequested()
signal pinRequested()
signal removeRequested()
signal selectToggleRequested()
signal selectRangeRequested(bool additive)
Expand Down Expand Up @@ -196,6 +198,22 @@ Rectangle {
font.pixelSize: Style.font.displayLarge
color: theme.muted
}

// Stays visible when the hover actions are hidden, so a pinned file
// still reads as pinned at a glance. Outline keeps the glyph readable
// on a bright thumbnail.
Text {
visible: chip.pinned
anchors.right: parent.right
anchors.bottom: parent.bottom
anchors.margins: 1
text: "\u{F0403}" // nf-md-pin
font.family: chip.fontFamily
font.pixelSize: Style.font.caption
color: theme.accent
style: Text.Outline
styleColor: theme.surface
}
}

Column {
Expand Down Expand Up @@ -233,6 +251,8 @@ Rectangle {
}
if (hover.hovered)
return "drag · click copies · ctrl-click selects"
if (chip.pinned)
return chip.ext ? chip.ext.toUpperCase() + " · pinned" : "pinned"
return chip.ext ? chip.ext.toUpperCase() : "FILE"
}
elide: Text.ElideRight
Expand Down Expand Up @@ -275,6 +295,15 @@ Rectangle {
onClicked: chip.openRequested()
}

LedgeIconButton {
theme: chip.theme
icon: chip.pinned ? "\u{F0403}" : "\u{F0931}" // nf-md-pin / nf-md-pin_outline
tooltip: chip.pinned ? "Unpin" : "Pin — kept on clear"
tooltipEdge: "left"
active: chip.pinned
onClicked: chip.pinRequested()
}

LedgeIconButton {
theme: chip.theme
icon: "\u{F01B4}" // nf-md-delete
Expand Down
42 changes: 37 additions & 5 deletions LedgeModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// without Qt (scripts/test-model.mjs). Everything here is a plain function
// over plain data.
//
// An item is: { path, fileName, ext, kind, icon, isImage, addedAt }
// An item is: { path, fileName, ext, kind, icon, isImage, addedAt, pinned }

var STATE_VERSION = 1

Expand Down Expand Up @@ -105,7 +105,7 @@ function uriList(paths) {
return paths.map(urlFromPath).join("\r\n") + "\r\n"
}

function makeItem(path, addedAt) {
function makeItem(path, addedAt, pinned) {
var kind = kindOf(path)
return {
path: path,
Expand All @@ -114,7 +114,8 @@ function makeItem(path, addedAt) {
kind: kind,
icon: iconFor(path),
isImage: isImageKind(kind),
addedAt: addedAt ? addedAt : 0
addedAt: addedAt ? addedAt : 0,
pinned: !!pinned
}
}

Expand All @@ -138,7 +139,12 @@ function itemsFromDrop(entries, addedAt) {

function serialize(items) {
var plain = items.map(function (item) {
return { path: item.path, addedAt: item.addedAt }
var entry = { path: item.path, addedAt: item.addedAt }
// Only the true pins are written, so an unpinned ledge still looks
// like the original { path, addedAt } records.
if (item.pinned)
entry.pinned = true
return entry
})
return JSON.stringify({ version: STATE_VERSION, items: plain }, null, 2) + "\n"
}
Expand All @@ -160,11 +166,37 @@ function deserialize(text) {
var path = typeof entry === "string" ? entry : (entry && entry.path)
if (!path || String(path).indexOf("/") !== 0)
continue
items.push(makeItem(String(path), entry && entry.addedAt ? entry.addedAt : 0))
items.push(makeItem(String(path),
entry && entry.addedAt ? entry.addedAt : 0,
boolSetting(entry && entry.pinned, false)))
}
return items
}

function isPinned(item) {
return !!(item && item.pinned)
}

// What `clear` leaves behind: pinned files stay until they are unpinned.
function keepPinned(items) {
return items.filter(isPinned)
}

// Pinned files sit at the top, in the order they already had. Unpinned files
// keep their relative order underneath. Used to check the rule; the live list
// is a ListModel and does the same shuffle with `move`.
function pinnedFirst(items) {
var pinned = []
var rest = []
for (var i = 0; i < items.length; i++) {
if (isPinned(items[i]))
pinned.push(items[i])
else
rest.push(items[i])
}
return pinned.concat(rest)
}

// Bar settings arrive as real JSON from shell.json, but `omarchy bar set
// <id> <key> <value>` writes the *string* "true"/"false" unless the caller
// remembers --json. Both spellings have to mean the same thing: otherwise the
Expand Down
Loading
Loading