[core] Observe visibility changes
This commit is contained in:
@@ -22,7 +22,7 @@
|
||||
"express": "^4.11.1",
|
||||
"mapbox-gl-shaders": "mapbox/mapbox-gl-shaders#de2ab007455aa2587c552694c68583f94c9f2747",
|
||||
"mapbox-gl-style-spec": "mapbox/mapbox-gl-style-spec#83b1a3e5837d785af582efd5ed1a212f2df6a4ae",
|
||||
"mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#ac0e1907ff628e662dafeb44cdab6e594b59a59b",
|
||||
"mapbox-gl-test-suite": "mapbox/mapbox-gl-test-suite#5b2e5749e85ad4241b5b4e4e54d2fa98e8920f34",
|
||||
"mkdirp": "^0.5.1",
|
||||
"node-cmake": "^1.2.1",
|
||||
"request": "^2.72.0",
|
||||
|
||||
@@ -19,7 +19,10 @@ VisibilityType Layer::getVisibility() const {
|
||||
}
|
||||
|
||||
void Layer::setVisibility(VisibilityType value) {
|
||||
if (value == getVisibility())
|
||||
return;
|
||||
baseImpl->visibility = value;
|
||||
baseImpl->observer->onLayerVisibilityChanged(*this);
|
||||
}
|
||||
|
||||
float Layer::getMinZoom() const {
|
||||
|
||||
@@ -10,6 +10,7 @@ public:
|
||||
virtual ~LayerObserver() = default;
|
||||
|
||||
virtual void onLayerFilterChanged(Layer&) {}
|
||||
virtual void onLayerVisibilityChanged(Layer&) {}
|
||||
virtual void onLayerPaintPropertyChanged(Layer&) {}
|
||||
virtual void onLayerLayoutPropertyChanged(Layer&) {}
|
||||
};
|
||||
|
||||
@@ -506,9 +506,11 @@ void Style::onSpriteError(std::exception_ptr error) {
|
||||
struct QueueSourceReloadVisitor {
|
||||
UpdateBatch& updateBatch;
|
||||
|
||||
void operator()(CustomLayer&) { assert(false); }
|
||||
void operator()(RasterLayer&) { assert(false); }
|
||||
void operator()(BackgroundLayer&) { assert(false); }
|
||||
// No need to reload sources for these types; their visibility can change but
|
||||
// they don't participate in layout.
|
||||
void operator()(CustomLayer&) {}
|
||||
void operator()(RasterLayer&) {}
|
||||
void operator()(BackgroundLayer&) {}
|
||||
|
||||
template <class VectorLayer>
|
||||
void operator()(VectorLayer& layer) {
|
||||
@@ -521,6 +523,11 @@ void Style::onLayerFilterChanged(Layer& layer) {
|
||||
observer->onUpdate(Update::Layout);
|
||||
}
|
||||
|
||||
void Style::onLayerVisibilityChanged(Layer& layer) {
|
||||
layer.accept(QueueSourceReloadVisitor { updateBatch });
|
||||
observer->onUpdate(Update::Layout);
|
||||
}
|
||||
|
||||
void Style::onLayerPaintPropertyChanged(Layer&) {
|
||||
observer->onUpdate(Update::RecalculateStyle | Update::Classes);
|
||||
}
|
||||
|
||||
@@ -136,6 +136,7 @@ private:
|
||||
|
||||
// LayerObserver implementation.
|
||||
void onLayerFilterChanged(Layer&) override;
|
||||
void onLayerVisibilityChanged(Layer&) override;
|
||||
void onLayerPaintPropertyChanged(Layer&) override;
|
||||
void onLayerLayoutPropertyChanged(Layer&) override;
|
||||
|
||||
|
||||
@@ -14,6 +14,10 @@ public:
|
||||
if (layerFilterChanged) layerFilterChanged(layer);
|
||||
}
|
||||
|
||||
void onLayerVisibilityChanged(Layer& layer) override {
|
||||
if (layerVisibilityChanged) layerVisibilityChanged(layer);
|
||||
}
|
||||
|
||||
void onLayerPaintPropertyChanged(Layer& layer) override {
|
||||
if (layerPaintPropertyChanged) layerPaintPropertyChanged(layer);
|
||||
}
|
||||
@@ -23,6 +27,7 @@ public:
|
||||
}
|
||||
|
||||
std::function<void (Layer&)> layerFilterChanged;
|
||||
std::function<void (Layer&)> layerVisibilityChanged;
|
||||
std::function<void (Layer&)> layerPaintPropertyChanged;
|
||||
std::function<void (Layer&)> layerLayoutPropertyChanged;
|
||||
};
|
||||
|
||||
@@ -226,6 +226,15 @@ TEST(Layer, Observer) {
|
||||
layer->setFilter(NullFilter());
|
||||
EXPECT_TRUE(filterChanged);
|
||||
|
||||
// Notifies observer on visibility change.
|
||||
bool visibilityChanged = false;
|
||||
observer.layerVisibilityChanged = [&] (Layer& layer_) {
|
||||
EXPECT_EQ(layer.get(), &layer_);
|
||||
visibilityChanged = true;
|
||||
};
|
||||
layer->setVisibility(VisibilityType::None);
|
||||
EXPECT_TRUE(visibilityChanged);
|
||||
|
||||
// Notifies observer on paint property change.
|
||||
bool paintPropertyChanged = false;
|
||||
observer.layerPaintPropertyChanged = [&] (Layer& layer_) {
|
||||
@@ -244,6 +253,11 @@ TEST(Layer, Observer) {
|
||||
layer->setLineCap(lineCap);
|
||||
EXPECT_TRUE(layoutPropertyChanged);
|
||||
|
||||
// Does not notify observer on no-op visibility change.
|
||||
visibilityChanged = false;
|
||||
layer->setVisibility(VisibilityType::None);
|
||||
EXPECT_FALSE(visibilityChanged);
|
||||
|
||||
// Does not notify observer on no-op paint property change.
|
||||
paintPropertyChanged = false;
|
||||
layer->setLineColor(color);
|
||||
|
||||
Reference in New Issue
Block a user