• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5part of engine;
6
7/// A surface that makes its children transparent.
8class PersistedOpacity extends PersistedContainerSurface
9    implements ui.OpacityEngineLayer {
10  PersistedOpacity(PersistedOpacity oldLayer, this.alpha, this.offset)
11      : super(oldLayer);
12
13  final int alpha;
14  final ui.Offset offset;
15
16  @override
17  void recomputeTransformAndClip() {
18    _transform = parent._transform;
19
20    final double dx = offset.dx;
21    final double dy = offset.dy;
22
23    if (dx != 0.0 || dy != 0.0) {
24      _transform = _transform.clone();
25      _transform.translate(dx, dy);
26    }
27
28    _globalClip = parent._globalClip;
29  }
30
31  @override
32  html.Element createElement() {
33    return defaultCreateElement('flt-opacity')..style.transformOrigin = '0 0 0';
34  }
35
36  @override
37  void apply() {
38    // TODO(yjbanov): evaluate using `filter: opacity(X)`. It is a longer string
39    //                but it reportedly has better hardware acceleration, so may
40    //                be worth the trade-off.
41    rootElement.style.opacity = '${alpha / 255}';
42    rootElement.style.transform = 'translate(${offset.dx}px, ${offset.dy}px)';
43  }
44
45  @override
46  void update(PersistedOpacity oldSurface) {
47    super.update(oldSurface);
48    if (alpha != oldSurface.alpha || offset != oldSurface.offset) {
49      apply();
50    }
51  }
52}
53