app: update clients to use `root_window`
[nit.git] / examples / calculator / src / calculator.nit
index 6ed8be7..32799ca 100644 (file)
@@ -14,8 +14,8 @@
 
 # Portable calculator UI
 module calculator is
-app_name "app.nit Calc."
-       app_version(0, 1, git_revision)
+       app_name "app.nit Calc"
+       app_version(0, 2, git_revision)
        app_namespace "org.nitlanguage.calculator"
 
        # Lock in portrait mode
@@ -29,14 +29,10 @@ import android::aware
 
 import calculator_logic
 
-redef class App
-       redef fun on_create
-       do
-               # Create the main window
-               window = new CalculatorWindow
-               super
-       end
-end
+# Show debug output?
+fun debug: Bool do return false
+
+redef fun root_window do return new CalculatorWindow
 
 # The main (and only) window of this calculator
 class CalculatorWindow
@@ -46,21 +42,23 @@ class CalculatorWindow
        private var context = new CalculatorContext
 
        # Main window layout
-       private var layout = new VerticalLayout(parent=self)
+       var layout = new VerticalLayout(parent=self)
 
        # Main display, at the top of the screen
        private var display = new TextInput(parent=layout)
 
        # Maps operators as `String` to their `Button`
-       private var buttons = new HashMap[String, Button]
+       var buttons = new HashMap[String, Button]
 
        init
        do
+               if debug then print "CalculatorWindow::init"
+
                # All the button labels, row by row
                var rows = [["7", "8", "9", "+"],
                            ["4", "5", "6", "-"],
-                           ["1", "2", "3", "*"],
-                           ["0", ".", "C", "/"],
+                           ["1", "2", "3", "×"],
+                           ["0", ".", "C", "÷"],
                            ["="]]
 
                for row in rows do
@@ -68,7 +66,6 @@ class CalculatorWindow
 
                        for op in row do
                                var but = new Button(parent=row_layout, text=op)
-                               but.observers.add self
                                buttons[op] = but
                        end
                end
@@ -76,6 +73,8 @@ class CalculatorWindow
 
        redef fun on_event(event)
        do
+               if debug then print "CalculatorWindow::on_event {event}"
+
                if event isa ButtonPressEvent then
 
                        var op = event.sender.text
@@ -85,9 +84,9 @@ class CalculatorWindow
                        else if op.is_numeric then
                                var n = op.to_i
                                context.push_digit n
-                       else
+                       else if op != null then
                                buttons["."].enabled = true
-                               context.push_op op.chars.first
+                               context.push_op op
                        end
 
                        display.text = context.display_text
@@ -96,12 +95,16 @@ class CalculatorWindow
 
        redef fun on_save_state
        do
+               if debug then print "CalculatorWindow::on_save_state"
+
                app.data_store["context"] = context
                super
        end
 
        redef fun on_restore_state
        do
+               if debug then print "CalculatorWindow::on_restore_state"
+
                super
 
                var context = app.data_store["context"]