JDK-8290844 : Add Skin.install() method
  • Type: Enhancement
  • Component: javafx
  • Sub-Component: controls
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2022-07-21
  • Updated: 2022-11-29
  • Resolved: 2022-11-03
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
openjfx20 b06Fixed
Related Reports
Blocks :  
Blocks :  
CSR :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Relates :  
Description
Summary
-------

Introduce a new Skin.install() method with an empty default implementation.  Modify Control.setSkin(Skin) implementation to invoke install() on the new skin after the old skin has been removed with dispose().


Problem
-------

Presently, switching skins is a two-step process: first, a new skin is constructed against the target Control instance, and is attached (i.s. listeners added, child nodes added) to that instance in the constructor.  Then, Control.setSkin() is invoked with a new skin - and inside, the old skin is detached via its dispose() method.  

This creates two problems:

 1. if the new skin instance is discarded before setSkin(), it remains attached, leaving the control in a weird state with two skins attached, causing memory leaks and performance degradation.

 2. if, in addition to adding listeners and child nodes, the skin sets a property, such as an event listener, or a handler, it overwrites the current value irreversibly.  As a result, either the old skin would not be able to cleanly remove itself, or the new skin would not be able to set the new values, as it does not know whether it should overwrite or keep a handler installed earlier (possibly by design).  Unsurprisingly, this also might cause memory leaks.

A number of related bugs have been collected under JDK-8241364 ☂ Cleanup skin implementations to allow switching, which refers a number of bugs:

JDK-8245145 Spinner: throws IllegalArgumentException when replacing skin
JDK-8245303 InputMap: memory leak due to incomplete cleanup on remove mapping
JDK-8268877 TextInputControlSkin: incorrect inputMethod event handler after switching skin



Solution
--------

This problem does not exist in e.g. Swing because the steps of instantiation, uninstalling the old ComponentUI ("skin"), and installing a new skin are cleanly separated.  ComponentUI constructor does not alter the component itself, ComponentUI.uninstallUI(JComponent) cleanly removes the old skin, ComponentUI.installUI(JComponent) installs the new skin.  We should follow the same model in javafx.

Specifically, I'd like to propose the following changes:

 1. Add Skin.install() with a default no-op implementation.
 2. Clarify skin life cycle (creation-attachment-detachment sequence) in Skin and Skin.install() javadoc
 3. Modify Control.setSkin(Skin) method (== invalidate listener in skin property) to call oldSkin.dispose() followed by newSkin.install()
 4. Check whether (newSkin.getSkinnable() == this) inside of Control.setSkin(Skin) property handler, and throw an Error if it isn't so (when newSkin is != null of course).
 5. Many existing skins that do not set properties in the corresponding control may remain unchanged.  The skins that do, such as TextInputControlSkin (JDK-8268877), must be refactored to utilize the new install() method.  I think the refactoring would simply move all the code that accesses its control instance away from the constructor to install().


Impact Analysis
-------------

see corresponding CSR JDK-8292213 


Specification
-------------
see corresponding CSR JDK-8292213 
Comments
Changeset: 7f17447a Author: Andy Goryachev <angorya@openjdk.org> Committer: Kevin Rushforth <kcr@openjdk.org> Date: 2022-11-03 16:39:55 +0000 URL: https://git.openjdk.org/jfx/commit/7f17447aa44fbf5b09aaa2b699266dac8b50cea1
03-11-2022

[~jvos] : I suspect you might be using custom skins at gluon - would you mind taking a look at this? PR: https://github.com/openjdk/jfx/pull/845
29-09-2022

A pull request was submitted for review. URL: https://git.openjdk.org/jfx/pull/845 Date: 2022-07-22 19:22:48 +0000
08-08-2022