 Hello and welcome to yet another episode of Nicola looks at some code and decides that it's mostly useless So he deletes it without losing any functionality in theory in practice It's all to be seen, but I think I did not remove anything. So today we are talking about Spacers and this discussion actually arise from, you know, an actual need an actual bug report that somebody reported That is you might not know this, but if you add Spacers to the panel at least two of them then everything that's in between those spacers like as an example Let's go with just the task manager will be centered It will be at the center the exact center of you know the screen, which is nice Now you might think that if you have even more spacers then what will happen is that You will have wow that looks bad. This is slightly better Okay, so you might think that what will happen if you have three spacers is that the first thing will be, you know 33% of the panel in the second thing will be 66% of the panel in and so on I mean more centering stuff like if you have two spacers The thing in the middle will be in the middle if you have three then you have beginning 33% 66% and then the end and if you add even more space or you know, even more stuff happens However, that simply doesn't happen. Sadly sadly however, I've decided to make a patch to fix it and whilst looking at the code of how this patch of if you have two spacers Then the content in the center will be sent centered. I realized that the code was like much Much much longer than necessary. So I did this. This is my merger quest It's it's a merge request But it has been approved and I think it will land very shortly and you can see that it deletes 207 lines of code and we are talking about a code base that is like 250 lines of code something like that. So how it worked We have three main files the main dot comel file and the panel spacers Yes, we get slightly technical but as always, I think I can explain without boring you too much So the main come out is, you know, how the things look and the panel spacer is the how things behave In the background and this is C++. So whereas this is JavaScript a bit easier to understand So what I did to the C++ files is I basically removed everything that was in them everything So what were they doing and it's actually pretty easy to understand So we actually created these spacers trackers class and every time a new spacer is Created then this spacer is added to a list of known spacers and If this is the crazy part if there is already a spacer then the new spacer is like gets collected Connected sorry to the other space and they become twin spacers and there's a lot of logic about these twin spacers And of course if you remove them they are no longer twin spacers and all of this kind of stuff So the whole thing is about being able to set twin spacers Automatically what it means to have a twin spacer if you have two spacers in the panel One will be the twin of the other and the other will be the twin of the one This is completely unnecessary from my point of view and my implementation doesn't have this at all But let's actually see how this was used in actually drawing the things on the screen. So this is all the Logic and of course we read what is our twin spacers Of course, we check whether there is a twin spacer and we only do like magic things If we have a twin spacer if we do a twin spacer then we have two different properties Which is the left twin and the right twin and of course the left twin left twin is going to be the one on the left And the right between you guessed it is one is going to be the one on the right So what do we do we go into the panel panel layout? And we start iterating Reading all of the outlets that are in the panel and then we count first of all all of the things that are left Left compared to the left twin parent Then we see things that are in the center and then we see things that are to the right of the twin parent and we actually measure the width all of all these three components and When we know about these three widths, which are of course right heat and sink the length of the items to the right of the right twin the middle heat and Hind hint, which is the length of the Uplets in the middle between the two twins and then we have somewhere the left items hint Which is you know the length of the items to the left of the left twin This is crazy when we have these three values Then we can do some math and actually, you know We do a lot of math and then return what we think is the optimum optimal size all of this in my opinion is Completely completely unnecessary. So what do I do which is much shorter? Well, I still go through all of the Uplets in the panel, which is you know kind of necessary and then we actually count how many spacers and are in the panel and We count all the sizes between all those space spacers So we don't know we don't have the distinction left center and right. We just have a list of Dimensions and every time we meet a spacer We had a new item to it and every time we meet something that's not a spacer We had its length to the last element in the list. So at the end we have this list with all of the sizes and We are going to have one spacer between each size. So this menu code is Shorter much much shorter and also agnostic to the amount of spacers that you have in the panel And here's the important thing There's actually no need whatsoever to have a C++ files that tracks every time you add or remove spacers Because since we are going through all of the outlets in the panel anyway We can just read the spacers from there because we meet them and the important thing is that Whenever we meet a spacer which is here if the plugin name is spacer So we know it's a spacer then if this child that we just met this spacer is Us like plasmoid parent means us like these spacers that is drink the logic Then we know which one of all of the spacers in the panel. We are we found ourselves into all of the spacers and At this point we just take the size that of the elements to the right the size of the elements to the left And we do the logic based on that now the logic is actually Nice because I managed to keep it super concise. It's literally just this line slightly more technical I don't think you want to learn and to hear an explanation But this means that after this batch is applied You can have as many spacers as you want in the panel and the things would be kind of centered Like if you have I don't know like four spacers as an example instead of two The thing that are in the middle of those four will still be at the center Which is something that is not to be taken for granted right now so that that works now and also like Almost all of the code is gone, which is always a good thing. So just to end this video I want to say thanks to the patrons and October is almost over and I kind of want to eat some pizzas But pizzas are actually pretty expensive here. So if you feel like a gifting some pizzas that would be appreciated Let's put it like that. Thanks everybody for following along and see you tomorrow with another video