helloSystem / Menu

Global menu bar written in Qt
43 stars 14 forks source link

Automatically update the System menu using QFileSystemWatcher #15

Closed probonopd closed 3 years ago

probonopd commented 3 years ago

The System menu now gets populated based on the filesystem structure on disk. E.g., you put an app into /Applications and it will show up in the menu.

Currently one has to restart the menu for changes to take effect.

Hence we should find a way to automatically update the System menu using QFileSystemWatcher whenever something changes in the directories that are put in the System menu:

image

probonopd commented 3 years ago

@antony-jr do you know how to do this?

antony-jr commented 3 years ago

Let me take a look at it.

antony-jr commented 3 years ago

Okay, QFileSystemWatcher only tells when a file is added into a directory or removed from a directory. Now to implement the auto integration. We must maintain a record on the Application we have already integrated into the menu and possibly store that info into some config file or local DB(sql?) and watch for changes in the directory and simply change the local DB and update the menu whenever a file is added or removed from the directories that we are watching.

If I implement this now without the DB part then everything will be in memory and the whole process is done on the startup of the Menu bar. For now if you have like 20 Apps or so it won't seem like a big thing because of our modern hardware. But if we are going to keep 100+ Apps, You will definitely see the degrade in performance. And even heavy usage of memory.

So I need to know how are you aiming to keep record of your application? I think KDE and Gnome uses a combination of config and local dbs? I have no idea.

probonopd commented 3 years ago

Currently, no database is implemented yet. I originally thought one would be needed, but when I implemented https://github.com/helloSystem/launch (which scans well-known locations for applications) I added a timer to count how long it takes... in most cases around 10ms, which I think is accaptable.

So all I need is the System menu to get re-generated (like it is done when the menu is started) whenever something changes in one of the directories watched by the QFileSystemWatcher.

If things prove to be too slow, we can always introduce a database later on. (It should be shared with https://github.com/helloSystem/launch.) Having a database has its own downsides, e.g., we would need to make sure it is always in sync with what is going on in the filesystem.

antony-jr commented 3 years ago

Okay let me send you a PR.

probonopd commented 3 years ago

@antony-jr do you think you might still be able to send that PR? Would really appreciate it. Thanks a ton.

antony-jr commented 3 years ago

@antony-jr do you think you might still be able to send that PR? Would really appreciate it. Thanks a ton.

My computer is currently out of order, Maybe in three days when I buy a new one and setup my distro? I really like to help though. :)

probonopd commented 3 years ago

Could it be as easy as calling

https://github.com/helloSystem/Menu/blob/aaf84275a9eb1c3aed494bc30b8665b2afe9440c/src/appmenuwidget.cpp#L191

when we get notified by QFileSystemWatcher?

antony-jr commented 3 years ago

Could it be as easy as calling

https://github.com/helloSystem/Menu/blob/aaf84275a9eb1c3aed494bc30b8665b2afe9440c/src/appmenuwidget.cpp#L191

when we get notified by QFileSystemWatcher?

I don't think that's right, Working on this right now so please wait.

antony-jr commented 3 years ago

Please test https://github.com/helloSystem/Menu/pull/43

probonopd commented 3 years ago

Hello @antony-jr I think I succeeded adding the QFileSystemWatcher in the way I would like to, but now we need to find a way to make this function do something:

https://github.com/helloSystem/Menu/blob/d3c191838428a2496f7a37618d5af32b468c8643/src/appmenuwidget.cpp#L453-L457

It basically should rebuild the whole System menu.

Do you know how to do that?

antony-jr commented 3 years ago

Hello @antony-jr I think I succeeded adding the QFileSystemWatcher in the way I would like to, but now we need to find a way to make this function do something:

https://github.com/helloSystem/Menu/blob/d3c191838428a2496f7a37618d5af32b468c8643/src/appmenuwidget.cpp#L453-L457

It basically should rebuild the whole System menu.

Do you know how to do that?

Yes I was thinking about rebuilding the menu itself on every update. But I used QFileSystemModel so I have to replace that to get which directories have added files and run a check on finding which was changed and rebuild the System Menu.

probonopd commented 3 years ago

Is there an easier way without a QFileSystemModel?

antony-jr commented 3 years ago

Is there an easier way without a QFileSystemModel?

Yes it is QFileSystemWatcher, both are very similar and QFileSystemModel uses QFileSystemWatcher internally. Only difference is that QFileSystemModel exactly tells the file which was added in a directory(with the path of the new file). But with QFileSystemWatcher we only get the directory in which some file was added or deleted or modified, So we have to check our registered application and the filesystem to find which was newly added and then rebuild the menu. When a application is removed we have to do the same thing again.

probonopd commented 3 years ago

My approach is much simpler: Rather than trying to figure out what has changed, just throw away the entire 'System' menu and rebuild it from scratch (in the same way like when the application is started initially). This happens so infrequently that optimizing for speed is not important here.

The only thing I need help with: How to rebuild the entire 'System' menu?

antony-jr commented 3 years ago

My approach is much simpler: Rather than trying to figure out what has changed, just throw away the entire 'System' menu and rebuild it from scratch (in the same way like when the application is started initially). This happens so infrequently that optimizing for speed is not important here.

The only thing I need help with: How to rebuild the entire 'System' menu?

That's not hard. Have to make it async so that it won't block the GUI thread, that's all.

probonopd commented 3 years ago

Could you help me there please? That'd be great

probonopd commented 3 years ago

Closed in https://github.com/helloSystem/Menu/pull/54