#![cfg(all(target_os = "linux", feature = "gtk-native"))]
use gtk::prelude::*;
#[test]
fn a_window_can_shrink_below_the_extent_of_its_controls() {
let (tx, rx) = std::sync::mpsc::channel();
std::thread::spawn(move || {
let result = std::panic::catch_unwind(|| {
if gtk::init().is_err() {
return Err("no GTK display available".to_string());
}
let window = gtk::Window::new(gtk::WindowType::Toplevel);
window.set_default_size(1440, 900);
let root = gtk::Box::new(gtk::Orientation::Vertical, 0);
let viewport =
gtk::ScrolledWindow::new(None::<>k::Adjustment>, None::<>k::Adjustment>);
viewport.set_policy(gtk::PolicyType::Never, gtk::PolicyType::Never);
let fixed = gtk::Fixed::new();
let _ = &viewport;
root.pack_start(&fixed, true, true, 0);
window.add(&root);
let left = gtk::DrawingArea::new();
left.set_size_request(932, 360);
fixed.put(&left, 12, 42);
let right = gtk::DrawingArea::new();
right.set_size_request(472, 242);
fixed.put(&right, 956, 42);
window.show_all();
for _ in 0..50 {
while gtk::events_pending() {
gtk::main_iteration_do(false);
}
}
let (min_w, min_h) = window.size_request();
let (fixed_w, _) = fixed.size_request();
let left_alloc = left.allocation();
let right_alloc = right.allocation();
if left_alloc.width() != 932 || right_alloc.width() != 472 {
return Err(format!(
"the controls lost their size: left={:?} right={:?}",
(left_alloc.x(), left_alloc.y(), left_alloc.width(), left_alloc.height()),
(right_alloc.x(), right_alloc.y(), right_alloc.width(), right_alloc.height())
));
}
if min_w >= 1428 {
return Err(format!(
"the window's minimum is {min_w}px, which is the content's \
extent (fixed reports {fixed_w}) — the window cannot be shrunk"
));
}
Ok((min_w, min_h))
});
let _ = tx.send(result);
});
match rx.recv_timeout(std::time::Duration::from_secs(30)) {
Ok(Ok(Ok((min_w, min_h)))) => {
println!("window minimum after the fix: {min_w}x{min_h}");
}
Ok(Ok(Err(message))) => panic!("{message}"),
Ok(Err(_)) => panic!("the GTK thread panicked"),
Err(_) => panic!("the GTK thread did not report within 30s"),
}
}