11import {
2+ ILayoutRestorer ,
23 JupyterFrontEnd ,
34 JupyterFrontEndPlugin
45} from '@jupyterlab/application' ;
5- import { ICommandPalette } from '@jupyterlab/apputils' ;
6+ import {
7+ ICommandPalette ,
8+ WidgetTracker
9+ } from '@jupyterlab/apputils' ;
610import { ILauncher } from '@jupyterlab/launcher' ;
711import { imageIcon } from '@jupyterlab/ui-components' ;
812
@@ -17,12 +21,14 @@ const plugin: JupyterFrontEndPlugin<void> = {
1721 description : 'A JupyterLab extension that displays a random image and caption.' ,
1822 autoStart : true ,
1923 requires : [ ICommandPalette , ILauncher ] , // dependencies of our extension
24+ optional : [ ILayoutRestorer ] ,
2025 activate : (
2126 app : JupyterFrontEnd ,
2227 // The activation method receives dependencies in the order they are specified in
2328 // the "requires" parameter above:
2429 palette : ICommandPalette ,
25- launcher : ILauncher
30+ launcher : ILauncher ,
31+ restorer : ILayoutRestorer | null
2632 ) => {
2733 console . log ( 'JupyterLab extension jupytercon2025-extension-workshop is activated!' ) ;
2834
@@ -36,13 +42,30 @@ const plugin: JupyterFrontEndPlugin<void> = {
3642 ) ;
3743 } ) ;
3844
45+ // Track widget state
46+ const tracker = new WidgetTracker < ImageCaptionMainAreaWidget > ( {
47+ namespace : 'jupytercon2025-extension-workshop'
48+ } ) ;
49+
3950 //Register a new command:
4051 const command_id = 'image-caption:open' ;
4152 app . commands . addCommand ( command_id , {
42- execute : ( ) => {
53+ execute : ( args ?: { id ?: string } ) => {
4354 // When the command is executed, create a new instance of our widget
4455 const widget = new ImageCaptionMainAreaWidget ( ) ;
4556
57+ // Use provided ID or generate a new one
58+ // During restoration, the args will contain the saved widget ID
59+ if ( args && args . id ) {
60+ widget . id = args . id ;
61+ } else {
62+ widget . id = `image-caption-${ crypto . randomUUID ( ) } ` ;
63+ }
64+
65+ if ( ! tracker . has ( widget ) ) {
66+ tracker . add ( widget ) ;
67+ }
68+
4669 // Then add it to the main area:
4770 app . shell . add ( widget , 'main' ) ;
4871 return widget ;
@@ -53,6 +76,15 @@ const plugin: JupyterFrontEndPlugin<void> = {
5376
5477 palette . addItem ( { command : command_id , category : 'Tutorial' } ) ;
5578 launcher . add ( { command : command_id } ) ;
79+
80+ // Restore widget state
81+ if ( restorer ) {
82+ restorer . restore ( tracker , {
83+ command : command_id ,
84+ name : widget => widget . id ,
85+ args : widget => ( { id : widget . id } )
86+ } ) ;
87+ }
5688 }
5789} ;
5890
0 commit comments