1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
use std::any::TypeId;
use super::events::*;
use super::graph::Graph;
use super::initialization_flags::*;
use super::RenderingOptions;
use crate::{prelude::*, ui::graphview::GraphNode};
struct NodeInfo {
id: usize,
top_left: Point,
origin: Point,
}
enum Drag {
None,
View(Point),
Node(NodeInfo),
}
#[CustomControl(overwrite=OnPaint+OnKeyPressed+OnMouseEvent+OnResize+OnFocus, internal=true)]
pub struct GraphView<T>
where
T: GraphNode + 'static,
{
graph: Graph<T>,
origin_point: Point,
background: Option<Character>,
flags: Flags,
drag: Drag,
comp: ListScrollBars,
arrange_method: ArrangeMethod,
rendering_options: RenderingOptions,
}
impl<T> GraphView<T>
where
T: GraphNode,
{
/// Creates a new GraphView control with the specified layout and flags.
///
/// # Parameters
/// - `layout`: The layout configuration for positioning and sizing the GraphView
/// - `flags`: Combination of initialization flags that control the GraphView behavior:
/// - `Flags::ScrollBars`: Enables scroll bars for navigating large graphs
/// - `Flags::SearchBar`: Enables a search bar for finding nodes
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// type MyNode = &'static str; // or any other type that implements the GraphNode trait
///
/// let mut graph_view: GraphView<MyNode> = GraphView::new(
/// layout!("x:1,y:1,w:50,h:30"),
/// graphview::Flags::ScrollBars | graphview::Flags::SearchBar
/// );
/// ```
pub fn new(layout: Layout, flags: Flags) -> Self {
Self {
base: ControlBase::with_status_flags(
layout,
(StatusFlags::Visible | StatusFlags::Enabled | StatusFlags::AcceptInput)
| if flags.contains_one(Flags::ScrollBars | Flags::SearchBar) {
StatusFlags::IncreaseBottomMarginOnFocus | StatusFlags::IncreaseRightMarginOnFocus
} else {
StatusFlags::None
},
),
flags,
origin_point: Point::ORIGIN,
background: None,
drag: Drag::None,
graph: Graph::default(),
arrange_method: ArrangeMethod::GridPacked,
rendering_options: RenderingOptions::new(),
comp: ListScrollBars::new(flags.contains(Flags::ScrollBars), flags.contains(Flags::SearchBar)),
}
}
/// Sets the background of the GraphView to the specified character.
///
/// This method allows you to customize the background appearance of the GraphView.
/// The background character will be used to fill the entire control area before
/// drawing the graph nodes and edges.
///
/// # Parameters
/// - `backgroud_char`: The character to use as background, including its color and formatting
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// type MyNode = &'static str; // or any other type that implements the GraphNode trait
///
/// let mut graph_view: GraphView<MyNode> = GraphView::new(
/// layout!("x:1,y:1,w:30,h:10"),
/// graphview::Flags::ScrollBars
/// );
/// graph_view.set_background(Character::new('·', Color::Gray, Color::Black, CharFlags::None));
/// ```
pub fn set_background(&mut self, backgroud_char: Character) {
self.background = Some(backgroud_char);
}
/// Clears the background character of the GraphView.
///
/// This method removes any previously set background character and resets the GraphView
/// to use transparent foreground and background colors, allowing the default theme
/// background to show through.
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// type MyNode = &'static str; // or any other type that implements the GraphNode trait
///
/// let mut graph_view: GraphView<MyNode> = GraphView::new(
/// layout!("x:1,y:1,w:30,h:10"),
/// graphview::Flags::None
/// );
/// graph_view.set_background(Character::new('*', Color::White, Color::Black, CharFlags::None));
/// graph_view.clear_background(); // Removes the background character
/// ```
pub fn clear_background(&mut self) {
self.background = None;
}
/// Sets the graph data to be displayed in the GraphView.
///
/// This method replaces the current graph with a new one, updates the rendering options,
/// and automatically arranges the nodes using the current arrangement method.
///
/// # Parameters
/// - `graph`: The graph containing nodes and edges to be displayed
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// type MyNode = &'static str; // or any other type that implements the GraphNode trait
///
/// let mut graph_view: GraphView<MyNode> = GraphView::new(
/// layout!("x:1,y:1,w:50,h:30"),
/// graphview::Flags::ScrollBars
/// );
///
/// let nodes = &["A", "B", "C", "D", "E"];
/// let edges = &[(0, 1), (0, 2), (1, 3), (2, 4)];
/// let graph = graphview::Graph::with_slices(nodes, edges, true);
/// graph_view.set_graph(graph);
/// ```
pub fn set_graph(&mut self, graph: Graph<T>) {
self.graph = graph;
self.graph.update_rendering_options(&self.rendering_options, &self.base);
self.arrange_nodes(self.arrange_method);
}
/// Sets the edge routing method for drawing connections between nodes.
///
/// This method determines how edges are drawn between nodes in the graph.
///
/// # Parameters
/// - `routing`: The routing method to use:
/// - `EdgeRouting::Direct`: Draw edges as straight lines between nodes
/// - `EdgeRouting::Orthogonal`: Draw edges as orthogonal (right-angled) lines
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// type MyNode = &'static str; // or any other type that implements the GraphNode trait
///
/// let mut graph_view: GraphView<MyNode> = GraphView::new(
/// layout!("x:1,y:1,w:50,h:30"),
/// graphview::Flags::ScrollBars
/// );
/// graph_view.set_edge_routing(graphview::EdgeRouting::Orthogonal);
/// ```
pub fn set_edge_routing(&mut self, routing: EdgeRouting) {
self.rendering_options.edge_routing = routing;
self.graph.update_rendering_options(&self.rendering_options, &self.base);
}
/// Sets the line type used for drawing edges between nodes.
///
/// This method controls the visual style of the lines used to draw edges.
/// Different line types provide different visual appearances.
///
/// # Parameters
/// - `line_type`: The line style to use:
/// - `LineType::Single`: Single lines with Unicode box-drawing characters
/// - `LineType::Double`: Double lines with Unicode box-drawing characters
/// - `LineType::SingleThick`: Thick single lines
/// - `LineType::Border`: Border-style thick lines
/// - `LineType::Ascii`: ASCII characters ('+', '-', '|')
/// - `LineType::AsciiRound`: ASCII with rounded corners
/// - `LineType::SingleRound`: Unicode single lines with rounded corners
/// - `LineType::Braille`: Double lines using Braille characters
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// type MyNode = &'static str; // or any other type that implements the GraphNode trait
///
/// let mut graph_view: GraphView<MyNode> = GraphView::new(
/// layout!("x:1,y:1,w:50,h:30"),
/// graphview::Flags::ScrollBars
/// );
/// graph_view.set_edge_line_type(LineType::Double);
/// ```
pub fn set_edge_line_type(&mut self, line_type: LineType) {
self.rendering_options.edge_line_type = line_type;
self.graph.update_rendering_options(&self.rendering_options, &self.base);
}
/// Enables or disables edge highlighting for the currently selected node.
///
/// When edge highlighting is enabled, edges connected to the currently selected
/// node will be visually highlighted to make the connections more apparent.
///
/// # Parameters
/// - `incoming`: Whether to highlight edges pointing to the current node
/// - `outgoing`: Whether to highlight edges originating from the current node
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// type MyNode = &'static str; // or any other type that implements the GraphNode trait
///
/// let mut graph_view: GraphView<MyNode> = GraphView::new(
/// layout!("x:1,y:1,w:50,h:30"),
/// graphview::Flags::ScrollBars
/// );
/// // Highlight both incoming and outgoing edges
/// graph_view.enable_edge_highlighting(true, true);
///
/// // Only highlight outgoing edges
/// graph_view.enable_edge_highlighting(false, true);
/// ```
pub fn enable_edge_highlighting(&mut self, incoming: bool, outgoing: bool) {
self.rendering_options.highlight_edges_in = incoming;
self.rendering_options.highlight_edges_out = outgoing;
self.graph.update_rendering_options(&self.rendering_options, &self.base);
}
/// Enables or disables arrow heads on directed edges.
///
/// When enabled, directed edges will display arrow heads to indicate
/// the direction of the connection between nodes.
///
/// # Parameters
/// - `enabled`: Whether to show arrow heads on directed edges
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// type MyNode = &'static str; // or any other type that implements the GraphNode trait
///
/// let mut graph_view: GraphView<MyNode> = GraphView::new(
/// layout!("x:1,y:1,w:50,h:30"),
/// graphview::Flags::ScrollBars
/// );
/// graph_view.enable_arrow_heads(true);
/// ```
pub fn enable_arrow_heads(&mut self, enabled: bool) {
self.rendering_options.show_arrow_heads = enabled;
self.graph.update_rendering_options(&self.rendering_options, &self.base);
}
/// Arranges the nodes in the graph using the specified layout algorithm.
///
/// This method automatically positions all nodes in the graph according to
/// the chosen arrangement algorithm. The graph will be resized and repainted
/// after the arrangement is complete.
///
/// # Parameters
/// - `method`: The arrangement algorithm to use:
/// - `ArrangeMethod::None`: No automatic arrangement (manual positioning)
/// - `ArrangeMethod::Grid`: Arrange nodes in a regular grid with spacing
/// - `ArrangeMethod::GridPacked`: Arrange nodes in a compact grid
/// - `ArrangeMethod::Circular`: Arrange nodes in a circular pattern
/// - `ArrangeMethod::Hierarchical`: Arrange nodes in hierarchical layers with spacing
/// - `ArrangeMethod::HierarchicalPacked`: Arrange nodes in compact hierarchical layers
/// - `ArrangeMethod::ForceDirected`: Use force-directed algorithm for natural positioning
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// type MyNode = &'static str; // or any other type that implements the GraphNode trait
///
/// let mut graph_view: GraphView<MyNode> = GraphView::new(
/// layout!("x:1,y:1,w:50,h:30"),
/// graphview::Flags::ScrollBars
/// );
///
/// // Arrange nodes in a hierarchical layout
/// graph_view.arrange_nodes(graphview::ArrangeMethod::Hierarchical);
///
/// // Use force-directed layout for organic appearance
/// graph_view.arrange_nodes(graphview::ArrangeMethod::ForceDirected);
/// ```
pub fn arrange_nodes(&mut self, method: ArrangeMethod) {
match method {
ArrangeMethod::None => { /* do nothing */ }
ArrangeMethod::Grid => super::node_layout::grid::rearange(&mut self.graph, 2),
ArrangeMethod::GridPacked => super::node_layout::grid::rearange(&mut self.graph, 1),
ArrangeMethod::Circular => super::node_layout::circular::rearange(&mut self.graph),
ArrangeMethod::Hierarchical => super::node_layout::hierarchical::rearange(&mut self.graph, 2),
ArrangeMethod::HierarchicalPacked => super::node_layout::hierarchical::rearange(&mut self.graph, 1),
ArrangeMethod::ForceDirected => super::node_layout::force_directed::rearange(&mut self.graph),
}
self.arrange_method = method;
self.graph.resize_graph(true);
self.graph.repaint(&self.base);
}
/// Returns an immutable reference to the underlying graph data.
///
/// This method provides read-only access to the graph structure,
/// allowing you to query nodes, edges, and other graph properties.
///
/// # Returns
/// A reference to the `Graph<T>` containing all nodes and edges
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// type MyNode = &'static str; // or any other type that implements the GraphNode trait
///
/// let graph_view: GraphView<MyNode> = GraphView::new(
/// layout!("x:1,y:1,w:50,h:30"),
/// graphview::Flags::ScrollBars
/// );
///
/// let graph = graph_view.graph();
/// if let Some(node) = graph.current_node() {
/// // do something with the current node
/// }
/// ```
pub fn graph(&self) -> &Graph<T> {
&self.graph
}
fn move_scroll_to(&mut self, x: i32, y: i32) {
let sz = self.size();
let surface_size = self.graph.size();
self.origin_point.x = if surface_size.width <= sz.width {
0
} else {
x.max((sz.width as i32) - (surface_size.width as i32))
};
self.origin_point.y = if surface_size.height <= sz.height {
0
} else {
y.max((sz.height as i32) - (surface_size.height as i32))
};
self.origin_point.x = self.origin_point.x.min(0);
self.origin_point.y = self.origin_point.y.min(0);
self.comp.set_indexes((-self.origin_point.x) as u64, (-self.origin_point.y) as u64);
}
fn update_scroll_pos_from_scrollbars(&mut self) {
let h = -(self.comp.horizontal_index() as i32);
let v = -(self.comp.vertical_index() as i32);
self.move_scroll_to(h, v);
}
fn update_scroll_bars(&mut self) {
let paint_sz = self.graph.size();
let sz = self.size();
self.comp.resize(paint_sz.width as u64, paint_sz.height as u64, &self.base, sz);
self.move_scroll_to(self.origin_point.x, self.origin_point.y);
}
fn ensure_node_is_visible(&mut self, node_id: usize) {
if let Some(node) = self.graph.nodes.get(node_id) {
let node_rect = node.rect;
let sz = self.size();
let view_rect = Rect::with_point_and_size(Point::new(-self.origin_point.x, -self.origin_point.y), sz);
if !view_rect.contains_rect(node_rect) {
let mut adx = 0;
let mut ady = 0;
if node_rect.right() > view_rect.right() {
adx = node_rect.right() - view_rect.right();
}
if node_rect.left() < view_rect.left() {
adx = node_rect.left() - view_rect.left();
}
if node_rect.bottom() > view_rect.bottom() {
ady = node_rect.bottom() - view_rect.bottom();
}
if node_rect.top() < view_rect.top() {
ady = node_rect.top() - view_rect.top();
}
self.move_scroll_to(self.origin_point.x - adx, self.origin_point.y - ady);
}
}
}
fn ensure_current_node_is_visible(&mut self) {
if let Some(id) = self.graph.current_node_id() {
self.ensure_node_is_visible(id);
}
}
fn search_text(&mut self) {
let txt = self.comp.search_text();
let count = self.graph.filter(txt, &self.base);
self.ensure_current_node_is_visible();
if count == self.graph.nodes.len() {
self.comp.clear_match_count();
} else {
self.comp.set_match_count(count);
}
}
fn goto_next_match(&mut self) {
self.graph.goto_next_match(&self.base);
self.ensure_current_node_is_visible();
}
fn goto_previous_match(&mut self) {
self.graph.goto_previous_match(&self.base);
self.ensure_current_node_is_visible();
}
fn raise_current_node_changed(&mut self, old_id: Option<usize>) {
let new_id = self.graph.current_node_id();
if (old_id != new_id) && new_id.is_some() {
self.raise_event(ControlEvent {
emitter: self.handle,
receiver: self.event_processor,
data: ControlEventData::GraphView(EventData {
event_type: GraphViewEventTypes::CurrentNodeChanged,
type_id: TypeId::of::<T>(),
}),
});
}
}
fn raise_action_on_node(&mut self, id: usize) {
self.raise_event(ControlEvent {
emitter: self.handle,
receiver: self.event_processor,
data: ControlEventData::GraphView(EventData {
event_type: GraphViewEventTypes::NodeAction(id),
type_id: TypeId::of::<T>(),
}),
});
}
}
impl<T> OnResize for GraphView<T>
where
T: GraphNode,
{
fn on_resize(&mut self, _: Size, _: Size) {
self.update_scroll_bars();
}
}
impl<T> OnPaint for GraphView<T>
where
T: GraphNode,
{
fn on_paint(&self, surface: &mut Surface, theme: &Theme) {
if (self.has_focus()) && (self.flags.contains_one(Flags::ScrollBars | Flags::SearchBar)) {
self.comp.paint(surface, theme, self);
surface.reduce_clip_by(0, 0, 1, 1);
}
if let Some(back) = self.background {
surface.clear(back);
}
surface.draw_surface(self.origin_point.x, self.origin_point.y, self.graph.surface());
// let sz = format!("Size {:?}", self.size());
// surface.write_string(0, 0, &sz, charattr!("w,black"), false);
}
}
impl<T> OnKeyPressed for GraphView<T>
where
T: GraphNode,
{
fn on_key_pressed(&mut self, key: Key, character: char) -> EventProcessStatus {
let nid = self.graph.current_node_id();
if self.comp.process_key_pressed(key, character) {
self.search_text();
self.raise_current_node_changed(nid);
return EventProcessStatus::Processed;
}
if self.graph.process_key_events(key, &self.base) {
self.ensure_current_node_is_visible();
self.comp.exit_edit_mode();
self.raise_current_node_changed(nid);
return EventProcessStatus::Processed;
}
let result = match key.value() {
key!("Alt+Left") => {
self.move_scroll_to(self.origin_point.x + 1, self.origin_point.y);
EventProcessStatus::Processed
}
key!("Alt+Right") => {
self.move_scroll_to(self.origin_point.x - 1, self.origin_point.y);
EventProcessStatus::Processed
}
key!("Alt+Up") => {
self.move_scroll_to(self.origin_point.x, self.origin_point.y + 1);
EventProcessStatus::Processed
}
key!("Alt+Down") => {
self.move_scroll_to(self.origin_point.x, self.origin_point.y - 1);
EventProcessStatus::Processed
}
key!("PageUp") => {
self.move_scroll_to(self.origin_point.x, self.origin_point.y + self.size().height as i32);
EventProcessStatus::Processed
}
key!("PageDown") => {
self.move_scroll_to(self.origin_point.x, self.origin_point.y - self.size().height as i32);
EventProcessStatus::Processed
}
key!("Home") => {
self.move_scroll_to(0, 0);
EventProcessStatus::Processed
}
key!("End") => {
self.move_scroll_to(i32::MIN, i32::MIN);
EventProcessStatus::Processed
}
key!("Enter") => {
if self.comp.is_in_edit_mode() {
self.goto_next_match();
self.raise_current_node_changed(nid);
// exist directly so that we don't exit the edit mode
return EventProcessStatus::Processed;
} else {
if let Some(id) = self.graph.current_node_id() {
self.raise_action_on_node(id);
return EventProcessStatus::Processed;
}
EventProcessStatus::Ignored
}
}
key!("Ctrl+Enter") => {
if self.comp.is_in_edit_mode() {
self.goto_previous_match();
self.raise_current_node_changed(nid);
// exist directly so that we don't exit the edit mode
return EventProcessStatus::Processed;
} else {
EventProcessStatus::Ignored
}
}
_ => EventProcessStatus::Ignored,
};
if result == EventProcessStatus::Processed {
self.comp.exit_edit_mode();
}
if self.comp.should_repaint() {
EventProcessStatus::Processed
} else {
result
}
}
}
impl<T> OnFocus for GraphView<T>
where
T: GraphNode,
{
fn on_focus(&mut self) {
self.graph.repaint(&self.base);
}
fn on_lose_focus(&mut self) {
self.graph.repaint(&self.base);
}
}
impl<T> OnMouseEvent for GraphView<T>
where
T: GraphNode,
{
fn on_mouse_event(&mut self, event: &MouseEvent) -> EventProcessStatus {
if self.comp.process_mouse_event(event) {
self.update_scroll_pos_from_scrollbars();
return EventProcessStatus::Processed;
}
match event {
MouseEvent::Enter | MouseEvent::Leave => {
self.graph.reset_hover(&self.base);
self.hide_tooltip();
EventProcessStatus::Processed
}
MouseEvent::Over(point) => {
let p = Point::new(point.x - self.origin_point.x, point.y - self.origin_point.y);
if !self.graph.process_mouse_over(&self.base, p) {
return EventProcessStatus::Ignored;
}
if let Some(id) = self.graph.hovered_node_id() {
let r = self.graph.nodes[id].rect + (self.origin_point.x, self.origin_point.y);
if let Some(desc) = self.graph.node_description(id) {
self.base.show_tooltip_on_rect(desc, &r);
} else {
self.hide_tooltip();
}
} else {
self.hide_tooltip();
}
EventProcessStatus::Processed
}
MouseEvent::Pressed(mouse_data) => {
let data = Point::new(mouse_data.x - self.origin_point.x, mouse_data.y - self.origin_point.y);
if let Some(id) = self.graph.mouse_pos_to_index(data.x, data.y) {
// click on a node
let nid = self.graph.current_node_id();
self.graph.set_current_node(id, &self.base);
let tl = self.graph.nodes[id].rect.top_left();
self.drag = Drag::Node(NodeInfo {
id,
top_left: tl,
origin: Point::new(data.x, data.y),
});
self.raise_current_node_changed(nid);
return EventProcessStatus::Processed;
}
if self.flags.contains_one(Flags::ScrollBars) && (self.has_focus()) {
let sz = self.size();
if (data.x == sz.width as i32) || (data.y == sz.height as i32) {
return EventProcessStatus::Ignored;
}
}
self.drag = Drag::View(Point::new(data.x, data.y));
EventProcessStatus::Processed
}
MouseEvent::Released(mouse_data) => match &self.drag {
Drag::None => {
EventProcessStatus::Ignored
}
Drag::View(p) => {
self.move_scroll_to(self.origin_point.x + mouse_data.x - p.x, self.origin_point.y + mouse_data.y - p.y);
self.drag = Drag::None;
EventProcessStatus::Processed
}
Drag::Node(node_info) => {
let data = Point::new(mouse_data.x - self.origin_point.x, mouse_data.y - self.origin_point.y);
if self.graph.move_node_to(
node_info.id,
node_info.top_left.x + data.x - node_info.origin.x,
node_info.top_left.y + data.y - node_info.origin.y,
&self.base,
) {
self.update_scroll_bars();
}
self.drag = Drag::None;
self.ensure_current_node_is_visible();
EventProcessStatus::Processed
}
},
MouseEvent::DoubleClick(mouse_data) => {
let data = Point::new(mouse_data.x - self.origin_point.x, mouse_data.y - self.origin_point.y);
if let Some(id) = self.graph.mouse_pos_to_index(data.x, data.y) {
self.raise_action_on_node(id);
EventProcessStatus::Processed
} else {
EventProcessStatus::Ignored
}
}
MouseEvent::Drag(mouse_data) => match &self.drag {
Drag::None => {
EventProcessStatus::Ignored
}
Drag::View(p) => {
self.move_scroll_to(self.origin_point.x + mouse_data.x - p.x, self.origin_point.y + mouse_data.y - p.y);
self.drag = Drag::View(Point::new(mouse_data.x, mouse_data.y));
EventProcessStatus::Processed
}
Drag::Node(node_info) => {
let data = Point::new(mouse_data.x - self.origin_point.x, mouse_data.y - self.origin_point.y);
if self.graph.move_node_to(
node_info.id,
node_info.top_left.x + data.x - node_info.origin.x,
node_info.top_left.y + data.y - node_info.origin.y,
&self.base,
) {
self.update_scroll_bars();
}
self.ensure_current_node_is_visible();
EventProcessStatus::Processed
}
},
MouseEvent::Wheel(dir) => {
match dir {
MouseWheelDirection::Left => self.move_scroll_to(self.origin_point.x + 1, self.origin_point.y),
MouseWheelDirection::Right => self.move_scroll_to(self.origin_point.x - 1, self.origin_point.y),
MouseWheelDirection::Up => self.move_scroll_to(self.origin_point.x, self.origin_point.y + 1),
MouseWheelDirection::Down => self.move_scroll_to(self.origin_point.x, self.origin_point.y - 1),
};
EventProcessStatus::Processed
}
}
}
}