Skip to content

feat(android): parity for Ti.UI.View clipMode #14191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,13 @@ public class UIModule extends KrollModule implements TiApplication.Configuration
@Kroll.constant
public static final int HIDDEN_BEHAVIOR_INVISIBLE = View.INVISIBLE;

@Kroll.constant
public static final int CLIP_MODE_DEFAULT = 0;
@Kroll.constant
public static final int CLIP_MODE_ENABLED = 1;
@Kroll.constant
public static final int CLIP_MODE_DISABLED = -1;

@Kroll.constant
public static final int USER_INTERFACE_STYLE_LIGHT = Configuration.UI_MODE_NIGHT_NO;
@Kroll.constant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ public class TiC
public static final String PROPERTY_CLASS_NAMES = "classNames";
public static final String PROPERTY_CLEAR_ON_EDIT = "clearOnEdit";
public static final String PROPERTY_CLIP_VIEWS = "clipViews";
public static final String PROPERTY_CLIP_MODE = "clipMode";
public static final String PROPERTY_CODE = "code";
public static final String PROPERTY_COLOR = "color";
public static final String PROPERTY_COLUMNS = "columns";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@
TiC.PROPERTY_TRANSITION_NAME,
TiC.PROPERTY_HIDDEN_BEHAVIOR,
TiC.PROPERTY_ANCHOR_POINT,
TiC.PROPERTY_ACCESSIBILITY_DISABLE_LONG
TiC.PROPERTY_ACCESSIBILITY_DISABLE_LONG,
TiC.PROPERTY_CLIP_MODE
})
public abstract class TiViewProxy extends KrollProxy
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;

import ti.modules.titanium.ui.UIModule;

/**
* This class is for Titanium View implementations, that correspond with TiViewProxy.
* A TiUIView is responsible for creating and maintaining a native Android View instance.
Expand Down Expand Up @@ -974,6 +976,10 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
} else {
Log.w(TAG, "Setting the 'viewShadowColor' property requires Android P or later");
}
} else if (key.equals(TiC.PROPERTY_CLIP_MODE)) {
if (nativeView != null) {
setClipMode(TiConvert.toInt(newValue, UIModule.CLIP_MODE_DEFAULT));
}
} else if (Log.isDebugModeEnabled()) {
Log.d(TAG, "Unhandled property key: " + key, Log.DEBUG_MODE);
}
Expand Down Expand Up @@ -1141,6 +1147,10 @@ public void processProperties(KrollDict d)
}
}

if (d.containsKey(TiC.PROPERTY_CLIP_MODE) && !nativeViewNull) {
setClipMode(TiConvert.toInt(d, UIModule.CLIP_MODE_DEFAULT));
}

if (!nativeViewNull && d.containsKeyAndNotNull(TiC.PROPERTY_TRANSITION_NAME)) {
ViewCompat.setTransitionName(nativeView, d.getString(TiC.PROPERTY_TRANSITION_NAME));
}
Expand Down Expand Up @@ -1197,6 +1207,14 @@ private void applyCustomBackground(boolean reuseCurrentDrawable)
}
}

private void setClipMode(int clipMode)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should work to get the root view (I haven't tested it though).

private void setClipMode(int clipMode)
{
	if (nativeView == null) {
		return;
	}

	View rootView = nativeView;
	while (rootView.getParent() != null && rootView instanceof ViewGroup) {
		rootView = (View) rootView.getParent();
	}

	if (rootView instanceof ViewGroup viewGroup) {
		viewGroup.setClipChildren(clipMode != CLIP_MODE_DISABLED);
	}	
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error: android.view.ViewRootImpl cannot be cast to android.view.View think it's one level to high. But I'll do some more tests at another point

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove the (View) casting, and declare rootView as ViewParent, even or simply keep it as an Object as it's already casted in next if condition.

{
if (nativeView instanceof ViewGroup viewGroup) {
viewGroup.setClipChildren(clipMode == UIModule.CLIP_MODE_ENABLED);
viewGroup.setClipToPadding(clipMode == UIModule.CLIP_MODE_ENABLED);
}
}

/**
* @param props View's property dictionary
* @return true if touch feedback can be applied.
Expand Down
22 changes: 12 additions & 10 deletions apidoc/Titanium/UI/View.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ description: |
The shadow of the view is drawn using a rounded rectangle with the arc radius set to the `borderRadius` property.

The `clipMode` property lets the user control the clipping behavior of the View.
Setting this to <Titanium.UI.iOS.CLIP_MODE_ENABLED> enforces all child views to be clipped to this views bounds.
Setting this to <Titanium.UI.iOS.CLIP_MODE_DISABLED> allows child views to be drawn outside the bounds of this view.
When set to <Titanium.UI.iOS.CLIP_MODE_DEFAULT> or when this property is not set, clipping behavior is defined by the following rules applied in order.
Setting this to <Titanium.UI.CLIP_MODE_ENABLED> enforces all child views to be clipped to this views bounds.
Setting this to <Titanium.UI.CLIP_MODE_DISABLED> allows child views to be drawn outside the bounds of this view.
When set to <Titanium.UI.CLIP_MODE_DEFAULT> or when this property is not set, clipping behavior is defined by the following rules applied in order.

* If the `viewShadowColor` is defined to be a color with alpha > 0, clipping is disabled.
* If the `borderWidth` or `borderRadius` of the view is set to a value > 0, clipping is enabled.
Expand Down Expand Up @@ -1431,15 +1431,17 @@ properties:
- name: clipMode
summary: View's clipping behavior.
description: |
Setting this to <Titanium.UI.iOS.CLIP_MODE_ENABLED> enforces all child views to be clipped to this views bounds.
Setting this to <Titanium.UI.iOS.CLIP_MODE_DISABLED> allows child views to be drawn outside the bounds of this view.
When set to <Titanium.UI.iOS.CLIP_MODE_DEFAULT> or when this property is not set, clipping behavior is inferred.
See section on iOS Clipping Behavior in <Titanium.UI.View>.
Setting this to <Titanium.UI.CLIP_MODE_ENABLED> enforces all child views to be clipped to this views bounds.
Setting this to <Titanium.UI.CLIP_MODE_DISABLED> allows child views to be drawn outside the bounds of this view.
When set to <Titanium.UI.CLIP_MODE_DEFAULT> or when this property is not set, clipping behavior is inferred.
See section on Clipping Behavior in <Titanium.UI.View>.

Defaults to `undefined`. Behaves as if set to <Titanium.UI.iOS.CLIP_MODE_DEFAULT>.
Android: Has to be on the top-level view.

Defaults to `undefined`. Behaves as if set to <Titanium.UI.CLIP_MODE_DEFAULT>.
type: Number
platforms: [iphone, ipad, macos]
since: "3.3.0"
platforms: [android, iphone, ipad, macos]
since: {android: "12.7.0", iphone: "3.3.0", ipad: "3.3.0"}

- name: elevation
summary: Base elevation of the view relative to its parent in pixels.
Expand Down
9 changes: 9 additions & 0 deletions apidoc/Titanium/UI/iOS/iOS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -340,20 +340,29 @@ properties:
type: Number
permission: read-only
since: "3.3.0"
deprecated:
since: "12.7.0"
notes: Use [Titanium.UI.CLIP_MODE_DEFAULT](Titanium.UI.CLIP_MODE_DEFAULT) instead.

- name: CLIP_MODE_DISABLED
summary: |
Use with <Titanium.UI.View.clipMode> to specify clipping behavior.
type: Number
permission: read-only
since: "3.3.0"
deprecated:
since: "12.7.0"
notes: Use [Titanium.UI.CLIP_MODE_DISABLED](Titanium.UI.CLIP_MODE_DISABLED) instead.

- name: CLIP_MODE_ENABLED
summary: |
Use with <Titanium.UI.View.clipMode> to specify clipping behavior.
type: Number
permission: read-only
since: "3.3.0"
deprecated:
since: "12.7.0"
notes: Use [Titanium.UI.CLIP_MODE_ENABLED](Titanium.UI.CLIP_MODE_ENABLED) instead.

- name: COLLISION_MODE_ALL
summary: |
Expand Down
3 changes: 3 additions & 0 deletions iphone/Classes/TiUIiOSProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,17 @@ - (NSNumber *)SCROLL_DECELERATION_RATE_FAST

- (NSNumber *)CLIP_MODE_DEFAULT
{
DEPRECATED_REPLACED(@"UI.iOS.CLIP_MODE_DEFAULT", @"12.7.0", @"UI.CLIP_MODE_DEFAULT");
return NUMINT(0);
}
- (NSNumber *)CLIP_MODE_ENABLED
{
DEPRECATED_REPLACED(@"UI.iOS.CLIP_MODE_ENABLED", @"12.7.0", @"UI.CLIP_MODE_ENABLED");
return NUMINT(1);
}
- (NSNumber *)CLIP_MODE_DISABLED
{
DEPRECATED_REPLACED(@"UI.iOS.CLIP_MODE_DISABLED", @"12.7.0", @"UI.CLIP_MODE_DISABLED");
return NUMINT(-1);
}

Expand Down
4 changes: 4 additions & 0 deletions iphone/Classes/UIModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@
@property (nonatomic, readonly) NSString *TEXT_STYLE_CALLOUT;
@property (nonatomic, readonly) NSString *TEXT_STYLE_LARGE_TITLE;

@property (nonatomic, readonly) NSNumber *CLIP_MODE_DEFAULT;
@property (nonatomic, readonly) NSNumber *CLIP_MODE_ENABLED;
@property (nonatomic, readonly) NSNumber *CLIP_MODE_DISABLED;

- (id)createMatrix2D:(id)args;
- (id)create2DMatrix:(id)args; // Deprecated since 8.0.0
- (id)createMatrix3D:(id)args;
Expand Down
12 changes: 12 additions & 0 deletions iphone/Classes/UIModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,18 @@ - (NSString *)TEXT_STYLE_LARGE_TITLE
{
return UIFontTextStyleLargeTitle;
}
- (NSNumber *)CLIP_MODE_DEFAULT
{
return NUMINT(0);
}
- (NSNumber *)CLIP_MODE_ENABLED
{
return NUMINT(1);
}
- (NSNumber *)CLIP_MODE_DISABLED
{
return NUMINT(-1);
}

- (void)setOverrideUserInterfaceStyle:(id)args
{
Expand Down
Loading