Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CodenameOne/src/com/codename1/ui/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -6707,6 +6707,11 @@ public DevicePosture getDevicePosture() {
/// delivered `com.codename1.ui.events.ActionEvent` has the type `PostureChange`; query the new
/// posture from `com.codename1.ui.DevicePosture#getInstance()`.
///
/// The current form already responds to these changes by itself, laying out again or
/// animating as its layouts ask (see `com.codename1.ui.layouts.Layout#getFoldBehavior()` and
/// `com.codename1.ui.Form#onFoldChanged(FoldState)`), so a listener is only needed for
/// changes a layout cannot express.
///
/// #### Parameters
///
/// - `l`: the listener to add
Expand Down
214 changes: 214 additions & 0 deletions CodenameOne/src/com/codename1/ui/FoldState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
/*
* Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.ui;

import com.codename1.ui.geom.Rectangle;

/// An immutable snapshot of a foldable device's fold, expressed relative to one container.
///
/// Fold-aware layouts such as `com.codename1.ui.layouts.FoldLayout` read the hinge through this
/// class rather than through `DevicePosture` directly, so the same layout can be driven by the live
/// device (`#current(Component)`) or by an explicit state: a preview, a large tablet that wants a
/// "virtual hinge", or a test that runs on a device that does not fold at all.
///
/// ```java
/// FoldState book = new FoldState(DevicePosture.POSTURE_HALF_OPENED,
/// DevicePosture.FOLD_ORIENTATION_VERTICAL, true, new Rectangle(500, 0, 20, 1200));
/// if (book.isVerticalSeparation()) {
/// // two panes side by side, one on each side of the hinge
/// }
/// ```
public final class FoldState {
/// The state of a device with no fold, or a fold that does not separate the screen.
public static final FoldState NONE = new FoldState(DevicePosture.POSTURE_UNKNOWN,
DevicePosture.FOLD_ORIENTATION_NONE, false, null);

private final int posture;
private final int orientation;
private final boolean separating;
private final int foldX;
private final int foldY;
private final int foldWidth;
private final int foldHeight;
private final boolean hasBounds;

/// Creates a fold state.
///
/// #### Parameters
///
/// - `posture`: one of the `DevicePosture` `POSTURE_*` constants
///
/// - `orientation`: one of the `DevicePosture` `FOLD_ORIENTATION_*` constants
///
/// - `separating`: true when the fold splits the screen into two logical areas
///
/// - `foldBounds`: the hinge bounds relative to the container being laid out, or null. A
/// zero-width (or zero-height) rectangle is a seamless fold line with no occluded area.
public FoldState(int posture, int orientation, boolean separating, Rectangle foldBounds) {
this.posture = posture;
this.orientation = orientation;
this.separating = separating;
if (foldBounds != null) {
hasBounds = true;
foldX = foldBounds.getX();
foldY = foldBounds.getY();
foldWidth = foldBounds.getWidth();
foldHeight = foldBounds.getHeight();
} else {
hasBounds = false;
foldX = 0;
foldY = 0;
foldWidth = 0;
foldHeight = 0;
}
}

/// The live fold state of this device, with the hinge translated into the coordinate space of
/// the given component.
///
/// #### Parameters
///
/// - `relativeTo`: the component whose coordinate space the hinge bounds use, or null for
/// display coordinates
///
/// #### Returns
///
/// the current fold state, `#NONE` on a device that does not fold
public static FoldState current(Component relativeTo) {
DevicePosture dp = DevicePosture.getInstance();
if (!dp.isFoldable()) {
return NONE;
}
Rectangle r = dp.getFoldBounds(null);
if (r != null && relativeTo != null) {
// getAbsoluteX/Y subtract the component's own scroll; a layout works in unscrolled
// child coordinates, so add it back or the hinge would move as the container scrolls.
int ax = relativeTo.getAbsoluteX() + relativeTo.getScrollX();
int ay = relativeTo.getAbsoluteY() + relativeTo.getScrollY();
r = new Rectangle(r.getX() - ax, r.getY() - ay,
r.getWidth(), r.getHeight());
}
return new FoldState(dp.getPosture(), dp.getFoldOrientation(), dp.isSeparating(), r);
}

/// The posture, one of the `DevicePosture` `POSTURE_*` constants.
///
/// #### Returns
///
/// the posture
public int getPosture() {
return posture;
}

/// The fold orientation, one of the `DevicePosture` `FOLD_ORIENTATION_*` constants.
/// `FOLD_ORIENTATION_VERTICAL` is a book or dual-screen fold, `FOLD_ORIENTATION_HORIZONTAL` a
/// flip or table-top fold.
///
/// #### Returns
///
/// the orientation
public int getOrientation() {
return orientation;
}

/// True when the fold splits the screen into two logical areas.
///
/// #### Returns
///
/// true for a separating fold
public boolean isSeparating() {
return separating;
}

/// True for a separating fold with known bounds and a vertical hinge: content belongs on the
/// left and the right of it.
///
/// #### Returns
///
/// true for a vertical separating fold
public boolean isVerticalSeparation() {
return separating && hasBounds && orientation == DevicePosture.FOLD_ORIENTATION_VERTICAL;
}

/// True for a separating fold with known bounds and a horizontal hinge: content belongs above
/// and below it.
///
/// #### Returns
///
/// true for a horizontal separating fold
public boolean isHorizontalSeparation() {
return separating && hasBounds && orientation == DevicePosture.FOLD_ORIENTATION_HORIZONTAL;
}

/// The hinge bounds in the coordinate space this state was created for.
///
/// #### Returns
///
/// a new rectangle, or null when the bounds are unknown
public Rectangle getFoldBounds() {
if (!hasBounds) {
return null;
}
return new Rectangle(foldX, foldY, foldWidth, foldHeight);
}

/// The left (or top, for a horizontal fold) edge of the hinge.
///
/// #### Returns
///
/// the leading coordinate of the hinge along the axis it divides
public int getFoldStart() {
return orientation == DevicePosture.FOLD_ORIENTATION_HORIZONTAL ? foldY : foldX;
}

/// The right (or bottom, for a horizontal fold) edge of the hinge.
///
/// #### Returns
///
/// the trailing coordinate of the hinge along the axis it divides
public int getFoldEnd() {
return orientation == DevicePosture.FOLD_ORIENTATION_HORIZONTAL ? foldY + foldHeight : foldX + foldWidth;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof FoldState)) {
return false;
}
FoldState f = (FoldState) o;
return posture == f.posture && orientation == f.orientation && separating == f.separating
&& hasBounds == f.hasBounds && foldX == f.foldX && foldY == f.foldY
&& foldWidth == f.foldWidth && foldHeight == f.foldHeight;
}

@Override
public int hashCode() {
int h = posture;
h = 31 * h + orientation;
h = 31 * h + (separating ? 1 : 0);
h = 31 * h + foldX;
h = 31 * h + foldY;
h = 31 * h + foldWidth;
return 31 * h + foldHeight;
}
}
88 changes: 86 additions & 2 deletions CodenameOne/src/com/codename1/ui/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.codename1.ui.geom.Rectangle;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.layouts.FlowLayout;
import com.codename1.ui.layouts.FoldLayout;
import com.codename1.ui.layouts.LayeredLayout;
import com.codename1.ui.layouts.Layout;
import com.codename1.ui.list.ListCellRenderer;
Expand Down Expand Up @@ -228,6 +229,8 @@ HoverTracker getHoverTracker() {
private EventDispatcher orientationListener;
private EventDispatcher pointerCancelledListener;
private EventDispatcher sizeChangedListener;
private ActionListener postureListener;
private FoldState lastFold;
private EventDispatcher pasteListener;
private UIManager uiManager;
private Component stickyDrag;
Expand Down Expand Up @@ -283,7 +286,8 @@ public Form(Layout contentPaneLayout) {
initAdPadding(d);

contentPane.setUIID("ContentPane");
contentPane.setScrollableY(true);
// A FoldLayout pane fills its side of the hinge and scrolls by itself, see setLayout.
contentPane.setScrollableY(!(contentPaneLayout instanceof FoldLayout));

if (title.getText() != null && title.shouldTickerStart()) {
title.startTicker(getUIManager().getLookAndFeel().getTickerSpeed(), true);
Expand Down Expand Up @@ -1125,6 +1129,72 @@ protected void showNotify() {
protected void sizeChanged(int w, int h) {
}

/// Invoked on the EDT when the device folds, unfolds or changes posture while this form is
/// showing, before the form lays itself out again. Override it to swap content for the new
/// posture; the relayout that follows is decided by the layouts in the form, see
/// `com.codename1.ui.layouts.Layout#getFoldBehavior()`.
///
/// #### Parameters
///
/// - `fold`: the new fold state, relative to this form
protected void onFoldChanged(FoldState fold) {
}

/// Handles a fold or posture change: calls `#onFoldChanged(FoldState)`, then lays the form
/// out again the way its layouts ask -- animated when any layout in the form answers
/// `Layout#FOLD_ANIMATE` (and reduced motion is off), revalidated when any answers
/// `Layout#FOLD_REVALIDATE`, and left alone when every layout ignores folds.
void foldChangedInternal() {
FoldState fold = FoldState.current(this);
// Ports report every step of a fold (the simulator's hinge slider fires per tick, and
// Android's window-layout stream repeats states); only a change of the fold itself --
// posture, orientation, separation or hinge bounds -- moves anything.
if (fold.equals(lastFold)) {
return;
}
lastFold = fold;
onFoldChanged(fold);
int[] behavior = new int[2];
collectFoldBehavior(getContentPane(), behavior);
if (behavior[0] == Layout.FOLD_ANIMATE && !Display.getInstance().isReduceMotionEnabled()) {
getContentPane().animateLayout(behavior[1]);
} else if (behavior[0] != Layout.FOLD_IGNORE) {
revalidate();
}
}

/// Records in `out[0]` the strongest fold behaviour among the layouts under `c` and in
/// `out[1]` the longest animation duration among the layouts that animate.
private static void collectFoldBehavior(Container c, int[] out) {
Layout l = c.getLayout();
if (l != null) {
int b = l.getFoldBehavior();
if (b > out[0]) {
out[0] = b;
}
if (b == Layout.FOLD_ANIMATE && l.getFoldAnimationDuration() > out[1]) {
out[1] = l.getFoldAnimationDuration();
}
}
int count = c.getComponentCount();
for (int i = 0; i < count; i++) {
Component child = c.getComponentAt(i);
if (child instanceof Container) {
collectFoldBehavior((Container) child, out);
}
}
}

/// Relays a posture change to the form it belongs to.
private final class PostureListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent evt) {
if (isInitialized()) {
foldChangedInternal();
}
}
}

/// Causes the display safe area to be recalculated the next time the form list laid out.
///
/// #### See also
Expand Down Expand Up @@ -2932,6 +3002,9 @@ void deinitializeImpl() {
// the release goes to the new form, and on a platform whose own recognizer starts the
// session the drag begins later still. It would then have carried the hidden form's
// payload, and reported its outcome to a component that is no longer anywhere.
if (postureListener != null) {
Display.getInstance().removePostureListener(postureListener);
}
NativeDragAndDrop.topLevelInputCancelled(this);
super.deinitializeImpl();
animMananger.flush();
Expand Down Expand Up @@ -3414,6 +3487,15 @@ void applyDocumentModified(java.util.List commands) {
void initComponentImpl() {
super.initComponentImpl();
dragged = null;
if (getParent() == null) {
// Only a form that owns the surface follows the device fold; an embedded one is
// laid out by the form that hosts it.
if (postureListener == null) {
postureListener = new PostureListener();
}
lastFold = FoldState.current(this);
Display.getInstance().addPostureListener(postureListener);
}
// Only a form that owns the surface publishes its commands to the platform.
// An embedded one -- a Dialog hosted in a window's layered pane, a form inside
// an EmbeddedContainer -- would otherwise overwrite the real surface's native
Expand Down Expand Up @@ -4090,7 +4172,9 @@ public Layout getLayout() {
/// {@inheritDoc}
@Override
public void setLayout(Layout layout) {
if (layout instanceof BorderLayout) {
if (layout instanceof BorderLayout || layout instanceof FoldLayout) {
// A FoldLayout pane fills its side of the hinge and scrolls by itself; a scrolling
// content pane would size the panes to their preferred height instead.
setScrollable(false);
}
contentPane.setLayout(layout);
Expand Down
Loading
Loading