added TRAY_IMAGE_SIZE_FALLBACK and TRAY_MENU_IMAGE_SIZE_FALLBACK, changed anony to lambdas

This commit is contained in:
Robinson 2021-04-07 14:51:40 +02:00
parent 0416f0b5d4
commit f5f4bca004

View File

@ -38,15 +38,24 @@ import dorkbox.jna.linux.structs.GtkStyle;
import dorkbox.jna.linux.structs.PangoRectangle; import dorkbox.jna.linux.structs.PangoRectangle;
import dorkbox.os.OS; import dorkbox.os.OS;
import dorkbox.os.OSUtil; import dorkbox.os.OSUtil;
import dorkbox.propertyLoader.Property;
import dorkbox.util.FileUtil; import dorkbox.util.FileUtil;
import dorkbox.util.MathUtil; import dorkbox.util.MathUtil;
/** /**
* Class to contain all of the various methods needed to get information set by a GTK theme. * Class to contain all of the various methods needed to get information set by a GTK theme.
*/ */
@SuppressWarnings({"deprecation", "WeakerAccess"}) @SuppressWarnings({"unused", "WeakerAccess"})
public public
class GtkTheme { class GtkTheme {
@Property
/** Fallback for an unknown tray image size. */
public static int TRAY_IMAGE_SIZE_FALLBACK = 24;
@Property
/** Fallback for an unknown tray menu image size. */
public static int TRAY_MENU_IMAGE_SIZE_FALLBACK = 16;
public static public static
Rectangle getPixelTextHeight(String text) { Rectangle getPixelTextHeight(String text) {
// the following method requires an offscreen widget to get the size of text (for the checkmark size) via pango // the following method requires an offscreen widget to get the size of text (for the checkmark size) via pango
@ -86,12 +95,9 @@ class GtkTheme {
*/ */
public static public static
int getMenuEntryImageSize() { int getMenuEntryImageSize() {
final AtomicReference<Integer> imageHeight = new AtomicReference<Integer>(); final AtomicReference<Integer> imageHeight = new AtomicReference<>();
GtkEventDispatch.dispatchAndWait(new Runnable() { GtkEventDispatch.dispatchAndWait(()->{
@Override
public
void run() {
Pointer offscreen = Gtk2.gtk_offscreen_window_new(); Pointer offscreen = Gtk2.gtk_offscreen_window_new();
// get the default icon size for the "paste" icon. // get the default icon size for the "paste" icon.
@ -124,7 +130,6 @@ class GtkTheme {
Gtk2.gtk_widget_destroy(offscreen); Gtk2.gtk_widget_destroy(offscreen);
} }
} }
}
}); });
@ -132,10 +137,9 @@ class GtkTheme {
if (height > 0) { if (height > 0) {
return height; return height;
} }
else {
LoggerFactory.getLogger(GtkTheme.class).warn("Unable to get tray menu image size. Using default."); LoggerFactory.getLogger(GtkTheme.class).warn("Unable to get tray menu image size. Using fallback: " + TRAY_MENU_IMAGE_SIZE_FALLBACK);
return 16; return TRAY_MENU_IMAGE_SIZE_FALLBACK;
}
} }
/** /**
@ -151,15 +155,12 @@ class GtkTheme {
// 96 DPI is the default // 96 DPI is the default
final double defaultDPI = 96.0; final double defaultDPI = 96.0;
final AtomicReference<Double> screenScale = new AtomicReference<Double>(); final AtomicReference<Double> screenScale = new AtomicReference<>();
final AtomicInteger screenDPI = new AtomicInteger(); final AtomicInteger screenDPI = new AtomicInteger();
screenScale.set(0D); screenScale.set(0D);
screenDPI.set(0); screenDPI.set(0);
GtkEventDispatch.dispatchAndWait(new Runnable() { GtkEventDispatch.dispatchAndWait(()->{
@Override
public
void run() {
// screen DPI // screen DPI
Pointer screen = Gtk2.gdk_screen_get_default(); Pointer screen = Gtk2.gdk_screen_get_default();
if (screen != null) { if (screen != null) {
@ -174,7 +175,6 @@ class GtkTheme {
screenScale.set(scale); screenScale.set(scale);
} }
} }
}
}); });
@ -423,10 +423,7 @@ class GtkTheme {
// try to use GTK to get the tray icon size // try to use GTK to get the tray icon size
final AtomicInteger traySize = new AtomicInteger(); final AtomicInteger traySize = new AtomicInteger();
GtkEventDispatch.dispatchAndWait(new Runnable() { GtkEventDispatch.dispatchAndWait(()->{
@Override
public
void run() {
Pointer screen = Gtk2.gdk_screen_get_default(); Pointer screen = Gtk2.gdk_screen_get_default();
Pointer settings = null; Pointer settings = null;
@ -446,9 +443,9 @@ class GtkTheme {
// A list of icon sizes. The list is separated by colons, and item has the form: size-name = width , height // A list of icon sizes. The list is separated by colons, and item has the form: size-name = width , height
GObject.g_object_get(settings, "gtk-icon-sizes", pointer.getPointer(), null); GObject.g_object_get(settings, "gtk-icon-sizes", pointer.getPointer(), null);
Pointer value = pointer.getValue(); Pointer value = pointer.getValue();
if (value != null) { if (value != null) {
// this might be null for later versions of GTK!
String iconSizes = value.getString(0); String iconSizes = value.getString(0);
String[] strings = new String[] {"panel-menu-bar=", "panel=", "gtk-large-toolbar=", "gtk-small-toolbar="}; String[] strings = new String[] {"panel-menu-bar=", "panel=", "gtk-large-toolbar=", "gtk-small-toolbar="};
for (String var : strings) { for (String var : strings) {
@ -464,7 +461,6 @@ class GtkTheme {
} }
} }
} }
}
}); });
int i = traySize.get(); int i = traySize.get();
@ -473,9 +469,8 @@ class GtkTheme {
} }
// sane default // sane default
LoggerFactory.getLogger(GtkTheme.class).warn("Unable to get tray image size. Using default."); LoggerFactory.getLogger(GtkTheme.class).warn("Unable to get tray image size. Using fallback: " + TRAY_IMAGE_SIZE_FALLBACK);
return TRAY_IMAGE_SIZE_FALLBACK;
return 24;
} }
/** /**
@ -483,13 +478,9 @@ class GtkTheme {
*/ */
public static public static
Color getTextColor() { Color getTextColor() {
final AtomicReference<Color> color = new AtomicReference<Color>(null); final AtomicReference<Color> color = new AtomicReference<>(null);
GtkEventDispatch.dispatchAndWait(new Runnable() { GtkEventDispatch.dispatchAndWait(()->{
@SuppressWarnings("UnusedAssignment") Color c;
@Override
public
void run() {
Color c = null;
// the following method requires an offscreen widget to get the style information from. // the following method requires an offscreen widget to get the style information from.
// don't forget to destroy everything! // don't forget to destroy everything!
@ -518,7 +509,6 @@ class GtkTheme {
Gtk2.gtk_widget_destroy(item); Gtk2.gtk_widget_destroy(item);
Gtk2.gtk_widget_destroy(menu); Gtk2.gtk_widget_destroy(menu);
} }
}
}); });
return color.get(); return color.get();