How to set Focus to Mainwindow


#1

Hello,
I have a UI::WebDialog. How can I set the focus on Sketchup MainWindow with a mouse click on a button in my WebDialog without closing the WebDialog?


#2

Thomas Thomassen may have that functionality in the SKUI library.
https://github.com/thomthom/SKUI

@tt_su


#3

There is no method in the API to do this.
What is the use case (what does the button do)? Normally one does not create a button that does nothing more than move the focus, so if we knew in what context you want to do this, maybe there is a work-around or a better UX design?


#4

But it is a long-standing API feature request. Jim had this issue a long time ago with his button command panels (implemented as WebDialogs.)

I know of a PC workaround using either WIN32OLE or Win32API calls. But I’m at a loss for the Mac.


#5

It starts a custom UI:Tool. I have to click in the MainWindow first for capture mousemove events.

I will try it. This tool working on Windows only.
API call would be fine.


#6

No guarantees. English only. Window caption text may vary per language edition.

This code would be inside your class or module.
The Ruby standard library must be availble with “win32ole.so” file loadable. (This is provided with SketchUp 2014+. It gets complicated with older SketchUp versions.)

The SketchUp application window caption may have changed over time, see Doc: Sketchup::app_name

Could not find my old snippet, so I typed this out just a few minutes ago.

EDIT: Tested and works on SU2015 Pro with “Untitled” and titled model file.


require "win32ole"

def activate_window( caption = get_caption() )
  #
  wsh = WIN32OLE.new("WScript.Shell")
  wsh.AppActivate caption
  #
end ###

def get_caption()
  #
  if Sketchup.active_model.path.empty? # new model
    caption = "Untitled - SketchUp"
  else
    filename = File.basename(Sketchup.active_model.path)
    if Sketchup.is_pro?
      caption = "#{filename} - SketchUp Pro"
    else
      caption = "#{filename} - SketchUp"
    end
  end
  #
end ###

:bulb:


Ref: MSDN: Windows Script Host - AppActivate Method


#7

Mousemove events should be captured just by hovering the main SketchUp window, independently from whether it has focus. If you mean keypresses, one can let the webdialog’s javascript listen for key events and pass them to Ruby.

Using external dependencies should not be the first means to solve a challenge/problem. Using only the official API, you can (somewhat) rely on that your extension will work in all installations of SketchUp on all platforms because SketchUp ships all you need. An external dependency (as well as unsupported methods) may fail due to anything in the environment outside of SketchUp, about which you take assumptions. It can potentially give a support nightmare.

Edit: I don’t mean to steal a joy, just stay aware of the conditions under which your extension will work.
Such assumptions can for example be string synthesis (like how the OS formats window titles), language (the above example can work on English systems) and typography (a hyphen - is different from a dash ).


#8

Fixed two things in the example:

Fixed the active_window() method name, was supposed to be named activate_window().

In the second method, the application module is named Sketchup, not SketchUp, so fixed the call to the is_pro? module function.

[quote=“Aerilius, post:7, topic:12383”]
Such assumptions can for example be string synthesis (like how the OS formats window titles), language (the above example can work on English systems) and typography (a hyphen - is different from a dash –).[/quote]
Which is exactly why we have begging for for years for an API module function to set the SketchUp application window to have the focus, regardless of platform, language build, or SketchUp edition (Pro / Make.)


#9

Thanks! I will test it.

Yes. But for Zoom with mouse Scrollwheel i need Focus on ManWindow.
Thanks for Tipps and Code! I will test it and i have an other Idea.

Edit: My extension is for Windows only, because it calling a exe written in C++ and use Sketchup C++ API


#10

Thomas Thomassen has a function that does this in his TT_Lib2 library. (But it use Win32API system calls that are not dependant upon correct language caption.):
TT::SketchUp::activate_main_window()


Discussions & code samples over at the SketchUcation forums:

Re: Web dialogs stealing focus within my tool.

Blurring the webdialog on Mac


#11

Solved! Simple and dirty :smile:

def FocusMainWindow()
  #create and close a dummie Webdialog
  dummieDlg = UI::WebDialog.new("dummie", true, "dummie", 0, 0, 10000, 10000, true)
  dummieDlg.show()
  dummieDlg.close()
end

Thanks @all


#12

Nice idea. I am surprised at how many workarounds for one issue we can come up with.

:thumbsup: