10
10
import org .cryptomator .integrations .tray .TrayMenuController ;
11
11
import org .cryptomator .integrations .tray .TrayMenuException ;
12
12
import org .cryptomator .integrations .tray .TrayMenuItem ;
13
+ import org .cryptomator .linux .util .CheckUtil ;
14
+ import org .purejava .appindicator .AppIndicator ;
13
15
import org .purejava .appindicator .GCallback ;
14
- import org .purejava .appindicator .NativeLibUtilities ;
16
+ import org .purejava .appindicator .GObject ;
17
+ import org .purejava .appindicator .Gtk ;
15
18
16
19
import java .lang .foreign .Arena ;
17
20
import java .lang .foreign .MemorySegment ;
18
21
import java .util .List ;
19
- import java .util .Optional ;
20
22
import java .util .function .Consumer ;
21
23
22
- import static org .purejava .appindicator .app_indicator_h .*;
24
+ import static org .purejava .appindicator .app_indicator_h .APP_INDICATOR_CATEGORY_APPLICATION_STATUS ;
25
+ import static org .purejava .appindicator .app_indicator_h .APP_INDICATOR_STATUS_ACTIVE ;
23
26
24
27
@ Priority (1000 )
25
28
@ CheckAvailability
@@ -30,37 +33,36 @@ public class AppindicatorTrayMenuController implements TrayMenuController {
30
33
31
34
private static final Arena ARENA = Arena .global ();
32
35
private MemorySegment indicator ;
33
- private MemorySegment menu = gtk_menu_new () ;
36
+ private MemorySegment menu ;
34
37
35
38
@ CheckAvailability
36
39
public static boolean isAvailable () {
37
- return NativeLibUtilities . isLoadedNativeLib ();
40
+ return AppIndicator . isLoaded ();
38
41
}
39
42
40
43
@ Override
41
44
public void showTrayIcon (Consumer <TrayIconLoader > iconLoader , Runnable runnable , String s ) throws TrayMenuException {
45
+ menu = Gtk .newMenu ();
42
46
TrayIconLoader .FreedesktopIconName callback = this ::showTrayIconWithSVG ;
43
47
iconLoader .accept (callback );
44
- gtk_widget_show_all (menu );
45
- app_indicator_set_status (indicator , APP_INDICATOR_STATUS_ACTIVE ());
48
+ Gtk . widgetShowAll (menu );
49
+ AppIndicator . setStatus (indicator , APP_INDICATOR_STATUS_ACTIVE ());
46
50
}
47
51
48
- private void showTrayIconWithSVG (String s ) {
49
- try (var arena = Arena .ofConfined ()) {
50
- var svgSourcePath = System .getProperty (SVG_SOURCE_PROPERTY );
51
- // flatpak
52
- if (svgSourcePath == null ) {
53
- indicator = app_indicator_new (arena .allocateUtf8String (APP_INDICATOR_ID ),
54
- arena .allocateUtf8String (s ),
55
- APP_INDICATOR_CATEGORY_APPLICATION_STATUS ());
56
- // AppImage and ppa
57
- } else {
58
- indicator = app_indicator_new_with_path (arena .allocateUtf8String (APP_INDICATOR_ID ),
59
- arena .allocateUtf8String (s ),
60
- APP_INDICATOR_CATEGORY_APPLICATION_STATUS (),
61
- // find tray icons theme in mounted AppImage / installed on system by ppa
62
- arena .allocateUtf8String (svgSourcePath ));
63
- }
52
+ private void showTrayIconWithSVG (String iconName ) {
53
+ var svgSourcePath = System .getProperty (SVG_SOURCE_PROPERTY );
54
+ // flatpak
55
+ if (svgSourcePath == null ) {
56
+ indicator = AppIndicator .newIndicator (APP_INDICATOR_ID ,
57
+ iconName ,
58
+ APP_INDICATOR_CATEGORY_APPLICATION_STATUS ());
59
+ // AppImage and ppa
60
+ } else {
61
+ indicator = AppIndicator .newIndicatorWithPath (APP_INDICATOR_ID ,
62
+ iconName ,
63
+ APP_INDICATOR_CATEGORY_APPLICATION_STATUS (),
64
+ // find tray icons theme in mounted AppImage / installed on system by ppa
65
+ svgSourcePath );
64
66
}
65
67
}
66
68
@@ -70,18 +72,18 @@ public void updateTrayIcon(Consumer<TrayIconLoader> iconLoader) {
70
72
iconLoader .accept (callback );
71
73
}
72
74
73
- private void updateTrayIconWithSVG (String s ) {
74
- try (var arena = Arena .ofConfined ()) {
75
- app_indicator_set_icon (indicator , arena .allocateUtf8String (s ));
76
- }
75
+ private void updateTrayIconWithSVG (String iconName ) {
76
+ CheckUtil .checkState (indicator != null , "Appindicator is not setup. Call showTrayIcon(...) first." );
77
+ AppIndicator .setIcon (indicator , iconName );
77
78
}
78
79
79
80
@ Override
80
81
public void updateTrayMenu (List <TrayMenuItem > items ) throws TrayMenuException {
81
- menu = gtk_menu_new ();
82
+ CheckUtil .checkState (indicator != null , "Appindicator is not setup. Call showTrayIcon(...) first." );
83
+ menu = Gtk .newMenu ();
82
84
addChildren (menu , items );
83
- gtk_widget_show_all (menu );
84
- app_indicator_set_menu (indicator , menu );
85
+ Gtk . widgetShowAll (menu );
86
+ AppIndicator . setMenu (indicator , menu );
85
87
}
86
88
87
89
@ Override
@@ -93,30 +95,26 @@ private void addChildren(MemorySegment menu, List<TrayMenuItem> items) {
93
95
for (var item : items ) {
94
96
switch (item ) {
95
97
case ActionItem a -> {
96
- var gtkMenuItem = gtk_menu_item_new ();
97
- try (var arena = Arena .ofConfined ()) {
98
- gtk_menu_item_set_label (gtkMenuItem , arena .allocateUtf8String (a .title ()));
99
- g_signal_connect_object (gtkMenuItem ,
100
- arena .allocateUtf8String ("activate" ),
101
- GCallback .allocate (new ActionItemCallback (a ), ARENA ),
102
- menu ,
103
- 0 );
104
- }
105
- gtk_menu_shell_append (menu , gtkMenuItem );
98
+ var gtkMenuItem = Gtk .newMenuItem ();
99
+ Gtk .menuItemSetLabel (gtkMenuItem , a .title ());
100
+ GObject .signalConnectObject (gtkMenuItem ,
101
+ "activate" ,
102
+ GCallback .allocate (new ActionItemCallback (a ), ARENA ),
103
+ menu ,
104
+ 0 );
105
+ Gtk .menuShellAppend (menu , gtkMenuItem );
106
106
}
107
107
case SeparatorItem _ -> {
108
- var gtkSeparator = gtk_menu_item_new ();
109
- gtk_menu_shell_append (menu , gtkSeparator );
108
+ var gtkSeparator = Gtk . newMenuItem ();
109
+ Gtk . menuShellAppend (menu , gtkSeparator );
110
110
}
111
111
case SubMenuItem s -> {
112
- var gtkMenuItem = gtk_menu_item_new ();
113
- var gtkSubmenu = gtk_menu_new ();
114
- try (var arena = Arena .ofConfined ()) {
115
- gtk_menu_item_set_label (gtkMenuItem , arena .allocateUtf8String (s .title ()));
116
- }
112
+ var gtkMenuItem = Gtk .newMenuItem ();
113
+ var gtkSubmenu = Gtk .newMenu ();
114
+ Gtk .menuItemSetLabel (gtkMenuItem , s .title ());
117
115
addChildren (gtkSubmenu , s .items ());
118
- gtk_menu_item_set_submenu (gtkMenuItem , gtkSubmenu );
119
- gtk_menu_shell_append (menu , gtkMenuItem );
116
+ Gtk . menuItemSetSubmenu (gtkMenuItem , gtkSubmenu );
117
+ Gtk . menuShellAppend (menu , gtkMenuItem );
120
118
}
121
119
}
122
120
}
0 commit comments