Rating widget: turn rating into a property
[cinaest] / src / rating-widget.vala
index d432b48..cafb73a 100644 (file)
 using Gtk;
 
 public class RatingWidget : DrawingArea {
-       private int rating;
+       private int _rating;
        private bool button_pressed;
        private Gdk.Color active_color;
        private Gdk.Color disabled_color;
 
+       public int rating {
+               get {
+                       return _rating;
+               }
+               set {
+                       if (_rating == value)
+                               return;
+
+                       _rating = value;
+
+                       unowned Gdk.Region region = this.window.get_clip_region ();
+                       // redraw the cairo canvas completely by exposing it
+                       this.window.invalidate_region (region, true);
+                       this.window.process_updates (true);
+               }
+       }
+
        public RatingWidget () {
                var style = Gtk.rc_get_style_by_paths (this.get_settings (), "GtkButton", null, typeof (Gtk.Button));
                if (!style.lookup_color ("ActiveTextColor", out active_color))
@@ -101,26 +118,20 @@ public class RatingWidget : DrawingArea {
        }
 
        private void rate (double x) {
-               double r = 10 * x / this.allocation.width;
-               if (r <= 0) {
-                       rating = 0;
+               int r;
+
+               x = 10 * x / this.allocation.width;
+               if (x <= 0) {
+                       r = 0;
                } else {
-                       int star = (int) r;
-                       rating = 10 * (star + 1);
-                       r -= star;
-                       if (r <= 0.333)
-                               rating -= 5;
-                       if (rating > 100)
-                               rating = 100;
+                       int star = (int) x;
+                       r = 10 * (star + 1);
+                       x -= star;
+                       if (x <= 0.333)
+                               r -= 5;
+                       if (r > 100)
+                               r = 100;
                }
-
-               unowned Gdk.Region region = this.window.get_clip_region ();
-               // redraw the cairo canvas completely by exposing it
-               this.window.invalidate_region (region, true);
-               this.window.process_updates (true);
-       }
-
-       public int get_rating () {
-               return rating;
+               rating = r;
        }
 }