falzonv / discreet-launcher

Enjoy a clean home screen while accessing everything in an instant! (Profitez d'un écran d'accueil épuré tout en accédant à tout en un instant !)
https://vincent-falzon.com
GNU General Public License v3.0
216 stars 41 forks source link

Time is not perfectly centered #328

Open antodc opened 4 months ago

antodc commented 4 months ago

When the clock is activated, the time is shown slightly on the right, while I would expect it to be perfectly centered in the screen. The issue is more evident when the (short) date is added, as it is (slightly) misaligned with respect to the time. My impression is that the date is fine, but the time is not. My settings are in Italian, but the issue is still there if I use English. Same issue also on my wife's phone.

falzonv commented 3 months ago

Hello,

Thank you for the report, I am surprised to never have noticed it before! Although to be fair, I only use the clock while developing or testing, not in my daily use of the launcher...

The last significant change to the clock was a bit more than one year ago, when adding new clock formats. I am pretty sure everything was fine before so probably a mistake with horizontal placement was introduced at that time, I will have a look.

Best regards

falzonv commented 2 weeks ago

Hello,

For info I also tried to fix this but haven't found yet what causes the issue. The date is properly centered but the time is not, even thought it seems to use the same formulas for horizontal position, very weird... I continue to investigate but it may take more time than I expected to fix.

Best regards

antodc commented 2 weeks ago

Thanks a lot for your feedback and your efforts!! Could it be an internal issue with the specific clock that is used? Maybe it has a small bug that makes it asymmetric around the colon.

falzonv commented 1 week ago

Hello,

Thank you for the idea! Internally both time and date are inserted as simple texts, the respective widths of these texts are computed using Android's method getTextBounds() and then used for the formulas. Maybe this method doesn't handle the measurement of the colon (or any non-alphanumeric character) properly... If this is the case, I guess a workaround could be to temporarily replace ":" with "i" during measurement.

I will check that when I have some time :-)

Best regards

antodc commented 1 week ago

I see your point, but there should be something else. I noticed that sometimes the clock moves slightly when the time changes. For instance, it goes to the right from 06:40 to 06:41. So, that function considers a “0” larger than a “1”. Maybe you should compute the difference between the bounds of 00:00 (which should be symmetric around the colon) and those of the real time and use that difference to adjust the position of the clock. No idea whether this is really of any help, but thanks a lot for your attention.

kaanelloed commented 1 day ago

Even the date is slightly off-center. The problem is that Paint.getTextBounds() gets the exact bounds of the text, whereas canvas.drawText() adds a space at the beginning and end. When the width is obtained with Rect.width(), the space at the beginning is ignored; the drawn text is therefore larger than the calculated bounds.. See this comment for an example.

We can fix this in three ways.

First way Remove the Rect.left when drawing the text. Text should be perfectly centered.

canvas.drawText(time_text, offset_x - rect_time.left, offset_y, textClock) ;

Second way Get the width with Paint.measureText(). Text can still be slightly off-center if the space at the beginning is different from the space at the end.

float time_width = textClock.measureText(time_text) ;

Third way For information purposes only, we're essentially doing the first option, but with a path. We retrieve the path from the text, calculate the path bounds and draw the path instead of the text.

//rect_time is now a RectF instead of a Rect
Path path_time = new Path(); //In constructor
textClock.getTextPath(time_text, 0, time_text.length(), 0f, 0f, path_time);
path_time.computeBounds(rect_time, true);

//[...]

textClock.getTextPath(time_text, 0, time_text.length(), offset_x - rect_time.left, offset_y, path_time);
canvas.drawPath(path_time, textClock);