Skip to content

Reduce sleep screen message's font size to fit one line#13026

Merged
Frenzie merged 9 commits into
koreader:masterfrom
Commodore64user:one-line-msg
Jan 12, 2025
Merged

Reduce sleep screen message's font size to fit one line#13026
Frenzie merged 9 commits into
koreader:masterfrom
Commodore64user:one-line-msg

Conversation

@Commodore64user

@Commodore64user Commodore64user commented Jan 8, 2025

Copy link
Copy Markdown
Member

what's new

  • Introduced the force_one_line property to the InfoMessage class to attempt to show text in a single line.
  • Modified the InfoMessage:init() function to calculate the maximum height of the widget based on the force_one_line property, ensuring that the text fits within the specified constraints.
  • Added the force_one_line property to the Screensaver:show() function to ensure the sleep screen message is displayed in a single line.

screenshots

before/after

fix #12109


This change is Reviewable

Comment thread frontend/ui/widget/infomessage.lua Outdated
Comment on lines +179 to +182
if self.shrink_to_fit then
self:shrinkFontToFit(frame, text_widget, 0.07)
elseif not self.height then
self:shrinkFontToFit(frame, text_widget, 0.95)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind moving the code into its own method.
But the 0.07 here feels a bit random in relation to a property name "shrink_to_fit"...
It looks like this makes the infomessage not take more than 0.07 x screen_height - and you hope it will stick on one line (which may not be the case if we provide a smaller font?). Anyway, none of these 2 aims sounds like "shrink to fit".

I guess a more proper way would be to have max_lines = N, and may be you guessing the height taken by N lines of the current font & size - and you would provide max_lines = 1.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the 7pc is a bit of a guesstimate, it comes from measuring the frame at one line with default font (53px on a 800px screen, that is roughly 6.625%, so 7 to give it some wiggle room). I guess it could be instead of boolean, a number (percentage) at which you cap the size of the frame... regardless of font size or other.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not self.height then
  if self.max_lines then
    max_height = self.max_lines * text_widget:getLineHeight()
  else
    max_height = screen height * 0.95
  end
  -- and provide directly that max_height to shrinkFontToFit

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and then, you wouldn't need to separate the code into its own function (as you don't really know what would happen if we call it from elsewhere than from :init() - that a separate function invites us to think we can).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took it a slightly different way, I hope you like it...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am all for making it simpler if possible, however, I don't like the max_lines implementation since it still needs me to figure out a way to guess then the size for values >2. I'd rather keep it a simple on/off boolean if it's all the same to you.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it still needs me to figure out a way to guess then the size for values >2

Isn't it just a matter of:

- max_height = math.max(text_widget:getLineHeight(), icon_height) + 2*frame.bordersize + 2*frame.padding + 3 
+ max_height = math.max(self.max_lines * text_widget:getLineHeight(), icon_height) + 2*frame.bordersize + 2*frame.padding + 3 

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but then we would have to account for all the spacing between the different lines, as I said, I'd rather do it boolean if it's all the same to you.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What "spacing between the different lines" ? getLineHeight() should return something that is used for all y and h computations, so there's no room/space in between.

if it's all the same to you

Guess it's not all the same to me :) If it's not more complicated to handle a number than a boolean, it will feel more generic and less adhoc.
Even if I don't really see why one would use it with wanting more than 1 line.

@Commodore64user Commodore64user Jan 9, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if I want n = 100? there are a lot more cases to handle there, and as you, yourself said, why would anyone want to use it with anything other than one? it should be all the same to you :)

Comment thread frontend/ui/widget/infomessage.lua Outdated
local max_height
if self.cap_height_to_one_line then
local icon_height = self.show_icon and image_widget:getSize().h or 0
-- calculate the size of the frame container when it's only displaying one line, and add a magic number (3) to give it some wiggle room

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does KOReader wiggle ?!!! It shouldn't ! If it does, fix that :)

And don't use magic numbers, KOReader should be deterministic, if you need magic, your reality is poor. Fix it !
I believe you shouldn't need any addition - if you need a +1, it's maybe because there's a < somewhere that could be a <= ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is not really need it, is just me giving it an extra 3 pixels just in case. I can remove it though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what I mean by wiggle room is that if we have a large enough font and the text is aaaa, that would be a smaller value than that of bbbb for example. So add those extra pixels in case something like was the case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, remove it. The "just in case" means you're not sure you don't need it. I'd rather you to be sure :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that if we have a large enough font and the text is aaaa, that would be a smaller value than that of bbbb for example

