use super::Expression;
use crate::langtype::{NativeClass, Type};
use std::collections::{BTreeMap, HashMap};
use std::num::NonZeroUsize;
use std::rc::Rc;
pub type PropertyIndex = usize;
#[derive(Debug, Clone)]
pub enum Animation {
Static(Expression),
Transition(Expression),
}
#[derive(Debug, Clone)]
pub struct BindingExpression {
pub expression: Expression,
pub animation: Option<Animation>,
pub is_constant: bool,
}
#[derive(Debug)]
pub struct GlobalComponent {
pub name: String,
pub properties: Vec<Property>,
pub init_values: Vec<Option<BindingExpression>>,
pub const_properties: Vec<bool>,
pub public_properties: PublicProperties,
pub exported: bool,
pub aliases: Vec<String>,
pub is_builtin: bool,
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub enum PropertyReference {
Local { sub_component_path: Vec<usize>, property_index: PropertyIndex },
InNativeItem { sub_component_path: Vec<usize>, item_index: usize, prop_name: String },
InParent { level: NonZeroUsize, parent_reference: Box<PropertyReference> },
Global { global_index: usize, property_index: usize },
}
#[derive(Debug)]
pub struct Property {
pub name: String,
pub ty: Type,
}
#[derive(Debug, Clone)]
pub struct ListViewInfo {
pub viewport_y: PropertyReference,
pub viewport_height: PropertyReference,
pub viewport_width: PropertyReference,
pub listview_height: PropertyReference,
pub listview_width: PropertyReference,
pub prop_y: PropertyReference,
pub prop_width: PropertyReference,
pub prop_height: PropertyReference,
}
#[derive(Debug)]
pub struct RepeatedElement {
pub model: Expression,
pub index_prop: Option<PropertyIndex>,
pub data_prop: Option<PropertyIndex>,
pub sub_tree: ItemTree,
pub index_in_tree: usize,
pub listview: Option<ListViewInfo>,
}
pub struct Item {
pub ty: Rc<NativeClass>,
pub name: String,
pub index_in_tree: usize,
pub is_flickable_viewport: bool,
}
impl std::fmt::Debug for Item {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Item")
.field("ty", &self.ty.class_name)
.field("name", &self.name)
.field("index_in_tree", &self.index_in_tree)
.field("is_flickable_viewport", &self.is_flickable_viewport)
.finish()
}
}
#[derive(Debug)]
pub struct TreeNode {
pub sub_component_path: Vec<usize>,
pub item_index: usize,
pub repeated: bool,
pub children: Vec<TreeNode>,
}
impl TreeNode {
fn children_count(&self) -> usize {
let mut count = self.children.len();
for c in &self.children {
count += c.children_count();
}
count
}
pub fn visit_in_array(
&self,
visitor: &mut dyn FnMut(
&TreeNode,
/*children_offset: */ usize,
/*parent_index: */ usize,
),
) {
visitor(self, 1, 0);
visit_in_array_recursive(self, 1, 0, visitor);
fn visit_in_array_recursive(
node: &TreeNode,
children_offset: usize,
current_index: usize,
visitor: &mut dyn FnMut(&TreeNode, usize, usize),
) {
let mut offset = children_offset + node.children.len();
for c in &node.children {
visitor(c, offset, current_index);
offset += c.children_count();
}
let mut offset = children_offset + node.children.len();
for (i, c) in node.children.iter().enumerate() {
visit_in_array_recursive(c, offset, children_offset + i, visitor);
offset += c.children_count();
}
}
}
}
#[derive(Debug)]
pub struct SubComponent {
pub name: String,
pub properties: Vec<Property>,
pub items: Vec<Item>,
pub repeated: Vec<RepeatedElement>,
pub popup_windows: Vec<ItemTree>,
pub sub_components: Vec<SubComponentInstance>,
pub property_init: Vec<(PropertyReference, BindingExpression)>,
pub animations: HashMap<PropertyReference, Expression>,
pub two_way_bindings: Vec<(PropertyReference, PropertyReference)>,
pub const_properties: Vec<PropertyReference>,
pub init_code: Vec<Expression>,
pub layout_info_h: Expression,
pub layout_info_v: Expression,
}
impl SubComponent {
pub fn repeater_count(&self) -> usize {
let mut count = self.repeated.len();
for x in self.sub_components.iter() {
count += x.ty.repeater_count();
}
count
}
}
pub struct SubComponentInstance {
pub ty: Rc<SubComponent>,
pub name: String,
pub index_in_tree: usize,
pub index_of_first_child_in_tree: usize,
pub repeater_offset: usize,
}
impl std::fmt::Debug for SubComponentInstance {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SubComponentInstance")
.field("ty", &self.ty.name)
.field("name", &self.name)
.field("index_in_tree", &self.index_in_tree)
.field("index_of_first_child_in_tree", &self.index_of_first_child_in_tree)
.field("repeater_offset", &self.repeater_offset)
.finish()
}
}
#[derive(Debug)]
pub struct ItemTree {
pub root: SubComponent,
pub tree: TreeNode,
pub parent_context: Option<String>,
}
#[derive(Debug)]
pub struct PublicComponent {
pub public_properties: PublicProperties,
pub item_tree: ItemTree,
pub sub_components: Vec<Rc<SubComponent>>,
pub globals: Vec<GlobalComponent>,
}
pub type PublicProperties = BTreeMap<String, (Type, PropertyReference)>;