获取 GWT DialogBox 绝对位置 - onLoad/onAttach/show 没有帮助

发布于 2024-12-24 00:43:04 字数 1544 浏览 0 评论 0原文

我陷入了获取 DialogBox 的绝对位置的困境。我知道这是 PopupPanel (它是 DialogBox 的父级)设置 的常见问题(也是奇怪的解决方法),但是如果我怎么办?想要得到它,盒子附加到 DOM 的确切时刻是什么?重写 showonAttachshow 都没有帮助:

class MyDialog extends DialogBox {

    public MyDialog(. . .) {
        ComplexPanel vert = new VerticalPanel();
        vert.add("Test");
        vert.add(new Button("Close", new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                MyDialog.this.hide();
            }
        }));
        setWidget(vert);

        this.addAttachHandler(new AttachEvent.Handler() {

            @Override
            public void onAttachOrDetach(AttachEvent event) {
                if (event.isAttached()) Log.debug("attach:"+MyDialog.this.getAbsoluteLeft() +";"+
                                                            MyDialog.this.getAbsoluteTop());
            }
        });
    }

    @Override
    protected void onLoad() {
        super.onLoad();
        Log.debug("load:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
    }

    @Override
    public void show() {
        super.show();
        Log.debug("show:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
    }

}

所以当我调用 new MyDialog().show();,所有这些行都会记录 0;0,但是对话框位于页面的中心。但我想要的是 offsetParent 位置链的总和。 (如果使用 JSNI 来检查的话,即使在 JavaScript 中,这些时刻它们也是 0)

同样,setPopupPositionAndShow 允许设置位置,但不能获取它:(

I am stuck in getting an absolute position of DialogBox. I know it is the common problem (and strange workaround) for PopupPanel (which is parent to DialogBox) to set it, but what if I want to get it, what is the exact moment when the box attached to DOM? Neither overriding show nor onAttach nor show does not help:

class MyDialog extends DialogBox {

    public MyDialog(. . .) {
        ComplexPanel vert = new VerticalPanel();
        vert.add("Test");
        vert.add(new Button("Close", new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                MyDialog.this.hide();
            }
        }));
        setWidget(vert);

        this.addAttachHandler(new AttachEvent.Handler() {

            @Override
            public void onAttachOrDetach(AttachEvent event) {
                if (event.isAttached()) Log.debug("attach:"+MyDialog.this.getAbsoluteLeft() +";"+
                                                            MyDialog.this.getAbsoluteTop());
            }
        });
    }

    @Override
    protected void onLoad() {
        super.onLoad();
        Log.debug("load:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
    }

    @Override
    public void show() {
        super.show();
        Log.debug("show:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
    }

}

So when I call new MyDialog().show();, all this lines do log 0;0, however dialog is positioned in center of a page. But what I want is the sum of the chain of offsetParent positions. (And they are 0 in these moments even in JavaScript, if use JSNI to check this)

Again, setPopupPositionAndShow allows to set position but not get it :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2024-12-31 00:43:04

最后,我已经让它工作了:

@Override
public void setPopupPosition(int left, int top) {
    super.setPopupPosition(left, top);
    if (this.isAttached()) Log.debug("setPos:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
}

它获得了正确的位置,我希望这是正确的方法,并且每次都会调用 setPopupPosition 。您甚至可以在使用setPopupPositionAndShow时手动调用它。

我认为让这个问题留在“为了子孙后代”是明智的。


更新。如果您计划调用 center(...) 或对话框的某些类似方法,请注意 setPopupPosition 将被调用两次或更多次(可能是第一次使用 0, 0),即使您会检查它是否 isAttached()。添加一些额外的检查以确保当前调用中的位置正确。

Finally, I've got this to work:

@Override
public void setPopupPosition(int left, int top) {
    super.setPopupPosition(left, top);
    if (this.isAttached()) Log.debug("setPos:"+this.getAbsoluteLeft() +";"+this.getAbsoluteTop());
}

It gets the proper position and I hope it is the right way to do it and setPopupPosition is called every time. You will even call it manually when using setPopupPositionAndShow.

I think it will be wise to let this question stay at SO "for future generations".


Upd. If you plan to call center(...) or some similar method of your dialog, be aware that setPopupPosition will be called twice or more times (may be first time with 0, 0), even if you'll check if it isAttached(). Add some additional check to ensure that positions are correct in current call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文