Skip to content

Commit c5e374f

Browse files
committed
added save Position functionality closes #38
1 parent 4fa50bf commit c5e374f

File tree

7 files changed

+227
-25
lines changed

7 files changed

+227
-25
lines changed

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import de.doubleslash.keeptime.viewpopup.GlobalScreenListener;
4343
import de.doubleslash.keeptime.viewpopup.ViewControllerPopup;
4444
import javafx.application.Application;
45+
import javafx.beans.value.ChangeListener;
46+
import javafx.beans.value.ObservableValue;
4547
import javafx.event.EventHandler;
4648
import javafx.fxml.FXMLLoader;
4749
import javafx.scene.Parent;
@@ -58,6 +60,7 @@
5860
import javafx.scene.layout.Pane;
5961
import javafx.scene.layout.Priority;
6062
import javafx.scene.paint.Color;
63+
import javafx.stage.Screen;
6164
import javafx.stage.Stage;
6265
import javafx.stage.StageStyle;
6366
import javafx.stage.WindowEvent;
@@ -200,6 +203,10 @@ private void readSettings() {
200203
model.useHotkey.set(settings.isUseHotkey());
201204
model.displayProjectsRight.set(settings.isDisplayProjectsRight());
202205
model.hideProjectsOnMouseExit.set(settings.isHideProjectsOnMouseExit());
206+
model.windowPositionX.set(settings.getWindowPositionX());
207+
model.windowPositionY.set(settings.getWindowPositionY());
208+
model.screenHash.set(settings.getScreenHash());
209+
model.saveWindowPosition.set(settings.isSaveWindowPosition());
203210
}
204211

