use crate::{
Matrix4, Rect, ScreenId, SelectionRect, Size, SystemMouseCursorKind, SystemUiOverlayStyle,
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 TitleBarArea {
pub height: f64,
pub left_inset: f64,
pub right_inset: f64,
}
#[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 system_gesture_insets: ViewPadding,
pub screen: Option<ScreenId>,
pub title_bar_area: Option<TitleBarArea>,
}
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,
system_gesture_insets: ViewPadding::ZERO,
screen: None,
title_bar_area: 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: std::any::Any {
fn id(&self) -> ViewId;
fn metrics(&self) -> ViewMetrics;
fn gesture_settings(&self) -> GestureSettings {
GestureSettings::default()
}
fn present(&self, scene: std::sync::Arc<crate::Scene>, size: Size);
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);
fn set_framework_handles_back(&self, handles: bool) {
let _ = handles;
}
fn system_bars(&self) -> Option<&dyn SystemBars>;
fn view_size_changed(&self, size: Size) {
let _ = size;
}
}
pub trait SystemBars {
fn set_overlay_style(&self, style: &SystemUiOverlayStyle);
fn hide(&self, behavior: SystemBarsBehavior);
fn show(&self);
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SystemBarsBehavior {
ShowBarsBySwipe,
ShowTransientBarsBySwipe,
}
pub trait TextInputHost {
fn set_client(&self, configuration: &TextInputConfiguration);
fn show(&self);
fn update_config(&self, configuration: &TextInputConfiguration);
fn clear_client(&self);
fn hide(&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 set_selection_rects(&self, selection_rects: &[SelectionRect]) {
let _ = selection_rects;
}
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>,
initial_data: Option<String>,
) -> Result<EmbeddedView, crate::ViewError>;
fn send_pointer_reports(&self, reports: Vec<crate::PointerReport>);
fn send_view_focus_event(&self, event: ViewFocusEvent);
}
pub struct EmbeddedView {
request_id: crate::RequestId,
host: Box<dyn HostEmbeddedView>,
}
impl EmbeddedView {
pub fn new(request_id: crate::RequestId, host: Box<dyn HostEmbeddedView>) -> EmbeddedView {
EmbeddedView { request_id, host }
}
pub fn request_id(&self) -> crate::RequestId {
self.request_id
}
pub fn set_metrics(&self, metrics: ViewMetrics) {
self.host.set_metrics(metrics);
}
pub fn set_in_sight(&self, in_sight: bool) {
self.host.set_in_sight(in_sight);
}
}
impl Drop for EmbeddedView {
fn drop(&mut self) {
self.host.remove();
}
}
impl std::fmt::Debug for EmbeddedView {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("EmbeddedView")
.field("request_id", &self.request_id)
.finish_non_exhaustive()
}
}
pub trait HostEmbeddedView {
fn set_metrics(&self, metrics: ViewMetrics);
fn set_in_sight(&self, in_sight: bool);
fn remove(&self);
}