nwg-piotr / nwg-drawer

Application drawer for wlroots-based Wayland compositors
MIT License
240 stars 25 forks source link

Custom categories #56

Closed Figuera closed 1 year ago

Figuera commented 2 years ago

Adding Custom Categories

The changes in this pull request add the ability for the dynamic creating of categories, allow of user defined categories to be user by nwg-drawer

Spiel

I want to spend some time convincing you that this changes are not only something necessary to cover my "very specific use case", I also think that the changes make the code simpler and easier to maintain. I have deleted more lines than I have added.

The current category code is using a case by case basis which imply that any modification or addition of categories means the developer have to modify the code in multiple sections, increasing work load and margin for error. My changes try to parametrize the process making it more dynamic and easier to deal with (in my opinion).

Changes

Brief explanation of the modification in each file.

main.go

The main change here is the exclusion of the listCategory* global variables, this is replaced by a new property in the category struct: Apps. That is what was previously saved in listUtility is now saved in categories[0].Apps.

The second change is the modification of the global variable categoryNames to categoryMatches. In the current code multiple entries' category are grouped in the same umbrella category by using a list of hardcoded matches. I moved the parametrization from the assignToList function to categoryMatches.

tools.go

I will start on the end of the File. assignToList function was rewritten and now it uses two for loops to go through all the possible categories and assigning the desktop entries to cat.App if the categories on the entry matches cat.Matches. (I just noticed I am not checking if the entry is not already on the category, have to fix that).

The other change is the introduction of the JSON configuration file. The program looks for $XDG_CONFIG_HOME/nwd-drawer/categories.json and adds any categories defined there, an example of a configuration file is:

[
  {
    "Name": "Retroarch",
    "DisplayName": "Retroarch",
    "Icon": "applications-games",
    "Matches": ["Retroarch"],
  }
]

Which creates a Category named Retrorarch.

To be honest I dislike the fact that this method of creating categories is different from the current one. But I think that using one file to create all user defined categories is more friendly and have to user define multiple *.directory file, also JSON is more well known and widely used which also contributes to user friendliness. I tried transforming the current *.directory files to JSON file(s) but that proved hard to do. I am looking forward for you opinion on the subject.

uicomponents.go

There is not much to talk about here, all references to listCategory* are changed to cat.Apps.

Sorry to give you trouble. Take your time going though this. I have tested this and it looks to be working nicely. I will keep using it though to see if I notice any hidden problems.

nwg-piotr commented 2 years ago

I promise to consider your changes this weekend.