tootle-linux-client/src/Widgets/StatusWidget.vala

214 lines
7.3 KiB
Vala
Raw Normal View History

2018-04-14 14:09:06 +02:00
using Gtk;
using Granite;
2018-04-25 13:45:50 +02:00
public class Tootle.StatusWidget : Gtk.EventBox {
2018-04-14 14:09:06 +02:00
2018-04-16 20:22:42 +02:00
public Status status;
2018-04-22 13:35:25 +02:00
public int avatar_size;
2018-04-14 14:09:06 +02:00
public Granite.Widgets.Avatar avatar;
2018-04-20 13:21:51 +02:00
public Gtk.Label user;
2018-04-23 19:02:21 +02:00
public Gtk.Revealer revealer;
2018-04-20 13:21:51 +02:00
public Gtk.Label content;
2018-04-22 13:35:25 +02:00
public Gtk.Separator? separator;
2018-04-24 12:11:10 +02:00
public Gtk.Label? spoiler_content;
2018-04-25 13:45:50 +02:00
Gtk.Grid grid;
2018-04-14 14:09:06 +02:00
Gtk.Box counters;
Gtk.Label reblogs;
Gtk.Label favorites;
2018-04-16 20:22:42 +02:00
Gtk.ToggleButton reblog;
Gtk.ToggleButton favorite;
2018-04-24 16:50:38 +02:00
Gtk.ToggleButton reply;
2018-04-23 18:43:29 +02:00
Gtk.Button? spoiler_button;
2018-04-14 14:09:06 +02:00
construct {
2018-04-25 13:45:50 +02:00
grid = new Gtk.Grid ();
grid.margin = 6;
2018-04-14 19:18:42 +02:00
2018-04-22 13:35:25 +02:00
avatar_size = 32;
avatar = new Granite.Widgets.Avatar.with_default_icon (avatar_size);
2018-04-14 14:09:06 +02:00
avatar.valign = Gtk.Align.START;
avatar.margin_end = 6;
2018-04-21 11:27:00 +02:00
user = new Gtk.Label (_("Anonymous"));
2018-04-14 14:09:06 +02:00
user.hexpand = true;
user.halign = Gtk.Align.START;
user.use_markup = true;
2018-04-23 19:02:21 +02:00
2018-04-21 11:27:00 +02:00
content = new Gtk.Label (_("Error parsing text :c"));
2018-04-14 14:09:06 +02:00
content.halign = Gtk.Align.START;
content.use_markup = true;
content.single_line_mode = false;
content.set_line_wrap (true);
content.justify = Gtk.Justification.LEFT;
content.margin_end = 6;
content.xalign = 0;
2018-04-23 19:02:21 +02:00
revealer = new Revealer ();
revealer.reveal_child = true;
revealer.add (content);
2018-04-14 14:09:06 +02:00
reblogs = new Gtk.Label ("0");
favorites = new Gtk.Label ("0");
2018-04-16 20:22:42 +02:00
2018-04-24 16:50:38 +02:00
reblog = get_action_button ("go-up-symbolic");
2018-04-16 20:22:42 +02:00
reblog.toggled.connect (() => {
if (reblog.sensitive)
toggle_reblog ();
});
2018-04-24 16:50:38 +02:00
favorite = get_action_button ("help-about-symbolic");
2018-04-16 20:22:42 +02:00
favorite.toggled.connect (() => {
if (favorite.sensitive)
toggle_fav ();
});
2018-04-24 16:50:38 +02:00
reply = get_action_button ("edit-undo-symbolic");
reply.toggled.connect (() => {
reply.set_active (false);
PostDialog.open_reply (Tootle.window, this.status);
});
2018-04-16 20:22:42 +02:00
2018-04-14 19:18:42 +02:00
counters = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6); //TODO: currently useless
2018-04-14 14:09:06 +02:00
counters.margin_top = 6;
2018-04-24 12:11:10 +02:00
counters.add (reblog);
counters.add (reblogs);
counters.add (favorite);
counters.add (favorites);
2018-04-24 16:50:38 +02:00
counters.add (reply);
2018-04-14 14:09:06 +02:00
counters.show_all ();
2018-04-25 13:45:50 +02:00
grid.attach (avatar, 1, 1, 1, 4);
grid.attach (user, 2, 2, 1, 1);
grid.attach (revealer, 2, 4, 1, 1);
grid.attach (counters, 2, 5, 1, 1);
add (grid);
2018-04-24 12:11:10 +02:00
show_all (); //TODO: display conversations
2018-04-14 14:09:06 +02:00
}
2018-04-16 20:22:42 +02:00
public StatusWidget (Status status) {
this.status = status;
2018-04-14 14:09:06 +02:00
get_style_context ().add_class ("status");
2018-04-18 11:13:22 +02:00
2018-04-21 11:21:03 +02:00
if (status.reblog != null){
2018-04-24 16:50:38 +02:00
var image = new Gtk.Image.from_icon_name("go-up-symbolic", Gtk.IconSize.SMALL_TOOLBAR);
2018-04-21 11:21:03 +02:00
image.halign = Gtk.Align.END;
image.margin_end = 8;
image.show ();
2018-04-21 11:27:00 +02:00
var label_text = _("<a href=\"%s\"><b>%s</b></a> boosted").printf (status.account.url, status.account.display_name);
2018-04-21 11:21:03 +02:00
var label = new Gtk.Label (label_text);
label.halign = Gtk.Align.START;
label.use_markup = true;
label.margin_bottom = 8;
label.show ();
2018-04-25 13:45:50 +02:00
grid.attach (image, 1, 0, 1, 1);
grid.attach (label, 2, 0, 2, 1);
2018-04-21 11:21:03 +02:00
}
2018-04-23 18:43:29 +02:00
if (status.spoiler_text != null){
2018-04-23 19:02:21 +02:00
revealer.reveal_child = false;
2018-04-23 18:43:29 +02:00
spoiler_button = new Button.with_label (_("Show content"));
spoiler_content = new Label (status.spoiler_text);
2018-04-23 19:02:21 +02:00
var spoiler_box = new Box (Gtk.Orientation.HORIZONTAL, 6);
2018-04-23 18:43:29 +02:00
spoiler_box.add (spoiler_content);
spoiler_box.add (spoiler_button);
spoiler_box.show_all ();
2018-04-23 19:02:21 +02:00
spoiler_button.clicked.connect (() => revealer.set_reveal_child (!revealer.child_revealed));
2018-04-25 13:45:50 +02:00
grid.attach (spoiler_box, 2, 3, 1, 1);
2018-04-23 18:43:29 +02:00
}
2018-04-18 11:13:22 +02:00
destroy.connect (() => {
if(separator != null)
separator.destroy ();
});
2018-04-14 14:09:06 +02:00
}
2018-04-22 13:35:25 +02:00
public void highlight (){
content.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL);
2018-04-23 18:43:29 +02:00
if (spoiler_content != null)
spoiler_content.get_style_context ().add_class (Granite.STYLE_CLASS_H3_LABEL);
2018-04-22 13:35:25 +02:00
avatar_size = 48;
avatar.show_default (avatar_size);
}
2018-04-16 20:22:42 +02:00
public void rebind (Status status = this.status){
2018-04-21 11:21:03 +02:00
var user_label = status.reblog != null ? status.reblog.account.display_name : status.account.display_name;
user.label = "<b>%s</b>".printf (user_label);
2018-04-14 14:09:06 +02:00
content.label = status.content;
reblogs.label = status.reblogs_count.to_string ();
favorites.label = status.favourites_count.to_string ();
2018-04-16 20:22:42 +02:00
reblog.active = status.reblogged;
reblog.sensitive = true;
favorite.active = status.favorited;
favorite.sensitive = true;
2018-04-21 11:21:03 +02:00
var avatar_url = status.reblog != null ? status.reblog.account.avatar : status.account.avatar;
2018-04-22 13:35:25 +02:00
CacheManager.instance.load_avatar (avatar_url, this.avatar, this.avatar_size);
2018-04-25 15:16:57 +02:00
avatar.button_press_event.connect(on_avatar_clicked);
}
private bool on_avatar_clicked (){
var account = status.reblog != null ? status.reblog.account : status.account;
var view = new AccountView (account);
Tootle.window.open_secondary_view (view);
return true;
2018-04-14 14:09:06 +02:00
}
2018-04-16 20:22:42 +02:00
2018-04-24 16:50:38 +02:00
private Gtk.ToggleButton get_action_button (string icon_path){
var icon = new Gtk.Image.from_icon_name (icon_path, Gtk.IconSize.SMALL_TOOLBAR);
2018-04-16 20:22:42 +02:00
var button = new Gtk.ToggleButton ();
2018-04-19 20:17:20 +02:00
button.can_default = false;
button.get_style_context ().add_class (Gtk.STYLE_CLASS_FLAT);
2018-04-16 20:22:42 +02:00
button.add (icon);
return button;
}
2018-04-24 12:11:10 +02:00
public void toggle_reblog (){
2018-04-16 20:22:42 +02:00
var state = reblog.get_active ();
var action = "reblog";
if (!state)
action = "unreblog";
var msg = new Soup.Message("POST", Settings.instance.instance_url + "/api/v1/statuses/" + status.id.to_string () + "/" + action);
2018-04-24 12:11:10 +02:00
msg.finished.connect (() => {
2018-04-16 20:22:42 +02:00
status.reblogged = state;
reblog.sensitive = false;
favorite.sensitive = false;
2018-04-24 12:11:10 +02:00
if (state)
2018-04-16 20:22:42 +02:00
status.reblogs_count += 1;
else
status.reblogs_count -= 1;
rebind ();
});
2018-04-24 12:11:10 +02:00
NetManager.instance.queue (msg, (sess, mess) => {
2018-04-16 20:22:42 +02:00
//NetManager.parse (msg);
});
}
2018-04-24 12:11:10 +02:00
public void toggle_fav (){
2018-04-16 20:22:42 +02:00
var state = favorite.get_active ();
var action = "favourite";
if (!state)
action = "unfavourite";
2018-04-24 12:11:10 +02:00
var msg = new Soup.Message ("POST", Settings.instance.instance_url + "/api/v1/statuses/" + status.id.to_string () + "/" + action);
msg.finished.connect (() => {
2018-04-16 20:22:42 +02:00
status.favorited = state;
reblog.sensitive = false;
favorite.sensitive = false;
2018-04-24 12:11:10 +02:00
if (state)
2018-04-16 20:22:42 +02:00
status.favourites_count += 1;
else
status.favourites_count -= 1;
rebind ();
});
2018-04-24 12:11:10 +02:00
NetManager.instance.queue (msg, (sess, mess) => {
2018-04-16 20:22:42 +02:00
//NetManager.parse (msg);
});
}
2018-04-14 14:09:06 +02:00
}