浏览代码

Imroved selection rendering on images

Anatoly Pulyaevskiy 6 年前
父节点
当前提交
11275cc3c7
共有 2 个文件被更改,包括 49 次插入14 次删除
  1. 45
    13
      packages/zefyr/lib/src/widgets/image.dart
  2. 4
    1
      packages/zefyr/lib/src/widgets/selection.dart

+ 45
- 13
packages/zefyr/lib/src/widgets/image.dart 查看文件

106
   }
106
   }
107
 
107
 
108
   @override
108
   @override
109
-  double get preferredLineHeight => size.height;
109
+  double get preferredLineHeight => size.height - kPaddingBottom;
110
 
110
 
111
   @override
111
   @override
112
-  SelectionOrder get selectionOrder => SelectionOrder.background;
112
+  SelectionOrder get selectionOrder => SelectionOrder.foreground;
113
 
113
 
114
   @override
114
   @override
115
   TextSelection getLocalSelection(TextSelection documentSelection) {
115
   TextSelection getLocalSelection(TextSelection documentSelection) {
127
   List<ui.TextBox> getEndpointsForSelection(TextSelection selection) {
127
   List<ui.TextBox> getEndpointsForSelection(TextSelection selection) {
128
     TextSelection local = getLocalSelection(selection);
128
     TextSelection local = getLocalSelection(selection);
129
     if (local.isCollapsed) {
129
     if (local.isCollapsed) {
130
-      final dx = local.extentOffset == 0 ? 0.0 : size.width;
130
+      final dx = local.extentOffset == 0 ? _childOffset.dx : size.width;
131
       return [
131
       return [
132
-        new ui.TextBox.fromLTRBD(dx, 0.0, dx, size.height, TextDirection.ltr),
132
+        new ui.TextBox.fromLTRBD(
133
+            dx, 0.0, dx, size.height - kPaddingBottom, TextDirection.ltr),
133
       ];
134
       ];
134
     }
135
     }
135
 
136
 
137
+    final rect = _childRect;
136
     return [
138
     return [
137
-      new ui.TextBox.fromLTRBD(0.0, 0.0, 0.0, size.height, TextDirection.ltr),
138
       new ui.TextBox.fromLTRBD(
139
       new ui.TextBox.fromLTRBD(
139
-          size.width, 0.0, size.width, size.height, TextDirection.ltr),
140
+          rect.left, rect.top, rect.left, rect.bottom, TextDirection.ltr),
141
+      new ui.TextBox.fromLTRBD(
142
+          rect.right, rect.top, rect.right, rect.bottom, TextDirection.ltr),
140
     ];
143
     ];
141
   }
144
   }
142
 
145
 
174
   }
177
   }
175
 
178
 
176
   @override
179
   @override
177
-  void paintSelection(PaintingContext context, ui.Offset offset,
178
-      TextSelection selection, ui.Color selectionColor) {
180
+  void paintSelection(PaintingContext context, Offset offset,
181
+      TextSelection selection, Color selectionColor) {
179
     final localSelection = getLocalSelection(selection);
182
     final localSelection = getLocalSelection(selection);
180
     assert(localSelection != null);
183
     assert(localSelection != null);
181
     if (!localSelection.isCollapsed) {
184
     if (!localSelection.isCollapsed) {
182
-      final Paint paint = new Paint()..color = selectionColor;
183
-      final rect = new Rect.fromLTWH(0.0, 0.0, size.width, size.height);
184
-      context.canvas.drawRect(rect.shift(offset), paint);
185
+      final Paint paint = new Paint()
186
+        ..color = selectionColor
187
+        ..style = PaintingStyle.stroke
188
+        ..strokeWidth = 3.0;
189
+      final rect = new Rect.fromLTWH(
190
+          0.0, 0.0, _lastChildSize.width, _lastChildSize.height);
191
+      context.canvas.drawRect(rect.shift(offset + _childOffset), paint);
185
     }
192
     }
186
   }
193
   }
187
 
194
 
195
+  void paint(PaintingContext context, Offset offset) {
196
+    super.paint(context, offset + _childOffset);
197
+  }
198
+
199
+  static const double kHorizontalPadding = 1.0;
200
+
201
+  Size _lastChildSize;
202
+
203
+  Offset get _childOffset {
204
+    final dx = (size.width - _lastChildSize.width) / 2 + kHorizontalPadding;
205
+    final dy = (size.height - _lastChildSize.height - kPaddingBottom) / 2;
206
+    return new Offset(dx, dy);
207
+  }
208
+
209
+  Rect get _childRect {
210
+    return new Rect.fromLTWH(_childOffset.dx, _childOffset.dy,
211
+        _lastChildSize.width, _lastChildSize.height);
212
+  }
213
+
188
   @override
214
   @override
189
   void performLayout() {
215
   void performLayout() {
190
     assert(constraints.hasBoundedWidth);
216
     assert(constraints.hasBoundedWidth);
191
     if (child != null) {
217
     if (child != null) {
192
       // Make constraints use 16:9 aspect ratio.
218
       // Make constraints use 16:9 aspect ratio.
219
+      final width = constraints.maxWidth - kHorizontalPadding * 2;
193
       final childConstraints = constraints.copyWith(
220
       final childConstraints = constraints.copyWith(
194
-        maxHeight: (constraints.maxWidth * 9 / 16).floorToDouble(),
221
+        minWidth: 0.0,
222
+        maxWidth: width,
223
+        minHeight: 0.0,
224
+        maxHeight: (width * 9 / 16).floorToDouble(),
195
       );
225
       );
196
       child.layout(childConstraints, parentUsesSize: true);
226
       child.layout(childConstraints, parentUsesSize: true);
197
-      size = new Size(child.size.width, child.size.height + kPaddingBottom);
227
+      _lastChildSize = child.size;
228
+      size = new Size(
229
+          constraints.maxWidth, _lastChildSize.height + kPaddingBottom);
198
     } else {
230
     } else {
199
       performResize();
231
       performResize();
200
     }
232
     }

+ 4
- 1
packages/zefyr/lib/src/widgets/selection.dart 查看文件

351
       handle = new CompositedTransformFollower(
351
       handle = new CompositedTransformFollower(
352
         link: block.layerLink,
352
         link: block.layerLink,
353
         showWhenUnlinked: false,
353
         showWhenUnlinked: false,
354
-        child: new Stack(children: <Widget>[handle]),
354
+        child: new Stack(
355
+          overflow: Overflow.visible,
356
+          children: <Widget>[handle],
357
+        ),
355
       );
358
       );
356
     }
359
     }
357
     // Always return this gesture detector even if handle is an empty container
360
     // Always return this gesture detector even if handle is an empty container