use crate::ferro_view::FerroView;
use crate::i_init_editor_info::EditorInfo;
use crate::interop::java::{
call_static_void, new_object, new_string_array, JavaClass, JavaLocal, JavaObject, JavaRef, JavaValue,
};
use crate::interop::natives::{FERRO_INPUT_CONNECTION, PLATFORM_HELPER};
use crate::platform::input::text_edit_buffer::IInputConnectionTopLevel;
use std::rc::Rc;
use crate::platform::specific::helpers::android_keyboard_events_helper::KeyEventData;
use crate::platform::specific::helpers::android_motion_events_helper::MotionEventData;
const RESULT_NONE: i32 = 0;
const RESULT_FALSE: i32 = 1;
const RESULT_TRUE: i32 = 2;
const CALL_BASE: i32 = 4;
impl FerroView {
pub(crate) fn dispatch_motion_event(&self, e: Option<&MotionEventData>) -> i32 {
let mut call_base = true;
let result = self.top_level_impl().pointer_helper().dispatch_motion_event(e, &mut call_base);
let result = match result {
None => RESULT_NONE,
Some(false) => RESULT_FALSE,
Some(true) => RESULT_TRUE,
};
result | if call_base { CALL_BASE } else { 0 }
}
pub(crate) fn dispatch_key_event(&self, e: Option<&KeyEventData>) -> i32 {
let mut call_base = true;
let res = self.top_level_impl().keyboard_helper().dispatch_key_event(e, &mut call_base);
if res == Some(false) {
let handled = e.is_some_and(|e| {
self.access_helper().dispatch_key_event(
e.action,
e.key_code,
!e.is_ctrl_pressed && !e.is_shift_pressed,
e.repeat_count,
)
});
call_base = !handled && call_base;
}
let result = match res {
None => RESULT_NONE,
Some(false) => RESULT_FALSE,
Some(true) => RESULT_TRUE,
};
result | if call_base { CALL_BASE } else { 0 }
}
pub(crate) fn dispatch_access_hover_event(&self, action: i32, x: f32, y: f32) -> bool {
self.access_helper().dispatch_hover_event(action, x, y)
}
pub(crate) fn on_focus_changed(&self, gain_focus: bool) {
self.access_helper().on_focus_changed(gain_focus);
}
pub(crate) fn on_create_input_connection(&self, out_attrs: &JavaObject) -> Option<JavaLocal> {
let init = self.top_level_impl().editor_info_init()?;
let top_level: Rc<dyn IInputConnectionTopLevel> = self.top_level_impl().clone();
let mut editor_info = EditorInfo::default();
let input_connection = init(&top_level, &mut editor_info)?;
let hint_locales = editor_info.hint_locales.as_deref().map(new_string_array);
call_static_void(
&JavaClass::find(PLATFORM_HELPER),
"setEditorInfo",
"(Landroid/view/inputmethod/EditorInfo;III[Ljava/lang/String;)V",
&[
JavaValue::Object(Some(out_attrs)),
JavaValue::Int(editor_info.input_type),
JavaValue::Int(editor_info.ime_options),
JavaValue::Int(editor_info.initial_caps_mode),
JavaValue::Object(hint_locales.as_ref().map(|locales| locales as &dyn JavaRef)),
],
);
Some(new_object(
&JavaClass::find(FERRO_INPUT_CONNECTION),
"(J)V",
&[JavaValue::Long(input_connection.register())],
))
}
}