mirror of
https://gitlab.gnome.org/GNOME/gitg
synced 2024-10-31 04:58:58 +00:00
Fix diff line renderer sizing issues
This commit is contained in:
parent
7b51de39a4
commit
c92aa7e049
3 changed files with 61 additions and 74 deletions
|
@ -248,12 +248,12 @@ render_lines (GtkSourceGutterRenderer *renderer,
|
|||
GtkSourceGutterRendererState renderer_state)
|
||||
{
|
||||
GitgDiffLineRenderer *lr = GITG_DIFF_LINE_RENDERER (renderer);
|
||||
/* Render new/old in the cell area */
|
||||
gchar old_str[16];
|
||||
gchar new_str[16];
|
||||
PangoLayout *layout;
|
||||
GtkWidget *widget;
|
||||
GtkStyleContext *style_context;
|
||||
guint xpad = 0;
|
||||
|
||||
widget = GTK_WIDGET (gtk_source_gutter_renderer_get_view (renderer));
|
||||
layout = lr->priv->cached_layout;
|
||||
|
@ -280,12 +280,14 @@ render_lines (GtkSourceGutterRenderer *renderer,
|
|||
*new_str = '\0';
|
||||
}
|
||||
|
||||
g_object_get (renderer, "xpad", &xpad, NULL);
|
||||
|
||||
pango_layout_set_text (layout, old_str, -1);
|
||||
style_context = gtk_widget_get_style_context (widget);
|
||||
|
||||
gtk_render_layout (style_context,
|
||||
ctx,
|
||||
cell_area->x + cell_area->width / 2 - 1,
|
||||
cell_area->x + cell_area->width / 2 - xpad,
|
||||
cell_area->y,
|
||||
layout);
|
||||
|
||||
|
@ -410,87 +412,48 @@ measure_text (GitgDiffLineRenderer *lr,
|
|||
static void
|
||||
recalculate_size (GitgDiffLineRenderer *lr)
|
||||
{
|
||||
/* Get size of this rendering */
|
||||
gint num_digits, num;
|
||||
gchar *markup;
|
||||
gint size;
|
||||
gint num = 1;
|
||||
gint i;
|
||||
|
||||
num_digits = 0;
|
||||
num = lr->priv->line_old;
|
||||
|
||||
while (num > 0)
|
||||
for (i = 1; i < lr->priv->num_digits; ++i)
|
||||
{
|
||||
num /= 10;
|
||||
++num_digits;
|
||||
num *= 10;
|
||||
}
|
||||
|
||||
num = lr->priv->line_new;
|
||||
markup = g_strdup_printf ("<b>%d %d</b>",
|
||||
num,
|
||||
num);
|
||||
|
||||
while (num > 0)
|
||||
measure_text (lr, markup, NULL, &size, NULL);
|
||||
g_free (markup);
|
||||
|
||||
gtk_source_gutter_renderer_set_size (GTK_SOURCE_GUTTER_RENDERER (lr),
|
||||
size);
|
||||
}
|
||||
|
||||
static void
|
||||
update_num_digits (GitgDiffLineRenderer *renderer,
|
||||
guint max_line_count)
|
||||
{
|
||||
/* Get size of this rendering */
|
||||
gint num_digits;
|
||||
|
||||
num_digits = 0;
|
||||
|
||||
while (max_line_count > 0)
|
||||
{
|
||||
num /= 10;
|
||||
max_line_count /= 10;
|
||||
++num_digits;
|
||||
}
|
||||
|
||||
num_digits = MAX (num_digits, 2);
|
||||
|
||||
if (num_digits != lr->priv->num_digits)
|
||||
if (num_digits != renderer->priv->num_digits)
|
||||
{
|
||||
gchar *markup;
|
||||
gint size;
|
||||
|
||||
lr->priv->num_digits = num_digits;
|
||||
|
||||
markup = g_strdup_printf ("<b>%d %d</b>",
|
||||
lr->priv->line_old,
|
||||
lr->priv->line_new);
|
||||
|
||||
measure_text (lr, markup, NULL, &size, NULL);
|
||||
g_free (markup);
|
||||
|
||||
gtk_source_gutter_renderer_set_size (GTK_SOURCE_GUTTER_RENDERER (lr),
|
||||
size);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
on_buffer_changed (GtkSourceBuffer *buffer,
|
||||
GitgDiffLineRenderer *renderer)
|
||||
{
|
||||
recalculate_size (renderer);
|
||||
}
|
||||
|
||||
static void
|
||||
gitg_diff_line_renderer_change_buffer (GtkSourceGutterRenderer *renderer,
|
||||
GtkTextBuffer *old_buffer)
|
||||
{
|
||||
GitgDiffLineRenderer *lr;
|
||||
GtkTextView *view;
|
||||
|
||||
lr = GITG_DIFF_LINE_RENDERER (renderer);
|
||||
|
||||
if (old_buffer)
|
||||
{
|
||||
g_signal_handler_disconnect (old_buffer,
|
||||
lr->priv->changed_handler_id);
|
||||
}
|
||||
|
||||
view = gtk_source_gutter_renderer_get_view (renderer);
|
||||
|
||||
if (view)
|
||||
{
|
||||
GtkTextBuffer *buffer;
|
||||
|
||||
buffer = gtk_text_view_get_buffer (view);
|
||||
|
||||
if (buffer)
|
||||
{
|
||||
lr->priv->changed_handler_id =
|
||||
g_signal_connect (buffer,
|
||||
"changed",
|
||||
G_CALLBACK (on_buffer_changed),
|
||||
lr);
|
||||
|
||||
recalculate_size (lr);
|
||||
}
|
||||
renderer->priv->num_digits = num_digits;
|
||||
recalculate_size (renderer);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -503,7 +466,6 @@ gitg_diff_line_renderer_class_init (GitgDiffLineRendererClass *klass)
|
|||
renderer_class->begin = gitg_diff_line_renderer_begin;
|
||||
renderer_class->draw = gitg_diff_line_renderer_draw;
|
||||
renderer_class->end= gitg_diff_line_renderer_end;
|
||||
renderer_class->change_buffer = gitg_diff_line_renderer_change_buffer;
|
||||
|
||||
object_class->set_property = gitg_diff_line_renderer_set_property;
|
||||
object_class->get_property = gitg_diff_line_renderer_get_property;
|
||||
|
@ -551,3 +513,12 @@ gitg_diff_line_renderer_new ()
|
|||
{
|
||||
return g_object_new (GITG_TYPE_DIFF_LINE_RENDERER, NULL);
|
||||
}
|
||||
|
||||
void
|
||||
gitg_diff_line_renderer_set_max_line_count (GitgDiffLineRenderer *renderer,
|
||||
guint max_line_count)
|
||||
{
|
||||
g_return_if_fail (GITG_IS_DIFF_LINE_RENDERER (renderer));
|
||||
|
||||
update_num_digits (renderer, max_line_count);
|
||||
}
|
||||
|
|
|
@ -52,6 +52,8 @@ struct _GitgDiffLineRendererClass {
|
|||
GType gitg_diff_line_renderer_get_type (void) G_GNUC_CONST;
|
||||
GitgDiffLineRenderer *gitg_diff_line_renderer_new (void);
|
||||
|
||||
void gitg_diff_line_renderer_set_max_line_count (GitgDiffLineRenderer *renderer,
|
||||
guint max_line_count);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
|
|
|
@ -183,6 +183,19 @@ region_free (Region *region,
|
|||
}
|
||||
}
|
||||
|
||||
static void
|
||||
set_max_line_count (GitgDiffView *view,
|
||||
guint max_line_count)
|
||||
{
|
||||
view->priv->max_line_count = max_line_count;
|
||||
|
||||
if (view->priv->line_renderer)
|
||||
{
|
||||
gitg_diff_line_renderer_set_max_line_count (view->priv->line_renderer,
|
||||
max_line_count);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
regions_free (GitgDiffView *view)
|
||||
{
|
||||
|
@ -194,7 +207,8 @@ regions_free (GitgDiffView *view)
|
|||
view->priv->regions = NULL;
|
||||
view->priv->last_region = NULL;
|
||||
view->priv->last_scan_line = 0;
|
||||
view->priv->max_line_count = 99;
|
||||
|
||||
set_max_line_count (view, 99);
|
||||
}
|
||||
|
||||
static void
|
||||
|
@ -499,7 +513,7 @@ ensure_max_line (GitgDiffView *view, Hunk *hunk)
|
|||
|
||||
if (m > view->priv->max_line_count)
|
||||
{
|
||||
view->priv->max_line_count = m;
|
||||
set_max_line_count (view, m);
|
||||
|
||||
gtk_source_gutter_queue_draw (gtk_source_view_get_gutter (GTK_SOURCE_VIEW (view), GTK_TEXT_WINDOW_LEFT));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue