How to add St.ScrollView to Gnome shell Extension

After going through the source code of some gnome shell extension I was able to add the scrollview to my extension. I want to know how can I make it scrollable.

 const scrollView = new St.ScrollView({
        overlay_scrollbars:true, 
        hscrollbar_policy:2, 
        enable_mouse_scrolling: true, 
        x_expand: true, 
        y_expand: true,
          
         });    
         
       const scrollBase  = new PopupMenu.PopupMenuSection();
       scrollBase.actor.add_child(scrollView);
                
     
       for (let i = 0; i<30; i++) {    
     
         let pmItem = new PopupMenu.PopupMenuItem("This is item "+ i,{});
         //this._indicator.menu.addMenuItem(pmItem);            
          scrollBase.addMenuItem(pmItem); 
      
         }           
      
         // `Main.panel` is the actual panel you see at the top of the screen,
        // not a class constructor.
        
        
        this._indicator.menu.addMenuItem(scrollBase);
         
        Main.panel.addToStatusArea(indicatorName, this._indicator);
    
    }

StScrollView is a container that provides scrollbars to its scrollable child.

That means

  1. the scrolled contents have to be inside the ScrollView, not added alongside
  2. the view’s child must implement the StScrollable interface (like StBoxLayout and StViewport)

So what you want is roughly along those lines:

const scrollView = new St.ScrollView();
this._indicator.menu.actor.add_child(scrollView);

const section = new PopupMenu.PopupMenuSection();
scrollView.set_child(section);

for (let i = 0; i < 30; i++) {
    const item = new PopupMenu.PopupMenuItem(`This is item ${i}`);
    section.addMenuItem(item);
}
1 Like

Okay, thank you. I understand that now.
Are there any isolated tutorials available for st and clutter for GJS?

At the moment, no, mostly because Clutter and St together are a pretty big topic.

Personally, I’m not a Clutter expert, but St.ScrollView is probably a good place to start, so maybe I can pick a few similar topics to start with (time permitting). We do have a bit of an Architecture Overview which might help a bit though.

As a beginner if I can help please let me know. I want to get involved so I can learn too!

1 Like

Absolutely, that’s a type of help we really need!

If you figure out how to do something (even something small like using St.ScrollView), it would be very helpful to open a merge request here for gjs.guide.

If you look at an accepted merge request like this one there are some simple examples of adding a page, with the extra bits for the sidebar and overview page. If you’re new to git, you’re welcome to do that all in the web interface and then request a review from me or @jrahmatzadeh.

1 Like

I am working on it. Thank you.

1 Like

I still couldn’t figure out how to add the scrollview. I understand that the menu items should be on the scrollview in order for them to be scrollable. I don’t understand how can I get a reference to the scrollview container once I add it to the menu.

I added the scrollview to the menu like this

this._indicator.menu.box.add_child(scrollView);

Thank You.

No:

  1. StScrollView can only have a single child
  2. the child must implement the StScrollable interface to be scrollable

That’s why I didn’t tell you to add menu items to the scroll view, but a menu section. PopupMenuSection uses an StBoxLayout as its actor, which is one of the widgets that implements StScrollable.

I did try your solution but i get the following error. I don’t understand what I am missing.

const scrollView = new St.ScrollView({
        overlay_scrollbars:true, 
        hscrollbar_policy:2, 
        enable_mouse_scrolling: true, 
        x_expand: true, 
        y_expand: true,
          
         });   
       
      this._indicator.menu.box.add_child(scrollView);  
      
     const section = new PopupMenu.PopupMenuSection();
     scrollView.set_child(section.actor);
          
     
       for (let i = 0; i<30; i++) {    
     
         let pmItem = new PopupMenu.PopupMenuItem("This is item "+ i,{});
         section.addMenuItem(pmItem);
      
         }   
             
        Main.panel.addToStatusArea(indicatorName, this._indicator);
    
    }


popupmenu_error

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.