Merge pull request #6035 from penpot/elenatorro-10314-allow-mutable-static-only-on-state

🔧 Use with_state and with_current_state macros allowing static…
This commit is contained in:
Elena Torró 2025-03-10 12:42:47 +01:00 committed by GitHub
commit d74bfd834d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 23 deletions

View file

@ -24,16 +24,26 @@ extern "C" {
) -> *const ::std::os::raw::c_void;
}
#[macro_export]
macro_rules! with_state {
($state:ident, $block:block) => {
let $state = unsafe { STATE.as_mut() }.expect("Got an invalid state pointer");
let $state = unsafe {
#[allow(static_mut_refs)]
STATE.as_mut()
}
.expect("Got an invalid state pointer");
$block
};
}
#[macro_export]
macro_rules! with_current_shape {
($state:ident, |$shape:ident: &mut Shape| $block:block) => {
let $state = unsafe { STATE.as_mut() }.expect("Got an invalid state pointer");
let $state = unsafe {
#[allow(static_mut_refs)]
STATE.as_mut()
}
.expect("Got an invalid state pointer");
if let Some($shape) = $state.current_shape() {
$block
}
@ -601,17 +611,17 @@ pub extern "C" fn propagate_modifiers() -> *mut u8 {
.map(|data| TransformEntry::from_bytes(data.try_into().unwrap()))
.collect();
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
let result = shapes::propagate_modifiers(state, entries);
mem::write_vec(result)
with_state!(state, {
let result = shapes::propagate_modifiers(state, entries);
return mem::write_vec(result);
});
}
#[no_mangle]
pub extern "C" fn clean_modifiers() {
if let Some(state) = unsafe { STATE.as_mut() } {
with_state!(state, {
state.modifiers.clear();
}
});
}
#[no_mangle]
@ -623,12 +633,12 @@ pub extern "C" fn set_modifiers() {
.map(|data| TransformEntry::from_bytes(data.try_into().unwrap()))
.collect();
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
for entry in entries {
state.modifiers.insert(entry.id, entry.transform);
}
state.render_state().clear_cache();
with_state!(state, {
for entry in entries {
state.modifiers.insert(entry.id, entry.transform);
}
state.render_state().clear_cache();
});
}
#[no_mangle]

View file

@ -1,23 +1,22 @@
use crate::mem;
use crate::with_current_shape;
use crate::STATE;
#[no_mangle]
pub extern "C" fn clear_shape_text() {
let state = unsafe { STATE.as_mut() }.expect("Got an invalid state pointer");
if let Some(shape) = state.current_shape() {
with_current_shape!(state, |shape: &mut Shape| {
shape.clear_text();
}
});
}
#[no_mangle]
pub extern "C" fn add_text_paragraph() {
let state = unsafe { STATE.as_mut() }.expect("Got an invalid state pointer");
if let Some(shape) = state.current_shape() {
with_current_shape!(state, |shape: &mut Shape| {
let res = shape.add_text_paragraph();
if let Err(err) = res {
eprintln!("{}", err);
}
}
});
}
#[no_mangle]
@ -27,11 +26,10 @@ pub extern "C" fn add_text_leaf() {
String::from_utf8_unchecked(bytes) // TODO: handle this error
};
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
if let Some(shape) = state.current_shape() {
with_current_shape!(state, |shape: &mut Shape| {
let res = shape.add_text_leaf(&text);
if let Err(err) = res {
eprintln!("{}", err);
}
}
});
}