Merge: Nitsmell : Adding new code smells and print console updated
[nit.git] / lib / app / ui.nit
index 0c7d524..1fdf801 100644 (file)
@@ -27,12 +27,34 @@ redef class App
 
        # The current `Window` of this activity
        #
-       # This attribute must be set by refinements of `App`.
-       var window: Window is writable
+       # This attribute is set by `push_window`.
+       var window: Window is noinit
 
-       redef fun on_create do window.on_create
+       # Make visible and push `window` on the top of `pop_window`
+       #
+       # This method must be called at least once within `App::on_create`.
+       # It can be called at any times while the app is active.
+       fun push_window(window: Window)
+       do
+               window_stack.add window
+               self.window = window
+       end
 
-       redef fun on_start do window.on_start
+       # Pop the current `window` from the stack and show the previous one
+       #
+       # Require: `window_stack.not_empty`
+       fun pop_window
+       do
+               assert window_stack.not_empty
+               window_stack.pop
+               window = window_stack.last
+               window.on_resume
+       end
+
+       # Stack of active windows
+       var window_stack = new Array[Window]
+
+       redef fun on_create do window.on_create
 
        redef fun on_resume do window.on_resume
 
@@ -40,8 +62,6 @@ redef class App
 
        redef fun on_stop do window.on_stop
 
-       redef fun on_destroy do window.on_destroy
-
        redef fun on_restore_state do window.on_restore_state
 
        redef fun on_save_state do window.on_save_state
@@ -83,11 +103,17 @@ class Control
 
        # Direct parent `Control` in the control tree
        #
+       # The parents (direct and indirect) receive all events from `self`,
+       # like the `observers`.
+       #
        # If `null` then `self` is at the root of the tree, or not yet attached.
        var parent: nullable CompositeControl = null is private writable(set_parent)
 
        # Direct parent `Control` in the control tree
        #
+       # The parents (direct and indirect) receive all events from `self`,
+       # like the `observers`.
+       #
        # Setting `parent` calls `remove` on the old parent and `add` on the new one.
        fun parent=(parent: nullable CompositeControl)
        is autoinit     do
@@ -100,12 +126,25 @@ class Control
 
                set_parent parent
        end
+
+       # Also notify the parents (both direct and indirect)
+       redef fun notify_observers(event)
+       do
+               super
+
+               var p = parent
+               while p != null do
+                       p.on_event event
+                       p = p.parent
+               end
+       end
 end
 
 # A `Control` grouping other controls
 class CompositeControl
        super Control
 
+       # Child controls composing this control
        protected var items = new Array[Control]
 
        # Add `item` as a child of `self`
@@ -122,16 +161,12 @@ class CompositeControl
 
        redef fun on_create do for i in items do i.on_create
 
-       redef fun on_start do for i in items do i.on_start
-
        redef fun on_resume do for i in items do i.on_resume
 
        redef fun on_pause do for i in items do i.on_pause
 
        redef fun on_stop do for i in items do i.on_stop
 
-       redef fun on_destroy do for i in items do i.on_destroy
-
        redef fun on_restore_state do for i in items do i.on_restore_state
 
        redef fun on_save_state do for i in items do i.on_save_state
@@ -140,6 +175,12 @@ end
 # A window, root of the `Control` tree
 class Window
        super CompositeControl
+
+       # Should the back button be shown and used to go back to a previous window?
+       fun enable_back_button: Bool do return app.window_stack.length > 1
+
+       # The back button has been pressed, usually to open the previous window
+       fun on_back_button do app.pop_window
 end
 
 # A viewable `Control`
@@ -181,7 +222,7 @@ abstract class TextView
        # depending on the customization options of each platform.
        # For consistent results, it is recommended to use only on instances
        # of `Label` and `size` should be either 0.0, 0.5 or 1.0.
-       fun align=(center: nullable Float) is autoinit do end
+       fun align=(align: nullable Float) is autoinit do end
 end
 
 # A control for the user to enter custom `text`
@@ -210,12 +251,29 @@ class CheckBox
        var is_checked = false is writable
 end
 
+# Event sent from a `VIEW`
+class ViewEvent
+       super AppEvent
+
+       # The `VIEW` that raised this event
+       var sender: VIEW
+
+       # Type of the `sender`
+       type VIEW: View
+end
+
 # A `Button` press event
 class ButtonPressEvent
-       super AppEvent
+       super ViewEvent
+
+       redef type VIEW: Button
+end
+
+# The `CheckBox` `sender` has been toggled
+class ToggleEvent
+       super ViewEvent
 
-       # The `Button` that raised this event
-       var sender: Button
+       redef type VIEW: CheckBox
 end
 
 # A layout to visually organize `Control`s
@@ -239,3 +297,8 @@ class ListLayout
        super View
        super CompositeControl
 end
+
+redef class Text
+       # Open the URL `self` with the default browser
+       fun open_in_browser do print_error "Text::open_in_browser not implemented on this platform."
+end