1use gpui::{actions, div, px, red, AppContext, Div, Render, Styled, ViewContext, VisualContext};
2use workspace::ModalRegistry;
3
4actions!(Toggle);
5
6pub fn init(cx: &mut AppContext) {
7 cx.global_mut::<ModalRegistry>()
8 .register_modal(Toggle, |_, cx| {
9 // if let Some(editor) = workspace
10 // .active_item(cx)
11 // .and_then(|active_item| active_item.downcast::<Editor>())
12 // {
13 // cx.build_view(|cx| GoToLine::new(editor, cx))
14 // }
15 let view = cx.build_view(|_| GoToLine);
16 view
17 });
18
19 // cx.add_action(GoToLine::toggle);
20 // cx.add_action(GoToLine::confirm);
21 // cx.add_action(GoToLine::cancel);
22}
23
24pub struct GoToLine;
25
26impl Render for GoToLine {
27 type Element = Div<Self>;
28
29 fn render(&mut self, _cx: &mut ViewContext<Self>) -> Self::Element {
30 dbg!("rendering GoToLine");
31 div().bg(red()).w(px(100.0)).h(px(100.0))
32 }
33}
34
35// pub struct GoToLine {
36// //line_editor: View<Editor>,
37// active_editor: View<Editor>,
38// prev_scroll_position: Option<gpui::Point<Pixels>>,
39// cursor_point: Point,
40// max_point: Point,
41// has_focus: bool,
42// }
43
44// pub enum Event {
45// Dismissed,
46// }
47
48// impl GoToLine {
49// pub fn new(active_editor: View<Editor>, cx: &mut ViewContext<Self>) -> Self {
50// // let line_editor = cx.build_view(|cx| {
51// // Editor::single_line(
52// // Some(Arc::new(|theme| theme.picker.input_editor.clone())),
53// // cx,
54// // )
55// // });
56// // cx.subscribe(&line_editor, Self::on_line_editor_event)
57// // .detach();
58
59// let (scroll_position, cursor_point, max_point) = active_editor.update(cx, |editor, cx| {
60// let scroll_position = editor.scroll_position(cx);
61// let buffer = editor.buffer().read(cx).snapshot(cx);
62// (
63// Some(scroll_position),
64// editor.selections.newest(cx).head(),
65// buffer.max_point(),
66// )
67// });
68
69// cx.on_release(|_, on_release| {}).detach();
70
71// Self {
72// //line_editor,
73// active_editor,
74// prev_scroll_position: scroll_position,
75// cursor_point,
76// max_point,
77// has_focus: false,
78// }
79// }
80
81// fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
82// cx.emit(Event::Dismissed);
83// }
84
85// fn confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
86// self.prev_scroll_position.take();
87// if let Some(point) = self.point_from_query(cx) {
88// self.active_editor.update(cx, |active_editor, cx| {
89// let snapshot = active_editor.snapshot(cx).display_snapshot;
90// let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
91// active_editor.change_selections(Some(Autoscroll::center()), cx, |s| {
92// s.select_ranges([point..point])
93// });
94// });
95// }
96
97// cx.emit(Event::Dismissed);
98// }
99
100// fn on_line_editor_event(
101// &mut self,
102// _: View<Editor>,
103// event: &editor::Event,
104// cx: &mut ViewContext<Self>,
105// ) {
106// match event {
107// editor::Event::Blurred => cx.emit(Event::Dismissed),
108// editor::Event::BufferEdited { .. } => {
109// if let Some(point) = self.point_from_query(cx) {
110// // todo!()
111// // self.active_editor.update(cx, |active_editor, cx| {
112// // let snapshot = active_editor.snapshot(cx).display_snapshot;
113// // let point = snapshot.buffer_snapshot.clip_point(point, Bias::Left);
114// // let display_point = point.to_display_point(&snapshot);
115// // let row = display_point.row();
116// // active_editor.highlight_rows(Some(row..row + 1));
117// // active_editor.request_autoscroll(Autoscroll::center(), cx);
118// // });
119// cx.notify();
120// }
121// }
122// _ => {}
123// }
124// }
125
126// fn point_from_query(&self, cx: &ViewContext<Self>) -> Option<Point> {
127// return None;
128// // todo!()
129// // let line_editor = self.line_editor.read(cx).text(cx);
130// // let mut components = line_editor
131// // .splitn(2, FILE_ROW_COLUMN_DELIMITER)
132// // .map(str::trim)
133// // .fuse();
134// // let row = components.next().and_then(|row| row.parse::<u32>().ok())?;
135// // let column = components.next().and_then(|col| col.parse::<u32>().ok());
136// // Some(Point::new(
137// // row.saturating_sub(1),
138// // column.unwrap_or(0).saturating_sub(1),
139// // ))
140// }
141// }
142
143// impl EventEmitter for GoToLine {
144// type Event = Event;
145// }
146
147// impl Entity for GoToLine {
148// fn release(&mut self, cx: &mut AppContext) {
149// let scroll_position = self.prev_scroll_position.take();
150// self.active_editor.window().update(cx, |cx| {
151// self.active_editor.update(cx, |editor, cx| {
152// editor.highlight_rows(None);
153// if let Some(scroll_position) = scroll_position {
154// editor.set_scroll_position(scroll_position, cx);
155// }
156// })
157// });
158// }
159// }
160
161// impl Render for GoToLine {
162// type Element = Div<Self>;
163
164// fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
165// // todo!()
166// div()
167// }
168// }
169
170// impl View for GoToLine {
171// fn ui_name() -> &'static str {
172// "GoToLine"
173// }
174
175// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
176// let theme = &theme::current(cx).picker;
177
178// let label = format!(
179// "{}{FILE_ROW_COLUMN_DELIMITER}{} of {} lines",
180// self.cursor_point.row + 1,
181// self.cursor_point.column + 1,
182// self.max_point.row + 1
183// );
184
185// Flex::new(Axis::Vertical)
186// .with_child(
187// ChildView::new(&self.line_editor, cx)
188// .contained()
189// .with_style(theme.input_editor.container),
190// )
191// .with_child(
192// Label::new(label, theme.no_matches.label.clone())
193// .contained()
194// .with_style(theme.no_matches.container),
195// )
196// .contained()
197// .with_style(theme.container)
198// .constrained()
199// .with_max_width(500.0)
200// .into_any_named("go to line")
201// }
202
203// fn focus_in(&mut self, _: AnyView, cx: &mut ViewContext<Self>) {
204// self.has_focus = true;
205// cx.focus(&self.line_editor);
206// }
207
208// fn focus_out(&mut self, _: AnyView, _: &mut ViewContext<Self>) {
209// self.has_focus = false;
210// }
211// }
212
213// impl Modal for GoToLine {
214// fn has_focus(&self) -> bool {
215// self.has_focus
216// }
217
218// fn dismiss_on_event(event: &Self::Event) -> bool {
219// matches!(event, Event::Dismissed)
220// }
221// }