Skip to content

Commit

Permalink
Ensure toolbar buttons toggle widgets, and turn off when widgets are …
Browse files Browse the repository at this point in the history
…closed. (#1763)

* Enable toggle on all relevant toolbar tools.

- Note that closing the tool directly does not update the toolbar button yet.

* Ensure that only one tool is active at a time

* Make sure that buttons untoggle when widget is closed

* Document the cleanup_toolbar_item decorator

* Add unit tests for toolbar cleanup functionality

* Allow multiple widgets, code cleanup

* Removed unused webbrowser

---------

Co-authored-by: Sufyan Abbasi <sufy@google.com>
Co-authored-by: Qiusheng Wu <giswqs@gmail.com>
  • Loading branch information
3 people authored Oct 7, 2023
1 parent eb1bdb5 commit 6aad7d2
Show file tree
Hide file tree
Showing 4 changed files with 413 additions and 277 deletions.
28 changes: 21 additions & 7 deletions geemap/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging
import math
from typing import Any, Dict, List, Optional, Sequence, Tuple, Type
import webbrowser

import ee
import ipyleaflet
Expand Down Expand Up @@ -765,25 +764,40 @@ def add_layer(

addLayer = add_layer

def _open_help_page(self, host_map: MapInterface, selected: bool) -> None:
del host_map # Unused.
def _open_help_page(
self, host_map: MapInterface, selected: bool, item: toolbar.Toolbar.Item
) -> None:
del host_map, item # Unused.
if selected:
common.open_url("https://geemap.org")

def _toolbar_main_tools(self) -> List[toolbar.Toolbar.Item]:
@toolbar._cleanup_toolbar_item
def inspector_tool_callback(
map: Map, selected: bool, item: toolbar.Toolbar.Item
):
del selected, item # Unused.
map.add("inspector")
return map._inspector

@toolbar._cleanup_toolbar_item
def basemap_tool_callback(map: Map, selected: bool, item: toolbar.Toolbar.Item):
del selected, item # Unused.
map.add("basemap_selector")
return map._basemap_selector

return [
toolbar.Toolbar.Item(
icon="map",
tooltip="Basemap selector",
callback=lambda m, selected: m.add("basemap_selector")
if selected
else None,
callback=basemap_tool_callback,
reset=False,
),
toolbar.Toolbar.Item(
icon="info",
tooltip="Inspector",
callback=lambda m, selected: m.add("inspector") if selected else None,
callback=inspector_tool_callback,
reset=False,
),
toolbar.Toolbar.Item(
icon="question", tooltip="Get help", callback=self._open_help_page
Expand Down
19 changes: 13 additions & 6 deletions geemap/map_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,14 @@ def __init__(
children=[self.toolbar_header, self.inspector_checks, self.tree_output]
)

def cleanup(self):
"""Removes the widget from the map and performs cleanup."""
if self._host_map:
self._host_map.default_style = {"cursor": "default"}
self._host_map.on_interaction(self._on_map_interaction, remove=True)
if self.on_close is not None:
self.on_close()

def _create_checkbox(self, title, checked):
layout = ipywidgets.Layout(width="auto", padding="0px 6px 0px 0px")
return ipywidgets.Checkbox(
Expand Down Expand Up @@ -578,11 +586,7 @@ def _on_toolbar_btn_click(self, change):

def _on_close_btn_click(self, change):
if change["new"]:
if self._host_map:
self._host_map.default_style = {"cursor": "default"}
self._host_map.on_interaction(self._on_map_interaction, remove=True)
if self.on_close is not None:
self.on_close()
self.cleanup()

def _get_visible_map_layers(self):
layers = {}
Expand Down Expand Up @@ -906,10 +910,13 @@ def _on_dropdown_click(self, change):
if self.on_basemap_changed and change["new"]:
self.on_basemap_changed(self._dropdown.value)

def _on_close_click(self, _):
def cleanup(self):
if self.on_close:
self.on_close()

def _on_close_click(self, _):
self.cleanup()


@Theme.apply
class LayerEditor(ipywidgets.VBox):
Expand Down
Loading

0 comments on commit 6aad7d2

Please sign in to comment.