205212
private void initialisePopupUI(final Stage primaryStage) throws IOException {
@@ -234,6 +241,69 @@ private void initialisePopupUI(final Stage primaryStage) throws IOException {
234241

235242
private void initialiseUI(final Stage primaryStage) throws IOException {
236243
LOG.debug("Initialising main UI.");
244+
245+
if (Boolean.TRUE.equals(model.saveWindowPosition.get())) {
246+
LOG.info("Setting position of lightsStage to X: '{}' Y: '{}'.", model.getAbsolutePositionX(),
247+
model.getAbsolutePositionY());
248+
primaryStage.setX(model.getAbsolutePositionX());
249+
primaryStage.setY(model.getAbsolutePositionY());
250+
}
251+
252+
final ChangeListener<Number> posChange = new ChangeListener<Number>() {
253+
254+
@Override
255+
public void changed(final ObservableValue<? extends Number> observable, final Number oldValue,
256+
final Number newValue) {
257+
if (!model.saveWindowPosition.get()) {
258+
return;
259+
}
260+
261+
final Settings newSettings = new Settings(model.hoverBackgroundColor.get(), model.hoverFontColor.get(),
262+
model.defaultBackgroundColor.get(), model.defaultFontColor.get(), model.taskBarColor.get(),
263+
model.useHotkey.get(), model.displayProjectsRight.get(), model.hideProjectsOnMouseExit.get(),
264+
model.windowPositionX.get(), model.windowPositionY.get(), model.screenHash.get(),
265+
model.saveWindowPosition.get());
266+
267+
if (observable.equals(primaryStage.xProperty())) {
268+
Screen screen = Screen.getPrimary();
269+
for (final Screen s : Screen.getScreens()) {
270+
if (s.getVisualBounds().getMinX() <= newValue.doubleValue()
271+
&& newValue.doubleValue() <= s.getVisualBounds().getMaxX()
272+
&& s.getVisualBounds().getMinY() <= model.windowPositionY.get()
273+
&& model.windowPositionY.get() <= s.getVisualBounds().getMaxY()) {
274+
screen = s;
275+
break;
276+
}
277+
}
278+
newSettings.setScreenHash(screen.hashCode());
279+
final double xInScreen = newValue.doubleValue() - screen.getVisualBounds().getMinX();
280+
newSettings.setWindowPositionX(xInScreen / screen.getVisualBounds().getWidth());
281+
282+
} else {
283+
284+
Screen screen = Screen.getPrimary();
285+
for (final Screen s : Screen.getScreens()) {
286+
if (s.getVisualBounds().getMinY() <= newValue.doubleValue()
287+
&& newValue.doubleValue() <= s.getVisualBounds().getMaxY()
288+
&& s.getVisualBounds().getMinX() <= model.windowPositionX.get()
289+
&& model.windowPositionX.get() <= s.getVisualBounds().getMaxX()) {
290+
screen = s;
291+
break;
292+
}
293+
}
294+
newSettings.setScreenHash(screen.hashCode());
295+
final double yInScreen = newValue.doubleValue() - screen.getVisualBounds().getMinY();
296+
newSettings.setWindowPositionY(yInScreen / screen.getVisualBounds().getHeight());
297+
}
298+
299+
controller.updateSettings(newSettings);
300+
LOG.debug("updated Position to x: {} y: {}", model.windowPositionX.get(), model.windowPositionY.get());
301+
}
302+
303+
};
304+
primaryStage.xProperty().addListener(posChange);
305+
primaryStage.yProperty().addListener(posChange);
306+
237307
Pane mainPane;
238308

239309
// Load root layout from fxml file.

src/main/java/de/doubleslash/keeptime/controller/Controller.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import de.doubleslash.keeptime.model.Settings;
3737
import de.doubleslash.keeptime.model.Work;
3838
import javafx.collections.ObservableList;
39-
import javafx.scene.paint.Color;
4039

4140
@Service
4241
public class Controller {
@@ -102,23 +101,23 @@ public void addNewProject(final Project project) {
102101
model.getProjectRepository().saveAll(changedProjects);
103102
}
104103

105-
public void updateSettings(final Color hoverBackgroundColor, final Color hoverFontColor,
106-
final Color defaultBackgroundColor, final Color defaultFontColor, final Color taskBarColor,
107-
final boolean useHotkey, final boolean displayProjectsRight, final boolean hideProjectsOnMouseExit) {
108-
// TODO create holder for all the properties (or reuse Settings.class?)
109-
final Settings settings = model.getSettingsRepository().findAll().get(0);
110-
settings.setTaskBarColor(taskBarColor);
104+
public void updateSettings(final Settings newValuedSettings) {
105+
Settings settings = model.getSettingsRepository().findAll().get(0);
111106

112-
settings.setDefaultBackgroundColor(defaultBackgroundColor);
113-
settings.setDefaultFontColor(defaultFontColor);
107+
settings.setTaskBarColor(newValuedSettings.getTaskBarColor());
108+
settings.setDefaultBackgroundColor(newValuedSettings.getDefaultBackgroundColor());
109+
settings.setDefaultFontColor(newValuedSettings.getDefaultFontColor());
110+
settings.setHoverBackgroundColor(newValuedSettings.getHoverBackgroundColor());
111+
settings.setHoverFontColor(newValuedSettings.getHoverFontColor());
112+
settings.setUseHotkey(newValuedSettings.isUseHotkey());
113+
settings.setDisplayProjectsRight(newValuedSettings.isDisplayProjectsRight());
114+
settings.setHideProjectsOnMouseExit(newValuedSettings.isHideProjectsOnMouseExit());
115+
settings.setSaveWindowPosition(newValuedSettings.isSaveWindowPosition());
116+
settings.setWindowPositionX(newValuedSettings.getWindowPositionX());
117+
settings.setWindowPositionY(newValuedSettings.getWindowPositionY());
118+
settings.setScreenHash(newValuedSettings.getScreenHash());
114119

115-
settings.setHoverBackgroundColor(hoverBackgroundColor);
116-
settings.setHoverFontColor(hoverFontColor);
117-
settings.setUseHotkey(useHotkey);
118-
settings.setDisplayProjectsRight(displayProjectsRight);
119-
settings.setHideProjectsOnMouseExit(hideProjectsOnMouseExit);
120-
121-
model.getSettingsRepository().save(settings);
120+
settings = model.getSettingsRepository().save(settings);
122121

123122
model.defaultBackgroundColor.set(settings.getDefaultBackgroundColor());
124123
model.defaultFontColor.set(settings.getDefaultFontColor());
@@ -128,6 +127,10 @@ public void updateSettings(final Color hoverBackgroundColor, final Color hoverFo
128127
model.useHotkey.set(settings.isUseHotkey());
129128
model.displayProjectsRight.set(settings.isDisplayProjectsRight());
130129
model.hideProjectsOnMouseExit.set(settings.isHideProjectsOnMouseExit());
130+
model.saveWindowPosition.set(settings.isSaveWindowPosition());
131+
model.windowPositionX.set(settings.getWindowPositionX());
132+
model.windowPositionY.set(settings.getWindowPositionY());
133+
model.screenHash.set(settings.getScreenHash());
131134
}
132135

133136
@PreDestroy
@@ -188,13 +191,13 @@ public void editProject(final Project projectToBeUpdated, final Project newValue
188191
* Changes the indexes of the originalList parameter to have a consistent order.
189192
*
190193
* @param originalList
191-
* list of all projects to adapt the indexes for
194+
* list of all projects to adapt the indexes for
192195
* @param changedProject
193-
* the project which has changed which already has the new index
196+
* the project which has changed which already has the new index
194197
* @param oldIndex
195-
* the old index of the changed project
198+
* the old index of the changed project
196199
* @param newIndex
197-
* the new index of the changed project (which the projects also already has)
200+
* the new index of the changed project (which the projects also already has)
198201
* @return all projects whose index has been adapted
199202
*/
200203
List<Project> resortProjectIndexes(final List<Project> originalList, final Project changedProject,
@@ -231,9 +234,9 @@ List<Project> resortProjectIndexes(final List<Project> originalList, final Proje
231234
* Decreases all indexes by one, after the removed index
232235
*
233236
* @param originalList
234-
* list of all projects to adapt the indexes for
237+
* list of all projects to adapt the indexes for
235238
* @param removedIndex
236-
* the index which has been removed
239+
* the index which has been removed
237240
* @return all projects whose index has been adapted
238241
*/
239242
List<Project> adaptProjectIndexesAfterRemoving(final List<Project> originalList, final int removedIndex) {

src/main/java/de/doubleslash/keeptime/model/Model.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import javafx.collections.ObservableList;
3131
import javafx.collections.transformation.SortedList;
3232
import javafx.scene.paint.Color;
33+
import javafx.stage.Screen;
3334

3435
@Component
3536
public class Model {
@@ -77,6 +78,11 @@ public Model(final ProjectRepository projectRepository, final WorkRepository wor
7778
public final ObjectProperty<Boolean> displayProjectsRight = new SimpleObjectProperty<>(false);
7879
public final ObjectProperty<Boolean> hideProjectsOnMouseExit = new SimpleObjectProperty<>(true);
7980

81+
public final ObjectProperty<Boolean> saveWindowPosition = new SimpleObjectProperty<>(false);
82+
public final ObjectProperty<Double> windowPositionY = new SimpleObjectProperty<>(0.5);
83+
public final ObjectProperty<Double> windowPositionX = new SimpleObjectProperty<>(0.5);
84+
public final ObjectProperty<Integer> screenHash = new SimpleObjectProperty<>(0);
85+
8086
public void setWorkRepository(final WorkRepository workRepository) {
8187
this.workRepository = workRepository;
8288
}
@@ -128,4 +134,26 @@ public ObservableList<Project> getAvailableProjects() {
128134
public ObservableList<Project> getAllProjects() {
129135
return allProjects;
130136
}
137+
138+
public double getAbsolutePositionX() {
139+
final Screen toDisplay = getScreenWithHashOrPrimary(screenHash.get());
140+
return toDisplay.getVisualBounds().getMinX() + (toDisplay.getVisualBounds().getWidth() * windowPositionX.get());
141+
142+
}
143+
144+
public double getAbsolutePositionY() {
145+
final Screen toDisplay = getScreenWithHashOrPrimary(screenHash.get());
146+
return toDisplay.getVisualBounds().getMinY() + (toDisplay.getVisualBounds().getHeight() * windowPositionY.get());
147+
148+
}
149+
150+
private Screen getScreenWithHashOrPrimary(final int hash) {
151+
152+
for (final Screen s : Screen.getScreens()) {
153+
if (s.hashCode() == hash) {
154+
return s;
155+
}
156+
}
157+
return Screen.getPrimary();
158+
}
131159
}

src/main/java/de/doubleslash/keeptime/model/Settings.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,36 @@ public class Settings {
5959

6060
private boolean hideProjectsOnMouseExit;
6161

62+
private double windowPositionX;
63+
64+
private double windowPositionY;
65+
66+
private int screenHash;
67+
68+
private boolean saveWindowPosition;
69+
70+
public Settings() {
71+
}
72+
73+
public Settings(final Color hoverBackgroundColor, final Color hoverFontColor, final Color defaultBackgroundColor,
74+
final Color defaultFontColor, final Color taskBarColor, final boolean useHotkey,
75+
final boolean displayProjectsRight, final boolean hideProjectsOnMouseExit, final double windowPositionX,
76+
final double windowPositionY, final int screenHash, final boolean saveWindowPosition) {
77+
this.hoverBackgroundColor = hoverBackgroundColor;
78+
this.hoverFontColor = hoverFontColor;
79+
this.defaultBackgroundColor = defaultBackgroundColor;
80+
this.defaultFontColor = defaultFontColor;
81+
this.taskBarColor = taskBarColor;
82+
this.useHotkey = useHotkey;
83+
this.displayProjectsRight = displayProjectsRight;
84+
this.hideProjectsOnMouseExit = hideProjectsOnMouseExit;
85+
this.windowPositionX = windowPositionX;
86+
this.windowPositionY = windowPositionY;
87+
this.screenHash = screenHash;
88+
this.saveWindowPosition = saveWindowPosition;
89+
90+
}
91+
6292
public long getId() {
6393
return id;
6494
}
@@ -127,4 +157,36 @@ public void setHideProjectsOnMouseExit(final boolean hideProjectsOnMouseExit) {
127157
this.hideProjectsOnMouseExit = hideProjectsOnMouseExit;
128158
}
129159

160+
public double getWindowPositionX() {
161+
return windowPositionX;
162+
}
163+
164+
public void setWindowPositionX(final double windowPositionX) {
165+
this.windowPositionX = windowPositionX;
166+
}
167+
168+
public double getWindowPositionY() {
169+
return windowPositionY;
170+
}
171+
172+
public void setWindowPositionY(final double windowPositionY) {
173+
this.windowPositionY = windowPositionY;
174+
}
175+
176+
public int getScreenHash() {
177+
return screenHash;
178+
}
179+
180+
public void setScreenHash(final int screenHash) {
181+
this.screenHash = screenHash;
182+
}
183+
184+
public boolean isSaveWindowPosition() {
185+
return saveWindowPosition;
186+
}
187+
188+
public void setSaveWindowPosition(final boolean saveWindowPosition) {
189+
this.saveWindowPosition = saveWindowPosition;
190+
}
191+
130192
}

src/main/java/de/doubleslash/keeptime/view/SettingsController.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import de.doubleslash.keeptime.controller.Controller;
2828
import de.doubleslash.keeptime.exceptions.FXMLLoaderException;
2929
import de.doubleslash.keeptime.model.Model;
30+
import de.doubleslash.keeptime.model.Settings;
3031
import javafx.fxml.FXML;
3132
import javafx.fxml.FXMLLoader;
3233
import javafx.scene.Parent;
@@ -75,6 +76,8 @@ public class SettingsController {
7576
private CheckBox displayProjectsRightCheckBox;
7677
@FXML
7778
private CheckBox hideProjectsOnMouseExitCheckBox;
79+
@FXML
80+
private CheckBox saveWindowPositionCheckBox;
7881

7982
@FXML
8083
private Button saveButton;
@@ -161,10 +164,11 @@ private void initialize() {
161164
}
162165
}
163166

164-
controller.updateSettings(hoverBackgroundColor.getValue(), hoverFontColor.getValue(),
167+
controller.updateSettings(new Settings(hoverBackgroundColor.getValue(), hoverFontColor.getValue(),
165168
defaultBackgroundColor.getValue(), defaultFontColor.getValue(), taskBarColor.getValue(),
166169
useHotkeyCheckBox.isSelected(), displayProjectsRightCheckBox.isSelected(),
167-
hideProjectsOnMouseExitCheckBox.isSelected());
170+
hideProjectsOnMouseExitCheckBox.isSelected(), model.windowPositionX.get(), model.windowPositionY.get(),
171+
model.screenHash.get(), saveWindowPositionCheckBox.isSelected()));
168172
thisStage.close();
169173

170174
});
@@ -212,6 +216,7 @@ void update() {
212216
useHotkeyCheckBox.setSelected(model.useHotkey.get());
213217
displayProjectsRightCheckBox.setSelected(model.displayProjectsRight.get());
214218
hideProjectsOnMouseExitCheckBox.setSelected(model.hideProjectsOnMouseExit.get());
219+
saveWindowPositionCheckBox.setSelected(model.saveWindowPosition.get());
215220
}
216221

217222
public void setStage(final Stage thisStage) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ALTER TABLE settings ADD COLUMN window_positionx DOUBLE NOT NULL DEFAULT 0.5;
2+
ALTER TABLE settings ADD COLUMN window_positiony DOUBLE NOT NULL DEFAULT 0.5;
3+
ALTER TABLE settings ADD COLUMN screen_hash INT NOT NULL DEFAULT 0;
4+
ALTER TABLE settings ADD COLUMN save_window_position BOOLEAN NOT NULL DEFAULT false

src/main/resources/layouts/settings.fxml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<?import javafx.scene.layout.VBox?>
2929
<?import javafx.scene.text.Font?>
3030

31-
<AnchorPane fx:id="settingsRoot" focusTraversable="true" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.doubleslash.keeptime.view.SettingsController">
31+
<AnchorPane fx:id="settingsRoot" focusTraversable="true" xmlns="http://javafx.com/javafx/8.0.202-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.doubleslash.keeptime.view.SettingsController">
3232
<children>
3333
<VBox spacing="10.0" stylesheets="@../css/menu.css">
3434
<children>
@@ -224,6 +224,36 @@
224224
</Group>
225225
</children>
226226
</VBox>
227+
<VBox styleClass="menuBorder">
228+
<children>
229+
<Group>
230+
<children>
231+
<VBox>
232+
<children>
233+
<HBox spacing="10.0">
234+
<children>
235+
<Label fx:id="hotkeyLabel1" text="Position">
236+
<font>
237+
<Font name="Open Sans Bold" size="12.0" />
238+
</font>
239+
</Label>
240+
</children>
241+
</HBox>
242+
<HBox spacing="10.0">
243+
<children>
244+
<CheckBox fx:id="saveWindowPositionCheckBox" mnemonicParsing="false" text="save Position on Screen">
245+
<font>
246+
<Font name="Open Sans Regular" size="12.0" />
247+
</font>
248+
</CheckBox>
249+
</children>
250+
</HBox>
251+
</children>
252+
</VBox>
253+
</children>
254+
</Group>
255+
</children>
256+
</VBox>
227257
<HBox spacing="10.0">
228258
<children>
229259
<Button fx:id="saveButton" mnemonicParsing="false" text="Save">

0 commit comments

Comments
 (0)