Overview: Resize the window title labels on content change

Reposition the window overlay when the title changes, using the current
transformed size of the window clone.
Includes a test that changes title to a string of random length every 3 seconds.

Based on a patch by Alex Hultman <alexhultman@gmail.com>

https://bugzilla.gnome.org/show_bug.cgi?id=620874
This commit is contained in:
Giovanni Campagna 2012-10-19 18:37:29 +02:00
parent eb09f34114
commit 687e1eabed
2 changed files with 64 additions and 3 deletions

View File

@ -159,6 +159,24 @@ const WindowClone = new Lang.Class({
this._selected = false; this._selected = false;
}, },
get slot() {
let x, y, w, h;
if (this.inDrag) {
x = this.dragOrigX;
y = this.dragOrigY;
w = this.actor.width * this.dragOrigScale;
h = this.actor.height * this.dragOrigScale;
} else {
x = this.actor.x;
y = this.actor.y;
w = this.actor.width * this.actor.scale_x;
h = this.actor.height * this.actor.scale_y;
}
return [x, y, w, h];
},
setStackAbove: function (actor) { setStackAbove: function (actor) {
this._stackAbove = actor; this._stackAbove = actor;
if (this.inDrag || this._zooming) if (this.inDrag || this._zooming)
@ -435,6 +453,11 @@ const WindowOverlay = new Lang.Class({
this._updateCaptionId = metaWindow.connect('notify::title', this._updateCaptionId = metaWindow.connect('notify::title',
Lang.bind(this, function(w) { Lang.bind(this, function(w) {
this.title.text = w.title; this.title.text = w.title;
// we need this for the next call to get_preferred_width
// to return useful results
this.title.set_size(-1, -1);
this._repositionSelf();
})); }));
let button = new St.Button({ style_class: 'window-close' }); let button = new St.Button({ style_class: 'window-close' });
@ -501,6 +524,11 @@ const WindowOverlay = new Lang.Class({
this.title.height + this.title._spacing]; this.title.height + this.title._spacing];
}, },
_repositionSelf: function() {
let [cloneX, cloneY, cloneWidth, cloneHeight] = this._windowClone.slot;
this.updatePositions(cloneX, cloneY, cloneWidth, cloneHeight, false);
},
/** /**
* @cloneX: x position of windowClone * @cloneX: x position of windowClone
* @cloneY: y position of windowClone * @cloneY: y position of windowClone
@ -538,9 +566,8 @@ const WindowOverlay = new Lang.Class({
else else
button.set_position(Math.floor(buttonX), Math.floor(buttonY)); button.set_position(Math.floor(buttonX), Math.floor(buttonY));
if (!title.fullWidth) let [titleMinWidth, titleNatWidth] = title.get_preferred_width(-1);
title.fullWidth = title.width; let titleWidth = Math.max(titleMinWidth, Math.min(titleNatWidth, cloneWidth));
let titleWidth = Math.min(title.fullWidth, cloneWidth);
let titleX = cloneX + (cloneWidth - titleWidth) / 2; let titleX = cloneX + (cloneWidth - titleWidth) / 2;
let titleY = cloneY + cloneHeight + title._spacing; let titleY = cloneY + cloneHeight + title._spacing;

34
tests/interactive/test-title.js Executable file
View File

@ -0,0 +1,34 @@
#!/usr/bin/env gjs
const Gtk = imports.gi.Gtk;
const Mainloop = imports.mainloop;
function nextTitle() {
let length = Math.random() * 20;
let str = '';
for (let i = 0; i < length; i++) {
// 97 == 'a'
str += String.fromCharCode(97 + Math.random() * 26);
}
return str;
}
function main() {
Gtk.init(null);
let win = new Gtk.Window({ title: nextTitle() });
win.connect('destroy', Gtk.main_quit);
win.present();
Mainloop.timeout_add(5000, function() {
win.title = nextTitle();
return true;
});
Gtk.main();
}
main();