Scale files and images to the available space
This commit is contained in:
parent
25751c88ce
commit
d55f6691c6
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<Image?> thread = new Thread<Image?> (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) => {
|
||||
|
|
91
main/src/ui/util/scaling_image.vala
Normal file
91
main/src/ui/util/scaling_image.vala
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
35
main/src/ui/util/sizing_bin.vala
Normal file
35
main/src/ui/util/sizing_bin.vala
Normal file
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue