use std::sync::OnceLock;
use crate::core::error::{AexloError, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AppColor {
pub red: u16,
pub green: u16,
pub blue: u16,
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct AppPixelF {
pub alpha: f32,
pub red: f32,
pub green: f32,
pub blue: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct AppPoint {
pub h: i32,
pub v: i32,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct PersonalInfo {
pub name: String,
pub org: String,
pub serial: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FontInfo {
pub name: String,
pub font_num: i16,
pub size: i16,
pub style: i16,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ProgressId(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct AppPixel8 {
pub alpha: u8,
pub red: u8,
pub green: u8,
pub blue: u8,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct InfoLine {
pub left: String,
pub right: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[non_exhaustive]
pub enum TimeDisplayMode {
#[default]
Timecode,
Frames,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeDisplayPref {
pub mode: TimeDisplayMode,
pub timebase: i32,
pub frames_per_foot: i32,
pub frames_start: i32,
pub starting_frame: i32,
pub non_drop: bool,
pub honor_source_timecode: bool,
pub use_feet_frames: bool,
}
impl Default for TimeDisplayPref {
fn default() -> Self {
Self {
mode: TimeDisplayMode::Timecode,
timebase: 30,
frames_per_foot: 16,
frames_start: 0,
starting_frame: 0,
non_drop: true,
honor_source_timecode: true,
use_feet_frames: false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OverlayTheme {
pub foreground: AppPixelF,
pub shadow: AppPixelF,
pub stroke_width: f32,
pub vertex_size: f32,
pub shadow_offset: AppPoint,
}
impl Default for OverlayTheme {
fn default() -> Self {
Self {
foreground: AppPixelF {
alpha: 1.0,
red: 1.0,
green: 1.0,
blue: 1.0,
},
shadow: AppPixelF {
alpha: 0.5,
red: 0.0,
green: 0.0,
blue: 0.0,
},
stroke_width: 1.0,
vertex_size: 6.0,
shadow_offset: AppPoint { h: 1, v: 1 },
}
}
}
pub trait AppHost: Send + Sync {
fn bg_color(&self) -> AppColor {
HeadlessAppHost::GRAY
}
fn color(&self, color_type: i16) -> AppColor {
let _ = color_type;
self.bg_color()
}
fn language(&self) -> String {
String::new()
}
fn personal_info(&self) -> PersonalInfo {
PersonalInfo::default()
}
fn font_style_sheet(&self, sheet: i32) -> FontInfo {
let _ = sheet;
FontInfo::default()
}
fn set_cursor(&self, cursor: i32) {
let _ = cursor;
}
fn is_render_engine(&self) -> bool {
false
}
fn pick_color(&self, title: Option<&str>, sample: AppPixelF, use_ws_to_monitor_xform: bool) -> Option<AppPixelF> {
let _ = (title, use_ws_to_monitor_xform);
Some(sample)
}
fn mouse(&self) -> AppPoint {
AppPoint::default()
}
fn invalidate_rect(&self, context: usize, rect: Option<(i32, i32, i32, i32)>) {
let _ = (context, rect);
}
fn local_to_global(&self, local: AppPoint) -> AppPoint {
local
}
fn color_at_global_point(&self, global: AppPoint, eye_size: i16, mode: i16) -> AppPixelF {
let _ = (global, eye_size, mode);
AppPixelF::default()
}
fn progress_begin(&self, title: &str, cancel_label: Option<&str>, indeterminate: bool) -> ProgressId {
let _ = (title, cancel_label, indeterminate);
ProgressId(0)
}
fn progress_update(&self, id: ProgressId, count: i32, total: i32) -> bool {
let _ = (id, count, total);
true
}
fn progress_end(&self, id: ProgressId) {
let _ = id;
}
fn abort_requested(&self, effect: usize) -> bool {
let _ = effect;
false
}
fn render_progress(&self, effect: usize, current: i32, total: i32) -> bool {
let _ = (effect, current, total);
true
}
fn set_project_dirty(&self) {}
fn save_project(&self) {}
fn save_background_state(&self) {}
fn force_foreground(&self) {}
fn restore_background_state(&self) {}
fn refresh_all_windows(&self) {}
fn info_draw_text(&self, lines: &[InfoLine; 3]) {
let _ = lines;
}
fn info_draw_color(&self, color: AppPixel8) {
let _ = color;
}
fn append_info_text(&self, text: &str) {
let _ = text;
}
fn move_time_step(&self, effect: usize, steps: i32) {
let _ = (effect, steps);
}
fn touch_active_item(&self) {}
fn force_rerender(&self, effect: usize) {
let _ = effect;
}
fn effect_is_active_or_enabled(&self, context: usize) -> bool {
let _ = context;
true
}
fn time_display_pref(&self) -> TimeDisplayPref {
TimeDisplayPref::default()
}
#[allow(clippy::unnecessary_cast)] fn current_tool(&self) -> i32 {
after_effects_sys::PF_ExtendedSuiteTool_NONE as i32
}
fn set_current_tool(&self, tool: i32) {
let _ = tool;
}
fn parse_clipboard(&self) {}
fn overlay_theme(&self) -> OverlayTheme {
OverlayTheme::default()
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct HeadlessAppHost;
impl HeadlessAppHost {
pub const GRAY: AppColor = AppColor {
red: 0x8000,
green: 0x8000,
blue: 0x8000,
};
}
impl AppHost for HeadlessAppHost {}
static APP_HOST: OnceLock<Box<dyn AppHost>> = OnceLock::new();
#[derive(Debug, Clone, Copy)]
pub struct Host {
_fixed: (),
}
impl Host {
pub fn install(app: impl AppHost + 'static) -> Result<Self> {
APP_HOST.set(Box::new(app)).map_err(|_| AexloError::AppHostAlreadySet)?;
Ok(Self { _fixed: () })
}
pub fn get() -> Self {
APP_HOST.get_or_init(|| Box::new(HeadlessAppHost));
Self { _fixed: () }
}
}
#[cfg(test)]
thread_local! {
pub(crate) static TEST_APP_HOST: std::cell::RefCell<Option<Box<dyn AppHost>>> =
const { std::cell::RefCell::new(None) };
}
pub(crate) fn with_app_host<R>(f: impl FnOnce(&dyn AppHost) -> R) -> R {
#[cfg(test)]
if TEST_APP_HOST.with_borrow(Option::is_some) {
return TEST_APP_HOST.with_borrow(|h| f(h.as_deref().unwrap()));
}
f(APP_HOST.get_or_init(|| Box::new(HeadlessAppHost)).as_ref())
}
pub(crate) fn with_app_host_ffi(
f: impl FnOnce(&dyn AppHost) -> after_effects_sys::PF_Err,
) -> after_effects_sys::PF_Err {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| with_app_host(f))).unwrap_or_else(|_| {
log::error!("AppHost panicked; reporting PF_Err_INTERNAL_STRUCT_DAMAGED");
after_effects_sys::PF_Err_INTERNAL_STRUCT_DAMAGED as after_effects_sys::PF_Err
})
}