This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added support for tooltips * Improved widget size calculation and layouting * Fixed visual issues
- Loading branch information
1 parent
3540f71
commit ed251cf
Showing
14 changed files
with
347 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/TooltipWidget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package org.mozilla.vrbrowser.ui.widgets; | ||
|
||
import android.content.Context; | ||
import android.graphics.PointF; | ||
import android.graphics.Rect; | ||
import android.view.View; | ||
import android.widget.TextView; | ||
|
||
import org.mozilla.vrbrowser.R; | ||
import org.mozilla.vrbrowser.utils.ViewUtils; | ||
|
||
public class TooltipWidget extends UIWidget { | ||
|
||
private View mTargetView; | ||
private UIWidget mParentWidget; | ||
protected TextView mText; | ||
private PointF mTranslation; | ||
private float mRatio; | ||
private float mDensityRatio; | ||
|
||
public TooltipWidget(Context aContext) { | ||
super(aContext); | ||
|
||
initialize(); | ||
} | ||
|
||
private void initialize() { | ||
inflate(getContext(), R.layout.tooltip, this); | ||
|
||
mText = findViewById(R.id.tooltipText); | ||
} | ||
|
||
@Override | ||
protected void initializeWidgetPlacement(WidgetPlacement aPlacement) { | ||
aPlacement.visible = false; | ||
aPlacement.width = 0; | ||
aPlacement.height = 0; | ||
aPlacement.parentAnchorX = 0.0f; | ||
aPlacement.parentAnchorY = 1.0f; | ||
aPlacement.anchorX = 0.5f; | ||
aPlacement.anchorY = 0.5f; | ||
aPlacement.translationZ = WidgetPlacement.unitFromMeters(getContext(), R.dimen.tooltip_z_distance); | ||
} | ||
|
||
@Override | ||
public void show(@ShowFlags int aShowFlags) { | ||
measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), | ||
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); | ||
mWidgetPlacement.translationX = mTranslation.x * (mRatio / mWidgetPlacement.density); | ||
mWidgetPlacement.translationY = mTranslation.y * (mRatio / mWidgetPlacement.density); | ||
int paddingH = getPaddingStart() + getPaddingEnd(); | ||
int paddingV = getPaddingTop() + getPaddingBottom(); | ||
mWidgetPlacement.width = (int)(WidgetPlacement.convertPixelsToDp(getContext(), getMeasuredWidth() + paddingH)/mDensityRatio); | ||
mWidgetPlacement.height = (int)(WidgetPlacement.convertPixelsToDp(getContext(), getMeasuredHeight() + paddingV)/mDensityRatio); | ||
|
||
super.show(aShowFlags); | ||
} | ||
|
||
public void setLayoutParams(View targetView) { | ||
this.setLayoutParams(targetView, ViewUtils.TooltipPosition.BOTTOM); | ||
} | ||
|
||
public void setLayoutParams(View targetView, ViewUtils.TooltipPosition position) { | ||
this.setLayoutParams(targetView, position, mWidgetPlacement.density); | ||
} | ||
|
||
public void setLayoutParams(View targetView, ViewUtils.TooltipPosition position, float density) { | ||
mTargetView = targetView; | ||
mParentWidget = ViewUtils.getParentWidget(mTargetView); | ||
if (mParentWidget != null) { | ||
mRatio = WidgetPlacement.worldToWidgetRatio(mParentWidget); | ||
mWidgetPlacement.density = density; | ||
mDensityRatio = mWidgetPlacement.density / getContext().getResources().getDisplayMetrics().density; | ||
|
||
Rect offsetViewBounds = new Rect(); | ||
getDrawingRect(offsetViewBounds); | ||
mParentWidget.offsetDescendantRectToMyCoords(mTargetView, offsetViewBounds); | ||
|
||
mWidgetPlacement.parentHandle = mParentWidget.getHandle(); | ||
// At the moment we only support showing tooltips on top or bottom of the target view | ||
if (position == ViewUtils.TooltipPosition.BOTTOM) { | ||
mWidgetPlacement.anchorY = 1.0f; | ||
mWidgetPlacement.parentAnchorY = 0.0f; | ||
mTranslation = new PointF( | ||
(offsetViewBounds.left + mTargetView.getWidth() / 2) * mDensityRatio, | ||
-offsetViewBounds.top * mDensityRatio); | ||
} else { | ||
mWidgetPlacement.anchorY = 0.0f; | ||
mWidgetPlacement.parentAnchorY = 1.0f; | ||
mTranslation = new PointF( | ||
(offsetViewBounds.left + mTargetView.getWidth() / 2) * mDensityRatio, | ||
offsetViewBounds.top * mDensityRatio); | ||
} | ||
} | ||
} | ||
|
||
public void setText(String text) { | ||
mText.setText(text); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
app/src/common/shared/org/mozilla/vrbrowser/utils/ViewUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.mozilla.vrbrowser.utils; | ||
|
||
import android.view.View; | ||
import android.view.ViewParent; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import org.mozilla.vrbrowser.ui.widgets.UIWidget; | ||
|
||
public class ViewUtils { | ||
|
||
public enum TooltipPosition { | ||
TOP(0), BOTTOM(1); | ||
int id; | ||
|
||
TooltipPosition(int id) { | ||
this.id = id; | ||
} | ||
|
||
public static TooltipPosition fromId(int id) { | ||
for (TooltipPosition f : values()) { | ||
if (f.id == id) return f; | ||
} | ||
throw new IllegalArgumentException(); | ||
} | ||
} | ||
|
||
public static UIWidget getParentWidget(@NonNull View view) { | ||
if (view == null) | ||
return null; | ||
|
||
ViewParent v = view.getParent(); | ||
if (v instanceof UIWidget) { | ||
return (UIWidget)v; | ||
|
||
} else if (v instanceof View){ | ||
return getParentWidget((View)v); | ||
|
||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<item> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> | ||
<corners android:radius="30dp" /> | ||
<stroke android:width="@dimen/blur_radius" android:color="@color/fog_blur" /> | ||
</shape> | ||
</item> | ||
<item android:start="@dimen/blur_radius_half" android:end="@dimen/blur_radius_half" android:top="@dimen/blur_radius_half" android:bottom="@dimen/blur_radius_half"> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> | ||
<corners android:radius="30dp" /> | ||
<solid android:color="@color/rhino" /> | ||
<stroke android:width="2dp" android:color="@color/rhino_blur" /> | ||
</shape> | ||
</item> | ||
</layer-list> |
Oops, something went wrong.