apply implemention for mouse events

This commit is contained in:
temportalflux
2026-07-11 09:31:07 -04:00
parent 56234ed27e
commit 84af172e35
5 changed files with 25 additions and 22 deletions
-10
View File
@@ -13,13 +13,3 @@ pub use shared_state::*;
pub use storage::*;
pub use text_area_element::*;
pub use text_area_state::*;
#[allow(dead_code)]
fn make_input(_app: &mut gpui::App) -> impl gpui::IntoElement {
input(0)
}
#[allow(dead_code)]
fn make_textarea(_app: &mut gpui::App) -> impl gpui::IntoElement {
text_area(0)
}
@@ -270,6 +270,7 @@ pub(super) trait EditableInputActionElement: super::StateBackedElement {
state.insert_tab(action, window, cx)
});
self.register_action(init_props.clone(), |state, action, window, cx| {
println!("backspace");
state.backspace(action, window, cx)
});
self.register_action(init_props.clone(), |state, action, window, cx| {
@@ -14,9 +14,8 @@ use smallvec::SmallVec;
use std::{ops::Range, sync::Arc};
#[track_caller]
pub fn input(id: impl Into<ElementId>) -> TextInputElement {
pub fn input() -> TextInputElement {
let mut this = TextInputElement {
id: id.into(),
placeholder: None,
interactivity: Interactivity::new(),
init_storage: InitStorage::default(),
@@ -28,7 +27,6 @@ pub fn input(id: impl Into<ElementId>) -> TextInputElement {
// TODO: Disabled flag/state?
pub struct TextInputElement {
id: ElementId,
placeholder: Option<SharedString>,
interactivity: Interactivity,
init_storage: InitStorage,
@@ -56,10 +54,11 @@ impl IntoElement for TextInputElement {
impl EditableInputActionElement for TextInputElement {}
impl super::StateBackedElement for TextInputElement {
type State = TextInputState;
type InitProps = (ElementId, InitStorage);
type InitProps = (Option<ElementId>, InitStorage);
fn init_props(&self) -> Self::InitProps {
(self.id.clone(), self.init_storage.clone())
let element_id = self.interactivity.element_id.clone();
(element_id, self.init_storage.clone())
}
fn get_or_init_state(
@@ -69,7 +68,12 @@ impl super::StateBackedElement for TextInputElement {
) -> Entity<TextInputState> {
// Get the state from the app using the element's id as the key.
// If it doesnt exist, initialize a new state with the user's desired storage medium.
window.use_keyed_state(init_props.0.clone(), cx, |_window, cx| {
// TODO: how to best garuntee an element id?
let element_id = init_props
.0
.clone()
.expect("user MUST assign an element id");
window.use_keyed_state(element_id, cx, |_window, cx| {
TextInputState::new(init_props.1.exec(cx), cx)
})
}
@@ -330,7 +330,9 @@ impl<'app> EditableTextActionHandler<'app> for TextInputState {
window: &mut Window,
cx: &mut Self::Context,
) {
let character_pos = self.internal.caret_pos(); // TODO: Should be index_for_pixel_point
let character_pos = self
.internal
.index_for_pixel_point(text_position, window.line_height());
self.internal.on_mouse_down(
text_position,
character_pos,
@@ -354,10 +356,12 @@ impl<'app> EditableTextActionHandler<'app> for TextInputState {
&mut self,
_event: &gpui::MouseMoveEvent,
text_position: Point<Pixels>,
_w: &mut Window,
window: &mut Window,
cx: &mut Self::Context,
) {
let character_pos = self.internal.caret_pos(); // TODO: Should be index_for_pixel_point
let character_pos = self
.internal
.index_for_pixel_point(text_position, window.line_height());
self.internal.on_mouse_move(character_pos, cx);
}
}
@@ -321,7 +321,9 @@ impl<'app> EditableTextActionHandler<'app> for TextAreaState {
window: &mut Window,
cx: &mut Self::Context,
) {
let character_pos = self.internal.caret_pos(); // TODO: Should be index_for_pixel_point
let character_pos = self
.internal
.index_for_pixel_point(text_position, window.line_height());
self.internal.on_mouse_down(
text_position,
character_pos,
@@ -345,10 +347,12 @@ impl<'app> EditableTextActionHandler<'app> for TextAreaState {
&mut self,
_event: &gpui::MouseMoveEvent,
text_position: Point<Pixels>,
_w: &mut Window,
window: &mut Window,
cx: &mut Self::Context,
) {
let character_pos = self.internal.caret_pos(); // TODO: Should be index_for_pixel_point
let character_pos = self
.internal
.index_for_pixel_point(text_position, window.line_height());
self.internal.on_mouse_move(character_pos, cx);
}
}