Skip to content

polymorphic tray icon loading#19

Merged
overheadhunter merged 2 commits into
developfrom
feature/tray-icon-loader
May 7, 2023
Merged

polymorphic tray icon loading#19
overheadhunter merged 2 commits into
developfrom
feature/tray-icon-loader

Conversation

@overheadhunter

@overheadhunter overheadhunter commented May 2, 2023

Copy link
Copy Markdown
Member

This is an attempt to solve the problem mentioned in #17 (comment):

Using sealed classes and IoC, we can now let the implementation decide, which kind of icon it needs, e.g.

class PngConsumingTrayMenuController implements TrayMenuController {
    // ...
    public void updateTrayIcon(Consumer<TrayIconLoader> iconLoader) {
        TrayIconLoader.PngData callback = this::updateTrayIconWithPngData;
        iconLoader.accept(callback);
    }
    
    private void updateTrayIconWithPngData(byte[] data) {
        // ...
    }
    // ...
}

On the consumer side, invocation would look like this:

// with JEP 433:
updateTrayIcon(loader -> {
	switch (loader) {
		case TrayIconLoader.PngData l -> l.loadPng(new byte[0]);
		case TrayIconLoader.FreedesktopIconName l -> l.lookupByName("foo");
	}
});

// without JEP 433:
updateTrayIcon(loader -> {
	if (loader instanceof TrayIconLoader.PngData l) {
		l.loadPng(new byte[0]);
	} else if (loader instanceof TrayIconLoader.FreedesktopIconName l) {
		l.lookupByName("");
	}
});

Note that the consumer is only required to implement loaders permitted by the sealed interface, so there can only be as many required implementation as the API allows:

sealed public interface TrayIconLoader permits TrayIconLoader.PngData, TrayIconLoader.FreedesktopIconName {

When the API is updated, new implementations can be added without breaking previous ones. With JEP 433, one will even get a compiler error due to exhaustiveness checks, if an implementation is missing.

@purejava

purejava commented May 3, 2023

Copy link
Copy Markdown
Contributor

Sophisticated approach! Something seems to be missing though ...
I could not fully implement it in integrations-linux. Error is:

Bildschirmfoto 2023-05-03 um 11 17 50

I commited all changes so far to integrations-linux, including this half-way implemented PR.

Edit: typo.

@overheadhunter

Copy link
Copy Markdown
Member Author

Should have been accept(...).

@purejava

purejava commented May 6, 2023

Copy link
Copy Markdown
Contributor

Polymorphic tray icon loading implemented.
This does work on Linux. Will test it on Mac later this day.

@purejava

purejava commented May 6, 2023

Copy link
Copy Markdown
Contributor

Mac (AwtTrayMenuController) is ok. Had two minor issues there that are fixed and commited now too.

@overheadhunter overheadhunter merged commit 5b43e50 into develop May 7, 2023
@overheadhunter overheadhunter added this to the 1.3.0 milestone May 7, 2023
@overheadhunter overheadhunter deleted the feature/tray-icon-loader branch July 5, 2023 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants