use of com.codename1.ui.Form in project CodenameOne by codenameone.
the class Component method getInvisibleAreaUnderVKB.
/**
* Returns the area of this component that is currently hidden by the virtual keyboard.
* @return
*/
private int getInvisibleAreaUnderVKB() {
Form f = getComponentForm();
if (f != null) {
int invisibleAreaUnderVKB = Form.getInvisibleAreaUnderVKB(f);
if (invisibleAreaUnderVKB == 0) {
return 0;
}
int bottomGap = f.getHeight() - getAbsoluteY() - getScrollY() - getHeight();
if (bottomGap < invisibleAreaUnderVKB) {
return invisibleAreaUnderVKB - bottomGap;
} else {
return 0;
}
}
return 0;
}
use of com.codename1.ui.Form in project CodenameOne by codenameone.
the class Component method pointerDragged.
/**
* If this Component is focused, the pointer dragged event
* will call this method
*
* @param x the pointer x coordinate
* @param y the pointer y coordinate
*/
public void pointerDragged(final int x, final int y) {
Form p = getComponentForm();
if (p == null) {
return;
}
if (pointerDraggedListeners != null && pointerDraggedListeners.hasListeners()) {
pointerDraggedListeners.fireActionEvent(new ActionEvent(this, ActionEvent.Type.PointerDrag, x, y));
}
if (dragAndDropInitialized) {
// keep call to pointerDragged to move the parent scroll if needed
if (dragCallbacks < 2) {
dragCallbacks++;
Display.getInstance().callSerially(new Runnable() {
public void run() {
if (dragActivated) {
pointerDragged(x, y);
}
dragCallbacks--;
}
});
}
if (!dragActivated) {
dragActivated = true;
setVisible(false);
p.setDraggedComponent(this);
oldx = x;
oldy = y;
draggedx = getAbsoluteX();
draggedy = getAbsoluteY();
}
Component dropTo = findDropTarget(this, x, y);
if (dropTo != null && dragOverListener != null) {
ActionEvent ev = new ActionEvent(this, dropTo, x, y);
dragOverListener.fireActionEvent(ev);
if (ev.isConsumed()) {
return;
}
}
if (dropTargetComponent != dropTo) {
if (dropTargetComponent != null) {
dropTargetComponent.dragExit(this);
}
dropTargetComponent = dropTo;
if (dropTargetComponent != null) {
dropTargetComponent.dragEnter(this);
}
}
// we repaint twice to create an intersection of the old and new position
p.repaint(draggedx, draggedy, getWidth(), getHeight());
draggedx = draggedx + (x - oldx);
draggedy = draggedy + (y - oldy);
oldx = x;
oldy = y;
p.repaint(draggedx, draggedy, getWidth(), getHeight());
Container scrollParent = getParent();
while (scrollParent != null && !scrollParent.isScrollable()) {
scrollParent = scrollParent.getParent();
}
if (scrollParent != null) {
Style s = getStyle();
int w = getWidth() - s.getHorizontalPadding();
int h = getHeight() - s.getVerticalPadding();
Rectangle view;
int invisibleAreaUnderVKB = getInvisibleAreaUnderVKB();
view = new Rectangle(getScrollX(), getScrollY(), w, h - invisibleAreaUnderVKB);
// if the dragging component is out of bounds move the scrollable parent
if (!view.contains(draggedx - scrollParent.getAbsoluteX(), draggedy - scrollParent.getAbsoluteY(), getWidth(), getHeight())) {
if ((scrollParent.isScrollableY() && scrollParent.getScrollY() >= 0 && scrollParent.getScrollY() + (draggedy + getHeight()) < scrollParent.getScrollDimension().getHeight()) || (scrollParent.isScrollableX() && scrollParent.getScrollX() >= 0 && scrollParent.getScrollX() + (draggedx + getWidth()) < scrollParent.getScrollDimension().getWidth())) {
int yposition = draggedy - scrollParent.getAbsoluteY() - 40;
if (yposition < 0) {
yposition = 0;
}
int xposition = draggedx - scrollParent.getAbsoluteX() - 40;
if (xposition < 0) {
xposition = 0;
}
int height = getHeight() + 80;
if (scrollParent.getScrollY() + draggedy + height >= scrollParent.getScrollDimension().getHeight()) {
yposition = draggedy - scrollParent.getAbsoluteY();
height = scrollParent.getScrollDimension().getHeight() - yposition;
}
int width = getWidth() + 80;
if (scrollParent.getScrollX() + draggedx + width >= scrollParent.getScrollDimension().getWidth()) {
xposition = draggedx - scrollParent.getAbsoluteX();
width = scrollParent.getScrollDimension().getWidth() - xposition;
}
scrollParent.scrollRectToVisible(xposition, yposition, width, height, scrollParent);
}
}
}
return;
}
if (dragActivated && p.getDraggedComponent() == null) {
dragActivated = false;
}
if (!dragActivated) {
boolean draggedOnX = Math.abs(p.initialPressX - x) > Math.abs(p.initialPressY - y);
shouldGrabScrollEvents = (isScrollableX() && draggedOnX) || isScrollableY() && !draggedOnX;
}
if (isScrollable() && isSmoothScrolling() && shouldGrabScrollEvents) {
if (!dragActivated) {
dragActivated = true;
lastScrollY = y;
lastScrollX = x;
p.setDraggedComponent(this);
p.registerAnimatedInternal(this);
Component fc = p.getFocused();
if (fc != null && fc != this) {
fc.dragInitiated();
}
}
// and pulling it in the reverse direction of the drag
if (isScrollableY()) {
int tl;
if (getTensileLength() > -1 && refreshTask == null) {
tl = getTensileLength();
} else {
tl = getHeight() / 2;
}
if (!isSmoothScrolling() || !isTensileDragEnabled()) {
tl = 0;
}
int scroll = getScrollY() + (lastScrollY - y);
if (isAlwaysTensile() && getScrollDimension().getHeight() + getInvisibleAreaUnderVKB() <= getHeight()) {
if (scroll >= -tl && scroll < getHeight() + tl) {
setScrollY(scroll);
}
} else {
if (scroll >= -tl && scroll < getScrollDimension().getHeight() + getInvisibleAreaUnderVKB() - getHeight() + tl) {
setScrollY(scroll);
}
}
updateTensileHighlightIntensity(lastScrollY, y, false);
}
if (isScrollableX()) {
int tl;
if (getTensileLength() > -1) {
tl = getTensileLength();
} else {
tl = getWidth() / 2;
}
if (!isSmoothScrolling() || !isTensileDragEnabled()) {
tl = 0;
}
int scroll = getScrollX() + (lastScrollX - x);
if (scroll >= -tl && scroll < getScrollDimension().getWidth() - getWidth() + tl) {
setScrollX(scroll);
}
}
lastScrollY = y;
lastScrollX = x;
} else {
// try to find a scrollable element until you reach the Form
Component parent = getParent();
if (!(parent instanceof Form)) {
parent.pointerDragged(x, y);
}
}
}
use of com.codename1.ui.Form in project CodenameOne by codenameone.
the class Component method setScrollY.
/**
* Indicates the X position of the scrolling, this number is relative to the
* component position and so a position of 0 would indicate the x position
* of the component.
*
* @param scrollY the Y position of the scrolling
*/
protected void setScrollY(int scrollY) {
if (this.scrollY != scrollY) {
CodenameOneImplementation ci = Display.impl;
if (ci.isAsyncEditMode() && ci.isEditingText()) {
Component editingText = ci.getEditingText();
if (editingText != null && this instanceof Container && ((Container) this).contains(editingText)) {
ci.hideTextEditor();
}
}
}
// the setter must always update the value regardless...
int scrollYtmp = scrollY;
if (!isSmoothScrolling() || !isTensileDragEnabled()) {
Form parentForm = getComponentForm();
int v = getInvisibleAreaUnderVKB();
int h = getScrollDimension().getHeight() - getHeight() + v;
scrollYtmp = Math.min(scrollYtmp, h);
scrollYtmp = Math.max(scrollYtmp, 0);
}
if (isScrollableY()) {
if (Form.activePeerCount > 0) {
onParentPositionChange();
}
repaint();
}
if (scrollListeners != null) {
scrollListeners.fireScrollEvent(this.scrollX, scrollYtmp, this.scrollX, this.scrollY);
}
this.scrollY = scrollYtmp;
onScrollY(this.scrollY);
}
use of com.codename1.ui.Form in project CodenameOne by codenameone.
the class Component method startTensile.
void startTensile(int offset, int dest, boolean vertical) {
Motion draggedMotion;
if (tensileDragEnabled) {
draggedMotion = Motion.createDecelerationMotion(offset, dest, 500);
draggedMotion.start();
} else {
draggedMotion = Motion.createLinearMotion(offset, dest, 0);
draggedMotion.start();
}
decelerationMotion = draggedMotion;
if (vertical) {
draggedMotionY = draggedMotion;
} else {
draggedMotionX = draggedMotion;
}
// just to be sure, there are some cases where this doesn't work as expected
Form p = getComponentForm();
if (p != null) {
p.registerAnimatedInternal(this);
}
}
use of com.codename1.ui.Form in project CodenameOne by codenameone.
the class Container method wrapInLayeredPane.
/**
* An atomic operation that wraps the current component in a Container with
* a layered layout. This prevents us from having to initialize and deinitialize
* all of the components in a sub-tree because we want to re-root it. In particular
* Form.getLayeredPane() re-roots the entire content pane the first time it is
* called on a form. If the form contains native peers there is a flicker which
* is quite annoying. Providing a way to do this atomically results in a better
* user experience.
* @return The Container that is the new parent of this component.
*/
Container wrapInLayeredPane() {
final Container oldParent = getParent();
final Container newParent = new Container(new LayeredLayout());
final Layout parentLayout = oldParent != null && oldParent.layout != null ? oldParent.layout : null;
final Object constraint = parentLayout != null ? parentLayout.getComponentConstraint(this) : null;
newParent.setParent(oldParent);
newParent.components.add(this);
final Runnable r = new Runnable() {
public void run() {
if (parentLayout != null) {
parentLayout.removeLayoutComponent(Container.this);
parentLayout.addLayoutComponent(constraint, newParent, oldParent);
}
newParent.initComponentImpl();
if (oldParent != null) {
int cmpIndex = -1;
for (int i = 0; i < oldParent.getComponentCount(); i++) {
Component c = oldParent.getComponentAt(i);
if (c.equals(Container.this)) {
cmpIndex = i;
break;
}
}
// int cmpIndex = oldParent.getComponentIndex(Container.this); <--- WTF... this always returns -1!!
if (cmpIndex == -1) {
throw new RuntimeException("WTF we have parent but no index!!!!");
}
oldParent.components.set(cmpIndex, newParent);
}
Container.this.setParent(newParent);
newParent.revalidate();
}
};
AnimationManager a = getAnimationManager();
if (a != null && a.isAnimating()) {
a.addAnimation(new ComponentAnimation() {
@Override
public boolean isInProgress() {
return false;
}
@Override
protected void updateState() {
r.run();
}
});
return newParent;
} else {
r.run();
return newParent;
}
}
Aggregations