Home Download Buy Blog Forum Support

Retain focus on Sublime Text

Retain focus on Sublime Text

Postby erinata on Sun Apr 15, 2012 5:19 pm

in my package MarkdownBuild I try to use
Code: Select all
webbrowser.open("file://" + output.name)

to open an html file.

However, after opening the file in a web browser, the focus is shifted to the web browser.

Any idea on how to switch the focus back to Sublime Text?
erinata
 
Posts: 29
Joined: Wed Jan 25, 2012 12:19 pm

Re: Retain focus on Sublime Text

Postby bizoo on Sun Apr 15, 2012 6:25 pm

Didn't try it and only work (if it work at all) on Windows:
Code: Select all
import ctypes
ctypes.windll.user32.SetForegroundWindow(self.window.hwnd())
bizoo
 
Posts: 879
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Retain focus on Sublime Text

Postby erinata on Mon Apr 16, 2012 1:32 am

I've tried exactly the same thing but it didn't work.

I also try to import win32com ........and it says that there is no module named win32com

I don't mind if it only works on windows.....if there is a way to do it.
erinata
 
Posts: 29
Joined: Wed Jan 25, 2012 12:19 pm

Re: Retain focus on Sublime Text

Postby sublimator on Mon Apr 16, 2012 5:22 am

The webbrowser.open call is likely async ... such that your call to SetForegroundWindow happens before focus has even shifted ....

You likely want to use `sublime.set_timeout` to do the call at some point in the future ... the exact amount of ms depending a lot I guess on your computer ...

If you set a bunch of timeouts in 25ms increments you can be sure that it will happen fairly snappily ... worst case you get a few redundant SetForegroundWindow calls ...

Tweak the increments / num of timeouts to your hearts content - but overall much less of a wrist slasher than having to alt - tab ...
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 649
Joined: Thu Mar 20, 2008 5:41 am

Re: Retain focus on Sublime Text

Postby bizoo on Mon Apr 16, 2012 7:04 am

erinata wrote:I've tried exactly the same thing but it didn't work.

I also try to import win32com ........and it says that there is no module named win32com

I don't mind if it only works on windows.....if there is a way to do it.

Bringing a window in front is tricky in Windows, and look worst in Win7:
MS Documentation for SetForegroundWindow reads as follows:
"Windows NT 5.0 and later: An application cannot force a window to the foreground while the user is working with another window. Instead, SetForegroundWindow will activate the window (see SetActiveWindow) and call the FlashWindowEx function to notify the user."

So the only way I found is this, however I cannot put the focus back to ST2:
Code: Select all
import sublime, sublime_plugin
import ctypes, time

class ExampleCommand(sublime_plugin.WindowCommand):
    def run(self):
        time.sleep(5)
        # ctypes.windll.user32.BringWindowToTop(self.window.hwnd())
        ctypes.windll.user32.SwitchToThisWindow(self.window.hwnd(), 0)
        # ctypes.windll.user32.SetForegroundWindow(self.window.hwnd())
        # ctypes.windll.user32.SetActiveWindow(self.window.hwnd())
        # ctypes.windll.user32.SetFocus(self.window.hwnd())
        # ctypes.windll.user32.ShowWindow(self.window.hwnd(), 5)

Hope it help.
bizoo
 
Posts: 879
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Retain focus on Sublime Text

Postby sublimator on Mon Apr 16, 2012 7:27 am

"Windows NT 5.0 and later: An application cannot force a window to the foreground while the user is working with another window. Instead, SetForegroundWindow will activate the window (see SetActiveWindow) and call the FlashWindowEx function to notify the user."


IIRC the rules are different for subprocesses of the caller of SetForegroundWindow

ie. If you launch a subprocess from sublime, are focused there, and a set_timeout calls you back to sublime it should work ...

import sublime, webbrowser
from functools import partial
from ctypes import windll

def do():
"""

>>> from tester import do
>>> do()

"""

webbrowser.open(
"http://www.google.com.au")
sublime.set_timeout (partial( windll.user32.SetForegroundWindow,
sublime.active_window().hwnd()),
500 )


Bisect from 500 downwards towards and you'll converge on a point where it's just too slow.
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 649
Joined: Thu Mar 20, 2008 5:41 am

Re: Retain focus on Sublime Text

Postby sublimator on Mon Apr 16, 2012 7:28 am

err, too `quick` ;)
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 649
Joined: Thu Mar 20, 2008 5:41 am

Re: Retain focus on Sublime Text

Postby bizoo on Mon Apr 16, 2012 7:48 am

Yes you're right, focus work fine with your code.
I only have to change this line to make it work (on Win7 64):
Code: Select all
sublime.set_timeout(partial(ctypes.windll.user32.SwitchToThisWindow,
                                      sublime.active_window().hwnd(), 0), 500)

It don't work for me with SetForegroundWindow.
bizoo
 
Posts: 879
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Retain focus on Sublime Text

Postby sublimator on Mon Apr 16, 2012 7:57 am

It don't work for me with SetForegroundWindow.


Ah man, what a clusterfuck :) Yeah, I'm on Windows 7 x32

Queue the relentlessly branching code
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 649
Joined: Thu Mar 20, 2008 5:41 am

Re: Retain focus on Sublime Text

Postby sublimator on Mon Apr 16, 2012 8:13 am

[This function is not intended for general use. It may be altered or unavailable in subsequent versions of Windows.]


So this function prettily avoids all the rules that apply to SetForegroundWindow huh?

Seems to have been around since Win 2000 :)

Learn something new everyday
It is better to remain silent and be thought a fool, than to speak out and remove all doubt
sublimator
 
Posts: 649
Joined: Thu Mar 20, 2008 5:41 am

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 11 guests