calculator: add scientific variation to the portable app
[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, 1, 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 class App
36 redef fun on_create
37 do
38 if debug then print "App::on_create"
39
40 # Create the main window
41 window = new CalculatorWindow
42 super
43 end
44 end
45
46 # The main (and only) window of this calculator
47 class CalculatorWindow
48 super Window
49
50 # Calculator context, our business logic
51 private var context = new CalculatorContext
52
53 # Main window layout
54 var layout = new VerticalLayout(parent=self)
55
56 # Main display, at the top of the screen
57 private var display = new TextInput(parent=layout)
58
59 # Maps operators as `String` to their `Button`
60 var buttons = new HashMap[String, Button]
61
62 init
63 do
64 if debug then print "CalculatorWindow::init"
65
66 # All the button labels, row by row
67 var rows = [["7", "8", "9", "+"],
68 ["4", "5", "6", "-"],
69 ["1", "2", "3", "×"],
70 ["0", ".", "C", "÷"],
71 ["="]]
72
73 for row in rows do
74 var row_layout = new HorizontalLayout(parent=layout)
75
76 for op in row do
77 var but = new Button(parent=row_layout, text=op)
78 but.observers.add self
79 buttons[op] = but
80 end
81 end
82 end
83
84 redef fun on_event(event)
85 do
86 if debug then print "CalculatorWindow::on_event {event}"
87
88 if event isa ButtonPressEvent then
89
90 var op = event.sender.text
91 if op == "." then
92 event.sender.enabled = false
93 context.switch_to_decimals
94 else if op.is_numeric then
95 var n = op.to_i
96 context.push_digit n
97 else if op != null then
98 buttons["."].enabled = true
99 context.push_op op
100 end
101
102 display.text = context.display_text
103 end
104 end
105
106 redef fun on_save_state
107 do
108 if debug then print "CalculatorWindow::on_save_state"
109
110 app.data_store["context"] = context
111 super
112 end
113
114 redef fun on_restore_state
115 do
116 if debug then print "CalculatorWindow::on_restore_state"
117
118 super
119
120 var context = app.data_store["context"]
121 if not context isa CalculatorContext then return
122
123 self.context = context
124 display.text = context.display_text
125 end
126 end