Skip to content

feat(ui): add ScrollView content alignment options#141

Open
orchid-world wants to merge 1 commit into
cocos:v4.0.0from
orchid-world:feat/scrollview-content-align
Open

feat(ui): add ScrollView content alignment options#141
orchid-world wants to merge 1 commit into
cocos:v4.0.0from
orchid-world:feat/scrollview-content-align

Conversation

@orchid-world

Copy link
Copy Markdown
Contributor

Add horizontal and vertical content alignment options for ScrollView when content is smaller than the view. The default remains left/top to preserve existing behavior.

Also add editor tooltip localization for the new alignment properties.

Re: #

Changelog


Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

Add horizontal and vertical content alignment options for ScrollView when content is smaller than the view.
The default remains left/top to preserve existing behavior.

Also add editor tooltip localization for the new alignment properties.
@github-actions

Copy link
Copy Markdown

Code Size Check Report

Wechat (WASM) Before After Diff
2D Empty (legacy pipeline) 1014386 bytes 1014386 bytes ✅ 0 bytes
2D All (legacy pipeline) 2681399 bytes 2683332 bytes ⚠️ +1933 bytes
2D All (new pipeline) 2773169 bytes 2775102 bytes ⚠️ +1933 bytes
(2D + 3D) All 10030380 bytes 10032313 bytes ⚠️ +1933 bytes
Web (WASM + ASMJS) Before After Diff
(2D + 3D) All 16866793 bytes 16868726 bytes ⚠️ +1933 bytes

Interface Check Report

! WARNING this pull request has changed these public interfaces:

@@ -57294,8 +57294,10 @@
      * 滚动视图组件。
      */
     export class ScrollView extends ViewGroup {
         static EventType: typeof __private._cocos_ui_scroll_view__ScrollViewEventType;
+        static HorizontalContentAlign: typeof __private._cocos_ui_scroll_view__ScrollViewHorizontalContentAlign;
+        static VerticalContentAlign: typeof __private._cocos_ui_scroll_view__ScrollViewVerticalContentAlign;
         /**
          * @en
          * The elapse time of bouncing back. A value of 0 will bounce back immediately.
          *
@@ -57346,8 +57348,17 @@
          */
         horizontal: boolean;
         /**
          * @en
+         * Horizontal alignment used when the content is not wider than the view.
+         *
+         * @zh
+         * 当内容宽度不大于视图时使用的水平对齐方式。
+         */
+        get horizontalContentAlign(): __private._cocos_ui_scroll_view__ScrollViewHorizontalContentAlign;
+        set horizontalContentAlign(value: __private._cocos_ui_scroll_view__ScrollViewHorizontalContentAlign);
+        /**
+         * @en
          * The horizontal scrollbar reference.
          * @zh
          * 水平滚动的 ScrollBar。
          */
@@ -57362,8 +57373,17 @@
          */
         vertical: boolean;
         /**
          * @en
+         * Vertical alignment used when the content is not taller than the view.
+         *
+         * @zh
+         * 当内容高度不大于视图时使用的垂直对齐方式。
+         */
+        get verticalContentAlign(): __private._cocos_ui_scroll_view__ScrollViewVerticalContentAlign;
+        set verticalContentAlign(value: __private._cocos_ui_scroll_view__ScrollViewVerticalContentAlign);
+        /**
+         * @en
          * The vertical scrollbar reference.
          *
          * @zh
          * 垂直滚动的 ScrollBar。
@@ -57397,8 +57417,10 @@
         protected _scrolling: boolean;
         protected _content: Node | null;
         protected _horizontalScrollBar: ScrollBar | null;
         protected _verticalScrollBar: ScrollBar | null;
+        protected _horizontalContentAlign: __private._cocos_ui_scroll_view__ScrollViewHorizontalContentAlign;
+        protected _verticalContentAlign: __private._cocos_ui_scroll_view__ScrollViewVerticalContentAlign;
         protected _topBoundary: number;
         protected _bottomBoundary: number;
         protected _leftBoundary: number;
         protected _rightBoundary: number;
@@ -57767,9 +57789,23 @@
         protected _isNecessaryAutoScrollBrake(): boolean;
         protected _processAutoScrolling(dt: any): void;
         protected _checkMouseWheel(dt: number): void;
         protected _calculateMovePercentDelta(options: any): math.Vec3;
-        protected _moveContentToTopLeft(scrollViewSize: math.Size): void;
+        /**
+         * @en Aligns the content when its size does not fill the view on one or both axes.
+         * @zh 当内容在某个轴向没有填满视图时,按照配置对齐内容。
+         */
+        protected _alignContentIfNeeded(scrollViewSize: math.Size): void;
+        /**
+         * @en Gets the horizontal movement needed to align the content in the view.
+         * @zh 获取将内容按水平配置对齐到视图中所需的位移。
+         */
+        protected _getHorizontalContentAlignDelta(): number;
+        /**
+         * @en Gets the vertical movement needed to align the content in the view.
+         * @zh 获取将内容按垂直配置对齐到视图中所需的位移。
+         */
+        protected _getVerticalContentAlignDelta(): number;
         protected _scaleChanged(value: TransformBit): void;
         protected _xrHoverEnter(event: XrUIPressEvent): void;
         protected _xrHoverExit(event: XrUIPressEvent): void;
         protected _xrThumbStickMove(event: math.Vec2): void;
@@ -74230,8 +74266,56 @@
              * 当用户松手的时候会发出一个事件。
              */
             TOUCH_UP = "touch-up"
         }
