Skip to content

Commit 44ff1a6

Browse files
committed
wip; toggle on click
1 parent 68b12de commit 44ff1a6

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

promkit-widgets/src/structured/json/document.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,40 @@ impl Document {
2727
self.rows.extract(self.position, n)
2828
}
2929

30+
/// Returns the current visible row position.
31+
pub fn position(&self) -> usize {
32+
self.position
33+
}
34+
35+
/// Resolves a visible row offset from the current position into the backing row index.
36+
pub fn visible_offset_to_index(&self, offset: usize) -> Option<usize> {
37+
if self.rows.is_empty() {
38+
return None;
39+
}
40+
41+
let mut index = self.position;
42+
for _ in 0..offset {
43+
let next = self.rows.down(index);
44+
if next == index {
45+
return None;
46+
}
47+
index = next;
48+
}
49+
50+
Some(index)
51+
}
52+
3053
/// Toggles the visibility of a node at the cursor's current position.
3154
pub fn toggle(&mut self) {
3255
let index = self.rows.toggle(self.position);
3356
self.position = index;
3457
}
3558

59+
/// Sets the current position directly.
60+
pub fn set_position(&mut self, position: usize) {
61+
self.position = position.min(self.rows.len().saturating_sub(1));
62+
}
63+
3664
/// Sets the visibility of all rows.
3765
pub fn set_nodes_visibility(&mut self, collapsed: bool) {
3866
self.rows.set_rows_visibility(collapsed);

promkit/src/preset/json/evaluate.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ pub async fn default(event: &Event, ctx: &mut Json) -> anyhow::Result<Signal> {
7878
ctx.json.document.toggle();
7979
}
8080

81+
Event::Mouse(MouseEvent {
82+
kind: MouseEventKind::Down(_),
83+
column,
84+
row,
85+
modifiers: KeyModifiers::NONE,
86+
}) => {
87+
if let Some(renderer) = ctx.renderer.as_ref() {
88+
if let Some(hit) = renderer.hit_test(*row, *column).await {
89+
if hit.key == super::Index::Json {
90+
if let Some(index) =
91+
ctx.json.document.visible_offset_to_index(hit.visible_row)
92+
{
93+
ctx.json.document.set_position(index);
94+
ctx.json.document.toggle();
95+
}
96+
}
97+
}
98+
}
99+
}
100+
81101
_ => (),
82102
}
83103
Ok(Signal::Continue)

0 commit comments

Comments
 (0)