Skip to content

Commit

Permalink
Merge pull request #18 from purejava/libappindicator
Browse files Browse the repository at this point in the history
appindicator support
  • Loading branch information
infeo authored May 10, 2023
2 parents e5928a0 + 78a3e42 commit eef050b
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 19
distribution: 'zulu'
java-version: 20
cache: 'maven'
- name: Ensure to use tagged version
if: startsWith(github.ref, 'refs/tags/')
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ jobs:
fetch-depth: 2
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 19
distribution: 'zulu'
java-version: 20
cache: 'maven'
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-central.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
ref: "refs/tags/${{ github.event.inputs.tag }}"
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 19
distribution: 'zulu'
java-version: 20
cache: 'maven'
server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml
server-username: MAVEN_USERNAME # env variable for username in deploy
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-github.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 19
distribution: 'zulu'
java-version: 20
cache: 'maven'
gpg-private-key: ${{ secrets.RELEASES_GPG_PRIVATE_KEY }} # Value of the GPG private key to import
gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase
Expand Down
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 22 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,17 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.jdk.version>19</project.jdk.version>
<project.jdk.version>20</project.jdk.version>

<!-- runtime dependencies -->

<api.version>1.2.0</api.version>
<api.version>1.3.0-beta1</api.version>
<secret-service.version>1.8.1-jdk17</secret-service.version>
<kdewallet.version>1.2.8</kdewallet.version>
<kdewallet.version>1.3.0</kdewallet.version>
<appindicator.version>1.3.0</appindicator.version>
<guava.version>31.1-jre</guava.version>
<slf4j.version>1.7.36</slf4j.version>
<commons-lang3.version>3.12.0</commons-lang3.version>

<!-- test dependencies -->
<junit.version>5.8.2</junit.version>
Expand Down Expand Up @@ -80,6 +82,19 @@
<artifactId>kdewallet</artifactId>
<version>${kdewallet.version}</version>
</dependency>
<!-- Apache Commons -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- Java bindings for appindicator -->
<dependency>
<groupId>org.purejava</groupId>
<artifactId>appindicator-gtk3-java</artifactId>
<classifier>libayatana-appindicator-libappindicator-minimal</classifier>
<version>${appindicator.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand All @@ -96,6 +111,9 @@
<version>3.9.0</version>
<configuration>
<release>${project.jdk.version}</release>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -190,6 +208,7 @@
<name>see</name>
</tag>
</tags>
<additionalOptions>--enable-preview</additionalOptions>
</configuration>
</plugin>
</plugins>
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import org.cryptomator.integrations.keychain.KeychainAccessProvider;
import org.cryptomator.integrations.revealpath.RevealPathService;
import org.cryptomator.integrations.tray.TrayMenuController;
import org.cryptomator.linux.keychain.SecretServiceKeychainAccess;
import org.cryptomator.linux.revealpath.DBusSendRevealPathService;
import org.cryptomator.linux.tray.AppindicatorTrayMenuController;

module org.cryptomator.integrations.linux {
requires org.cryptomator.integrations.api;
requires org.slf4j;
requires com.google.common;
requires org.apache.commons.lang3;
requires org.freedesktop.dbus;
requires org.purejava.appindicator;
requires org.purejava.kwallet;
requires secret.service;

provides KeychainAccessProvider with SecretServiceKeychainAccess;
provides RevealPathService with DBusSendRevealPathService;
provides TrayMenuController with AppindicatorTrayMenuController;

opens org.cryptomator.linux.tray to org.cryptomator.integrations.api;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.kde.KWallet;
import org.kde.Static;
import org.purejava.KDEWallet;
import org.purejava.kwallet.KDEWallet;
import org.purejava.kwallet.Static;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/cryptomator/linux/tray/ActionItemCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.cryptomator.linux.tray;

import org.cryptomator.integrations.tray.ActionItem;
import org.purejava.appindicator.GCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

record ActionItemCallback (ActionItem actionItem) implements GCallback {
private static final Logger LOG = LoggerFactory.getLogger(ActionItemCallback.class);

@Override
public void apply() {
LOG.trace("Hit tray menu action '{}'", actionItem.title());
actionItem.action().run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.cryptomator.linux.tray;

import org.cryptomator.integrations.common.CheckAvailability;
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.purejava.appindicator.GCallback;
import org.purejava.appindicator.MemoryAllocator;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SegmentScope;
import java.util.List;
import java.util.function.Consumer;

import static org.purejava.appindicator.app_indicator_h.*;

@Priority(1000)
@OperatingSystem(OperatingSystem.Value.LINUX)
public class AppindicatorTrayMenuController implements TrayMenuController {

private static final String APP_INDICATOR_ID = "org.cryptomator.Cryptomator";

private static final SegmentScope SCOPE = SegmentScope.global();
private MemorySegment indicator;
private MemorySegment menu = gtk_menu_new();

@CheckAvailability
public static boolean isAvailable() {
return MemoryAllocator.isLoadedNativeLib();
}

@Override
public void showTrayIcon(Consumer<TrayIconLoader> iconLoader, Runnable runnable, String s) throws TrayMenuException {
TrayIconLoader.FreedesktopIconName callback = this::showTrayIconWithSVG;
iconLoader.accept(callback);
gtk_widget_show_all(menu);
app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE());
}

private void showTrayIconWithSVG(String s) {
try (var arena = Arena.openConfined()) {
indicator = app_indicator_new(arena.allocateUtf8String(APP_INDICATOR_ID),
arena.allocateUtf8String(s),
APP_INDICATOR_CATEGORY_APPLICATION_STATUS());
}
}

@Override
public void updateTrayIcon(Consumer<TrayIconLoader> iconLoader) {
TrayIconLoader.FreedesktopIconName callback = this::updateTrayIconWithSVG;
iconLoader.accept(callback);
}

private void updateTrayIconWithSVG(String s) {
try (var arena = Arena.openConfined()) {
app_indicator_set_icon(indicator, arena.allocateUtf8String(s));
}
}

@Override
public void updateTrayMenu(List<TrayMenuItem> items) throws TrayMenuException {
menu = gtk_menu_new();
addChildren(menu, items);
gtk_widget_show_all(menu);
app_indicator_set_menu(indicator, menu);
}

@Override
public void onBeforeOpenMenu(Runnable runnable) {

}

private void addChildren(MemorySegment menu, List<TrayMenuItem> items) {
for (var item : items) {
switch (item) {
case ActionItem a -> {
var gtkMenuItem = gtk_menu_item_new();
try (var arena = Arena.openConfined()) {
gtk_menu_item_set_label(gtkMenuItem, arena.allocateUtf8String(a.title()));
g_signal_connect_object(gtkMenuItem,
arena.allocateUtf8String("activate"),
GCallback.allocate(new ActionItemCallback(a), SCOPE),
menu,
0);
}
gtk_menu_shell_append(menu, gtkMenuItem);
}
case SeparatorItem separatorItem -> {
var gtkSeparator = gtk_menu_item_new();
gtk_menu_shell_append(menu, gtkSeparator);
}
case SubMenuItem s -> {
var gtkMenuItem = gtk_menu_item_new();
var gtkSubmenu = gtk_menu_new();
try (var arena = Arena.openConfined()) {
gtk_menu_item_set_label(gtkMenuItem, arena.allocateUtf8String(s.title()));
}
addChildren(gtkSubmenu, s.items());
gtk_menu_item_set_submenu(gtkMenuItem, gtkSubmenu);
gtk_menu_shell_append(menu, gtkMenuItem);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.cryptomator.linux.tray.AppindicatorTrayMenuController

0 comments on commit eef050b

Please sign in to comment.