Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[core] Observe visibility changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Sep 6, 2016
1 parent 6d770cb commit d6f667a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions src/mbgl/style/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/style/layer_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class LayerObserver {
virtual ~LayerObserver() = default;

virtual void onLayerFilterChanged(Layer&) {}
virtual void onLayerVisibilityChanged(Layer&) {}
virtual void onLayerPaintPropertyChanged(Layer&) {}
virtual void onLayerLayoutPropertyChanged(Layer&) {}
};
Expand Down
13 changes: 10 additions & 3 deletions src/mbgl/style/style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions src/mbgl/style/style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class Style : public GlyphStoreObserver,

// LayerObserver implementation.
void onLayerFilterChanged(Layer&) override;
void onLayerVisibilityChanged(Layer&) override;
void onLayerPaintPropertyChanged(Layer&) override;
void onLayerLayoutPropertyChanged(Layer&) override;

Expand Down
5 changes: 5 additions & 0 deletions test/src/mbgl/test/stub_layer_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class StubLayerObserver : public style::LayerObserver {
if (layerFilterChanged) layerFilterChanged(layer);
}

void onLayerVisibilityChanged(Layer& layer) override {
if (layerVisibilityChanged) layerVisibilityChanged(layer);
}

void onLayerPaintPropertyChanged(Layer& layer) override {
if (layerPaintPropertyChanged) layerPaintPropertyChanged(layer);
}
Expand All @@ -23,6 +27,7 @@ class StubLayerObserver : public style::LayerObserver {
}

std::function<void (Layer&)> layerFilterChanged;
std::function<void (Layer&)> layerVisibilityChanged;
std::function<void (Layer&)> layerPaintPropertyChanged;
std::function<void (Layer&)> layerLayoutPropertyChanged;
};
14 changes: 14 additions & 0 deletions test/style/style_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_) {
Expand All @@ -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);
Expand Down

0 comments on commit d6f667a

Please sign in to comment.