feat: copy and paste selected emoji
This commit is contained in:
@@ -7,7 +7,7 @@ final class EmojiPickerController: NSViewController, NSCollectionViewDataSource,
|
||||
private let category = NSPopUpButton()
|
||||
private let status = NSTextField(labelWithString: "")
|
||||
private let empty = NSTextField(labelWithString: "No matching emoji")
|
||||
private let copyButton = NSButton(title: "Copy Emoji", target: nil, action: nil)
|
||||
private let copyButton = NSButton(title: "Copy and Paste", target: nil, action: nil)
|
||||
private let actionsButton = NSButton(title: "Actions", target: nil, action: nil)
|
||||
private let actions = ApplicationActionsController()
|
||||
private var favoriteCount = 0
|
||||
@@ -19,6 +19,7 @@ final class EmojiPickerController: NSViewController, NSCollectionViewDataSource,
|
||||
var input: (() -> String)?
|
||||
var closed: (() -> Void)?
|
||||
var copied: (() -> Void)?
|
||||
var pasteRequested: (() -> Void)?
|
||||
var focusSearch: (() -> Void)?
|
||||
|
||||
init(defaults: UserDefaults = .standard, pasteboard: NSPasteboard = .general) {
|
||||
@@ -59,7 +60,7 @@ final class EmojiPickerController: NSViewController, NSCollectionViewDataSource,
|
||||
status.lineBreakMode = .byTruncatingTail
|
||||
status.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
|
||||
copyButton.target = self
|
||||
copyButton.action = #selector(copySelection)
|
||||
copyButton.action = #selector(copyAndPasteSelection)
|
||||
copyButton.keyEquivalent = "\r"
|
||||
copyButton.isBordered = false
|
||||
copyButton.font = .systemFont(ofSize: 12, weight: .medium)
|
||||
@@ -198,7 +199,8 @@ final class EmojiPickerController: NSViewController, NSCollectionViewDataSource,
|
||||
guard results.indices.contains(selected) else { return }
|
||||
let emoji = results[selected]
|
||||
actions.isFavorite = (defaults.stringArray(forKey: "favoriteEmoji") ?? []).contains(emoji.value)
|
||||
actions.open = { [weak self] in self?.copySelection() }
|
||||
actions.open = { [weak self] in self?.copyAndPasteSelection() }
|
||||
actions.copyEmoji = { [weak self] in self?.copySelection() }
|
||||
actions.toggleFavorite = { [weak self] in self?.toggleFavorite() }
|
||||
actions.showEmoji(named: emoji.name, relativeTo: actionsButton)
|
||||
copyButton.keyEquivalent = actions.isShown ? "" : "\r"
|
||||
@@ -220,7 +222,7 @@ final class EmojiPickerController: NSViewController, NSCollectionViewDataSource,
|
||||
case #selector(NSResponder.moveRight(_:)): moveSelection(by: 1)
|
||||
case #selector(NSResponder.moveUp(_:)): moveVertically(-1)
|
||||
case #selector(NSResponder.moveDown(_:)): moveVertically(1)
|
||||
case #selector(NSResponder.insertNewline(_:)): copySelection()
|
||||
case #selector(NSResponder.insertNewline(_:)): copyAndPasteSelection()
|
||||
case #selector(NSResponder.cancelOperation(_:)): goBack()
|
||||
default: return false
|
||||
}
|
||||
@@ -238,7 +240,10 @@ final class EmojiPickerController: NSViewController, NSCollectionViewDataSource,
|
||||
(collection.item(at: indexPath(for: selected)) as? EmojiItem)?.highlight(true)
|
||||
}
|
||||
|
||||
@objc func copySelection() {
|
||||
@objc func copySelection() { copySelection(pasting: false) }
|
||||
@objc func copyAndPasteSelection() { copySelection(pasting: true) }
|
||||
|
||||
private func copySelection(pasting: Bool) {
|
||||
guard results.indices.contains(selected) else { return }
|
||||
let value = results[selected].value
|
||||
pasteboard.clearContents()
|
||||
@@ -250,7 +255,7 @@ final class EmojiPickerController: NSViewController, NSCollectionViewDataSource,
|
||||
recent.removeAll { $0 == value }
|
||||
defaults.set(Array(([value] + recent).prefix(30)), forKey: "recentEmoji")
|
||||
closed?()
|
||||
copied?()
|
||||
if pasting { pasteRequested?() } else { copied?() }
|
||||
}
|
||||
|
||||
func numberOfSections(in collectionView: NSCollectionView) -> Int { splitFavorites ? 2 : 1 }
|
||||
@@ -273,7 +278,7 @@ final class EmojiPickerController: NSViewController, NSCollectionViewDataSource,
|
||||
item.highlight(index == selected)
|
||||
item.clicked = { [weak self] in
|
||||
self?.selected = index
|
||||
self?.copySelection()
|
||||
self?.copyAndPasteSelection()
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user