0

I am working on an extension for gnome, in which there is a popupmenu with menuitems.One of the menuitems is called "Log out" .I have managed to display next to "Log out" the real name of the user with this code:

    let username = GLib.get_real_name();
                .........
                .........
    item = new PopupMenu.PopupMenuItem(_(list[x].text) + username);

Log out menuitem

Now I want to display and the user profile image next to real name.I tried this code but it does not work:

    let usename= GLib.get_user_name();
    let user = AccountsService.UserManager.get_default().get_user(username);
    let iconpath = user.get_icon_file();
    let icon = Gio.icon_new_for_string(iconpath);
    Icon = new St.Icon(icon);
    boxicon = new St.BoxLayout();
    boxicon.add(Icon);

It seems that the "iconpath" is null.How can I get the user profile image and display it in the menu. Thanks in advance.

cgiannakidis
  • 81
  • 1
  • 6

1 Answers1

0

There is a lot of JavaScript in gnome-shell that has already been written to be easily reused. Bonus, you will automatically get bugfixes and performance improvements that are made to the code (if they happen).

I would recommend you re-use, or at least look the the Avatar class in ui/userWidget.js. Not only is the code already written, but it keeps the avatar up to date, and any GNOME Shell theme that has custom styles will probably also work automatically.

You can use this class like so:

const UserWidget = imports.ui.userWidget;

let user = AccountsService.UserManager.get_default().get_user(username);
let avatar = new UserWidget.Avatar(user);
let boxicon = new St.BoxLayout();

// Notice that UserWidget.Avatar is a container class for the actual actor
boxicon.add_child(avatar.actor);

There are also other classes in there for the user name label, and a parent class that wraps them both. Be aware of classes or functions prefixed with an underscore like _doStuff(), since that's a common way of marking things as "private" or internal, and subject to change without notice.

EDIT

Also, if you're not using or targetting the latest release, use the dropdown menu in GitLab to select the branch for your release, or view the history of a file to see if anything important changed.

andy.holmes
  • 3,383
  • 17
  • 28
  • Thanks a lot for your answer.This is what I was looking for.Something else, if I want to add the 'avatar' to a menuitem what code should I use.Sorry for my ignorance but I am a newbie in javascript. – cgiannakidis Nov 14 '18 at 08:27
  • That's okay; there's old stuff kept in there for old extensions. Most of these classes (like `PopupMenuItem`) are "empty" container classes with a `Foo.actor` which is the real widget you want to add to (`menuItem.actor.add_child(avatar.actor)`). `PopupMenuItem` expects `PopupMenuItem.box` to be there for signals (open/close/etc) and in most cases `PopupMenuItem.actor === PopupMenuItem.box`. Have a look at [`PopupMenuSection`](https://gitlab.gnome.org/GNOME/gnome-shell/blob/master/js/ui/popupMenu.js#L1078) to see how they use this trick the make sections behave like items. – andy.holmes Nov 14 '18 at 09:12
  • Also checkout the GJS wiki if you're new: https://gitlab.gnome.org/GNOME/gjs/wikis/home and the official docs: http://devdocs.baznga.org/ – andy.holmes Nov 14 '18 at 09:18
  • Hi again and sorry if I am boring but what I get is an empty avatar regardless I have already choose one in user account settings. – cgiannakidis Nov 14 '18 at 10:56
  • No problem. If you have an IRC/Matrix client, come chat with us on irc.gimp.org #javascript (and be patient ;)) – andy.holmes Nov 14 '18 at 13:40
  • unfortunately I dont have an IRC client. But what can be wrong with the avatar and is empty? I followed your instructions and I study Avatar class in userWidget.js and there are no typos in my extension which is running well.Any suggestion? – cgiannakidis Nov 14 '18 at 19:25