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

Reposition a popup widget when it extends beyond the screen size #407

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions include/nanogui/popup.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class NANOGUI_EXPORT Popup : public Window {
protected:
Window *mParentWindow;
Vector2i mAnchorPos;
Vector2i pPos;
int mAnchorHeight;
Side mSide;
public:
Expand Down
10 changes: 8 additions & 2 deletions src/popup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
BSD-style license that can be found in the LICENSE.txt file.
*/

#include <nanogui/screen.h>
#include <nanogui/popup.h>
#include <nanogui/theme.h>
#include <nanogui/opengl.h>
Expand Down Expand Up @@ -37,7 +38,12 @@ void Popup::performLayout(NVGcontext *ctx) {
void Popup::refreshRelativePlacement() {
mParentWindow->refreshRelativePlacement();
mVisible &= mParentWindow->visibleRecursive();
mPos = mParentWindow->position() + mAnchorPos - Vector2i(0, mAnchorHeight);
pPos = mParentWindow->position() + mAnchorPos - Vector2i(0, mAnchorHeight);

const Screen* screen = this->screen();
assert(screen);
mPos = pPos.cwiseMax(Vector2i::Zero());
mPos = mPos.cwiseMin(screen->size() - mSize);
}

void Popup::draw(NVGcontext* ctx) {
Expand Down Expand Up @@ -67,7 +73,7 @@ void Popup::draw(NVGcontext* ctx) {
nvgBeginPath(ctx);
nvgRoundedRect(ctx, mPos.x(), mPos.y(), mSize.x(), mSize.y(), cr);

Vector2i base = mPos + Vector2i(0, mAnchorHeight);
Vector2i base = pPos + Vector2i(0, mAnchorHeight);
int sign = -1;
if (mSide == Side::Left) {
base.x() += mSize.x();
Expand Down