Skip to content

Length::Fill fills window, not parent #304

Closed
@zackyancey

Description

@zackyancey

I'm new to GUI programming, so I may just be going about this the wrong way, but I haven't found a way to get the results that I'm looking for. Consider the following layout:

use iced::{button, Button, Column, Element, Length, Row, Sandbox, Settings, Text};

pub fn main() {
    App::run(Settings::default())
}

#[derive(Default)]
struct App {
    button_a: button::State,
    button_b: button::State,
    button_c: button::State,
}

impl Sandbox for App {
    type Message = ();

    fn new() -> Self {
        Self::default()
    }

    fn title(&self) -> String {
        String::from("Fill parent")
    }

    fn update(&mut self, _message: ()) {}

    fn view(&mut self) -> Element<()> {
        let button_a = Button::new(&mut self.button_a, Text::new("aaa")).on_press(());
        let button_b = Button::new(&mut self.button_b, Text::new("bbbb")).on_press(());
        let button_c = Button::new(&mut self.button_c, Text::new("c")).on_press(());
        Row::new()
            .height(Length::Shrink)
            .width(Length::Shrink)
            .push(button_a.height(Length::Units(100)))
            .push(
                Column::new()
                    .width(Length::Shrink)
                    .height(Length::Fill)
                    .push(button_b.width(Length::Fill).height(Length::Fill))
                    .push(button_c.width(Length::Fill).height(Length::Fill)),
            )
            .into()
    }
}

I want B and C to fill their column horizontally and vertically. Since the column's width is set to shrink, the column's width should be the shortest possible (just wide enough for button B). Similarly, the column's height should fill the row, which is shrunk to the height of button A. So something like this:

image

However, instead on desktop I get this:
image
The elements fill the entire window, not their container.

and on web:
image
Widths seem to work the way I hoped, but setting height doesn't seem to work at all.

I think what should be happening is that Length::Fill elements should only fill the space available in the parent, and Length::Shrink containers should take the minimum possible size for all elements and pick the largest one. If that's not the way it should work, then maybe a new variant (Length::FillParent?) would be useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions