mirror of
https://github.com/penpot/penpot.git
synced 2025-05-05 00:15:52 +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;
|
) -> *const ::std::os::raw::c_void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
macro_rules! with_state {
|
macro_rules! with_state {
|
||||||
($state:ident, $block:block) => {
|
($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
|
$block
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
macro_rules! with_current_shape {
|
macro_rules! with_current_shape {
|
||||||
($state:ident, |$shape:ident: &mut Shape| $block:block) => {
|
($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() {
|
if let Some($shape) = $state.current_shape() {
|
||||||
$block
|
$block
|
||||||
}
|
}
|
||||||
|
@ -601,17 +611,17 @@ pub extern "C" fn propagate_modifiers() -> *mut u8 {
|
||||||
.map(|data| TransformEntry::from_bytes(data.try_into().unwrap()))
|
.map(|data| TransformEntry::from_bytes(data.try_into().unwrap()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
with_state!(state, {
|
||||||
let result = shapes::propagate_modifiers(state, entries);
|
let result = shapes::propagate_modifiers(state, entries);
|
||||||
|
return mem::write_vec(result);
|
||||||
mem::write_vec(result)
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn clean_modifiers() {
|
pub extern "C" fn clean_modifiers() {
|
||||||
if let Some(state) = unsafe { STATE.as_mut() } {
|
with_state!(state, {
|
||||||
state.modifiers.clear();
|
state.modifiers.clear();
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -623,12 +633,12 @@ pub extern "C" fn set_modifiers() {
|
||||||
.map(|data| TransformEntry::from_bytes(data.try_into().unwrap()))
|
.map(|data| TransformEntry::from_bytes(data.try_into().unwrap()))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
with_state!(state, {
|
||||||
|
for entry in entries {
|
||||||
for entry in entries {
|
state.modifiers.insert(entry.id, entry.transform);
|
||||||
state.modifiers.insert(entry.id, entry.transform);
|
}
|
||||||
}
|
state.render_state().clear_cache();
|
||||||
state.render_state().clear_cache();
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
|
|
@ -1,23 +1,22 @@
|
||||||
use crate::mem;
|
use crate::mem;
|
||||||
|
use crate::with_current_shape;
|
||||||
use crate::STATE;
|
use crate::STATE;
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn clear_shape_text() {
|
pub extern "C" fn clear_shape_text() {
|
||||||
let state = unsafe { STATE.as_mut() }.expect("Got an invalid state pointer");
|
with_current_shape!(state, |shape: &mut Shape| {
|
||||||
if let Some(shape) = state.current_shape() {
|
|
||||||
shape.clear_text();
|
shape.clear_text();
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn add_text_paragraph() {
|
pub extern "C" fn add_text_paragraph() {
|
||||||
let state = unsafe { STATE.as_mut() }.expect("Got an invalid state pointer");
|
with_current_shape!(state, |shape: &mut Shape| {
|
||||||
if let Some(shape) = state.current_shape() {
|
|
||||||
let res = shape.add_text_paragraph();
|
let res = shape.add_text_paragraph();
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
eprintln!("{}", err);
|
eprintln!("{}", err);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
|
@ -27,11 +26,10 @@ pub extern "C" fn add_text_leaf() {
|
||||||
String::from_utf8_unchecked(bytes) // TODO: handle this error
|
String::from_utf8_unchecked(bytes) // TODO: handle this error
|
||||||
};
|
};
|
||||||
|
|
||||||
let state = unsafe { STATE.as_mut() }.expect("got an invalid state pointer");
|
with_current_shape!(state, |shape: &mut Shape| {
|
||||||
if let Some(shape) = state.current_shape() {
|
|
||||||
let res = shape.add_text_leaf(&text);
|
let res = shape.add_text_leaf(&text);
|
||||||
if let Err(err) = res {
|
if let Err(err) = res {
|
||||||
eprintln!("{}", err);
|
eprintln!("{}", err);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue