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?
.. in the SKUI library ?
Thomas Thomassen may have that functionality in the SKUI library.
[url]https://github.com/thomthom/SKUI[/url]
UPDATE - added with the 2021.1 release …
-
Sketchup::focus()
This is API module method is cross-platform. The dummy dialog hack never worked on the Mac platform.
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?
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.
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.
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.
EDIT (2019-01-28): SketchUp 2016 and higher add a space and the year to the window caption, so the code below will need to be edited to sniff out the version and add the year to the caption string.
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 ###
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 –).
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.
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.)
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
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:
Solved! Simple and dirty
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
Nice idea. I am surprised at how many workarounds for one issue we can come up with.
UPDATE - added with the 2021.1 release …
-
Sketchup::focus()
This API module method is cross-platform. The dummy dialog hack never worked on the Mac platform.
Very good news thanks!
Before I was using a solution which I found in this forum.
def activate
focus_sketchup_window
end
def html_properties
{
dialog_title: 'Focus',
resizable: false,
width: 10,
height: 10,
left: 0,
top: 0
}
end
def focus_sketchup_window
dlg = UI::HtmlDialog.new(html_properties)
dlg.set_html('<html></html>')
dlg.show
dlg.close
Sketchup.active_model.select_tool(nil)
end
Yes, but as said, that (dummy dialog) hack never worked on Mac platform.
The code I found works on my Mac Book Pro (15-inch 2019) and MacOS Big Sur with SketchUp 2021.
It has been said, that (on the Mac) if another window (rather than a document model window) has the focus, then the “hack” will just give the focus back to the previous window.