Fix iterators in WindowsManager and GLFWInput which didn't allow to remove items from loops

This commit is contained in:
2021-03-13 14:42:30 +01:00
parent d063679c3a
commit dfa07781f7
2 changed files with 7 additions and 3 deletions

View File

@@ -63,7 +63,9 @@ public final class WindowManager extends BaseWidget {
} }
public void closeAll() { public void closeAll() {
for (var ignored : windows) {
// Use iterator to support removal from loop inside
for (var iterator = windows.iterator(); iterator.hasNext(); ) {
close(); close();
} }
} }

View File

@@ -23,17 +23,19 @@ public class GLFWInput implements Input {
this.windowHandle = screen.getID(); this.windowHandle = screen.getID();
} }
@SuppressWarnings("ForLoopReplaceableByForEach")
public GLFWInput init() { public GLFWInput init() {
log.info("Registering key callback"); log.info("Registering key callback");
glfwSetKeyCallback(windowHandle, (window, key, scancode, action, mods) -> { glfwSetKeyCallback(windowHandle, (window, key, scancode, action, mods) -> {
var event = GLFWKeyEvent.of(key, action); var event = GLFWKeyEvent.of(key, action);
for (var handler : keyEventHandlers) { // Use iterator to support removal from loop inside
for (var iterator = keyEventHandlers.iterator(); iterator.hasNext(); ) {
if (event.isConsumed()) { if (event.isConsumed()) {
return; return;
} }
handler.handleKeyEvent(event); iterator.next().handleKeyEvent(event);
} }
}); });