File: gitg-diff-view-file-renderer-image.vala

package info (click to toggle)
gitg 41-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,876 kB
  • sloc: ansic: 1,636; ruby: 1,445; sh: 314; python: 261; xml: 57; makefile: 15
file content (152 lines) | stat: -rw-r--r-- 4,489 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * This file is part of gitg
 *
 * Copyright (C) 2016 - Jesse van den Kieboom
 *
 * gitg is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * gitg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gitg. If not, see <http://www.gnu.org/licenses/>.
 */

[GtkTemplate (ui = "/org/gnome/gitg/ui/gitg-diff-view-file-renderer-image.ui")]
class Gitg.DiffViewFileRendererImage : Gtk.Grid, DiffViewFileRenderer
{
	public Ggit.DiffDelta? delta { get; construct set; }
	public Repository repository { get; construct set; }

	[GtkChild( name = "diff_image_side_by_side" )]
	private unowned Gitg.DiffImageSideBySide d_diff_image_side_by_side;

	[GtkChild( name = "diff_image_slider" )]
	private unowned Gitg.DiffImageSlider d_diff_image_slider;

	[GtkChild( name = "scale_slider_adjustment" )]
	private unowned Gtk.Adjustment d_scale_slider_adjustment;

	[GtkChild( name = "diff_image_overlay" )]
	private unowned Gitg.DiffImageOverlay d_diff_image_overlay;

	[GtkChild( name = "scale_overlay_adjustment" )]
	private unowned Gtk.Adjustment d_scale_overlay_adjustment;

	[GtkChild( name = "diff_image_difference" )]
	private unowned Gitg.DiffImageDifference d_diff_image_difference;

	[GtkChild( name = "stack_switcher" )]
	private unowned Gtk.StackSwitcher d_stack_switcher;

	private SurfaceCache d_cache;

	public DiffViewFileRendererImage(Repository repository, Ggit.DiffDelta delta)
	{
		Object(repository: repository, delta: delta);
	}

	construct
	{
		d_cache = new SurfaceCache(pixbuf_for_file(delta.get_old_file()),
		                           pixbuf_for_file(delta.get_new_file()));

		d_diff_image_side_by_side.cache = d_cache;
		d_diff_image_slider.cache = d_cache;
		d_diff_image_overlay.cache = d_cache;
		d_diff_image_difference.cache = d_cache;

		if (d_cache.old_pixbuf == null || d_cache.new_pixbuf == null ||
		    d_cache.old_pixbuf.get_width() != d_cache.new_pixbuf.get_width() ||
		    d_cache.old_pixbuf.get_height() != d_cache.new_pixbuf.get_height())
		{
			d_stack_switcher.sensitive = false;
		}

		d_scale_slider_adjustment.bind_property("value", d_diff_image_slider, "position", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
		d_scale_overlay_adjustment.bind_property("value", d_diff_image_overlay, "alpha", BindingFlags.DEFAULT | BindingFlags.SYNC_CREATE);
	}

	private Gdk.Pixbuf? pixbuf_for_file(Ggit.DiffFile file)
	{
		if ((file.get_flags() & Ggit.DiffFlag.VALID_ID) == 0 || file.get_oid().is_zero())
		{
			return null;
		}

		Ggit.Blob blob;

		try
		{
			blob = repository.lookup<Ggit.Blob>(file.get_oid());
		}
		catch (Error e)
		{
			stderr.printf(@"ERROR: failed to load image blob: $(e.message)\n");
			return null;
		}

		var stream = new MemoryInputStream.from_data(blob.get_raw_content(), null);

		try
		{
			return new Gdk.Pixbuf.from_stream(stream);
		}
		catch (Error e)
		{
			stderr.printf(@"ERROR: failed to create pixbuf: $(e.message)\n");
			return null;
		}
	}

	public void add_hunk(Ggit.DiffHunk hunk, Gee.ArrayList<Ggit.DiffLine> lines)
	{
	}

	private class SurfaceCache : Object, Gitg.DiffImageSurfaceCache {
		private Cairo.Surface? d_old_surface;
		private Cairo.Surface? d_new_surface;

		public Gdk.Pixbuf? old_pixbuf { get; construct set; }
		public Gdk.Pixbuf? new_pixbuf { get; construct set; }

		public Gdk.Window window { get; construct set; }

		public SurfaceCache(Gdk.Pixbuf? old_pixbuf, Gdk.Pixbuf? new_pixbuf)
		{
			Object(old_pixbuf: old_pixbuf, new_pixbuf: new_pixbuf);
		}

		public Cairo.Surface? get_old_surface(Gdk.Window window)
		{
			return get_cached_surface(window, old_pixbuf, ref d_old_surface);
		}

		public Cairo.Surface? get_new_surface(Gdk.Window window)
		{
			return get_cached_surface(window, new_pixbuf, ref d_new_surface);
		}

		private Cairo.Surface? get_cached_surface(Gdk.Window window, Gdk.Pixbuf? pixbuf, ref Cairo.Surface? cached)
		{
			if (pixbuf == null)
			{
				return null;
			}

			if (cached == null)
			{
				cached = Gdk.cairo_surface_create_from_pixbuf(pixbuf, 0, window);
			}

			return cached;
		}
	}
}

// ex:ts=4 noet