blish-hud / Blish-HUD

A Guild Wars 2 overlay with extreme extensibility through compiled modules.
https://blishhud.com
MIT License
311 stars 60 forks source link

Module SettingsView.Unload() not called when switching between Module Settings tab and other tabs of the blish settings window #956

Open Taschenbuch opened 3 months ago

Taschenbuch commented 3 months ago

occurs in Blish 1.1.1

observed behavior

View.Unload() method is never called on the custom settings view created by overriding Module.GetSettingsView() when switching between the Module Settings tab and other tabs of the blish settings window. As a consequence when in View.Build() an event is subscribed to, it cannot be unsubscribed in View.Unload(). This leads to an additional event subscription each time the module settings tab is selected.

Example:

Module class

public override IView GetSettingsView()
{
    return new ModuleSettingsView(_service);
}

ModuleSettingsView class

public class ModuleSettingsView : View
{
    public ModuleSettingsView(Service service)
    {
        _service = service;
    }

    protected override async void Build(Container buildPanel) // called each time the module settings tab is opened
    {
        _service.MyEvent += OnMyEvent;
    }

    protected override void Unload() // never called
    {
        _service.MyEvent -= OnMyEvent;
    }
}

expected behavior

View.Unload() is called when

workaround until fixed

Module class

private ModuleSettingsView _moduleSettingsView;

public override IView GetSettingsView()
{
    _moduleSettingsView ??= new ModuleSettingsView(_service); 
    return _moduleSettingsView;
}

ModuleSettingsView class

public class ModuleSettingsView : View
{
    public ModuleSettingsView(Service service)
    {
        _service = service;
    }

    protected override async void Build(Container buildPanel) // called each time the module settings tab is opened
    {
        _service.MyEvent -= OnMyEvent;
        _service.MyEvent += OnMyEvent;
    }
}

relevant discord discussion:

https://discord.com/channels/531175899588984842/599270434642460753/1227691731231838360