Skip to content

Commit 8b9e6c6

Browse files
committed
added check if position is valid on startup. removed unused methods/constructor. renamed variables. simplified bounds check
1 parent 1e14e9e commit 8b9e6c6

File tree

2 files changed

+77
-60
lines changed

2 files changed

+77
-60
lines changed

src/main/java/de/doubleslash/keeptime/Main.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,12 @@ private void initialiseUI(final Stage primaryStage) throws IOException {
248248

249249
final ScreenPosHelper poshelper = new ScreenPosHelper(model.screenSettings.screenHash.get(),
250250
model.screenSettings.xProportion.get(), model.screenSettings.yProportion.get());
251+
poshelper.resetPositionIfInvalid();
252+
251253
// set stage to saved Position
252254
if (model.screenSettings.saveWindowPosition.get()) {
253-
254-
primaryStage.setX(poshelper.getAbsolutX());
255-
primaryStage.setY(poshelper.getAbsolutY());
255+
primaryStage.setX(poshelper.getAbsoluteX());
256+
primaryStage.setY(poshelper.getAbsoluteY());
256257
}
257258

258259
// add listeners to record Windowpositionchange
@@ -269,15 +270,15 @@ public void changed(final ObservableValue<? extends Number> observable, final Nu
269270
}
270271

271272
if (observable.equals(primaryStage.xProperty())) {
272-
poshelper.setAbsolutX(newValue.doubleValue());
273+
poshelper.setAbsoluteX(newValue.doubleValue());
273274
} else {
274-
poshelper.setAbsolutY(newValue.doubleValue());
275+
poshelper.setAbsoluteY(newValue.doubleValue());
275276
}
276277

277278
final Settings newSettings = new Settings(model.hoverBackgroundColor.get(), model.hoverFontColor.get(),
278279
model.defaultBackgroundColor.get(), model.defaultFontColor.get(), model.taskBarColor.get(),
279280
model.useHotkey.get(), model.displayProjectsRight.get(), model.hideProjectsOnMouseExit.get(),
280-
poshelper.getxProportion(), poshelper.getyProportion(), poshelper.getScreenhash(),
281+
poshelper.getProportionalX(), poshelper.getProportionalY(), poshelper.getScreenHash(),
281282
model.screenSettings.saveWindowPosition.get(), model.remindIfNotesAreEmpty.get());
282283

283284
controller.updateSettings(newSettings);

src/main/java/de/doubleslash/keeptime/common/ScreenPosHelper.java

Lines changed: 70 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,109 @@
11
package de.doubleslash.keeptime.common;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import javafx.geometry.Rectangle2D;
37
import javafx.stage.Screen;
48

9+
/**
10+
* Helper to handle screen coordinates. Converts between absolute coordinates to screen+proportional coordinates. E.g.
11+
* you have two screens, each with 1000px width. Absolute position gives you 1500px and this class will get you hash for
12+
* screen #2 and proportional value 0.5. <br>
13+
* Reference #38
14+
*/
515
public class ScreenPosHelper {
616

7-
private int screenhash;
8-
private double absolutX;
9-
private double absolutY;
10-
private double xProportion;
11-
private double yProportion;
17+
private static final Logger LOG = LoggerFactory.getLogger(ScreenPosHelper.class);
1218

13-
public ScreenPosHelper(final double absolutX, final double absolutY) {
14-
this.absolutX = absolutX;
15-
this.absolutY = absolutY;
16-
calcRelativScreenpos();
17-
}
19+
private int screenHash;
1820

19-
public ScreenPosHelper(final int screenhash, final double xProportion, final double yProportion) {
20-
this.screenhash = screenhash;
21-
this.xProportion = xProportion;
22-
this.yProportion = yProportion;
23-
calcAbsolutScreenpos();
24-
}
21+
private double absoluteX;
22+
private double absoluteY;
2523

26-
public double getAbsolutX() {
27-
return absolutX;
28-
}
24+
private double proportionalX;
25+
private double proportionalY;
2926

30-
public void setAbsolutX(final double absolutX) {
31-
this.absolutX = absolutX;
32-
calcRelativScreenpos();
27+
public ScreenPosHelper(final int screenhash, final double proportionalX, final double proportionalY) {
28+
this.screenHash = screenhash;
29+
this.proportionalX = proportionalX;
30+
this.proportionalY = proportionalY;
31+
calcAbsolutePosition();
3332
}
3433

35-
public double getAbsolutY() {
36-
return absolutY;
34+
public double getAbsoluteX() {
35+
return absoluteX;
3736
}
3837

39-
public void setAbsolutY(final double absolutY) {
40-
this.absolutY = absolutY;
41-
calcRelativScreenpos();
38+
public void setAbsoluteX(final double absolutX) {
39+
this.absoluteX = absolutX;
40+
calcProportionalPosition();
4241
}
4342

44-
public int getScreenhash() {
45-
return screenhash;
43+
public double getAbsoluteY() {
44+
return absoluteY;
4645
}
4746

48-
public void setScreenhash(final int screenhash) {
49-
this.screenhash = screenhash;
50-
calcRelativScreenpos();
47+
public void setAbsoluteY(final double absolutY) {
48+
this.absoluteY = absolutY;
49+
calcProportionalPosition();
5150
}
5251

53-
public double getxProportion() {
54-
return xProportion;
52+
public int getScreenHash() {
53+
return screenHash;
5554
}
5655

57-
public void setxProportion(final double xProportion) {
58-
this.xProportion = xProportion;
59-
calcAbsolutScreenpos();
56+
public double getProportionalX() {
57+
return proportionalX;
6058
}
6159

62-
public double getyProportion() {
63-
return yProportion;
60+
public double getProportionalY() {
61+
return proportionalY;
6462
}
6563

66-
public void setyProportion(final double yProportion) {
67-
this.yProportion = yProportion;
68-
calcAbsolutScreenpos();
64+
public void resetPositionIfInvalid() {
65+
if (!isPositionValid()) {
66+
final Screen screen = getScreenWithHashOrPrimary(this.screenHash);
67+
final Rectangle2D screenBounds = screen.getBounds();
68+
LOG.warn(
69+
"Position is not in the range of the screen. Screen (hash '{}') range '{}'. Calculated positions '{}'/'{}'. Setting to '0'/'0'.",
70+
screen.hashCode(), screenBounds, absoluteX, absoluteY);
71+
proportionalX = 0;
72+
proportionalY = 0;
73+
absoluteX = 0;
74+
absoluteY = 0;
75+
}
6976
}
7077

71-
private void calcRelativScreenpos() {
78+
private void calcProportionalPosition() {
7279
Screen screen = Screen.getPrimary();
7380
for (final Screen s : Screen.getScreens()) {
74-
if (s.getBounds().getMinX() <= this.absolutX && this.absolutX <= s.getBounds().getMaxX()
75-
&& s.getBounds().getMinY() <= this.absolutY && this.absolutY <= s.getBounds().getMaxY()) {
81+
if (s.getBounds().contains(this.absoluteX, this.absoluteY)) {
7682
screen = s;
7783
break;
7884
}
7985
}
80-
this.screenhash = screen.hashCode();
81-
final double xInScreen = absolutX - screen.getBounds().getMinX();
82-
this.xProportion = xInScreen / screen.getBounds().getWidth();
83-
final double yInScreen = absolutY - screen.getBounds().getMinY();
84-
this.yProportion = yInScreen / screen.getBounds().getHeight();
86+
this.screenHash = screen.hashCode();
87+
final Rectangle2D bounds = screen.getBounds();
88+
final double xInScreen = absoluteX - bounds.getMinX();
89+
this.proportionalX = xInScreen / bounds.getWidth();
90+
final double yInScreen = absoluteY - bounds.getMinY();
91+
this.proportionalY = yInScreen / bounds.getHeight();
92+
93+
}
94+
95+
private void calcAbsolutePosition() {
96+
final Screen screen = getScreenWithHashOrPrimary(this.screenHash);
97+
final Rectangle2D screenBounds = screen.getBounds();
8598

99+
this.absoluteX = screenBounds.getMinX() + (screenBounds.getWidth() * this.proportionalX);
100+
this.absoluteY = screenBounds.getMinY() + (screenBounds.getHeight() * this.proportionalY);
86101
}
87102

88-
private void calcAbsolutScreenpos() {
89-
final Screen screen = getScreenWithHashOrPrimary(this.screenhash);
90-
this.absolutX = screen.getBounds().getMinX() + (screen.getBounds().getWidth() * this.xProportion);
91-
this.absolutY = screen.getBounds().getMinY() + (screen.getBounds().getHeight() * this.yProportion);
103+
private boolean isPositionValid() {
104+
final Screen screen = getScreenWithHashOrPrimary(this.screenHash);
105+
final Rectangle2D screenBounds = screen.getBounds();
106+
return screenBounds.contains(this.absoluteX, this.absoluteY);
92107
}
93108

94109
private Screen getScreenWithHashOrPrimary(final int screenHash) {
@@ -97,6 +112,7 @@ private Screen getScreenWithHashOrPrimary(final int screenHash) {
97112
return s;
98113
}
99114
}
115+
LOG.warn("Could not find wanted screen with hash '{}'. Using primary.", screenHash);
100116
return Screen.getPrimary();
101117
}
102118

0 commit comments

Comments
 (0)