2013-02-17 20:54:46 -05:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
//
|
|
|
|
// A widget showing the user avatar and name
|
2019-01-31 09:07:06 -05:00
|
|
|
/* exported UserWidget */
|
2013-08-26 18:36:16 -04:00
|
|
|
|
2019-01-31 09:04:56 -05:00
|
|
|
const { Clutter, GLib, GObject, St } = imports.gi;
|
2013-02-17 20:54:46 -05:00
|
|
|
|
2013-04-23 22:31:08 -04:00
|
|
|
const Params = imports.misc.params;
|
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var AVATAR_ICON_SIZE = 64;
|
2013-04-23 22:31:08 -04:00
|
|
|
|
|
|
|
// Adapted from gdm/gui/user-switch-applet/applet.c
|
|
|
|
//
|
|
|
|
// Copyright (C) 2004-2005 James M. Cape <jcape@ignore-your.tv>.
|
|
|
|
// Copyright (C) 2008,2009 Red Hat, Inc.
|
|
|
|
|
2019-10-28 14:35:33 -04:00
|
|
|
var Avatar = GObject.registerClass(
|
|
|
|
class Avatar extends St.Bin {
|
2019-07-16 05:24:13 -04:00
|
|
|
_init(user, params) {
|
|
|
|
let themeContext = St.ThemeContext.get_for_stage(global.stage);
|
2013-04-23 22:31:08 -04:00
|
|
|
params = Params.parse(params, { reactive: false,
|
|
|
|
iconSize: AVATAR_ICON_SIZE,
|
2019-02-05 11:28:41 -05:00
|
|
|
styleClass: 'user-icon' });
|
2013-04-23 22:31:08 -04:00
|
|
|
|
2019-07-16 05:24:13 -04:00
|
|
|
super._init({
|
|
|
|
style_class: params.styleClass,
|
|
|
|
reactive: params.reactive,
|
|
|
|
width: params.iconSize * themeContext.scaleFactor,
|
|
|
|
height: params.iconSize * themeContext.scaleFactor
|
|
|
|
});
|
|
|
|
|
|
|
|
this._iconSize = params.iconSize;
|
|
|
|
this._user = user;
|
2017-08-10 11:21:50 -04:00
|
|
|
|
2019-08-30 19:32:03 -04:00
|
|
|
this.bind_property('reactive', this, 'track-hover',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE);
|
|
|
|
this.bind_property('reactive', this, 'can-focus',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE);
|
|
|
|
|
2017-08-10 11:21:50 -04:00
|
|
|
// Monitor the scaling factor to make sure we recreate the avatar when needed.
|
2019-07-16 05:24:13 -04:00
|
|
|
this._scaleFactorChangeId =
|
|
|
|
themeContext.connect('notify::scale-factor', this.update.bind(this));
|
|
|
|
|
|
|
|
this.connect('destroy', this._onDestroy.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
_onDestroy() {
|
|
|
|
if (this._scaleFactorChangeId) {
|
|
|
|
let themeContext = St.ThemeContext.get_for_stage(global.stage);
|
|
|
|
themeContext.disconnect(this._scaleFactorChangeId);
|
|
|
|
delete this._scaleFactorChangeId;
|
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-04-23 22:31:08 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
setSensitive(sensitive) {
|
2019-07-16 05:24:13 -04:00
|
|
|
this.reactive = sensitive;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-04-23 22:31:08 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
update() {
|
2013-04-23 22:31:08 -04:00
|
|
|
let iconFile = this._user.get_icon_file();
|
|
|
|
if (iconFile && !GLib.file_test(iconFile, GLib.FileTest.EXISTS))
|
|
|
|
iconFile = null;
|
|
|
|
|
|
|
|
if (iconFile) {
|
2019-07-16 05:24:13 -04:00
|
|
|
this.child = null;
|
2019-07-10 11:18:16 -04:00
|
|
|
let { scaleFactor } = St.ThemeContext.get_for_stage(global.stage);
|
2019-07-16 05:24:13 -04:00
|
|
|
this.set_size(
|
2019-07-10 11:18:16 -04:00
|
|
|
this._iconSize * scaleFactor,
|
|
|
|
this._iconSize * scaleFactor);
|
2019-07-16 05:24:13 -04:00
|
|
|
this.style = `
|
2019-03-07 07:39:17 -05:00
|
|
|
background-image: url("${iconFile}");
|
2019-07-10 11:18:16 -04:00
|
|
|
background-size: cover;`;
|
2013-04-23 22:31:08 -04:00
|
|
|
} else {
|
2019-07-16 05:24:13 -04:00
|
|
|
this.style = null;
|
|
|
|
this.child = new St.Icon({ icon_name: 'avatar-default-symbolic',
|
|
|
|
icon_size: this._iconSize });
|
2013-04-23 22:31:08 -04:00
|
|
|
}
|
|
|
|
}
|
2019-07-16 05:24:13 -04:00
|
|
|
});
|
2013-02-17 20:54:46 -05:00
|
|
|
|
2017-10-30 21:23:39 -04:00
|
|
|
var UserWidgetLabel = GObject.registerClass(
|
|
|
|
class UserWidgetLabel extends St.Widget {
|
2017-10-30 20:03:21 -04:00
|
|
|
_init(user) {
|
2017-10-30 21:23:39 -04:00
|
|
|
super._init({ layout_manager: new Clutter.BinLayout() });
|
2013-08-26 18:36:16 -04:00
|
|
|
|
|
|
|
this._user = user;
|
|
|
|
|
|
|
|
this._realNameLabel = new St.Label({ style_class: 'user-widget-label',
|
|
|
|
y_align: Clutter.ActorAlign.CENTER });
|
|
|
|
this.add_child(this._realNameLabel);
|
|
|
|
|
|
|
|
this._userNameLabel = new St.Label({ style_class: 'user-widget-label',
|
|
|
|
y_align: Clutter.ActorAlign.CENTER });
|
|
|
|
this.add_child(this._userNameLabel);
|
|
|
|
|
|
|
|
this._currentLabel = null;
|
|
|
|
|
2017-12-01 19:27:35 -05:00
|
|
|
this._userLoadedId = this._user.connect('notify::is-loaded', this._updateUser.bind(this));
|
|
|
|
this._userChangedId = this._user.connect('changed', this._updateUser.bind(this));
|
2013-08-26 18:36:16 -04:00
|
|
|
this._updateUser();
|
2013-12-02 17:50:45 -05:00
|
|
|
|
|
|
|
// We can't override the destroy vfunc because that might be called during
|
|
|
|
// object finalization, and we can't call any JS inside a GC finalize callback,
|
|
|
|
// so we use a signal, that will be disconnected by GObject the first time
|
|
|
|
// the actor is destroyed (which is guaranteed to be as part of a normal
|
|
|
|
// destroy() call from JS, possibly from some ancestor)
|
2017-12-01 19:27:35 -05:00
|
|
|
this.connect('destroy', this._onDestroy.bind(this));
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2013-08-26 18:36:16 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onDestroy() {
|
2013-08-26 18:36:16 -04:00
|
|
|
if (this._userLoadedId != 0) {
|
|
|
|
this._user.disconnect(this._userLoadedId);
|
|
|
|
this._userLoadedId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._userChangedId != 0) {
|
|
|
|
this._user.disconnect(this._userChangedId);
|
|
|
|
this._userChangedId = 0;
|
|
|
|
}
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2013-08-26 18:36:16 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
vfunc_allocate(box, flags) {
|
2013-08-26 18:36:16 -04:00
|
|
|
this.set_allocation(box, flags);
|
|
|
|
|
|
|
|
let availWidth = box.x2 - box.x1;
|
|
|
|
let availHeight = box.y2 - box.y1;
|
|
|
|
|
2019-02-01 08:41:55 -05:00
|
|
|
let [, , natRealNameWidth] = this._realNameLabel.get_preferred_size();
|
2013-08-26 18:36:16 -04:00
|
|
|
|
|
|
|
if (natRealNameWidth <= availWidth)
|
|
|
|
this._currentLabel = this._realNameLabel;
|
|
|
|
else
|
|
|
|
this._currentLabel = this._userNameLabel;
|
2015-03-06 10:56:11 -05:00
|
|
|
this.label_actor = this._currentLabel;
|
2013-08-26 18:36:16 -04:00
|
|
|
|
|
|
|
let childBox = new Clutter.ActorBox();
|
|
|
|
childBox.x1 = 0;
|
|
|
|
childBox.y1 = 0;
|
|
|
|
childBox.x2 = availWidth;
|
|
|
|
childBox.y2 = availHeight;
|
|
|
|
|
|
|
|
this._currentLabel.allocate(childBox, flags);
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2013-08-26 18:36:16 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
vfunc_paint() {
|
2013-08-26 18:36:16 -04:00
|
|
|
this._currentLabel.paint();
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2013-08-26 18:36:16 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_updateUser() {
|
2013-08-26 18:36:16 -04:00
|
|
|
if (this._user.is_loaded) {
|
|
|
|
this._realNameLabel.text = this._user.get_real_name();
|
|
|
|
this._userNameLabel.text = this._user.get_user_name();
|
|
|
|
} else {
|
|
|
|
this._realNameLabel.text = '';
|
|
|
|
this._userNameLabel.text = '';
|
|
|
|
}
|
2017-10-30 21:23:39 -04:00
|
|
|
}
|
2013-08-26 18:36:16 -04:00
|
|
|
});
|
|
|
|
|
2019-07-16 05:24:13 -04:00
|
|
|
var UserWidget = GObject.registerClass(
|
|
|
|
class UserWidget extends St.BoxLayout {
|
|
|
|
_init(user) {
|
|
|
|
super._init({ style_class: 'user-widget', vertical: false });
|
|
|
|
|
2013-02-17 20:54:46 -05:00
|
|
|
this._user = user;
|
|
|
|
|
2019-07-16 05:24:13 -04:00
|
|
|
this.connect('destroy', this._onDestroy.bind(this));
|
2013-02-17 20:54:46 -05:00
|
|
|
|
2013-04-23 22:31:08 -04:00
|
|
|
this._avatar = new Avatar(user);
|
2019-07-16 05:24:13 -04:00
|
|
|
this.add_child(this._avatar);
|
2013-08-26 18:36:16 -04:00
|
|
|
|
|
|
|
this._label = new UserWidgetLabel(user);
|
2019-07-16 05:24:13 -04:00
|
|
|
this.add_child(this._label);
|
2013-08-26 18:36:16 -04:00
|
|
|
|
2019-07-16 05:24:13 -04:00
|
|
|
this._label.bind_property('label-actor', this, 'label-actor',
|
2015-03-06 10:56:11 -05:00
|
|
|
GObject.BindingFlags.SYNC_CREATE);
|
|
|
|
|
2017-12-01 19:27:35 -05:00
|
|
|
this._userLoadedId = this._user.connect('notify::is-loaded', this._updateUser.bind(this));
|
|
|
|
this._userChangedId = this._user.connect('changed', this._updateUser.bind(this));
|
2013-08-26 18:36:16 -04:00
|
|
|
this._updateUser();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-02-17 20:54:46 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onDestroy() {
|
2013-02-17 20:54:46 -05:00
|
|
|
if (this._userLoadedId != 0) {
|
|
|
|
this._user.disconnect(this._userLoadedId);
|
|
|
|
this._userLoadedId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._userChangedId != 0) {
|
|
|
|
this._user.disconnect(this._userChangedId);
|
|
|
|
this._userChangedId = 0;
|
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-02-17 20:54:46 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_updateUser() {
|
2013-02-17 20:54:46 -05:00
|
|
|
this._avatar.update();
|
|
|
|
}
|
2019-07-16 05:24:13 -04:00
|
|
|
});
|