This is not the case, the height we get is constant, from the font metrics, a fixed thing - it's not the max of "blackbox/inkbox" of each letter that compose the line.

Comment thread frontend/ui/widget/infomessage.lua Outdated
_timeout_func = nil,
width = nil, -- The width of the InfoMessage. Keep it nil to use default value.
height = nil, -- The height of the InfoMessage. If this field is set, a scrollbar may be shown.
cap_height_to_one_line = false, -- Attempt to show text in one single line. This setting and height are not to be used conjointly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels this is still not a good name.
It explains the how we achieve one line (explanation which is not really needed) and moreover it's wrong (we don't cap the height - which could invoke crop & scroll -, we reduce the font size).
So, single_line or force_one_line.

Comment thread frontend/ui/widget/infomessage.lua Outdated
local max_height
if self.force_one_line then
local icon_height = self.show_icon and image_widget:getSize().h or 0
-- calculate the size of the frame container when it's only displaying one line.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(If a sentence ends with a dot, it should start with an uppercase.)

Comment thread frontend/ui/widget/infomessage.lua
Comment thread frontend/ui/widget/infomessage.lua Outdated
text = screensaver_message,
readonly = true,
dismissable = false,
force_one_line = true,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when there's a \n involved?

@Commodore64user Commodore64user Jan 11, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing, besides the fact that you get a literal \n as part of the message. What i mean is, \n is not recognised as new line neither pre PR nor post PR.

Edit: i don’t think that the founding fathers intended to have a sleep screen message with multiple lines ;)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make the question more generic :) What happens if we call InfoMessage with force_one_line=true and a normal multiline message ? Does it go at displaying it with the minimal font size ?
Just curious - as it would be a coder error.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I meant.

@Commodore64user Commodore64user Jan 11, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you cheeky ferrets, may I ask why would anyone so obtusely try and do just that? I mean is like wondering why your feet are wet after taking off your shoes during a storm.

but I understand the sentiment, I imagine it would in fact loop all the way to end of the font size (10 or whatever) and then show it like that. I'll see if I can test it though.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quite comical really

Comment thread frontend/ui/widget/infomessage.lua Outdated
Co-authored-by: David <97603719+Commodore64user@users.noreply.github.com>
@Frenzie Frenzie added this to the 2025.01 milestone Jan 12, 2025
@Frenzie Frenzie merged commit f8dec18 into koreader:master Jan 12, 2025
@Commodore64user Commodore64user deleted the one-line-msg branch January 12, 2025 11:41
@poire-z

poire-z commented Jan 12, 2025

Copy link
Copy Markdown
Contributor

I think what I'm getting at is amply illustrated by the fact that it happens right here in this very context. Before vs. after \n detection:

Why is there a \n here in this case ? And why wasn't it the case for @Commodore64user use case ? Just because of emulator having F2, or is it a different InfoMessage ?
If the caller knows there is a \n, it should just not provide force_one_line.

@Frenzie

Frenzie commented Jan 12, 2025

Copy link
Copy Markdown
Member

Just because of emulator having F2, or is it a different InfoMessage ?

Yes, no one ever envisioned it might be broken on purpose. My point is the same thing applies to every InfoMessage ever.

@poire-z

poire-z commented Jan 12, 2025

Copy link
Copy Markdown
Contributor

I'd say its a 100% @Commodore64user oversight.
It does not apply to every InfoMessage, just to those that get force_one_line=true - and it's in screensaver that we should have set it to false when we know we've added a \nPress F2...

@Frenzie

Frenzie commented Jan 12, 2025

Copy link
Copy Markdown
Member

Well, in any case it's now automatically ignored when it comes across \n. Seems good enough to me. :-)

@Commodore64user

Copy link
Copy Markdown
Member Author

@poire-z acting like the kid in the classroom that upon agreeing for everyone to take the blame for some mischief, immediately points and accuses the kid whose grandiose free kick broke the classroom window. How was I to know that the emulator had that? ;)

I can make the change if you want anyway force_one_line = not Device:isEmulator(),

@poire-z

poire-z commented Jan 12, 2025

Copy link
Copy Markdown
Contributor

How was I to know that the emulator had that? ;)

By looking around ? By thinking about the world and not only your little self / use-case ? :)

Anyway, let it be - I'm just mentionning all that so my point of view is remembered by the future humanity.

@Commodore64user

Copy link
Copy Markdown
Member Author

By thinking about the world and not only your little self / use-case ? :)

I only want to Make Kindle Great Again. ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FR: Sleep screen message "shrink to fit"

3 participants