Skip to main content

below_view/controllers/
mod.rs

1// Copyright (c) Facebook, Inc. and its affiliates.
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//! Controllers module is used for how below will react on a user's input.
16//!
17//! # Terms
18//! ## Command
19//! A command is the name or id of a certain below view behavior. Each command will be
20//! mapped to a uniq EventController and can be explicitly called from CommandPalette.
21//!
22//! ## Event
23//! An Event is a trigger of an EventController. Each EventController has a default
24//! Event associate with it and can be customized by an cmdrc file.
25//!
26//! ## EventController
27//! An EventController is the handler of a "Command". Each EventController will have the following 4 pieces:
28//! * command: The command of this EventController
29//! * default_events: An array of default event triggers for this EventController
30//! * handle: How should below handle such event given the currst StatsView<T>
31//! * callback: How should below handle such event with a cursive object.
32//! EventController is a interface ONLY struct.
33//!
34//! ## Controllers
35//! The Controllers is a enum of controllers and each enum value will be mapped to a uniq EventController. This
36//! will help us to unify EventController types. Controllers will provide similar interface as EventController
37//! except it will take a reference of self object. The impl of Controllers does nothing but call the corresponding
38//! fn in the associated EventController.
39//!
40//! # Construction
41//! ## make_event_controller
42//! Convenience macro of making an EventController.
43//!
44//! ## make_controllers
45//! make_controllers macro will take a series of (enum value: EventController) pairs and generate the Controllers
46//! enum. Besides that, it will also generate HashMap construction fns: make_event_controller_map and
47//! make_cmd_controller_map
48//!
49//! ## make_event_controller_map && make_cmd_controller_map
50//! On constructing global ViewState, we will have two HashMap refcells constructed: event_controller_map and
51//! cmd_controller_map. As their name indicated, they are maps between "Event" or "Command" to their corresponding
52//! Controllers enum values. The event_controller_map will be referenced by StatsView<T> and cmd_controller_map
53//! will be referenced by CommandPalette. While generating the event_controller_map, we will read the cmdrc file
54//! to replace default Event with user specified one.
55//!
56//! # Calling flow
57//! ## Event to EventController
58//! 1. User typed something when not in the "command mode". For example "c".
59//! 2. StatsView<T> capture the cursive event. For example Event::char('c').
60//! 3. StatsView<T> trys to find the corresponding Controllers value in event_controller_map
61//!   3.1 if not found, send the event to its parent and return.
62//!   3.2 if found, get the Controllers value. For example Controllers::Cgroup.
63//! 4. Invoke the handle function. For example Controllers::Cgroup.hanle()
64//! 5. Invoke the callback function. For example Controllers::Cgroup.callback()
65//! 6. Mark the event as consumed.
66//!
67//! ## Command to EventController
68//! 1. User typed something in "command mode" and hit enter. For example: "cgroup".
69//! 2. CommandPalette capture the input and try to find the corresponding Controllers value in cmd_controller_map
70//!   2.1 if not found, raise error message
71//!   2.2 if found, get the Controllers value. For example Controllers::Cgroup
72//! 3. Invoke the handle function. For example Controllers::Cgroup.hanle()
73//! 4. Invoke the callback function. For example Controllers::Cgroup.callback()
74use cursive::event::Event;
75use cursive::event::EventTrigger;
76use cursive::event::Key;
77use cursive::Cursive;
78use toml::value::Value;
79
80#[macro_use]
81mod controller_infra;
82mod content_controllers;
83mod sample_controllers;
84mod view_controllers;
85
86#[cfg(test)]
87mod test;
88
89use common::open_source_shim;
90use content_controllers::*;
91use controller_infra::*;
92use sample_controllers::*;
93use view_controllers::*;
94
95use crate::refresh;
96use crate::stats_view::StateCommon;
97use crate::stats_view::StatsView;
98use crate::stats_view::ViewBridge;
99use crate::ViewState;
100
101open_source_shim!();
102
103use std::collections::HashMap;
104
105pub use controller_infra::event_to_string;
106pub use controller_infra::str_to_event;
107
108make_controllers!(
109    CmdPalette: InvokeCmdPalette,
110    NextTab: NextTabImpl,
111    PrevTab: PrevTabImpl,
112    NextCol: NextColImpl,
113    PrevCol: PrevColImpl,
114    Right: RightImpl,
115    Left: LeftImpl,
116    SortCol: SortByColumn,
117    Filter: FilterPopup,
118    CFilter: ClearFilter,
119    JForward: JumpForward,
120    JBackward: JumpBackward,
121    NSample: NextSample,
122    PSample: PrevSample,
123    Pause: PauseImpl,
124    Quit: QuitImpl,
125    Help: HelpMenu,
126    Process: ProcessView,
127    Cgroup: CgroupView,
128    System: SystemView,
129    Gpu: GpuView,
130    GpuProcess: GpuProcessView,
131    GpuZoom: GpuZoomView,
132    Zoom: ZoomView,
133    Fold: FoldProcessView,
134    NextPage: NextPageImpl,
135    PrevPage: PrevPageImpl,
136    NextSelection: NextSelectionImpl,
137    PrevSelection: PrevSelectionImpl,
138);