chore: Upgrade to GPUI3 (#6)

* wip: gpui3

* wip: gpui3

* chore: fix clippy
This commit is contained in:
reya
2025-01-28 08:25:49 +07:00
committed by GitHub
parent 3c15e74e56
commit 72a6d79bc5
62 changed files with 2572 additions and 2511 deletions

View File

@@ -5,18 +5,19 @@ use crate::{
v_flex, Icon, IconName, Size,
};
use gpui::{
actions, div, prelude::FluentBuilder, px, uniform_list, AnyElement, AppContext, Entity,
FocusHandle, FocusableView, InteractiveElement, IntoElement, KeyBinding, Length,
actions, div, prelude::FluentBuilder, px, uniform_list, AnyElement, App, AppContext, Context,
Entity, FocusHandle, Focusable, InteractiveElement, IntoElement, KeyBinding, Length,
ListSizingBehavior, MouseButton, ParentElement, Render, ScrollStrategy, SharedString, Styled,
Task, UniformListScrollHandle, View, ViewContext, VisualContext, WindowContext,
Task, UniformListScrollHandle, Window,
};
use smol::Timer;
use std::{cell::Cell, rc::Rc, time::Duration};
actions!(list, [Cancel, Confirm, SelectPrev, SelectNext]);
pub fn init(cx: &mut AppContext) {
pub fn init(cx: &mut App) {
let context: Option<&str> = Some("List");
cx.bind_keys([
KeyBinding::new("escape", Cancel, context),
KeyBinding::new("enter", Confirm, context),
@@ -32,20 +33,30 @@ pub trait ListDelegate: Sized + 'static {
/// When Query Input change, this method will be called.
/// You can perform search here.
fn perform_search(&mut self, query: &str, cx: &mut ViewContext<List<Self>>) -> Task<()> {
fn perform_search(
&mut self,
query: &str,
window: &mut Window,
cx: &mut Context<List<Self>>,
) -> Task<()> {
Task::ready(())
}
/// Return the number of items in the list.
fn items_count(&self, cx: &AppContext) -> usize;
fn items_count(&self, cx: &App) -> usize;
/// Render the item at the given index.
///
/// Return None will skip the item.
fn render_item(&self, ix: usize, cx: &mut ViewContext<List<Self>>) -> Option<Self::Item>;
fn render_item(
&self,
ix: usize,
window: &mut Window,
cx: &mut Context<List<Self>>,
) -> Option<Self::Item>;
/// Return a Element to show when list is empty.
fn render_empty(&self, cx: &mut ViewContext<List<Self>>) -> impl IntoElement {
fn render_empty(&self, window: &mut Window, cx: &mut Context<List<Self>>) -> impl IntoElement {
div()
}
@@ -56,30 +67,39 @@ pub trait ListDelegate: Sized + 'static {
/// For example: The last search results, or the last selected item.
///
/// Default is None, that means no initial state.
fn render_initial(&self, cx: &mut ViewContext<List<Self>>) -> Option<AnyElement> {
fn render_initial(
&self,
window: &mut Window,
cx: &mut Context<List<Self>>,
) -> Option<AnyElement> {
None
}
/// Return the confirmed index of the selected item.
fn confirmed_index(&self, cx: &AppContext) -> Option<usize> {
fn confirmed_index(&self, cx: &App) -> Option<usize> {
None
}
/// Set the selected index, just store the ix, don't confirm.
fn set_selected_index(&mut self, ix: Option<usize>, cx: &mut ViewContext<List<Self>>);
fn set_selected_index(
&mut self,
ix: Option<usize>,
window: &mut Window,
cx: &mut Context<List<Self>>,
);
/// Set the confirm and give the selected index, this is means user have clicked the item or pressed Enter.
fn confirm(&mut self, ix: Option<usize>, cx: &mut ViewContext<List<Self>>) {}
fn confirm(&mut self, ix: Option<usize>, window: &mut Window, cx: &mut Context<List<Self>>) {}
/// Cancel the selection, e.g.: Pressed ESC.
fn cancel(&mut self, cx: &mut ViewContext<List<Self>>) {}
fn cancel(&mut self, window: &mut Window, cx: &mut Context<List<Self>>) {}
}
pub struct List<D: ListDelegate> {
focus_handle: FocusHandle,
delegate: D,
max_height: Option<Length>,
query_input: Option<View<TextInput>>,
query_input: Option<Entity<TextInput>>,
last_query: Option<String>,
loading: bool,
@@ -97,11 +117,11 @@ impl<D> List<D>
where
D: ListDelegate,
{
pub fn new(delegate: D, cx: &mut ViewContext<Self>) -> Self {
let query_input = cx.new_view(|cx| {
TextInput::new(cx)
pub fn new(delegate: D, window: &mut Window, cx: &mut Context<Self>) -> Self {
let query_input = cx.new(|cx| {
TextInput::new(window, cx)
.appearance(false)
.prefix(|cx| {
.prefix(|_window, cx| {
Icon::new(IconName::Search)
.text_color(cx.theme().base.step(cx, ColorScaleStep::ELEVEN))
})
@@ -109,7 +129,7 @@ where
.cleanable()
});
cx.subscribe(&query_input, Self::on_query_input_event)
cx.subscribe_in(&query_input, window, Self::on_query_input_event)
.detach();
Self {
@@ -130,10 +150,10 @@ where
}
/// Set the size
pub fn set_size(&mut self, size: Size, cx: &mut ViewContext<Self>) {
pub fn set_size(&mut self, size: Size, window: &mut Window, cx: &mut Context<Self>) {
if let Some(input) = &self.query_input {
input.update(cx, |input, cx| {
input.set_size(size, cx);
input.set_size(size, window, cx);
})
}
self.size = size;
@@ -154,8 +174,13 @@ where
self
}
pub fn set_query_input(&mut self, query_input: View<TextInput>, cx: &mut ViewContext<Self>) {
cx.subscribe(&query_input, Self::on_query_input_event)
pub fn set_query_input(
&mut self,
query_input: Entity<TextInput>,
window: &mut Window,
cx: &mut Context<Self>,
) {
cx.subscribe_in(&query_input, window, Self::on_query_input_event)
.detach();
self.query_input = Some(query_input);
}
@@ -168,13 +193,18 @@ where
&mut self.delegate
}
pub fn focus(&mut self, cx: &mut WindowContext) {
self.focus_handle(cx).focus(cx);
pub fn focus(&mut self, window: &mut Window, cx: &mut App) {
self.focus_handle(cx).focus(window);
}
pub fn set_selected_index(&mut self, ix: Option<usize>, cx: &mut ViewContext<Self>) {
pub fn set_selected_index(
&mut self,
ix: Option<usize>,
window: &mut Window,
cx: &mut Context<Self>,
) {
self.selected_index = ix;
self.delegate.set_selected_index(ix, cx);
self.delegate.set_selected_index(ix, window, cx);
}
pub fn selected_index(&self) -> Option<usize> {
@@ -182,31 +212,35 @@ where
}
/// Set the query_input text
pub fn set_query(&mut self, query: &str, cx: &mut ViewContext<Self>) {
pub fn set_query(&mut self, query: &str, window: &mut Window, cx: &mut Context<Self>) {
if let Some(query_input) = &self.query_input {
let query = query.to_owned();
query_input.update(cx, |input, cx| input.set_text(query, cx))
query_input.update(cx, |input, cx| input.set_text(query, window, cx))
}
}
/// Get the query_input text
pub fn query(&self, cx: &mut ViewContext<Self>) -> Option<SharedString> {
pub fn query(&self, _window: &mut Window, cx: &mut Context<Self>) -> Option<SharedString> {
self.query_input.as_ref().map(|input| input.read(cx).text())
}
fn render_scrollbar(&self, cx: &mut ViewContext<Self>) -> Option<impl IntoElement> {
fn render_scrollbar(
&self,
_window: &mut Window,
cx: &mut Context<Self>,
) -> Option<impl IntoElement> {
if !self.enable_scrollbar {
return None;
}
Some(Scrollbar::uniform_scroll(
cx.view().entity_id(),
cx.model().entity_id(),
self.scrollbar_state.clone(),
self.vertical_scroll_handle.clone(),
))
}
fn scroll_to_selected_item(&mut self, _cx: &mut ViewContext<Self>) {
fn scroll_to_selected_item(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {
if let Some(ix) = self.selected_index {
self.vertical_scroll_handle
.scroll_to_item(ix, ScrollStrategy::Top);
@@ -215,9 +249,10 @@ where
fn on_query_input_event(
&mut self,
_: View<TextInput>,
_: &Entity<TextInput>,
event: &InputEvent,
cx: &mut ViewContext<Self>,
window: &mut Window,
cx: &mut Context<Self>,
) {
match event {
InputEvent::Change(text) => {
@@ -226,13 +261,13 @@ where
return;
}
self.set_loading(true, cx);
let search = self.delegate.perform_search(&text, cx);
self.set_loading(true, window, cx);
let search = self.delegate.perform_search(&text, window, cx);
self._search_task = cx.spawn(|this, mut cx| async move {
self._search_task = cx.spawn_in(window, |this, mut window| async move {
search.await;
let _ = this.update(&mut cx, |this, _| {
_ = this.update_in(&mut window, |this, _, _| {
this.vertical_scroll_handle
.scroll_to_item(0, ScrollStrategy::Top);
this.last_query = Some(text);
@@ -240,40 +275,45 @@ where
// Always wait 100ms to avoid flicker
Timer::after(Duration::from_millis(100)).await;
let _ = this.update(&mut cx, |this, cx| {
this.set_loading(false, cx);
_ = this.update_in(&mut window, |this, window, cx| {
this.set_loading(false, window, cx);
});
});
}
InputEvent::PressEnter => self.on_action_confirm(&Confirm, cx),
InputEvent::PressEnter => self.on_action_confirm(&Confirm, window, cx),
_ => {}
}
}
fn set_loading(&mut self, loading: bool, cx: &mut ViewContext<Self>) {
fn set_loading(&mut self, loading: bool, window: &mut Window, cx: &mut Context<Self>) {
self.loading = loading;
if let Some(input) = &self.query_input {
input.update(cx, |input, cx| input.set_loading(loading, cx))
input.update(cx, |input, cx| input.set_loading(loading, window, cx))
}
cx.notify();
}
fn on_action_cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
self.set_selected_index(None, cx);
self.delegate.cancel(cx);
fn on_action_cancel(&mut self, _: &Cancel, window: &mut Window, cx: &mut Context<Self>) {
self.set_selected_index(None, window, cx);
self.delegate.cancel(window, cx);
cx.notify();
}
fn on_action_confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
fn on_action_confirm(&mut self, _: &Confirm, window: &mut Window, cx: &mut Context<Self>) {
if self.delegate.items_count(cx) == 0 {
return;
}
self.delegate.confirm(self.selected_index, cx);
self.delegate.confirm(self.selected_index, window, cx);
cx.notify();
}
fn on_action_select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext<Self>) {
fn on_action_select_prev(
&mut self,
_: &SelectPrev,
window: &mut Window,
cx: &mut Context<Self>,
) {
if self.delegate.items_count(cx) == 0 {
return;
}
@@ -285,12 +325,18 @@ where
self.selected_index = Some(self.delegate.items_count(cx) - 1);
}
self.delegate.set_selected_index(self.selected_index, cx);
self.scroll_to_selected_item(cx);
self.delegate
.set_selected_index(self.selected_index, window, cx);
self.scroll_to_selected_item(window, cx);
cx.notify();
}
fn on_action_select_next(&mut self, _: &SelectNext, cx: &mut ViewContext<Self>) {
fn on_action_select_next(
&mut self,
_: &SelectNext,
window: &mut Window,
cx: &mut Context<Self>,
) {
if self.delegate.items_count(cx) == 0 {
return;
}
@@ -305,17 +351,23 @@ where
self.selected_index = Some(0);
}
self.delegate.set_selected_index(self.selected_index, cx);
self.scroll_to_selected_item(cx);
self.delegate
.set_selected_index(self.selected_index, window, cx);
self.scroll_to_selected_item(window, cx);
cx.notify();
}
fn render_list_item(&mut self, ix: usize, cx: &mut ViewContext<Self>) -> impl IntoElement {
fn render_list_item(
&mut self,
ix: usize,
window: &mut Window,
cx: &mut Context<Self>,
) -> impl IntoElement {
div()
.id("list-item")
.w_full()
.relative()
.children(self.delegate.render_item(ix, cx))
.children(self.delegate.render_item(ix, window, cx))
.when_some(self.selected_index, |this, selected_index| {
this.when(ix == selected_index, |this| {
this.child(
@@ -345,15 +397,15 @@ where
})
.on_mouse_down(
MouseButton::Left,
cx.listener(move |this, _, cx| {
cx.listener(move |this, _, window, cx| {
this.right_clicked_index = None;
this.selected_index = Some(ix);
this.on_action_confirm(&Confirm, cx);
this.on_action_confirm(&Confirm, window, cx);
}),
)
.on_mouse_down(
MouseButton::Right,
cx.listener(move |this, _, cx| {
cx.listener(move |this, _, _window, cx| {
this.right_clicked_index = Some(ix);
cx.notify();
}),
@@ -361,11 +413,11 @@ where
}
}
impl<D> FocusableView for List<D>
impl<D> Focusable for List<D>
where
D: ListDelegate,
{
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
fn focus_handle(&self, cx: &App) -> FocusHandle {
if let Some(query_input) = &self.query_input {
query_input.focus_handle(cx)
} else {
@@ -378,8 +430,8 @@ impl<D> Render for List<D>
where
D: ListDelegate,
{
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
let view = cx.view().clone();
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let view = cx.model().clone();
let vertical_scroll_handle = self.vertical_scroll_handle.clone();
let items_count = self.delegate.items_count(cx);
let sizing_behavior = if self.max_height.is_some() {
@@ -390,7 +442,7 @@ where
let initial_view = if let Some(input) = &self.query_input {
if input.read(cx).text().is_empty() {
self.delegate().render_initial(cx)
self.delegate().render_initial(window, cx)
} else {
None
}
@@ -432,14 +484,14 @@ where
.when_some(self.max_height, |this, h| this.max_h(h))
.overflow_hidden()
.when(items_count == 0, |this| {
this.child(self.delegate().render_empty(cx))
this.child(self.delegate().render_empty(window, cx))
})
.when(items_count > 0, |this| {
this.child(
uniform_list(view, "uniform-list", items_count, {
move |list, visible_range, cx| {
move |list, visible_range, window, cx| {
visible_range
.map(|ix| list.render_list_item(ix, cx))
.map(|ix| list.render_list_item(ix, window, cx))
.collect::<Vec<_>>()
}
})
@@ -449,13 +501,13 @@ where
.into_any_element(),
)
})
.children(self.render_scrollbar(cx)),
.children(self.render_scrollbar(window, cx)),
)
}
})
// Click out to cancel right clicked row
.when(self.right_clicked_index.is_some(), |this| {
this.on_mouse_down_out(cx.listener(|this, _, cx| {
this.on_mouse_down_out(cx.listener(|this, _, _window, cx| {
this.right_clicked_index = None;
cx.notify();
}))

View File

@@ -4,15 +4,15 @@ use crate::{
Disableable, Icon, IconName, Selectable, Sizable as _,
};
use gpui::{
div, prelude::FluentBuilder as _, AnyElement, ClickEvent, Div, ElementId, InteractiveElement,
IntoElement, MouseButton, MouseMoveEvent, ParentElement, RenderOnce, Stateful,
StatefulInteractiveElement as _, Styled, WindowContext,
div, prelude::FluentBuilder as _, AnyElement, App, ClickEvent, Div, ElementId,
InteractiveElement, IntoElement, MouseButton, MouseMoveEvent, ParentElement, RenderOnce,
Stateful, StatefulInteractiveElement as _, Styled, Window,
};
use smallvec::SmallVec;
type OnClick = Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>;
type OnMouseEnter = Option<Box<dyn Fn(&MouseMoveEvent, &mut WindowContext) + 'static>>;
type Suffix = Option<Box<dyn Fn(&mut WindowContext) -> AnyElement + 'static>>;
type OnClick = Option<Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>>;
type OnMouseEnter = Option<Box<dyn Fn(&MouseMoveEvent, &mut Window, &mut App) + 'static>>;
type Suffix = Option<Box<dyn Fn(&mut Window, &mut App) -> AnyElement + 'static>>;
#[derive(IntoElement)]
pub struct ListItem {
@@ -71,21 +71,26 @@ impl ListItem {
/// Set the suffix element of the input field, for example a clear button.
pub fn suffix<F, E>(mut self, builder: F) -> Self
where
F: Fn(&mut WindowContext) -> E + 'static,
F: Fn(&mut Window, &mut App) -> E + 'static,
E: IntoElement,
{
self.suffix = Some(Box::new(move |cx| builder(cx).into_any_element()));
self.suffix = Some(Box::new(move |window, cx| {
builder(window, cx).into_any_element()
}));
self
}
pub fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self {
pub fn on_click(
mut self,
handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
) -> Self {
self.on_click = Some(Box::new(handler));
self
}
pub fn on_mouse_enter(
mut self,
handler: impl Fn(&MouseMoveEvent, &mut WindowContext) + 'static,
handler: impl Fn(&MouseMoveEvent, &mut Window, &mut App) + 'static,
) -> Self {
self.on_mouse_enter = Some(Box::new(handler));
self
@@ -123,7 +128,7 @@ impl ParentElement for ListItem {
}
impl RenderOnce for ListItem {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let is_active = self.selected || self.confirmed;
self.base
@@ -134,7 +139,7 @@ impl RenderOnce for ListItem {
.when_some(self.on_click, |this, on_click| {
if !self.disabled {
this.cursor_pointer()
.on_mouse_down(MouseButton::Left, move |_, cx| {
.on_mouse_down(MouseButton::Left, move |_, _window, cx| {
cx.stop_propagation();
})
.on_click(on_click)
@@ -151,7 +156,7 @@ impl RenderOnce for ListItem {
// Mouse enter
.when_some(self.on_mouse_enter, |this, on_mouse_enter| {
if !self.disabled {
this.on_mouse_move(move |ev, cx| (on_mouse_enter)(ev, cx))
this.on_mouse_move(move |ev, window, cx| (on_mouse_enter)(ev, window, cx))
} else {
this
}
@@ -176,6 +181,6 @@ impl RenderOnce for ListItem {
))
}),
)
.when_some(self.suffix, |this, suffix| this.child(suffix(cx)))
.when_some(self.suffix, |this, suffix| this.child(suffix(window, cx)))
}
}