Module below_view::controllers
source · [−]Expand description
Controllers module is used for how below will react on a user’s input.
Terms
Command
A command is the name or id of a certain below view behavior. Each command will be mapped to a uniq EventController and can be explicitly called from CommandPalette.
Event
An Event is a trigger of an EventController. Each EventController has a default Event associate with it and can be customized by an cmdrc file.
EventController
An EventController is the handler of a “Command”. Each EventController will have the following 4 pieces:
- command: The command of this EventController
- default_event: The event trigger of this EventController by default.
- handle: How should below handle such event given the currst StatsView
- callback: How should below handle such event with a cursive object. EventController is a interface ONLY struct.
Controllers
The Controllers is a enum of controllers and each enum value will be mapped to a uniq EventController. This will help us to unify EventController types. Controllers will provide similar interface as EventController except it will take a reference of self object. The impl of Controllers does nothing but call the corresponding fn in the associated EventController.
Construction
make_event_controller
Convenience macro of making an EventController.
make_controllers
make_controllers macro will take a series of (enum value: EventController) pairs and generate the Controllers enum. Besides that, it will also generate HashMap construction fns: make_event_controller_map and make_cmd_controller_map
make_event_controller_map && make_cmd_controller_map
On constructing global ViewState, we will have two HashMap refcells constructed: event_controller_map and
cmd_controller_map. As their name indicated, they are maps between “Event” or “Command” to their corresponding
Controllers enum values. The event_controller_map will be referenced by StatsView
Calling flow
Event to EventController
- User typed something when not in the “command mode”. For example “c”.
- StatsView
capture the cursive event. For example Event::char(‘c’). - StatsView
trys to find the corresponding Controllers value in event_controller_map 3.1 if not found, send the event to its parent and return. 3.2 if found, get the Controllers value. For example Controllers::Cgroup. - Invoke the handle function. For example Controllers::Cgroup.hanle()
- Invoke the callback function. For example Controllers::Cgroup.callback()
- Mark the event as consumed.
Command to EventController
- User typed something in “command mode” and hit enter. For example: “cgroup”.
- CommandPalette capture the input and try to find the corresponding Controllers value in cmd_controller_map 2.1 if not found, raise error message 2.2 if found, get the Controllers value. For example Controllers::Cgroup
- Invoke the handle function. For example Controllers::Cgroup.hanle()
- Invoke the callback function. For example Controllers::Cgroup.callback()
Enums
Functions
Map the controller enum to cmd string
Map the controller enum to event trigger
Convert a command to Cursive event.