Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Jan 14, 2025
1 parent a17adee commit a617c29
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 49 deletions.
26 changes: 12 additions & 14 deletions src/main/java/mpo/dayon/assisted/utils/ScreenUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public final class ScreenUtilities {

private static Rectangle sharedScreenSize;

private static int[] rgb;

private static byte[] gray;

private static boolean shareAllScreens;
Expand All @@ -35,23 +33,22 @@ private ScreenUtilities() {
NUMBER_OF_SCREENS = countScreens();
DEFAULT_SIZE = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().getBounds();
COMBINED_SCREEN_SIZE = getCombinedScreenSize();
sharedScreenSize = shareAllScreens ? COMBINED_SCREEN_SIZE : DEFAULT_SIZE;
rgb = new int[sharedScreenSize.height * sharedScreenSize.width];
gray = new byte[rgb.length];
init();
try {
ROBOT = new Robot();
} catch (AWTException ex) {
throw new IllegalStateException("Could not initialize the AWT robot!", ex);
}
}

public static void setShareAllScreens(boolean doShareAllScreens) {
synchronized (ScreenUtilities.class) {
shareAllScreens = doShareAllScreens;
sharedScreenSize = doShareAllScreens ? COMBINED_SCREEN_SIZE : DEFAULT_SIZE;
rgb = new int[sharedScreenSize.height * sharedScreenSize.width];
gray = new byte[rgb.length];
}
public static synchronized void setShareAllScreens(boolean doShareAllScreens) {
shareAllScreens = doShareAllScreens;
init();
}

private static void init() {
sharedScreenSize = shareAllScreens ? COMBINED_SCREEN_SIZE : DEFAULT_SIZE;
gray = new byte[sharedScreenSize.height * sharedScreenSize.width];
}

public static Rectangle getSharedScreenSize() {
Expand All @@ -68,8 +65,9 @@ private static int countScreens() {

private static Rectangle getCombinedScreenSize() {
Rectangle fullSize = new Rectangle();
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
stream(environment.getScreenDevices()).flatMap(gd -> stream(gd.getConfigurations())).forEach(graphicsConfiguration -> Rectangle2D.union(fullSize, graphicsConfiguration.getBounds(), fullSize));
stream(GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices())
.flatMap(gd -> stream(gd.getConfigurations()))
.forEach(graphicsConfiguration -> Rectangle2D.union(fullSize, graphicsConfiguration.getBounds(), fullSize));
return fullSize.getBounds();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public int hashCode() {
*/
@Override
protected void persist(boolean clear) {
final Preferences.Props props = getProps(clear);
Preferences.getPreferences().update(props); // atomic (!)
Preferences.getPreferences().update(getProps(clear)); // atomic (!)
}

private Preferences.Props getProps(boolean clear) {
Expand Down
47 changes: 14 additions & 33 deletions src/main/java/mpo/dayon/common/preference/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,12 @@ public static synchronized Preferences getPreferences() {
}

try {
final Preferences xpreferences;
final File file = getOrCreatePreferencesFile();

if (file.exists()) {
Log.info("Preferences (existing) [" + file.getAbsolutePath() + "]");
} else {
Log.info("Preferences (new) [" + file.getAbsolutePath() + "]");
}
xpreferences = new Preferences(file);
setupPersister(xpreferences);
preferences = xpreferences;
return preferences;
Log.info("Preferences (" + (file.exists() ? "existing" : "new") + ") [" + file.getAbsolutePath() + "]");
return preferences = setupPersister(new Preferences(file));
} catch (IOException ex) {
Log.warn("Preferences get/create error!", ex);
preferences = NULL;
return preferences;
return preferences = NULL;
}
}

Expand Down Expand Up @@ -180,41 +170,32 @@ public void update(Props props) {

/**
* Some components are possibly sending a lot of updates (e.g., main frame
* resize) and it makes no sense to write every changes as we want the last
* resize) and it makes no sense to write every change as we want the last
* one only => I'm polling instead of saving each time a value has changed
* ...
*/
private static void setupPersister(final Preferences preferences) {
private static Preferences setupPersister(final Preferences preferences) {
new Timer("PreferencesWriter").schedule(new TimerTask() {
@Override
public void run() {
if (preferences.isNull() || preferences.getWriteError().get()) {
return;
}

try {
Properties cloned = null;
synchronized (preferences.getCloneLOCK()) {
if (preferences.getDirty().get()) {
cloned = (Properties) preferences.getProps().clone();
preferences.getDirty().set(false);
}
}
if (cloned != null) {
Log.debug("Writing the preferences [%s]", () -> preferences.getFile().getAbsolutePath());
synchronized (preferences.getCloneLOCK()) {
if (preferences.getDirty().get()) {
try (PrintWriter out = new PrintWriter(preferences.getFile(), UTF_8)) {
cloned.store(out, null);
out.flush();
Log.debug("Writing the preferences [%s]", () -> preferences.getFile().getAbsolutePath());
preferences.getProps().store(out, null);
preferences.getDirty().set(false);
} catch (IOException ex) {
Log.error("Preferences write error!", ex);
preferences.getWriteError().set(true);
}
}
} catch (FileNotFoundException e) {
Log.error("Preferences (write) permission denied");
preferences.getWriteError().set(true);
} catch (IOException ex) {
Log.error("Preferences write error!", ex);
preferences.getWriteError().set(true);
}
}
}, 0, 2000);
return preferences;
}
}

0 comments on commit a617c29

Please sign in to comment.