Skip to content

Commit

Permalink
feat: closing viewer mdi windows (#6)
Browse files Browse the repository at this point in the history
* feat: closing viewer mdi windows

* chore: restrict solara to version >=1.40.0
We need at least version 1.40.0, since widgetti/solara#812 is needed for closing viewers to work.
  • Loading branch information
iisakkirotko authored Oct 16, 2024
1 parent d131063 commit d8ab017
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
13 changes: 12 additions & 1 deletion glue_solara/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from glue_jupyter.data import require_data
from solara import Reactive

from .hooks import use_glue_watch, use_layers_watch
from .hooks import ClosedMessage, use_glue_watch, use_glue_watch_close, use_layers_watch
from .linker import Linker
from .mdi import MDI_HEADER_SIZES, Mdi, MdiWindow
from .misc import Snackbar, ToolBar
Expand Down Expand Up @@ -73,6 +73,7 @@ def GlueApp(app: gj.JupyterApplication):
# TODO: check if we can limit the messages we listen to
# for better performance (less re-renders)
use_glue_watch(app.session.hub, glue.core.message.Message)
use_glue_watch_close(app)
data_collection = app.data_collection
viewer_index: Reactive[Optional[int]] = solara.use_reactive(None)
show_error = solara.use_reactive(False)
Expand Down Expand Up @@ -347,10 +348,20 @@ def on_windows(windows_layout):
sorted.sort(key=lambda x: x[1]["order"])
on_viewer_index(sorted[-1][0])

def on_close(index):
new_layouts = mdi_layouts.value
new_layouts.pop(index)
mdi_layouts.set(new_layouts)
on_viewer_index(None)
# Close the viewer
message = ClosedMessage(viewers[index])
viewers[index].session.hub.broadcast(message)

with Mdi(
children=layouts,
windows=mdi_layouts.value,
on_windows=on_windows,
event_close=on_close,
size=header_size,
):
pass
Expand Down
29 changes: 26 additions & 3 deletions glue_solara/hooks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import glue.core.hub
import glue.core.message
import glue_jupyter
import solara
from glue.viewers.common.viewer import Viewer

Expand All @@ -9,14 +10,20 @@ def notify(self, message):
pass


# Message indicating a viewer should be closed
class ClosedMessage(glue.core.message.Message):
def __init__(self, sender):
super().__init__(sender=sender)


helper = []


def use_glue_watch(hub: glue.core.hub.Hub, msg_class=glue.core.message.Message):
def use_glue_watch(hub: glue.core.hub.Hub, msg_class=glue.core.message.Message, on_msg=None):
# listener = solara.use_memo(lambda: DummyListener(), [])
counter, set_counter = solara.use_state(0)

def on_msg(msg):
def _on_msg(msg):
# breakpoint()
# print("MSG", msg, counter)
set_counter(lambda counter: counter + 1)
Expand All @@ -25,14 +32,30 @@ def connect():
listener = DummyListener()
helper.append(listener)
# breakpoint()
hub.subscribe(listener, msg_class, handler=on_msg)
hub.subscribe(listener, msg_class, handler=on_msg or _on_msg)
assert listener in hub._subscriptions
# def clean
# return lambda: hub.unsubscribe(listener, msg_class)

solara.use_effect(connect, [id(hub)])


def use_glue_watch_close(app: glue_jupyter.JupyterApplication):
counter, set_counter = solara.use_state(0)

def remove_viewer(msg):
viewers = app._viewer_refs
for _viewer in viewers:
viewer = _viewer()
if viewer is not None and viewer is msg.sender:
# TODO: a proper close method should be implemented in glue-jupyter
viewer.cleanup()
viewers.remove(_viewer)
set_counter(lambda counter: counter + 1)

use_glue_watch(app.session.hub, ClosedMessage, remove_viewer)


def use_layers_watch(viewers: list[Viewer]):
"""Using this hook causes the component to rerender when one of the viewers layers changes"""
# we use this in DataList, and we need to trigger *after* _layer_artist_container changes
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name = "glue-solara"
description = "Flexible Solara based Glue UI"
version = "0.0.1"
dependencies = [
"solara",
"solara>=1.40.0",
"glue-jupyter",
"ipypopout",
]
Expand Down

0 comments on commit d8ab017

Please sign in to comment.