Skip to content

table widget#3018

Merged
hecrj merged 11 commits intomasterfrom
feature/table-widget
Jul 18, 2025
Merged

table widget#3018
hecrj merged 11 commits intomasterfrom
feature/table-widget

Conversation

@hecrj
Copy link
Member

@hecrj hecrj commented Jul 18, 2025

This PR introduces a new built-in table widget.

iced-table.mp4

The new table example shows how to use it:

let bold = |header| {
    text(header).font(Font {
        weight: font::Weight::Bold,
        ..Font::DEFAULT
    })
};

let columns = [
    table::column(bold("Name"), |event: &Event| text(&event.name)),
    table::column(bold("Time"), |event: &Event| {
        let minutes = event.duration.as_secs() / 60;

        text!("{minutes} min").style(if minutes > 90 {
            text::warning
        } else {
            text::default
        })
    })
    .align_x(Right)
    .align_y(Center),
    table::column(bold("Price"), |event: &Event| {
        if event.price > 0.0 {
            text!("${:.2}", event.price).style(
                if event.price > 100.0 {
                    text::warning
                } else {
                    text::default
                },
            )
        } else {
            text("Free").style(text::success).width(Fill).center()
        }
    })
    .align_x(Right)
    .align_y(Center),
    table::column(bold("Rating"), |event: &Event| {
        text!("{:.2}", event.rating).style(if event.rating > 4.7 {
            text::success
        } else if event.rating < 2.0 {
            text::danger
        } else {
            text::default
        })
    })
    .align_x(Right)
    .align_y(Center),
];

table(columns, &self.events)

Additionally, the markdown widget supports tables now!

This PR also includes a QoL change that implements From<Option<T>> for Element. Effectively, this allows users to directly use any Option containing a widget directly in view code—rendering push_maybe methods redundant.

@hecrj hecrj added this to the 0.14 milestone Jul 18, 2025
@hecrj hecrj enabled auto-merge July 18, 2025 19:33
@hecrj hecrj merged commit 71b0b6a into master Jul 18, 2025
30 checks passed
@hecrj hecrj deleted the feature/table-widget branch July 18, 2025 19:38
@GyulyVGC
Copy link
Contributor

GyulyVGC commented Jul 18, 2025

That's awesome, thanks for all the work 🙏

Question: would it be possible to truncate strings with ellipsis in case the content is too large for its column's width?
I don't know if iced / cosmic support this already...

@hecrj
Copy link
Member Author

hecrj commented Jul 18, 2025

@GyulyVGC That is better implemented as an ellipsized text widget at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments