SoFriendly / 2fhey

Creative Commons Zero v1.0 Universal
115 stars 23 forks source link

Feature: Add Clipboard Restore Functionality #22

Closed Cavallando closed 1 year ago

Cavallando commented 1 year ago

Hey there,

Awesome job on this app! It has been working perfectly for me! Similar to other apps like 1Password I thought it would be cool to add Clipboard restore feature to this.

Apologies in advance for opening a PR without discussing first on an Issue, I happy to change anything you all like to fit your architecture or if this feature is not desired then I have no problem closing the PR.

With the Restore Clipboard Contents settings item enabled, 2FHey will save what is currently in the user's clipboard before writing the code to the clipboard. After waiting 20sec it will restore the clipboard contents to what was there before.

Notes

Screenshot 2023-02-08 at 12 03 46 PM

Screenshot 2023-02-08 at 12 04 22 PM

Screenshot 2023-02-08 at 2 30 58 PM

itsthisjustin commented 1 year ago

Dude this is awesome! Let me test this weekend and if all good I'll merge.

For the settings, I'd just shove it in the menu bar. Eventually we may need a settings window that opens up with more stuff. Maybe just a sub menu with a couple predefined time intervals will work for now?

Cavallando commented 1 year ago

Thanks man! Was super impressed with how easy the project was to navigate and add this change (with my limited swift knowledge)!

Great idea for the time intervals ill definitely make that change!

ditorodev commented 1 year ago

Hello! Newbie to the project here 😅.

  • I really wanted this feature to detect when the code was pasted and then perform the restore but NSPasteboard seems lacking on some listening/observing functionality, i guess it could listen for key strokes or system paste actions maybe? But felt

I think this is an amazing idea! You can probably implement it using NSEvent it is a bit raw of an API and probably will have a ton of edge-cases to handle; but I had say if it does not overwrite the base functionality of NSPasteboard's CMD+V listener then it is an easier feat as you only need to listen for it and then just restore the clipboard!

PS: From quick reading the API docs it does not seem like it overwrites behavior, more like is just a normal listener. Although not being hooked to NSPasteboard I'm sure will bring problems with users that rebind that shortcut for something else. unlikely to be relevant at all tbf

Cavallando commented 1 year ago

@ditorojuan ooooo thank you so much for the info! (I also just realized I never finished that sentence lol) I assumed something like that existed but still a bit new to swift so not as familiar with all of the interfaces/classes. I will take a look at NSEvent and see what I can come up with!

FWIW I also agree that handling rebound shortcuts feels out of scope but I am also new to the project, maybe thats a future improvement

Cavallando commented 1 year ago

@itsthisjustin as a heads up, I ended up changing the implementation slightly to use @ditorojuan's suggestion and use NSEvent for CMD+V listener, since this increases the access scope for 2FHey to require Accessibility permissions I understand if you don't want that included. Happy to revert back to the original implementation!

Note that in this new NSEvent implementation I am still using a delay to delay the time between pasting and restoring contents.

itsthisjustin commented 1 year ago

No I like that a lot. What happens if they right click and paste instead? haha

Cavallando commented 1 year ago

Hahah yea I thought that might come up, let me look around and see what I can find to listen for that event

Cavallando commented 1 year ago

So from my searches it does not appear that you can listen for a global paste action, I was hoping NSPasteboard would have the ability to listen for like onRead but it appears the only way to detect changes is to poll for them but even polling only shows changes which there wouldnt be any changes in the case of pasting contents (I also tested this out to be sure and no change is found). I'm assuming other clipboard apps like "Paste" or "FlyCut" maintain their own clipboard store with their own listeners that they wrote bound to their own actions/shortcuts.

I searched and read for quite some time but if there's something i'm clearly missing definitely let me know! FWIW I looked at 1Password and it appears their settings are listed as "Remove copied information and authentication codes after 90 seconds"

With that then do you think I should revert to just using the time delay? Or do you think I should combine the features? like "restore after x time or restore when cmd+v is triggered" which ever happens first

itsthisjustin commented 1 year ago

I think a combo of both makes sense. And tbh maybe just default to the 90secs thing like they do?

umangloria commented 1 year ago

Hurray, This is nice work. PR is merged. We will refactor the code whenever we get bandwidth.