|
| 1 | +package org.cryptomator.linux.tray; |
| 2 | + |
| 3 | +import org.cryptomator.integrations.common.CheckAvailability; |
| 4 | +import org.cryptomator.integrations.common.OperatingSystem; |
| 5 | +import org.cryptomator.integrations.common.Priority; |
| 6 | +import org.cryptomator.integrations.tray.ActionItem; |
| 7 | +import org.cryptomator.integrations.tray.SeparatorItem; |
| 8 | +import org.cryptomator.integrations.tray.SubMenuItem; |
| 9 | +import org.cryptomator.integrations.tray.TrayIconLoader; |
| 10 | +import org.cryptomator.integrations.tray.TrayMenuController; |
| 11 | +import org.cryptomator.integrations.tray.TrayMenuException; |
| 12 | +import org.cryptomator.integrations.tray.TrayMenuItem; |
| 13 | +import org.purejava.appindicator.GCallback; |
| 14 | +import org.purejava.appindicator.MemoryAllocator; |
| 15 | + |
| 16 | +import java.lang.foreign.Arena; |
| 17 | +import java.lang.foreign.MemorySegment; |
| 18 | +import java.lang.foreign.SegmentScope; |
| 19 | +import java.util.List; |
| 20 | +import java.util.function.Consumer; |
| 21 | + |
| 22 | +import static org.purejava.appindicator.app_indicator_h.*; |
| 23 | + |
| 24 | +@Priority(1000) |
| 25 | +@OperatingSystem(OperatingSystem.Value.LINUX) |
| 26 | +public class AppindicatorTrayMenuController implements TrayMenuController { |
| 27 | + |
| 28 | + private static final String APP_INDICATOR_ID = "org.cryptomator.Cryptomator"; |
| 29 | + |
| 30 | + private static final SegmentScope SCOPE = SegmentScope.global(); |
| 31 | + private MemorySegment indicator; |
| 32 | + private MemorySegment menu = gtk_menu_new(); |
| 33 | + |
| 34 | + @CheckAvailability |
| 35 | + public static boolean isAvailable() { |
| 36 | + return MemoryAllocator.isLoadedNativeLib(); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void showTrayIcon(Consumer<TrayIconLoader> iconLoader, Runnable runnable, String s) throws TrayMenuException { |
| 41 | + TrayIconLoader.FreedesktopIconName callback = this::showTrayIconWithSVG; |
| 42 | + iconLoader.accept(callback); |
| 43 | + gtk_widget_show_all(menu); |
| 44 | + app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE()); |
| 45 | + } |
| 46 | + |
| 47 | + private void showTrayIconWithSVG(String s) { |
| 48 | + try (var arena = Arena.openConfined()) { |
| 49 | + indicator = app_indicator_new(arena.allocateUtf8String(APP_INDICATOR_ID), |
| 50 | + arena.allocateUtf8String(s), |
| 51 | + APP_INDICATOR_CATEGORY_APPLICATION_STATUS()); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void updateTrayIcon(Consumer<TrayIconLoader> iconLoader) { |
| 57 | + TrayIconLoader.FreedesktopIconName callback = this::updateTrayIconWithSVG; |
| 58 | + iconLoader.accept(callback); |
| 59 | + } |
| 60 | + |
| 61 | + private void updateTrayIconWithSVG(String s) { |
| 62 | + try (var arena = Arena.openConfined()) { |
| 63 | + app_indicator_set_icon(indicator, arena.allocateUtf8String(s)); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void updateTrayMenu(List<TrayMenuItem> items) throws TrayMenuException { |
| 69 | + menu = gtk_menu_new(); |
| 70 | + addChildren(menu, items); |
| 71 | + gtk_widget_show_all(menu); |
| 72 | + app_indicator_set_menu(indicator, menu); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void onBeforeOpenMenu(Runnable runnable) { |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + private void addChildren(MemorySegment menu, List<TrayMenuItem> items) { |
| 81 | + for (var item : items) { |
| 82 | + switch (item) { |
| 83 | + case ActionItem a -> { |
| 84 | + var gtkMenuItem = gtk_menu_item_new(); |
| 85 | + try (var arena = Arena.openConfined()) { |
| 86 | + gtk_menu_item_set_label(gtkMenuItem, arena.allocateUtf8String(a.title())); |
| 87 | + g_signal_connect_object(gtkMenuItem, |
| 88 | + arena.allocateUtf8String("activate"), |
| 89 | + GCallback.allocate(new ActionItemCallback(a), SCOPE), |
| 90 | + menu, |
| 91 | + 0); |
| 92 | + } |
| 93 | + gtk_menu_shell_append(menu, gtkMenuItem); |
| 94 | + } |
| 95 | + case SeparatorItem separatorItem -> { |
| 96 | + var gtkSeparator = gtk_menu_item_new(); |
| 97 | + gtk_menu_shell_append(menu, gtkSeparator); |
| 98 | + } |
| 99 | + case SubMenuItem s -> { |
| 100 | + var gtkMenuItem = gtk_menu_item_new(); |
| 101 | + var gtkSubmenu = gtk_menu_new(); |
| 102 | + try (var arena = Arena.openConfined()) { |
| 103 | + gtk_menu_item_set_label(gtkMenuItem, arena.allocateUtf8String(s.title())); |
| 104 | + } |
| 105 | + addChildren(gtkSubmenu, s.items()); |
| 106 | + gtk_menu_item_set_submenu(gtkMenuItem, gtkSubmenu); |
| 107 | + gtk_menu_shell_append(menu, gtkMenuItem); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments