Skip to contentSkip to conversation

gitk: workaround Tcl/Tk Cmd-TAB behavior on OSX

PATCH In review Tair Sabirgaliev 3 participants 7 messages Jun 15, 2016 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gitk: workaround Tcl/Tk Cmd-TAB behavior on OSX

On OSX Tcl/Tk application windows are created behind all the applications down the stack of windows. This is very annoying, because once a gitk window appears, it's the downmost window and switching to it is pain.

The patch is trivial: if we are on OSX, emulate Cmd-Shift-TAB key event, so that the gitk application window is brought from bottom to top.

---
 gitk | 13 +++++++++++++
 1 file changed, 13 insertions(+)
gitk +11 -0
index 572f73f..60a87fc 100755
@@ -11687,6 +11687,19 @@ if {[catch {package require Tk 8.4} err]} {
     exit 1
 }
 +# On OSX workaround the Tcl/Tk windows going down the stack of Cmd-TAB
+if {[tk windowingsystem] eq "aqua"} {
+    exec osascript -e {
+        tell application "System Events"
+            key down command
+            key down shift
+            keystroke tab
+            key up shift
+            key up command
+        end tell    +    }
+}
+
 # Unset GIT_TRACE var if set
 if { [info exists ::env(GIT_TRACE)] } {
     unset ::env(GIT_TRACE)
Signature · 1 line
1.8.2
#1 lore raw

The prev message was garbled :( Here is the correct patch (I hope).

On OSX Tcl/Tk application windows are created behind all the applications down the stack of windows. This is very annoying, because once a gitk window appears, it's the downmost window and switching to it is pain.

The patch is trivial: if we are on OSX, emulate Cmd-Shift-TAB key event, so that the gitk application window is brought from bottom to top.

---
 gitk | 13 +++++++++++++
 1 file changed, 13 insertions(+)
gitk +13 -0
index 572f73f..60a87fc 100755
@@ -11687,6 +11687,19 @@ if {[catch {package require Tk 8.4} err]} {
     exit 1
 }
 
+# On OSX workaround the Tcl/Tk windows going down the stack of Cmd-TAB
+if {[tk windowingsystem] eq "aqua"} {
+    exec osascript -e {
+        tell application "System Events"
+            key down command
+            key down shift
+            keystroke tab
+            key up shift
+            key up command
+        end tell
+    }
+}
+
 # Unset GIT_TRACE var if set
 if { [info exists ::env(GIT_TRACE)] } {
     unset ::env(GIT_TRACE)
Signature · 1 line
1.8.2

A message that is not in the archive (sent off-list or not yet received): CAJDDKr76K_-d6JSgv67DcXnZybpxkTjYPJxdObU3wrn+nYV3dw@mail.gmail.com

#3 lore raw
Quoted 6 lines · David Aguilar

Apologies because I don't know the Tk API very well.

In Qt there is an API method to raise windows -- QWidget::raise().

Is there no similar API in Tk?

Quoted 8 lines

I seems like this may be related:

http://wiki.tcl.tk/9457

..but it seems that -topmost may not be exactly the same thing.

That said, if there's no portable way to do it, then this is certainly a workaround.

Yes, I tried that one. Here is a quick test:

========== hello.tcl =============
#!/bin/sh
# Tcl ignores the next line -*- tcl -*- \
exec wish "$0" -- "$@"

package require Tk

grid [ttk::button .b -text "Hello World"]
wm attributes . -topmost true
==================================

I'm not sure I'm doing it right, but it doesn't work.

Look like Tcl/Tk dev are very paranoic about windows created from Terminal/shell scripts: they don't let them take over , even if it is desired.

#4 lore raw
In reply to Tair Sabirgaliev
Quoted 22 lines · Tair Sabirgaliev

On OSX Tcl/Tk application windows are created behind all the applications down the stack of windows. This is very annoying, because once a gitk window appears, it's the downmost window and switching to it is pain.

The patch is trivial: if we are on OSX, emulate Cmd-Shift-TAB key event, so that the gitk application window is brought from bottom to top.

---
 gitk | 13 +++++++++++++
 1 file changed, 13 insertions(+)
gitk +0 -0
index 572f73f..60a87fc 100755
@@ -11687,6 +11687,19 @@ if {[catch {package require Tk 8.4} err]} {
     exit 1
 }
 +# On OSX workaround the Tcl/Tk windows going down the stack of Cmd-TAB

gitk if {[catch {package require Tk 8.4} err]} { · line 11699

Quoted 10 lines
gitk +10 -0
+if {[tk windowingsystem] eq "aqua"} {
+    exec osascript -e {
+        tell application "System Events"
+            key down command
+            key down shift
+            keystroke tab
+            key up shift
+            key up command
+        end tell    +    }
+}

Is this really the only way to do it? It seems a bit hacky to me. I would think you should be able to use the "wm" command to achieve what you want. I don't use Mac OS, so I don't know exactly how Tcl/Tk behaves on it, but this page looks like it might hold some clues for what you want to do:

http://teapot.activestate.com/package/name/windowlist/ver/1.4/arch/tcl/file.tm

Paul.

#5 lore raw
Quoted 10 lines · Paul Mackerras
Quoted 8 lines · Tair Sabirgaliev

On OSX Tcl/Tk application windows are created behind all the applications down the stack of windows. This is very annoying, because once a gitk window appears, it's the downmost window and switching to it is pain.

The patch is trivial: if we are on OSX, emulate Cmd-Shift-TAB key event, so that the gitk application window is brought from bottom to top.

Quoted 7 lines

Is this really the only way to do it? It seems a bit hacky to me. I would think you should be able to use the "wm" command to achieve what you want. I don't use Mac OS, so I don't know exactly how Tcl/Tk behaves on it, but this page looks like it might hold some clues for what you want to do:

http://teapot.activestate.com/package/name/windowlist/ver/1.4/arch/tcl/file.tm

Today I did some further investigations on reasons app windows going behind. Turns out this is not something specific to Tcl/Tk.

On OSX, GUI applications can be launched from Terminal in 2 ways:
1. Interactively, directly using app executable:
sh$ /Applications/Chess.app/Contents/MacOS/Chess
2. Asynchronously, using OSX 'open' facility
sh$ open -a Chess

The problem with 1st way is that OSX always puts the new app window behind the others, thus making sure the focus remains at Terminal input.

The problem with the 2nd approach is that app is launched 'asynchronously', detached from Terminal, so when you close the app, focus doesn't necessarily return back to Terminal as it would normally do in Linux for example.

So, yes, this is a really hacky workaround, but it consistently takes the interactively launched app window to foreground on most modern OSX versions.

And regarding the link you provided -- I tried them, unfortunately the techniques there don't bring the application to foreground. Looks like they are mostly intended to manipulate windows within a single multi-window application.

Tair.

#6 lore raw
In reply to Tair Sabirgaliev
Quoted 14 lines · Tair Sabirgaliev

On OSX Tcl/Tk application windows are created behind all the applications down the stack of windows. This is very annoying, because once a gitk window appears, it's the downmost window and switching to it is pain.

The patch is trivial: if we are on OSX, emulate Cmd-Shift-TAB key event, so that the gitk application window is brought from bottom to top.

---

+# On OSX workaround the Tcl/Tk windows going down the stack of Cmd-TAB

gitk +1 -0
+if {[tk windowingsystem] eq "aqua"} {

gitk if {[catch {package require Tk 8.4} err]} { · line 11700

Quoted 10 lines
gitk +10 -0
+    exec osascript -e {
+        tell application "System Events"
+            key down command
+            key down shift
+            keystroke tab
+            key up shift
+            key up command
+        end tell    +    }
+}
+

First of all, I absolutely want this behaviour. Bringing windows up in the background is one of the biggest usability problems of git on Mac.

However, I don't think that synthesizing the keystrokes for Command-Shift-Tab is a good solution. It would be better to explicitly bring our process to the front. One way to achieve this would be:

if {[tk windowingsystem] eq "aqua"} {
exec osascript -e [format {
tell application "System Events"
set frontmost of processes whose unix id is %d to true
end tell
} [pid] ]
}

(Not sure about the formatting, I don't speak Tcl...)

Also, we need the same thing in git gui as well.

Best,
Stefan

Signature · 3 lines
Stefan Haller
Berlin, Germany
http://www.haller-berlin.de/
#7 lore raw

Re: [PATCH 1] gitk: on OSX bring the gitk window to front

On OSX Tcl/Tk application windows are created behind all the applications down the stack of windows. This is very annoying, because once a gitk window appears, it's the downmost window and switching to it is pain.

The patch is: if we are on OSX, use osascript to bring the current Wish process window to front.

Thanks-to: Stefan Haller <[email protected]>

---
 gitk | 9 +++++++++
 1 file changed, 9 insertions(+)
gitk +9 -0
index 572f73f..66e59b1 100755
@@ -11687,6 +11687,15 @@ if {[catch {package require Tk 8.4} err]} {
     exit 1
 }
 
+# on OSX bring the current Wish process window to front
+if {[tk windowingsystem] eq "aqua"} {
+    exec osascript -e [format {
+        tell application "System Events"
+            set frontmost of processes whose unix id is %d to true
+        end tell
+    } [pid] ]
+}
+
 # Unset GIT_TRACE var if set
 if { [info exists ::env(GIT_TRACE)] } {
     unset ::env(GIT_TRACE)
Signature · 1 line
1.8.2.1