Skip to content

Commit

Permalink
Make window layer identifiers into big integers
Browse files Browse the repository at this point in the history
Affects: #45
  • Loading branch information
io7m committed Dec 8, 2023
1 parent 6028711 commit 5a83e17
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 88 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright © 2022 Mark Raynsford <code@io7m.com> https://www.io7m.com
* Copyright © 2023 Mark Raynsford <code@io7m.com> https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand All @@ -17,50 +17,51 @@

package com.io7m.jsycamore.api.windows;

import java.math.BigInteger;

/**
* Conventional values for window layers.
* An identifier for a window layer.
*
* @param value The value
*/

public final class SyWindowLayers
public record SyWindowLayerID(BigInteger value)
implements Comparable<SyWindowLayerID>
{
private SyWindowLayers()
{

}

/**
* @return The layer identifier used for normal windows
*/
private static final SyWindowLayerID FIRST_ID =
new SyWindowLayerID(BigInteger.ZERO);

public static int layerForNormalWindows()
@Override
public int compareTo(
final SyWindowLayerID other)
{
return 0;
return this.value.compareTo(other.value);
}

/**
* @return The layer identifier used for menus
* @return The next layer ID
*/

public static int layerForMenus()
public SyWindowLayerID nextHigher()
{
return layerHighest();
return new SyWindowLayerID(this.value.add(BigInteger.ONE));
}

/**
* @return The lowest (furthest away from the viewer) layer
* @return The next layer ID
*/

public static int layerLowest()
public SyWindowLayerID nextLower()
{
return Integer.MIN_VALUE;
return new SyWindowLayerID(this.value.subtract(BigInteger.ONE));
}

/**
* @return The highest (closest to the viewer) layer
* @return The default layer ID
*/

public static int layerHighest()
public static SyWindowLayerID defaultLayer()
{
return Integer.MAX_VALUE;
return FIRST_ID;
}
}
Expand Up @@ -110,7 +110,7 @@ public interface SyWindowReadableType
* @return The window layer
*/

int layer();
SyWindowLayerID layer();

/**
* @return The window deletion policy
Expand Down
Expand Up @@ -27,6 +27,18 @@

public interface SyWindowServiceType extends SyServiceType
{
/**
* @return The default layer for windows
*/

SyWindowLayerID windowLayerDefault();

/**
* @return The default layer for menus
*/

SyWindowLayerID windowLayerForMenus();

/**
* Create a new window.
*
Expand All @@ -43,7 +55,7 @@ default SyWindowType windowCreate(
return this.windowCreateOnLayer(
sizeX,
sizeY,
SyWindowLayers.layerForNormalWindows()
this.windowLayerDefault()
);
}

Expand All @@ -60,7 +72,7 @@ default SyWindowType windowCreate(
SyWindowType windowCreateOnLayer(
int sizeX,
int sizeY,
int layer);
SyWindowLayerID layer);

/**
* @return A read-only list of the currently open windows in order from
Expand Down
Expand Up @@ -41,13 +41,13 @@
public final class SyWindowSet
{
private final Map<SyWindowID, SyWindowType> windows;
private final TreeMap<Integer, LinkedList<SyWindowType>> windowsVisibleOrdered;
private final Set<Integer> windowLayersHidden;
private final TreeMap<SyWindowLayerID, LinkedList<SyWindowType>> windowsVisibleOrdered;
private final Set<SyWindowLayerID> windowLayersHidden;

private SyWindowSet(
final Map<SyWindowID, SyWindowType> inWindows,
final TreeMap<Integer, LinkedList<SyWindowType>> inWindowsVisibleOrdered,
final Set<Integer> inWindowLayersVisible)
final TreeMap<SyWindowLayerID, LinkedList<SyWindowType>> inWindowsVisibleOrdered,
final Set<SyWindowLayerID> inWindowLayersVisible)
{
this.windows = Map.copyOf(inWindows);
this.windowsVisibleOrdered = inWindowsVisibleOrdered;
Expand Down Expand Up @@ -83,7 +83,7 @@ public static SyWindowSet empty()
*/

