1use std::{
2 cell::{LazyCell, RefCell, RefMut},
3 fmt::Write,
4 ops::RangeInclusive,
5 rc::Rc,
6 sync::{Arc, LazyLock},
7 time::Duration,
8};
9
10use editor::{Editor, EditorElement, EditorStyle};
11use gpui::{
12 Action, Along, AppContext, Axis, DismissEvent, DragMoveEvent, Empty, Entity, FocusHandle,
13 Focusable, ListHorizontalSizingBehavior, MouseButton, Point, ScrollStrategy, ScrollWheelEvent,
14 Subscription, Task, TextStyle, UniformList, UniformListScrollHandle, WeakEntity, actions,
15 anchored, deferred, uniform_list,
16};
17use notifications::status_toast::{StatusToast, ToastIcon};
18use project::debugger::{MemoryCell, dap_command::DataBreakpointContext, session::Session};
19use settings::Settings;
20use theme::ThemeSettings;
21use ui::{
22 ContextMenu, Divider, DropdownMenu, FluentBuilder, IntoElement, PopoverMenuHandle, Render,
23 ScrollableHandle, StatefulInteractiveElement, Tooltip, WithScrollbar, prelude::*,
24};
25use workspace::Workspace;
26
27use crate::{ToggleDataBreakpoint, session::running::stack_frame_list::StackFrameList};
28
29actions!(debugger, [GoToSelectedAddress]);
30
31pub(crate) struct MemoryView {
32 workspace: WeakEntity<Workspace>,
33 stack_frame_list: WeakEntity<StackFrameList>,
34 focus_handle: FocusHandle,
35 view_state_handle: ViewStateHandle,
36 query_editor: Entity<Editor>,
37 session: Entity<Session>,
38 width_picker_handle: PopoverMenuHandle<ContextMenu>,
39 is_writing_memory: bool,
40 open_context_menu: Option<(Entity<ContextMenu>, Point<Pixels>, Subscription)>,
41}
42
43impl Focusable for MemoryView {
44 fn focus_handle(&self, _: &ui::App) -> FocusHandle {
45 self.focus_handle.clone()
46 }
47}
48#[derive(Clone, Debug)]
49struct Drag {
50 start_address: u64,
51 end_address: u64,
52}
53
54impl Drag {
55 fn contains(&self, address: u64) -> bool {
56 let range = self.memory_range();
57 range.contains(&address)
58 }
59
60 fn memory_range(&self) -> RangeInclusive<u64> {
61 if self.start_address < self.end_address {
62 self.start_address..=self.end_address
63 } else {
64 self.end_address..=self.start_address
65 }
66 }
67}
68#[derive(Clone, Debug)]
69enum SelectedMemoryRange {
70 DragUnderway(Drag),
71 DragComplete(Drag),
72}
73
74impl SelectedMemoryRange {
75 fn contains(&self, address: u64) -> bool {
76 match self {
77 SelectedMemoryRange::DragUnderway(drag) => drag.contains(address),
78 SelectedMemoryRange::DragComplete(drag) => drag.contains(address),
79 }
80 }
81 fn is_dragging(&self) -> bool {
82 matches!(self, SelectedMemoryRange::DragUnderway(_))
83 }
84 fn drag(&self) -> &Drag {
85 match self {
86 SelectedMemoryRange::DragUnderway(drag) => drag,
87 SelectedMemoryRange::DragComplete(drag) => drag,
88 }
89 }
90}
91
92#[derive(Clone)]
93struct ViewStateHandle(Rc<RefCell<ViewState>>);
94
95impl ViewStateHandle {
96 fn new(base_row: u64, line_width: ViewWidth) -> Self {
97 Self(Rc::new(RefCell::new(ViewState::new(base_row, line_width))))
98 }
99}
100
101#[derive(Clone)]
102struct ViewState {
103 /// Uppermost row index
104 base_row: u64,
105 /// How many cells per row do we have?
106 line_width: ViewWidth,
107 scroll_handle: UniformListScrollHandle,
108 selection: Option<SelectedMemoryRange>,
109}
110
111impl ViewState {
112 fn new(base_row: u64, line_width: ViewWidth) -> Self {
113 Self {
114 scroll_handle: UniformListScrollHandle::new(),
115 base_row,
116 line_width,
117 selection: None,
118 }
119 }
120 fn row_count(&self) -> u64 {
121 // This was picked fully arbitrarily. There's no incentive for us to care about page sizes other than the fact that it seems to be a good
122 // middle ground for data size.
123 const PAGE_SIZE: u64 = 4096;
124 PAGE_SIZE / self.line_width.width as u64
125 }
126 fn schedule_scroll_down(&mut self) {
127 self.base_row = self.base_row.saturating_add(1)
128 }
129 fn schedule_scroll_up(&mut self) {
130 self.base_row = self.base_row.saturating_sub(1);
131 }
132
133 fn set_offset(&mut self, point: Point<Pixels>) {
134 if point.y >= -Pixels::ZERO {
135 self.schedule_scroll_up();
136 } else if point.y <= -self.scroll_handle.max_offset().height {
137 self.schedule_scroll_down();
138 }
139 self.scroll_handle.set_offset(point);
140 }
141}
142
143impl ScrollableHandle for ViewStateHandle {
144 fn max_offset(&self) -> gpui::Size<Pixels> {
145 self.0.borrow().scroll_handle.max_offset()
146 }
147
148 fn set_offset(&self, point: Point<Pixels>) {
149 self.0.borrow_mut().set_offset(point);
150 }
151
152 fn offset(&self) -> Point<Pixels> {
153 self.0.borrow().scroll_handle.offset()
154 }
155
156 fn viewport(&self) -> gpui::Bounds<Pixels> {
157 self.0.borrow().scroll_handle.viewport()
158 }
159}
160
161static HEX_BYTES_MEMOIZED: LazyLock<[SharedString; 256]> =
162 LazyLock::new(|| std::array::from_fn(|byte| SharedString::from(format!("{byte:02X}"))));
163static UNKNOWN_BYTE: SharedString = SharedString::new_static("??");
164
165impl MemoryView {
166 pub(crate) fn new(
167 session: Entity<Session>,
168 workspace: WeakEntity<Workspace>,
169 stack_frame_list: WeakEntity<StackFrameList>,
170 window: &mut Window,
171 cx: &mut Context<Self>,
172 ) -> Self {
173 let view_state_handle = ViewStateHandle::new(0, WIDTHS[4].clone());
174
175 let query_editor = cx.new(|cx| Editor::single_line(window, cx));
176
177 let mut this = Self {
178 workspace,
179 stack_frame_list,
180 focus_handle: cx.focus_handle(),
181 view_state_handle,
182 query_editor,
183 session,
184 width_picker_handle: Default::default(),
185 is_writing_memory: true,
186 open_context_menu: None,
187 };
188 this.change_query_bar_mode(false, window, cx);
189 cx.on_focus_out(&this.focus_handle, window, |this, _, window, cx| {
190 this.change_query_bar_mode(false, window, cx);
191 cx.notify();
192 })
193 .detach();
194 this
195 }
196
197 fn view_state(&self) -> RefMut<'_, ViewState> {
198 self.view_state_handle.0.borrow_mut()
199 }
200
201 fn render_memory(&self, cx: &mut Context<Self>) -> UniformList {
202 let weak = cx.weak_entity();
203 let session = self.session.clone();
204 let view_state = self.view_state_handle.0.borrow().clone();
205 uniform_list(
206 "debugger-memory-view",
207 view_state.row_count() as usize,
208 move |range, _, cx| {
209 let mut line_buffer = Vec::with_capacity(view_state.line_width.width as usize);
210 let memory_start =
211 (view_state.base_row + range.start as u64) * view_state.line_width.width as u64;
212 let memory_end = (view_state.base_row + range.end as u64)
213 * view_state.line_width.width as u64
214 - 1;
215 let mut memory = session.update(cx, |this, cx| {
216 this.read_memory(memory_start..=memory_end, cx)
217 });
218 let mut rows = Vec::with_capacity(range.end - range.start);
219 for ix in range {
220 line_buffer.extend((&mut memory).take(view_state.line_width.width as usize));
221 rows.push(render_single_memory_view_line(
222 &line_buffer,
223 ix as u64,
224 weak.clone(),
225 cx,
226 ));
227 line_buffer.clear();
228 }
229 rows
230 },
231 )
232 .track_scroll(view_state.scroll_handle)
233 .with_horizontal_sizing_behavior(ListHorizontalSizingBehavior::Unconstrained)
234 .on_scroll_wheel(cx.listener(|this, evt: &ScrollWheelEvent, window, _| {
235 let mut view_state = this.view_state();
236 let delta = evt.delta.pixel_delta(window.line_height());
237 let current_offset = view_state.scroll_handle.offset();
238 view_state
239 .set_offset(current_offset.apply_along(Axis::Vertical, |offset| offset + delta.y));
240 }))
241 }
242 fn render_query_bar(&self, cx: &Context<Self>) -> impl IntoElement {
243 EditorElement::new(
244 &self.query_editor,
245 Self::editor_style(&self.query_editor, cx),
246 )
247 }
248 pub(super) fn go_to_memory_reference(
249 &mut self,
250 memory_reference: &str,
251 evaluate_name: Option<&str>,
252 stack_frame_id: Option<u64>,
253 cx: &mut Context<Self>,
254 ) {
255 use parse_int::parse;
256 let Ok(as_address) = parse::<u64>(memory_reference) else {
257 return;
258 };
259 let access_size = evaluate_name
260 .map(|typ| {
261 self.session.update(cx, |this, cx| {
262 this.data_access_size(stack_frame_id, typ, cx)
263 })
264 })
265 .unwrap_or_else(|| Task::ready(None));
266 cx.spawn(async move |this, cx| {
267 let access_size = access_size.await.unwrap_or(1);
268 this.update(cx, |this, cx| {
269 this.view_state().selection = Some(SelectedMemoryRange::DragComplete(Drag {
270 start_address: as_address,
271 end_address: as_address + access_size - 1,
272 }));
273 this.jump_to_address(as_address, cx);
274 })
275 .ok();
276 })
277 .detach();
278 }
279
280 fn handle_memory_drag(&mut self, evt: &DragMoveEvent<Drag>) {
281 let mut view_state = self.view_state();
282 if !view_state
283 .selection
284 .as_ref()
285 .is_some_and(|selection| selection.is_dragging())
286 {
287 return;
288 }
289 let row_count = view_state.row_count();
290 debug_assert!(row_count > 1);
291 let scroll_handle = &view_state.scroll_handle;
292 let viewport = scroll_handle.viewport();
293
294 if viewport.bottom() < evt.event.position.y {
295 view_state.schedule_scroll_down();
296 } else if viewport.top() > evt.event.position.y {
297 view_state.schedule_scroll_up();
298 }
299 }
300
301 fn editor_style(editor: &Entity<Editor>, cx: &Context<Self>) -> EditorStyle {
302 let is_read_only = editor.read(cx).read_only(cx);
303 let settings = ThemeSettings::get_global(cx);
304 let theme = cx.theme();
305 let text_style = TextStyle {
306 color: if is_read_only {
307 theme.colors().text_muted
308 } else {
309 theme.colors().text
310 },
311 font_family: settings.buffer_font.family.clone(),
312 font_features: settings.buffer_font.features.clone(),
313 font_size: TextSize::Small.rems(cx).into(),
314 font_weight: settings.buffer_font.weight,
315
316 ..Default::default()
317 };
318 EditorStyle {
319 background: theme.colors().editor_background,
320 local_player: theme.players().local(),
321 text: text_style,
322 ..Default::default()
323 }
324 }
325
326 fn render_width_picker(&self, window: &mut Window, cx: &mut Context<Self>) -> DropdownMenu {
327 let weak = cx.weak_entity();
328 let selected_width = self.view_state().line_width.clone();
329 DropdownMenu::new(
330 "memory-view-width-picker",
331 selected_width.label.clone(),
332 ContextMenu::build(window, cx, |mut this, window, cx| {
333 for width in &WIDTHS {
334 let weak = weak.clone();
335 let width = width.clone();
336 this = this.entry(width.label.clone(), None, move |_, cx| {
337 _ = weak.update(cx, |this, _| {
338 let mut view_state = this.view_state();
339 // Convert base ix between 2 line widths to keep the shown memory address roughly the same.
340 // All widths are powers of 2, so the conversion should be lossless.
341 match view_state.line_width.width.cmp(&width.width) {
342 std::cmp::Ordering::Less => {
343 // We're converting up.
344 let shift = width.width.trailing_zeros()
345 - view_state.line_width.width.trailing_zeros();
346 view_state.base_row >>= shift;
347 }
348 std::cmp::Ordering::Greater => {
349 // We're converting down.
350 let shift = view_state.line_width.width.trailing_zeros()
351 - width.width.trailing_zeros();
352 view_state.base_row <<= shift;
353 }
354 _ => {}
355 }
356 view_state.line_width = width.clone();
357 });
358 });
359 }
360 if let Some(ix) = WIDTHS
361 .iter()
362 .position(|width| width.width == selected_width.width)
363 {
364 for _ in 0..=ix {
365 this.select_next(&Default::default(), window, cx);
366 }
367 }
368 this
369 }),
370 )
371 .handle(self.width_picker_handle.clone())
372 }
373
374 fn page_down(&mut self, _: &menu::SelectLast, _: &mut Window, cx: &mut Context<Self>) {
375 let mut view_state = self.view_state();
376 view_state.base_row = view_state
377 .base_row
378 .overflowing_add(view_state.row_count())
379 .0;
380 cx.notify();
381 }
382 fn page_up(&mut self, _: &menu::SelectFirst, _: &mut Window, cx: &mut Context<Self>) {
383 let mut view_state = self.view_state();
384 view_state.base_row = view_state
385 .base_row
386 .overflowing_sub(view_state.row_count())
387 .0;
388 cx.notify();
389 }
390
391 fn change_query_bar_mode(
392 &mut self,
393 is_writing_memory: bool,
394 window: &mut Window,
395 cx: &mut Context<Self>,
396 ) {
397 if is_writing_memory == self.is_writing_memory {
398 return;
399 }
400 if !self.is_writing_memory {
401 self.query_editor.update(cx, |this, cx| {
402 this.clear(window, cx);
403 this.set_placeholder_text("Write to Selected Memory Range", window, cx);
404 });
405 self.is_writing_memory = true;
406 self.query_editor.focus_handle(cx).focus(window);
407 } else {
408 self.query_editor.update(cx, |this, cx| {
409 this.clear(window, cx);
410 this.set_placeholder_text("Go to Memory Address / Expression", window, cx);
411 });
412 self.is_writing_memory = false;
413 }
414 }
415
416 fn toggle_data_breakpoint(
417 &mut self,
418 _: &crate::ToggleDataBreakpoint,
419 _: &mut Window,
420 cx: &mut Context<Self>,
421 ) {
422 let Some(SelectedMemoryRange::DragComplete(selection)) =
423 self.view_state().selection.clone()
424 else {
425 return;
426 };
427 let range = selection.memory_range();
428 let context = Arc::new(DataBreakpointContext::Address {
429 address: range.start().to_string(),
430 bytes: Some(*range.end() - *range.start()),
431 });
432
433 self.session.update(cx, |this, cx| {
434 let data_breakpoint_info = this.data_breakpoint_info(context.clone(), None, cx);
435 cx.spawn(async move |this, cx| {
436 if let Some(info) = data_breakpoint_info.await {
437 let Some(data_id) = info.data_id else {
438 return;
439 };
440 _ = this.update(cx, |this, cx| {
441 this.create_data_breakpoint(
442 context,
443 data_id.clone(),
444 dap::DataBreakpoint {
445 data_id,
446 access_type: None,
447 condition: None,
448 hit_condition: None,
449 },
450 cx,
451 );
452 });
453 }
454 })
455 .detach();
456 })
457 }
458
459 fn confirm(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
460 let selection = self.view_state().selection.clone();
461 if let Some(SelectedMemoryRange::DragComplete(drag)) = selection {
462 // Go into memory writing mode.
463 if !self.is_writing_memory {
464 let should_return = self.session.update(cx, |session, cx| {
465 if !session
466 .capabilities()
467 .supports_write_memory_request
468 .unwrap_or_default()
469 {
470 let adapter_name = session.adapter();
471 // We cannot write memory with this adapter.
472 _ = self.workspace.update(cx, |this, cx| {
473 this.toggle_status_toast(
474 StatusToast::new(format!(
475 "Debug Adapter `{adapter_name}` does not support writing to memory"
476 ), cx, |this, cx| {
477 cx.spawn(async move |this, cx| {
478 cx.background_executor().timer(Duration::from_secs(2)).await;
479 _ = this.update(cx, |_, cx| {
480 cx.emit(DismissEvent)
481 });
482 }).detach();
483 this.icon(ToastIcon::new(IconName::XCircle).color(Color::Error))
484 }),
485 cx,
486 );
487 });
488 true
489 } else {
490 false
491 }
492 });
493 if should_return {
494 return;
495 }
496
497 self.change_query_bar_mode(true, window, cx);
498 } else if self.query_editor.focus_handle(cx).is_focused(window) {
499 let mut text = self.query_editor.read(cx).text(cx);
500 if text.chars().any(|c| !c.is_ascii_hexdigit()) {
501 // Interpret this text as a string and oh-so-conveniently convert it.
502 text = text.bytes().map(|byte| format!("{:02x}", byte)).collect();
503 }
504 self.session.update(cx, |this, cx| {
505 let range = drag.memory_range();
506
507 if let Ok(as_hex) = hex::decode(text) {
508 this.write_memory(*range.start(), &as_hex, cx);
509 }
510 });
511 self.change_query_bar_mode(false, window, cx);
512 }
513
514 cx.notify();
515 return;
516 }
517 // Just change the currently viewed address.
518 if !self.query_editor.focus_handle(cx).is_focused(window) {
519 return;
520 }
521 self.jump_to_query_bar_address(cx);
522 }
523
524 fn jump_to_query_bar_address(&mut self, cx: &mut Context<Self>) {
525 use parse_int::parse;
526 let text = self.query_editor.read(cx).text(cx);
527
528 let Ok(as_address) = parse::<u64>(&text) else {
529 return self.jump_to_expression(text, cx);
530 };
531 self.jump_to_address(as_address, cx);
532 }
533
534 fn jump_to_address(&mut self, address: u64, cx: &mut Context<Self>) {
535 let mut view_state = self.view_state();
536 view_state.base_row = (address & !0xfff) / view_state.line_width.width as u64;
537 let line_ix = (address & 0xfff) / view_state.line_width.width as u64;
538 view_state
539 .scroll_handle
540 .scroll_to_item(line_ix as usize, ScrollStrategy::Center);
541 cx.notify();
542 }
543
544 fn jump_to_expression(&mut self, expr: String, cx: &mut Context<Self>) {
545 let Ok(selected_frame) = self
546 .stack_frame_list
547 .update(cx, |this, _| this.opened_stack_frame_id())
548 else {
549 return;
550 };
551 let expr = format!("?${{{expr}}}");
552 let reference = self.session.update(cx, |this, cx| {
553 this.memory_reference_of_expr(selected_frame, expr, cx)
554 });
555 cx.spawn(async move |this, cx| {
556 if let Some((reference, typ)) = reference.await {
557 _ = this.update(cx, |this, cx| {
558 let sizeof_expr = if typ.as_ref().is_some_and(|t| {
559 t.chars()
560 .all(|c| c.is_whitespace() || c.is_alphabetic() || c == '*')
561 }) {
562 typ.as_deref()
563 } else {
564 None
565 };
566 this.go_to_memory_reference(&reference, sizeof_expr, selected_frame, cx);
567 });
568 }
569 })
570 .detach();
571 }
572
573 fn cancel(&mut self, _: &menu::Cancel, _: &mut Window, cx: &mut Context<Self>) {
574 self.view_state().selection = None;
575 cx.notify();
576 }
577
578 /// Jump to memory pointed to by selected memory range.
579 fn go_to_address(
580 &mut self,
581 _: &GoToSelectedAddress,
582 window: &mut Window,
583 cx: &mut Context<Self>,
584 ) {
585 let Some(SelectedMemoryRange::DragComplete(drag)) = self.view_state().selection.clone()
586 else {
587 return;
588 };
589 let range = drag.memory_range();
590 let Some(memory): Option<Vec<u8>> = self.session.update(cx, |this, cx| {
591 this.read_memory(range, cx).map(|cell| cell.0).collect()
592 }) else {
593 return;
594 };
595 if memory.len() > 8 {
596 return;
597 }
598 let zeros_to_write = 8 - memory.len();
599 let mut acc = String::from("0x");
600 acc.extend(std::iter::repeat("00").take(zeros_to_write));
601 let as_query = memory.into_iter().rev().fold(acc, |mut acc, byte| {
602 _ = write!(&mut acc, "{:02x}", byte);
603 acc
604 });
605 self.query_editor.update(cx, |this, cx| {
606 this.set_text(as_query, window, cx);
607 });
608 self.jump_to_query_bar_address(cx);
609 }
610
611 fn deploy_memory_context_menu(
612 &mut self,
613 range: RangeInclusive<u64>,
614 position: Point<Pixels>,
615 window: &mut Window,
616 cx: &mut Context<Self>,
617 ) {
618 let session = self.session.clone();
619 let context_menu = ContextMenu::build(window, cx, |menu, _, cx| {
620 let range_too_large = range.end() - range.start() > std::mem::size_of::<u64>() as u64;
621 let caps = session.read(cx).capabilities();
622 let supports_data_breakpoints = caps.supports_data_breakpoints.unwrap_or_default()
623 && caps.supports_data_breakpoint_bytes.unwrap_or_default();
624 let memory_unreadable = LazyCell::new(|| {
625 session.update(cx, |this, cx| {
626 this.read_memory(range.clone(), cx)
627 .any(|cell| cell.0.is_none())
628 })
629 });
630
631 let mut menu = menu.action_disabled_when(
632 range_too_large || *memory_unreadable,
633 "Go To Selected Address",
634 GoToSelectedAddress.boxed_clone(),
635 );
636
637 if supports_data_breakpoints {
638 menu = menu.action_disabled_when(
639 *memory_unreadable,
640 "Set Data Breakpoint",
641 ToggleDataBreakpoint { access_type: None }.boxed_clone(),
642 );
643 }
644 menu.context(self.focus_handle.clone())
645 });
646
647 cx.focus_view(&context_menu, window);
648 let subscription = cx.subscribe_in(
649 &context_menu,
650 window,
651 |this, _, _: &DismissEvent, window, cx| {
652 if this.open_context_menu.as_ref().is_some_and(|context_menu| {
653 context_menu.0.focus_handle(cx).contains_focused(window, cx)
654 }) {
655 cx.focus_self(window);
656 }
657 this.open_context_menu.take();
658 cx.notify();
659 },
660 );
661
662 self.open_context_menu = Some((context_menu, position, subscription));
663 }
664}
665
666#[derive(Clone)]
667struct ViewWidth {
668 width: u8,
669 label: SharedString,
670}
671
672impl ViewWidth {
673 const fn new(width: u8, label: &'static str) -> Self {
674 Self {
675 width,
676 label: SharedString::new_static(label),
677 }
678 }
679}
680
681static WIDTHS: [ViewWidth; 7] = [
682 ViewWidth::new(1, "1 byte"),
683 ViewWidth::new(2, "2 bytes"),
684 ViewWidth::new(4, "4 bytes"),
685 ViewWidth::new(8, "8 bytes"),
686 ViewWidth::new(16, "16 bytes"),
687 ViewWidth::new(32, "32 bytes"),
688 ViewWidth::new(64, "64 bytes"),
689];
690
691fn render_single_memory_view_line(
692 memory: &[MemoryCell],
693 ix: u64,
694 weak: gpui::WeakEntity<MemoryView>,
695 cx: &mut App,
696) -> AnyElement {
697 let Ok(view_state) = weak.update(cx, |this, _| this.view_state().clone()) else {
698 return div().into_any();
699 };
700 let base_address = (view_state.base_row + ix) * view_state.line_width.width as u64;
701
702 h_flex()
703 .id((
704 "memory-view-row-full",
705 ix * view_state.line_width.width as u64,
706 ))
707 .size_full()
708 .gap_x_2()
709 .child(
710 div()
711 .child(
712 Label::new(format!("{:016X}", base_address))
713 .buffer_font(cx)
714 .size(ui::LabelSize::Small)
715 .color(Color::Muted),
716 )
717 .px_1()
718 .border_r_1()
719 .border_color(Color::Muted.color(cx)),
720 )
721 .child(
722 h_flex()
723 .id((
724 "memory-view-row-raw-memory",
725 ix * view_state.line_width.width as u64,
726 ))
727 .px_1()
728 .children(memory.iter().enumerate().map(|(cell_ix, cell)| {
729 let weak = weak.clone();
730 div()
731 .id(("memory-view-row-raw-memory-cell", cell_ix as u64))
732 .px_0p5()
733 .when_some(view_state.selection.as_ref(), |this, selection| {
734 this.when(selection.contains(base_address + cell_ix as u64), |this| {
735 let weak = weak.clone();
736
737 this.bg(Color::Selected.color(cx).opacity(0.2)).when(
738 !selection.is_dragging(),
739 |this| {
740 let selection = selection.drag().memory_range();
741 this.on_mouse_down(
742 MouseButton::Right,
743 move |click, window, cx| {
744 _ = weak.update(cx, |this, cx| {
745 this.deploy_memory_context_menu(
746 selection.clone(),
747 click.position,
748 window,
749 cx,
750 )
751 });
752 cx.stop_propagation();
753 },
754 )
755 },
756 )
757 })
758 })
759 .child(
760 Label::new(
761 cell.0
762 .map(|val| HEX_BYTES_MEMOIZED[val as usize].clone())
763 .unwrap_or_else(|| UNKNOWN_BYTE.clone()),
764 )
765 .buffer_font(cx)
766 .when(cell.0.is_none(), |this| this.color(Color::Muted))
767 .size(ui::LabelSize::Small),
768 )
769 .on_drag(
770 Drag {
771 start_address: base_address + cell_ix as u64,
772 end_address: base_address + cell_ix as u64,
773 },
774 {
775 let weak = weak.clone();
776 move |drag, _, _, cx| {
777 _ = weak.update(cx, |this, _| {
778 this.view_state().selection =
779 Some(SelectedMemoryRange::DragUnderway(drag.clone()));
780 });
781
782 cx.new(|_| Empty)
783 }
784 },
785 )
786 .on_drop({
787 let weak = weak.clone();
788 move |drag: &Drag, _, cx| {
789 _ = weak.update(cx, |this, _| {
790 this.view_state().selection =
791 Some(SelectedMemoryRange::DragComplete(Drag {
792 start_address: drag.start_address,
793 end_address: base_address + cell_ix as u64,
794 }));
795 });
796 }
797 })
798 .drag_over(move |style, drag: &Drag, _, cx| {
799 _ = weak.update(cx, |this, _| {
800 this.view_state().selection =
801 Some(SelectedMemoryRange::DragUnderway(Drag {
802 start_address: drag.start_address,
803 end_address: base_address + cell_ix as u64,
804 }));
805 });
806
807 style
808 })
809 })),
810 )
811 .child(
812 h_flex()
813 .id((
814 "memory-view-row-ascii-memory",
815 ix * view_state.line_width.width as u64,
816 ))
817 .h_full()
818 .px_1()
819 .mr_4()
820 // .gap_x_1p5()
821 .border_x_1()
822 .border_color(Color::Muted.color(cx))
823 .children(memory.iter().enumerate().map(|(ix, cell)| {
824 let as_character = char::from(cell.0.unwrap_or(0));
825 let as_visible = if as_character.is_ascii_graphic() {
826 as_character
827 } else {
828 'ยท'
829 };
830 div()
831 .px_0p5()
832 .when_some(view_state.selection.as_ref(), |this, selection| {
833 this.when(selection.contains(base_address + ix as u64), |this| {
834 this.bg(Color::Selected.color(cx).opacity(0.2))
835 })
836 })
837 .child(
838 Label::new(format!("{as_visible}"))
839 .buffer_font(cx)
840 .when(cell.0.is_none(), |this| this.color(Color::Muted))
841 .size(ui::LabelSize::Small),
842 )
843 })),
844 )
845 .into_any()
846}
847
848impl Render for MemoryView {
849 fn render(
850 &mut self,
851 window: &mut ui::Window,
852 cx: &mut ui::Context<Self>,
853 ) -> impl ui::IntoElement {
854 let (icon, tooltip_text) = if self.is_writing_memory {
855 (IconName::Pencil, "Edit memory at a selected address")
856 } else {
857 (
858 IconName::LocationEdit,
859 "Change address of currently viewed memory",
860 )
861 };
862 v_flex()
863 .id("Memory-view")
864 .on_action(cx.listener(Self::cancel))
865 .on_action(cx.listener(Self::go_to_address))
866 .p_1()
867 .on_action(cx.listener(Self::confirm))
868 .on_action(cx.listener(Self::toggle_data_breakpoint))
869 .on_action(cx.listener(Self::page_down))
870 .on_action(cx.listener(Self::page_up))
871 .size_full()
872 .track_focus(&self.focus_handle)
873 .child(
874 h_flex()
875 .w_full()
876 .mb_0p5()
877 .gap_1()
878 .child(
879 h_flex()
880 .w_full()
881 .rounded_md()
882 .border_1()
883 .gap_x_2()
884 .px_2()
885 .py_0p5()
886 .mb_0p5()
887 .bg(cx.theme().colors().editor_background)
888 .when_else(
889 self.query_editor
890 .focus_handle(cx)
891 .contains_focused(window, cx),
892 |this| this.border_color(cx.theme().colors().border_focused),
893 |this| this.border_color(cx.theme().colors().border_transparent),
894 )
895 .child(
896 div()
897 .id("memory-view-editor-icon")
898 .child(Icon::new(icon).size(ui::IconSize::XSmall))
899 .tooltip(Tooltip::text(tooltip_text)),
900 )
901 .child(self.render_query_bar(cx)),
902 )
903 .child(self.render_width_picker(window, cx)),
904 )
905 .child(Divider::horizontal())
906 .child(
907 v_flex()
908 .size_full()
909 .on_drag_move(cx.listener(|this, evt, _, _| {
910 this.handle_memory_drag(evt);
911 }))
912 .child(self.render_memory(cx).size_full())
913 .children(self.open_context_menu.as_ref().map(|(menu, position, _)| {
914 deferred(
915 anchored()
916 .position(*position)
917 .anchor(gpui::Corner::TopLeft)
918 .child(menu.clone()),
919 )
920 .with_priority(1)
921 }))
922 .custom_scrollbars(
923 ui::Scrollbars::new(ui::ScrollAxes::Both)
924 .tracked_scroll_handle(self.view_state_handle.clone())
925 .with_track_along(
926 ui::ScrollAxes::Both,
927 cx.theme().colors().panel_background,
928 )
929 .tracked_entity(cx.entity_id()),
930 window,
931 cx,
932 ),
933 )
934 }
935}