|
| 1 | +import 'package:flutter/gestures.dart'; |
| 2 | +import 'package:flutter/rendering.dart'; |
| 3 | + |
| 4 | +import '../classic_app/classic_app.dart'; |
| 5 | +import '../classic_app/repaint_event.dart'; |
| 6 | + |
| 7 | +class ClassicAppRenderObject extends RenderBox { |
| 8 | + ClassicAppRenderObject(ClassicApp classicApp) { |
| 9 | + _classicApp = classicApp; |
| 10 | + _classicApp.events.subscribe(_clientAppRepaint); |
| 11 | + _isSubscribe = true; |
| 12 | + } |
| 13 | + |
| 14 | + @override |
| 15 | + bool get isRepaintBoundary => true; |
| 16 | + |
| 17 | + @override |
| 18 | + void performLayout() { |
| 19 | + size = Size( |
| 20 | + constraints.maxWidth == double.infinity ? 0 : constraints.maxWidth, |
| 21 | + constraints.maxHeight == double.infinity ? 0 : constraints.maxHeight, |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + @override |
| 26 | + void paint(PaintingContext context, Offset offset) { |
| 27 | + context.canvas.translate(offset.dx, offset.dy); |
| 28 | + context.canvas.clipRect(offset & size); |
| 29 | + _classicApp.onPaint(context.canvas, size); |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + void dispose() { |
| 34 | + if (_isSubscribe) { |
| 35 | + _classicApp.events.unsubscribe(_clientAppRepaint); |
| 36 | + _isSubscribe = false; |
| 37 | + } |
| 38 | + super.dispose(); |
| 39 | + } |
| 40 | + |
| 41 | + late ClassicApp _classicApp; |
| 42 | + var _isSubscribe = false; |
| 43 | + |
| 44 | + ClassicApp get classicApp => _classicApp; |
| 45 | + |
| 46 | + set classicApp(ClassicApp newClassicApp) { |
| 47 | + if (newClassicApp == _classicApp) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + if (_isSubscribe) { |
| 52 | + _classicApp.events.unsubscribe(_clientAppRepaint); |
| 53 | + _isSubscribe = false; |
| 54 | + } |
| 55 | + |
| 56 | + _classicApp = newClassicApp; |
| 57 | + _classicApp.events.subscribe(_clientAppRepaint); |
| 58 | + _isSubscribe = true; |
| 59 | + } |
| 60 | + |
| 61 | + void _clientAppRepaint(RepaintEvent e) => markNeedsPaint(); |
| 62 | + |
| 63 | + @override |
| 64 | + void handleEvent(PointerEvent event, covariant BoxHitTestEntry entry) { |
| 65 | + if (event is PointerHoverEvent || event is PointerMoveEvent) { |
| 66 | + } else if (event is PointerDownEvent) { |
| 67 | + if (event.buttons == kPrimaryMouseButton) { |
| 68 | + _classicApp.onMouseDown(); |
| 69 | + } else if (event.buttons == kSecondaryMouseButton) {} |
| 70 | + else if (event.buttons == kMiddleMouseButton) {} |
| 71 | + } else if (event is PointerScrollEvent) { |
| 72 | + _classicApp.onPointerWheel(event.scrollDelta.dx, event.scrollDelta.dy); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @override |
| 77 | + bool hitTest(BoxHitTestResult result, {required Offset position}) { |
| 78 | + if (size.contains(position)) { |
| 79 | + result.add(BoxHitTestEntry(this, position)); |
| 80 | + return true; |
| 81 | + } |
| 82 | + return false; |
| 83 | + } |
| 84 | +} |
0 commit comments