SketchUp / api-issue-tracker

Public issue tracker for the SketchUp and LayOut's APIs
https://developer.sketchup.com/
39 stars 10 forks source link

Sketchup.focus() does not work on Windows #899

Closed J-E-L closed 10 months ago

J-E-L commented 1 year ago

Bug Report

Sketchup Version 2023

OS: Windows 10 and 11

ruby: Funktion "Sketchup.focus()" does nor work.

A Ruby Tool can not Display view.tooltip = "message" after return from other Application

A small tool that works with Sketchup 2022 and not with Sketchup 2023

class TestFocusTool
    def onMouseMove(flags, x, y, view)
        Sketchup.focus()
        ph = view.pick_helper
        ph.do_pick x,y, 10 * UI.scale_factor
        best_picked = ph.best_picked
        if best_picked
            tooltip = "class: #{best_picked.class}"
            view.tooltip = tooltip
        end        
    end
end

Sketchup.active_model.select_tool(TestFocusTool.new)
J-E-L commented 1 year ago

A small tool that works with Sketchup 2022 and not with Sketchup 2023

class TestFocusTool
    def onMouseMove(flags, x, y, view)
        Sketchup.focus()
        ph = view.pick_helper
        ph.do_pick x,y, 10 * UI.scale_factor
        best_picked = ph.best_picked
        if best_picked
            tooltip = "class: #{best_picked.class}"
            view.tooltip = tooltip
        end        
    end
end

Sketchup.active_model.select_tool(TestFocusTool.new)
DanRathbun commented 1 year ago

First of all, you should not be calling Sketchup::focus each and every time that the mouse moves. This is going to potentially cause extreme delays as it's likely that the SketchUp application interface will be updated (ie, all the inspector dialogs updated and redrawn, etc.)

Find another place to call the focusing for the SketchUp application. Please include (in the original post above) the use case for calling Sketchup::focus that you think does not work.

Secondly, I think that the bot needs the reproducible code in the opening post (as the template asks.)

... that works with Sketchup 2022 ... and not with Sketchup 2023

Are you aware that SketchUp 2023 was migrated from the old MFC framework to the cross-platform Qt library for SketchUp's GUI?

Yes, there are still some GUI bugs that made it by this migration, some are the fault of the version of the Qt library, some are just booboos that slipped through the cracks.

The goal was parity with the old pre23 GUI interface.

A Ruby Tool can not Display view.tooltip = "message" after return from other Application.

FYI, Sketchup::focus cannot give total focus back to the SketchUp application (as far as I know,) nor can say calling the Windows Scripting Hosts AppActivate method. What happens is that Windows will flash the SketchUp application's taskbar icon to notify the user that the application desires to have the focus.

This is a security feature built into Windows to try to prevent malware. This user must manually give true focus back to the SketchUp application if another application has the focus.


So, is the issue really with the display of the tooltip ?

If so then a call to view.invalidate may be in order if the tooltip is changed.

For example, the following tool code will display the tooltip for me even though another application on another display (ie, the Chrome browser showing a page from the API documentation) has the focus ...

class TestFocusTool
  def onMouseMove(flags, x, y, view)
    ph = view.pick_helper
    ph.do_pick x,y, 10 * UI.scale_factor
    best_picked = ph.best_picked
    if best_picked
      view.tooltip = "class: #{best_picked.class}"
      view.invalidate
    end
  end
end
J-E-L commented 1 year ago

A more detailed example of the focus Bug. Check out the attached video!

The bug does not occur regularly

class TestFocusTool

    def onMouseEnter(view)
        Sketchup.focus()
        puts "onMouseEnter"
    end

    def onMouseMove(flags, x, y, view)
        ph = view.pick_helper
        ph.do_pick x,y, 10 * UI.scale_factor
        best_picked = ph.best_picked
        if best_picked
            tooltip = "class: #{best_picked.class}"
            view.tooltip = tooltip
            Sketchup.status_text = tooltip
            view.invalidate
        end        
    end
end

Sketchup.active_model.select_tool(TestFocusTool.new)

https://github.com/SketchUp/api-issue-tracker/assets/5636213/c691215c-60ab-4085-9d90-6757954db320

thomthom commented 1 year ago

The usage of Sketchup.focus in this is a workaround another issue then? The tooltip from the tool.

I'm seeing issues with the tooltip if Sketchup.focus is being used. But the tooltip is working fine for me when Sketchup.focus is not used.

I'm not sure why the viewport tooltip isn't showing after Sketchup.focus. That's one thing to look into. But I'm not seeing why Sketchup.focus is used at all in this case?

J-E-L commented 1 year ago

The problem shows up as follows. If I activate a web dialog and then move the mouse over the model window, the tooltip no longer works. Up to and including sketchup 2021 I used the following c code as a workaround

VALUE method_other_setWindowFocus() {
#if defined(WIN32) 
    ::POINT mouse_position = {};
    if (::GetCursorPos(&mouse_position)) {
        SetFocus(::WindowFromPoint(mouse_position));
        return Qtrue;
    } else {
        return Qfalse;
    }
#elif defined(__APPLE__) 

    return Qfalse;
#endif 

}

With Sketchup 2022 you have implemented "Sketchup.focus()"

As of Sketchup 2023, the above features no longer work.

I found another workaround.

 class DummiTool
 end
 def onMouseEnter(view)
     @tool = DummiTool.new unless @tool
     tools = Sketchup.active_model.tools
     tools.push_tool @tool
     tools.pop_tool
 end

I just want to report a bug!

DanRathbun commented 1 year ago

@thomthom I'm seeing issues with the tooltip if Sketchup.focus is being used. But the tooltip is working fine for me when Sketchup.focus is not used.

Same, same.

@J-E-L I just want to report a bug!

Yes there are focusing bugs between the model viewport, inspector panels and trays and html dialogs that snuck by the migration to Qt.

The Sketchup::focus method was implemented especially for web dialogs so that say clicking a button in the dialog could give focus back to the modeling viewport, without having to close the dialog. Kind of like how toolbars work. In fact, some of the proponents had implemented "monster toolpanels" using web dialogs and complained that they could not give focus back to SketchUp without closing their dialog.

@J-E-L I found another workaround.

Interesting workaround. I'm working on a focusing "proof of concept" extension and this workaround may come in handy.

So, your extension is seeking to give focus back to SketchUp when the mouse is moved into the modeling area ? My "proof of concept" is meant to work on ALL web dialogs for ALL extensions ... causing the "focus to follow mouse".

This concept project came from discussion in: Issue 889 : Ruby Windows Consume Keystrokes When Focused - Focus Does Not Follow Mouse

It this project, I am going to code an interface that allows the user to switch on and off auto-focusing for specific dialogs according to their caption text. (Say because it is not needed or in your case it's extension already does this.)

sketchup[bot] commented 1 year ago

Logged as: SKEXT-3711

thomthom commented 1 year ago

The focus appear to be set on the application. However, I am seeing the tooltips not always showing up.

J-E-L commented 1 year ago

The Sketchup::focus method is working on OS X. Maybe the focus has to set to the rendering window.

DanRathbun commented 1 year ago

@thomthom The focus appear to be set on the application. However, I am seeing the tooltips not always showing up.

Should this issue title be edited to better reflect what the bug is and what will be fixed?