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 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip
- Loading branch information
Showing
14 changed files
with
352 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
120 changes: 120 additions & 0 deletions
120
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,120 @@ | ||
package org.mozilla.vrbrowser.ui.widgets; | ||
|
||
import android.content.Context; | ||
import android.graphics.PointF; | ||
import android.graphics.Rect; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.view.ViewTreeObserver; | ||
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 int mPaddingH; | ||
private int mPaddingV; | ||
|
||
public TooltipWidget(Context aContext) { | ||
super(aContext); | ||
|
||
initialize(); | ||
} | ||
|
||
private void initialize() { | ||
inflate(getContext(), R.layout.tooltip, this); | ||
|
||
mText = findViewById(R.id.tooltipText); | ||
|
||
ViewGroup layout = findViewById(R.id.layout); | ||
mPaddingH = layout.getPaddingStart() + layout.getPaddingEnd(); | ||
mPaddingV = layout.getPaddingTop() + layout.getPaddingBottom(); | ||
} | ||
|
||
@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); | ||
mWidgetPlacement.width = (int)((getMeasuredWidth() + mPaddingH)/mWidgetPlacement.density); | ||
mWidgetPlacement.height = (int)((getMeasuredHeight() + mPaddingV)/mWidgetPlacement.density); | ||
super.show(aShowFlags); | ||
|
||
ViewTreeObserver viewTreeObserver = getViewTreeObserver(); | ||
if (viewTreeObserver.isAlive()) { | ||
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { | ||
@Override | ||
public void onGlobalLayout() { | ||
getViewTreeObserver().removeOnGlobalLayoutListener(this); | ||
mWidgetPlacement.width = (int)(getWidth() / mWidgetPlacement.density); | ||
mWidgetPlacement.height = (int)(getHeight() / mWidgetPlacement.density); | ||
mWidgetManager.updateWidget(TooltipWidget.this); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
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); | ||
|
||
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.density = density; | ||
mWidgetPlacement.anchorY = 1.0f; | ||
mWidgetPlacement.parentAnchorY = 0.0f; | ||
float densityRatio = mWidgetPlacement.density / getContext().getResources().getDisplayMetrics().density; | ||
mTranslation = new PointF( | ||
(offsetViewBounds.left + mTargetView.getWidth() / 2) * densityRatio, | ||
-offsetViewBounds.top * densityRatio); | ||
} else { | ||
mWidgetPlacement.density = density; | ||
mWidgetPlacement.anchorY = 0.0f; | ||
mWidgetPlacement.parentAnchorY = 1.0f; | ||
float densityRatio = mWidgetPlacement.density / getContext().getResources().getDisplayMetrics().density; | ||
mTranslation = new PointF( | ||
(offsetViewBounds.left + mTargetView.getWidth() / 2) * densityRatio, | ||
offsetViewBounds.top * densityRatio); | ||
} | ||
} | ||
} | ||
|
||
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,7 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<shape xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:shape="rectangle"> | ||
<corners android:radius="30dp" /> | ||
<stroke android:color="@color/rhino_blur" android:width="@dimen/blur_radius" /> | ||
<solid android:color="@color/rhino"/> | ||
</shape> |
Oops, something went wrong.