Skip to content

Commit

Permalink
save docker selections
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Dec 10, 2023
1 parent 9f3ed4a commit e7c45a8
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 49 deletions.
7 changes: 2 additions & 5 deletions enigma-swing/src/main/java/org/quiltmc/enigma/gui/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.quiltmc.enigma.api.analysis.EntryReference;
import org.quiltmc.enigma.api.translation.mapping.EntryMapping;
import org.quiltmc.enigma.api.translation.mapping.EntryRemapper;
import org.quiltmc.enigma.gui.config.DockerConfig;
import org.quiltmc.enigma.gui.config.Config;
import org.quiltmc.enigma.gui.dialog.JavadocDialog;
import org.quiltmc.enigma.gui.dialog.SearchDialog;
Expand Down Expand Up @@ -145,10 +144,8 @@ private void setupDockers() {
this.dockerManager.registerDocker(new AllClassesDocker(this));
this.dockerManager.registerDocker(new DeobfuscatedClassesDocker(this));

if (Config.dockers().dockerLocations.value().isEmpty()) {
for (var entry : DockerConfig.getDefaultLocations(this.dockerManager).entrySet()) {
Config.dockers().putLocation(entry.getKey(), entry.getValue());
}
if (Config.dockers().buttonLocations.value().isEmpty()) {
Config.dockers().updateButtonLocations(this.dockerManager);
}

// set default docker sizes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,16 @@

import org.quiltmc.config.api.ReflectiveConfig;
import org.quiltmc.config.api.annotations.SerializedName;
import org.quiltmc.config.api.values.ComplexConfigValue;
import org.quiltmc.config.api.values.ConfigSerializableObject;
import org.quiltmc.config.api.values.TrackedValue;
import org.quiltmc.config.api.values.ValueMap;
import org.quiltmc.enigma.gui.docker.AllClassesDocker;
import org.quiltmc.enigma.gui.docker.CallsTreeDocker;
import org.quiltmc.enigma.gui.docker.CollabDocker;
import org.quiltmc.enigma.gui.docker.DeobfuscatedClassesDocker;
import org.quiltmc.enigma.gui.docker.Docker;
import org.quiltmc.enigma.gui.docker.DockerManager;
import org.quiltmc.enigma.gui.docker.ImplementationsTreeDocker;
import org.quiltmc.enigma.gui.docker.InheritanceTreeDocker;
import org.quiltmc.enigma.gui.docker.NotificationsDocker;
import org.quiltmc.enigma.gui.docker.ObfuscatedClassesDocker;
import org.quiltmc.enigma.gui.docker.StructureDocker;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class DockerConfig extends ReflectiveConfig {
@SerializedName("left_vertical_divider_location")
Expand All @@ -33,28 +25,28 @@ public class DockerConfig extends ReflectiveConfig {
@SerializedName("saved_with_left_docker_open")
public final TrackedValue<Boolean> savedWithLeftDockerOpen = this.value(true);

@SerializedName("docker_locations")
public final TrackedValue<ValueMap<Docker.Location>> dockerLocations = this.map(new Docker.Location(Docker.Side.LEFT, Docker.VerticalLocation.TOP)).build();
@SerializedName("button_locations")
public final TrackedValue<ValueMap<Docker.Location>> buttonLocations = this.map(new Docker.Location(Docker.Side.LEFT, Docker.VerticalLocation.TOP)).build();
@SerializedName("left_dockers")
public final TrackedValue<SelectedDockers> leftDockers = this.value(new SelectedDockers("", "", "all_classes"));
@SerializedName("right_dockers")
public final TrackedValue<SelectedDockers> rightDockers = this.value(new SelectedDockers("", "", "structure"));

public void putLocation(Docker docker, Docker.Location location) {
this.putLocation(docker.getId(), location);
public SelectedDockers getSelectedDockers(Docker.Side side) {
return side == Docker.Side.LEFT ? this.leftDockers.value() : this.rightDockers.value();
}

public void putLocation(String id, Docker.Location location) {
this.dockerLocations.value().put(id, location);
public void putButtonLocation(String id, Docker.Location location) {
this.buttonLocations.value().put(id, location);
}

public void putLocation(Docker docker, Docker.Side side, Docker.VerticalLocation verticalLocation) {
this.putLocation(docker.getId(), new Docker.Location(side, verticalLocation));
public void putButtonLocation(Docker docker, Docker.Side side, Docker.VerticalLocation verticalLocation) {
this.putButtonLocation(docker.getId(), new Docker.Location(side, verticalLocation));
}

@Nullable
public Docker.Location getLocation(String id) {
return this.dockerLocations.value().get(id);
}

public Map<String, Docker.Location> getLocations(Docker.Side side) {
return this.dockerLocations.value().entrySet().stream().filter((entry) -> entry.getValue().side() == side).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
public Docker.Location getButtonLocation(String id) {
return this.buttonLocations.value().get(id);
}

public int getVerticalDividerLocation(Docker.Side side) {
Expand All @@ -81,23 +73,75 @@ public void setHorizontalDividerLocation(Docker.Side side, int value) {
}
}

public static Map<Docker, Docker.Location> getDefaultLocations(DockerManager manager) {
Map<Docker, Docker.Location> locations = new HashMap<>();
public void updateButtonLocations(DockerManager manager) {
for (Docker docker : manager.getDockers()) {
this.putButtonLocation(docker.getId(), docker.getPreferredButtonLocation());
}
}

public static class SelectedDockers implements ConfigSerializableObject<ValueMap<String>> {
private String top;
private String bottom;
private String full;

public SelectedDockers(String top, String bottom, String full) {
this.top = top;
this.bottom = bottom;
this.full = full;
}

public void add(String id, Docker.VerticalLocation location) {
switch (location) {
case TOP -> {
this.full = "";
this.top = id;
}
case BOTTOM -> {
this.full = "";
this.bottom = id;
}
case FULL -> {
this.top = "";
this.bottom = "";
this.full = id;
}
}
}

public Map<String, Docker.VerticalLocation> asMap() {
Map<String, Docker.VerticalLocation> map = new HashMap<>();
if (!this.top.isBlank()) {
map.put(this.top, Docker.VerticalLocation.TOP);
}

if (!this.bottom.isBlank()) {
map.put(this.bottom, Docker.VerticalLocation.BOTTOM);
}

if (!this.full.isBlank()) {
map.put(this.full, Docker.VerticalLocation.FULL);
}

// left
locations.put(manager.getDocker(ObfuscatedClassesDocker.class), new Docker.Location(Docker.Side.LEFT, Docker.VerticalLocation.TOP));
locations.put(manager.getDocker(AllClassesDocker.class), new Docker.Location(Docker.Side.LEFT, Docker.VerticalLocation.TOP));
locations.put(manager.getDocker(DeobfuscatedClassesDocker.class), new Docker.Location(Docker.Side.LEFT, Docker.VerticalLocation.BOTTOM));
return map;
}

// right
locations.put(manager.getDocker(StructureDocker.class), new Docker.Location(Docker.Side.RIGHT, Docker.VerticalLocation.TOP));
locations.put(manager.getDocker(InheritanceTreeDocker.class), new Docker.Location(Docker.Side.RIGHT, Docker.VerticalLocation.TOP));
locations.put(manager.getDocker(ImplementationsTreeDocker.class), new Docker.Location(Docker.Side.RIGHT, Docker.VerticalLocation.TOP));
locations.put(manager.getDocker(CallsTreeDocker.class), new Docker.Location(Docker.Side.RIGHT, Docker.VerticalLocation.TOP));
@Override
public SelectedDockers convertFrom(ValueMap<String> representation) {
return new SelectedDockers(representation.get("top"), representation.get("bottom"), representation.get("full"));
}

locations.put(manager.getDocker(CollabDocker.class), new Docker.Location(Docker.Side.RIGHT, Docker.VerticalLocation.BOTTOM));
locations.put(manager.getDocker(NotificationsDocker.class), new Docker.Location(Docker.Side.RIGHT, Docker.VerticalLocation.BOTTOM));
@Override
public ValueMap<String> getRepresentation() {
return ValueMap.builder("")
.put("top", this.top)
.put("bottom", this.bottom)
.put("full", this.full)
.build();
}

return locations;
@Override
public ComplexConfigValue copy() {
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.quiltmc.enigma.gui.Gui;
import org.quiltmc.enigma.gui.config.Config;
import org.quiltmc.enigma.gui.config.DockerConfig;
import org.quiltmc.enigma.gui.docker.component.DockerButton;
import org.quiltmc.enigma.gui.docker.component.DockerSelector;
import org.quiltmc.enigma.gui.docker.component.Draggable;
Expand All @@ -17,7 +18,6 @@
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
Expand Down Expand Up @@ -63,8 +63,8 @@ public Dock(Gui gui, Docker.Side side) {
*/
public void restoreState(DockerManager manager) {
// restore docker state
Map<String, Docker.Location> hostedDockers = Config.dockers().getLocations(this.side);
hostedDockers.forEach((id, location) -> this.host(manager.getDocker(id), location.verticalLocation()));
DockerConfig.SelectedDockers hostedDockers = Config.dockers().getSelectedDockers(this.side);
hostedDockers.asMap().forEach((id, location) -> this.host(manager.getDocker(id), location));

this.restoreDividerState(true);

Expand Down Expand Up @@ -135,6 +135,8 @@ public void host(Docker docker, Docker.VerticalLocation verticalLocation) {
}

public void host(Docker docker, Docker.VerticalLocation verticalLocation, boolean avoidEmptySpace) {
Config.dockers().getSelectedDockers(this.side).add(docker.getId(), verticalLocation);

Dock dock = Util.findDock(docker);
if (dock != null) {
dock.removeDocker(verticalLocation, avoidEmptySpace);
Expand Down Expand Up @@ -175,7 +177,7 @@ public void host(Docker docker, Docker.VerticalLocation verticalLocation, boolea
parent.remove(button);
(verticalLocation == Docker.VerticalLocation.TOP ? selector.getTopSelector() : selector.getBottomSelector()).add(button);
button.setSide(this.side);
Config.dockers().putLocation(docker, this.side, verticalLocation);
Config.dockers().putButtonLocation(docker, this.side, verticalLocation);

button.getParent().revalidate();
button.getParent().repaint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public DockerButton getButton() {
* @return the position of the docker's button in the selector panels. this also represents where the docker will open when its button is clicked cannot use {@link Docker.VerticalLocation#FULL}
*/
public final Location getButtonLocation() {
Location savedLocation = Config.dockers().getLocation(this.getId());
Location savedLocation = Config.dockers().getButtonLocation(this.getId());
return savedLocation == null ? this.getPreferredButtonLocation() : savedLocation;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private boolean dropButton(DockerButton button, MouseEvent event) {
if (hoveredPanel != null) {
hoveredPanel.add(button);
button.setSide(this.side);
Config.dockers().putLocation(button.getDocker(), this.side, hoveredPanel.equals(this.bottomSelector) ? Docker.VerticalLocation.BOTTOM : Docker.VerticalLocation.TOP);
Config.dockers().putButtonLocation(button.getDocker(), this.side, hoveredPanel.equals(this.bottomSelector) ? Docker.VerticalLocation.BOTTOM : Docker.VerticalLocation.TOP);
return true;
}

Expand Down

0 comments on commit e7c45a8

Please sign in to comment.