Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

polymorphic tray icon loading #19

Merged
merged 2 commits into from
May 7, 2023
Merged

Conversation

overheadhunter
Copy link
Member

@overheadhunter overheadhunter commented May 2, 2023

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
Copy link
Contributor

purejava commented May 3, 2023

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
Member Author

Should have been accept(...).

@purejava
Copy link
Contributor

purejava commented May 6, 2023

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

@purejava
Copy link
Contributor

purejava commented May 6, 2023

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