Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'package:sky/framework/theme2/colors.dart' as colors; | |
| 6 | |
| 7 import 'dart:sky' as sky; | |
| 5 import '../fn2.dart'; | 8 import '../fn2.dart'; |
| 9 import '../rendering/box.dart'; | |
| 10 import '../rendering/object.dart'; | |
| 6 import 'button_base.dart'; | 11 import 'button_base.dart'; |
| 7 import 'dart:sky' as sky; | |
| 8 | 12 |
| 9 typedef void ValueChanged(value); | 13 typedef void ValueChanged(value); |
| 10 | 14 |
| 11 class Checkbox extends ButtonBase { | 15 class Checkbox extends ButtonBase { |
| 12 static final Style _style = new Style(''' | |
| 13 transform: translateX(0); | |
| 14 justify-content: center; | |
| 15 align-items: center; | |
| 16 -webkit-user-select: none; | |
| 17 cursor: pointer; | |
| 18 width: 30px; | |
| 19 height: 30px;''' | |
| 20 ); | |
| 21 | |
| 22 static final Style _containerStyle = new Style(''' | |
| 23 border: solid 2px; | |
| 24 border-color: rgba(90, 90, 90, 0.25); | |
| 25 width: 10px; | |
| 26 height: 10px;''' | |
| 27 ); | |
| 28 | |
| 29 static final Style _containerHighlightStyle = new Style(''' | |
| 30 border: solid 2px; | |
| 31 border-color: rgba(90, 90, 90, 0.25); | |
| 32 width: 10px; | |
| 33 height: 10px; | |
| 34 border-radius: 10px; | |
| 35 background-color: orange; | |
| 36 border-color: orange;''' | |
| 37 ); | |
| 38 | |
| 39 static final Style _uncheckedStyle = new Style(''' | |
| 40 top: 0px; | |
| 41 left: 0px;''' | |
| 42 ); | |
| 43 | |
| 44 static final Style _checkedStyle = new Style(''' | |
| 45 top: 0px; | |
| 46 left: 0px; | |
| 47 transform: translate(2px, -15px) rotate(45deg); | |
| 48 width: 10px; | |
| 49 height: 20px; | |
| 50 border-style: solid; | |
| 51 border-top: none; | |
| 52 border-left: none; | |
| 53 border-right-width: 2px; | |
| 54 border-bottom-width: 2px; | |
| 55 border-color: #0f9d58;''' | |
| 56 ); | |
| 57 | 16 |
| 58 bool checked; | 17 bool checked; |
| 59 ValueChanged onChanged; | 18 ValueChanged onChanged; |
| 60 | 19 |
| 61 Checkbox({ Object key, this.onChanged, this.checked }) : super(key: key); | 20 Checkbox({ Object key, this.onChanged, this.checked }) : super(key: key); |
| 62 | 21 |
| 63 void _handleClick(sky.Event e) { | 22 void _handleClick(sky.Event e) { |
| 64 onChanged(!checked); | 23 onChanged(!checked); |
| 65 } | 24 } |
| 66 | 25 |
| 67 UINode buildContent() { | 26 UINode buildContent() { |
| 27 // TODO(jackson): This should change colors with the theme | |
| 28 sky.Color color = highlight ? colors.Purple[500] : const sky.Color(0x8A00000 0); | |
| 29 const double edgeSize = 20.0; | |
| 30 const double edgeRadius = 5.0; | |
| 68 return new EventListenerNode( | 31 return new EventListenerNode( |
| 69 new FlexContainer( | 32 new Container( |
| 70 style: _style, | 33 margin: const EdgeDims.symmetric(horizontal: 5.0), |
| 71 direction: FlexDirection.horizontal, | 34 desiredSize: new sky.Size(edgeSize + 2.0, edgeSize + 2.0), |
| 72 children: [ | 35 child: new CustomPaint( |
| 73 new Container( | 36 callback: (sky.Canvas canvas) { |
| 74 style: highlight ? _containerHighlightStyle : _containerStyle, | 37 |
| 75 children: [ | 38 sky.Paint paint = new sky.Paint()..color = color; |
| 76 new Container( | 39 paint.isAntiAlias = true; |
| 77 style: checked ? _checkedStyle : _uncheckedStyle | 40 paint.strokeWidth = 2.0; |
|
abarth-chromium
2015/06/05 21:00:59
Why not use the .. operator to set these values?
jackson
2015/06/05 21:14:11
Done.
| |
| 78 ) | 41 |
| 79 ] | 42 // Draw the outer rrect |
| 80 ) | 43 paint.setStyle(checked ? sky.PaintingStyle.strokeAndFill : sky.Paint ingStyle.stroke); |
| 81 ] | 44 sky.Rect rect = new sky.Rect.fromLTRB(0.0, 0.0, edgeSize, edgeSize); |
| 45 sky.RRect rrect = new sky.RRect()..setRectXY(rect, edgeRadius, edgeR adius); | |
| 46 canvas.drawRRect(rrect, paint); | |
| 47 | |
| 48 // Draw the inner check | |
| 49 if (checked) { | |
| 50 // TODO(jackson): Use the theme color | |
| 51 paint.color = const sky.Color(0xFFFFFFFF); | |
| 52 paint.setStyle(sky.PaintingStyle.stroke); | |
| 53 sky.Path path = new sky.Path(); | |
| 54 path.moveTo(edgeSize * 0.2, edgeSize * 0.5); | |
| 55 path.lineTo(edgeSize * 0.4, edgeSize * 0.7); | |
| 56 path.lineTo(edgeSize * 0.8, edgeSize * 0.3); | |
| 57 canvas.drawPath(path, paint); | |
| 58 } | |
| 59 } | |
| 60 ) | |
| 82 ), | 61 ), |
| 83 onGestureTap: _handleClick | 62 onGestureTap: _handleClick |
| 84 ); | 63 ); |
| 85 } | 64 } |
| 86 } | 65 } |
| OLD | NEW |