Skip to content

Commit 468a014

Browse files
committed
Allow measuring arbitrary items in UniformList
1 parent c44db3b commit 468a014

File tree

2 files changed

+23
-77
lines changed

2 files changed

+23
-77
lines changed

crates/editor2/src/editor.rs

Lines changed: 7 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,13 @@ impl CodeActionsMenu {
16051605
.bg(cx.theme().colors().element_background)
16061606
.px_2()
16071607
.py_1()
1608+
.with_width_from_item(
1609+
self.actions
1610+
.iter()
1611+
.enumerate()
1612+
.max_by_key(|(_, action)| action.lsp_action.title.chars().count())
1613+
.map(|(ix, _)| ix),
1614+
)
16081615
.render();
16091616

16101617
if self.deployed_from_indicator {
@@ -1613,78 +1620,6 @@ impl CodeActionsMenu {
16131620

16141621
(cursor_position, element)
16151622
}
1616-
// enum ActionTag {}
1617-
1618-
// let container_style = style.autocomplete.container;
1619-
// let actions = self.actions.clone();
1620-
// let selected_item = self.selected_item;
1621-
// let element = UniformList::new(
1622-
// self.list.clone(),
1623-
// actions.len(),
1624-
// cx,
1625-
// move |_, range, items, cx| {
1626-
// let start_ix = range.start;
1627-
// for (ix, action) in actions[range].iter().enumerate() {
1628-
// let item_ix = start_ix + ix;
1629-
// items.push(
1630-
// MouseEventHandler::new::<ActionTag, _>(item_ix, cx, |state, _| {
1631-
// let item_style = if item_ix == selected_item {
1632-
// style.autocomplete.selected_item
1633-
// } else if state.hovered() {
1634-
// style.autocomplete.hovered_item
1635-
// } else {
1636-
// style.autocomplete.item
1637-
// };
1638-
1639-
// Text::new(action.lsp_action.title.clone(), style.text.clone())
1640-
// .with_soft_wrap(false)
1641-
// .contained()
1642-
// .with_style(item_style)
1643-
// })
1644-
// .with_cursor_style(CursorStyle::PointingHand)
1645-
// .on_down(MouseButton::Left, move |_, this, cx| {
1646-
// let workspace = this
1647-
// .workspace
1648-
// .as_ref()
1649-
// .and_then(|(workspace, _)| workspace.upgrade(cx));
1650-
// cx.window_context().defer(move |cx| {
1651-
// if let Some(workspace) = workspace {
1652-
// workspace.update(cx, |workspace, cx| {
1653-
// if let Some(task) = Editor::confirm_code_action(
1654-
// workspace,
1655-
// &ConfirmCodeAction {
1656-
// item_ix: Some(item_ix),
1657-
// },
1658-
// cx,
1659-
// ) {
1660-
// task.detach_and_log_err(cx);
1661-
// }
1662-
// });
1663-
// }
1664-
// });
1665-
// })
1666-
// .into_any(),
1667-
// );
1668-
// }
1669-
// },
1670-
// )
1671-
// .with_width_from_item(
1672-
// self.actions
1673-
// .iter()
1674-
// .enumerate()
1675-
// .max_by_key(|(_, action)| action.lsp_action.title.chars().count())
1676-
// .map(|(ix, _)| ix),
1677-
// )
1678-
// .contained()
1679-
// .with_style(container_style)
1680-
// .into_any();
1681-
1682-
// if self.deployed_from_indicator {
1683-
// *cursor_position.column_mut() = 0;
1684-
// }
1685-
1686-
// (cursor_position, element)
1687-
// }
16881623
}
16891624

16901625
pub struct CopilotState {

crates/gpui2/src/elements/uniform_list.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ where
3030
id: id.clone(),
3131
style,
3232
item_count,
33+
item_to_measure_index: 0,
3334
render_items: Box::new(move |view, visible_range, cx| {
3435
f(view, visible_range, cx)
3536
.into_iter()
@@ -45,6 +46,7 @@ pub struct UniformList<V: 'static> {
4546
id: ElementId,
4647
style: StyleRefinement,
4748
item_count: usize,
49+
item_to_measure_index: usize,
4850
render_items: Box<
4951
dyn for<'a> Fn(
5052
&'a mut V,
@@ -112,7 +114,7 @@ impl<V: 'static> Element<V> for UniformList<V> {
112114
cx: &mut ViewContext<V>,
113115
) -> Self::ElementState {
114116
element_state.unwrap_or_else(|| {
115-
let item_size = self.measure_first_item(view_state, None, cx);
117+
let item_size = self.measure_item(view_state, None, cx);
116118
UniformListState {
117119
interactive: InteractiveElementState::default(),
118120
item_size,
@@ -174,7 +176,7 @@ impl<V: 'static> Element<V> for UniformList<V> {
174176
let content_size;
175177
if self.item_count > 0 {
176178
let item_height = self
177-
.measure_first_item(view_state, Some(padded_bounds.size.width), cx)
179+
.measure_item(view_state, Some(padded_bounds.size.width), cx)
178180
.height;
179181
if let Some(scroll_handle) = self.scroll_handle.clone() {
180182
scroll_handle.0.lock().replace(ScrollHandleState {
@@ -240,14 +242,23 @@ impl<V: 'static> Element<V> for UniformList<V> {
240242
}
241243

242244
impl<V> UniformList<V> {
243-
fn measure_first_item(
245+
pub fn with_width_from_item(mut self, item_index: Option<usize>) -> Self {
246+
self.item_to_measure_index = item_index.unwrap_or(0);
247+
self
248+
}
249+
250+
fn measure_item(
244251
&self,
245252
view_state: &mut V,
246253
list_width: Option<Pixels>,
247254
cx: &mut ViewContext<V>,
248255
) -> Size<Pixels> {
249-
let mut items = (self.render_items)(view_state, 0..1, cx);
250-
debug_assert_eq!(items.len(), 1);
256+
if self.item_count == 0 {
257+
return Size::default();
258+
}
259+
260+
let item_ix = cmp::min(self.item_to_measure_index, self.item_count - 1);
261+
let mut items = (self.render_items)(view_state, item_ix..item_ix + 1, cx);
251262
let mut item_to_measure = items.pop().unwrap();
252263
let available_space = size(
253264
list_width.map_or(AvailableSpace::MinContent, |width| {

0 commit comments

Comments
 (0)