use gpui_kit::test::{TestQueryExt as _, TestWindowExt as _};
use gpui_kit::{
AppContext as _, Bounds, Context, IntoElement, ParentElement as _, Pixels, Point, Render,
Styled as _, TestAppContext, Window, WindowBounds, WindowOptions, canvas, div, px, size,
};
use std::{cell::Cell, rc::Rc, time::Duration};
use u0ui::t3::workspace::{KeybindingRow, keybinding_min_width};
type Slot = Rc<Cell<Option<Bounds<Pixels>>>>;
struct Page {
width: f32,
row: Slot,
}
impl Render for Page {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
let slot = self.row.clone();
div().w(px(self.width)).overflow_hidden().child(
div()
.relative()
.child(
KeybindingRow::new("kb", "Approve pending request", "⌘+⇧+Y")
.when_clause("pendingApproval")
.warning("Conflicts with another binding")
.custom(true)
.on_edit(|_, _| {}),
)
.child(
canvas(move |bounds, _, _| slot.set(Some(bounds)), |_, _, _, _| {})
.absolute()
.top_0()
.left_0()
.size_full(),
),
)
}
}
fn row_bounds(cx: &mut TestAppContext, width: f32) -> Bounds<Pixels> {
measure(cx, width).0
}
fn measure(cx: &mut TestAppContext, width: f32) -> (Bounds<Pixels>, Bounds<Pixels>, bool) {
cx.update(|cx| {
gpui_kit::init(cx);
u0ui::init(cx);
});
let row: Slot = Rc::default();
let window = cx.update(|cx| {
let row = row.clone();
gpui_kit::open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(Bounds {
origin: Point::default(),
size: size(px(1000.), px(400.)),
})),
..Default::default()
},
cx,
move |_, cx| cx.new(|_| Page { width, row }),
)
.expect("open test window")
.0
});
cx.executor().advance_clock(Duration::from_secs(1));
cx.run_until_parked();
cx.update_window(window, |_, window, cx| window.render_frame(cx))
.unwrap();
let edit = cx
.update_window(window, |_, window, _| {
window.find_by_label("Edit keybinding")
})
.unwrap();
(row.get().expect("measured"), edit.bounds(), edit.visible())
}
fn assert_edit_on_row(row: Bounds<Pixels>, edit: Bounds<Pixels>) {
assert!(
edit.origin.x >= row.origin.x && edit.right() <= row.right() - px(3.5),
"edit {edit:?} off row {row:?}"
);
assert!(
(f32::from(row.right() - edit.right()) - 4.).abs() < 0.5,
"{row:?} {edit:?}"
);
}
#[gpui_kit::test]
fn a_narrow_row_keeps_its_controls(cx: &mut TestAppContext) {
let (row, edit, visible) = measure(cx, 440.);
assert!((f32::from(row.size.width) - 440.).abs() < 0.5, "{row:?}");
assert!(visible, "Edit is clipped away");
assert_edit_on_row(row, edit);
}
#[gpui_kit::test]
fn a_row_narrower_than_its_columns_grows(cx: &mut TestAppContext) {
let (_, edit, _) = measure(cx, 240.);
assert!((f32::from(edit.size.width) - 32.).abs() < 0.5, "{edit:?}");
assert!(
f32::from(edit.right()) > keybinding_min_width(0.) + 40. - 4.,
"{edit:?}"
);
}
#[gpui_kit::test]
fn a_wide_row_keeps_the_command_on_one_line(cx: &mut TestAppContext) {
let row = row_bounds(cx, 960.);
assert!((f32::from(row.size.height) - 40.).abs() < 0.5, "{row:?}");
}
#[gpui_kit::test]
fn a_narrow_row_wraps_the_command(cx: &mut TestAppContext) {
let row = row_bounds(cx, 574.);
assert!(f32::from(row.size.height) > 40.5, "{row:?}");
assert!((f32::from(row.size.width) - 574.).abs() < 0.5, "{row:?}");
}