use crate::core::diagnostics::diag;
use crate::host::app::with_app_host_ffi as with_host;
use crate::sys::*;
#[allow(non_upper_case_globals)]
fn legacy_tool(tool: i32) -> PF_SuiteTool {
let legacy = match tool {
t if t == PF_ExtendedSuiteTool_ARROW => PF_SuiteTool_ARROW,
t if t == PF_ExtendedSuiteTool_ROTATE => PF_SuiteTool_ROTATE,
t if t == PF_ExtendedSuiteTool_PEN_NORMAL
|| t == PF_ExtendedSuiteTool_PEN_ADD_POINT
|| t == PF_ExtendedSuiteTool_PEN_DELETE_POINT
|| t == PF_ExtendedSuiteTool_PEN_CONVERT_POINT =>
{
PF_SuiteTool_PEN
}
t if t == PF_ExtendedSuiteTool_RECT || t == PF_ExtendedSuiteTool_OVAL => PF_SuiteTool_SHAPE,
t if t == PF_ExtendedSuiteTool_PAN_BEHIND => PF_SuiteTool_PAN,
t if t == PF_ExtendedSuiteTool_HAND => PF_SuiteTool_HAND,
t if t == PF_ExtendedSuiteTool_MAGNIFY => PF_SuiteTool_MAGNIFY,
t if t == PF_ExtendedSuiteTool_ROUNDED_RECT => PF_SuiteTool_ROUNDED_RECT,
t if t == PF_ExtendedSuiteTool_POLYGON => PF_SuiteTool_POLYGON,
t if t == PF_ExtendedSuiteTool_STAR => PF_SuiteTool_STAR,
t if t == PF_ExtendedSuiteTool_PIN => PF_SuiteTool_PIN,
t if t == PF_ExtendedSuiteTool_PIN_STARCH => PF_SuiteTool_PIN_STARCH,
t if t == PF_ExtendedSuiteTool_PIN_DEPTH => PF_SuiteTool_PIN_DEPTH,
_ => PF_SuiteTool_NONE,
};
legacy as PF_SuiteTool
}
unsafe extern "C" fn get_current_tool(toolP: *mut PF_SuiteTool) -> PF_Err {
crate::core::ffi::guard("get_current_tool", || {
diag!("PF_HelperSuite1/PF_GetCurrentTool",
"toolP" => format!("{:#x}", toolP as usize),
);
if toolP.is_null() {
return PF_Err_BAD_CALLBACK_PARAM;
}
with_host(|host| {
unsafe { *toolP = legacy_tool(host.current_tool()) };
PF_Err_NONE
})
})
}
unsafe extern "C" fn parse_clipboard() -> PF_Err {
crate::core::ffi::guard("parse_clipboard", || {
diag!("PF_HelperSuite2/PF_ParseClipboard");
with_host(|host| {
host.parse_clipboard();
PF_Err_NONE
})
})
}
unsafe extern "C" fn set_current_extended_tool(tool: PF_ExtendedSuiteTool) -> PF_Err {
crate::core::ffi::guard("set_current_extended_tool", || {
diag!("PF_HelperSuite2/PF_SetCurrentExtendedTool",
"tool" => tool,
);
with_host(|host| {
host.set_current_tool(tool.long());
PF_Err_NONE
})
})
}
unsafe extern "C" fn get_current_extended_tool(tool: *mut PF_ExtendedSuiteTool) -> PF_Err {
crate::core::ffi::guard("get_current_extended_tool", || {
diag!("PF_HelperSuite2/PF_GetCurrentExtendedTool",
"tool" => format!("{:#x}", tool as usize),
);
if tool.is_null() {
return PF_Err_BAD_CALLBACK_PARAM;
}
with_host(|host| {
unsafe { *tool = host.current_tool() as PF_ExtendedSuiteTool };
PF_Err_NONE
})
})
}
pub(super) const fn create_helper_suite_1() -> PF_HelperSuite1 {
PF_HelperSuite1 {
PF_GetCurrentTool: Some(get_current_tool),
}
}
pub(super) const fn create_helper_suite_2() -> PF_HelperSuite2 {
PF_HelperSuite2 {
PF_ParseClipboard: Some(parse_clipboard),
PF_SetCurrentExtendedTool: Some(set_current_extended_tool),
PF_GetCurrentExtendedTool: Some(get_current_extended_tool),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::host::app::{AppHost, TEST_APP_HOST};
struct PenHost;
impl AppHost for PenHost {
fn current_tool(&self) -> i32 {
PF_ExtendedSuiteTool_PEN_ADD_POINT
}
}
#[test]
fn tools_come_from_host() {
TEST_APP_HOST.set(Some(Box::new(PenHost)));
let (mut legacy, mut extended) = (0 as PF_SuiteTool, 0 as PF_ExtendedSuiteTool);
unsafe {
create_helper_suite_1().PF_GetCurrentTool.unwrap()(&mut legacy);
create_helper_suite_2().PF_GetCurrentExtendedTool.unwrap()(&mut extended);
}
TEST_APP_HOST.set(None);
assert_eq!(legacy.long(), PF_SuiteTool_PEN);
assert_eq!(extended.long(), PF_ExtendedSuiteTool_PEN_ADD_POINT);
}
}