Skip to content
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

Android: Apply slider padding from css #170

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -23,13 +23,15 @@
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewProps;
import com.facebook.react.uimanager.annotations.ReactProp;
import com.facebook.react.uimanager.annotations.ReactPropGroup;
import com.facebook.yoga.YogaMeasureFunction;
import com.facebook.yoga.YogaMeasureMode;
import com.facebook.yoga.YogaMeasureOutput;
import com.facebook.yoga.YogaNode;
import java.util.Map;

import javax.annotation.Nullable;
import android.content.res.Resources;

/**
* Manages instances of {@code ReactSlider}.
Expand Down Expand Up @@ -213,6 +215,33 @@ public void setInverted(ReactSlider view, boolean inverted) {
else view.setScaleX(1f);
}


public static int dpToPx(int dp) {
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

public static int pxToDp(int px) {
Copy link
Member

Choose a reason for hiding this comment

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

Why not just using PixelUtil, instead of implementing both conversions on your own?

return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}

@ReactPropGroup(names = {
ViewProps.PADDING,
ViewProps.PADDING_LEFT,
ViewProps.PADDING_TOP,
ViewProps.PADDING_RIGHT,
ViewProps.PADDING_BOTTOM,
}, customType = "Style")
public void setStyle(ReactSlider view, int index, Integer value) {
value = dpToPx(value);
if(index == 0) {
view.setPadding(value, value, value, value);
} else {
int array[] = {view.getPaddingLeft(), view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom()};
array[index - 1] = value;
view.setPadding(array[0], array[1], array[2], array[3]);
}
}

@Override
protected void addEventEmitters(final ThemedReactContext reactContext, final ReactSlider view) {
view.setOnSeekBarChangeListener(ON_CHANGE_LISTENER);
Expand Down