PistonDevelopers / conrod

An easy-to-use, 2D GUI library written entirely in Rust.
Other
3.35k stars 296 forks source link

Dynamic size from window #1434

Closed tachyon-ops closed 2 years ago

tachyon-ops commented 2 years ago

Hi all,

I am using conrad with backend wgpu. I was able to extract the conrod_example_shared UI example and place it in a standalone project. Now I went on and started tweaking and figuring out what all the moving parts were doing. I came across the fact that I cannot use a dynamic window size. Say, instead of setting up the window like this:

let window = winit::window::WindowBuilder::new()
            .with_title("Conrod with wgpu")
            .with_inner_size(winit::dpi::LogicalSize {
                width: WIN_W,
                height: WIN_H,
            })
            .build(&event_loop)
            .unwrap();

I would like to allow the window to be constructed without a defined size. Then later on we could setup the UI like this:

let mut ui = conrod_core::UiBuilder::new([
        window.inner_size().width as f64,
        window.inner_size().height as f64,
    ])
    .theme(conrod_example_shared::theme())
    .build();

instead of

let mut ui = conrod_core::UiBuilder::new([WIN_W as f64, WIN_H as f64])
        .theme(conrod_example_shared::theme())
        .build();

But then I get the following error:

thread 'main' panicked at 'Handling wgpu errors as fatal by default', /Users/[ME]/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-0.9.0/src/backend/direct.rs:1978:5

If I comment out render_pass.set_scissor_rect(x, y, w, h); I get to run the example, but of course my layout is broken.

Is it possible to make the size of the UI dynamic from window size instead of making the window a specific size? That would be nice for any kind of window from any kind of platform (also for fullscreen). Am I missing something?

tachyon-ops commented 2 years ago

Actually, got it:

let scale_factor = window.scale_factor();
    let mut size = window.inner_size();
    let dimensions: LogicalSize<u32> = size.to_logical(scale_factor);
    let mut ui = conrod_core::UiBuilder::new([dimensions.width as f64, dimensions.height as f64])
        .theme(conrod_example_shared::theme())
        .build();

We need to convert from window dimensions (which are scaled) to actual pixel dimensions. Then it's fine.