Skip to content

Commit

Permalink
Add menu size option to limit visible items
Browse files Browse the repository at this point in the history
Closes #102 and #90

Co-Authored-By: Huseyin Yagli <huseyinyagli@users.noreply.github.com>
  • Loading branch information
p0deje and huseyinyagli committed Jun 9, 2020
1 parent 98394c7 commit 201e1d6
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 22 deletions.
4 changes: 3 additions & 1 deletion Maccy/Maccy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ class Maccy: NSObject {
pasteByDefaultObserver = UserDefaults.standard.observe(\.pasteByDefault, options: .new, changeHandler: { _, _ in
self.rebuild()
})
statusItemConfigurationObserver = UserDefaults.standard.observe(\.showInStatusBar, options: .new, changeHandler: { _, change in
statusItemConfigurationObserver = UserDefaults.standard.observe(\.showInStatusBar,
options: .new,
changeHandler: { _, change in
if self.statusItem.isVisible != change.newValue! {
self.statusItem.isVisible = change.newValue!
}
Expand Down
23 changes: 22 additions & 1 deletion Maccy/Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ class Menu: NSMenu, NSMenuDelegate {
private var clipboard: Clipboard!
private var history: History!
private var historyItems: [HistoryMenuItem] = []
private var maxMenuItems: Int {
UserDefaults.standard.maxMenuItems * 2
}

required init(coder decoder: NSCoder) {
super.init(coder: decoder)
}

init(history: History, clipboard: Clipboard) {
UserDefaults.standard.register(defaults: [UserDefaults.Keys.maxMenuItems: UserDefaults.Values.maxMenuItems])
super.init(title: "Maccy")
self.history = history
self.clipboard = clipboard
Expand All @@ -35,6 +39,9 @@ class Menu: NSMenu, NSMenuDelegate {

func buildItems(_ allItems: [HistoryItem]) {
clearAll()

let menuSizeLimit = items.count + maxMenuItems

for item in Sorter(by: UserDefaults.standard.sortBy).sort(allItems) {
let copyHistoryItem = HistoryMenuItem(item: item, onSelected: copy(_:))
let pasteHistoryItem = HistoryMenuItem(item: item, onSelected: copyAndPaste(_:))
Expand All @@ -47,6 +54,16 @@ class Menu: NSMenu, NSMenuDelegate {
prependHistoryItems(copyHistoryItem, pasteHistoryItem)
}
}

if maxMenuItems > 0 {
for historyItem in historyItems.reversed() {
if items.count > menuSizeLimit {
removeItem(historyItem)
} else {
break
}
}
}
}

func clearAll() {
Expand All @@ -58,7 +75,11 @@ class Menu: NSMenu, NSMenuDelegate {
}

func updateFilter(filter: String) {
let results = search.search(string: filter, within: historyItems)
var results = search.search(string: filter, within: historyItems)

if maxMenuItems > 0 && maxMenuItems < results.count {
results.removeSubrange(maxMenuItems...results.count - 1)
}

// First, remove items that don't match search.
for item in historyItems {
Expand Down
26 changes: 26 additions & 0 deletions Maccy/Preferences/AppearancePreferenceViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class AppearancePreferenceViewController: NSViewController, PreferencePane {
@IBOutlet weak var popupAtButton: NSPopUpButton!
@IBOutlet weak var imageHeightSlider: NSSlider!
@IBOutlet weak var imageHeightLabel: NSTextField!
@IBOutlet weak var menuSizeSlider: NSSlider!
@IBOutlet weak var menuSizeLabel: NSTextField!
@IBOutlet weak var showMenuIconButton: NSButton!
@IBOutlet weak var showSearchFieldButton: NSButton!
@IBOutlet weak var showTitleButton: NSButton!
Expand All @@ -20,6 +22,7 @@ class AppearancePreferenceViewController: NSViewController, PreferencePane {
super.viewWillAppear()
populatePopupPosition()
populateImageHeight()
populateMenuSize()
populateShowMenuIcon()
populateShowSearchField()
populateShowTitle()
Expand All @@ -44,6 +47,13 @@ class AppearancePreferenceViewController: NSViewController, PreferencePane {
UserDefaults.standard.imageMaxHeight = sender.integerValue
}

@IBAction func menuSizeChanged(_ sender: NSSlider) {
let old = String(UserDefaults.standard.maxMenuItems)
let new = String(menuSizeSlider.integerValue)
updateMenuSizeLabel(old: old, new: new)
UserDefaults.standard.maxMenuItems = sender.integerValue
}

@IBAction func showMenuIconChanged(_ sender: NSButton) {
UserDefaults.standard.showInStatusBar = (sender.state == .on)
}
Expand Down Expand Up @@ -87,6 +97,22 @@ class AppearancePreferenceViewController: NSViewController, PreferencePane {
imageHeightLabel.stringValue = newLabelValue
}

private func populateMenuSize() {
menuSizeSlider.integerValue = UserDefaults.standard.maxMenuItems
let new = String(menuSizeSlider.integerValue)
updateMenuSizeLabel(old: "{menuSize}", new: new)
}

private func updateMenuSizeLabel(old: String, new: String) {
let newLabelValue = menuSizeLabel.stringValue.replacingOccurrences(
of: old,
with: new,
options: [],
range: menuSizeLabel.stringValue.range(of: old)
)
menuSizeLabel.stringValue = newLabelValue
}

private func populateShowMenuIcon() {
showMenuIconButton.state = UserDefaults.standard.showInStatusBar ? .on : .off
}
Expand Down
Loading

0 comments on commit 201e1d6

Please sign in to comment.