A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.
TypeScript animationcartoonclip-studio-paintcollaborativedesktop-appdigital-paintingdrawing-appillustrationmangamultiplayeropen-sourcerusttaurithreejswebglwebtoon
//! Thread-safe physical-pixel hit-test region and UI occlusion mask.
//!
//! Shared between the native input hooks (`tablet_input.rs`, Win32 nav hook) and
//! `ink-engine` so pointer-down routing happens in-process with zero IPC latency.
use crate::types::{InteractionPolicy, UiRect, ViewportRect};
use std::sync::{Arc, RwLock};
#[derive(Debug, Clone, Default)]
pub struct HitTestState {
pub viewport: ViewportRect,
pub occluded_rects: Vec<UiRect>,
pub visible: bool,
pub policy: InteractionPolicy,
}
impl HitTestState {
/// Returns `true` if `(phys_x, phys_y)` lands inside the visible canvas viewport
/// and is NOT covered by any occluded UI element (modal, popover, floating panel,
/// `FloatingBrushPill`, context menu, etc.).
#[inline]
pub fn is_unoccluded_canvas_point(&self, phys_x: f32, phys_y: f32) -> bool {
if !self.visible || self.policy.modal_open || self.policy.workspace_interacting {
return false;
}
if !self.viewport.contains_phys(phys_x, phys_y) {
return false;
}
for rect in &self.occluded_rects {
if rect.contains(phys_x, phys_y) {
return false;
}
}
true
}
/// Returns `true` if a stylus/pointer-down at `(phys_x, phys_y)` should be captured
/// natively by `ink-engine` for zero-IPC brush/eraser rendering.
#[inline]
pub fn should_capture_native_stroke(&self, phys_x: f32, phys_y: f32) -> bool {
self.policy.native_stroke_enabled
&& self.policy.active_tool.is_native_stroke_tool()
&& self.is_unoccluded_canvas_point(phys_x, phys_y)
}
/// Returns `true` if a navigation wheel/drag event at `(phys_x, phys_y)` should
/// drive native 120–240 Hz GPU camera navigation directly in Rust.
#[inline]
pub fn should_capture_native_nav(&self, phys_x: f32, phys_y: f32) -> bool {
!self.policy.text_input_focused && self.is_unoccluded_canvas_point(phys_x, phys_y)
}
}
/// Shared concurrent snapshot of viewport bounds, occlusion rects, and interaction policy.
#[derive(Debug, Clone, Default)]
pub struct SharedHitTest {
inner: Arc<RwLock<HitTestState>>,
}
impl SharedHitTest {
pub fn new() -> Self {
Self::default()
}
pub fn set_viewport(&self, viewport: ViewportRect) {
if let Ok(mut guard) = self.inner.write() {
guard.viewport = viewport;
}
}
pub fn set_occluded_rects(&self, rects: Vec<UiRect>) {
if let Ok(mut guard) = self.inner.write() {
guard.occluded_rects = rects;
}
}
pub fn set_visible(&self, visible: bool) {
if let Ok(mut guard) = self.inner.write() {
guard.visible = visible;
}
}
pub fn set_policy(&self, policy: InteractionPolicy) {
if let Ok(mut guard) = self.inner.write() {
guard.policy = policy;
}
}
pub fn snapshot(&self) -> HitTestState {
self.inner
.read()
.map(|g| g.clone())
.unwrap_or_default()
}
#[inline]
pub fn is_unoccluded_canvas_point(&self, phys_x: f32, phys_y: f32) -> bool {
self.inner
.read()
.map(|g| g.is_unoccluded_canvas_point(phys_x, phys_y))
.unwrap_or(false)
}
#[inline]
pub fn should_capture_native_stroke(&self, phys_x: f32, phys_y: f32) -> bool {
self.inner
.read()
.map(|g| g.should_capture_native_stroke(phys_x, phys_y))
.unwrap_or(false)
}
#[inline]
pub fn should_capture_native_nav(&self, phys_x: f32, phys_y: f32) -> bool {
self.inner
.read()
.map(|g| g.should_capture_native_nav(phys_x, phys_y))
.unwrap_or(false)
}
#[inline]
pub fn wheel_mode(&self) -> crate::types::WheelNavMode {
self.inner
.read()
.map(|g| g.policy.wheel_mode)
.unwrap_or_default()
}
#[inline]
pub fn text_input_focused(&self) -> bool {
self.inner
.read()
.map(|g| g.policy.text_input_focused)
.unwrap_or(false)
}
/// Non-blocking hit-test helpers for low-level Win32 hooks (`WH_MOUSE_LL`) that must
/// return within microseconds and never contend on a write lock.
#[inline]
pub fn try_is_unoccluded_canvas_point(&self, phys_x: f32, phys_y: f32) -> bool {
self.inner
.try_read()
.map(|g| g.is_unoccluded_canvas_point(phys_x, phys_y))
.unwrap_or(false)
}
#[inline]
pub fn try_should_capture_native_stroke(&self, phys_x: f32, phys_y: f32) -> bool {
self.inner
.try_read()
.map(|g| g.should_capture_native_stroke(phys_x, phys_y))
.unwrap_or(false)
}
/// Single non-blocking `try_read()` returning `(is_unoccluded_canvas, should_capture_native_stroke)`
/// for high-frequency WinTab / Windows Ink hover & routing hot paths.
#[inline]
pub fn try_classify_canvas_point(&self, phys_x: f32, phys_y: f32) -> (bool, bool) {
self.inner
.try_read()
.map(|g| {
let unoccluded = g.is_unoccluded_canvas_point(phys_x, phys_y);
let capture = unoccluded
&& g.policy.native_stroke_enabled
&& g.policy.active_tool.is_native_stroke_tool();
(unoccluded, capture)
})
.unwrap_or((false, false))
}
#[inline]
pub fn try_should_capture_native_nav(&self, phys_x: f32, phys_y: f32) -> bool {
self.inner
.try_read()
.map(|g| g.should_capture_native_nav(phys_x, phys_y))
.unwrap_or(false)
}
#[inline]
pub fn try_wheel_mode(&self) -> crate::types::WheelNavMode {
self.inner
.try_read()
.map(|g| g.policy.wheel_mode)
.unwrap_or_default()
}
#[inline]
pub fn try_text_input_focused(&self) -> bool {
self.inner
.try_read()
.map(|g| g.policy.text_input_focused)
.unwrap_or(false)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::NativeToolKind;
#[test]
fn test_occlusion_mask_blocks_floating_ui_and_allows_canvas() {
let ht = SharedHitTest::new();
ht.set_visible(true);
ht.set_viewport(ViewportRect {
x_px: 200,
y_px: 80,
width_px: 1000,
height_px: 700,
dpr: 1.5,
});
// FloatingBrushPill on left edge of canvas viewport
ht.set_occluded_rects(vec![UiRect {
x: 212.0,
y: 280.0,
width: 48.0,
height: 260.0,
}]);
// Inside unoccluded canvas -> allowed
assert!(ht.should_capture_native_stroke(600.0, 400.0));
// Inside FloatingBrushPill -> blocked
assert!(!ht.should_capture_native_stroke(230.0, 350.0));
// Outside viewport (left dock) -> blocked
assert!(!ht.should_capture_native_stroke(100.0, 400.0));
// Non-native tool (e.g. text/pen) -> stroke blocked, nav still allowed
ht.set_policy(InteractionPolicy {
active_tool: NativeToolKind::Other,
native_stroke_enabled: true,
..Default::default()
});
assert!(!ht.should_capture_native_stroke(600.0, 400.0));
assert!(ht.should_capture_native_nav(600.0, 400.0));
// Modal open -> both blocked
ht.set_policy(InteractionPolicy {
active_tool: NativeToolKind::Brush,
native_stroke_enabled: true,
modal_open: true,
..Default::default()
});
assert!(!ht.should_capture_native_stroke(600.0, 400.0));
assert!(!ht.should_capture_native_nav(600.0, 400.0));
}
}