nakas Claude commited on
Commit
ff2c24d
Β·
1 Parent(s): fc945f6

Fix theme toggle functionality with Dark Matter and Light Matter labels

Browse files

- Updated theme toggle to properly switch between CartoDB tiles
- Replaced unreliable layer detection with direct tile layer management
- Updated button labels to "Dark Matter" and "Light Matter" as requested
- Fixed theme detection for particle color adaptation
- Improved tile layer switching with proper cleanup and replacement

πŸ€– Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +36 -40
app.py CHANGED
@@ -404,7 +404,7 @@ def create_wind_map(region="global"):
404
  # Add light theme option (but don't make it active by default)
405
  folium.TileLayer(
406
  tiles="CartoDB positron",
407
- name="Light Theme",
408
  control=False # We'll control this with our custom button
409
  ).add_to(m)
410
 
@@ -453,16 +453,15 @@ def create_wind_map(region="global"):
453
  // Check if we're on light theme by looking at current tile layer
454
  var currentTileLayer = 'dark'; // default to dark
455
  map.eachLayer(function(layer) {{
456
- if (layer.options && layer.options.tiles) {{
457
- if (layer.options.tiles.includes('positron') || layer.options.tiles.includes('light')) {{
 
458
  currentTileLayer = 'light';
459
  }}
460
  }}
461
- // Also check the layer name/attribution
462
- if (layer.options && layer.options.name === 'Light Theme') {{
463
- if (map.hasLayer(layer)) {{
464
- currentTileLayer = 'light';
465
- }}
466
  }}
467
  }});
468
 
@@ -725,7 +724,7 @@ def create_wind_map(region="global"):
725
 
726
  // Add theme toggle button
727
  var buttonTheme = L.DomUtil.create('button', 'theme-toggle', windControlDiv);
728
- buttonTheme.innerHTML = 'πŸŒ™ Dark Theme';
729
  buttonTheme.style.display = 'block';
730
  buttonTheme.style.width = '120px';
731
  buttonTheme.style.margin = '2px 0';
@@ -752,53 +751,50 @@ def create_wind_map(region="global"):
752
 
753
  // Theme toggle functionality
754
  var isLightTheme = false; // Start with dark theme
755
- var darkTileLayer = null;
756
- var lightTileLayer = null;
757
 
758
- // Find the tile layers
759
  map.eachLayer(function(layer) {{
760
- if (layer.options && layer.options.tiles) {{
761
- if (layer.options.tiles.includes('dark_matter')) {{
762
- darkTileLayer = layer;
763
- console.log("πŸŒ™ Found dark tile layer");
764
- }} else if (layer.options.tiles.includes('positron')) {{
765
- lightTileLayer = layer;
766
- console.log("β˜€οΈ Found light tile layer");
767
- // Remove light layer by default (start with dark)
768
- map.removeLayer(layer);
769
- }}
770
  }}
771
  }});
772
 
773
  L.DomEvent.on(buttonTheme, 'click', function() {{
774
  isLightTheme = !isLightTheme;
775
 
 
 
 
 
 
776
  if (isLightTheme) {{
777
- // Switch to light theme
778
- console.log("🌞 Switching to LIGHT theme...");
779
- if (darkTileLayer && map.hasLayer(darkTileLayer)) {{
780
- map.removeLayer(darkTileLayer);
781
- }}
782
- if (lightTileLayer) {{
783
- map.addLayer(lightTileLayer);
784
- }}
785
- buttonTheme.innerHTML = 'β˜€οΈ Light Theme';
786
  buttonTheme.style.background = '#f59e0b';
787
  }} else {{
788
- // Switch to dark theme
789
- console.log("πŸŒ™ Switching to DARK theme...");
790
- if (lightTileLayer && map.hasLayer(lightTileLayer)) {{
791
- map.removeLayer(lightTileLayer);
792
- }}
793
- if (darkTileLayer) {{
794
- map.addLayer(darkTileLayer);
795
- }}
796
- buttonTheme.innerHTML = 'πŸŒ™ Dark Theme';
797
  buttonTheme.style.background = '#374151';
798
  }}
799
 
800
  // Trigger particle color update after theme change
801
  setTimeout(reloadParticlesWithTheme, 200);
 
802
  }});
