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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
use cgmath::Point2;
use std::{
cell::RefCell,
default::default,
fmt::{Debug, Formatter, Result},
rc::Rc,
};
use stretch::{geometry, node::Node, number, style};
use crate::prelude::{OnDemand, Singleton};
use crate::{
foundation::{Id, KeyEvent, MouseEvent, ScaleChangeEvent, Signal, TextEvent},
rendering::backend::{WidgetRenderFactory, WidgetRenderHolder, WidgetRenderer},
services::LayoutSystem,
widgets::WidgetsApp,
};
use super::{Element, WidgetComponent};
#[derive(Default, Debug, Clone, Copy)]
struct WidgetsAppState {
depth_idx: f32, // = 0.0;
mouse_down: bool, // = false;
}
/// A canvas is a root object::
/// It requires a rendering instance, and handles all incoming events,
/// propagating them to the children.
/// Additional Signals: none
pub struct WidgetsAppElement {
pub component: Rc<RefCell<WidgetComponent>>, // TODO: should deal with other
pub home: Box<dyn Element>,
/// The current focused control, None if none
pub focused: Option<Id>, // WidgetComponent
/// The current marked control, None if none
pub marked: Option<Id>, // WidgetComponent
/// The current modal control, None if none
pub captured: Option<Id>, // WidgetComponent
/// Whether or not the current focus needs refreshing.
pub focus_invalid: bool, // = true;
/// The canvas scale factor.
/// Note that this value is a hint for rendering the canvas,
/// it does not affect any coordinates directly.
pub scale: f32, // = 1.0;
/// An event for when the focused state changes
pub onfocusedchange: Signal<Id>,
/// An event for when the marked state changes
pub onmarkedchange: Signal<Id>,
/// An event for when the captured state changes
pub oncapturedchange: Signal<Id>,
/// On scale changed
pub onscalechange: Signal<ScaleChangeEvent>,
state: RefCell<WidgetsAppState>,
// The concrete renderer for this control instance
pub renderer: Option<Rc<WidgetRenderHolder<Self>>>,
// The node in layout system
pub node: Node,
}
impl Debug for WidgetsAppElement {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.debug_struct("WidgetsAppElement").finish()
}
}
impl WidgetsAppElement {
pub fn new(widget: &WidgetsApp) -> Self {
let node = LayoutSystem::new_node(
style::Style {
size: geometry::Size {
width: style::Dimension::Percent(1.0),
height: style::Dimension::Percent(1.0),
},
..default()
},
vec![],
)
.unwrap();
// assert!(options.rendering.is_some(), "No Rendering given to Canvas, cannot create a canvas without one.");
let component = WidgetComponent::get(widget.key.id());
// component.onrender.listen(box |_| {
// log::info!("Got signal to render WidgetsApp");
// });
// canvas = self;
// let scale = widget.scale;
let home = widget.home.create_element();
// If home is not NullElement so override
// the home layout and set it as child
if let Some(child) = home.node() {
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,
home,
captured: None,
state: Default::default(),
focus_invalid: true,
focused: None,
marked: None,
oncapturedchange: Signal::new(),
onfocusedchange: Signal::new(),
onmarkedchange: Signal::new(),
onscalechange: Signal::new(),
scale: 1.0,
renderer: WidgetRenderFactory::global().get::<Self>(),
node,
}
}
pub fn bring_to_front(&self, control: &dyn Element) {
//re-add it to the canvas will put it above
let component = self.component.borrow();
if let Some(canvas) = component.canvas.as_ref() {
canvas.add(control);
self.sync_depth();
}
}
/// Get the top most control under the given point, or None if there is none (or is the canvas itself)
pub fn topmost_at_point(&self, x: f32, y: f32) -> Option<&Box<dyn Element>> {
if let Some(control) = self.topmost_child_at_point(x, y) {
// control is not this canvas
if control.id() != self.id() {
return Some(control);
}
}
None
}
//Internal
pub fn apply_depth(&self, control: &dyn Element) {
let mut state = self.state.borrow_mut();
control.set_depth(state.depth_idx + control.depth_offset());
state.depth_idx += 1.0;
// for child in control.as_ref().borrow().children.iter() {
// if let Some(widget) = child.widget() {
// self.apply_depth(widget.as_ref());
// }
// }
}
pub fn sync_depth(&self) {
self.state.borrow_mut().depth_idx = 0.0;
self.apply_depth(self);
}
//getters/setters
/// The canvas scale factor.
/// Note that this value is a hint for rendering the canvas,
/// it does not affect any coordinates directly.
pub fn scale(&self) -> f32 {
self.scale
}
/// The canvas scale factor.
/// Note that this value is a hint for rendering the canvas,
/// it does not affect any coordinates directly.
pub fn set_scale(&mut self, value: f32) {
let prev = self.scale;
self.scale = value;
if value != prev {
self.refresh_bounds();
}
self.onscalechange.emit(&ScaleChangeEvent { value, prev });
}
/// The current focused control, None if none
pub fn focused(&self) -> Option<&Box<dyn Element>> {
// self.focused
todo!()
}
pub fn set_focused(&mut self, control: Option<&dyn Element>) {
if let Some(focused) = self.focused {
// if let Some(widget) = focused.widget() {
// widget.set_focused(false);
// }
}
self.focused = control.map(|x| x.id());
if let Some(widget) = control {
self.onfocusedchange.emit(&widget.id());
}
if let Some(focused) = self.focused {
// if let Some(widget) = focused.widget() {
// widget.set_focused(true);
// }
}
}
/// The current modal control, None if none
pub fn captured(&self) -> Option<&Box<dyn Element>> {
// self.captured
todo!()
}
/// The current modal control, None if none
pub fn set_captured(&mut self, control: Option<&dyn Element>) {
if let Some(captured) = self.captured {
// if let Some(widget) = captured.widget() {
// widget.set_captured(false);
// }
}
self.captured = control.map(|x| x.id());
if let Some(widget) = control {
self.oncapturedchange.emit(&widget.id());
}
if let Some(captured) = self.captured {
// if let Some(widget) = captured.widget() {
// widget.set_captured(true);
// }
}
}
/// The current marked control, None if none
pub fn marked(&self) -> Option<&Box<dyn Element>> {
// self.marked
todo!()
}
/// The current marked control, None if none
pub fn set_marked(&mut self, control: Option<&dyn Element>) {
if let Some(marked) = self.marked {
// if let Some(widget) = marked.widget() {
// widget.set_marked(false);
// }
}
self.marked = control.map(|x| x.id());
if let Some(widget) = control {
self.onmarkedchange.emit(&widget.id());
}
if let Some(marked) = self.marked {
// if let Some(widget) = marked.widget() {
// widget.set_marked(true);
// }
}
}
}
impl AsRef<RefCell<WidgetComponent>> for WidgetsAppElement {
fn as_ref(&self) -> &RefCell<WidgetComponent> {
self.component.as_ref()
}
}
// Overrides from WidgetComponent
impl Element for WidgetsAppElement {
fn mouseup(&self, e: &mut MouseEvent) {
// log::info!("{:?}", e);
// self.state.borrow_mut().mouse_down = false;
// let component = self.component.borrow();
// component.onmouseup.emit(e);
// coz home is fit whole WidgetsApp,
// so we do not check that MouseEvent inside home bounds
self.home.mouseup(e);
}
fn mousedown(&self, e: &mut MouseEvent) {
// log::info!("{:?}", e);
// self.state.borrow_mut().mouse_down = true;
// let component = self.component.borrow();
// component.onmousedown.emit(e);
// coz home is fit whole WidgetsApp,
// so we do not check that MouseEvent inside home bounds
self.home.mousedown(e);
}
fn mousemove(&self, e: &mut MouseEvent) {
// log::info!("{:?}", e);
// let inside = Helper::in_rect(
// e.x as f32,
// e.y as f32,
// self.x(),
// self.y(),
// self.w(),
// self.h(),
// );
// let component = self.component.borrow();
// //inside but leaving
// if component.ishovered && !inside {
// self.mouseleave(e);
// if self.state.borrow().mouse_down {
// // TODO: config + move to mouseleave
// self.mouseup(e);
// }
// } else if !component.ishovered && inside {
// self.mouseenter(e);
// }
// if component.onmousemove.is_some() {
// component.onmousemove.get().try_send(e.clone());
// }
// coz home is fit whole WidgetsApp,
// so we do not check that MouseEvent inside home bounds
self.home.mousemove(e);
}
fn mousewheel(&self, e: &mut MouseEvent) {
// log::info!("{:?}", e);
// let component = self.component.borrow();
//
// if component.onmousewheel.is_some() {
// component.onmousewheel.get().try_send(e.clone());
// }
// coz home is fit whole WidgetsApp,
// so we do not check that MouseEvent inside home bounds
self.home.mousemove(e);
}
fn keyup(&self, e: &mut KeyEvent) {
// log::info!("{:?}", e);
// let component = self.component.borrow();
//
// if component.onkeyup.is_some() {
// component.onkeyup.get().try_send(e.clone());
// }
// coz home is fit whole WidgetsApp,
// so we do not check that home is focused
self.home.keyup(e);
}
fn keydown(&self, e: &mut KeyEvent) {
// log::info!("{:?}", e);
// let component = self.component.borrow();
//
// if component.onkeydown.is_some() {
// component.onkeydown.get().try_send(e.clone());
// }
// coz home is fit whole WidgetsApp,
// so we do not check that home is focused
self.home.keydown(e);
}
fn textinput(&self, e: &mut TextEvent) {
// log::info!("{:?}", e);
// let component = self.component.borrow();
//
// if component.ontextinput.is_some() {
// component.ontextinput.get().try_send(e.clone());
// }
// coz home is fit whole WidgetsApp,
// so we do not check that home is focused
self.home.textinput(e);
}
fn set_size(&self, w: f32, h: f32) {
// log::warn!("Set Size WidgetsAppElement {}x{}", w, h);
// let (dw, dh) = {
// let mut comp = self.as_ref().borrow_mut();
// assert!(
// !comp.destroyed,
// "Widget was already destroyed but is being interacted with"
// );
// comp.updating = true;
// // we calculate here delta from prev size to emit this value
// // and relayout childs
// let dw = w - comp.w;
// let dh = h - comp.h;
// comp.w = w;
// comp.h = h;
// comp.updating = false;
// (dw, dh)
// };
// self.bounds_changed(0.0, 0.0, dw, dh);
// just compute layout, update size and relayout whole widget tree
{
let mut comp = self.as_ref().borrow_mut();
comp.w = w;
comp.h = h;
}
let _ = LayoutSystem::compute_layout(
self.node,
geometry::Size {
width: number::Number::Defined(w),
height: number::Number::Defined(h),
},
);
// println!("{:#?}", LayoutSystem::layout(self.node).unwrap());
self.relayout(Point2 { x: 0.0, y: 0.0 });
}
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.home.relayout(Point2 {
x: comp.x,
y: comp.y,
});
}
}
fn update(&self, dt: f32) {
log::info!("Update WidgetsAppElement");
let component = self.component.borrow();
// for control in component.children.iter() {
// if let Some(widget) = control.widget() {
// widget.update(dt);
// }
// }
}
fn render(&self) {
{
let mut comp = self.component.borrow_mut();
assert!(
!comp.destroyed,
"Widget was already destroyed but is being interacted with"
);
if comp.renderable && comp.onrender.is_some() {
let _ = comp.onrender.get().try_send(());
}
}
if let Some(ref render) = self.renderer {
render.render(self);
}
self.home.render();
{
let comp = self.component.borrow();
// for child in comp.children.iter() {
// if let Some(widget) = child.widget() {
// widget.render();
// }
// }
}
if let Some(ref render) = self.renderer {
render.post_render(self);
}
}
fn destroy(&self) {
// self.base.destroy();
self.onfocusedchange.clear();
self.onmarkedchange.clear();
self.oncapturedchange.clear();
self.onscalechange.clear();
}
fn node(&self) -> Option<Node> {
Some(self.node)
}
}