Fix FileWidget sometimes showing two states

This commit is contained in:
fiaxh 2020-04-13 13:00:41 +02:00
parent 5b9199e7d4
commit bcbd5f0807

View file

@ -37,51 +37,45 @@ public class FileWidget : Box {
private StreamInteractor stream_interactor; private StreamInteractor stream_interactor;
private FileTransfer file_transfer; private FileTransfer file_transfer;
private State state; private State? state = null;
private FileDefaultWidgetController default_widget_controller; private FileDefaultWidgetController default_widget_controller;
private Widget content; private Widget? content = null;
public FileWidget(StreamInteractor stream_interactor, FileTransfer file_transfer) { public FileWidget(StreamInteractor stream_interactor, FileTransfer file_transfer) {
this.stream_interactor = stream_interactor; this.stream_interactor = stream_interactor;
this.file_transfer = file_transfer; this.file_transfer = file_transfer;
load_widget.begin(); update_widget.begin();
size_allocate.connect((allocation) => { size_allocate.connect((allocation) => {
if (allocation.height > parent.get_allocated_height()) { if (allocation.height > parent.get_allocated_height()) {
Idle.add(() => { parent.queue_resize(); return false; }); Idle.add(() => { parent.queue_resize(); return false; });
} }
}); });
file_transfer.notify["state"].connect(update_widget_type); file_transfer.notify["state"].connect(update_widget);
file_transfer.notify["mime-type"].connect(update_widget_type); file_transfer.notify["mime-type"].connect(update_widget);
} }
private async void load_widget() { private async void update_widget() {
if (show_image()) { if (show_image() && state != State.IMAGE) {
content = yield get_image_widget(file_transfer.get_file(), file_transfer.file_name); var content_bak = content;
if (content != null) { Widget? image_widget = yield get_image_widget(file_transfer.get_file(), file_transfer.file_name);
this.state = State.IMAGE;
this.add(content);
return;
}
}
FileDefaultWidget default_file_widget = new FileDefaultWidget() { visible=true };
default_widget_controller = new FileDefaultWidgetController(default_file_widget, file_transfer, stream_interactor);
content = default_file_widget;
this.state = State.DEFAULT;
this.add(content);
}
private async void update_widget_type() { // If the widget changed in the meanwhile, stop
if (file_transfer.state == FileTransfer.State.COMPLETE && show_image() && state != State.IMAGE) { if (content != content_bak) return;
this.remove(content);
this.add(yield get_image_widget(file_transfer.get_file(), file_transfer.file_name)); if (image_widget != null) {
if (content != null) this.remove(content);
content = image_widget;
state = State.IMAGE; state = State.IMAGE;
this.add(content);
return; return;
} }
if (file_transfer.state == FileTransfer.State.FAILED && state == State.IMAGE) { }
this.remove(content);
if (state != State.DEFAULT) {
if (content != null) this.remove(content);
FileDefaultWidget default_file_widget = new FileDefaultWidget() { visible=true }; FileDefaultWidget default_file_widget = new FileDefaultWidget() { visible=true };
default_widget_controller = new FileDefaultWidgetController(default_file_widget, file_transfer, stream_interactor); default_widget_controller = new FileDefaultWidgetController(default_file_widget, file_transfer, stream_interactor);
content = default_file_widget; content = default_file_widget;
@ -161,7 +155,11 @@ public class FileWidget : Box {
} }
private bool show_image() { private bool show_image() {
if (file_transfer.mime_type == null || file_transfer.state != FileTransfer.State.COMPLETE) return false; if (file_transfer.mime_type == null) return false;
if (file_transfer.state != FileTransfer.State.COMPLETE &&
!(file_transfer.direction == FileTransfer.DIRECTION_SENT && file_transfer.state == FileTransfer.State.IN_PROGRESS)) {
return false;
}
foreach (PixbufFormat pixbuf_format in Pixbuf.get_formats()) { foreach (PixbufFormat pixbuf_format in Pixbuf.get_formats()) {
foreach (string mime_type in pixbuf_format.get_mime_types()) { foreach (string mime_type in pixbuf_format.get_mime_types()) {