app: update clients to use `root_window`
[nit.git] / examples / calculator / src / calculator.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Portable calculator UI
16 module calculator is
17 app_name "app.nit Calc"
18 app_version(0, 2, git_revision)
19 app_namespace "org.nitlanguage.calculator"
20
21 # Lock in portrait mode
22 android_manifest_activity """android:screenOrientation="portrait""""
23 android_api_target 15
24 end
25
26 import app::ui
27 import app::data_store
28 import android::aware
29
30 import calculator_logic
31
32 # Show debug output?
33 fun debug: Bool do return false
34
35 redef fun root_window do return new CalculatorWindow
36
37 # The main (and only) window of this calculator
38 class CalculatorWindow
39 super Window
40
41 # Calculator context, our business logic
42 private var context = new CalculatorContext
43
44 # Main window layout
45 var layout = new VerticalLayout(parent=self)
46
47 # Main display, at the top of the screen
48 private var display = new TextInput(parent=layout)
49
50 # Maps operators as `String` to their `Button`
51 var buttons = new HashMap[String, Button]
52
53 init
54 do
55 if debug then print "CalculatorWindow::init"
56
57 # All the button labels, row by row
58 var rows = [["7", "8", "9", "+"],
59 ["4", "5", "6", "-"],
60 ["1", "2", "3", "×"],
61 ["0", ".", "C", "÷"],
62 ["="]]
63
64 for row in rows do
65 var row_layout = new HorizontalLayout(parent=layout)
66
67 for op in row do
68 var but = new Button(parent=row_layout, text=op)
69 buttons[op] = but
70 end
71 end
72 end
73
74 redef fun on_event(event)
75 do
76 if debug then print "CalculatorWindow::on_event {event}"
77
78 if event isa ButtonPressEvent then
79
80 var op = event.sender.text
81 if op == "." then
82 event.sender.enabled = false
83 context.switch_to_decimals
84 else if op.is_numeric then
85 var n = op.to_i
86 context.push_digit n
87 else if op != null then
88 buttons["."].enabled = true
89 context.push_op op
90 end
91
92 display.text = context.display_text
93 end
94 end
95
96 redef fun on_save_state
97 do
98 if debug then print "CalculatorWindow::on_save_state"
99
100 app.data_store["context"] = context
101 super
102 end
103
104 redef fun on_restore_state
105 do
106 if debug then print "CalculatorWindow::on_restore_state"
107
108 super
109
110 var context = app.data_store["context"]
111 if not context isa CalculatorContext then return
112
113 self.context = context
114 display.text = context.display_text
115 end
116 end