e3e870af644728028e63f8ada28dbb9ae87b291f
[nit.git] / lib / app / app_base.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2011-2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Base of the _app.nit_ framework, defines `App`
18 module app_base is
19 new_annotation app_name
20 new_annotation app_namespace
21 new_annotation app_version
22 new_annotation app_files
23 end
24
25 # App subclasses are cross-platform applications
26 #
27 # This class is refined by platform modules and so
28 # App can be specialized directly in the user application code.
29 class App
30 super AppComponent
31
32 protected init do end
33
34 # Starts the internal setup of graphical and other stuff
35 # Is called just before run
36 fun setup do end
37
38 # Main entry point of your application
39 fun run do end
40 end
41
42 # An element of an application that is notified of the application life cycle
43 #
44 # Most users of _app.nit_ need only to implement `on_create` to setup the application.
45 #
46 # On mobile devices, the application can be stopped a anytime when another application takes the foreground.
47 # Implement the callbacks `on_save_state` and `on_load_state` to keep the state of the application between execution,
48 # for an illusion of continuous execution.
49 abstract class AppComponent
50
51 # The application is being created
52 #
53 # You should build the UI at this time.
54 #
55 # Triggers are platform specific:
56 # * Android: `Activity.onCreate`
57 # * iOS: `UIApplicationDelegate application:didFinishLaunchingWithOptions`
58 fun on_create do end
59
60 # The application enters the active state, it is in the foreground and interactive
61 #
62 # Triggers are platform specific:
63 # * Android: `Activity.onResume`
64 # * iOS: `UIApplicationDelegate applicationDidBecomeActive`
65 fun on_resume do end
66
67 # The application leaves the active state but is still partially visible
68 #
69 # It may then go back to `on_resume` or `on_stop`.
70 #
71 # Triggers are platform specific:
72 # * Android: `Activity.onPause`
73 # * iOS: `UIApplicationDelegate applicationWillResignActive`
74 fun on_pause do end
75
76 # The application is completely hidden from the user
77 #
78 # It may then be destroyed or go back to a paused state with `on_restart`.
79 #
80 # Triggers are platform specific:
81 # * Android: `Activity.onStop`
82 # * iOS: `UIApplicationDelegate applicationDidEnterBackground`
83 fun on_stop do end
84
85 # The application returns to a visible state from a previous `on_stop`
86 #
87 # Triggers are platform specific:
88 # * Android: `Activity.onRestart`
89 # * iOS: `UIApplicationDelegate applicationWillEnterForeground`
90 fun on_restart do end
91
92 # The application may be destroyed soon, save its state for a future `on_restore_state`
93 #
94 # Triggers are platform specific:
95 # * Android: `Activity.onSaveInstanceState`
96 # * iOS: `UIApplicationDelegate applicationDidEnterBackground`
97 fun on_save_state do end
98
99 # The application is launching, restore its state from a previous `on_save_state`
100 #
101 # Triggers are platform specific:
102 # * Android: `Activity.onCreate`, _not_ `Activity.onRestoreInstanceState`
103 # as it is trigged only if there is a previous Android specific save state.
104 # * iOS: `UIApplicationDelegate applicationDidEnterBackground`
105 fun on_restore_state do end
106 end
107
108 # Print a warning
109 fun print_warning(object: Object)
110 do
111 sys.stderr.write object.to_s
112 sys.stderr.write "\n"
113 end
114
115 # The running `App`
116 fun app: App do return once new App
117
118 # Platform bound at compilation (by importation or -m)
119 #
120 # This value should not be used to decide the behavior of the software.
121 # Class refinement provide a safer and a static solution to apply variations.
122 # However, this value can be used in log files and communications with servers.
123 fun bound_platform: String do return "none"
124
125 app.setup
126 app.run