summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Ward <patrick@endlessm.com>2014-01-17 11:13:01 -0800
committerPatrick Ward <patrick@endlessm.com>2014-01-29 10:27:44 -0800
commitb71ccdd0f0718fdea5da42108afcf194507a84fd (patch)
tree4222bdf940504d407fb05424586ccbe8e03f502a
parentb124b623944c574d2325589cd20198d2a79bb993 (diff)
Add publicly available font size property
Added a publicly available calculated font size property. This allows Endless applications to perform actions based on the calculated font size of an EosWindow. In particular, the WebHelper application now uses the property to update the 'default-font-size' property of a WebSettings object. [endlessm/eos-sdk#484]
-rw-r--r--docs/reference/endless/endless-sections.txt1
-rw-r--r--endless/eoswindow.c53
-rw-r--r--endless/eoswindow.h37
-rw-r--r--test/Makefile.am.inc1
-rw-r--r--test/webhelper/testUpdateFontSize.js101
-rw-r--r--webhelper/webhelper.js23
6 files changed, 193 insertions, 23 deletions
diff --git a/docs/reference/endless/endless-sections.txt b/docs/reference/endless/endless-sections.txt
index 963629f..9715286 100644
--- a/docs/reference/endless/endless-sections.txt
+++ b/docs/reference/endless/endless-sections.txt
@@ -40,6 +40,7 @@ eos_window_get_font_scaling_default_window_size
eos_window_set_font_scaling_default_window_size
eos_window_get_font_scaling_min_font_size
eos_window_set_font_scaling_min_font_size
+eos_window_get_font_scaling_calculated_font_size
<SUBSECTION Standard>
EosWindowClass
EOS_IS_WINDOW
diff --git a/endless/eoswindow.c b/endless/eoswindow.c
index bbe2422..7b89312 100644
--- a/endless/eoswindow.c
+++ b/endless/eoswindow.c
@@ -49,11 +49,15 @@
* The default minimum font size under which a font will never scale can be set
* by #EosWindow:font-scaling-min-font-size.
*
+ * The calculated minimum font size by which children widgets will scale and can
+ * be retrieved by #EosWindow:font-scaling-calculated-font-size. This property is
+ * only readable and is only set by #EosWindow internally.
+ *
* For instance, supose we have a default font size of 12px, a default window size
- * of 720px, and a window allocation of 360px. The calculated base pixel size
+ * of 720px, and a window allocation of 360px. The calculated font pixel size
* will be 12px * (360px / 720px) = 6px. A corresponding CSS font-size of 1em will
* be equivalent to 6 px. A CSS font-size of 0.5em will be equivalent to 3px. If the
- * window is resized to a height of 720px, then the calculated base pixel size will
+ * window is resized to a height of 720px, then the calculated pixel size will
* be 12px, and the CSS font-size of 1em will be equivalent to 12px. A CSS
* font-size of 0.5em will be equivalent to 6px. If the minimum font size is set
* to 12px, then the font-size will be forced to 12px, ignoring the calculated font
@@ -103,6 +107,7 @@ typedef struct {
gint font_scaling_default_size;
gint font_scaling_default_window_size;
gint font_scaling_min_font_size;
+ gdouble font_scaling_calculated_font_size;
/* For keeping track of what to display alongside the current page */
GtkWidget *current_page;
@@ -122,6 +127,7 @@ enum
PROP_FONT_SCALING_DEFAULT_SIZE,
PROP_FONT_SCALING_DEFAULT_WINDOW_SIZE,
PROP_FONT_SCALING_MIN_FONT_SIZE,
+ PROP_FONT_SCALING_CALCULATED_FONT_SIZE,
NPROPS
};
@@ -429,6 +435,10 @@ eos_window_get_property (GObject *object,
g_value_set_int (value, priv->font_scaling_min_font_size);
break;
+ case PROP_FONT_SCALING_CALCULATED_FONT_SIZE:
+ g_value_set_double (value, priv->font_scaling_calculated_font_size);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}
@@ -581,15 +591,15 @@ eos_window_size_allocate (GtkWidget *window, GtkAllocation *allocation)
if (priv->font_scaling_active)
{
GtkStyleProvider *provider = GTK_STYLE_PROVIDER (priv->font_size_provider);
- gdouble base_pixel_size = (gdouble) priv->font_scaling_default_size *
+ priv->font_scaling_calculated_font_size = (gdouble) priv->font_scaling_default_size *
((gdouble) allocation->height / (gdouble) priv->font_scaling_default_window_size);
- if (base_pixel_size < priv->font_scaling_min_font_size)
- base_pixel_size = priv->font_scaling_min_font_size;
+ if (priv->font_scaling_calculated_font_size < priv->font_scaling_min_font_size)
+ priv->font_scaling_calculated_font_size = priv->font_scaling_min_font_size;
GError *error = NULL;
- gchar *font_size_css = g_strdup_printf (FONT_SIZE_TEMPLATE, base_pixel_size);
+ gchar *font_size_css = g_strdup_printf (FONT_SIZE_TEMPLATE, priv->font_scaling_calculated_font_size);
GdkScreen *screen = gdk_screen_get_default ();
gtk_style_context_remove_provider_for_screen (screen, provider);
@@ -700,6 +710,19 @@ eos_window_class_init (EosWindowClass *klass)
8,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
+ /**
+ * EosWindow:font-scaling-calculated-font-size:
+ *
+ * The calculated font-size by which children widgets scale. Units are in pixels.
+ */
+ eos_window_props[PROP_FONT_SCALING_CALCULATED_FONT_SIZE] =
+ g_param_spec_double ("font-scaling-calculated-font-size", "Font scaling calculated size",
+ "This is the calculated font-size by which children widgets scale",
+ 1,
+ G_MAXDOUBLE,
+ 12,
+ G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
+
g_object_class_install_properties (object_class, NPROPS, eos_window_props);
}
@@ -1057,3 +1080,21 @@ eos_window_set_font_scaling_min_font_size (EosWindow *self,
EosWindowPrivate *priv = eos_window_get_instance_private (self);
priv->font_scaling_min_font_size = new_min_font_size;
}
+
+/**
+ * eos_window_get_font_scaling_calculated_font_size:
+ * @self: the window
+ *
+ * See #EosWindow:font-scaling-calculated-font-size for details.
+ *
+ * Returns: the calculated font size by which the font size of children widgets
+ * will scale.
+ */
+gdouble
+eos_window_get_font_scaling_calculated_font_size (EosWindow *self)
+{
+ g_return_val_if_fail (self != NULL && EOS_IS_WINDOW (self), -1);
+ EosWindowPrivate *priv = eos_window_get_instance_private (self);
+
+ return priv->font_scaling_calculated_font_size;
+}
diff --git a/endless/eoswindow.h b/endless/eoswindow.h
index 317f0c5..af39160 100644
--- a/endless/eoswindow.h
+++ b/endless/eoswindow.h
@@ -59,45 +59,48 @@ struct _EosWindowClass
};
EOS_SDK_ALL_API_VERSIONS
-GType eos_window_get_type (void) G_GNUC_CONST;
+GType eos_window_get_type (void) G_GNUC_CONST;
EOS_SDK_ALL_API_VERSIONS
-GtkWidget *eos_window_new (EosApplication *application);
+GtkWidget *eos_window_new (EosApplication *application);
EOS_SDK_ALL_API_VERSIONS
-EosPageManager *eos_window_get_page_manager (EosWindow *self);
+EosPageManager *eos_window_get_page_manager (EosWindow *self);
EOS_SDK_ALL_API_VERSIONS
-void eos_window_set_page_manager (EosWindow *self,
- EosPageManager *page_manager);
+void eos_window_set_page_manager (EosWindow *self,
+ EosPageManager *page_manager);
EOS_SDK_ALL_API_VERSIONS
-gboolean eos_window_get_font_scaling_active (EosWindow *self);
+gboolean eos_window_get_font_scaling_active (EosWindow *self);
EOS_SDK_ALL_API_VERSIONS
-void eos_window_set_font_scaling_active (EosWindow *self,
- gboolean is_scaling);
+void eos_window_set_font_scaling_active (EosWindow *self,
+ gboolean is_scaling);
EOS_SDK_ALL_API_VERSIONS
-gint eos_window_get_font_scaling_default_size (EosWindow *self);
+gint eos_window_get_font_scaling_default_size (EosWindow *self);
EOS_SDK_ALL_API_VERSIONS
-void eos_window_set_font_scaling_default_size (EosWindow *self,
- gint new_default_font_size);
+void eos_window_set_font_scaling_default_size (EosWindow *self,
+ gint new_default_font_size);
EOS_SDK_ALL_API_VERSIONS
-gint eos_window_get_font_scaling_default_window_size (EosWindow *self);
+gint eos_window_get_font_scaling_default_window_size (EosWindow *self);
EOS_SDK_ALL_API_VERSIONS
-void eos_window_set_font_scaling_default_window_size (EosWindow *self,
- gint new_default_window_size);
+void eos_window_set_font_scaling_default_window_size (EosWindow *self,
+ gint new_default_window_size);
EOS_SDK_ALL_API_VERSIONS
-gint eos_window_get_font_scaling_min_font_size (EosWindow *self);
+gint eos_window_get_font_scaling_min_font_size (EosWindow *self);
EOS_SDK_ALL_API_VERSIONS
-void eos_window_set_font_scaling_min_font_size (EosWindow *self,
- gint new_min_font_size);
+void eos_window_set_font_scaling_min_font_size (EosWindow *self,
+ gint new_min_font_size);
+
+EOS_SDK_ALL_API_VERSIONS
+gdouble eos_window_get_font_scaling_calculated_font_size (EosWindow *self);
G_END_DECLS
diff --git a/test/Makefile.am.inc b/test/Makefile.am.inc
index 74f6fe6..5a95942 100644
--- a/test/Makefile.am.inc
+++ b/test/Makefile.am.inc
@@ -14,6 +14,7 @@ javascript_tests = \
test/tools/eos-application-manifest/testInit.js \
test/webhelper/testTranslate.js \
test/webhelper/testWebActions.js \
+ test/webhelper/testUpdateFontSize.js \
test/wikipedia/models/testCategoryModel.js \
test/wikipedia/models/testArticleModel.js \
test/wikipedia/models/testDomainWikiModel.js \
diff --git a/test/webhelper/testUpdateFontSize.js b/test/webhelper/testUpdateFontSize.js
new file mode 100644
index 0000000..4aaaefb
--- /dev/null
+++ b/test/webhelper/testUpdateFontSize.js
@@ -0,0 +1,101 @@
+const Endless = imports.gi.Endless;
+const Gio = imports.gi.Gio;
+const GLib = imports.gi.GLib;
+const Gtk = imports.gi.Gtk;
+const Lang = imports.lang;
+const WebHelper = imports.webhelper;
+const WebKit = imports.gi.WebKit;
+
+const WebUpdateFontSizeTestApplication = new Lang.Class({
+ Name: 'WebUpdateFontSizeTestApplication',
+ Extends: WebHelper.Application,
+
+ vfunc_startup: function () {
+ this.parent();
+
+ this.webview = new WebKit.WebView();
+ this.websettings = this.webview.get_settings();
+ this.webview.set_settings(this.websettings);
+ this.win = new Endless.Window({
+ application: this,
+ font_scaling_active: true
+ });
+
+ /* Typically logic in tests is bad. In this case, we need to inject these
+ * callbacks to test specific cases that only occur during the runtime main
+ * loop of the test application. */
+ if (this.set_font_resizable_callback !== null) {
+ this.set_font_resizable_callback(this.win, this.websettings);
+ this.websettings.connect('notify::default-font-size', Lang.bind(this, function () {
+ this.default_font_size_changed_callback(this.win, this.websettings);
+ }));
+ }
+
+ if (this.accept_callback !== null)
+ this.accept_callback(this.win, this.websettings.default_font_size);
+
+ // Add an upper bound on how long the app runs, in case app.quit() does
+ // not get called
+ GLib.timeout_add_seconds(GLib.PRIORITY_HIGH, 5, Lang.bind(this, function () {
+ this.quit();
+ }));
+ },
+
+ accept_callback: null,
+
+ set_font_resizable_callback: null,
+
+ default_font_size_changed_callback: null
+});
+
+// TODO: These tests depend on a running X Server and Window Manager. That means
+// that they are not runnable in a continuous-integration server
+describe("Web Helper Font Sizes", function () {
+ let app;
+
+ beforeEach(function () {
+ // Generate a unique ID for each app instance that we test
+ let fake_pid = GLib.random_int();
+ // FIXME In this version of GJS there is no Posix module, so fake the PID
+ let id_string = 'com.endlessm.webhelper.test' + GLib.get_real_time() + fake_pid;
+
+ app = new WebUpdateFontSizeTestApplication({
+ application_id: id_string
+ });
+ });
+
+ it("does not have the default font scale for a tiny window", function () {
+ let test_initial_font_size = function (my_win, default_font_size) {
+ my_win.font_scaling_default_window_size = 200; // Force a ridiculous value
+ expect(my_win.font_scaling_calculated_font_size).not.toEqual(default_font_size);
+
+ /* This does not immediately quit the application. It sets a flag for termination
+ * which will cause the application to be disposed on the next main loop iteration. */
+ app.quit();
+ };
+
+ app.accept_callback = test_initial_font_size;
+
+ spyOn(app, 'accept_callback').and.callThrough();
+ app.run([]);
+ expect(app.accept_callback).toHaveBeenCalled();
+ });
+
+ it("takes the provided settings default font size on setting resizable", function () {
+ app.set_font_resizable_callback = app.set_web_settings_font_resizable;
+
+ let test_font_sizing = function (my_win, my_websettings) {
+ expect(my_win.font_scaling_calculated_font_size).toEqual(my_websettings.default_font_size);
+
+ /* This does not immediately quit the application. It sets a flag for termination
+ * which will cause the application to be disposed on the next main loop iteration. */
+ app.quit();
+ };
+
+ app.default_font_size_changed_callback = test_font_sizing;
+
+ spyOn(app, 'default_font_size_changed_callback').and.callThrough();
+ app.run([]);
+ expect(app.default_font_size_changed_callback).toHaveBeenCalled();
+ });
+});
diff --git a/webhelper/webhelper.js b/webhelper/webhelper.js
index 32ad4ec..d4f6811 100644
--- a/webhelper/webhelper.js
+++ b/webhelper/webhelper.js
@@ -263,5 +263,28 @@ const Application = new Lang.Class({
"your app?");
element.inner_html = this._translationFunction(element.inner_text);
}
+ },
+
+ /**
+ * Method: set_web_settings_font_resizable
+ * Set an eos_window to update font size of web_settings
+ *
+ * Parameters:
+ * eos_windw - an <Endless.Window>
+ * web_settings - a <WebKit.WebSettings>
+ *
+ * The <Endless.Window> will be connected on its "size-allocate" signal
+ * to the given <WebKit.WebSettings>. The <Endless.Window> will update the
+ * "default-font-size" property of the <WebKit.WebSettings> calculated font size
+ * to the <Endless.Window>'s calculated font size.
+ */
+ set_web_settings_font_resizable: function (eos_window, web_settings) {
+ eos_window.connect('size-allocate',
+ Lang.bind(this, function (widget, allocation) {
+ if (eos_window.font_scaling_active) {
+ web_settings.default_font_size = eos_window.font_scaling_calculated_font_size;
+ }
+ }
+ ));
}
});