use crate::{
Matrix4, Rect, ScreenId, Size, SystemMouseCursorKind, TextEditingValue, TextInputConfiguration,
};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ViewId(pub u64);
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ViewFocusEvent {
pub view_id: ViewId,
pub state: ViewFocusState,
pub direction: ViewFocusDirection,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ViewFocusState {
Unfocused,
Focused,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ViewFocusDirection {
Undefined,
Forward,
Backward,
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ViewPadding {
pub left: f64,
pub top: f64,
pub right: f64,
pub bottom: f64,
}
impl ViewPadding {
pub const ZERO: ViewPadding = ViewPadding {
left: 0.0,
top: 0.0,
right: 0.0,
bottom: 0.0,
};
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ViewConstraints {
pub min_width: f64,
pub max_width: f64,
pub min_height: f64,
pub max_height: f64,
}
impl ViewConstraints {
pub const fn new(
min_width: f64,
max_width: f64,
min_height: f64,
max_height: f64,
) -> ViewConstraints {
ViewConstraints {
min_width,
max_width,
min_height,
max_height,
}
}
pub const fn tight(width: f64, height: f64) -> ViewConstraints {
ViewConstraints {
min_width: width,
max_width: width,
min_height: height,
max_height: height,
}
}
}
impl Default for ViewConstraints {
fn default() -> ViewConstraints {
ViewConstraints {
min_width: 0.0,
max_width: f64::INFINITY,
min_height: 0.0,
max_height: f64::INFINITY,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ViewMetrics {
pub physical_size: [f64; 2],
pub physical_constraints: ViewConstraints,
pub device_pixel_ratio: f64,
pub padding: ViewPadding,
pub view_padding: ViewPadding,
pub view_insets: ViewPadding,
pub screen: Option<ScreenId>,
}
impl Default for ViewMetrics {
fn default() -> ViewMetrics {
ViewMetrics {
physical_size: [0.0, 0.0],
physical_constraints: ViewConstraints::tight(0.0, 0.0),
device_pixel_ratio: 1.0,
padding: ViewPadding::ZERO,
view_padding: ViewPadding::ZERO,
view_insets: ViewPadding::ZERO,
screen: None,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct GestureSettings {
pub physical_touch_slop: Option<f64>,
pub physical_double_tap_slop: Option<f64>,
}
impl GestureSettings {
pub fn copy_with(
self,
physical_touch_slop: Option<f64>,
physical_double_tap_slop: Option<f64>,
) -> GestureSettings {
GestureSettings {
physical_touch_slop: physical_touch_slop.or(self.physical_touch_slop),
physical_double_tap_slop: physical_double_tap_slop.or(self.physical_double_tap_slop),
}
}
}
pub trait View: 'static {
fn id(&self) -> ViewId;
fn metrics(&self) -> ViewMetrics;
fn gesture_settings(&self) -> GestureSettings {
GestureSettings::default()
}
fn present(&self, scene: std::sync::Arc<crate::Scene>);
fn host(&self) -> &dyn ViewHost;
fn initial_data(&self) -> Option<String> {
None
}
fn request_id(&self) -> Option<crate::RequestId> {
None
}
fn window(&self) -> Option<crate::WindowRef> {
None
}
}
pub trait ViewHost {
fn text_input(&self) -> Option<&dyn TextInputHost>;
fn activate_system_cursor(&self, device: i64, kind: SystemMouseCursorKind);
fn request_focus_change(&self, state: ViewFocusState, direction: ViewFocusDirection);
}
pub trait TextInputHost {
fn start(&self, configuration: &TextInputConfiguration);
fn stop(&self);
fn set_editing_state(&self, value: &TextEditingValue);
fn set_composing_rect(&self, rect: Rect);
fn set_caret_rect(&self, rect: Rect);
fn set_client_geometry(&self, size: Size, transform: &Matrix4);
fn handles_editing_keys(&self) -> bool {
false
}
fn provides_edit_commands(&self) -> bool {
false
}
}
pub trait ViewEmbedder {
fn add_view(
&self,
layer: &std::sync::Arc<dyn crate::CompositionLayer>,
metrics: ViewMetrics,
host: std::rc::Rc<dyn ViewHost>,
) -> Result<crate::RequestId, crate::ViewError>;
fn set_view_metrics(&self, view: ViewId, metrics: ViewMetrics);
fn set_view_in_sight(&self, view: ViewId, in_sight: bool);
fn remove_view(&self, view: ViewId);
fn send_pointer_reports(&self, reports: Vec<crate::PointerReport>);
fn send_view_focus_event(&self, event: ViewFocusEvent);
}