Project

General

Profile

How to get a container's height whenever the browser window is resized

Added by Ola A 4 months ago

Hello,

I would like to track the height of a WContainerWidget. I need a way to get the height of the widget whenever the browser is resized. Does anyone know how I may do this?
Thanks.

Kind regards,

Ola


Replies (3)

RE: How to get a container's height whenever the browser window is resized - Added by Wim Dumon 4 months ago

Ola,

Wt supports this in combination with layout managers. See documentation of WWidget::setLayoutSizeAware() and layoutSizeChanged().

Wim.

RE: How to get a container's height whenever the browser window is resized - Added by Ola A 4 months ago

Thanks, Wim.

I will take a look at that.

Ola

RE: How to get a container's height whenever the browser window is resized - Added by Mark Travis 4 months ago

If you are using the "old school" layout patterns, those will work.

However, if you are using Bootstrap 5 and templates, that won't work. Here's how I solved it based on a couple of posts from a few years ago.

    screenSignal = std::make_unique<Wt::JSignal<std::string, std::string>>(this, "screenSignal");
    std::initializer_list<std::string> jsList = {"width", "height"};
    screenSignal->connect(this, &YourModule::getScreenSize);

    screenSignalJavaScript = "var width = window.innerWidth"
            "|| document.documentElement.clientWidth"
            "|| document.body.clientWidth;"

            "var height = window.innerHeight"
            "|| document.documentElement.clientHeight"
            "|| document.body.clientHeight;" +
            screenSignal->createCall(jsList);

    doJavaScript(screenSignalJavaScript);

When doJavaScript is executed, it will call back your 'getScreenSize' method with a (std::string, std::string) signature.

Unfortunately, this only works the first time you run doJavaScript, which I do in the initializer. I haven't spent any time yet figuring out how to propogate a resize signal from the browser, but my future clients aren't likely to resize the screen where I needed this once they get there.

The signal and callback MUST be defined before doJavaScript.

    (1-3/3)