toga icon indicating copy to clipboard operation
toga copied to clipboard

Add App.focus() method

Open raj921 opened this issue 1 year ago • 2 comments

This PR introduces a new focus() method to the App class that lets you programmatically bring the application into focus (#3032). The implementation depends on the platform:

macOS: Uses activateIgnoringOtherApps_. Windows: Calls Form.Activate(). GTK: Doesn't do anything (window managers manage focus). Mobile/Web: Also no-op. The method comes with a note in the docs to remind everyone that forcing focus isn't great UX and should only be used when necessary.

Why this change? It solves the problem of needing a way to programmatically focus the app, especially for certain workflows or edge cases.

PR Checklist: Tested the new feature. Updated the documentation. Read the CONTRIBUTING.md. Promise to follow the code of conduct.

raj921 avatar Dec 09 '24 09:12 raj921

According to this page, Form.Activate doesn't actually activate a background app, but only flashes the taskbar button. So this is apparently a feature which can only be implemented on one platform, and is highly discouraged on that one. That seems inconsistent with the Toga philosophy.

Other Microsoft sources:

  • https://learn.microsoft.com/en-gb/windows/win32/api/winuser/nf-winuser-setforegroundwindow
  • https://devblogs.microsoft.com/oldnewthing/20090220-00/?p=19083

mhsmith avatar Dec 10 '24 22:12 mhsmith

According to this page, Form.Activate doesn't actually activate a background app, but only flashes the taskbar button.

Form.Activate() may be the wrong (or incomplete) implementation - this SO answer suggests some other steps are needed.

Even if Activate is the right API, the implementation here likely needs some modification - it should be using current_window rather than main_window.

So this is apparently a feature which can only be implemented on one platform, and is highly discouraged on that one. That seems inconsistent with the Toga philosophy.

Agreed this is a weird one. My counterargument would be that from a high level, the concept has a clear expression, which is why the subject has come up more than once. However bad an idea it may be, people ask for it; and I suspect being able to say "Here's the API, but be aware it won't work in a lot of scenarios and that's the platform's fault" is a lot more effective as line of argument than trying to convince people they don't want to use a feature (cf "image button" discussions).

freakboy3742 avatar Dec 10 '24 23:12 freakboy3742

FYI: activateIgnoringOtherApps is going to get deprecated in macOS 26.0 (see https://developer.apple.com/documentation/appkit/nsapplication/activate(ignoringotherapps:)?language=objc ).

The new activate API is weird and only macOS 14.0+, and the Winforms solution isn't really satisfying. Windows is the only platform that supports glowing on the taskbar to request attention, and while that may be a good concept, it is non-existent on any other platform. Therefore my inclination is to discontinue any work on this.

@freakboy3742

johnzhou721 avatar Jun 19 '25 15:06 johnzhou721

The new activate API is weird and only macOS 14.0+, and the Winforms solution isn't really satisfying. Windows is the only platform that supports glowing on the taskbar to request attention, and while that may be a good concept, it is non-existent on any other platform. Therefore my inclination is to discontinue any work on this.

Given that the feature only potentially exists on macOS, and all signs point to it being less effective in the near future - on top of the fact that it's somewhere between "impossible" and "highly non-recommended" everywhere - I think I've come around to the idea of closing this feature request as WONTFIX.

freakboy3742 avatar Jun 20 '25 04:06 freakboy3742