Search code examples
pangognome-shell-extensionsgjs

Determining the number of lines in a notification, depending on width and height


So I am yet again facing another problem concerning my extension. I would like to calculate the number of lines in a notification, based on its width and height.

For context, I am designing an extension that adds a button to the notification in the notification list. That is supposed to FULLY expand the notification. The notification has a layout manager, that is the same as the notification from the message tray. Since I want to fully expand the messages, I need a way to calculate the amount of lines. Here is what I have tried and what has worked the best:

let nLines = (this._expandedLabel.clutter_text.get_width()) / (this.bodyLabel.clutter_text.get_width());
this._bodyStack.layout_manager._expandLines = nLines;
this._bodyStack.layout_manager.expansion = 1;
this.get_parent().set_height(this.get_height() + fix);
this.get_parent().layout_manager.layout_changed();

Now, this almost works, there is a small offset with the number of lines. Rounding the nLines is not enough to get the correct number of lines.

I have tried another solution, which involves creating a Pango.Layout Object, then trying to calculate the number of lines:

let pangoLayout = this._expandedLabel.clutter_text.create_pango_layout(this._bodyText);
pangoLayout.set_height(this._bodyStack.get_height());
pangoLayout.set_width(this._bodyStack.get_width());
log("Line Count: " + pangoLayout.get_line_count());

In this case, the line count represents the number of words instead of the number of lines. I could not find a function in Pango.Layout that can help me create lines from the words. Does anyone have an idea or can help me find a different approach?

I should probably also mention that I tried line wrapping, but that just increases the line count ridiculously high.

EDIT: Forgot to mentino this is all happening in the expand function, defined by the class Message.


Solution

  • Okay I have it. I missed the fact that Pango uses Pango Units, not Pixels. So in order to get the Pango unit size, the pixels have to be multiplied with the Pango.SCALE constant.