Skip to content

Commit 1b09250

Browse files
committed
calendar@cinnamon.org: Apply 'grow-only' behavior to the panel
label. - Switch the applet from Applet.TextApplet to Applet.Applet so we can manage the label's parent. - Set a minimum width on the parent based on the label's natural width, and only allow it to increase if the label requires it. - Reset this minimum size only if the label shrinks substantially (like if the day/month changes), or some other event demands it, such as a theme or settings change. This should reduce any minor size fluctuations in the panel label that may cause adjacent applets to shift repeatedly when the date/ time label changes. ref: #13336, #13459.
1 parent ee98c76 commit 1b09250

1 file changed

Lines changed: 77 additions & 3 deletions

File tree

  • files/usr/share/cinnamon/applets/calendar@cinnamon.org

files/usr/share/cinnamon/applets/calendar@cinnamon.org/applet.js

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Gio = imports.gi.Gio;
33
const GLib = imports.gi.GLib;
44
const Lang = imports.lang;
55
const Clutter = imports.gi.Clutter;
6+
const Pango = imports.gi.Pango;
67
const St = imports.gi.St;
78
const Util = imports.misc.util;
89
const PopupMenu = imports.ui.popupMenu;
@@ -20,7 +21,7 @@ const DAY_FORMAT = CinnamonDesktop.WallClock.lctime_format("cinnamon", "%A");
2021
const DATE_FORMAT_SHORT = CinnamonDesktop.WallClock.lctime_format("cinnamon", _("%B %-e, %Y"));
2122
const DATE_FORMAT_FULL = CinnamonDesktop.WallClock.lctime_format("cinnamon", _("%A, %B %-e, %Y"));
2223

23-
class CinnamonCalendarApplet extends Applet.TextApplet {
24+
class CinnamonCalendarApplet extends Applet.Applet {
2425
constructor(orientation, panel_height, instance_id) {
2526
super(orientation, panel_height, instance_id);
2627

@@ -30,6 +31,19 @@ class CinnamonCalendarApplet extends Applet.TextApplet {
3031
this.menuManager = new PopupMenu.PopupMenuManager(this);
3132
this.orientation = orientation;
3233

34+
this._clockLabel = new St.Label({
35+
reactive: true,
36+
track_hover: true,
37+
style_class: 'applet-label'
38+
});
39+
this._clockLabel.clutter_text.ellipsize = Pango.EllipsizeMode.NONE;
40+
41+
this._labelBin = new St.Bin();
42+
this._labelBin.set_child(this._clockLabel);
43+
44+
this.actor.add(this._labelBin, { y_align: St.Align.MIDDLE, y_fill: false });
45+
this.actor.set_label_actor(this._clockLabel);
46+
3347
this._initContextMenu();
3448
this.menu.setCustomStyleClass('calendar-background');
3549

@@ -38,6 +52,8 @@ class CinnamonCalendarApplet extends Applet.TextApplet {
3852

3953
this.clock = new CinnamonDesktop.WallClock();
4054
this.clock_notify_id = 0;
55+
this.theme_set_id = 0;
56+
this.panel_edit_mode_id = 0;
4157

4258
// Events
4359
this.events_manager = new EventView.EventsManager(this.settings, this.desktop_settings);
@@ -190,6 +206,8 @@ class CinnamonCalendarApplet extends Applet.TextApplet {
190206
}
191207

192208
_onSettingsChanged() {
209+
this._labelBin.min_width = 0;
210+
193211
this._updateFormatString();
194212
this._updateClockAndDate();
195213
this.event_list.actor.visible = this.events_manager.is_active();
@@ -245,6 +263,44 @@ class CinnamonCalendarApplet extends Applet.TextApplet {
245263
}
246264
}
247265

266+
_onThemeSet() {
267+
this._resetLabelWidth();
268+
}
269+
270+
_onPanelEditModeChanged() {
271+
this._resetLabelWidth();
272+
}
273+
274+
on_panel_height_changed() {
275+
this._resetLabelWidth();
276+
}
277+
278+
_resetLabelWidth() {
279+
this._labelBin.min_width = 0;
280+
this._updateLabelWidth();
281+
}
282+
283+
/* Grow to any wider string, but only snap back down when the width drops by
284+
* more than ~2 average character widths - ignores jitter from digit changes
285+
* while still reclaiming space when a whole word shrinks ("Saturday" -> "Sunday").
286+
*
287+
* We measure the label but force the min-width on the parent (_labelBin),
288+
* not the label itself, otherwise the label's natural width could never
289+
* shrink again. */
290+
_updateLabelWidth() {
291+
let [, natWidth] = this._clockLabel.get_preferred_width(-1);
292+
if (natWidth <= 0) {
293+
return;
294+
}
295+
296+
let avgChar = natWidth / Math.max(1, this._clockLabel.get_text().length);
297+
298+
if (natWidth > this._labelBin.min_width ||
299+
natWidth < this._labelBin.min_width - 2 * avgChar) {
300+
this._labelBin.min_width = natWidth;
301+
}
302+
}
303+
248304
_events_manager_ready(em) {
249305
this.event_list.actor.visible = this.events_manager.is_active();
250306
this.events_manager.select_date(this._calendar.getSelectedDate(), true);
@@ -264,8 +320,10 @@ class CinnamonCalendarApplet extends Applet.TextApplet {
264320
label_string = this.clock.get_clock_for_format(this.custom_format);
265321
}
266322

267-
if (label_string)
268-
this.set_applet_label(label_string);
323+
if (label_string) {
324+
this._clockLabel.set_text(label_string);
325+
this._updateLabelWidth();
326+
}
269327

270328
this.go_home_button.reactive = !this._calendar.todaySelected();
271329
if (this._calendar.todaySelected()) {
@@ -303,6 +361,14 @@ class CinnamonCalendarApplet extends Applet.TextApplet {
303361
this.clock_notify_id = this.clock.connect("notify::clock", () => this._clockNotify());
304362
}
305363

364+
if (this.theme_set_id == 0) {
365+
this.theme_set_id = Main.themeManager.connect("theme-set", () => this._onThemeSet());
366+
}
367+
368+
if (this.panel_edit_mode_id == 0) {
369+
this.panel_edit_mode_id = global.settings.connect("changed::panel-edit-mode", () => this._onPanelEditModeChanged());
370+
}
371+
306372
/* Populates the calendar so our menu allocation is correct for animation */
307373
this.events_manager.start_events();
308374
this._resetCalendar();
@@ -314,6 +380,14 @@ class CinnamonCalendarApplet extends Applet.TextApplet {
314380
this.clock.disconnect(this.clock_notify_id);
315381
this.clock_notify_id = 0;
316382
}
383+
if (this.theme_set_id > 0) {
384+
Main.themeManager.disconnect(this.theme_set_id);
385+
this.theme_set_id = 0;
386+
}
387+
if (this.panel_edit_mode_id > 0) {
388+
global.settings.disconnect(this.panel_edit_mode_id);
389+
this.panel_edit_mode_id = 0;
390+
}
317391
}
318392

319393
_initContextMenu() {

0 commit comments

Comments
 (0)