[recipes] details: Replace serves with yields
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [recipes] details: Replace serves with yields
- Date: Mon, 26 Jun 2017 01:46:57 +0000 (UTC)
commit da74ffaddffea63e483db3d5e3f12402d86731ad
Author: Matthias Clasen <mclasen redhat com>
Date: Sun Jun 25 15:40:45 2017 -0400
details: Replace serves with yields
Show the yield instead of serves.
src/gr-details-page.c | 96 ++++++++++++++++++++++++++++++++++++------------
src/gr-details-page.ui | 66 +++++++++++++++++++++-----------
2 files changed, 115 insertions(+), 47 deletions(-)
---
diff --git a/src/gr-details-page.c b/src/gr-details-page.c
index 31d0b1f..cfae58b 100644
--- a/src/gr-details-page.c
+++ b/src/gr-details-page.c
@@ -70,7 +70,8 @@ struct _GrDetailsPage
GtkWidget *meal_label;
GtkWidget *season_desc;
GtkWidget *season_label;
- GtkWidget *serves_spin;
+ GtkWidget *yield_spin;
+ GtkWidget *yield_label;
GtkWidget *warning_box;
GtkWidget *spicy_warning;
GtkWidget *garlic_warning;
@@ -176,19 +177,62 @@ gr_details_page_contribute_recipe (GrDetailsPage *page)
gr_recipe_exporter_contribute (page->exporter, page->recipe);
}
-static void populate_ingredients (GrDetailsPage *page,
- int num,
- int denom);
+static void populate_ingredients (GrDetailsPage *page, double scale);
static void
-serves_value_changed (GrDetailsPage *page)
+update_yield_label (GrDetailsPage *page,
+ double yield)
{
- int serves;
- int new_value;
+ const char *yield_unit;
- new_value = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (page->serves_spin));
- serves = gr_recipe_get_serves (page->recipe);
- populate_ingredients (page, new_value, serves);
+ yield_unit = gr_recipe_get_yield_unit (page->recipe);
+ if (yield_unit && yield_unit[0])
+ gtk_label_set_label (GTK_LABEL (page->yield_label), yield_unit);
+ else
+ gtk_label_set_label (GTK_LABEL (page->yield_label),
+ yield == 1.0 ? _("serving") : _("servings"));
+}
+
+static void
+yield_spin_value_changed (GrDetailsPage *page)
+{
+ double yield;
+ double new_value;
+
+ new_value = gtk_spin_button_get_value (GTK_SPIN_BUTTON (page->yield_spin));
+ yield = gr_recipe_get_yield (page->recipe);
+
+ update_yield_label (page, new_value);
+ populate_ingredients (page, new_value / yield);
+}
+
+static int
+yield_spin_input (GtkSpinButton *spin,
+ double *new_val)
+{
+ char *text;
+
+ text = (char *)gtk_entry_get_text (GTK_ENTRY (spin));
+
+ if (!gr_number_parse (new_val, &text, NULL)) {
+ *new_val = 0.0;
+ return GTK_INPUT_ERROR;
+ }
+
+ return TRUE;
+}
+
+static int
+yield_spin_output (GtkSpinButton *spin)
+{
+ GtkAdjustment *adj;
+ g_autofree char *text = NULL;
+
+ adj = gtk_spin_button_get_adjustment (spin);
+ text = gr_number_format (gtk_adjustment_get_value (adj));
+ gtk_entry_set_text (GTK_ENTRY (spin), text);
+
+ return TRUE;
}
static void
@@ -208,11 +252,11 @@ shop_it (GrDetailsPage *page)
{
GrRecipeStore *store;
GtkWidget *window;
- int serves;
+ double yield;
store = gr_recipe_store_get ();
- serves = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (page->serves_spin));
- gr_recipe_store_add_to_shopping (store, page->recipe, serves);
+ yield = gtk_spin_button_get_value (GTK_SPIN_BUTTON (page->yield_spin));
+ gr_recipe_store_add_to_shopping (store, page->recipe, yield);
window = gtk_widget_get_ancestor (GTK_WIDGET (page), GTK_TYPE_APPLICATION_WINDOW);
gr_window_offer_shopping (GR_WINDOW (window));
@@ -392,7 +436,8 @@ gr_details_page_class_init (GrDetailsPageClass *klass)
gtk_widget_class_bind_template_child (widget_class, GrDetailsPage, meal_label);
gtk_widget_class_bind_template_child (widget_class, GrDetailsPage, season_desc);
gtk_widget_class_bind_template_child (widget_class, GrDetailsPage, season_label);
- gtk_widget_class_bind_template_child (widget_class, GrDetailsPage, serves_spin);
+ gtk_widget_class_bind_template_child (widget_class, GrDetailsPage, yield_spin);
+ gtk_widget_class_bind_template_child (widget_class, GrDetailsPage, yield_label);
gtk_widget_class_bind_template_child (widget_class, GrDetailsPage, warning_box);
gtk_widget_class_bind_template_child (widget_class, GrDetailsPage, spicy_warning);
gtk_widget_class_bind_template_child (widget_class, GrDetailsPage, garlic_warning);
@@ -417,7 +462,9 @@ gr_details_page_class_init (GrDetailsPageClass *klass)
gtk_widget_class_bind_template_callback (widget_class, more_recipes);
gtk_widget_class_bind_template_callback (widget_class, print_recipe);
gtk_widget_class_bind_template_callback (widget_class, export_recipe);
- gtk_widget_class_bind_template_callback (widget_class, serves_value_changed);
+ gtk_widget_class_bind_template_callback (widget_class, yield_spin_value_changed);
+ gtk_widget_class_bind_template_callback (widget_class, yield_spin_input);
+ gtk_widget_class_bind_template_callback (widget_class, yield_spin_output);
gtk_widget_class_bind_template_callback (widget_class, cook_it_later);
gtk_widget_class_bind_template_callback (widget_class, shop_it);
gtk_widget_class_bind_template_callback (widget_class, activate_link);
@@ -437,8 +484,7 @@ gr_details_page_new (void)
static void
populate_ingredients (GrDetailsPage *page,
- int num,
- int denom)
+ double scale)
{
g_autoptr(GtkSizeGroup) group = NULL;
g_autofree char **segments = NULL;
@@ -454,8 +500,7 @@ populate_ingredients (GrDetailsPage *page,
"title", segments[j],
"editable-title", FALSE,
"editable", FALSE,
- "scale-num", num,
- "scale-denom", denom,
+ "scale", scale,
"ingredients", page->ing_text,
NULL);
gtk_container_add (GTK_CONTAINER (page->ingredients_box), list);
@@ -565,7 +610,7 @@ gr_details_page_set_recipe (GrDetailsPage *page,
const char *cuisine;
const char *meal;
const char *season;
- int serves;
+ double yield;
const char *ingredients;
const char *instructions;
const char *notes;
@@ -577,11 +622,13 @@ gr_details_page_set_recipe (GrDetailsPage *page,
gboolean favorite;
int index;
g_autofree char *processed = NULL;
+ g_autofree char *amount = NULL;
+ g_autofree char *unit = NULL;
g_set_object (&page->recipe, recipe);
author = gr_recipe_get_author (recipe);
- serves = gr_recipe_get_serves (recipe);
+ yield = gr_recipe_get_yield (recipe);
prep_time = gr_recipe_get_prep_time (recipe);
cook_time = gr_recipe_get_cook_time (recipe);
cuisine = gr_recipe_get_cuisine (recipe);
@@ -601,7 +648,7 @@ gr_details_page_set_recipe (GrDetailsPage *page,
g_free (page->ing_text);
page->ing_text = g_strdup (ingredients);
- populate_ingredients (page, serves, serves);
+ populate_ingredients (page, yield);
if (prep_time[0] == '\0') {
gtk_widget_hide (page->prep_time_label);
@@ -657,8 +704,9 @@ gr_details_page_set_recipe (GrDetailsPage *page,
gtk_label_set_label (GTK_LABEL (page->instructions_label), processed);
gtk_label_set_track_visited_links (GTK_LABEL (page->instructions_label), FALSE);
- gtk_spin_button_set_value (GTK_SPIN_BUTTON (page->serves_spin), serves);
- gtk_widget_set_sensitive (page->serves_spin, ing != NULL);
+ update_yield_label (page, yield);
+ gtk_spin_button_set_value (GTK_SPIN_BUTTON (page->yield_spin), yield);
+ gtk_widget_set_sensitive (page->yield_spin, ing != NULL);
store = gr_recipe_store_get ();
diff --git a/src/gr-details-page.ui b/src/gr-details-page.ui
index 1803c7c..c980b8c 100644
--- a/src/gr-details-page.ui
+++ b/src/gr-details-page.ui
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface domain="gnome-recipes">
- <object class="GtkAdjustment" id="serves_adjustment">
+ <object class="GtkAdjustment" id="yield_adjustment">
<property name="lower">1</property>
<property name="upper">99</property>
<property name="step-increment">1</property>
@@ -161,7 +161,9 @@
<child>
<object class="GtkLabel">
<property name="visible">1</property>
- <property name="label" translatable="yes">Serves</property>
+ <property name="label" translatable="yes">_Yield</property>
+ <property name="use-underline">1</property>
+ <property name="mnemonic-widget">yield_spin</property>
<property name="valign">baseline</property>
<property name="xalign">1</property>
<property name="margin-start">20</property>
@@ -177,15 +179,33 @@
</packing>
</child>
<child>
- <object class="GtkSpinButton" id="serves_spin">
+ <object class="GtkBox">
<property name="visible">1</property>
+ <property name="orientation">horizontal</property>
<property name="halign">start</property>
<property name="valign">baseline</property>
- <property name="hexpand">1</property>
- <property name="adjustment">serves_adjustment</property>
- <property name="width-chars">2</property>
- <property name="margin-bottom">8</property>
- <signal name="value-changed" handler="serves_value_changed" swapped="yes"/>
+ <property name="spacing">10</property>
+ <child>
+ <object class="GtkSpinButton" id="yield_spin">
+ <property name="visible">1</property>
+ <property name="halign">start</property>
+ <property name="valign">baseline</property>
+ <property name="adjustment">yield_adjustment</property>
+ <property name="width-chars">4</property>
+ <property name="margin-bottom">8</property>
+ <signal name="value-changed" handler="yield_spin_value_changed"
swapped="yes"/>
+ <signal name="input" handler="yield_spin_input"/>
+ <signal name="output" handler="yield_spin_output"/>
+ </object>
+ </child>
+ <child>
+ <object class="GtkLabel" id="yield_label">
+ <property name="visible">1</property>
+ <property name="xalign">0</property>
+ <property name="valign">baseline</property>
+ <property name="margin-bottom">15</property>
+ </object>
+ </child>
</object>
<packing>
<property name="left-attach">1</property>
@@ -207,7 +227,7 @@
</object>
<packing>
<property name="left-attach">0</property>
- <property name="top-attach">1</property>
+ <property name="top-attach">2</property>
</packing>
</child>
<child>
@@ -219,7 +239,7 @@
</object>
<packing>
<property name="left-attach">1</property>
- <property name="top-attach">1</property>
+ <property name="top-attach">2</property>
</packing>
</child>
<child>
@@ -237,7 +257,7 @@
</object>
<packing>
<property name="left-attach">0</property>
- <property name="top-attach">2</property>
+ <property name="top-attach">3</property>
</packing>
</child>
<child>
@@ -249,7 +269,7 @@
</object>
<packing>
<property name="left-attach">1</property>
- <property name="top-attach">2</property>
+ <property name="top-attach">3</property>
</packing>
</child>
<child>
@@ -267,7 +287,7 @@
</object>
<packing>
<property name="left-attach">0</property>
- <property name="top-attach">3</property>
+ <property name="top-attach">4</property>
</packing>
</child>
<child>
@@ -279,7 +299,7 @@
</object>
<packing>
<property name="left-attach">1</property>
- <property name="top-attach">3</property>
+ <property name="top-attach">4</property>
</packing>
</child>
<child>
@@ -297,7 +317,7 @@
</object>
<packing>
<property name="left-attach">0</property>
- <property name="top-attach">4</property>
+ <property name="top-attach">5</property>
</packing>
</child>
<child>
@@ -309,7 +329,7 @@
</object>
<packing>
<property name="left-attach">1</property>
- <property name="top-attach">4</property>
+ <property name="top-attach">5</property>
</packing>
</child>
<child>
@@ -327,7 +347,7 @@
</object>
<packing>
<property name="left-attach">0</property>
- <property name="top-attach">5</property>
+ <property name="top-attach">6</property>
</packing>
</child>
<child>
@@ -339,7 +359,7 @@
</object>
<packing>
<property name="left-attach">1</property>
- <property name="top-attach">5</property>
+ <property name="top-attach">6</property>
</packing>
</child>
<child>
@@ -353,7 +373,7 @@
</object>
<packing>
<property name="left-attach">0</property>
- <property name="top-attach">6</property>
+ <property name="top-attach">7</property>
<property name="width">2</property>
</packing>
</child>
@@ -399,7 +419,7 @@
</object>
<packing>
<property name="left-attach">0</property>
- <property name="top-attach">7</property>
+ <property name="top-attach">8</property>
<property name="width">2</property>
</packing>
</child>
@@ -414,7 +434,7 @@
</object>
<packing>
<property name="left-attach">0</property>
- <property name="top-attach">8</property>
+ <property name="top-attach">9</property>
<property name="width">2</property>
</packing>
</child>
@@ -431,7 +451,7 @@
</object>
<packing>
<property name="left-attach">0</property>
- <property name="top-attach">9</property>
+ <property name="top-attach">10</property>
<property name="width">2</property>
</packing>
</child>
@@ -444,7 +464,7 @@
</object>
<packing>
<property name="left-attach">0</property>
- <property name="top-attach">10</property>
+ <property name="top-attach">11</property>
<property name="width">2</property>
</packing>
</child>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]