+        /**
+         * @en
+         * Enum for the horizontal alignment of the content when it is not wider than the ScrollView.
+         *
+         * @zh
+         * 当内容宽度不大于 ScrollView 时,内容的水平对齐方式。
+         */
+        export enum _cocos_ui_scroll_view__ScrollViewHorizontalContentAlign {
+            /**
+             * @en Align the content to the left side of the view.
+             * @zh 将内容对齐到视图左侧。
+             */
+            LEFT = 0,
+            /**
+             * @en Align the content to the horizontal center of the view.
+             * @zh 将内容对齐到视图水平中心。
+             */
+            CENTER = 1,
+            /**
+             * @en Align the content to the right side of the view.
+             * @zh 将内容对齐到视图右侧。
+             */
+            RIGHT = 2
+        }
+        /**
+         * @en
+         * Enum for the vertical alignment of the content when it is not taller than the ScrollView.
+         *
+         * @zh
+         * 当内容高度不大于 ScrollView 时,内容的垂直对齐方式。
+         */
+        export enum _cocos_ui_scroll_view__ScrollViewVerticalContentAlign {
+            /**
+             * @en Align the content to the top side of the view.
+             * @zh 将内容对齐到视图顶部。
+             */
+            TOP = 0,
+            /**
+             * @en Align the content to the vertical center of the view.
+             * @zh 将内容对齐到视图垂直中心。
+             */
+            CENTER = 1,
+            /**
+             * @en Align the content to the bottom side of the view.
+             * @zh 将内容对齐到视图底部。
+             */
+            BOTTOM = 2
+        }
         export enum _cocos_ui_scroll_view__XrhoverType {
             NONE = 0,
             LEFT = 1,
             RIGHT = 2

@star-e

star-e commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

@cocos-robot run test cases

@star-e star-e self-requested a review June 2, 2026 09:50
@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown

@orchid-world, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
web-mobile PASS PASS FAIL graphics-line-join
ios PASS PASS PASS
mac PASS PASS PASS

@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown

@orchid-world, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
windows PASS PASS PASS
android PASS PASS FAIL CoreJsTest
wechatgame PASS FAIL FAIL

@star-e star-e added the investigation Investigate the impact on the code base label Jun 3, 2026
@star-e

star-e commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Package size increased a bit. Maybe we can find a way to reduce code size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

investigation Investigate the impact on the code base

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants