diff options
author | fiaxh <git@lightrise.org> | 2020-04-10 21:08:23 +0200 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2020-04-10 21:08:23 +0200 |
commit | ff4403fb21ea5a3575a7a3f7cafcf83a7fb46bf9 (patch) | |
tree | cabe6b83aba5caf67dab538595201f879f6db9a7 | |
parent | bd8063284c7d3f5f6a2646e91b4f14e87e2765a8 (diff) | |
download | dino-ff4403fb21ea5a3575a7a3f7cafcf83a7fb46bf9.tar.gz dino-ff4403fb21ea5a3575a7a3f7cafcf83a7fb46bf9.zip |
Clean up file widget code
-rw-r--r-- | main/CMakeLists.txt | 2 | ||||
-rw-r--r-- | main/data/file_default_widget.ui | 97 | ||||
-rw-r--r-- | main/src/ui/conversation_content_view/file_default_widget.vala | 127 | ||||
-rw-r--r-- | main/src/ui/conversation_content_view/file_widget.vala | 268 |
4 files changed, 293 insertions, 201 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index e2f8ecf8..278daf76 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -50,6 +50,7 @@ set(RESOURCE_LIST conversation_list_titlebar_csd.ui conversation_row.ui conversation_view.ui + file_default_widget.ui emojichooser.ui global_search.ui conversation_content_view/image_toolbar.ui @@ -140,6 +141,7 @@ SOURCES src/ui/conversation_content_view/conversation_item_skeleton.vala src/ui/conversation_content_view/conversation_view.vala src/ui/conversation_content_view/date_separator_populator.vala + src/ui/conversation_content_view/file_default_widget.vala src/ui/conversation_content_view/file_widget.vala src/ui/conversation_content_view/message_widget.vala src/ui/conversation_content_view/subscription_notification.vala diff --git a/main/data/file_default_widget.ui b/main/data/file_default_widget.ui new file mode 100644 index 00000000..7019f0c2 --- /dev/null +++ b/main/data/file_default_widget.ui @@ -0,0 +1,97 @@ +<?xml version="1.0" encoding="UTF-8"?> +<interface> + <template class="DinoUiFileDefaultWidget" parent="GtkEventBox"> + <property name="halign">start</property> + <property name="visible">True</property> + <style> + <class name="file-box-outer"/> + </style> + <child> + <object class="DinoUiSizingBin"> + <property name="target-width">500</property> + <property name="max-width">500</property> + <property name="hexpand">True</property> + <property name="visible">True</property> + <child> + <object class="GtkBox"> + <property name="orientation">horizontal</property> + <property name="halign">fill</property> + <property name="hexpand">true</property> + <property name="spacing">10</property> + <property name="visible">True</property> + <style> + <class name="file-box"/> + </style> + <child> + <object class="GtkEventBox" id="stack_event_box"> + <property name="visible">True</property> + <child> + <object class="GtkStack" id="image_stack"> + <property name="transition-type">crossfade</property> + <property name="transition-duration">50</property> + <property name="valign">center</property> + <property name="visible">True</property> + <child> + <object class="GtkImage"> + <property name="icon-name">dino-file-download-symbolic</property> + <property name="icon-size">5</property> + <property name="opacity">0.7</property> + <property name="visible">True</property> + </object> + <packing> + <property name="name">download_image</property> + </packing> + </child> + <child> + <object class="GtkImage" id="content_type_image"> + <property name="icon-size">5</property> + <property name="opacity">0.7</property> + <property name="visible">True</property> + </object> + <packing> + <property name="name">content_type_image</property> + </packing> + </child> + <child> + <object class="GtkSpinner" id="spinner"> + <property name="visible">True</property> + </object> + <packing> + <property name="name">spinner</property> + </packing> + </child> + </object> + </child> + </object> + </child> + <child> + <object class="GtkBox"> + <property name="orientation">vertical</property> + <property name="hexpand">True</property> + <property name="visible">True</property> + <child> + <object class="GtkLabel" id="name_label"> + <property name="ellipsize">middle</property> + <property name="xalign">0</property> + <property name="yalign">0</property> + <property name="visible">True</property> + </object> + </child> + <child> + <object class="GtkLabel" id="mime_label"> + <property name="xalign">0</property> + <property name="yalign">1</property> + <property name="visible">True</property> + <attributes> + <attribute name="scale" value="0.8"/> + </attributes> + </object> + </child> + </object> + </child> + </object> + </child> + </object> + </child> + </template> +</interface> diff --git a/main/src/ui/conversation_content_view/file_default_widget.vala b/main/src/ui/conversation_content_view/file_default_widget.vala new file mode 100644 index 00000000..045223fe --- /dev/null +++ b/main/src/ui/conversation_content_view/file_default_widget.vala @@ -0,0 +1,127 @@ +using Gee; +using Gdk; +using Gtk; + +using Dino.Entities; + +namespace Dino.Ui { + +[GtkTemplate (ui = "/im/dino/Dino/file_default_widget.ui")] +public class FileDefaultWidget : EventBox { + + [GtkChild] public Stack image_stack; + [GtkChild] public Label name_label; + [GtkChild] public Label mime_label; + [GtkChild] public Image content_type_image; + [GtkChild] public Spinner spinner; + [GtkChild] public EventBox stack_event_box; + + private FileTransfer.State state; + private bool pointer_inside = false; + + public FileDefaultWidget() { + this.enter_notify_event.connect(on_pointer_entered); + this.leave_notify_event.connect(on_pointer_left); + + stack_event_box.enter_notify_event.connect((event) => { pointer_inside = true; return false; }); + mime_label.enter_notify_event.connect((event) => { pointer_inside = true; return false; }); + stack_event_box.leave_notify_event.connect((event) => { pointer_inside = true; return false; }); + mime_label.leave_notify_event.connect((event) => { pointer_inside = true; return false; }); + } + + public void update_file_info(string? mime_type, FileTransfer.State state, int size) { + this.state = state; + + spinner.active = false; // A hidden spinning spinner still uses CPU. Deactivate asap + + content_type_image.icon_name = get_file_icon_name(mime_type); + string? mime_description = mime_type != null ? ContentType.get_description(mime_type) : null; + + switch (state) { + case FileTransfer.State.COMPLETE: + mime_label.label = mime_description; + image_stack.set_visible_child_name("content_type_image"); + break; + case FileTransfer.State.IN_PROGRESS: + mime_label.label = _("Downloading %s…").printf(get_size_string(size)); + spinner.active = true; + image_stack.set_visible_child_name("spinner"); + break; + case FileTransfer.State.NOT_STARTED: + if (mime_description != null) { + mime_label.label = _("%s offered: %s").printf(mime_description, get_size_string(size)); + } else if (size != -1) { + mime_label.label = _("File offered: %s").printf(get_size_string(size)); + } else { + mime_label.label = _("File offered"); + } + image_stack.set_visible_child_name("content_type_image"); + break; + case FileTransfer.State.FAILED: + mime_label.use_markup = true; + mime_label.label = "<span foreground=\"#f44336\">" + _("File transfer failed") + "</span>"; + image_stack.set_visible_child_name("content_type_image"); + break; + } + } + + private bool on_pointer_entered(Gdk.EventCrossing event) { + pointer_inside = true; + Timeout.add(20, () => { + if (pointer_inside) { + event.get_window().set_cursor(new Cursor.for_display(Gdk.Display.get_default(), CursorType.HAND2)); + content_type_image.opacity = 0.7; + if (state == FileTransfer.State.NOT_STARTED) { + image_stack.set_visible_child_name("download_image"); + } + } + return false; + }); + return false; + } + + private bool on_pointer_left(Gdk.EventCrossing event) { + pointer_inside = false; + Timeout.add(20, () => { + if (!pointer_inside) { + event.get_window().set_cursor(new Cursor.for_display(Gdk.Display.get_default(), CursorType.XTERM)); + content_type_image.opacity = 0.5; + if (state == FileTransfer.State.NOT_STARTED) { + image_stack.set_visible_child_name("content_type_image"); + } + } + return false; + }); + return false; + } + + private static string get_file_icon_name(string? mime_type) { + if (mime_type == null) return "dino-file-symbolic"; + + string generic_icon_name = ContentType.get_generic_icon_name(mime_type) ?? ""; + switch (generic_icon_name) { + case "audio-x-generic": return "dino-file-music-symbolic"; + case "image-x-generic": return "dino-file-image-symbolic"; + case "text-x-generic": return "dino-file-document-symbolic"; + case "text-x-generic-template": return "dino-file-document-symbolic"; + case "video-x-generic": return "dino-file-video-symbolic"; + case "x-office-document": return "dino-file-document-symbolic"; + case "x-office-spreadsheet": return "dino-file-table-symbolic"; + default: return "dino-file-symbolic"; + } + } + + private static string get_size_string(int size) { + if (size < 1024) { + return @"$(size) B"; + } else if (size < 1000 * 1000) { + return @"$(size / 1000) kB"; + } else if (size < 1000 * 1000 * 1000) { + return @"$(size / 1000 / 1000) MB"; + } else { + return @"$(size / 1000 / 1000 / 1000) GB"; + } + } +} + +} diff --git a/main/src/ui/conversation_content_view/file_widget.vala b/main/src/ui/conversation_content_view/file_widget.vala index ee14af7a..85ac0d56 100644 --- a/main/src/ui/conversation_content_view/file_widget.vala +++ b/main/src/ui/conversation_content_view/file_widget.vala @@ -39,18 +39,9 @@ public class FileWidget : Box { private FileTransfer file_transfer; private State state; - // default box - private Box main_box; - private Image content_type_image; - private Image download_image; - private Spinner spinner; - private Label mime_label; - private Stack image_stack; - + private FileDefaultWidgetController default_widget_controller; private Widget content; - private bool pointer_inside = false; - public FileWidget(StreamInteractor stream_interactor, FileTransfer file_transfer) { this.stream_interactor = stream_interactor; this.file_transfer = file_transfer; @@ -61,32 +52,54 @@ public class FileWidget : Box { Idle.add(() => { parent.queue_resize(); return false; }); } }); + + file_transfer.notify["state"].connect(update_widget_type); + file_transfer.notify["mime-type"].connect(update_widget_type); } private async void load_widget() { if (show_image()) { - content = yield get_image_widget(file_transfer); + content = yield get_image_widget(file_transfer.get_file(), file_transfer.file_name); if (content != null) { this.state = State.IMAGE; this.add(content); return; } } - content = get_default_widget(file_transfer); + 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 Widget? get_image_widget(FileTransfer file_transfer) { + private async void update_widget_type() { + if (file_transfer.state == FileTransfer.State.COMPLETE && show_image() && state != State.IMAGE) { + this.remove(content); + this.add(yield get_image_widget(file_transfer.get_file(), file_transfer.file_name)); + state = State.IMAGE; + return; + } + if (file_transfer.state == FileTransfer.State.FAILED && state == State.IMAGE) { + this.remove(content); + 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); + } + } + + public static async Widget? get_image_widget(File file, string file_name, int MAX_WIDTH=600, int MAX_HEIGHT=300) { // Load and prepare image in tread Thread<Image?> thread = new Thread<Image?> (null, () => { ScalingImage image = new ScalingImage() { halign=Align.START, visible = true, max_width = MAX_WIDTH, max_height = MAX_HEIGHT }; Gdk.Pixbuf pixbuf; try { - pixbuf = new Gdk.Pixbuf.from_file(file_transfer.get_file().get_path()); + pixbuf = new Gdk.Pixbuf.from_file(file.get_path()); } catch (Error error) { - warning("Can't load picture %s - %s", file_transfer.get_file().get_path(), error.message); + warning("Can't load picture %s - %s", file.get_path(), error.message); Idle.add(get_image_widget.callback); return null; } @@ -112,8 +125,8 @@ public class FileWidget : Box { Label url_label = builder.get_object("url_label") as Label; Util.force_color(url_label, "#eee"); - if (file_transfer.file_name != null && file_transfer.file_name != "") { - string caption = file_transfer.file_name; + if (file_name != null && file_name != "") { + string caption = file_name; url_label.label = caption; } else { url_label.visible = false; @@ -125,9 +138,9 @@ public class FileWidget : Box { Util.force_css(open_button, "*:hover { background-color: rgba(255,255,255,0.3); border-color: transparent; }"); open_button.clicked.connect(() => { try{ - AppInfo.launch_default_for_uri(file_transfer.get_file().get_uri(), null); + AppInfo.launch_default_for_uri(file.get_uri(), null); } catch (Error err) { - info("Could not to open file://%s: %s", file_transfer.get_file().get_path(), err.message); + info("Could not to open file://%s: %s", file.get_path(), err.message); } }); @@ -147,208 +160,61 @@ public class FileWidget : Box { return event_box; } - private static Gdk.Pixbuf crop_corners(Gdk.Pixbuf pixbuf, double radius = 3) { - Cairo.Context ctx = new Cairo.Context(new Cairo.ImageSurface(Cairo.Format.ARGB32, pixbuf.width, pixbuf.height)); - Gdk.cairo_set_source_pixbuf(ctx, pixbuf, 0, 0); - double degrees = Math.PI / 180.0; - ctx.new_sub_path(); - ctx.arc(pixbuf.width - radius, radius, radius, -90 * degrees, 0 * degrees); - ctx.arc(pixbuf.width - radius, pixbuf.height - radius, radius, 0 * degrees, 90 * degrees); - ctx.arc(radius, pixbuf.height - radius, radius, 90 * degrees, 180 * degrees); - ctx.arc(radius, radius, radius, 180 * degrees, 270 * degrees); - ctx.close_path(); - ctx.clip(); - ctx.paint(); - return Gdk.pixbuf_get_from_surface(ctx.get_target(), 0, 0, pixbuf.width, pixbuf.height); - } - - private Widget get_default_widget(FileTransfer file_transfer) { - string icon_name = get_file_icon_name(file_transfer.mime_type); - - main_box = new Box(Orientation.HORIZONTAL, 10) { halign=Align.FILL, hexpand=true, visible=true }; - content_type_image = new Image.from_icon_name(icon_name, IconSize.DND) { opacity=0.5, visible=true }; - download_image = new Image.from_icon_name("dino-file-download-symbolic", IconSize.DND) { opacity=0.7, visible=true }; - spinner = new Spinner() { visible=true }; - - EventBox stack_event_box = new EventBox() { visible=true }; - image_stack = new Stack() { transition_type = StackTransitionType.CROSSFADE, transition_duration=50, valign=Align.CENTER, visible=true }; - image_stack.add_named(download_image, "download_image"); - image_stack.add_named(spinner, "spinner"); - image_stack.add_named(content_type_image, "content_type_image"); - stack_event_box.add(image_stack); - - main_box.add(stack_event_box); - - Box right_box = new Box(Orientation.VERTICAL, 0) { hexpand=true, visible=true }; - Label name_label = new Label(file_transfer.file_name) { ellipsize=EllipsizeMode.MIDDLE, xalign=0, yalign=0, visible=true}; - right_box.add(name_label); + private bool show_image() { + if (file_transfer.mime_type == null || file_transfer.state != FileTransfer.State.COMPLETE) return false; - EventBox mime_label_event_box = new EventBox() { visible=true }; - mime_label = new Label("") { use_markup=true, xalign=0, yalign=1, visible=true}; + foreach (PixbufFormat pixbuf_format in Pixbuf.get_formats()) { + foreach (string mime_type in pixbuf_format.get_mime_types()) { + if (mime_type == file_transfer.mime_type) { + return true; + } + } + } + return false; + } +} - mime_label_event_box.add(mime_label); - mime_label.get_style_context().add_class("dim-label"); +public class FileDefaultWidgetController : Object { - right_box.add(mime_label_event_box); - main_box.add(right_box); + private FileDefaultWidget widget; + private FileTransfer file_transfer; + private StreamInteractor stream_interactor; - SizingBin bin = new SizingBin() { visible=true, hexpand=true, max_width=500, target_width=500 }; - bin.add(main_box); + public FileDefaultWidgetController(FileDefaultWidget widget, FileTransfer file_transfer, StreamInteractor stream_interactor) { + this.widget = widget; + this.file_transfer = file_transfer; + this.stream_interactor = stream_interactor; - EventBox event_box = new EventBox() { margin_top=5, halign=Align.START, visible=true }; - event_box.get_style_context().add_class("file-box-outer"); - event_box.add(bin); - - main_box.get_style_context().add_class("file-box"); - - event_box.enter_notify_event.connect((event) => { - pointer_inside = true; - Timeout.add(20, () => { - if (pointer_inside) { - event.get_window().set_cursor(new Cursor.for_display(Gdk.Display.get_default(), CursorType.HAND2)); - content_type_image.opacity = 0.7; - if (file_transfer.state == FileTransfer.State.NOT_STARTED) { - image_stack.set_visible_child_name("download_image"); - } - } - return false; - }); - return false; - }); - stack_event_box.enter_notify_event.connect((event) => { pointer_inside = true; return false; }); - mime_label_event_box.enter_notify_event.connect((event) => { pointer_inside = true; return false; }); - mime_label.enter_notify_event.connect((event) => { pointer_inside = true; return false; }); - event_box.leave_notify_event.connect((event) => { - pointer_inside = false; - Timeout.add(20, () => { - if (!pointer_inside) { - event.get_window().set_cursor(new Cursor.for_display(Gdk.Display.get_default(), CursorType.XTERM)); - content_type_image.opacity = 0.5; - if (file_transfer.state == FileTransfer.State.NOT_STARTED) { - image_stack.set_visible_child_name("content_type_image"); - } - } - return false; - }); - return false; - }); - stack_event_box.leave_notify_event.connect((event) => { pointer_inside = true; return false; }); - mime_label_event_box.leave_notify_event.connect((event) => { pointer_inside = true; return false; }); - mime_label.leave_notify_event.connect((event) => { pointer_inside = true; return false; }); - event_box.button_release_event.connect((event_button) => { - switch (file_transfer.state) { - case FileTransfer.State.COMPLETE: - if (event_button.button == 1) { - try{ - AppInfo.launch_default_for_uri(file_transfer.get_file().get_uri(), null); - } catch (Error err) { - print("Tried to open " + file_transfer.get_file().get_path()); - } - } - break; - case FileTransfer.State.NOT_STARTED: - stream_interactor.get_module(FileManager.IDENTITY).download_file.begin(file_transfer); - break; - } - return false; - }); + widget.name_label.label = file_transfer.file_name; - main_box.events = EventMask.POINTER_MOTION_MASK; - content_type_image.events = EventMask.POINTER_MOTION_MASK; - download_image.events = EventMask.POINTER_MOTION_MASK; - spinner.events = EventMask.POINTER_MOTION_MASK; - image_stack.events = EventMask.POINTER_MOTION_MASK; - right_box.events = EventMask.POINTER_MOTION_MASK; - name_label.events = EventMask.POINTER_MOTION_MASK; - mime_label.events = EventMask.POINTER_MOTION_MASK; - event_box.events = EventMask.POINTER_MOTION_MASK; - mime_label.events = EventMask.POINTER_MOTION_MASK; - mime_label_event_box.events = EventMask.POINTER_MOTION_MASK; + widget.button_release_event.connect(on_clicked); file_transfer.notify["path"].connect(update_file_info); file_transfer.notify["state"].connect(update_file_info); file_transfer.notify["mime-type"].connect(update_file_info); - update_file_info.begin(); - return event_box; + update_file_info(); } - private async void update_file_info() { - if (file_transfer.state == FileTransfer.State.COMPLETE && show_image() && state != State.IMAGE) { - this.remove(content); - this.add(yield get_image_widget(file_transfer)); - state = State.IMAGE; - } - - spinner.active = false; // A hidden spinning spinner still uses CPU. Deactivate asap - - string? mime_description = file_transfer.mime_type != null ? ContentType.get_description(file_transfer.mime_type) : null; + private void update_file_info() { + widget.update_file_info(file_transfer.mime_type, file_transfer.state, file_transfer.size); + } + private bool on_clicked(EventButton event_button) { switch (file_transfer.state) { case FileTransfer.State.COMPLETE: - mime_label.label = "<span size='small'>" + mime_description + "</span>"; - image_stack.set_visible_child_name("content_type_image"); - break; - case FileTransfer.State.IN_PROGRESS: - mime_label.label = "<span size='small'>" + _("Downloading %s…").printf(get_size_string(file_transfer.size)) + "</span>"; - spinner.active = true; - image_stack.set_visible_child_name("spinner"); - break; - case FileTransfer.State.NOT_STARTED: - if (mime_description != null) { - mime_label.label = "<span size='small'>" + _("%s offered: %s").printf(mime_description, get_size_string(file_transfer.size)) + "</span>"; - } else if (file_transfer.size != -1) { - mime_label.label = "<span size='small'>" + _("File offered: %s").printf(get_size_string(file_transfer.size)) + "</span>"; - } else { - mime_label.label = "<span size='small'>" + _("File offered") + "</span>"; + if (event_button.button == 1) { + try{ + AppInfo.launch_default_for_uri(file_transfer.get_file().get_uri(), null); + } catch (Error err) { + print("Tried to open " + file_transfer.get_file().get_path()); + } } - image_stack.set_visible_child_name("content_type_image"); break; - case FileTransfer.State.FAILED: - mime_label.label = "<span size='small' foreground=\"#f44336\">" + _("File transfer failed") + "</span>"; - image_stack.set_visible_child_name("content_type_image"); + case FileTransfer.State.NOT_STARTED: + stream_interactor.get_module(FileManager.IDENTITY).download_file.begin(file_transfer); break; } - } - - private static string get_file_icon_name(string? mime_type) { - if (mime_type == null) return "dino-file-symbolic"; - - string generic_icon_name = ContentType.get_generic_icon_name(mime_type) ?? ""; - switch (generic_icon_name) { - case "audio-x-generic": return "dino-file-music-symbolic"; - case "image-x-generic": return "dino-file-image-symbolic"; - case "text-x-generic": return "dino-file-document-symbolic"; - case "text-x-generic-template": return "dino-file-document-symbolic"; - case "video-x-generic": return "dino-file-video-symbolic"; - case "x-office-document": return "dino-file-document-symbolic"; - case "x-office-spreadsheet": return "dino-file-table-symbolic"; - default: return "dino-file-symbolic"; - } - } - - private static string get_size_string(int size) { - if (size < 1024) { - return @"$(size) B"; - } else if (size < 1000 * 1000) { - return @"$(size / 1000) kB"; - } else if (size < 1000 * 1000 * 1000) { - return @"$(size / 1000 / 1000) MB"; - } else { - return @"$(size / 1000 / 1000 / 1000) GB"; - } - } - - private bool show_image() { - if (file_transfer.mime_type == null || file_transfer.state != FileTransfer.State.COMPLETE) return false; - - foreach (PixbufFormat pixbuf_format in Pixbuf.get_formats()) { - foreach (string mime_type in pixbuf_format.get_mime_types()) { - if (mime_type == file_transfer.mime_type) { - return true; - } - } - } return false; } } |