mirror of
https://github.com/penpot/penpot.git
synced 2025-05-03 06:05:55 +02:00
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:
commit
d74bfd834d
2 changed files with 31 additions and 23 deletions
|
@ -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]
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue