Skip to content

Commit

Permalink
refactor(framework): Implement requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
LeStegii committed Mar 4, 2024
1 parent 1f383da commit 1f19370
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,23 +233,16 @@ else if (view.startsWith("#")) {
*/
private void registerKeyEvents(Object instance) {
Reflection.getMethodsWithAnnotation(instance.getClass(), onKey.class).forEach(method -> {

onKey annotation = method.getAnnotation(onKey.class);
EventType<KeyEvent> type = annotation.type().asEventType();
EventHandler<KeyEvent> handler = createKeyEventHandler(method, instance, annotation);

keyEventHandlers.computeIfAbsent(instance, k -> new HashSet<>()).add(new KeyEventHolder(annotation.target(), type, handler));

switch (annotation.target()) {
case SCENE -> {
EventHandler<KeyEvent> handler = createKeyEventHandler(method, instance, annotation);
keyEventHandlers.computeIfAbsent(instance, k -> new HashSet<>());
keyEventHandlers.get(instance).add(new KeyEventHolder(onKey.Target.SCENE, type, handler));
app.get().stage().getScene().addEventFilter(type, handler);
}
case STAGE -> {
System.out.println("Registering key event for stage");
EventHandler<KeyEvent> handler = createKeyEventHandler(method, instance, annotation);
keyEventHandlers.computeIfAbsent(instance, k -> new HashSet<>());
keyEventHandlers.get(instance).add(new KeyEventHolder(onKey.Target.STAGE, type, handler));
app.get().stage().addEventFilter(type, handler);
}
case SCENE -> app.get().stage().getScene().addEventFilter(type, handler);
case STAGE -> app.get().stage().addEventFilter(type, handler);
}
});
}
Expand Down Expand Up @@ -674,7 +667,7 @@ private EventHandler<KeyEvent> createKeyEventHandler(Method method, Object insta
method.setAccessible(true);

return event -> {
if (shouldBeCalled(event, annotation)) {
if (keyEventMatchesAnnotation(event, annotation)) {
try {
if (hasEventParameter) {
method.invoke(instance, event);
Expand All @@ -688,7 +681,7 @@ private EventHandler<KeyEvent> createKeyEventHandler(Method method, Object insta
};
}

private boolean shouldBeCalled(KeyEvent event, onKey annotation) {
private boolean keyEventMatchesAnnotation(KeyEvent event, onKey annotation) {
return (annotation.code() == KeyCode.UNDEFINED || event.getCode() == annotation.code()) &&
(annotation.character().isEmpty() || event.getCharacter().equals(annotation.character())) &&
(annotation.text().isEmpty() || event.getText().equals(annotation.text())) &&
Expand All @@ -704,7 +697,7 @@ private boolean shouldBeCalled(KeyEvent event, onKey annotation) {
* @param instance The instance to clear the key handlers for
*/
private void cleanUpListeners(Object instance) {
var handlers = keyEventHandlers.get(instance);
Collection<KeyEventHolder> handlers = keyEventHandlers.get(instance);
if (handlers != null) {
for (KeyEventHolder holder : handlers) {
switch (holder.target()) {
Expand All @@ -716,7 +709,6 @@ private void cleanUpListeners(Object instance) {
}
}


/**
* Returns the title of the given controller instance if it has one.
* If the title is a key, the title will be looked up in the resource bundle of the controller.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.fulib.fx.util;

import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.input.KeyEvent;
import org.fulib.fx.annotation.event.onKey;

public record KeyEventHolder(
onKey.Target target,
javafx.event.EventType<KeyEvent> type,
EventType<KeyEvent> type,
EventHandler<KeyEvent> handler
) {
}

0 comments on commit 1f19370

Please sign in to comment.