Skip to content

Commit

Permalink
New option to move used clips to the top of the stack
Browse files Browse the repository at this point in the history
As a preliminary feature to avoid clippings rolling out of use (before
adding a full "favorites" or "pinned" set of options), we add the
option to move clippings to the top of the stack when selected (either
via the menu or via keyboard).

Github issues #57, #92
  • Loading branch information
Steve Cook committed Nov 12, 2023
1 parent 9467605 commit 6388af5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
18 changes: 18 additions & 0 deletions Jumpcut/Jumpcut/Clippings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public class ClippingStack: NSObject {
return store.itemAt(position: position)
}

func moveItemToTop(position: Int) {
store.moveItemToTop(position: position)
}

func up() {
let newPosition = position - 1
if newPosition >= 0 {
Expand Down Expand Up @@ -237,6 +241,20 @@ private class ClippingStore: NSObject {
writeClippings()
}

func moveItemToTop(position: Int) {
// Don't move from an invalid position; also, position 0
// is a null operation, because it's already at the top.
guard !clippings.isEmpty,
position <= clippings.count,
position > 0
else {
return
}
let element = clippings.remove(at: position)
clippings.insert(element, at: 0)
writeClippings()
}

func itemAt(position: Int) -> Clipping? {
if clippings.isEmpty || position > clippings.count {
return nil
Expand Down
20 changes: 20 additions & 0 deletions Jumpcut/Jumpcut/Interactions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public class Interactions: NSObject {
} else {
place(clipping!)
}
let moveToTop = UserDefaults.standard.value(
forKey: SettingsPath.moveClippingsAfterUse.rawValue
) as? Bool ?? false
if moveToTop {
stack.moveItemToTop(position: stack.position)
stack.position = 0
menu.rebuild(stack: stack)
}
}

private func handleBezelNumber(numberKey: SauceKey) {
Expand Down Expand Up @@ -230,6 +238,18 @@ public class Interactions: NSObject {
} else {
place(clipping!)
}
let moveToTop = UserDefaults.standard.value(
forKey: SettingsPath.moveClippingsAfterUse.rawValue
) as? Bool ?? false
if moveToTop {
stack.moveItemToTop(position: idx)
if idx == stack.position {
stack.position = 0
} else if idx > stack.position {
stack.position += 1
}
menu.rebuild(stack: stack)
}
}

@objc public func menuSelection(sender: NSMenuItem!) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ final class ClippingsPreferenceViewController: NSViewController, PreferencePane
title: "Allow whitespace clippings",
key: SettingsPath.allowWhitespaceClippings
)
let btn5 = settings.checkbox(
title: "Move clippings to top after use",
key: SettingsPath.moveClippingsAfterUse
)
skipSaveButton!.state = UserDefaults.standard.value(
forKey: SettingsPath.skipSave.rawValue
) as? Bool ?? false ? .on : .off
let grid = NSStackView(views: [ btn1, btn2, skipSaveButton!, btn4 ])
let grid = NSStackView(views: [ btn1, btn2, skipSaveButton!, btn4, btn5 ])
grid.orientation = .vertical
grid.alignment = .leading
self.view.addSubview(grid)
Expand Down
4 changes: 2 additions & 2 deletions Jumpcut/Jumpcut/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ enum SettingsPath: String {
case mainHotkey
case menuBehaviorFlags
case menuIcon
case menuSelectionMovesToTop
case menuSelectionPastes
case moveClippingsAfterUse
case rememberNum
case skipSave
case stickyBezel
Expand Down Expand Up @@ -73,8 +73,8 @@ private let settingsDefaults: [String: Any] = [
"modifierFlags": 786432
],
SettingsPath.menuBehaviorFlags.rawValue: MenuBehaviorFlags.none.rawValue,
SettingsPath.menuSelectionMovesToTop.rawValue: false, // TODO: still meaningful?
SettingsPath.menuSelectionPastes.rawValue: true,
SettingsPath.moveClippingsAfterUse.rawValue: false,
SettingsPath.menuIcon.rawValue: 0,
SettingsPath.rememberNum.rawValue: 99,
SettingsPath.skipSave.rawValue: false,
Expand Down

0 comments on commit 6388af5

Please sign in to comment.