Skip to content

Commit

Permalink
Windows will now retain their previous size and location if they are …
Browse files Browse the repository at this point in the history
…persisted in the maximized state. (#201)
  • Loading branch information
andrewauclair authored Jan 21, 2024
1 parent 7c58856 commit d05f768
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
32 changes: 32 additions & 0 deletions docking-api/src/ModernDocking/api/AppStateAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ of this software and associated documentation files (the "Software"), to deal
import ModernDocking.internal.DockingInternal;
import ModernDocking.layouts.ApplicationLayout;
import ModernDocking.layouts.DockingLayouts;
import ModernDocking.layouts.WindowLayout;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -47,6 +51,7 @@ public class AppStateAPI {
private static final Map<DockingAPI, File> autoPersistFiles = new HashMap<>();

private static ApplicationLayout defaultAppLayout = null;
private static ApplicationLayout lastPersistedLayout = null;

private static boolean paused = false;

Expand Down Expand Up @@ -131,6 +136,33 @@ public void actionPerformed(ActionEvent e) {
if (!paused) {
ApplicationLayout layout = docking.getDockingState().getApplicationLayout();

if (lastPersistedLayout != null) {
if (layout.getMainFrameLayout().getState() != Frame.NORMAL){
// set position and size of all frames into the new layout
layout.getMainFrameLayout().setLocation(lastPersistedLayout.getMainFrameLayout().getLocation());
layout.getMainFrameLayout().setSize(lastPersistedLayout.getMainFrameLayout().getSize());
}

List<WindowLayout> oldFrames = lastPersistedLayout.getFloatingFrameLayouts();
List<WindowLayout> newFrames = layout.getFloatingFrameLayouts();

for (WindowLayout newFrame : newFrames) {
if (newFrame.getState() == Frame.NORMAL) {
continue;
}

Optional<WindowLayout> oldFrame = oldFrames.stream()
.filter(windowLayout -> windowLayout.getWindowHashCode() == newFrame.getWindowHashCode())
.findFirst();

if (oldFrame.isPresent()) {
newFrame.setLocation(oldFrame.get().getLocation());
newFrame.setSize(oldFrame.get().getSize());
}
}
}
lastPersistedLayout = layout;

try {
docking.getLayoutPersistence().saveLayoutToFile(autoPersistFiles.get(docking), layout);

Expand Down
21 changes: 19 additions & 2 deletions docking-api/src/ModernDocking/layouts/WindowLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ of this software and associated documentation files (the "Software"), to deal
*/
public class WindowLayout {
private boolean isMainFrame;
private final Point location;
private Point location;
private final boolean hasSizeAndLocationInformation;
private final Dimension size;
private Dimension size;
private final int state;
private final ModalityType modalityType;
private final DockingLayoutNode rootNode;
private String maximizedDockable = null;

private final int windowHashCode;

private final List<String> westUnpinnedToolbarIDs = new ArrayList<>();
private final List<String> eastUnpinnedToolbarIDs = new ArrayList<>();
private final List<String> southUnpinnedToolbarIDs = new ArrayList<>();
Expand All @@ -60,6 +62,7 @@ public WindowLayout(boolean isMainFrame, Point location, Dimension size, int sta
this.state = state;
this.rootNode = rootNode;
this.modalityType = ModalityType.MODELESS;
this.windowHashCode = 0;

hasSizeAndLocationInformation = true;
}
Expand All @@ -72,6 +75,7 @@ public WindowLayout(boolean isMainFrame, Point location, Dimension size, int sta
public WindowLayout(DockingLayoutNode rootNode) {
this.rootNode = rootNode;
this.state = Frame.NORMAL;
this.windowHashCode = 0;

hasSizeAndLocationInformation = false;

Expand All @@ -90,6 +94,7 @@ public WindowLayout(Window window, DockingLayoutNode rootNode) {
this.rootNode = rootNode;
this.location = window.getLocation();
this.size = window.getSize();
this.windowHashCode = window.hashCode();

if (window instanceof JFrame) {
this.state = ((JFrame) window).getExtendedState();
Expand Down Expand Up @@ -121,6 +126,10 @@ public Point getLocation() {
return location;
}

public void setLocation(Point location) {
this.location = location;
}

/**
* Get the size of this window
*
Expand All @@ -130,6 +139,10 @@ public Dimension getSize() {
return size;
}

public void setSize(Dimension size) {
this.size = size;
}

public int getState() {
return state;
}
Expand Down Expand Up @@ -215,4 +228,8 @@ public List<String> getSouthUnpinnedToolbarIDs() {
public boolean hasSizeAndLocationInformation() {
return hasSizeAndLocationInformation;
}

public int getWindowHashCode() {
return windowHashCode;
}
}

0 comments on commit d05f768

Please sign in to comment.