Skip to content

Commit

Permalink
preserve clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
artginzburg committed Mar 23, 2020
1 parent e65065c commit bb6f584
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 11 deletions.
4 changes: 4 additions & 0 deletions 2FA to Tray/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ var otpInstances: [OTP] = []
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
hotKey = HotKey(key: .g, modifiers: [.command, .option])

if defaults.object(forKey: "preserveClipboard") == nil {
defaults.set(true, forKey: "preserveClipboard")
}
}

public var hotKey: HotKey? {
Expand Down
7 changes: 7 additions & 0 deletions 2FA to Tray/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<outlet property="pasteOnClickButton" destination="6CR-IJ-zXp" id="Rtt-mm-SeB"/>
<outlet property="pasteOnDoubleClickButton" destination="2iy-PT-cH5" id="qMa-Di-2sF"/>
<outlet property="permissionsButton" destination="045-Ih-Tf7" id="7A2-sI-Q01"/>
<outlet property="preserveClipboardButton" destination="9lA-KV-tUH" id="pPk-kb-Bh1"/>
<outlet property="showNamesButton" destination="IJY-1K-iQW" id="MnU-dh-ORS"/>
<outlet property="statusMenu" destination="Xfq-v7-Hh9" id="IVL-Qf-J99"/>
</connections>
Expand Down Expand Up @@ -132,6 +133,12 @@
<action selector="enterAfterAutoPasteClicked:" target="dok-4Z-Dc3" id="4Jy-2H-RIV"/>
</connections>
</menuItem>
<menuItem title="Preserve Clipboard Contents" id="9lA-KV-tUH">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="preserveClipboardClicked:" target="dok-4Z-Dc3" id="ZWn-BD-dgv"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
Expand Down
14 changes: 14 additions & 0 deletions 2FA to Tray/Models/Clipboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ class Clipboard {
public static let shared = Clipboard()
private let pasteboard = NSPasteboard.general

var preservedString: String?

func get() -> [NSPasteboardItem]? {
pasteboard.pasteboardItems
}

func copy(_ string: String) {
pasteboard.declareTypes([NSPasteboard.PasteboardType.string], owner: nil)
pasteboard.setString(string, forType: NSPasteboard.PasteboardType.string)
Expand All @@ -30,6 +36,14 @@ class Clipboard {
keyVDown?.post(tap: .cgAnnotatedSessionEventTap)
keyVUp?.post(tap: .cgAnnotatedSessionEventTap)
}

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
if defaults.bool(forKey: "preserveClipboard") {
if let item = self.preservedString {
self.copy(item)
}
}
}
}

func checkAccessibilityPermissions() {
Expand Down
11 changes: 11 additions & 0 deletions 2FA to Tray/Models/OTP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ class OTP {
}

func copy() {
if defaults.bool(forKey: "preserveClipboard") {
let item = Clipboard.shared.get()?.last

if (item?.types.contains(.string))! {
if let data = item!.data(forType: .string) {
let encoded = String(data: data, encoding: .utf8)!
Clipboard.shared.preservedString = encoded
}
}
}

Clipboard.shared.copy(self.token)
print("copied the token")
}
Expand Down
7 changes: 7 additions & 0 deletions 2FA to Tray/StatusMenuController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -534,20 +534,27 @@ class StatusMenuController: NSObject, NSMenuDelegate {
print("switched enterAfterAutoPaste pref")
}

@IBOutlet weak var preserveClipboardButton: NSMenuItem!
@IBAction func preserveClipboardClicked(_ sender: NSMenuItem) {
defaults.boolToggle("preserveClipboard")
}

func menuNeedsUpdate(_ menu: NSMenu) {
launchAtLoginButton.state.by(LoginServiceKit.isExistLoginItems())
showNamesButton.state.by(defaults.bool(forKey: "showNames"))
hotkeyButton.state.by(defaults.bool(forKey: "pasteOnHotkey"))
pasteOnClickButton.state.by(defaults.bool(forKey: "pasteOnClick"))
pasteOnDoubleClickButton.state.by(defaults.bool(forKey: "pasteOnDoubleClick"))
enterAfterAutoPasteButton.state.by(defaults.bool(forKey: "enterAfterAutoPaste"))
preserveClipboardButton.state.by(defaults.bool(forKey: "preserveClipboard"))

let isProcessTrusted = AXIsProcessTrusted()
permissionsButton.isHidden = isProcessTrusted
hotkeyButton.isEnabled = isProcessTrusted
pasteOnClickButton.isEnabled = isProcessTrusted
pasteOnDoubleClickButton.isEnabled = isProcessTrusted
enterAfterAutoPasteButton.isEnabled = isProcessTrusted
preserveClipboardButton.isEnabled = isProcessTrusted
automaticUpdatesButton.state.by(SUUpdater.shared()!.automaticallyDownloadsUpdates)
}
}
Expand Down
21 changes: 10 additions & 11 deletions appcast.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@
<description>Most recent changes with links to updates.</description>
<language>en</language>
<item>
<title>1.4.9Autoupdate and handful menu</title>
<title>1.5Preserve clipboard, and switch accounts easier</title>
<description>
<![CDATA[
<ul>
<li>Added support for app autoupdate.
(Plus, "Check for updates" in Preferences)</li>
<li>Dropdown to the status menu by force-clicking on status icon
(Handful when you already clicked the icon but realized that you have the wrong account selected, so you put a bit more force and select the right one)</li>
<li>Open the status menu with <kbd>OPTION (⌥)</kbd> + <kbd>Click</kbd> on status icon</li>
<li>Hold <kbd>Shift</kbd> while reopening the app (Spotlight action) to open the status menu</li>
<li>Added a preference to preserve clipboard contents after auto-pasting
(Works exclusively with text so far. Turned on by default)</li>
<li>Select accounts in status menu with <kbd>CMD (⌘) + digit</kbd> keys</li>
<li>Added a preference for automatically downloading updates. (Preferences > Check for Updates > Automatic updates)
P.S. You still can check for updates manually by clicking the "Check for Updates" submenu item</li>
</ul>
]]>
</description>
<pubDate>2020-03-21</pubDate>
<releaseNotesLink>https://github.com/DaFuqtor/2FAtoTray/releases/tag/1.4.9</releaseNotesLink>
<pubDate>2020-03-23</pubDate>
<releaseNotesLink>https://github.com/DaFuqtor/2FAtoTray/releases/tag/1.5</releaseNotesLink>
<sparkle:minimumSystemVersion>10.14</sparkle:minimumSystemVersion>
<enclosure
url="https://github.com/DaFuqtor/2FAtoTray/releases/download/1.4.9/2FAtoTray.zip"
sparkle:version="1.4.9"
url="https://github.com/DaFuqtor/2FAtoTray/releases/download/1.5/2FAtoTray.zip"
sparkle:version="1.5"
length="0"
type="application/octet-stream"
/>
Expand Down

0 comments on commit bb6f584

Please sign in to comment.