Repository navigation
Feature: Mac tray menu controller #135
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5e1b580
Add native TrayMenuController implementation for macOS
oc7o 0b46807
Add null guard to loadPng function
oc7o 7d0af14
Add comment explaining why the defaultAction parameter is unused
oc7o 54dddd3
Log error to stderr for better debugging
oc7o cd9c435
removed obsolete availability check as suggested in PR
oc7o bc54039
autoreleasepool, thread safety, global Runnable, and minor fixes
oc7o cb5ad23
Remove autoreleasepools that guard no autoreleased objects
oc7o 0b72ee5
Merge remote-tracking branch 'upstream/develop' into feature/mac-tray…
oc7o 34372bc
Harden native tray menu: ownership, contract, and atomic rebuild
oc7o 58757e4
Fixed blank menu entry if name contains emoji
infeo d2bb783
propagate java errors to lib consumer
infeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
src/main/headers/org_cryptomator_macos_tray_MacTrayMenuController_Native.h
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
src/main/java/org/cryptomator/macos/tray/MacTrayMenuController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| package org.cryptomator.macos.tray; | ||
|
|
||
| import org.cryptomator.integrations.common.OperatingSystem; | ||
| import org.cryptomator.integrations.common.Priority; | ||
| import org.cryptomator.integrations.tray.ActionItem; | ||
| import org.cryptomator.integrations.tray.SeparatorItem; | ||
| import org.cryptomator.integrations.tray.SubMenuItem; | ||
| import org.cryptomator.integrations.tray.TrayIconLoader; | ||
| import org.cryptomator.integrations.tray.TrayMenuController; | ||
| import org.cryptomator.integrations.tray.TrayMenuException; | ||
| import org.cryptomator.integrations.tray.TrayMenuItem; | ||
| import org.cryptomator.macos.common.NativeLibLoader; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.List; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.function.Consumer; | ||
|
|
||
| @Priority(1000) | ||
| @OperatingSystem(OperatingSystem.Value.MAC) | ||
| public class MacTrayMenuController implements TrayMenuController { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(MacTrayMenuController.class); | ||
|
|
||
| private volatile boolean iconShown; | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * <p> | ||
| * {@code defaultAction} is ignored, since clicking a macOS status item always opens its menu. | ||
| */ | ||
| @Override | ||
| public void showTrayIcon(Consumer<TrayIconLoader> iconLoader, Runnable defaultAction, String tooltip) throws TrayMenuException { | ||
| byte[] png = loadPng(iconLoader); | ||
| if (png == null) { | ||
| throw new TrayMenuException("Icon loader did not provide PNG data"); | ||
| } | ||
| if (!Native.INSTANCE.showTrayIcon(png, tooltip)) { | ||
| throw new TrayMenuException("Failed to add icon to status bar"); | ||
| } | ||
|
infeo marked this conversation as resolved.
Resolved
|
||
| iconShown = true; | ||
| } | ||
|
|
||
| @Override | ||
| public void updateTrayIcon(Consumer<TrayIconLoader> iconLoader) { | ||
| if (!iconShown) { | ||
| throw new IllegalStateException("Failed to update the icon as it has not yet been added"); | ||
| } | ||
| byte[] png = loadPng(iconLoader); | ||
| if (png == null || !Native.INSTANCE.updateTrayIcon(png)) { | ||
| LOG.warn("Failed to update tray icon, keeping the previous one"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void updateTrayMenu(List<TrayMenuItem> items) { | ||
| // Rebuilding within a single main thread hop keeps concurrent rebuilds and | ||
| // menu tracking from interleaving with a half-built menu. | ||
| runOnMainThread(() -> { | ||
| Native.INSTANCE.clearMenu(); | ||
| buildMenu(Native.ROOT_MENU, items); | ||
| }); | ||
| } | ||
|
|
||
| @Override | ||
| public void onBeforeOpenMenu(Runnable listener) { | ||
| if (!iconShown) { | ||
| throw new IllegalStateException("Failed to add listener as there is no tray menu"); | ||
| } | ||
| Native.INSTANCE.setBeforeOpenMenuListener(logExceptions(listener)); | ||
| } | ||
|
|
||
| private static byte[] loadPng(Consumer<TrayIconLoader> iconLoader) { | ||
| var png = new AtomicReference<byte[]>(); | ||
| iconLoader.accept((TrayIconLoader.PngData) png::set); | ||
| return png.get(); | ||
| } | ||
|
|
||
| private static void buildMenu(long menuHandle, List<TrayMenuItem> items) { | ||
| for (var item : items) { | ||
| switch (item) { | ||
| case ActionItem a -> Native.INSTANCE.addActionItem(menuHandle, a.title(), a.enabled(), a.action() == null ? null : logExceptions(a.action())); | ||
| case SeparatorItem ignored -> Native.INSTANCE.addSeparator(menuHandle); | ||
| case SubMenuItem s -> { | ||
| long submenuHandle = Native.INSTANCE.addSubMenuItem(menuHandle, s.title()); | ||
| buildMenu(submenuHandle, s.items()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void runOnMainThread(Runnable task) { | ||
| // Errors are captured too, since the native side would otherwise print and clear them. | ||
| var failure = new AtomicReference<Throwable>(); | ||
| Native.INSTANCE.runOnMainThread(() -> { | ||
| try { | ||
| task.run(); | ||
| } catch (RuntimeException | Error e) { | ||
| failure.set(e); | ||
| } | ||
| }); | ||
| switch (failure.get()) { | ||
| case null -> {} | ||
| case RuntimeException e -> throw e; | ||
| case Error e -> throw e; | ||
| default -> throw new IllegalStateException("Unexpected checked exception", failure.get()); | ||
| } | ||
| } | ||
|
|
||
| private static Runnable logExceptions(Runnable callback) { | ||
| return () -> { | ||
| try { | ||
| callback.run(); | ||
| } catch (RuntimeException e) { | ||
| LOG.error("Tray menu callback failed", e); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private static final class Native { | ||
|
|
||
| static final long ROOT_MENU = 0L; | ||
| static final Native INSTANCE = new Native(); | ||
|
|
||
| private Native() { | ||
| NativeLibLoader.loadLib(); | ||
| } | ||
|
|
||
| native void runOnMainThread(Runnable task); | ||
|
|
||
| native boolean showTrayIcon(byte[] pngData, String tooltip); | ||
|
|
||
| native boolean updateTrayIcon(byte[] pngData); | ||
|
|
||
| native void clearMenu(); | ||
|
|
||
| native void addActionItem(long menuHandle, String title, boolean enabled, Runnable action); | ||
|
|
||
| native void addSeparator(long menuHandle); | ||
|
|
||
| native long addSubMenuItem(long menuHandle, String title); | ||
|
|
||
| native void setBeforeOpenMenuListener(Runnable listener); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: cryptomator/integrations-mac
Length of output: 22090
Script executed:
Repository: cryptomator/integrations-mac
Length of output: 8373
Translate native linkage failures to
TrayMenuException.If the native library cannot load,
Native.INSTANCE.showTrayIconcan throwUnsatisfiedLinkError. Callers that handle the documentedTrayMenuExceptioncannot recover from that failure.Suggested fix