const RAIL_BUTTON_WIDTH: i32 = 83;
const RAIL_BUTTON_LABEL_CHARS: i32 = 10;

/// Build a rail button that can shrink without forcing the operations column wider.
fn rail_button(label: &str, tooltip: &str) -> gtk::Button {
    let button = gtk::Button::new();
    button.set_hexpand(true);
    button.set_tooltip_text(Some(tooltip));
    stabilize_button(&button, RAIL_BUTTON_WIDTH);

    let text = gtk::Label::new(Some(label));
    text.set_ellipsize(pango::EllipsizeMode::End);
    text.set_halign(gtk::Align::Center);
    text.set_max_width_chars(RAIL_BUTTON_LABEL_CHARS);
    text.set_xalign(0.5);
    button.set_child(Some(&text));
    button
}

/// Updates both the button property and the custom ellipsized label child.
pub(crate) fn set_rail_button_label(button: &gtk::Button, label: &str) {
    button.set_label(label);
    if let Some(child_label) = button
        .child()
        .and_then(|child| child.downcast::<gtk::Label>().ok())
    {
        child_label.set_text(label);
    }
}

fn stabilize_button(button: &impl IsA<gtk::Widget>, width: i32) {
    button.as_ref().set_size_request(width, 36);
}
