Added mnemonic support for linux

This commit is contained in:
nathan 2016-10-08 00:18:17 +02:00
parent bdf76e13fd
commit f2065a59fa
5 changed files with 55 additions and 4 deletions

View File

@ -99,6 +99,16 @@ interface MenuEntry {
*/ */
void setCallback(SystemTrayMenuAction callback); void setCallback(SystemTrayMenuAction callback);
/**
* Sets a menu entry shortcut key (Mnemonic) so that menu entry can be "selected" via the keyboard while the menu is displayed.
*
* Mnemonics are case-insensitive, and if the character defined by the mnemonic is found within the text, the first occurrence
* of it will be underlined.
*
* @param key this is the key to set as the mnemonic
*/
void setShortcut(char key);
/** /**
* Removes this menu entry from the menu and releases all system resources associated with this menu entry * Removes this menu entry from the menu and releases all system resources associated with this menu entry
*/ */

View File

@ -90,6 +90,11 @@ class GtkEntry implements MenuEntry {
} }
} }
@Override
public
void setShortcut(final char key) {
}
@Override @Override
public public
String getText() { String getText() {

View File

@ -39,12 +39,17 @@ class GtkEntryItem extends GtkEntry implements GCallback {
// these are necessary BECAUSE GTK menus look funky as hell when there are some menu entries WITH icons and some WITHOUT // these are necessary BECAUSE GTK menus look funky as hell when there are some menu entries WITH icons and some WITHOUT
private volatile boolean hasLegitIcon = true; private volatile boolean hasLegitIcon = true;
// The mnemonic will ONLY show-up once a menu entry is selected. IT WILL NOT show up before then!
// AppIndicators will only show if you use the keyboard to navigate
// GtkStatusIndicator will show on mouse+keyboard movement
private volatile char mnemonicKey = 0;
/** /**
* called from inside dispatch thread. ONLY creates the menu item, but DOES NOT attach it! * called from inside dispatch thread. ONLY creates the menu item, but DOES NOT attach it!
* this is a FLOATING reference. See: https://developer.gnome.org/gobject/stable/gobject-The-Base-Object-Type.html#floating-ref * this is a FLOATING reference. See: https://developer.gnome.org/gobject/stable/gobject-The-Base-Object-Type.html#floating-ref
*/ */
GtkEntryItem(final GtkMenu parent, final SystemTrayMenuAction callback) { GtkEntryItem(final GtkMenu parent, final SystemTrayMenuAction callback) {
super(parent, Gtk.gtk_image_menu_item_new_with_label("")); super(parent, Gtk.gtk_image_menu_item_new_with_mnemonic(""));
this.callback = callback; this.callback = callback;
@ -62,6 +67,20 @@ class GtkEntryItem extends GtkEntry implements GCallback {
} }
} }
@Override
public
void setShortcut(final char key) {
this.mnemonicKey = Character.toLowerCase(key);
Gtk.dispatch(new Runnable() {
@Override
public
void run() {
renderText(getText());
}
});
}
@Override @Override
public public
void setCallback(final SystemTrayMenuAction callback) { void setCallback(final SystemTrayMenuAction callback) {
@ -118,8 +137,19 @@ class GtkEntryItem extends GtkEntry implements GCallback {
/** /**
* must always be called in the GTK thread * must always be called in the GTK thread
*/ */
void renderText(final String text) { void renderText(String text) {
if (this.mnemonicKey != 0) {
// they are CASE INSENSITIVE!
int i = text.toLowerCase()
.indexOf(this.mnemonicKey);
if (i >= 0) {
text = text.substring(0, i) + "_" + text.substring(i);
}
}
Gtk.gtk_menu_item_set_label(_native, text); Gtk.gtk_menu_item_set_label(_native, text);
Gtk.gtk_widget_show_all(_native); Gtk.gtk_widget_show_all(_native);
} }

View File

@ -152,6 +152,12 @@ class GtkMenu extends Menu implements MenuEntry {
}); });
} }
@Override
public
void setShortcut(final char key) {
menuEntry.setShortcut(key);
}
@Override @Override
public public
String getText() { String getText() {

View File

@ -429,8 +429,8 @@ class Gtk {
// to create a menu entry WITH an icon. // to create a menu entry WITH an icon.
public static native Pointer gtk_image_new_from_file(String iconPath); public static native Pointer gtk_image_new_from_file(String iconPath);
// uses '_' to define which key is the mnemonic
public static native Pointer gtk_image_menu_item_new_with_label(String label); public static native Pointer gtk_image_menu_item_new_with_mnemonic(String label);
public static native void gtk_image_menu_item_set_image(Pointer image_menu_item, Pointer image); public static native void gtk_image_menu_item_set_image(Pointer image_menu_item, Pointer image);