803
 
804
  // Add controls to map
 
404
  # Add light theme option (but don't make it active by default)
405
  folium.TileLayer(
406
  tiles="CartoDB positron",
407
+ name="Light Matter",
408
  control=False # We'll control this with our custom button
409
  ).add_to(m)
410
 
 
453
  // Check if we're on light theme by looking at current tile layer
454
  var currentTileLayer = 'dark'; // default to dark
455
  map.eachLayer(function(layer) {{
456
+ if (layer.options && layer.options.attribution) {{
457
+ if (layer.options.attribution.includes('Light Matter') ||
458
+ layer.options.attribution.includes('light_all')) {{
459
  currentTileLayer = 'light';
460
  }}
461
  }}
462
+ // Also check tile URL patterns
463
+ if (layer._url && layer._url.includes('light_all')) {{
464
+ currentTileLayer = 'light';
 
 
465
  }}
466
  }});
467
 
 
724
 
725
  // Add theme toggle button
726
  var buttonTheme = L.DomUtil.create('button', 'theme-toggle', windControlDiv);
727
+ buttonTheme.innerHTML = 'πŸŒ™ Dark Matter';
728
  buttonTheme.style.display = 'block';
729
  buttonTheme.style.width = '120px';
730
  buttonTheme.style.margin = '2px 0';
 
751
 
752
  // Theme toggle functionality
753
  var isLightTheme = false; // Start with dark theme
754
+ var currentTileLayer = null;
 
755
 
756
+ // Find and store the current base layer
757
  map.eachLayer(function(layer) {{
758
+ if (layer.options && layer.options.attribution && layer.options.attribution.includes('CartoDB')) {{
759
+ currentTileLayer = layer;
 
 
 
 
 
 
 
 
760
  }}
761
  }});
762
 
763
  L.DomEvent.on(buttonTheme, 'click', function() {{
764
  isLightTheme = !isLightTheme;
765
 
766
+ // Remove current tile layer
767
+ if (currentTileLayer) {{
768
+ map.removeLayer(currentTileLayer);
769
+ }}
770
+
771
  if (isLightTheme) {{
772
+ // Switch to light matter theme
773
+ console.log("🌞 Switching to LIGHT MATTER theme...");
774
+ currentTileLayer = L.tileLayer('https://{{s}}.basemaps.cartocdn.com/light_all/{{z}}/{{x}}/{{y}}{{r}}.png', {{
775
+ attribution: 'Β© CartoDB Light Matter',
776
+ subdomains: 'abcd',
777
+ maxZoom: 19
778
+ }});
779
+ currentTileLayer.addTo(map);
780
+ buttonTheme.innerHTML = 'β˜€οΈ Light Matter';
781
  buttonTheme.style.background = '#f59e0b';
782
  }} else {{
783
+ // Switch to dark matter theme
784
+ console.log("πŸŒ™ Switching to DARK MATTER theme...");
785
+ currentTileLayer = L.tileLayer('https://{{s}}.basemaps.cartocdn.com/dark_all/{{z}}/{{x}}/{{y}}{{r}}.png', {{
786
+ attribution: 'Β© CartoDB Dark Matter',
787
+ subdomains: 'abcd',
788
+ maxZoom: 19
789
+ }});
790
+ currentTileLayer.addTo(map);
791
+ buttonTheme.innerHTML = 'πŸŒ™ Dark Matter';
792
  buttonTheme.style.background = '#374151';
793
  }}
794
 
795
  // Trigger particle color update after theme change
796
  setTimeout(reloadParticlesWithTheme, 200);
797
+ console.log("βœ… Theme switched successfully!");
798
  }});
799
 
800
  // Add controls to map