threads / patch / 33464

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

Subject: [PATCH] gitk: workaround Tcl/Tk Cmd-TAB behavior on OSX

## tl;dr

7 messages between Apr 10, 2013 and Apr 24, 2013. Diffs are folded; open one to read it.

replies: 6people: 3as markdown or json

Tair Sabirgaliev· Apr 10, 2013, 19:02 UTC · lore

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.

Signed-off-by: Tair Sabirgaliev <tair.sabirgaliev@gmail.com>
---
  gitk | 13 +++++++++++++
  1 file changed, 13 insertions(+)
Show changes to gitk +11 −0
diff --git a/gitk b/gitk
index 572f73f..60a87fc 100755
--- a/gitk
+++ b/gitk
@@ -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)
-- 
1.8.2
Tair Sabirgaliev· Apr 10, 2013, 19:12 UTC · re: Tair Sabirgaliev · lore

Re: [PATCH] gitk: workaround Tcl/Tk Cmd-TAB behavior on OSX

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.

Signed-off-by: Tair Sabirgaliev <tair.sabirgaliev@gmail.com>
---
  gitk | 13 +++++++++++++
  1 file changed, 13 insertions(+)
Show changes to gitk +13 −0
diff --git a/gitk b/gitk
index 572f73f..60a87fc 100755
--- a/gitk
+++ b/gitk
@@ -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)
-- 
1.8.2
Paul Mackerras· Apr 20, 2013, 03:19 UTC · re: Tair Sabirgaliev · lore

Re: [PATCH] gitk: workaround Tcl/Tk Cmd-TAB behavior on OSX

On Thu, Apr 11, 2013 at 01:02:48AM +0600, Tair Sabirgaliev wrote:
Show 32 quoted lines
> 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.
> 
> Signed-off-by: Tair Sabirgaliev <tair.sabirgaliev@gmail.com>
> ---
>  gitk | 13 +++++++++++++
>  1 file changed, 13 insertions(+)
> 
> diff --git a/gitk b/gitk
> index 572f73f..60a87fc 100755
> --- a/gitk
> +++ b/gitk
> @@ -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    +    }
> +}

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.
Tair Sabirgaliev· Apr 20, 2013, 10:45 UTC · re: Paul Mackerras · lore

Re: [PATCH] gitk: workaround Tcl/Tk Cmd-TAB behavior on OSX

On Apr 20, 2013, at 9:19 AM, Paul Mackerras <paulus@samba.org> wrote:
Show 17 quoted lines
> On Thu, Apr 11, 2013 at 01:02:48AM +0600, Tair Sabirgaliev wrote:
>> 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.
> 
> 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.
Stefan Haller· Apr 23, 2013, 13:26 UTC · re: Tair Sabirgaliev · lore

Re: [PATCH] gitk: workaround Tcl/Tk Cmd-TAB behavior on OSX

Tair Sabirgaliev <tair.sabirgaliev@bee.kz> wrote:
Show 24 quoted lines
> 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.
> 
> Signed-off-by: Tair Sabirgaliev <tair.sabirgaliev@gmail.com>
> ---
>
>   +# 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    +    }
> +}
> +

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
-- 
Stefan Haller
Berlin, Germany
http://www.haller-berlin.de/
Tair Sabirgaliev· Apr 24, 2013, 09:48 UTC · re: Stefan Haller · lore

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.

Signed-off-by: Tair Sabirgaliev <tair.sabirgaliev@gmail.com>
Thanks-to: Stefan Haller <lists@haller-berlin.de>
---
 gitk | 9 +++++++++
 1 file changed, 9 insertions(+)
Show changes to gitk +9 −0
diff --git a/gitk b/gitk
index 572f73f..66e59b1 100755
--- a/gitk
+++ b/gitk
@@ -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)
-- 
1.8.2.1
Tair Sabirgaliev· Apr 11, 2013, 04:36 UTC · lore

Re: [PATCH] gitk: workaround Tcl/Tk Cmd-TAB behavior on OSX

On 4/11/13 8:46 AM, David Aguilar wrote:
Show 14 quoted lines
> 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?
>
> 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.

← back to recent threads