aboutsummaryrefslogtreecommitdiff
path: root/main/src/ui/util
diff options
context:
space:
mode:
authorfiaxh <git@lightrise.org>2020-04-22 15:44:12 +0200
committerfiaxh <git@lightrise.org>2020-04-22 15:44:12 +0200
commit51a23728694a3f1312cc9396fc093ca178457c3c (patch)
tree321771ae3d807d19387a8656805a648d75347994 /main/src/ui/util
parent7c4260eed718961874fc0ea665263ea2ce59338b (diff)
downloaddino-51a23728694a3f1312cc9396fc093ca178457c3c.tar.gz
dino-51a23728694a3f1312cc9396fc093ca178457c3c.zip
Add file upload preview
fixes #756
Diffstat (limited to 'main/src/ui/util')
-rw-r--r--main/src/ui/util/helper.vala2
-rw-r--r--main/src/ui/util/scaling_image.vala70
-rw-r--r--main/src/ui/util/sizing_bin.vala4
3 files changed, 55 insertions, 21 deletions
diff --git a/main/src/ui/util/helper.vala b/main/src/ui/util/helper.vala
index 78c8f527..65318870 100644
--- a/main/src/ui/util/helper.vala
+++ b/main/src/ui/util/helper.vala
@@ -219,7 +219,7 @@ public static void image_set_from_scaled_pixbuf(Image image, Gdk.Pixbuf pixbuf,
private const string force_background_css = "%s { background-color: %s; }";
private const string force_color_css = "%s { color: %s; }";
-private static void force_css(Gtk.Widget widget, string css) {
+public static void force_css(Gtk.Widget widget, string css) {
var p = new Gtk.CssProvider();
try {
p.load_from_data(css);
diff --git a/main/src/ui/util/scaling_image.vala b/main/src/ui/util/scaling_image.vala
index 276cc25c..cfc60847 100644
--- a/main/src/ui/util/scaling_image.vala
+++ b/main/src/ui/util/scaling_image.vala
@@ -25,17 +25,51 @@ class ScalingImage : Image {
queue_resize();
}
+ private void calculate_size(ref double exact_width, ref double exact_height) {
+ if (exact_width == -1 && exact_height == -1) {
+ if (target_width == -1) {
+ exact_width = ((double)image_width) / ((double)scale_factor);
+ exact_height = ((double)image_height) / ((double)scale_factor);
+ } else {
+ exact_width = target_width;
+ exact_height = exact_width * image_ratio;
+ }
+ } else if (exact_width != -1) {
+ exact_height = exact_width * image_ratio;
+ } else if (exact_height != -1) {
+ exact_width = exact_height / image_ratio;
+ } else {
+ if (exact_width * image_ratio > exact_height + scale_factor) {
+ exact_width = exact_height / image_ratio;
+ } else if (exact_height / image_ratio > exact_width + scale_factor) {
+ exact_height = exact_width * image_ratio;
+ }
+ }
+ if (max_width != -1 && exact_width > max_width) {
+ exact_width = max_width;
+ exact_height = exact_width * image_ratio;
+ }
+ if (max_height != -1 && exact_height > max_height) {
+ exact_height = max_height;
+ exact_width = exact_height / image_ratio;
+ }
+ if (exact_width < min_width) exact_width = min_width;
+ if (exact_height < min_height) exact_height = min_height;
+ }
+
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));
+ allocation.height = int.max(allocation.height, min_height);
+ allocation.width = int.max(allocation.width, min_width);
+ double exact_width = allocation.width, exact_height = allocation.height;
+ calculate_size(ref exact_width, ref exact_height);
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);
+ Pixbuf scaled = image.scale_simple((int) Math.floor(exact_width * scale_factor), (int) Math.floor(exact_height * scale_factor), Gdk.InterpType.BILINEAR);
scaled = crop_corners(scaled, 3 * scale_factor);
Util.image_set_from_scaled_pixbuf(this, scaled);
}
@@ -58,34 +92,34 @@ class ScalingImage : Image {
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);
- }
+ double exact_width = -1, exact_height = -1;
+ calculate_size(ref exact_width, ref exact_height);
+ natural_width = (int) Math.ceil(exact_width);
}
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);
- }
+ double exact_width = -1, exact_height = -1;
+ calculate_size(ref exact_width, ref exact_height);
+ natural_height = (int) Math.ceil(exact_height);
}
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;
+ double exact_width = width, exact_height = -1;
+ calculate_size(ref exact_width, ref exact_height);
+ natural_height = (int) Math.ceil(exact_height);
+ minimum_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;
+ double exact_width = -1, exact_height = height;
+ calculate_size(ref exact_width, ref exact_height);
+ natural_width = (int) Math.ceil(exact_width);
+ minimum_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
index b81ff7e3..9c5ff4c7 100644
--- a/main/src/ui/util/sizing_bin.vala
+++ b/main/src/ui/util/sizing_bin.vala
@@ -1,7 +1,7 @@
using Gtk;
namespace Dino.Ui {
-class SizingBin : Bin {
+public 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; }
@@ -32,4 +32,4 @@ class SizingBin : Bin {
}
}
-} \ No newline at end of file
+}