TextEditor widget differences between pre-release and now

I am following the Iced text editor YouTube video put out by Héctor and I have reached the point where I have added the line:column output under the text editor widget. But unlike the video where the line:column output is always at the bottom of the screen no matter how much content is in the text editor, the text editor in my version (trying to use version 0.12.1) pushes the line:column output off the screen until you scroll to the bottom of the TextEditor widget even though there is still padding involved and I can see that the TextEditor isn’t going off screen.

TextEditor scrolled to the top: https://imgur.com/a/esNZ7eI
TextEditor scrolled to the bottom: https://imgur.com/a/y7E8VOD

I have double checked, and other than the changes I needed to make to have the project build (changing on_edit to on_action) I can’t see any differences that would cause this behaviour. Is there something new about the TextEditor widget that needs to be set to have it match the behaviour in Héctor’s video?

Could you share the code?

Sure, here it is.

use iced::{Element, Sandbox, Theme};
use iced::widget::{column, container, text, text_editor};

fn main() -> iced::Result {
    Editor::run(iced::Settings::default() )
}

pub struct Editor {
    content: text_editor::Content,
}

impl Editor {
}

#[derive(Clone, Debug)]
pub enum EditorMessage {
    UpdateContent(text_editor::Action)
}

impl Sandbox for Editor {
    type Message = EditorMessage;

    fn new() -> Self {
        Self {
            content: text_editor::Content::with_text(include_str!("main.rs")),
        }
    }

    fn title(&self) -> String {
        "Iced Demo".into()
    }

    fn update(&mut self, message: Self::Message) {
        match message {
            EditorMessage::UpdateContent(action) => {
                self.content.perform(action)
            }
        }
    }

    fn view(&self) -> Element<'_, Self::Message> {
        let input = text_editor(&self.content)
            .on_action(EditorMessage::UpdateContent);

        let position = {
            let (line, column) = self.content.cursor_position();

            text(format!("{}:{}", line + 1, column + 1))
        };


        container(column![input, position])
            .padding(10)
            .into()
    }

    fn theme(&self) -> Theme {
        Theme::Dark
    }
}

Also for context, this is running on Windows 10 using an AMD GPU if that has any effect

Ah, I see.

The text_editor has now a Shrink behavior by default on its height. This means that it will fill space as needed; leaving no space left for the text.

This is a limitation of the layout approach of column and row widgets. There are plans to allow further customization (e.g. layout from bottom to top would solve the issue here).

The actual solution here is to use height(Length::Fill) on the text_editor. This causes the layout algorithm to measure the text first and use the remaining space for the editor.

2 Likes

Thanks, that fixed it. I had actually tried .height(Length::Fill) but I tried it on the container itself not the text editor, my assumption being that if I told the container to fill the window that it wouldn’t let the text widget be pushed outside the window.