Skip to content

Commit

Permalink
use images instead of avatars
Browse files Browse the repository at this point in the history
  • Loading branch information
itmm committed Nov 20, 2021
1 parent 3defd57 commit e63b9a2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
31 changes: 22 additions & 9 deletions src/View/Contact.vala
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ namespace View {
private signal void has_icon (bool has);
private signal void make_bottom_section_unavailable (bool not_available);

private Granite.Widgets.Avatar icon =
new Granite.Widgets.Avatar.from_file (
Constants.DATADIR + "/avatars/64/contacts-avatar-default.svg", 64
);
private bool icon_is_set = false;
public Gdk.Pixbuf? default_image;
public Image icon_image { get; private set; default = new Image (); }

private EditableTitle name_label = new EditableTitle ("");

Expand Down Expand Up @@ -132,8 +129,11 @@ namespace View {
);

if (handler.icon != null) {
icon.pixbuf = handler.icon;
icon_image.pixbuf = handler.icon;
has_icon (true);
} else {
icon_image.pixbuf = default_image;
has_icon (false);
}

handler.contact_error.connect ((contact_error) => show_error (contact_error.message));
Expand All @@ -148,15 +148,23 @@ namespace View {
construct {
hscrollbar_policy = Gtk.PolicyType.NEVER;

var icon_theme = IconTheme.get_default ();
try {
default_image = icon_theme.load_icon ("avatar-default", 64, 0);
}
catch (Error err) {
error (err.message);
}
icon_image.pixbuf = default_image;

var icon_button = new Gtk.Button ();
icon_button.set_tooltip_text (_("Change the icon"));
icon_button.get_style_context ().add_class ("flat");
icon_button.add (icon);
icon_button.add (icon_image);

var popup = new IconPopup (icon_button);
has_icon.connect ((has) => {
popup.can_delete = has;
icon_is_set = has;
});

popup.change_image.connect (() => {
Expand All @@ -166,6 +174,11 @@ namespace View {
print ("You canceled the operation\n");
}
});
popup.remove_image.connect (() => {
handler.icon = null;
icon_image.pixbuf = default_image;
has_icon (false);
});

var title_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 12);
title_box.pack_start (icon_button, false, false, 0);
Expand Down Expand Up @@ -284,7 +297,7 @@ namespace View {
public void set_image_path (string path) {
try {
handler.icon = new Gdk.Pixbuf.from_file_at_scale (path, 64, 64, true);
icon.pixbuf = handler.icon;
icon_image.pixbuf = handler.icon;
has_icon (true);
} catch (Error e) {
error (e.message);
Expand Down
27 changes: 19 additions & 8 deletions src/View/Widgets/SidebarRow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ using ViewModel;
namespace View.Widgets {
private class SidebarRow : Gtk.ListBoxRow {

public Avatar display_widget { get; private set;
default = new Granite.Widgets.Avatar.from_file (
Constants.DATADIR + "/avatars/32/contacts-avatar-default.svg", 32
);
}
public Gdk.Pixbuf? default_image;
public Image icon_image { get; private set; default = new Image (); }

public string? header { get; set; }

Expand Down Expand Up @@ -98,7 +95,16 @@ namespace View.Widgets {

var overlay = new Gtk.Overlay ();
overlay.width_request = 38;
overlay.add (display_widget);
overlay.add (icon_image);

var icon_theme = IconTheme.get_default ();
try {
default_image = icon_theme.load_icon ("avatar-default", 32, 0);
}
catch (Error err) {
error (err.message);
}
icon_image.pixbuf = default_image;

var grid = new Gtk.Grid ();
grid.margin = 6;
Expand All @@ -118,12 +124,17 @@ namespace View.Widgets {
public void on_info_changed () {
this.title = handler.name;

if (handler.icon != null)
this.display_widget.pixbuf = handler.icon.scale_simple (32, 32, Gdk.InterpType.HYPER);
if (handler.icon != null) {
this.icon_image.pixbuf = handler.icon.scale_simple (32, 32, Gdk.InterpType.HYPER);
} else {
this.icon_image.pixbuf = default_image;
}

if (handler.phones != null && handler.phones.length () != 0) {
var phone = handler.phones.nth_data (0).data;
this.status = phone.locale_to_utf8 (-1, null, null);
} else {
this.status = "";
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions src/ViewModel/ContactHandler.vala
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,25 @@ namespace ViewModel {
}
}

public Pixbuf icon {
public Pixbuf? icon {
get {
return contact.icon;
}
set {
var image = value;

if (image.width < image.height) {
int y = image.height / 2 - image.width / 2;
image = new Gdk.Pixbuf.subpixbuf (image, 0, y, image.width, image.width);
} else if (image.width > image.height) {
int x = image.width / 2 - image.height / 2;
image = new Gdk.Pixbuf.subpixbuf (image, x, 0, image.height, image.height);
if (image != null) {
if (image.width < image.height) {
int y = image.height / 2 - image.width / 2;
image = new Gdk.Pixbuf.subpixbuf (image, 0, y, image.width, image.width);
} else if (image.width > image.height) {
int x = image.width / 2 - image.height / 2;
image = new Gdk.Pixbuf.subpixbuf (image, x, 0, image.height, image.height);
}
if (image.width != 64)
image = image.scale_simple (64, 64, Gdk.InterpType.HYPER);
}

if (image.width != 64)
image = image.scale_simple (64, 64, Gdk.InterpType.HYPER);

contact.icon = image;
changed ();
}
Expand Down

0 comments on commit e63b9a2

Please sign in to comment.