Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update menu right when needed, rather than periodically #241

Closed
fabiospampinato opened this issue Nov 11, 2021 · 8 comments
Closed

Update menu right when needed, rather than periodically #241

fabiospampinato opened this issue Nov 11, 2021 · 8 comments
Labels
enhancement New feature or request
Milestone

Comments

@fabiospampinato
Copy link

Is it possible to update the menu right when the menubar item is clicked rather than periodically?

An example use case: I'd like to have something that lists all ejectable drives, so that I can eject them from there, the problem is I want the list to be up-to-date when I click on it, or it may show me old drives or not show me new ones, but I also don't want to update the menu every second as that would be wasteful since most seconds I won't be using that menu.

Is this implementable?

@melonamin
Copy link
Contributor

Can you give me your eject drive script? I'll use it to test if this is reasonably doable

@fabiospampinato
Copy link
Author

I haven't written it yet, I had an alfred workflow for doing that that I'd like to migrate to SwiftBar potentially.

For what is worth this is the code inside that workflow:

/* IMPORT */

const fs = require ( 'fs' ),
      ls = require ( 'ls' ),
      njds = require ( 'nodejs-disks' );

/* LIST */

const list = { items: [] },
      volumes = ls ( '/Volumes/*' );

njds.drives ( ( err, drives ) => {

  njds.drivesDetail ( drives, ( err, data ) => {

    for ( let {mountpoint, drive} of data ) {

      if ( !/^\/Volumes\//.test ( mountpoint ) ) continue;
      if ( /^\/Volumes\/BOOTCAMP/.test ( mountpoint ) ) continue;

      const volume = volumes.find ( volume => volume.full.startsWith ( mountpoint ) );

      if ( !volume ) continue;

      const pathParts = drive.match ( /(\/dev\/disk\d+)s\d+/ );

      list.items.push ({
        title: volume.name,
        arg: pathParts[1]
      });

    }

    fs.writeFileSync ( 'list.json', JSON.stringify ( list, undefined, 2 ) );

  });

});

@melonamin
Copy link
Contributor

That will do, let me explore.

@fabiospampinato
Copy link
Author

A simpler similar use case may just be having the current time with seconds in the menu, you want it to be accurate but you don't want to update it every second.

@fabiospampinato
Copy link
Author

Coincidentally I have this issue also with a desktop app of mine, the menu is highly dynamic so it gets updated very often, but most of the times that's just performance thrown out of the window as the user isn't going to open the menu before it changes again.

If you can find a way to provide macOS with a "just in time" menu I'd be very interested in applying that to the desktop app as well.

@melonamin melonamin added this to the 1.4.1 milestone Nov 19, 2021
@melonamin
Copy link
Contributor

A simpler similar use case may just be having the current time with seconds in the menu, you want it to be accurate but you don't want to update it every second.

This is not a good test in case of SwiftBar. Currently menu updates as a whole, I don't track individual menu items and adding this would explode the complexity.

I've added a naive implementation - SwiftBar runs the script right after you click and before showing the menu, if script running long it adds an indicator next to the menubar item. It should work just fine in case of "lists all ejectable drives" plugin.

Check out the beta

@xilopaint
Copy link
Contributor

Check out the beta

Please, add the application file to the releases page.

@melonamin
Copy link
Contributor

done

@melonamin melonamin added the enhancement New feature or request label Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants