Added tooltips (if possible) to checkbox menu items

This commit is contained in:
nathan 2018-11-10 15:53:39 +01:00
parent 4a6881df4a
commit 077a53d30e
6 changed files with 77 additions and 1 deletions

View File

@ -32,6 +32,7 @@ class Checkbox extends Entry {
private volatile boolean enabled = true;
private volatile char mnemonicKey;
private volatile String tooltip;
public
Checkbox() {
@ -195,4 +196,45 @@ class Checkbox extends Entry {
((CheckboxPeer) peer).setShortcut(this);
}
}
/**
* Specifies the tooltip text, usually this is used to brand the SystemTray icon with your product's name, or to provide extra
* information during mouse-over for menu entries.
* <p>
* NOTE: Maximum length is 64 characters long, and it is not supported on all Operating Systems and Desktop Environments.
* <p>
* For more details on Linux see https://bugs.launchpad.net/indicator-application/+bug/527458/comments/12.
*
* @param tooltipText the text to use as a mouse-over tooltip for the tray icon or menu entry, null to remove.
*/
public
void setTooltip(final String tooltipText) {
if (tooltipText != null) {
// this is a safety precaution, since the behavior of really long text is undefined.
if (tooltipText.length() > 64) {
throw new RuntimeException("Tooltip text cannot be longer than 64 characters.");
}
if (!MenuItem.alreadyEmittedTooltipWarning) {
MenuItem.alreadyEmittedTooltipWarning = true;
SystemTray.logger.warn("Please disable tooltips, as they are not consistent across all platforms and tray types.");
}
}
this.tooltip = tooltipText;
if (peer != null) {
((CheckboxPeer) peer).setTooltip(this);
}
}
/**
* Gets the mouse-over tooltip for the meme entry.
*
* NOTE: This is not consistent across all platforms and tray types.
*/
public
String getTooltip() {
return this.tooltip;
}
}

View File

@ -33,7 +33,7 @@ import dorkbox.util.SwingUtil;
@SuppressWarnings({"unused", "SameParameterValue", "WeakerAccess"})
public
class MenuItem extends Entry {
private static boolean alreadyEmittedTooltipWarning = false;
static boolean alreadyEmittedTooltipWarning = false;
private volatile String text;
private volatile File imageFile;

View File

@ -31,5 +31,7 @@ interface CheckboxPeer extends EntryPeer {
void setShortcut(Checkbox menuItem);
void setTooltip(Checkbox menuItem);
void setChecked(Checkbox menuItem);
}

View File

@ -122,6 +122,12 @@ class AwtMenuItemCheckbox implements CheckboxPeer {
});
}
@Override
public
void setTooltip(final Checkbox menuItem) {
// no op. (awt menus cannot show tooltips)
}
@Override
public
void setChecked(final Checkbox menuItem) {

View File

@ -268,6 +268,20 @@ class GtkMenuItemCheckbox extends GtkBaseMenuItem implements CheckboxPeer, GCall
}
}
@Override
public
void setTooltip(final Checkbox menuItem) {
GtkEventDispatch.dispatch(new Runnable() {
@Override
public
void run() {
// NOTE: this will not work for AppIndicator tray types!
// null will remove the tooltip
Gtk2.gtk_widget_set_tooltip_text(_native, menuItem.getTooltip());
}
});
}
// this is pretty much ONLY for Ubuntu AppIndicators
private
void setCheckedIconForFakeCheckMarks() {

View File

@ -173,4 +173,16 @@ class SwingMenuItemCheckbox extends SwingMenuItem implements CheckboxPeer {
});
}
}
@Override
public
void setTooltip(final Checkbox menuItem) {
SwingUtil.invokeLater(new Runnable() {
@Override
public
void run() {
_native.setToolTipText(menuItem.getTooltip());
}
});
}
}