1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Portable calculator UI
17 app_name
"app.nit Calc."
18 app_version
(0, 1, git_revision
)
19 app_namespace
"org.nitlanguage.calculator"
21 # Lock in portrait mode
22 android_manifest_activity
"""android:screenOrientation="portrait""""
27 import app::data_store
30 import calculator_logic
33 fun debug: Bool do return false
38 if debug then print "App::on_create
"
40 # Create the main window
41 push_window new CalculatorWindow
46 # The main (and only) window of this calculator
47 class CalculatorWindow
50 # Calculator context, our business logic
51 private var context = new CalculatorContext
54 var layout = new VerticalLayout(parent=self)
56 # Main display, at the top of the screen
57 private var display = new TextInput(parent=layout)
59 # Maps operators as `String` to their `Button`
60 var buttons = new HashMap[String, Button]
64 if debug then print "CalculatorWindow::init"
66 # All the button labels, row by row
67 var rows = [["7", "8", "9", "+"],
74 var row_layout = new HorizontalLayout(parent=layout)
77 var but = new Button(parent=row_layout, text=op)
83 redef fun on_event(event)
85 if debug then print "CalculatorWindow::on_event
{event}"
87 if event isa ButtonPressEvent then
89 var op = event.sender.text
91 event.sender.enabled = false
92 context.switch_to_decimals
93 else if op.is_numeric then
96 else if op != null then
97 buttons["."].enabled = true
101 display.text = context.display_text
105 redef fun on_save_state
107 if debug then print "CalculatorWindow::on_save_state
"
109 app.data_store["context
"] = context
113 redef fun on_restore_state
115 if debug then print "CalculatorWindow::on_restore_state
"
119 var context = app.data_store["context
"]
120 if not context isa CalculatorContext then return
122 self.context = context
123 display.text = context.display_text