Add mouse handling to gpui input example (#14350)
Release Notes: - N/A --------- Co-authored-by: Jason Lee <huacnlee@gmail.com>
This commit is contained in:
co-authored by
Jason Lee
parent
b58abb171f
commit
1856320516
@@ -22,6 +22,7 @@ actions!(
|
|||||||
struct TextInput {
|
struct TextInput {
|
||||||
focus_handle: FocusHandle,
|
focus_handle: FocusHandle,
|
||||||
content: SharedString,
|
content: SharedString,
|
||||||
|
placeholder: SharedString,
|
||||||
selected_range: Range<usize>,
|
selected_range: Range<usize>,
|
||||||
selection_reversed: bool,
|
selection_reversed: bool,
|
||||||
marked_range: Option<Range<usize>>,
|
marked_range: Option<Range<usize>>,
|
||||||
@@ -84,7 +85,12 @@ impl TextInput {
|
|||||||
|
|
||||||
fn on_mouse_down(&mut self, event: &MouseDownEvent, cx: &mut ViewContext<Self>) {
|
fn on_mouse_down(&mut self, event: &MouseDownEvent, cx: &mut ViewContext<Self>) {
|
||||||
self.is_selecting = true;
|
self.is_selecting = true;
|
||||||
self.move_to(self.index_for_mouse_position(event.position), cx)
|
|
||||||
|
if event.modifiers.shift {
|
||||||
|
self.select_to(self.index_for_mouse_position(event.position), cx);
|
||||||
|
} else {
|
||||||
|
self.move_to(self.index_for_mouse_position(event.position), cx)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_mouse_up(&mut self, _: &MouseUpEvent, _: &mut ViewContext<Self>) {
|
fn on_mouse_up(&mut self, _: &MouseUpEvent, _: &mut ViewContext<Self>) {
|
||||||
@@ -115,6 +121,10 @@ impl TextInput {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn index_for_mouse_position(&self, position: Point<Pixels>) -> usize {
|
fn index_for_mouse_position(&self, position: Point<Pixels>) -> usize {
|
||||||
|
if self.content.is_empty() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
let (Some(bounds), Some(line)) = (self.last_bounds.as_ref(), self.last_layout.as_ref())
|
let (Some(bounds), Some(line)) = (self.last_bounds.as_ref(), self.last_layout.as_ref())
|
||||||
else {
|
else {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -348,10 +358,17 @@ impl Element for TextElement {
|
|||||||
let selected_range = input.selected_range.clone();
|
let selected_range = input.selected_range.clone();
|
||||||
let cursor = input.cursor_offset();
|
let cursor = input.cursor_offset();
|
||||||
let style = cx.text_style();
|
let style = cx.text_style();
|
||||||
|
|
||||||
|
let (display_text, text_color) = if content.is_empty() {
|
||||||
|
(input.placeholder.clone(), hsla(0., 0., 0., 0.2))
|
||||||
|
} else {
|
||||||
|
(content.clone(), style.color)
|
||||||
|
};
|
||||||
|
|
||||||
let run = TextRun {
|
let run = TextRun {
|
||||||
len: input.content.len(),
|
len: display_text.len(),
|
||||||
font: style.font(),
|
font: style.font(),
|
||||||
color: style.color,
|
color: text_color,
|
||||||
background_color: None,
|
background_color: None,
|
||||||
underline: None,
|
underline: None,
|
||||||
strikethrough: None,
|
strikethrough: None,
|
||||||
@@ -372,7 +389,7 @@ impl Element for TextElement {
|
|||||||
..run.clone()
|
..run.clone()
|
||||||
},
|
},
|
||||||
TextRun {
|
TextRun {
|
||||||
len: input.content.len() - marked_range.end,
|
len: display_text.len() - marked_range.end,
|
||||||
..run.clone()
|
..run.clone()
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@@ -386,7 +403,7 @@ impl Element for TextElement {
|
|||||||
let font_size = style.font_size.to_pixels(cx.rem_size());
|
let font_size = style.font_size.to_pixels(cx.rem_size());
|
||||||
let line = cx
|
let line = cx
|
||||||
.text_system()
|
.text_system()
|
||||||
.shape_line(content, font_size, &runs)
|
.shape_line(display_text, font_size, &runs)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let cursor_pos = line.x_for_index(cursor);
|
let cursor_pos = line.x_for_index(cursor);
|
||||||
@@ -585,6 +602,7 @@ fn main() {
|
|||||||
let text_input = cx.new_view(|cx| TextInput {
|
let text_input = cx.new_view(|cx| TextInput {
|
||||||
focus_handle: cx.focus_handle(),
|
focus_handle: cx.focus_handle(),
|
||||||
content: "".into(),
|
content: "".into(),
|
||||||
|
placeholder: "Type here...".into(),
|
||||||
selected_range: 0..0,
|
selected_range: 0..0,
|
||||||
selection_reversed: false,
|
selection_reversed: false,
|
||||||
marked_range: None,
|
marked_range: None,
|
||||||
|
|||||||
Reference in New Issue
Block a user