public Optional<SyWindowType> windowFocused(
final int layer)
final SyWindowLayerID layer)
{
final var visible =
this.windowsVisibleOrdered.get(layer);
Expand Down Expand Up @@ -183,10 +183,10 @@ public SyWindowSetChanged windowShow(

final var newMap =
new TreeMap<>(this.windowsVisibleOrdered);
final var layerBoxed =
Integer.valueOf(window.layer());
final var windowLayer =
window.layer();
final var newVisible =
new LinkedList<>(newMap.getOrDefault(layerBoxed, new LinkedList<>()));
new LinkedList<>(newMap.getOrDefault(windowLayer, new LinkedList<>()));

final var removed = newVisible.remove(window);
final List<SyWindowEventType> changes;
Expand All @@ -196,7 +196,7 @@ public SyWindowSetChanged windowShow(
changes = List.of(new SyWindowBecameVisible(window.id()));
}
newVisible.addFirst(window);
newMap.put(layerBoxed, newVisible);
newMap.put(windowLayer, newVisible);

return new SyWindowSetChanged(
new SyWindowSet(this.windows, newMap, this.windowLayersHidden),
Expand Down Expand Up @@ -232,10 +232,10 @@ public SyWindowSetChanged windowHide(

final var newMap =
new TreeMap<>(this.windowsVisibleOrdered);
final var layerBoxed =
Integer.valueOf(window.layer());
final var windowLayer =
window.layer();
final var newVisible =
new LinkedList<>(newMap.getOrDefault(layerBoxed, new LinkedList<>()));
new LinkedList<>(newMap.getOrDefault(windowLayer, new LinkedList<>()));

final var removed = newVisible.remove(window);
final List<SyWindowEventType> changes;
Expand All @@ -244,7 +244,7 @@ public SyWindowSetChanged windowHide(
} else {
changes = List.of();
}
newMap.put(layerBoxed, newVisible);
newMap.put(windowLayer, newVisible);

return new SyWindowSetChanged(
new SyWindowSet(this.windows, newMap, this.windowLayersHidden),
Expand Down Expand Up @@ -283,17 +283,17 @@ public SyWindowSetChanged windowClose(
final var newMap =
new TreeMap<>(this.windowsVisibleOrdered);

final var layerBoxed =
Integer.valueOf(window.layer());
final var windowLayer =
window.layer();
final var newVisible =
new LinkedList<>(newMap.getOrDefault(layerBoxed, new LinkedList<>()));
new LinkedList<>(newMap.getOrDefault(windowLayer, new LinkedList<>()));

newVisible.remove(window);

final List<SyWindowEventType> changes =
List.of(new SyWindowClosed(window.id()));

newMap.put(layerBoxed, newVisible);
newMap.put(windowLayer, newVisible);
newWindows.remove(window.id());

return new SyWindowSetChanged(
Expand All @@ -320,17 +320,16 @@ public SyWindowSetChanged windowFocus(
final var newMap =
new TreeMap<>(this.windowsVisibleOrdered);

final var layerBoxed =
Integer.valueOf(window.layer());

final var windowLayer =
window.layer();
final var existingVisible =
newMap.getOrDefault(layerBoxed, new LinkedList<>());
newMap.getOrDefault(windowLayer, new LinkedList<>());
final var newVisible =
new LinkedList<>(existingVisible);

newVisible.remove(window);
newVisible.addFirst(window);
newMap.put(layerBoxed, newVisible);
newMap.put(windowLayer, newVisible);

return new SyWindowSetChanged(
new SyWindowSet(this.windows, newMap, this.windowLayersHidden),
Expand All @@ -349,13 +348,13 @@ public boolean windowIsVisible(
{
Objects.requireNonNull(window, "window");

final var layerBoxed = Integer.valueOf(window.layer());
if (this.windowLayersHidden.contains(layerBoxed)) {
final var windowLayer = window.layer();
if (this.windowLayersHidden.contains(windowLayer)) {
return false;
}

final var layer =
this.windowsVisibleOrdered.get(layerBoxed);
this.windowsVisibleOrdered.get(windowLayer);

if (layer != null) {
return layer.contains(window);
Expand All @@ -373,11 +372,12 @@ public boolean windowIsVisible(
*/

public SyWindowSetChanged windowLayerHide(
final int layer)
final SyWindowLayerID layer)
{
final var layerBoxed = Integer.valueOf(layer);
Objects.requireNonNull(layer, "layer");

final var newHidden = new HashSet<>(this.windowLayersHidden);
newHidden.add(layerBoxed);
newHidden.add(layer);

return new SyWindowSetChanged(
new SyWindowSet(
Expand All @@ -397,11 +397,12 @@ public SyWindowSetChanged windowLayerHide(
*/

public SyWindowSetChanged windowLayerShow(
final int layer)
final SyWindowLayerID layer)
{
final var layerBoxed = Integer.valueOf(layer);
Objects.requireNonNull(layer, "layer");

final var newHidden = new HashSet<>(this.windowLayersHidden);
newHidden.remove(layerBoxed);
newHidden.remove(layer);

return new SyWindowSetChanged(
new SyWindowSet(
Expand All @@ -419,9 +420,10 @@ public SyWindowSetChanged windowLayerShow(
*/

public boolean windowLayerIsShown(
final int layer)
final SyWindowLayerID layer)
{
return !this.windowLayersHidden.contains(Integer.valueOf(layer));
Objects.requireNonNull(layer, "layer");
return !this.windowLayersHidden.contains(layer);
}

/**
Expand Down

0 comments on commit 5a83e17

Please sign in to comment.