From d55f6691c68757d2c8f7a361c20ace8dac61da5c Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sat, 14 Mar 2020 00:24:41 +0100 Subject: [PATCH] Scale files and images to the available space --- main/CMakeLists.txt | 2 + main/data/theme.css | 6 +- .../content_item_widget_factory.vala | 2 +- .../file_widget.vala | 28 +++--- main/src/ui/util/scaling_image.vala | 91 +++++++++++++++++++ main/src/ui/util/sizing_bin.vala | 35 +++++++ 6 files changed, 146 insertions(+), 18 deletions(-) create mode 100644 main/src/ui/util/scaling_image.vala create mode 100644 main/src/ui/util/sizing_bin.vala diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 64627565..f2c605d4 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -156,6 +156,8 @@ SOURCES src/ui/util/data_forms.vala src/ui/util/helper.vala src/ui/util/label_hybrid.vala + src/ui/util/sizing_bin.vala + src/ui/util/scaling_image.vala src/ui/util/preview_file_chooser_native.vala CUSTOM_VAPIS ${CMAKE_BINARY_DIR}/exports/xmpp-vala.vapi diff --git a/main/data/theme.css b/main/data/theme.css index 856e1b27..f72a20d2 100644 --- a/main/data/theme.css +++ b/main/data/theme.css @@ -52,14 +52,14 @@ window.dino-main .dino-sidebar > frame { transition: background .05s ease; } -.file-box-outer { +window.dino-main .dino-conversation .file-box-outer { background: @theme_base_color; border-radius: 3px; border: 1px solid alpha(@theme_fg_color, 0.1); } -.file-box { - margin: 12px; +window.dino-main .dino-conversation .file-box { + margin: 12px 16px 12px 12px; } window.dino-main .dino-sidebar > frame.collapsed { diff --git a/main/src/ui/conversation_content_view/content_item_widget_factory.vala b/main/src/ui/conversation_content_view/content_item_widget_factory.vala index 54283e75..da092e34 100644 --- a/main/src/ui/conversation_content_view/content_item_widget_factory.vala +++ b/main/src/ui/conversation_content_view/content_item_widget_factory.vala @@ -40,7 +40,7 @@ public interface WidgetGenerator : Object { public class MessageItemWidgetGenerator : WidgetGenerator, Object { - public string handles_type { get; set; default=FileItem.TYPE; } + public string handles_type { get; set; default=MessageItem.TYPE; } private StreamInteractor stream_interactor; diff --git a/main/src/ui/conversation_content_view/file_widget.vala b/main/src/ui/conversation_content_view/file_widget.vala index dd28b385..f5ba08e3 100644 --- a/main/src/ui/conversation_content_view/file_widget.vala +++ b/main/src/ui/conversation_content_view/file_widget.vala @@ -38,6 +38,11 @@ public class FileWidget : Box { this.file_transfer = file_transfer; load_widget.begin(); + size_allocate.connect((allocation) => { + if (allocation.height > parent.get_allocated_height()) { + Idle.add(() => { parent.queue_resize(); return false; }); + } + }); } private async void load_widget() { @@ -57,7 +62,7 @@ public class FileWidget : Box { private async Widget? get_image_widget(FileTransfer file_transfer) { // Load and prepare image in tread Thread thread = new Thread (null, () => { - Image image = new Image() { halign=Align.START, visible = true }; + ScalingImage image = new ScalingImage() { halign=Align.START, visible = true, max_width = MAX_WIDTH, max_height = MAX_HEIGHT }; Gdk.Pixbuf pixbuf; try { @@ -70,16 +75,7 @@ public class FileWidget : Box { pixbuf = pixbuf.apply_embedded_orientation(); - int max_scaled_height = MAX_HEIGHT * image.scale_factor; - if (pixbuf.height > max_scaled_height) { - pixbuf = pixbuf.scale_simple((int) ((double) max_scaled_height / pixbuf.height * pixbuf.width), max_scaled_height, Gdk.InterpType.BILINEAR); - } - int max_scaled_width = MAX_WIDTH * image.scale_factor; - if (pixbuf.width > max_scaled_width) { - pixbuf = pixbuf.scale_simple(max_scaled_width, (int) ((double) max_scaled_width / pixbuf.width * pixbuf.height), Gdk.InterpType.BILINEAR); - } - pixbuf = crop_corners(pixbuf, 3 * image.get_scale_factor()); - Util.image_set_from_scaled_pixbuf(image, pixbuf); + image.load(pixbuf); Idle.add(get_image_widget.callback); return image; @@ -166,7 +162,7 @@ public class FileWidget : Box { 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, max_width_chars=1, hexpand=true, xalign=0, yalign=0, 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); EventBox mime_label_event_box = new EventBox() { visible=true }; @@ -178,9 +174,13 @@ public class FileWidget : Box { right_box.add(mime_label_event_box); main_box.add(right_box); - EventBox event_box = new EventBox() { margin_top=5, width_request=500, halign=Align.START, visible=true }; + SizingBin bin = new SizingBin() { visible=true, hexpand=true, max_width=500, target_width=500 }; + bin.add(main_box); + + 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(main_box); + event_box.add(bin); + main_box.get_style_context().add_class("file-box"); event_box.enter_notify_event.connect((event) => { diff --git a/main/src/ui/util/scaling_image.vala b/main/src/ui/util/scaling_image.vala new file mode 100644 index 00000000..276cc25c --- /dev/null +++ b/main/src/ui/util/scaling_image.vala @@ -0,0 +1,91 @@ +using Gdk; +using Gtk; + +namespace Dino.Ui { +class ScalingImage : Image { + public int min_width { get; set; default = -1; } + public int target_width { get; set; default = -1; } + public int max_width { get; set; default = -1; } + public int min_height { get; set; default = -1; } + public int max_height { get; set; default = -1; } + + private Pixbuf image; + private double image_ratio; + private int image_height = 0; + private int image_width = 0; + private int last_allocation_height = -1; + private int last_allocation_width = -1; + private int last_scale_factor = -1; + + public void load(Pixbuf image) { + this.image = image; + this.image_ratio = ((double)image.height) / ((double)image.width); + this.image_height = image.height; + this.image_width = image.width; + queue_resize(); + } + + public override void size_allocate(Allocation allocation) { + if (max_width != -1) allocation.width = int.min(allocation.width, max_width); + if (max_height != -1) allocation.height = int.min(allocation.height, max_height); + allocation.height = int.min(allocation.height, (int)(allocation.width * image_ratio)); + allocation.width = int.min(allocation.width, (int)(allocation.height / image_ratio)); + base.size_allocate(allocation); + if (last_allocation_height != allocation.height || last_allocation_width != allocation.width || last_scale_factor != scale_factor) { + last_allocation_height = allocation.height; + last_allocation_width = allocation.width; + last_scale_factor = scale_factor; + Pixbuf scaled = image.scale_simple(allocation.width * scale_factor, allocation.height * scale_factor, Gdk.InterpType.BILINEAR); + scaled = crop_corners(scaled, 3 * scale_factor); + Util.image_set_from_scaled_pixbuf(this, scaled); + } + } + + 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); + } + + public override void get_preferred_width(out int minimum_width, out int natural_width) { + minimum_width = int.max(0, min_width); + natural_width = target_width != -1 ? target_width : (image_width / scale_factor); + natural_width = int.min(natural_width, max_width); + if (natural_width * image_ratio > max_height) { + natural_width = (int) (max_height / image_ratio); + } + } + + public override void get_preferred_height(out int minimum_height, out int natural_height) { + minimum_height = int.max(0, min_height); + natural_height = (int) (target_width != -1 ? target_width * image_ratio : image_width / scale_factor); + natural_height = int.min(natural_height, max_height); + if (natural_height / image_ratio > max_width) { + natural_height = (int) (max_width * image_ratio); + } + } + + public override void get_preferred_height_for_width(int width, out int minimum_height, out int natural_height) { + natural_height = (int) (width * image_ratio); + minimum_height = min_height != -1 ? int.min(min_height, natural_height) : natural_height; + } + + public override void get_preferred_width_for_height(int height, out int minimum_width, out int natural_width) { + natural_width = (int) (height / image_ratio); + minimum_width = min_width != -1 ? int.min(min_width, natural_width) : natural_width; + } + + public override SizeRequestMode get_request_mode() { + return SizeRequestMode.HEIGHT_FOR_WIDTH; + } +} +} \ No newline at end of file diff --git a/main/src/ui/util/sizing_bin.vala b/main/src/ui/util/sizing_bin.vala new file mode 100644 index 00000000..b81ff7e3 --- /dev/null +++ b/main/src/ui/util/sizing_bin.vala @@ -0,0 +1,35 @@ +using Gtk; + +namespace Dino.Ui { +class SizingBin : Bin { + public int min_width { get; set; default = -1; } + public int target_width { get; set; default = -1; } + public int max_width { get; set; default = -1; } + public int min_height { get; set; default = -1; } + public int target_height { get; set; default = -1; } + public int max_height { get; set; default = -1; } + + public override void size_allocate(Allocation allocation) { + if (max_height != -1) allocation.height = int.min(allocation.height, max_height); + if (max_width != -1) allocation.width = int.min(allocation.width, max_width); + base.size_allocate(allocation); + } + + public override void get_preferred_width(out int minimum_width, out int natural_width) { + base.get_preferred_width(out minimum_width, out natural_width); + if (min_width != -1) minimum_width = int.max(minimum_width, min_width); + if (max_width != -1) natural_width = int.min(natural_width, max_width); + if (target_width != -1) natural_width = int.max(natural_width, target_width); + natural_width = int.max(natural_width, minimum_width); + } + + public override void get_preferred_height_for_width(int width, out int minimum_height, out int natural_height) { + base.get_preferred_height_for_width(width, out minimum_height, out natural_height); + if (min_height != -1) minimum_height = int.max(minimum_height, min_height); + if (max_height != -1) natural_height = int.min(natural_height, max_height); + if (target_height != -1) natural_height = int.max(natural_height, target_height); + natural_height = int.max(natural_height, minimum_height); + } + +} +} \ No newline at end of file