hi,
I have two labels placed next to each other in a bigger canvas. When I move the first label, the second label’s position is also adjusted like :
label2.setLeft(label1.getLeft() + label1.getWidth());
Problem:
Initially they are next to each other. When I keep dragging label1 and reposition both of them, they dont stick together. Sometimes there is a gap of around 10 px between them, other times label2 gets partially/fully tucked in label1. At times they are positioned correctly.
I checked the location after updating them. The location, size numbers are as expected.
I have a suspicion that the parent canvas has some setting like ‘minimal space between components’ or something like that.
Let me know if you are aware of such parameter, or faced such alignment problem.
getWidth() returns specified width, but the Label may have overflowed to accommodate it’s contents. Use getVisibleWidth() to get overflowed width.
Iso, the visibleWidth didnt help as it is having the same value as getWidth()
Here is the code that is giving the problem. While simulating this, I am getting issue with y axis also.
No more problem I noticed is, if I minimize the hosted browser and max it, the labels and the background color get disoriented.
public void onModuleLoad() {
Canvas canvas = fetchFamily();
canvas.draw();
// RootPanel.get().add(canvas);
}
private Label label2 = new Label();
private int oldPositionX;
private int oldPositionY;
private final DragRepositionStartHandler dragRepositionStartHandler = new DragRepositionStartHandler(){
public void onDragRepositionStart(DragRepositionStartEvent event) {
oldPositionX = event.getX();
oldPositionY = event.getY();
}
};
private final DragRepositionStopHandler dragRepositionStopHandler = new DragRepositionStopHandler(){
public void onDragRepositionStop(DragRepositionStopEvent event) {
Canvas canvas = (Canvas) event.getSource();
int displaceX = event.getX() - oldPositionX;
int displaceY = event.getY() - oldPositionY;
canvas.setLeft(canvas.getLeft() + displaceX);
canvas.setTop(canvas.getTop() + displaceY);
updateRelativeTo(canvas, label2);
System.out.println("label1 " + canvas.getLeft() + "*" + canvas.getTop() + ", " + canvas.getWidth() + "*" + canvas.getHeight() + " off " + canvas.getOffsetWidth() + "*" + canvas.getOffsetHeight() + " vis " + canvas.getVisibleWidth() + "*" + canvas.getVisibleHeight());
System.out.println("label2 " + label2.getLeft() + "*" + label2.getTop() + ", " + label2.getWidth() + "*" + label2.getHeight() + " off " + label2.getOffsetWidth() + "*" + label2.getOffsetHeight() + " vis " + label2.getVisibleWidth() + "*" + label2.getVisibleHeight());
}
};
public void updateRelativeTo(Canvas canvas, Canvas smallCanvas)
{
smallCanvas.setLeft(canvas.getLeft() + canvas.getWidth());
smallCanvas.setTop(canvas.getTop() + (canvas.getHeight() / 2) - (smallCanvas.getHeight() / 2));
}
public Canvas fetchFamily()
{
Label label1 = new Label();
label1.setAlign(Alignment.CENTER);
label1.setContents("label1");
label1.setShowEdges(true);
label1.setBackgroundColor("lightgreen");
label1.setOverflow(Overflow.HIDDEN);
label1.setWidth(100);
label1.setHeight(100);
label1.setCanDragReposition(true);
label1.setDragAppearance(DragAppearance.OUTLINE);
label1.addDragRepositionStartHandler(dragRepositionStartHandler);
label1.addDragRepositionStopHandler(dragRepositionStopHandler);
label1.setLeft(150);
label1.setTop(50);
label2.setWidth(20);
label2.setHeight(20);
label2.setShowEdges(false);
label2.setBackgroundColor("brown");
updateRelativeTo(label1, label2);
Canvas canvas = new Canvas();
canvas.addChild(label1);
canvas.addChild(label2);
return canvas;
}
This code doesn’t contain the suggested changes. Please try them rather than logging values, and if you think you have a minimal test case that actually shows a bug, please post that.
You’re mentioning some other bizarre problems. Try running your tests with no CSS other than the CSS from the skin.
hi Iso,
I replaced the update function with the following code.
public void updateRelativeTo(Canvas canvas, Canvas smallCanvas)
{
System.out.println("label1 " + canvas.getLeft() + "*" + canvas.getTop() + ", " + canvas.getWidth() + "*" + canvas.getHeight() + " off " + canvas.getOffsetWidth() + "*" + canvas.getOffsetHeight() + " vis " + canvas.getVisibleWidth() + "*" + canvas.getVisibleHeight());
smallCanvas.setLeft(canvas.getLeft() + canvas.getVisibleWidth());
smallCanvas.setTop(canvas.getTop() + (canvas.getVisibleHeight() / 2) - (smallCanvas.getVisibleHeight() / 2));
System.out.println("label2 " + smallCanvas.getLeft() + "*" + smallCanvas.getTop() + ", " + smallCanvas.getWidth() + "*" + smallCanvas.getHeight() + " off " + smallCanvas.getOffsetWidth() + "*" + smallCanvas.getOffsetHeight() + " vis " + smallCanvas.getVisibleWidth() + "*" + smallCanvas.getVisibleHeight());
}
Still seeing the earlier disorientation. One difference is: with getHeight(), atleast the very first placement of labels was correct and repositioning caused the problem. With getVisibleHeight() even the first time placement have the same problem.
>> Try running your tests with no CSS other than the CSS from the skin.
Kindly let me know the type of css code that need to be turned off. The original code is the complete test module I have.
One more observation. When I move label1 towards right, label2 tries to tuck inside label1. When I move label1 towards left, label2 tries to move away from label1. (this movement discussion is only in horizontal direction)
As part of the css avoidance suggestion, I commented the following code. Got the same position mismatch.
// label1.setAlign(Alignment.CENTER);
// label1.setBackgroundColor("lightgreen");
// label1.setOverflow(Overflow.HIDDEN);
// label2.setBackgroundColor("brown");
hi Iso,
I found something interesting with the positions. Just had print statement at drag reposition start and stop handlers. I have printed the left, top of label1 and label2.
Once the movement stops at a place, the positions are recorded. When the label1 is dragged again, we would expect the position to be the same as where it had stopped in the previous move. However, the following output doesn’t say so.
onDragRepositionStart label1 150*50, label2 250*90
onDragRepositionStop label1 371*64, label2 471*102
onDragRepositionStart label1 377*65, label2 471*102
onDragRepositionStop label1 424*65, label2 524*103
onDragRepositionStart label1 433*66, label2 524*103
onDragRepositionStop label1 206*107, label2 306*145
onDragRepositionStart label1 198*109, label2 306*145
onDragRepositionStop label1 317*126, label2 417*164
Here is the code:
private final DragRepositionStartHandler dragRepositionStartHandler = new DragRepositionStartHandler(){
public void onDragRepositionStart(DragRepositionStartEvent event) {
oldPositionX = event.getX();
oldPositionY = event.getY();
System.out.println("onDragRepositionStart label1 " + label1.getLeft() + "*" + label1.getTop() + ", label2 " + label2.getLeft() + "*" + label2.getTop());
}
};
private final DragRepositionStopHandler dragRepositionStopHandler = new DragRepositionStopHandler(){
public void onDragRepositionStop(DragRepositionStopEvent event) {
Canvas canvas = (Canvas) event.getSource();
int displaceX = event.getX() - oldPositionX;
int displaceY = event.getY() - oldPositionY;
canvas.setLeft(canvas.getLeft() + displaceX);
canvas.setTop(canvas.getTop() + displaceY);
updateRelativeTo(canvas, label2);
System.out.println("onDragRepositionStop label1 " + label1.getLeft() + "*" + label1.getTop() + ", label2 " + label2.getLeft() + "*" + label2.getTop());
}
};
Is the flaw in code due to displaceX and displaceY calculation? Is there a better handler to use for the above purpose, ie to use the new position of the dragged element when the drag completes?
I had attached the images of the problematic movements.
couple1 is the image obtained when the program runs. The labels are positioned as expected.
couple2: I had dragged label1 towards right. label2 gets tucked inside
couple3: I had dragged label1 towards left. label2 moves away from label1 and there is a gap.
Thanks for looking into this.
solved!!!
It is working like a gem.
After some more debugging, it was clear that the displacement position through dragstop event event.getX() is slightly different than where the canvas is actually placed after dragstop.
I placed a deferred command inside the dragstop event and things work fine. No longer I need to listen to the dragstart.
public void onDragRepositionStop(DragRepositionStopEvent event) {
final Canvas canvas = (Canvas) event.getSource();
DeferredCommand.addCommand(new Command() {
public void execute() {
updateRelativeTo(canvas, label2);
}
});
}
QED