Merge lp:~brandontschaefer/overlay-scrollbar/hidpi-support into lp:overlay-scrollbar

Proposed by Brandon Schaefer
Status: Approved
Approved by: Christopher Townsend
Approved revision: 388
Proposed branch: lp:~brandontschaefer/overlay-scrollbar/hidpi-support
Merge into: lp:overlay-scrollbar
Diff against target: 503 lines (+294/-33)
6 files modified
os/Makefile.am (+2/-0)
os/em-converter.c (+153/-0)
os/em-converter.h (+43/-0)
os/os-private.h (+14/-10)
os/os-scrollbar.c (+51/-21)
os/os-thumb.c (+31/-2)
To merge this branch: bzr merge lp:~brandontschaefer/overlay-scrollbar/hidpi-support
Reviewer Review Type Date Requested Status
Christopher Townsend (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+214283@code.launchpad.net

Commit message

Scale up the overlay-scrollbars correctly when the DPI changes. Does not work very nice when the gtk-scaling-factor is changed.

Description of the change

Scale up the overlay-scrollbars correctly when the DPI changes. Does not work very nice when the gtk-scaling-factor is changed.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Christopher Townsend (townsend) wrote :

Seems to work fine for < 2. That's good enough for now.

review: Approve
Revision history for this message
Christopher Townsend (townsend) wrote :

Ok, update looks fine as well.

review: Approve
389. By Brandon Schaefer

* Fix problem if a newly mapped monitor is plugged in and left
  to the default scale value.

Unmerged revisions

389. By Brandon Schaefer

* Fix problem if a newly mapped monitor is plugged in and left
  to the default scale value.

388. By Brandon Schaefer

* Increase the prox check by the DPI!

387. By Brandon Schaefer

* Move the setting the device scale code, becuase if we are not on gtk3 we crash!
* Fix the grip not being drawn when divison of the w/h changed the w/h to be off by 1

386. By Brandon Schaefer

* Dynamically update the scrollbar gtk allocation if we change
  monitors. (Only when are attempting to use the scrollbar do we check)

385. By Brandon Schaefer

* Set the monitor when it changes. Need to find a good way to tell gtk to re-do
  gtk allocation.

384. By Brandon Schaefer

* Add HiDPI Support for the overlay scrollbars

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'os/Makefile.am'
--- os/Makefile.am 2012-04-26 14:44:19 +0000
+++ os/Makefile.am 2014-05-09 17:06:57 +0000
@@ -5,10 +5,12 @@
5endif5endif
66
7source_h = \7source_h = \
8 $(srcdir)/em-converter.h \
8 $(srcdir)/os-private.h \9 $(srcdir)/os-private.h \
9 $(srcdir)/os-scrollbar.h10 $(srcdir)/os-scrollbar.h
1011
11source_c = \12source_c = \
13 $(srcdir)/em-converter.c \
12 $(srcdir)/os-animation.c \14 $(srcdir)/os-animation.c \
13 $(srcdir)/os-bar.c \15 $(srcdir)/os-bar.c \
14 $(srcdir)/os-log.c \16 $(srcdir)/os-log.c \
1517
=== added file 'os/em-converter.c'
--- os/em-converter.c 1970-01-01 00:00:00 +0000
+++ os/em-converter.c 2014-05-09 17:06:57 +0000
@@ -0,0 +1,153 @@
1/* overlay-scrollbar
2 *
3 * Copyright © 2014 Canonical Ltd
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
18 *
19 * Authored by Brandon Schaefer <brandon.schaefer@canonical.com>
20 */
21
22#include "em-converter.h"
23
24#include <stdio.h>
25#include <stdlib.h>
26
27#define BASE_DPI 96.0
28#define PIXELS_PER_INCH 72.0
29#define CONVERT_TO_SCALE 8.0
30
31#define SCALE_FATOR "scale-factor"
32#define UNITY_GSETTINGS_SCHEMA "com.ubuntu.user-interface"
33
34static void
35update_dpi_value(EMConverter* converter, double scale_value)
36{
37 if (!converter)
38 return;
39
40 converter->dpi = BASE_DPI * scale_value;
41}
42
43static void
44parse_font_scale_factor (EMConverter* converter)
45{
46 float value;
47 int raw_value;
48 GVariant* dict;
49 GdkScreen *screen;
50 gchar* monitor_name;
51
52 g_settings_get(converter->unity_settings, SCALE_FATOR, "@a{si}", &dict);
53
54 screen = gtk_widget_get_screen (converter->parent);
55
56
57 monitor_name = gdk_screen_get_monitor_plug_name (screen, converter->monitor);
58 if (g_variant_lookup(dict, monitor_name, "i", &raw_value))
59 value = raw_value / CONVERT_TO_SCALE;
60 else
61 value = BASE_DPI;
62
63 g_free (monitor_name);
64
65 update_dpi_value(converter, value);
66}
67
68gboolean
69set_converter_monitor (EMConverter* converter, int monitor)
70{
71 if (converter->monitor != monitor)
72 {
73 converter->monitor = monitor;
74 parse_font_scale_factor(converter);
75
76 return TRUE;
77 }
78
79 return FALSE;
80}
81
82static void
83font_scale_changed_callback (GSettings* settings, gchar *key, gpointer data)
84{
85 EMConverter* converter = (EMConverter*)data;
86
87 parse_font_scale_factor(converter);
88}
89
90static void
91get_unity_settings (EMConverter* converter)
92{
93 converter->unity_settings = g_settings_new(UNITY_GSETTINGS_SCHEMA);
94
95 g_signal_connect (converter->unity_settings, "changed::"SCALE_FATOR,
96 G_CALLBACK (font_scale_changed_callback), converter);
97}
98
99
100EMConverter*
101new_converter (GtkWidget *parent)
102{
103 EMConverter* converter = (EMConverter*)malloc(sizeof(EMConverter));
104
105 converter->monitor = 0;
106 converter->dpi = BASE_DPI;
107 converter->unity_settings = NULL;
108 converter->parent = parent;
109
110 get_unity_settings(converter);
111 parse_font_scale_factor(converter);
112
113 return converter;
114}
115
116void
117cleanup_converter (EMConverter* converter)
118{
119 if (converter)
120 {
121 if (converter->unity_settings != NULL)
122 {
123 g_object_unref(converter->unity_settings);
124 converter->unity_settings = NULL;
125 }
126
127 converter = NULL;
128 }
129}
130
131double
132convert_pixels (EMConverter* converter, double pixel)
133{
134 if (!converter || BASE_DPI == converter->dpi)
135 return pixel;
136
137 double base_ppe = BASE_DPI / PIXELS_PER_INCH;
138 double pixels_em = pixel / base_ppe;
139
140 double current_ppe = converter->dpi / PIXELS_PER_INCH;
141 double new_pixels = pixels_em * current_ppe;
142
143 return new_pixels;
144}
145
146double
147dpi_scale (EMConverter* converter)
148{
149 if (!converter)
150 return 1.0;
151
152 return converter->dpi / BASE_DPI;
153}
0154
=== added file 'os/em-converter.h'
--- os/em-converter.h 1970-01-01 00:00:00 +0000
+++ os/em-converter.h 2014-05-09 17:06:57 +0000
@@ -0,0 +1,43 @@
1/* overlay-scrollbar
2 *
3 * Copyright © 2014 Canonical Ltd
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
18 *
19 * Authored by Brandon Schaefer <brandon.schaefer@canonical.com>
20 */
21
22#ifndef EM_CONVERTER_H
23#define EM_CONVERTER_H
24
25#include <gtk/gtk.h>
26
27typedef struct {
28 int monitor;
29 int dpi;
30 GSettings *unity_settings;
31 GtkWidget *parent;
32
33} EMConverter;
34
35extern double convert_pixels (EMConverter* converter, double pixel);
36extern double dpi_scale (EMConverter* converter);
37extern gboolean set_converter_monitor (EMConverter* converter, int monitor);
38
39extern EMConverter* new_converter (GtkWidget *parent);
40extern void cleanup_converter (EMConverter* converter);
41
42
43#endif /* EM_CONVERTER_H */
044
=== modified file 'os/os-private.h'
--- os/os-private.h 2012-11-26 14:35:24 +0000
+++ os/os-private.h 2014-05-09 17:06:57 +0000
@@ -23,6 +23,7 @@
23#define __OS_PRIVATE_H__23#define __OS_PRIVATE_H__
2424
25#include <gtk/gtk.h>25#include <gtk/gtk.h>
26#include "em-converter.h"
2627
27/* Tell GCC not to export internal functions. */28/* Tell GCC not to export internal functions. */
28#ifdef __GNUC__29#ifdef __GNUC__
@@ -235,16 +236,19 @@
235 GtkWindowClass parent_class;236 GtkWindowClass parent_class;
236};237};
237238
238GType os_thumb_get_type (void) G_GNUC_CONST;239GType os_thumb_get_type (void) G_GNUC_CONST;
239240
240GtkWidget* os_thumb_new (GtkOrientation orientation);241GtkWidget* os_thumb_new (GtkOrientation orientation);
241242
242void os_thumb_resize (OsThumb *thumb,243void os_thumb_resize (OsThumb *thumb,
243 gint width,244 gint width,
244 gint height);245 gint height);
245246
246void os_thumb_set_detached (OsThumb *thumb,247void os_thumb_set_detached (OsThumb *thumb,
247 gboolean detached);248 gboolean detached);
249
250void os_thumb_set_converter (OsThumb *thumb,
251 EMConverter *converter);
248252
249G_END_DECLS253G_END_DECLS
250254
251255
=== modified file 'os/os-scrollbar.c'
--- os/os-scrollbar.c 2013-12-12 10:17:22 +0000
+++ os/os-scrollbar.c 2014-05-09 17:06:57 +0000
@@ -116,6 +116,7 @@
116 GtkWindowGroup *window_group;116 GtkWindowGroup *window_group;
117 OsAnimation *animation;117 OsAnimation *animation;
118 OsBar *bar;118 OsBar *bar;
119 EMConverter *converter;
119 OsCoordinate pointer;120 OsCoordinate pointer;
120 OsCoordinate thumb_win;121 OsCoordinate thumb_win;
121 OsEventFlags event;122 OsEventFlags event;
@@ -676,6 +677,7 @@
676 qdata->fine_scroll_multiplier = 1.0;677 qdata->fine_scroll_multiplier = 1.0;
677 qdata->bar = os_bar_new ();678 qdata->bar = os_bar_new ();
678 qdata->window_group = gtk_window_group_new ();679 qdata->window_group = gtk_window_group_new ();
680 qdata->converter = new_converter (widget);
679 qdata->animation = os_animation_new (RATE_ANIMATION, MAX_DURATION_SCROLLING,681 qdata->animation = os_animation_new (RATE_ANIMATION, MAX_DURATION_SCROLLING,
680 scrolling_cb, scrolling_end_cb, widget);682 scrolling_cb, scrolling_end_cb, widget);
681683
@@ -823,6 +825,15 @@
823 priv->state &= ~(OS_STATE_RECONNECTING);825 priv->state &= ~(OS_STATE_RECONNECTING);
824}826}
825827
828static void
829update_scrollbar_size_allocation(GtkScrollbar *scrollbar)
830{
831 /* Hack to update the size allocation if the monitor changes */
832 GtkAllocation allocation;
833 gtk_widget_get_allocation ((GTK_WIDGET (scrollbar)), &allocation);
834 hijacked_scrollbar_size_allocate (GTK_WIDGET(scrollbar), &allocation);
835}
836
826/* Sanitize x coordinate of thumb window. */837/* Sanitize x coordinate of thumb window. */
827static gint838static gint
828sanitize_x (GtkScrollbar *scrollbar,839sanitize_x (GtkScrollbar *scrollbar,
@@ -845,6 +856,10 @@
845856
846 screen = gtk_widget_get_screen (GTK_WIDGET (scrollbar));857 screen = gtk_widget_get_screen (GTK_WIDGET (scrollbar));
847 n_monitor = gdk_screen_get_monitor_at_point (screen, monitor_x, y);858 n_monitor = gdk_screen_get_monitor_at_point (screen, monitor_x, y);
859
860 if (set_converter_monitor(priv->converter, n_monitor))
861 update_scrollbar_size_allocation(scrollbar);
862
848#ifdef USE_GTK3863#ifdef USE_GTK3
849 gdk_screen_get_monitor_geometry (screen, n_monitor, &rect);864 gdk_screen_get_monitor_geometry (screen, n_monitor, &rect);
850#else865#else
@@ -988,6 +1003,10 @@
9881003
989 screen = gtk_widget_get_screen (GTK_WIDGET (scrollbar));1004 screen = gtk_widget_get_screen (GTK_WIDGET (scrollbar));
990 n_monitor = gdk_screen_get_monitor_at_point (screen, x, monitor_y);1005 n_monitor = gdk_screen_get_monitor_at_point (screen, x, monitor_y);
1006
1007 if (set_converter_monitor(priv->converter, n_monitor))
1008 update_scrollbar_size_allocation(scrollbar);
1009
991#ifdef USE_GTK31010#ifdef USE_GTK3
992 gdk_screen_get_monitor_geometry (screen, n_monitor, &rect);1011 gdk_screen_get_monitor_geometry (screen, n_monitor, &rect);
993#else1012#else
@@ -1259,6 +1278,8 @@
1259 G_CALLBACK (thumb_scroll_event_cb), scrollbar);1278 G_CALLBACK (thumb_scroll_event_cb), scrollbar);
1260 g_signal_connect (G_OBJECT (priv->thumb), "unmap",1279 g_signal_connect (G_OBJECT (priv->thumb), "unmap",
1261 G_CALLBACK (thumb_unmap_cb), scrollbar);1280 G_CALLBACK (thumb_unmap_cb), scrollbar);
1281
1282 os_thumb_set_converter (OS_THUMB(priv->thumb), priv->converter);
1262 }1283 }
1263}1284}
12641285
@@ -2820,7 +2841,7 @@
28202841
2821 priv = get_private (GTK_WIDGET (scrollbar));2842 priv = get_private (GTK_WIDGET (scrollbar));
28222843
2823 proximity_size = PROXIMITY_SIZE;2844 proximity_size = convert_pixels (priv->converter, PROXIMITY_SIZE);
28242845
2825 /* If the thumb is internal, enlarge the proximity area. */2846 /* If the thumb is internal, enlarge the proximity area. */
2826 if (priv->state & OS_STATE_INTERNAL)2847 if (priv->state & OS_STATE_INTERNAL)
@@ -3325,6 +3346,11 @@
3325 priv->animation = NULL;3346 priv->animation = NULL;
3326 }3347 }
33273348
3349 if (priv->converter != NULL)
3350 {
3351 cleanup_converter (priv->converter);
3352 }
3353
3328 if (priv->bar != NULL)3354 if (priv->bar != NULL)
3329 {3355 {
3330 g_object_unref (priv->bar);3356 g_object_unref (priv->bar);
@@ -3383,8 +3409,8 @@
3383 *minimal_width = *natural_width = 0;3409 *minimal_width = *natural_width = 0;
3384 else3410 else
3385 {3411 {
3386 *minimal_width = MIN_THUMB_HEIGHT;3412 *minimal_width = convert_pixels(priv->converter, MIN_THUMB_HEIGHT);
3387 *natural_width = THUMB_HEIGHT;3413 *natural_width = convert_pixels(priv->converter, THUMB_HEIGHT);
3388 }3414 }
33893415
3390 return;3416 return;
@@ -3408,8 +3434,8 @@
3408 *minimal_height = *natural_height = 0;3434 *minimal_height = *natural_height = 0;
3409 else3435 else
3410 {3436 {
3411 *minimal_height = MIN_THUMB_HEIGHT;3437 *minimal_height = convert_pixels(priv->converter, MIN_THUMB_HEIGHT);
3412 *natural_height = THUMB_HEIGHT;3438 *natural_height = convert_pixels(priv->converter, THUMB_HEIGHT);
3413 }3439 }
34143440
3415 return;3441 return;
@@ -3720,6 +3746,10 @@
3720 scrollbar = GTK_SCROLLBAR (widget);3746 scrollbar = GTK_SCROLLBAR (widget);
3721 priv = get_private (widget);3747 priv = get_private (widget);
37223748
3749 int thumb_width = convert_pixels(priv->converter, THUMB_WIDTH);
3750 int thumb_height = convert_pixels(priv->converter, THUMB_HEIGHT);
3751 int bar_size = convert_pixels(priv->converter, BAR_SIZE);
3752
3723 /* Get the side, then move thumb and bar accordingly. */3753 /* Get the side, then move thumb and bar accordingly. */
3724 retrieve_side (scrollbar);3754 retrieve_side (scrollbar);
37253755
@@ -3733,19 +3763,19 @@
37333763
3734 if (priv->orientation == GTK_ORIENTATION_VERTICAL)3764 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
3735 {3765 {
3736 priv->slider.width = THUMB_WIDTH;3766 priv->slider.width = thumb_width;
3737 if (priv->slider.height != MIN (THUMB_HEIGHT, allocation->height))3767 if (priv->slider.height != MIN (thumb_height, allocation->height))
3738 {3768 {
3739 priv->slider.height = MIN (THUMB_HEIGHT, allocation->height);3769 priv->slider.height = MIN (thumb_height, allocation->height);
3740 os_thumb_resize (OS_THUMB (priv->thumb), priv->slider.width, priv->slider.height);3770 os_thumb_resize (OS_THUMB (priv->thumb), priv->slider.width, priv->slider.height);
3741 }3771 }
37423772
3743 if (priv->side == OS_SIDE_RIGHT)3773 if (priv->side == OS_SIDE_RIGHT)
3744 priv->bar_all.x = allocation->x - BAR_SIZE;3774 priv->bar_all.x = allocation->x - bar_size;
37453775
3746 priv->bar_all.width = BAR_SIZE;3776 priv->bar_all.width = bar_size;
37473777
3748 priv->thumb_all.width = THUMB_WIDTH;3778 priv->thumb_all.width = thumb_width;
37493779
3750 if (priv->side == OS_SIDE_RIGHT)3780 if (priv->side == OS_SIDE_RIGHT)
3751 priv->thumb_all.x = allocation->x - priv->bar_all.width;3781 priv->thumb_all.x = allocation->x - priv->bar_all.width;
@@ -3756,19 +3786,19 @@
3756 }3786 }
3757 else3787 else
3758 {3788 {
3759 priv->slider.height = THUMB_WIDTH;3789 priv->slider.height = thumb_width;
3760 if (priv->slider.width != MIN (THUMB_HEIGHT, allocation->width))3790 if (priv->slider.width != MIN (thumb_height, allocation->width))
3761 {3791 {
3762 priv->slider.width = MIN (THUMB_HEIGHT, allocation->width);3792 priv->slider.width = MIN (thumb_height, allocation->width);
3763 os_thumb_resize (OS_THUMB (priv->thumb), priv->slider.width, priv->slider.height);3793 os_thumb_resize (OS_THUMB (priv->thumb), priv->slider.width, priv->slider.height);
3764 }3794 }
37653795
3766 if (priv->side == OS_SIDE_BOTTOM)3796 if (priv->side == OS_SIDE_BOTTOM)
3767 priv->bar_all.y = allocation->y - BAR_SIZE;3797 priv->bar_all.y = allocation->y - bar_size;
37683798
3769 priv->bar_all.height = BAR_SIZE;3799 priv->bar_all.height = bar_size;
37703800
3771 priv->thumb_all.height = THUMB_WIDTH;3801 priv->thumb_all.height = thumb_width;
37723802
3773 if (priv->side == OS_SIDE_BOTTOM)3803 if (priv->side == OS_SIDE_BOTTOM)
3774 priv->thumb_all.y = allocation->y - priv->bar_all.height;3804 priv->thumb_all.y = allocation->y - priv->bar_all.height;
37753805
=== modified file 'os/os-thumb.c'
--- os/os-thumb.c 2013-02-02 00:46:51 +0000
+++ os/os-thumb.c 2014-05-09 17:06:57 +0000
@@ -53,6 +53,7 @@
53 GtkOrientation orientation;53 GtkOrientation orientation;
54 GtkWidget *grabbed_widget;54 GtkWidget *grabbed_widget;
55 OsAnimation *animation;55 OsAnimation *animation;
56 EMConverter *converter;
56 OsCoordinate pointer;57 OsCoordinate pointer;
57 OsCoordinate pointer_root;58 OsCoordinate pointer_root;
58 OsEventFlags event;59 OsEventFlags event;
@@ -679,6 +680,13 @@
679 cr = gdk_cairo_create (gtk_widget_get_window (widget));680 cr = gdk_cairo_create (gtk_widget_get_window (widget));
680#endif681#endif
681682
683 cairo_surface_t *surface = cairo_get_target(cr);
684 float scale = dpi_scale(priv->converter);
685 cairo_surface_set_device_scale(surface, scale, scale);
686
687 width /= scale;
688 height /= scale;
689
682 cairo_save (cr);690 cairo_save (cr);
683691
684 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);692 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
@@ -815,8 +823,8 @@
815 cairo_stroke (cr);823 cairo_stroke (cr);
816824
817 /* Only draw the grip when the thumb is at full height. */825 /* Only draw the grip when the thumb is at full height. */
818 if ((priv->orientation == GTK_ORIENTATION_VERTICAL && height == THUMB_HEIGHT - 1) ||826 if ((priv->orientation == GTK_ORIENTATION_VERTICAL && height < THUMB_HEIGHT) ||
819 (priv->orientation == GTK_ORIENTATION_HORIZONTAL && width == THUMB_HEIGHT - 1) )827 (priv->orientation == GTK_ORIENTATION_HORIZONTAL && width < THUMB_HEIGHT) )
820 {828 {
821 if (priv->orientation == GTK_ORIENTATION_VERTICAL)829 if (priv->orientation == GTK_ORIENTATION_VERTICAL)
822 pat = cairo_pattern_create_linear (0, 0, 0, height);830 pat = cairo_pattern_create_linear (0, 0, 0, height);
@@ -1268,3 +1276,24 @@
1268 gtk_widget_queue_draw (GTK_WIDGET (thumb));1276 gtk_widget_queue_draw (GTK_WIDGET (thumb));
1269 }1277 }
1270}1278}
1279
1280/**
1281* os_thumb_set_converter:
1282* @thumb: a #OsThumb
1283* @converter: an #EMConveter
1284*
1285* Sets the thumbs EMConverter, needed to scale DPI dynamically
1286**/
1287
1288void
1289os_thumb_set_converter (OsThumb* thumb,
1290 EMConverter* converter)
1291{
1292 OsThumbPrivate *priv;
1293
1294 g_return_if_fail (OS_IS_THUMB (thumb));
1295
1296 priv = thumb->priv;
1297
1298 priv->converter = converter;
1299}

Subscribers

People subscribed via source and target branches