-
Notifications
You must be signed in to change notification settings - Fork 3
Add native TrayMenuController implementation for macOS #120
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
Open
oc7o
wants to merge
5
commits into
cryptomator:develop
Choose a base branch
from
oc7o:feature/mac-tray-menu-controller
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 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 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
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 |
|---|---|---|
|
|
@@ -16,3 +16,6 @@ pom.xml.versionsBackup | |
| .idea/compiler.xml | ||
| .idea/jarRepositories.xml | ||
| *.iml | ||
|
|
||
| # MacOS | ||
| .DS_Store | ||
71 changes: 71 additions & 0 deletions
71
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
87 changes: 87 additions & 0 deletions
87
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,87 @@ | ||
| 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 java.util.List; | ||
| import java.util.function.Consumer; | ||
|
|
||
| @Priority(1000) | ||
| @OperatingSystem(OperatingSystem.Value.MAC) | ||
| public class MacTrayMenuController implements TrayMenuController { | ||
|
|
||
| @Override | ||
| public void showTrayIcon(Consumer<TrayIconLoader> iconLoader, Runnable defaultAction, String tooltip) throws TrayMenuException { | ||
| Native.INSTANCE.showTrayIcon(loadPng(iconLoader), tooltip, defaultAction); | ||
| } | ||
|
|
||
| @Override | ||
| public void updateTrayIcon(Consumer<TrayIconLoader> iconLoader) { | ||
| Native.INSTANCE.updateTrayIcon(loadPng(iconLoader)); | ||
| } | ||
|
|
||
| @Override | ||
| public void updateTrayMenu(List<TrayMenuItem> items) { | ||
| Native.INSTANCE.clearMenu(); | ||
| buildMenu(Native.ROOT_MENU, items); | ||
| } | ||
|
|
||
| @Override | ||
| public void onBeforeOpenMenu(Runnable listener) { | ||
| Native.INSTANCE.setBeforeOpenMenuListener(listener); | ||
| } | ||
|
|
||
| private static byte[] loadPng(Consumer<TrayIconLoader> iconLoader) { | ||
| byte[][] holder = {null}; | ||
| iconLoader.accept((TrayIconLoader.PngData) data -> holder[0] = data); | ||
| if (holder[0] == null) { | ||
| throw new IllegalStateException("Icon loader did not provide PNG data"); | ||
| } | ||
| return holder[0]; | ||
| } | ||
|
oc7o marked this conversation as resolved.
|
||
|
|
||
| private 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()); | ||
| case SeparatorItem ignored -> Native.INSTANCE.addSeparator(menuHandle); | ||
| case SubMenuItem s -> { | ||
| long submenuHandle = Native.INSTANCE.addSubMenuItem(menuHandle, s.title()); | ||
| buildMenu(submenuHandle, s.items()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static final class Native { | ||
|
|
||
| static final long ROOT_MENU = 0L; | ||
| static final Native INSTANCE = new Native(); | ||
|
|
||
| private Native() { | ||
| NativeLibLoader.loadLib(); | ||
| } | ||
|
|
||
| native void showTrayIcon(byte[] pngData, String tooltip, Runnable defaultAction); | ||
|
|
||
| native void 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.
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.
Uh oh!
There was an error while loading. Please reload this page.