therecipe / qt

Qt binding for Go (Golang) with support for Windows / macOS / Linux / FreeBSD / Android / iOS / Sailfish OS / Raspberry Pi / AsteroidOS / Ubuntu Touch / JavaScript / WebAssembly
GNU Lesser General Public License v3.0
10.51k stars 748 forks source link

Qt adding to general right click context menu #947

Open amlwwalker opened 5 years ago

amlwwalker commented 5 years ago

Alot of software adds a right click option to the general context menu. For instance gpg/FileZilla etc have right click options when u tight click on a file. I'm having trouble googling this, my research says on Windows its a registry fix, but I am really trying to find if Qt offers any ability to do this kind of thing cross platform. I asked once before about global shortcuts which Qt seems to support (did you ever get a chance to look into this btw? And so it seemed like adding to the global right click menu may also be something Qt offers. Do you know how this generally works?

therecipe commented 5 years ago

I think this is only possible on windows, at least I never saw it on macOS. It might be also possible on linux, don't know though.

About the global shortcut, I remember looking into https://github.com/Skycoder42/QHotkey but haven't had time to get something working back then. Now I looked into this again and found https://github.com/falceeffect/UGlobalHotkey as well, which seemed to be exactly what you were looking for.

So I created a new example here https://github.com/therecipe/qt/tree/master/internal/examples/3rdparty/uglobalhotkey

Just run the init.go (you might need to fix the QT_DIR path or something) and then just run the example in the demo folder. It should work on macOS and windows out of the box (also when cross compiling with docker). On linux you will need to download the xcb-keysyms libs (libxcb-keysyms1-dev under debian/ubuntu) and maybe also change the path in the ugh_cgo.go file first.

amlwwalker commented 5 years ago

Hmm so on OSX gpg (the encryption key manager) adds a right click option under the services menu. That's what triggered my thinking. Anyway thanks for this, I'll do some more reading. My alpha is getting there as another app using this library, just adding all the niceness now. My hope is I can get information about the currently focused window and the ultimate would be to screen shot it as a result of coming across this https://stackoverflow.com/questions/1774222/taking-screenshot-of-a-specific-window-c-qt which got me thinking I could have a global shortcut that would screen shot the window the user currently has in focus.

therecipe commented 5 years ago

Ah yes, you are right. Keka seems to add entries to the context menu as well, I just never noticed :D I looked into this and it seems like you just need to edit the Info.plist to make it work.

This somewhat works for the widgets/service examples and adds an entry to the menu, and also creates a global shortcut (Shift+Control+Q) to open the app:

diff --git a/internal/examples/widgets/service/darwin/Contents/Info.plist b/internal/examples/widgets/service/darwin/Contents/Info.plist
index 3a967033..64a6b61e 100644
--- a/internal/examples/widgets/service/darwin/Contents/Info.plist
+++ b/internal/examples/widgets/service/darwin/Contents/Info.plist
@@ -39,5 +39,33 @@
             </array>
         </dict>
     </array>
+    <key>NSServices</key>
+   <array>
+   <dict>
+       <key>NSMessage</key>
+       <string>serviceHandle</string>
+       <key>NSKeyEquivalent</key>
+       <dict>
+           <key>default</key>
+           <string>^$Q</string>
+       </dict>
+       <key>NSPortName</key>
+       <string>service</string>
+       <key>NSRequiredContext</key>
+       <dict>
+           <key>NSTextContent</key>
+           <string>FilePath</string>
+       </dict>
+       <key>NSSendTypes</key>
+       <array>
+           <string>NSFilenamesPboardType</string>
+       </array>
+       <key>NSMenuItem</key>
+       <dict>
+           <key>default</key>
+           <string>service/send to service</string>
+       </dict>
+   </dict>
+   </array>
 </dict>
 </plist>

I couldn't figure how to actually receive the filepath though.

And about the stackoveflow question, this example here https://github.com/therecipe/qt/issues/16#issuecomment-182590007 lets you grab 3rdparty windows on windows.

amlwwalker commented 5 years ago

I am going to have a deeper look at this, as I'll also want it for Windows, which I think requires a registry edit, but I can across this https://thecoderslife.blogspot.com/2015/06/adding-right-click-context-menu-to.html?m=1

Which I think outlines a bit more about receiving the context, however not sure how to register an action as a service, is that a Qt thing?

therecipe commented 5 years ago

To access the registry on windows you can use https://doc.qt.io/qt-5/qsettings.html#accessing-common-registry-settings-on-windows

About the service on macOS, I will try to look into it once I find some time. But I doubt, that there is anything directly from Qt available to handle this.

amlwwalker commented 5 years ago

Hello, on this topic. I noticed that the uglobalhotkey library hijacks any shortcut, and doesn't pass it through to the focused application. Thats kind of hilarious as I can register Cmd+S as a shortcut and now nothing can save 😂

therecipe commented 5 years ago

Yeah, it seems to be the same for the QHotkey library: https://github.com/Skycoder42/QHotkey#known-limitations I'm not sure if you can work around this.