Get basic text input working

Co-authored-by: Marshall <marshall@zed.dev>
This commit is contained in:
Max Brunsfeld 2023-11-07 16:33:02 -08:00
parent bd12e3edb6
commit 9fe3073af7
3 changed files with 141 additions and 135 deletions

View file

@ -8135,15 +8135,14 @@ impl Editor {
} }
fn start_transaction_at(&mut self, now: Instant, cx: &mut ViewContext<Self>) { fn start_transaction_at(&mut self, now: Instant, cx: &mut ViewContext<Self>) {
todo!() self.end_selection(cx);
// self.end_selection(cx); if let Some(tx_id) = self
// if let Some(tx_id) = self .buffer
// .buffer .update(cx, |buffer, cx| buffer.start_transaction_at(now, cx))
// .update(cx, |buffer, cx| buffer.start_transaction_at(now, cx)) {
// { self.selection_history
// self.selection_history .insert_transaction(tx_id, self.selections.disjoint_anchors());
// .insert_transaction(tx_id, self.selections.disjoint_anchors()); }
// }
} }
fn end_transaction_at( fn end_transaction_at(
@ -8151,22 +8150,21 @@ impl Editor {
now: Instant, now: Instant,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<TransactionId> { ) -> Option<TransactionId> {
todo!() if let Some(tx_id) = self
// if let Some(tx_id) = self .buffer
// .buffer .update(cx, |buffer, cx| buffer.end_transaction_at(now, cx))
// .update(cx, |buffer, cx| buffer.end_transaction_at(now, cx)) {
// { if let Some((_, end_selections)) = self.selection_history.transaction_mut(tx_id) {
// if let Some((_, end_selections)) = self.selection_history.transaction_mut(tx_id) { *end_selections = Some(self.selections.disjoint_anchors());
// *end_selections = Some(self.selections.disjoint_anchors()); } else {
// } else { log::error!("unexpectedly ended a transaction that wasn't started by this editor");
// error!("unexpectedly ended a transaction that wasn't started by this editor"); }
// }
// cx.emit(Event::Edited); cx.emit(Event::Edited);
// Some(tx_id) Some(tx_id)
// } else { } else {
// None None
// } }
} }
// pub fn fold(&mut self, _: &Fold, cx: &mut ViewContext<Self>) { // pub fn fold(&mut self, _: &Fold, cx: &mut ViewContext<Self>) {
@ -8689,17 +8687,17 @@ impl Editor {
// results // results
// } // }
// pub fn highlight_text<T: 'static>( pub fn highlight_text<T: 'static>(
// &mut self, &mut self,
// ranges: Vec<Range<Anchor>>, ranges: Vec<Range<Anchor>>,
// style: HighlightStyle, style: HighlightStyle,
// cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
// ) { ) {
// self.display_map.update(cx, |map, _| { self.display_map.update(cx, |map, _| {
// map.highlight_text(TypeId::of::<T>(), ranges, style) map.highlight_text(TypeId::of::<T>(), ranges, style)
// }); });
// cx.notify(); cx.notify();
// } }
// pub fn highlight_inlays<T: 'static>( // pub fn highlight_inlays<T: 'static>(
// &mut self, // &mut self,
@ -9591,113 +9589,112 @@ impl InputHandler for Editor {
new_selected_range_utf16: Option<Range<usize>>, new_selected_range_utf16: Option<Range<usize>>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) { ) {
// if !self.input_enabled { if !self.input_enabled {
// cx.emit(Event::InputIgnored { text: text.into() }); cx.emit(Event::InputIgnored { text: text.into() });
// return; return;
// } }
// let transaction = self.transact(cx, |this, cx| { let transaction = self.transact(cx, |this, cx| {
// let ranges_to_replace = if let Some(mut marked_ranges) = this.marked_text_ranges(cx) { let ranges_to_replace = if let Some(mut marked_ranges) = this.marked_text_ranges(cx) {
// let snapshot = this.buffer.read(cx).read(cx); let snapshot = this.buffer.read(cx).read(cx);
// if let Some(relative_range_utf16) = range_utf16.as_ref() { if let Some(relative_range_utf16) = range_utf16.as_ref() {
// for marked_range in &mut marked_ranges { for marked_range in &mut marked_ranges {
// marked_range.end.0 = marked_range.start.0 + relative_range_utf16.end; marked_range.end.0 = marked_range.start.0 + relative_range_utf16.end;
// marked_range.start.0 += relative_range_utf16.start; marked_range.start.0 += relative_range_utf16.start;
// marked_range.start = marked_range.start =
// snapshot.clip_offset_utf16(marked_range.start, Bias::Left); snapshot.clip_offset_utf16(marked_range.start, Bias::Left);
// marked_range.end = marked_range.end =
// snapshot.clip_offset_utf16(marked_range.end, Bias::Right); snapshot.clip_offset_utf16(marked_range.end, Bias::Right);
// } }
// } }
// Some(marked_ranges) Some(marked_ranges)
// } else if let Some(range_utf16) = range_utf16 { } else if let Some(range_utf16) = range_utf16 {
// let range_utf16 = OffsetUtf16(range_utf16.start)..OffsetUtf16(range_utf16.end); let range_utf16 = OffsetUtf16(range_utf16.start)..OffsetUtf16(range_utf16.end);
// Some(this.selection_replacement_ranges(range_utf16, cx)) Some(this.selection_replacement_ranges(range_utf16, cx))
// } else { } else {
// None None
// }; };
// let range_to_replace = ranges_to_replace.as_ref().and_then(|ranges_to_replace| { let range_to_replace = ranges_to_replace.as_ref().and_then(|ranges_to_replace| {
// let newest_selection_id = this.selections.newest_anchor().id; let newest_selection_id = this.selections.newest_anchor().id;
// this.selections this.selections
// .all::<OffsetUtf16>(cx) .all::<OffsetUtf16>(cx)
// .iter() .iter()
// .zip(ranges_to_replace.iter()) .zip(ranges_to_replace.iter())
// .find_map(|(selection, range)| { .find_map(|(selection, range)| {
// if selection.id == newest_selection_id { if selection.id == newest_selection_id {
// Some( Some(
// (range.start.0 as isize - selection.head().0 as isize) (range.start.0 as isize - selection.head().0 as isize)
// ..(range.end.0 as isize - selection.head().0 as isize), ..(range.end.0 as isize - selection.head().0 as isize),
// ) )
// } else { } else {
// None None
// } }
// }) })
// }); });
// cx.emit(Event::InputHandled { cx.emit(Event::InputHandled {
// utf16_range_to_replace: range_to_replace, utf16_range_to_replace: range_to_replace,
// text: text.into(), text: text.into(),
// }); });
// if let Some(ranges) = ranges_to_replace { if let Some(ranges) = ranges_to_replace {
// this.change_selections(None, cx, |s| s.select_ranges(ranges)); this.change_selections(None, cx, |s| s.select_ranges(ranges));
// } }
// let marked_ranges = { let marked_ranges = {
// let snapshot = this.buffer.read(cx).read(cx); let snapshot = this.buffer.read(cx).read(cx);
// this.selections this.selections
// .disjoint_anchors() .disjoint_anchors()
// .iter() .iter()
// .map(|selection| { .map(|selection| {
// selection.start.bias_left(&*snapshot)..selection.end.bias_right(&*snapshot) selection.start.bias_left(&*snapshot)..selection.end.bias_right(&*snapshot)
// }) })
// .collect::<Vec<_>>() .collect::<Vec<_>>()
// }; };
// if text.is_empty() { if text.is_empty() {
// this.unmark_text(cx); this.unmark_text(cx);
// } else { } else {
// this.highlight_text::<InputComposition>( this.highlight_text::<InputComposition>(
// marked_ranges.clone(), marked_ranges.clone(),
// this.style(cx).composition_mark, HighlightStyle::default(), // todo!() this.style(cx).composition_mark,
// cx, cx,
// ); );
// } }
// this.handle_input(text, cx); this.handle_input(text, cx);
// if let Some(new_selected_range) = new_selected_range_utf16 { if let Some(new_selected_range) = new_selected_range_utf16 {
// let snapshot = this.buffer.read(cx).read(cx); let snapshot = this.buffer.read(cx).read(cx);
// let new_selected_ranges = marked_ranges let new_selected_ranges = marked_ranges
// .into_iter() .into_iter()
// .map(|marked_range| { .map(|marked_range| {
// let insertion_start = marked_range.start.to_offset_utf16(&snapshot).0; let insertion_start = marked_range.start.to_offset_utf16(&snapshot).0;
// let new_start = OffsetUtf16(new_selected_range.start + insertion_start); let new_start = OffsetUtf16(new_selected_range.start + insertion_start);
// let new_end = OffsetUtf16(new_selected_range.end + insertion_start); let new_end = OffsetUtf16(new_selected_range.end + insertion_start);
// snapshot.clip_offset_utf16(new_start, Bias::Left) snapshot.clip_offset_utf16(new_start, Bias::Left)
// ..snapshot.clip_offset_utf16(new_end, Bias::Right) ..snapshot.clip_offset_utf16(new_end, Bias::Right)
// }) })
// .collect::<Vec<_>>(); .collect::<Vec<_>>();
// drop(snapshot); drop(snapshot);
// this.change_selections(None, cx, |selections| { this.change_selections(None, cx, |selections| {
// selections.select_ranges(new_selected_ranges) selections.select_ranges(new_selected_ranges)
// }); });
// } }
// }); });
// self.ime_transaction = self.ime_transaction.or(transaction); self.ime_transaction = self.ime_transaction.or(transaction);
// if let Some(transaction) = self.ime_transaction { if let Some(transaction) = self.ime_transaction {
// self.buffer.update(cx, |buffer, cx| { self.buffer.update(cx, |buffer, cx| {
// buffer.group_until_transaction(transaction, cx); buffer.group_until_transaction(transaction, cx);
// }); });
// } }
// if self.text_highlights::<InputComposition>(cx).is_none() { if self.text_highlights::<InputComposition>(cx).is_none() {
// self.ime_transaction.take(); self.ime_transaction.take();
// } }
todo!()
} }
fn bounds_for_range( fn bounds_for_range(
@ -9705,7 +9702,9 @@ impl InputHandler for Editor {
range_utf16: Range<usize>, range_utf16: Range<usize>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Option<gpui::Bounds<f32>> { ) -> Option<gpui::Bounds<f32>> {
todo!() // todo!()
// See how we did it before: `rect_for_range`
None
} }
} }

View file

@ -255,7 +255,7 @@ impl Window {
handle handle
.update(&mut cx, |_, cx| cx.dispatch_event(event)) .update(&mut cx, |_, cx| cx.dispatch_event(event))
.log_err() .log_err()
.unwrap_or(true) .unwrap_or(false)
}) })
}); });
@ -1011,6 +1011,9 @@ impl<'a> WindowContext<'a> {
.take() .take()
.unwrap_or(CursorStyle::Arrow); .unwrap_or(CursorStyle::Arrow);
self.platform.set_cursor_style(cursor_style); self.platform.set_cursor_style(cursor_style);
if let Some(handler) = self.window.requested_input_handler.take() {
self.window.platform_window.set_input_handler(handler);
}
self.window.dirty = false; self.window.dirty = false;
} }
@ -1155,6 +1158,7 @@ impl<'a> WindowContext<'a> {
.insert(any_mouse_event.type_id(), handlers); .insert(any_mouse_event.type_id(), handlers);
} }
} else if let Some(any_key_event) = event.keyboard_event() { } else if let Some(any_key_event) = event.keyboard_event() {
let mut did_handle_action = false;
let key_dispatch_stack = mem::take(&mut self.window.key_dispatch_stack); let key_dispatch_stack = mem::take(&mut self.window.key_dispatch_stack);
let key_event_type = any_key_event.type_id(); let key_event_type = any_key_event.type_id();
let mut context_stack = SmallVec::<[&DispatchContext; 16]>::new(); let mut context_stack = SmallVec::<[&DispatchContext; 16]>::new();
@ -1175,6 +1179,7 @@ impl<'a> WindowContext<'a> {
self.dispatch_action(action, &key_dispatch_stack[..ix]); self.dispatch_action(action, &key_dispatch_stack[..ix]);
} }
if !self.app.propagate_event { if !self.app.propagate_event {
did_handle_action = true;
break; break;
} }
} }
@ -1203,6 +1208,7 @@ impl<'a> WindowContext<'a> {
} }
if !self.app.propagate_event { if !self.app.propagate_event {
did_handle_action = true;
break; break;
} }
} }
@ -1216,6 +1222,7 @@ impl<'a> WindowContext<'a> {
drop(context_stack); drop(context_stack);
self.window.key_dispatch_stack = key_dispatch_stack; self.window.key_dispatch_stack = key_dispatch_stack;
return did_handle_action;
} }
true true
@ -2007,7 +2014,7 @@ where
cx: self.app.this.clone(), cx: self.app.this.clone(),
window: self.window_handle(), window: self.window_handle(),
handler: self.view().downgrade(), handler: self.view().downgrade(),
})) }));
} }
} }

View file

@ -2694,7 +2694,7 @@ impl Workspace {
.any(|item| item.has_conflict(cx) || item.is_dirty(cx)); .any(|item| item.has_conflict(cx) || item.is_dirty(cx));
if is_edited != self.window_edited { if is_edited != self.window_edited {
self.window_edited = is_edited; self.window_edited = is_edited;
todo!() // todo!()
// cx.set_window_edited(self.window_edited) // cx.set_window_edited(self.window_edited)
} }
} }