Skip to content

Commit 5efa81d

Browse files
authored
Store click targets in Arc (#3726)
1 parent 1a9f0a5 commit 5efa81d

File tree

5 files changed

+29
-18
lines changed

5 files changed

+29
-18
lines changed

editor/src/messages/portfolio/document/document_message.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::path::PathBuf;
2+
use std::sync::Arc;
23

34
use super::utility_types::misc::{GroupFolderType, SnappingState};
45
use crate::messages::input_mapper::utility_types::input_keyboard::Key;
@@ -203,7 +204,7 @@ pub enum DocumentMessage {
203204
first_element_source_id: HashMap<NodeId, Option<NodeId>>,
204205
},
205206
UpdateClickTargets {
206-
click_targets: HashMap<NodeId, Vec<ClickTarget>>,
207+
click_targets: HashMap<NodeId, Vec<Arc<ClickTarget>>>,
207208
},
208209
UpdateClipTargets {
209210
clip_targets: HashSet<NodeId>,

editor/src/messages/portfolio/document/document_message_handler.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use graphene_std::vector::misc::{dvec2_to_point, point_to_dvec2};
4242
use graphene_std::vector::style::RenderMode;
4343
use kurbo::{Affine, CubicBez, Line, ParamCurve, PathSeg, QuadBez};
4444
use std::path::PathBuf;
45+
use std::sync::Arc;
4546
use std::time::Duration;
4647

4748
#[derive(ExtractField)]
@@ -3071,7 +3072,14 @@ impl<'a> ClickXRayIter<'a> {
30713072
}
30723073

30733074
/// Handles the checking of the layer where the target is a rect or path
3074-
fn check_layer_area_target(&mut self, click_targets: Option<&Vec<ClickTarget>>, clip: bool, layer: LayerNodeIdentifier, path: Vec<path_bool_lib::PathSegment>, transform: DAffine2) -> XRayResult {
3075+
fn check_layer_area_target(
3076+
&mut self,
3077+
click_targets: Option<&[Arc<ClickTarget>]>,
3078+
clip: bool,
3079+
layer: LayerNodeIdentifier,
3080+
path: Vec<path_bool_lib::PathSegment>,
3081+
transform: DAffine2,
3082+
) -> XRayResult {
30753083
// Convert back to Kurbo types for intersections
30763084
let segment = |bezier: &path_bool_lib::PathSegment| match *bezier {
30773085
path_bool_lib::PathSegment::Line(start, end) => PathSeg::Line(Line::new(dvec2_to_point(start), dvec2_to_point(end))),
@@ -3088,7 +3096,7 @@ impl<'a> ClickXRayIter<'a> {
30883096
// In the case of a clip path where the area partially intersects, it is necessary to do a boolean operation.
30893097
// We do this on this using the target area to reduce computation (as the target area is usually very simple).
30903098
if clip && intersects {
3091-
let clip_path = click_targets_to_path_lib_segments(click_targets.iter().flat_map(|x| x.iter()), transform);
3099+
let clip_path = click_targets_to_path_lib_segments(click_targets.iter().flat_map(|x| x.iter()).map(|x| x.as_ref()), transform);
30923100
let subtracted = boolean_intersect(path, clip_path).into_iter().flatten().collect::<Vec<_>>();
30933101
if subtracted.is_empty() {
30943102
use_children = false;

editor/src/messages/portfolio/document/utility_types/document_metadata.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use graphene_std::vector::click_target::{ClickTarget, ClickTargetType};
1313
use graphene_std::vector::{PointId, Vector};
1414
use std::collections::{HashMap, HashSet};
1515
use std::num::NonZeroU64;
16+
use std::sync::Arc;
1617

1718
// ================
1819
// DocumentMetadata
@@ -26,7 +27,7 @@ pub struct DocumentMetadata {
2627
pub local_transforms: HashMap<NodeId, DAffine2>,
2728
pub first_element_source_ids: HashMap<NodeId, Option<NodeId>>,
2829
pub structure: HashMap<LayerNodeIdentifier, NodeRelations>,
29-
pub click_targets: HashMap<LayerNodeIdentifier, Vec<ClickTarget>>,
30+
pub click_targets: HashMap<LayerNodeIdentifier, Vec<Arc<ClickTarget>>>,
3031
pub clip_targets: HashSet<NodeId>,
3132
pub vector_modify: HashMap<NodeId, Vector>,
3233
/// Transform from document space to viewport space.
@@ -46,8 +47,8 @@ impl DocumentMetadata {
4647
self.structure.contains_key(&layer)
4748
}
4849

49-
pub fn click_targets(&self, layer: LayerNodeIdentifier) -> Option<&Vec<ClickTarget>> {
50-
self.click_targets.get(&layer)
50+
pub fn click_targets(&self, layer: LayerNodeIdentifier) -> Option<&[Arc<ClickTarget>]> {
51+
self.click_targets.get(&layer).map(|x| x.as_slice())
5152
}
5253

5354
/// Access the [`NodeRelations`] of a layer.
@@ -206,7 +207,7 @@ impl DocumentMetadata {
206207
}
207208

208209
pub fn layer_outline(&self, layer: LayerNodeIdentifier) -> impl Iterator<Item = &subpath::Subpath<PointId>> {
209-
static EMPTY: Vec<ClickTarget> = Vec::new();
210+
static EMPTY: Vec<Arc<ClickTarget>> = Vec::new();
210211
let click_targets = self.click_targets.get(&layer).unwrap_or(&EMPTY);
211212
click_targets.iter().filter_map(|target| match target.target_type() {
212213
ClickTargetType::Subpath(subpath) => Some(subpath),
@@ -215,7 +216,7 @@ impl DocumentMetadata {
215216
}
216217

217218
pub fn layer_with_free_points_outline(&self, layer: LayerNodeIdentifier) -> impl Iterator<Item = &ClickTargetType> {
218-
static EMPTY: Vec<ClickTarget> = Vec::new();
219+
static EMPTY: Vec<Arc<ClickTarget>> = Vec::new();
219220
let click_targets = self.click_targets.get(&layer).unwrap_or(&EMPTY);
220221
click_targets.iter().map(|target| target.target_type())
221222
}

editor/src/messages/portfolio/document/utility_types/network_interface.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use serde_json::{Value, json};
3131
use std::collections::{HashMap, HashSet, VecDeque};
3232
use std::hash::Hash;
3333
use std::ops::Deref;
34+
use std::sync::Arc;
3435

3536
/// All network modifications should be done through this API, so the fields cannot be public. However, all fields within this struct can be public since it it not possible to have a public mutable reference.
3637
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
@@ -3078,7 +3079,7 @@ impl NodeNetworkInterface {
30783079
self.document_metadata
30793080
.click_targets
30803081
.get(&layer)
3081-
.map(|click| click.iter().map(ClickTarget::target_type))
3082+
.map(|click| click.iter().map(|x| x.target_type()))
30823083
.map(|target_types| Vector::from_target_types(target_types, true))
30833084
}
30843085

@@ -3180,7 +3181,7 @@ impl NodeNetworkInterface {
31803181
}
31813182

31823183
/// Update the cached click targets of the layers
3183-
pub fn update_click_targets(&mut self, new_click_targets: HashMap<LayerNodeIdentifier, Vec<ClickTarget>>) {
3184+
pub fn update_click_targets(&mut self, new_click_targets: HashMap<LayerNodeIdentifier, Vec<Arc<ClickTarget>>>) {
31843185
self.document_metadata.click_targets = new_click_targets;
31853186
}
31863187

node-graph/libraries/rendering/src/renderer.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub struct RenderMetadata {
247247
pub upstream_footprints: HashMap<NodeId, Footprint>,
248248
pub local_transforms: HashMap<NodeId, DAffine2>,
249249
pub first_element_source_id: HashMap<NodeId, Option<NodeId>>,
250-
pub click_targets: HashMap<NodeId, Vec<ClickTarget>>,
250+
pub click_targets: HashMap<NodeId, Vec<Arc<ClickTarget>>>,
251251
pub clip_targets: HashSet<NodeId>,
252252
}
253253

@@ -471,7 +471,7 @@ impl Render for Artboard {
471471
fn collect_metadata(&self, metadata: &mut RenderMetadata, mut footprint: Footprint, element_id: Option<NodeId>) {
472472
if let Some(element_id) = element_id {
473473
let subpath = Subpath::new_rectangle(DVec2::ZERO, self.dimensions.as_dvec2());
474-
metadata.click_targets.insert(element_id, vec![ClickTarget::new_with_subpath(subpath, 0.)]);
474+
metadata.click_targets.insert(element_id, vec![ClickTarget::new_with_subpath(subpath, 0.).into()]);
475475
metadata.upstream_footprints.insert(element_id, footprint);
476476
metadata.local_transforms.insert(element_id, DAffine2::from_translation(self.location.as_dvec2()));
477477
if self.clip {
@@ -666,7 +666,7 @@ impl Render for Table<Graphic> {
666666
all_upstream_click_targets.extend(new_click_targets);
667667
}
668668

669-
metadata.click_targets.insert(element_id, all_upstream_click_targets);
669+
metadata.click_targets.insert(element_id, all_upstream_click_targets.into_iter().map(|x| x.into()).collect());
670670
}
671671
}
672672

@@ -1173,7 +1173,7 @@ impl Render for Table<Vector> {
11731173
let anchor = vector.point_domain.position_from_id(point_id).unwrap_or_default();
11741174
let point = FreePoint::new(point_id, anchor);
11751175

1176-
Some(ClickTarget::new_with_free_point(point))
1176+
Some(ClickTarget::new_with_free_point(point).into())
11771177
} else {
11781178
None
11791179
}
@@ -1182,9 +1182,9 @@ impl Render for Table<Vector> {
11821182
let click_targets = vector
11831183
.stroke_bezier_paths()
11841184
.map(fill)
1185-
.map(|subpath| ClickTarget::new_with_subpath(subpath, stroke_width))
1185+
.map(|subpath| ClickTarget::new_with_subpath(subpath, stroke_width).into())
11861186
.chain(single_anchors_targets.into_iter())
1187-
.collect::<Vec<ClickTarget>>();
1187+
.collect::<Vec<_>>();
11881188

11891189
metadata.click_targets.entry(element_id).or_insert(click_targets);
11901190
}
@@ -1366,7 +1366,7 @@ impl Render for Table<Raster<CPU>> {
13661366
let Some(element_id) = element_id else { return };
13671367
let subpath = Subpath::new_rectangle(DVec2::ZERO, DVec2::ONE);
13681368

1369-
metadata.click_targets.insert(element_id, vec![ClickTarget::new_with_subpath(subpath, 0.)]);
1369+
metadata.click_targets.insert(element_id, vec![ClickTarget::new_with_subpath(subpath, 0.).into()]);
13701370
metadata.upstream_footprints.insert(element_id, footprint);
13711371
// TODO: Find a way to handle more than one row of the raster table
13721372
if let Some(raster) = self.iter().next() {
@@ -1426,7 +1426,7 @@ impl Render for Table<Raster<GPU>> {
14261426
let Some(element_id) = element_id else { return };
14271427
let subpath = Subpath::new_rectangle(DVec2::ZERO, DVec2::ONE);
14281428

1429-
metadata.click_targets.insert(element_id, vec![ClickTarget::new_with_subpath(subpath, 0.)]);
1429+
metadata.click_targets.insert(element_id, vec![ClickTarget::new_with_subpath(subpath, 0.).into()]);
14301430
metadata.upstream_footprints.insert(element_id, footprint);
14311431
// TODO: Find a way to handle more than one row of the raster table
14321432
if let Some(raster) = self.iter().next() {

0 commit comments

Comments
 (0)