1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
use cgmath::Point2;
use std::{
cell::RefCell,
default::default,
fmt::{Debug, Formatter, Result},
rc::Rc,
};
use stretch::{geometry, node::Node, style};
use crate::prelude::Singleton;
use crate::{
foundation::{Helper, MouseEvent},
material::ModalBottomSheet,
rendering::backend::{WidgetRenderFactory, WidgetRenderHolder, WidgetRenderer},
services::LayoutSystem,
};
use super::{Element, WidgetComponent};
/// A utility class for building Material buttons that depend on the ambient ButtonTheme and Theme
///
/// Additional Signals: none
pub struct ModalBottomSheetElement {
component: Rc<RefCell<WidgetComponent>>,
// /// The label the button displays
// pub child: Box<dyn Element>,
// The concrete renderer for this control instance
pub renderer: Option<Rc<WidgetRenderHolder<Self>>>,
// The node in layout system
pub node: Node,
}
impl Debug for ModalBottomSheetElement {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.debug_struct("ButtonElement").finish()
}
}
impl ModalBottomSheetElement {
pub fn new(widget: &ModalBottomSheet) -> Self {
let node = LayoutSystem::new_node(
style::Style {
padding: geometry::Rect {
start: style::Dimension::Points(2.0),
end: style::Dimension::Points(2.0),
top: style::Dimension::Points(2.0),
bottom: style::Dimension::Points(2.0),
},
size: geometry::Size {
width: style::Dimension::Percent(1.0),
height: style::Dimension::Percent(1.0),
},
..default()
},
vec![],
)
.unwrap();
let component = WidgetComponent::get(widget.key.id());
// if widget.onclick.is_some() {
// // onmouseup.listen(options.onclick);
// }
// let child = widget.child.create_element();
// child.node().map(|child| {
// let child_style = LayoutSystem::style(child).unwrap();
// LayoutSystem::set_style(
// child,
// style::Style {
// size: geometry::Size {
// width: style::Dimension::Percent(1.0),
// height: style::Dimension::Percent(1.0),
// },
// ..child_style
// },
// )
// .unwrap();
// LayoutSystem::set_children(node, vec![child]).unwrap()
// });
Self {
component,
// child,
renderer: WidgetRenderFactory::global().get::<Self>(),
node,
}
}
}
impl AsRef<RefCell<WidgetComponent>> for ModalBottomSheetElement {
fn as_ref(&self) -> &RefCell<WidgetComponent> {
self.component.as_ref()
}
}
impl Element for ModalBottomSheetElement {
fn mousedown(&self, e: &mut MouseEvent) {
// self.state.borrow_mut().mouse_down = true;
// let component = self.component.borrow();
// component.onmousedown.emit(e);
log::info!("ModalBottomSheetElement clicked");
// self.state.borrow_mut().mouse_down = true;
// let component = self.component.borrow();
// component.onmousedown.emit(e);
// TODO: remove this check
let inside = {
let comp = self.component.borrow();
Helper::in_rect(e.x as f32, e.y as f32, comp.x, comp.y, comp.w, comp.h)
};
// Scaffold should used as root widget after MaterialApp
// so all events should be inside
if inside {
let x = e.x as f32;
let y = e.y as f32;
// if self.child.contains(x, y) {
// self.child.mousedown(e);
// }
// if self.title.contains(x, y) {
// log::info!("Is an Title");
// self.title.mousedown(e);
// }
// if self.flexible_space.contains(x, y) {
// // log::info!("Is an FAB");
// self.flexible_space.mousedown(e);
// }
// for action in self.actions.iter() {
// if action.contains(x, y) {
// log::info!("Is an ACTION");
// action.mousedown(e);
// }
// }
// if self.drawer.contains(x, y) {
// // log::info!("Is an Drawer");
// self.drawer.mousedown(e);
// }
}
}
fn render(&self) {
// {
// let comp = self.component.borrow();
// assert!(
// !comp.destroyed,
// "Widget was already destroyed but is being interacted with"
// );
// if comp.renderable {
// comp.onrender.emit(&());
// }
// }
if let Some(ref render) = self.renderer {
render.render(self);
}
// for child in comp.children.iter() {
// if let Some(widget) = child.widget() {
// widget.render();
// }
// }
// center do not have a render, so we render the child
// self.child.render();
}
fn node(&self) -> Option<Node> {
Some(self.node)
}
fn relayout(&self, origin: Point2<f32>) {
let update_childs = match LayoutSystem::layout(self.node) {
Ok(layout) => {
let mut comp = self.as_ref().borrow_mut();
comp.x = layout.location.x + origin.x;
comp.y = layout.location.y + origin.y;
comp.w = layout.size.width;
comp.h = layout.size.height;
true
}
Err(e) => {
log::error!("{}", e);
false
}
};
if update_childs {
let comp = self.as_ref().borrow();
// self.child.relayout(Point2 {
// x: comp.x,
// y: comp.y,
// });
}
}
}