Commit fe6ebf02 authored by Gaurav Agrawal's avatar Gaurav Agrawal 🌳 Committed by Alberto Fanjul
Browse files

Refactor diferencia and visual edit diff model

- Turn type into an enum
- Extract linkmap widget to a class
- turn diffs info into a list of diff model
- Refactored Application to Class
- Visually edit the diff model
parent fd4e0671
Pipeline #88377 failed
public class DiferenciaApp : Gtk.Application {
public DiferenciaApp () {
Object (application_id: "org.gnome.Diferencia",
flags : ApplicationFlags.FLAGS_NONE);
}
protected override void activate () {
new Window (this).show_all ();
}
}
public class DiffModel {
public enum DiffType {
ADD,
MODIFIED,
REMOVED
}
public int f0 { get; set; }
public int f1 { get; set; }
public int t0 { get; set; }
public int t1 { get; set; }
public DiffType diff_type { get; set; }
}
public class LinkMap : Gtk.DrawingArea {
public LinkMap () {
Object ();
}
public Gtk.SourceView left_sourceview { get; set; }
public Gtk.SourceView right_sourceview { get; set; }
private List<weak DiffModel> d_diff_model_list = null;
public unowned List<weak DiffModel> diff_model_list {
get {
return d_diff_model_list;
}
set {
d_diff_model_list = value.copy ();
}
}
public override bool draw (Cairo.Context context) {
if (d_diff_model_list != null) {
bool has_diffs = d_diff_model_list.length () > 0;
if (!has_diffs)
return false;
Gtk.Allocation ? left_rectangle = null;
Gtk.Allocation ? right_rectangle = null;
left_sourceview.get_allocation (out left_rectangle);
right_sourceview.get_allocation (out right_rectangle);
// int[] pix_start = { left_rectangle.y, right_rectangle.y };
int dxl = 0, dyl = 0;
left_sourceview.translate_coordinates (get_toplevel (), 0, 0, out dxl, out dyl);
int dxr = 0, dyr = 0;
right_sourceview.translate_coordinates (get_toplevel (), 0, 0, out dxr, out dyr);
int[] y_offset = { dyl + 1, dyr + 1 };
var clip_y = array_get_min (y_offset) - 1;
int[] heights = { left_rectangle.height, right_rectangle.height };
var clip_height = array_get_max (heights) + 2;
Gtk.Allocation ? allocation = null;
get_allocation (out allocation);
weak Gtk.StyleContext style_context = get_style_context ();
style_context.render_background (context, 0, clip_y, allocation.width, clip_height);
context.set_line_width (1.0);
int height = get_allocated_height ();
/*
int[] visible = {
get_line_num_for_y (left_sourceview, pix_start[0]),
get_line_num_for_y (left_sourceview, pix_start[0] + height),
get_line_num_for_y (right_sourceview, pix_start[1]),
get_line_num_for_y (right_sourceview, pix_start[1] + height),
};
*/
int wtotal = get_allocated_width ();
// For bezier control points
double[] x_steps = { -0.5, wtotal / 2, wtotal / 2, wtotal + 0.5 };
double q_rad = GLib.Math.PI / 2;
// left, right = self.view_indices
var RADIUS = 3;
d_diff_model_list.foreach ((data) => {
int f0 = data.f0;
int t0 = data.t0;
int f1 = data.f1;
int t1 = data.t1;
f1 = f1 == f0 ? f1 : f1 - 1;
t1 = t1 == t0 ? t1 : t1 - 1;
if ((t0 < 0 && t1 < 0) || (t0 > height && t1 > height)) {
if (f0 == f1)
return;
context.arc (x_steps[0], f0 - 0.5 + RADIUS, RADIUS, -q_rad, 0);
context.arc (x_steps[0], f1 - 0.5 - RADIUS, RADIUS, 0, q_rad);
context.close_path ();
} else if ((f0 < 0 && f1 < 0) || (f0 > height && f1 > height)) {
if (t0 == t1)
return;
context.arc_negative (x_steps[3], t0 - 0.5 + RADIUS, RADIUS, -q_rad, q_rad * 2);
context.arc_negative (x_steps[3], t1 - 0.5 - RADIUS, RADIUS, q_rad * 2, q_rad);
context.close_path ();
} else {
context.move_to (x_steps[0], f0 - 0.5);
context.curve_to (x_steps[1], f0 - 0.5, x_steps[2], t0 - 0.5, x_steps[3], t0 - 0.5);
context.line_to (x_steps[3], t1 - 0.5);
context.curve_to (x_steps[2], t1 - 0.5, x_steps[1], f1 - 0.5, x_steps[0], f1 - 0.5);
context.close_path ();
}
// context.set_source_rgba(self.fill_colors[c[0]]);
var color = Gdk.RGBA ();
string color_str = null;
switch (data.diff_type) {
case DiffModel.DiffType.ADD:
color_str = "#008800";
break;
case DiffModel.DiffType.MODIFIED:
color_str = "#1d59d6";
break;
case DiffModel.DiffType.REMOVED:
color_str = "#ff0000";
break;
}
color.parse (color_str);
context.set_source_rgba (color.red, color.green, color.blue, color.alpha);
context.fill_preserve ();
// var chunk_idx = self.filediff.linediffer.locate_chunk(left, c[1])[0]
// if chunk_idx == self.filediff.cursor.chunk:
// if (false) {
//// var highlight = self.fill_colors['current-chunk-highlight']
// context.set_source_rgba ( /*highlight*/ 0, 0, 0, 0);
// context.fill_preserve ();
// }
// context.set_source_rgba(self.line_colors[c[0]]);
color_str = null;
switch (data.diff_type) {
case DiffModel.DiffType.ADD:
color_str = "#a5ff4c";
break;
case DiffModel.DiffType.MODIFIED:
color_str = "#0053a6";
break;
case DiffModel.DiffType.REMOVED:
color_str = "#ac3b39";
break;
}
color.parse (color_str);
context.set_source_rgba (color.red, color.green, color.blue, color.alpha);
context.stroke ();
});
return true;
}
return false;
}
/*
int view_offset_line (Gtk.SourceView source_view, int line_num, int pix_start, int y_offset) {
int line_start = get_y_for_line_num (source_view, line_num);
return line_start - pix_start + y_offset;
}
int get_y_for_line_num (Gtk.SourceView source_view, int line) {
var buf = source_view.get_buffer ();
Gtk.TextIter it;
buf.get_iter_at_line (out it, line);
int y, h;
source_view.get_line_yrange (it, out y, out h);
if (line >= buf.get_line_count ())
return y + h;
return y;
}
int get_line_num_for_y (Gtk.SourceView source_view, int y) {
int line_start;
source_view.get_line_at_y (null, y, out line_start);
return line_start;
}
*/
int array_get_max (int[] array) {
int max = array[0];
for (int i = 0; i < array.length; i++)
if (array[i] > max)
max = array[i];
return max;
}
int array_get_min (int[] array) {
int min = array[0];
for (int i = 0; i < array.length; i++)
if (array[i] < min)
min = array[i];
return min;
}
}
......@@ -18,15 +18,6 @@
int main (string[] args) {
typeof (Gtk.SourceView).ensure ();
var app = new Gtk.Application ("org.gnome.Diferencia", ApplicationFlags.FLAGS_NONE);
app.activate.connect (() => {
var win = app.active_window;
if (win == null) {
win = new Diferencia.Window (app);
}
win.present ();
});
DiferenciaApp app = new DiferenciaApp ();
return app.run (args);
}
diferencia_sources = [
'main.vala',
'diferencia.vala',
'window.vala',
'linkmap.vala',
'diff_model.vala'
]
diferencia_deps = [
......
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<!-- <requires lib="gtk+" version="3.12"/> -->
<!-- <requires lib="gtksourceview" version="3.0"/> -->
<!-- interface-requires gtk+ 3.12 -->
<!-- interface-requires gtksourceview 3.0 -->
<template class="DiferenciaWindow" parent="GtkApplicationWindow">
<property name="default-width">600</property>
<property name="default-height">300</property>
<property name="title" translatable="no">Diferencia</property>
<requires lib="gtk+" version="3.12"/>
<requires lib="gtksourceview" version="3.0"/>
<object class="GtkAdjustment" id="f0_adjustment">
<property name="upper">60000</property>
<property name="step_increment">1</property>
<property name="page_increment">3</property>
</object>
<object class="GtkAdjustment" id="f1_adjustment">
<property name="upper">60000</property>
<property name="step_increment">1</property>
<property name="page_increment">3</property>
</object>
<object class="GtkAdjustment" id="t0_adjustment">
<property name="upper">60000</property>
<property name="step_increment">1</property>
<property name="page_increment">3</property>
</object>
<object class="GtkAdjustment" id="t1_adjustment">
<property name="upper">60000</property>
<property name="step_increment">1</property>
<property name="page_increment">3</property>
</object>
<template class="Window" parent="GtkApplicationWindow">
<property name="can_focus">False</property>
<property name="title">Diferencia</property>
<property name="default_width">600</property>
<property name="default_height">300</property>
<child>
<object class="GtkBox" id="box">
<placeholder/>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkScrolledWindow" id="left_scrolledwindow">
<object class="GtkBox" id="box">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<property name="can_focus">False</property>
<child>
<object class="GtkScrolledWindow" id="left_scrolledwindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkSourceView" id="left_sourceview">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="left_margin">2</property>
<property name="right_margin">2</property>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSourceView" id="left_sourceview">
<object class="LinkMap" id="linkmap">
<property name="width_request">30</property>
<property name="visible">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow" id="right_scrolledwindow">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="left_margin">2</property>
<property name="right_margin">2</property>
<property name="shadow_type">in</property>
<child>
<object class="GtkSourceView" id="right_sourceview">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="left_margin">2</property>
<property name="right_margin">2</property>
</object>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
......@@ -33,34 +99,217 @@
</packing>
</child>
<child>
<object class="GtkDrawingArea" id="linkmap">
<property name="width_request">50</property>
<object class="GtkListBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="selection_mode">none</property>
<child>
<object class="GtkListBoxRow">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="margin_left">20</property>
<property name="label">f0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">3</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="f0_button">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="adjustment">f0_adjustment</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">3</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label">t0</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">3</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="t0_button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="text" translatable="yes">0</property>
<property name="adjustment">t0_adjustment</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">3</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label">f1</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">3</property>
<property name="position">4</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="f1_button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="text" translatable="yes">0</property>
<property name="adjustment">f1_adjustment</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">3</property>
<property name="position">5</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label">t1</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">3</property>
<property name="position">6</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="t1_button">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="text" translatable="yes">0</property>
<property name="adjustment">t1_adjustment</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">3</property>
<property name="position">7</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="label">type</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">3</property>
<property name="position">8</property>
</packing>
</child>
<child>
<object class="GtkComboBoxText" id="type_activity">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="padding">3</property>
<property name="position">9</property>
</packing>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow" id="right_scrolledwindow">
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">in</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton">
<property name="label">Add</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<signal name="clicked" handler="on_add_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSourceView" id="right_sourceview">
<object class="GtkButton">
<property name="label">Remove</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="left_margin">2</property>
<property name="right_margin">2</property>
<property name="receives_default">False</property>
<signal name="clicked" handler="on_remove_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton">
<property name="label">Update</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">False</property>
<signal name="clicked" handler="on_update_clicked" swapped="no"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">2</property>
</packing>
</child>
......@@ -68,4 +317,3 @@
</child>
</template>
</interface>
......@@ -16,198 +16,113 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Diferencia {
[GtkTemplate (ui = "/org/gnome/Diferencia/window.ui")]
public class Window : Gtk.ApplicationWindow {
[GtkChild (name = "left_sourceview")]
private Gtk.SourceView left_sourceview;
[GtkChild (name = "right_sourceview")]
private Gtk.SourceView right_sourceview;
[GtkChild (name = "linkmap")]
private Gtk.DrawingArea linkmap;
public Window (Gtk.Application app) {
Object (application: app);
left_sourceview.buffer.text = "adios\nadios\nadios\neco\neco\neco\neco\neco\neco\neco\nadios\nadios\nadios\nadios\n";
right_sourceview.buffer.text = "hola\nhola\nadios\nadiosadios\nmola\nmola\nmola\nadios\nadios\nadios\nadios\n";
linkmap.draw.connect ((context) => {
int[, ] diff_model = { { 1, 1, 1, 35, 0 },
{ 69, 86, 171, 137, 1 },
{ 239, 205, 205, 205, 2 }, }; // TODO: Model lines increments in ~18. first to numbers represent left range and last two right_range
bool has_diffs = diff_model.length[0] > 0;