Skip to content

Commit 802bd15

Browse files
author
Chris Yang
authored
iOS platform view opacity (flutter#9667)
1 parent 3b6265b commit 802bd15

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

flow/embedded_views.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ void MutatorsStack::PushTransform(const SkMatrix& matrix) {
3030
vector_.push_back(element);
3131
};
3232

33+
void MutatorsStack::PushOpacity(const int& alpha) {
34+
std::shared_ptr<Mutator> element = std::make_shared<Mutator>(alpha);
35+
vector_.push_back(element);
36+
};
37+
3338
void MutatorsStack::Pop() {
3439
vector_.pop_back();
3540
};

flow/embedded_views.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
namespace flutter {
1919

20-
enum MutatorType { clip_rect, clip_rrect, clip_path, transform };
20+
enum MutatorType { clip_rect, clip_rrect, clip_path, transform, opacity };
2121

2222
// Stores mutation information like clipping or transform.
2323
//
@@ -42,6 +42,9 @@ class Mutator {
4242
case transform:
4343
matrix_ = other.matrix_;
4444
break;
45+
case opacity:
46+
alpha_ = other.alpha_;
47+
break;
4548
default:
4649
break;
4750
}
@@ -53,12 +56,15 @@ class Mutator {
5356
: type_(clip_path), path_(new SkPath(path)) {}
5457
explicit Mutator(const SkMatrix& matrix)
5558
: type_(transform), matrix_(matrix) {}
59+
explicit Mutator(const int& alpha) : type_(opacity), alpha_(alpha) {}
5660

5761
const MutatorType& GetType() const { return type_; }
5862
const SkRect& GetRect() const { return rect_; }
5963
const SkRRect& GetRRect() const { return rrect_; }
6064
const SkPath& GetPath() const { return *path_; }
6165
const SkMatrix& GetMatrix() const { return matrix_; }
66+
const int& GetAlpha() const { return alpha_; }
67+
float GetAlphaFloat() const { return (alpha_ / 255.0); }
6268

6369
bool operator==(const Mutator& other) const {
6470
if (type_ != other.type_) {
@@ -73,6 +79,8 @@ class Mutator {
7379
return *path_ == *other.path_;
7480
case transform:
7581
return matrix_ == other.matrix_;
82+
case opacity:
83+
return alpha_ == other.alpha_;
7684
}
7785

7886
return false;
@@ -98,6 +106,7 @@ class Mutator {
98106
SkRRect rrect_;
99107
SkMatrix matrix_;
100108
SkPath* path_;
109+
int alpha_;
101110
};
102111

103112
}; // Mutator
@@ -119,6 +128,7 @@ class MutatorsStack {
119128
void PushClipRRect(const SkRRect& rrect);
120129
void PushClipPath(const SkPath& path);
121130
void PushTransform(const SkMatrix& matrix);
131+
void PushOpacity(const int& alpha);
122132

123133
// Removes the `Mutator` on the top of the stack
124134
// and destroys it.

flow/layers/opacity_layer.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ void OpacityLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
3737
EnsureSingleChild();
3838
SkMatrix child_matrix = matrix;
3939
child_matrix.postTranslate(offset_.fX, offset_.fY);
40+
context->mutators_stack.PushTransform(
41+
SkMatrix::MakeTrans(offset_.fX, offset_.fY));
42+
context->mutators_stack.PushOpacity(alpha_);
4043
ContainerLayer::Preroll(context, child_matrix);
44+
context->mutators_stack.Pop();
45+
context->mutators_stack.Pop();
4146
set_paint_bounds(paint_bounds().makeOffset(offset_.fX, offset_.fY));
4247
// See |EnsureSingleChild|.
4348
FML_DCHECK(layers().size() == 1);

flow/mutators_stack_unittests.cc

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ TEST(MutatorsStack, PushClipRRect) {
4242
}
4343

4444
TEST(MutatorsStack, PushClipPath) {
45-
flutter::MutatorsStack stack;
45+
MutatorsStack stack;
4646
SkPath path;
4747
stack.PushClipPath(path);
4848
auto iter = stack.Bottom();
@@ -60,6 +60,15 @@ TEST(MutatorsStack, PushTransform) {
6060
ASSERT_TRUE(iter->get()->GetMatrix() == matrix);
6161
}
6262

63+
TEST(MutatorsStack, PushOpacity) {
64+
MutatorsStack stack;
65+
int alpha = 240;
66+
stack.PushOpacity(alpha);
67+
auto iter = stack.Bottom();
68+
ASSERT_TRUE(iter->get()->GetType() == MutatorType::opacity);
69+
ASSERT_TRUE(iter->get()->GetAlpha() == 240);
70+
}
71+
6372
TEST(MutatorsStack, Pop) {
6473
MutatorsStack stack;
6574
SkMatrix matrix;
@@ -113,6 +122,8 @@ TEST(MutatorsStack, Equality) {
113122
stack.PushClipRRect(rrect);
114123
SkPath path;
115124
stack.PushClipPath(path);
125+
int alpha = 240;
126+
stack.PushOpacity(alpha);
116127

117128
MutatorsStack stackOther;
118129
SkMatrix matrixOther = SkMatrix::MakeScale(1, 1);
@@ -123,6 +134,8 @@ TEST(MutatorsStack, Equality) {
123134
stackOther.PushClipRRect(rrectOther);
124135
SkPath otherPath;
125136
stackOther.PushClipPath(otherPath);
137+
int otherAlpha = 240;
138+
stackOther.PushOpacity(otherAlpha);
126139

127140
ASSERT_TRUE(stack == stackOther);
128141
}
@@ -148,6 +161,10 @@ TEST(Mutator, Initialization) {
148161
Mutator mutator4 = Mutator(matrix);
149162
ASSERT_TRUE(mutator4.GetType() == MutatorType::transform);
150163
ASSERT_TRUE(mutator4.GetMatrix() == matrix);
164+
165+
int alpha = 240;
166+
Mutator mutator5 = Mutator(alpha);
167+
ASSERT_TRUE(mutator5.GetType() == MutatorType::opacity);
151168
}
152169

153170
TEST(Mutator, CopyConstructor) {
@@ -171,6 +188,11 @@ TEST(Mutator, CopyConstructor) {
171188
Mutator mutator4 = Mutator(matrix);
172189
Mutator copy4 = Mutator(mutator4);
173190
ASSERT_TRUE(mutator4 == copy4);
191+
192+
int alpha = 240;
193+
Mutator mutator5 = Mutator(alpha);
194+
Mutator copy5 = Mutator(mutator5);
195+
ASSERT_TRUE(mutator5 == copy5);
174196
}
175197

176198
TEST(Mutator, Equality) {
@@ -195,6 +217,10 @@ TEST(Mutator, Equality) {
195217
flutter::Mutator otherMutator4 = flutter::Mutator(path);
196218
ASSERT_TRUE(mutator4 == otherMutator4);
197219
ASSERT_FALSE(mutator2 == mutator);
220+
int alpha = 240;
221+
Mutator mutator5 = Mutator(alpha);
222+
Mutator otherMutator5 = Mutator(alpha);
223+
ASSERT_TRUE(mutator5 == otherMutator5);
198224
}
199225

200226
TEST(Mutator, UnEquality) {
@@ -204,6 +230,12 @@ TEST(Mutator, UnEquality) {
204230
matrix.setIdentity();
205231
Mutator notEqualMutator = Mutator(matrix);
206232
ASSERT_TRUE(notEqualMutator != mutator);
233+
234+
int alpha = 240;
235+
int alpha2 = 241;
236+
Mutator mutator2 = Mutator(alpha);
237+
Mutator otherMutator2 = Mutator(alpha2);
238+
ASSERT_TRUE(mutator2 != otherMutator2);
207239
}
208240

209241
} // namespace testing

shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@
278278
head = clipView;
279279
break;
280280
}
281+
case opacity:
282+
embedded_view.alpha = (*iter)->GetAlphaFloat() * embedded_view.alpha;
283+
break;
281284
}
282285
++iter;
283286
}
@@ -299,6 +302,7 @@
299302
UIView* touchInterceptor = touch_interceptors_[view_id].get();
300303
touchInterceptor.layer.transform = CATransform3DIdentity;
301304
touchInterceptor.frame = frame;
305+
touchInterceptor.alpha = 1;
302306

303307
int currentClippingCount = CountClips(params.mutatorsStack);
304308
int previousClippingCount = clip_count_[view_id];

0 commit comments

Comments
 (0)