Skip to content

Commit

Permalink
PERF : Don't notify if the PTWidget is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
NSV committed Sep 17, 2018
1 parent 68565f3 commit ad413dc
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,26 @@

package com.ponysdk.core.terminal.ui;

import java.util.function.Consumer;

import com.google.gwt.event.dom.client.DomEvent;
import com.google.gwt.event.dom.client.KeyEvent;
import com.google.gwt.event.dom.client.MouseEvent;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.ui.FocusWidget;
import com.ponysdk.core.model.DomHandlerType;
import com.ponysdk.core.model.ServerToClientModel;
import com.ponysdk.core.terminal.PonySDK;
import com.ponysdk.core.terminal.UIBuilder;
import com.ponysdk.core.terminal.instruction.PTInstruction;
import com.ponysdk.core.terminal.model.BinaryModel;
import com.ponysdk.core.terminal.model.ReaderBuffer;

public abstract class PTFocusWidget<T extends FocusWidget> extends PTWidget<T> {

protected boolean enabled = true;
private boolean showLoadingOnRequest = false;
private boolean enabledOnRequest = false;
private boolean enabled = true;

@Override
public void create(final ReaderBuffer buffer, final int objectId, final UIBuilder uiBuilder) {
Expand Down Expand Up @@ -67,10 +73,22 @@ public boolean update(final ReaderBuffer buffer, final BinaryModel binaryModel)
}

@Override
protected void triggerMouseEvent(final DomHandlerType domHandlerType, final MouseEvent<?> event) {
protected void triggerDomEvent(final DomHandlerType domHandlerType, final DomEvent<?> event,
final Consumer<PTInstruction> enricher) {
if (enabled) super.triggerDomEvent(domHandlerType, event, enricher);
}

@Override
protected void triggerMouseClickEvent(final DomHandlerType domHandlerType, final MouseEvent<?> event) {
if (!enabled) return;
if (!enabledOnRequest) uiObject.setEnabled(false);
if (showLoadingOnRequest) uiObject.addStyleName("pony-Loading");
super.triggerMouseEvent(domHandlerType, event);
}

@Override
protected void triggerKeyEvent(final DomHandlerType domHandlerType, final KeyEvent<?> event, final JSONArray keyFilter) {
if (enabled) super.triggerKeyEvent(domHandlerType, event, keyFilter);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public int getTabIndex() {
@Override
public void onBrowserEvent(final Event event) {
super.onBrowserEvent(event);
if (Event.ONPASTE == event.getTypeInt()) {
if (handlePasteEnabled) Scheduler.get().scheduleDeferred(() -> sendPasteEvent(event));
if (handlePasteEnabled && Event.ONPASTE == event.getTypeInt() && enabled) {
Scheduler.get().scheduleDeferred(() -> sendPasteEvent(event));
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public int getTabIndex() {
@Override
public void onBrowserEvent(final Event event) {
super.onBrowserEvent(event);
if (Event.ONPASTE == event.getTypeInt()) {
if (handlePasteEnabled) Scheduler.get().scheduleDeferred(() -> sendPasteEvent(event));
if (handlePasteEnabled && Event.ONPASTE == event.getTypeInt() && enabled) {
Scheduler.get().scheduleDeferred(() -> sendPasteEvent(event));
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public int getTabIndex() {
@Override
public void onBrowserEvent(final Event event) {
super.onBrowserEvent(event);
if (Event.ONPASTE == event.getTypeInt()) {
if (Event.ONPASTE == event.getTypeInt() && enabled) {
filterText();
if (handlePasteEnabled) Scheduler.get().scheduleDeferred(() -> sendPasteEvent(event));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.util.Objects;

import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.user.client.ui.TextBoxBase;
Expand Down Expand Up @@ -64,42 +65,42 @@ public boolean update(final ReaderBuffer buffer, final BinaryModel binaryModel)
}

@Override
protected void triggerKeyUpEvent(final DomHandlerType domHandlerType, final JSONArray keyUpFilter) {
uiObject.addKeyUpHandler(event -> {
if (keyUpFilter != null) {
for (int i = 0; i < keyUpFilter.size(); i++) {
final JSONNumber keyCode = keyUpFilter.get(i).isNumber();
if (keyCode.doubleValue() == event.getNativeKeyCode()) {
final String newValue = uiObject.getText();
if (!Objects.equals(newValue, this.lastValue)) {
this.lastValue = newValue;
final PTInstruction changeHandlerInstruction = new PTInstruction(getObjectID());
changeHandlerInstruction.put(ClientToServerModel.HANDLER_STRING_VALUE_CHANGE, this.lastValue);
uiBuilder.sendDataToServer(changeHandlerInstruction);
}
protected void triggerKeyUpEvent(final DomHandlerType domHandlerType, final KeyUpEvent event, final JSONArray keyFilter) {
if (!enabled) return;
final int nativeKeyCode = event.getNativeKeyCode();
if (keyFilter != null) {
for (int i = 0; i < keyFilter.size(); i++) {
final JSONNumber keyCode = keyFilter.get(i).isNumber();
if (keyCode.doubleValue() == nativeKeyCode) {
final String newValue = uiObject.getText();
if (!Objects.equals(newValue, this.lastValue)) {
this.lastValue = newValue;
final PTInstruction changeHandlerInstruction = new PTInstruction(getObjectID());
changeHandlerInstruction.put(ClientToServerModel.HANDLER_STRING_VALUE_CHANGE, this.lastValue);
uiBuilder.sendDataToServer(changeHandlerInstruction);
}

final PTInstruction eventInstruction = buildEventInstruction(domHandlerType);
eventInstruction.put(ClientToServerModel.VALUE_KEY, event.getNativeKeyCode());
uiBuilder.sendDataToServer(eventInstruction);
final PTInstruction eventInstruction = buildEventInstruction(domHandlerType);
eventInstruction.put(ClientToServerModel.VALUE_KEY, nativeKeyCode);
uiBuilder.sendDataToServer(eventInstruction);

break;
}
}
} else {
final String newValue = uiObject.getText();
if (!Objects.equals(newValue, this.lastValue)) {
this.lastValue = newValue;
final PTInstruction changeHandlerInstruction = new PTInstruction(getObjectID());
changeHandlerInstruction.put(ClientToServerModel.HANDLER_STRING_VALUE_CHANGE, this.lastValue);
uiBuilder.sendDataToServer(changeHandlerInstruction);
break;
}

final PTInstruction eventInstruction = buildEventInstruction(domHandlerType);
eventInstruction.put(ClientToServerModel.VALUE_KEY, event.getNativeKeyCode());
uiBuilder.sendDataToServer(eventInstruction);
}
preventOrStopEvent(event);
});
} else {
final String newValue = uiObject.getText();
if (!Objects.equals(newValue, this.lastValue)) {
this.lastValue = newValue;
final PTInstruction changeHandlerInstruction = new PTInstruction(getObjectID());
changeHandlerInstruction.put(ClientToServerModel.HANDLER_STRING_VALUE_CHANGE, this.lastValue);
uiBuilder.sendDataToServer(changeHandlerInstruction);
}

final PTInstruction eventInstruction = buildEventInstruction(domHandlerType);
eventInstruction.put(ClientToServerModel.VALUE_KEY, nativeKeyCode);
uiBuilder.sendDataToServer(eventInstruction);
}
preventOrStopEvent(event);
}

}
Loading

0 comments on commit ad413dc

Please sign in to comment.