use crate::graph::LinkModel;
use crate::hit_test::NodeGeometry;
use crate::state::GeometryCache;
use slint::{Model, SharedString, VecModel};
use std::cell::RefCell;
use std::rc::Rc;
trait ModelSyncer {
fn sync(&self, paths: &[LinkPathData]);
}
struct ConcreteModelSyncer<P, F> {
model: Rc<VecModel<P>>,
constructor: F,
}
impl<P, F> ModelSyncer for ConcreteModelSyncer<P, F>
where
P: Clone + 'static,
F: Fn(i32, SharedString, slint::Color, f32, i32) -> P,
{
fn sync(&self, paths: &[LinkPathData]) {
for (i, path) in paths.iter().enumerate() {
let item = (self.constructor)(
path.id,
SharedString::from(path.path_commands.as_str()),
path.color,
path.line_width,
path.status,
);
if i < self.model.row_count() {
self.model.set_row_data(i, item);
} else {
self.model.push(item);
}
}
while self.model.row_count() > paths.len() {
self.model.remove(self.model.row_count() - 1);
}
}
}
pub struct LinkManager<L, N = crate::hit_test::SimpleNodeGeometry> {
links: Vec<L>,
cache: Rc<RefCell<GeometryCache<N>>>,
paths: Rc<RefCell<Vec<LinkPathData>>>,
current_zoom: f32,
bezier_offset: f32,
syncer: Option<Box<dyn ModelSyncer>>,
}
#[derive(Clone)]
struct LinkPathData {
id: i32,
path_commands: String,
color: slint::Color,
line_width: f32,
status: i32,
}
impl<L, N> LinkManager<L, N>
where
L: LinkModel,
N: NodeGeometry + Copy,
{
pub fn new(cache: Rc<RefCell<GeometryCache<N>>>) -> Self {
Self {
links: Vec::new(),
cache,
paths: Rc::new(RefCell::new(Vec::new())),
current_zoom: 1.0,
bezier_offset: 50.0,
syncer: None,
}
}
pub fn bind_model<P, F>(&mut self, model: Rc<VecModel<P>>, constructor: F)
where
P: Clone + 'static,
F: Fn(i32, SharedString, slint::Color, f32, i32) -> P + 'static,
{
self.syncer = Some(Box::new(ConcreteModelSyncer { model, constructor }));
}
pub fn add(&mut self, link: L) {
self.links.push(link);
}
pub fn remove(&mut self, id: i32) -> bool {
let len_before = self.links.len();
self.links.retain(|link| link.id() != id);
self.links.len() != len_before
}
pub fn clear(&mut self) {
self.links.clear();
self.paths.borrow_mut().clear();
}
pub fn len(&self) -> usize {
self.links.len()
}
pub fn is_empty(&self) -> bool {
self.links.is_empty()
}
pub fn links(&self) -> &[L] {
&self.links
}
pub fn links_mut(&mut self) -> &mut Vec<L> {
&mut self.links
}
pub fn set_bezier_offset(&mut self, offset: f32) {
self.bezier_offset = offset;
}
pub fn update_paths(&mut self, zoom: f32) {
self.current_zoom = zoom;
let cache = self.cache.borrow();
let mut paths = self.paths.borrow_mut();
paths.clear();
for link in &self.links {
if let Some(path) = cache.compute_link_path(
link.start_pin_id(),
link.end_pin_id(),
zoom,
self.bezier_offset,
) {
paths.push(LinkPathData {
id: link.id(),
path_commands: path,
color: link.color(),
line_width: link.line_width(),
status: link.status(),
});
}
}
if let Some(syncer) = &self.syncer {
syncer.sync(&paths);
}
}
pub fn ids(&self) -> impl Iterator<Item = i32> + '_ {
self.links.iter().map(|l| l.id())
}
pub fn find(&self, id: i32) -> Option<&L> {
self.links.iter().find(|l| l.id() == id)
}
pub fn find_mut(&mut self, id: i32) -> Option<&mut L> {
self.links.iter_mut().find(|l| l.id() == id)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::graph::SimpleLink;
use crate::hit_test::SimpleNodeGeometry;
use slint::Color;
fn setup_cache() -> Rc<RefCell<GeometryCache<SimpleNodeGeometry>>> {
let cache = Rc::new(RefCell::new(GeometryCache::new()));
cache
.borrow_mut()
.update_node_rect(1, 0.0, 0.0, 100.0, 50.0);
cache
.borrow_mut()
.update_node_rect(2, 200.0, 100.0, 100.0, 50.0);
cache.borrow_mut().handle_pin_report(3, 1, 2, 100.0, 25.0); cache.borrow_mut().handle_pin_report(4, 2, 1, 0.0, 25.0);
cache
}
#[test]
fn test_new_manager_is_empty() {
let cache = setup_cache();
let manager: LinkManager<SimpleLink, _> = LinkManager::new(cache);
assert!(manager.is_empty());
assert_eq!(manager.len(), 0);
}
#[test]
fn test_add_link() {
let cache = setup_cache();
let mut manager = LinkManager::new(cache);
manager.add(SimpleLink::new(1, 3, 4, Color::from_rgb_u8(255, 0, 0)));
assert_eq!(manager.len(), 1);
assert!(!manager.is_empty());
}
#[test]
fn test_remove_link() {
let cache = setup_cache();
let mut manager = LinkManager::new(cache);
manager.add(SimpleLink::new(1, 3, 4, Color::from_rgb_u8(255, 0, 0)));
manager.add(SimpleLink::new(2, 3, 4, Color::from_rgb_u8(0, 255, 0)));
assert!(manager.remove(1));
assert_eq!(manager.len(), 1);
assert_eq!(manager.links()[0].id(), 2);
}
#[test]
fn test_remove_nonexistent_link() {
let cache = setup_cache();
let mut manager = LinkManager::new(cache);
manager.add(SimpleLink::new(1, 3, 4, Color::from_rgb_u8(255, 0, 0)));
assert!(!manager.remove(999));
assert_eq!(manager.len(), 1);
}
#[test]
fn test_clear() {
let cache = setup_cache();
let mut manager = LinkManager::new(cache);
manager.add(SimpleLink::new(1, 3, 4, Color::from_rgb_u8(255, 0, 0)));
manager.add(SimpleLink::new(2, 3, 4, Color::from_rgb_u8(0, 255, 0)));
manager.clear();
assert!(manager.is_empty());
}
#[test]
fn test_update_paths() {
let cache = setup_cache();
let mut manager = LinkManager::new(cache);
manager.add(SimpleLink::new(1, 3, 4, Color::from_rgb_u8(255, 0, 0)));
manager.update_paths(1.0);
let paths = manager.paths.borrow();
assert_eq!(paths.len(), 1);
assert_eq!(paths[0].id, 1);
assert!(paths[0].path_commands.starts_with("M "));
}
#[test]
fn test_update_paths_missing_pin() {
let cache = setup_cache();
let mut manager = LinkManager::new(cache);
manager.add(SimpleLink::new(1, 3, 999, Color::from_rgb_u8(255, 0, 0)));
manager.update_paths(1.0);
let paths = manager.paths.borrow();
assert!(paths.is_empty());
}
#[test]
fn test_find() {
let cache = setup_cache();
let mut manager = LinkManager::new(cache);
manager.add(SimpleLink::new(1, 3, 4, Color::from_rgb_u8(255, 0, 0)));
manager.add(SimpleLink::new(2, 3, 4, Color::from_rgb_u8(0, 255, 0)));
let link = manager.find(2);
assert!(link.is_some());
assert_eq!(link.unwrap().id(), 2);
assert!(manager.find(999).is_none());
}
#[test]
fn test_ids() {
let cache = setup_cache();
let mut manager = LinkManager::new(cache);
manager.add(SimpleLink::new(1, 3, 4, Color::from_rgb_u8(255, 0, 0)));
manager.add(SimpleLink::new(5, 3, 4, Color::from_rgb_u8(0, 255, 0)));
manager.add(SimpleLink::new(3, 3, 4, Color::from_rgb_u8(0, 0, 255)));
let ids: Vec<i32> = manager.ids().collect();
assert_eq!(ids, vec![1, 5, 3]);
}
#[test]
fn test_custom_link_type() {
#[derive(Clone)]
struct MyLink {
id: i32,
from: i32,
to: i32,
label: String,
}
impl LinkModel for MyLink {
fn id(&self) -> i32 {
self.id
}
fn start_pin_id(&self) -> i32 {
self.from
}
fn end_pin_id(&self) -> i32 {
self.to
}
fn color(&self) -> Color {
Color::from_rgb_u8(128, 128, 128)
}
}
let cache = setup_cache();
let mut manager = LinkManager::new(cache);
manager.add(MyLink {
id: 1,
from: 3,
to: 4,
label: "data flow".to_string(),
});
assert_eq!(manager.len(), 1);
assert_eq!(manager.links()[0].label, "data flow");
}
}