Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Fix relayout on window resize. Set up better anchor points for left/r…
Browse files Browse the repository at this point in the history
…ight window resizes.
  • Loading branch information
MortimerGoro committed Jul 12, 2019
1 parent 7cb40e2 commit 50e7192
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -976,8 +976,13 @@ public void removeWidget(final Widget aWidget) {
}

@Override
public void startWidgetResize(final Widget aWidget) {
queueRunnable(() -> startWidgetResizeNative(aWidget.getHandle()));
public void updateVisibleWidgets() {
queueRunnable(this::updateVisibleWidgetsNative);
}

@Override
public void startWidgetResize(final Widget aWidget, float anchorX) {
queueRunnable(() -> startWidgetResizeNative(aWidget.getHandle(), anchorX));
}

@Override
Expand Down Expand Up @@ -1193,8 +1198,9 @@ public void openNewWindow(String uri) {

private native void addWidgetNative(int aHandle, WidgetPlacement aPlacement);
private native void updateWidgetNative(int aHandle, WidgetPlacement aPlacement);
private native void updateVisibleWidgetsNative();
private native void removeWidgetNative(int aHandle);
private native void startWidgetResizeNative(int aHandle);
private native void startWidgetResizeNative(int aHandle, float anchorX);
private native void finishWidgetResizeNative(int aHandle);
private native void setWorldBrightnessNative(float aBrigthness);
private native void setTemporaryFilePath(String aPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,15 @@ private void finishWidgetResize() {
}

private void startWidgetResize() {
mWidgetManager.startWidgetResize(mAttachedWindow);
if (mAttachedWindow != null) {
float anchorX = 0.5f;
if (mAttachedWindow.getWindowPlacement() == Windows.WindowPlacement.LEFT) {
anchorX = 0.0f;
} else if (mAttachedWindow.getWindowPlacement() == Windows.WindowPlacement.RIGHT) {
anchorX = 1.0f;
}
mWidgetManager.startWidgetResize(mAttachedWindow, anchorX);
}
}

private void updateWidget() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ interface WorldClickListener {
void addWidget(@NonNull Widget aWidget);
void updateWidget(@NonNull Widget aWidget);
void removeWidget(@NonNull Widget aWidget);
void startWidgetResize(@NonNull Widget aWidget);
void updateVisibleWidgets();
void startWidgetResize(@NonNull Widget aWidget, float anchorX);
void finishWidgetResize(@NonNull Widget aWidget);
void addUpdateListener(@NonNull UpdateListener aUpdateListener);
void removeUpdateListener(@NonNull UpdateListener aUpdateListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ public void handleResizeEvent(float aWorldWidth, float aWorldHeight) {
mWidgetPlacement.height = (int) ((aWorldHeight * defaultHeight) / worldHeight) + mBorderWidth * 2;
mWidgetPlacement.worldWidth = aWorldWidth;
mWidgetManager.updateWidget(this);
mWidgetManager.updateVisibleWidgets();

if (mSaveResizeChanges) {
SettingsStore.getInstance(getContext()).setBrowserWorldWidth(aWorldWidth);
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,11 +880,11 @@ BrowserWorld::RemoveWidget(int32_t aHandle) {
}

void
BrowserWorld::StartWidgetResize(int32_t aHandle) {
BrowserWorld::StartWidgetResize(int32_t aHandle, const float aAnchorX) {
ASSERT_ON_RENDER_THREAD();
WidgetPtr widget = m.GetWidget(aHandle);
if (widget) {
widget->StartResize();
widget->StartResize(aAnchorX);
}
}

Expand Down Expand Up @@ -1313,14 +1313,20 @@ JNI_METHOD(void, updateWidgetNative)
}
}

JNI_METHOD(void, updateVisibleWidgetsNative)
(JNIEnv* aEnv, jobject) {
crow::BrowserWorld::Instance().UpdateVisibleWidgets();
}


JNI_METHOD(void, removeWidgetNative)
(JNIEnv*, jobject, jint aHandle) {
crow::BrowserWorld::Instance().RemoveWidget(aHandle);
}

JNI_METHOD(void, startWidgetResizeNative)
(JNIEnv*, jobject, jint aHandle) {
crow::BrowserWorld::Instance().StartWidgetResize(aHandle);
(JNIEnv*, jobject, jint aHandle, jfloat aAnchorX) {
crow::BrowserWorld::Instance().StartWidgetResize(aHandle, aAnchorX);
}

JNI_METHOD(void, finishWidgetResizeNative)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/BrowserWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class BrowserWorld {
void AddWidget(int32_t aHandle, const WidgetPlacementPtr& placement);
void UpdateWidget(int32_t aHandle, const WidgetPlacementPtr& aPlacement);
void RemoveWidget(int32_t aHandle);
void StartWidgetResize(int32_t aHandle);
void StartWidgetResize(int32_t aHandle, const float aAnchorX);
void FinishWidgetResize(int32_t aHandle);
void UpdateVisibleWidgets();
void LayoutWidget(int32_t aHandle);
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/cpp/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,20 @@ Widget::SetPlacement(const WidgetPlacementPtr& aPlacement) {
}

void
Widget::StartResize() {
Widget::StartResize(const float aAnchorX) {
vrb::Vector worldMin, worldMax;
GetWidgetMinAndMax(worldMin, worldMax);
if (m.resizer) {
m.resizer->SetSize(worldMin, worldMax);
m.resizer->SetAnchorX(aAnchorX);
} else {
vrb::RenderContextPtr render = m.context.lock();
if (!render) {
return;
}
vrb::CreationContextPtr create = render->GetRenderThreadCreationContext();
m.resizer = WidgetResizer::Create(create, this);
m.resizer->SetAnchorX(aAnchorX);
m.transform->InsertNode(m.resizer->GetRoot(), 0);
}
m.resizing = true;
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/Widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Widget {
vrb::TransformPtr GetTransformNode() const;
const WidgetPlacementPtr& GetPlacement() const;
void SetPlacement(const WidgetPlacementPtr& aPlacement);
void StartResize();
void StartResize(const float aAnchorX);
void FinishResize();
bool IsResizing() const;
void HandleResize(const vrb::Vector& aPoint, bool aPressed, bool& aResized, bool &aResizeEnded);
Expand Down
26 changes: 25 additions & 1 deletion app/src/main/cpp/WidgetResizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ struct ResizeHandle {
result->geometry = ResizeHandle::CreateGeometry(aContext);
result->transform = vrb::Transform::Create(aContext);
result->transform->AddNode(result->geometry);
result->root = vrb::Toggle::Create(aContext);
result->root->AddNode(result->transform);
result->resizeState = ResizeState ::Default;
UpdateResizeMaterial(result->geometry->GetRenderState(), result->resizeState);
return result;
Expand Down Expand Up @@ -336,13 +338,22 @@ struct ResizeHandle {
return geometry;
}

void SetVisible(const bool aVisible) {
if (visible != aVisible) {
root->ToggleAll(aVisible);
visible = aVisible;
}
}

vrb::Vector center;
ResizeMode resizeMode;
std::vector<ResizeBarPtr> attachedBars;
vrb::GeometryPtr geometry;
vrb::TogglePtr root;
vrb::TransformPtr transform;
ResizeState resizeState;
float touchRatio;
bool visible = true;
};

struct WidgetResizer::State {
Expand All @@ -361,11 +372,13 @@ struct WidgetResizer::State {
std::vector<ResizeBarPtr> resizeBars;
ResizeHandlePtr activeHandle;
bool wasPressed;
float anchorX;

State()
: widget(nullptr)
, resizing(false)
, wasPressed(false)
, anchorX(0.5f)
{}

void Initialize() {
Expand Down Expand Up @@ -425,7 +438,7 @@ struct WidgetResizer::State {
ResizeHandlePtr result = ResizeHandle::Create(create, aCenter, aResizeMode, aBars);
result->touchRatio = aTouchRatio;
resizeHandles.push_back(result);
root->InsertNode(result->transform, 0);
root->InsertNode(result->root, 0);
return result;
}

Expand Down Expand Up @@ -525,6 +538,9 @@ struct WidgetResizer::State {

ResizeHandlePtr GetIntersectingHandler(const vrb::Vector& point) {
for (const ResizeHandlePtr& handle: resizeHandles) {
if (!handle->visible) {
continue;
}
vrb::Vector worldCenter(min.x() + WorldWidth() * handle->center.x(), min.y() + WorldHeight() * handle->center.y(), 0.0f);
float distance = (point - worldCenter).Magnitude();
if (distance < kHandleRadius * handle->touchRatio) {
Expand Down Expand Up @@ -603,6 +619,14 @@ WidgetResizer::SetSize(const vrb::Vector& aMin, const vrb::Vector& aMax) {
m.Layout();
}

void
WidgetResizer::SetAnchorX(const float aAnchorX) {
m.anchorX = aAnchorX;
for (ResizeHandlePtr & handle: m.resizeHandles) {
handle->SetVisible(aAnchorX == 0.5f || handle->center.x() == 0.5f || handle->center.x() == aAnchorX);
}
}

void
WidgetResizer::ToggleVisible(bool aVisible) {
m.root->ToggleAll(aVisible);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/cpp/WidgetResizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class WidgetResizer {
static WidgetResizerPtr Create(vrb::CreationContextPtr& aContext, Widget * aWidget);
vrb::NodePtr GetRoot() const;
void SetSize(const vrb::Vector& aMin, const vrb::Vector& aMax);
void SetAnchorX(const float aAnchorX);
void ToggleVisible(bool aVisible);
bool TestIntersection(const vrb::Vector& point) const;
void HandleResizeGestures(const vrb::Vector& aPoint, bool aPressed, bool& aResized, bool &aResizeEnded);
Expand Down

0 comments on commit 50e7192

Please sign in to comment.