Skip to content
This repository was archived by the owner on May 11, 2023. It is now read-only.

Commit 3e056dc

Browse files
committed
terminal-tui: Add global event listener to demo
Signed-off-by: Erik Kundt <erik@zirkular.io>
1 parent 78f67ca commit 3e056dc

File tree

3 files changed

+53
-31
lines changed

3 files changed

+53
-31
lines changed

terminal-tui/examples/tui-demo/app.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
use anyhow::Result;
22

33
use tui_realm_stdlib::Textarea;
4+
5+
use tuirealm::event::{Key, KeyEvent, KeyModifiers};
46
use tuirealm::props::{AttrValue, Attribute, BorderSides, Borders, Color, TextSpan};
57
use tuirealm::tui::layout::{Constraint, Direction, Layout, Rect};
6-
use tuirealm::Frame;
8+
use tuirealm::{Frame, Sub, SubClause, SubEventClause};
79

810
use radicle_terminal_tui as tui;
911
use tui::components::{ApplicationTitle, Shortcut, ShortcutBar, TabContainer};
1012
use tui::{App, Tui};
1113

14+
use super::components::GlobalListener;
15+
1216
/// Messages handled by this tui-application.
1317
#[derive(Debug, Eq, PartialEq)]
1418
pub enum Message {
@@ -18,6 +22,7 @@ pub enum Message {
1822
/// All components known to the application.
1923
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
2024
pub enum Id {
25+
Global,
2126
Title,
2227
Content,
2328
Shortcuts,
@@ -82,7 +87,8 @@ impl Default for Demo {
8287

8388
impl Tui<Id, Message> for Demo {
8489
fn init(&mut self, app: &mut App<Id, Message>) -> Result<()> {
85-
app.mount(Id::Title, ApplicationTitle::new("my-project"))?;
90+
// Add app components
91+
app.mount(Id::Title, ApplicationTitle::new("my-project"), vec![])?;
8692
app.mount(
8793
Id::Content,
8894
TabContainer::default()
@@ -98,13 +104,29 @@ impl Tui<Id, Message> for Demo {
98104
.borders(Borders::default().sides(BorderSides::NONE))
99105
.text_rows(&Self::help_content()),
100106
),
107+
vec![],
101108
)?;
102109
app.mount(
103110
Id::Shortcuts,
104111
ShortcutBar::default()
105112
.child(Shortcut::new("q", "quit"))
106113
.child(Shortcut::new("?", "help")),
114+
vec![],
115+
)?;
116+
117+
// Add global key listener and subscribe to key events
118+
app.mount(
119+
Id::Global,
120+
GlobalListener::default(),
121+
vec![Sub::new(
122+
SubEventClause::Keyboard(KeyEvent {
123+
code: Key::Char('q'),
124+
modifiers: KeyModifiers::NONE,
125+
}),
126+
SubClause::Always,
127+
)],
107128
)?;
129+
108130
// We need to give focus to a component then
109131
app.activate(Id::Content)?;
110132

terminal-tui/examples/tui-demo/components.rs

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use tui_realm_stdlib::Textarea;
1+
use tui_realm_stdlib::{Phantom, Textarea};
22

33
use tuirealm::command::{Cmd, CmdResult, Direction};
44
use tuirealm::event::{Event, Key, KeyEvent};
@@ -9,10 +9,12 @@ use tui::components::{ApplicationTitle, ShortcutBar, TabContainer};
99

1010
use super::app::Message;
1111

12-
/// Since `terminal-tui` does not know the type of messages that are being
13-
/// passed around in the app, the following handlers need to be implemented for
14-
/// each component used.
15-
impl Component<Message, NoUserEvent> for ApplicationTitle {
12+
#[derive(Default, MockComponent)]
13+
pub struct GlobalListener {
14+
component: Phantom,
15+
}
16+
17+
impl Component<Message, NoUserEvent> for GlobalListener {
1618
fn on(&mut self, event: Event<NoUserEvent>) -> Option<Message> {
1719
match event {
1820
Event::Keyboard(KeyEvent {
@@ -24,27 +26,24 @@ impl Component<Message, NoUserEvent> for ApplicationTitle {
2426
}
2527
}
2628

29+
/// Since `terminal-tui` does not know the type of messages that are being
30+
/// passed around in the app, the following handlers need to be implemented for
31+
/// each component used.
32+
impl Component<Message, NoUserEvent> for ApplicationTitle {
33+
fn on(&mut self, _event: Event<NoUserEvent>) -> Option<Message> {
34+
None
35+
}
36+
}
37+
2738
impl Component<Message, NoUserEvent> for Textarea {
28-
fn on(&mut self, event: Event<NoUserEvent>) -> Option<Message> {
29-
match event {
30-
Event::Keyboard(KeyEvent {
31-
code: Key::Char('q'),
32-
..
33-
}) => Some(Message::Quit),
34-
_ => None,
35-
}
39+
fn on(&mut self, _event: Event<NoUserEvent>) -> Option<Message> {
40+
None
3641
}
3742
}
3843

3944
impl Component<Message, NoUserEvent> for ShortcutBar {
40-
fn on(&mut self, event: Event<NoUserEvent>) -> Option<Message> {
41-
match event {
42-
Event::Keyboard(KeyEvent {
43-
code: Key::Char('q'),
44-
..
45-
}) => Some(Message::Quit),
46-
_ => None,
47-
}
45+
fn on(&mut self, _event: Event<NoUserEvent>) -> Option<Message> {
46+
None
4847
}
4948
}
5049

@@ -54,10 +53,6 @@ impl Component<Message, NoUserEvent> for TabContainer {
5453
Event::Keyboard(KeyEvent { code: Key::Tab, .. }) => {
5554
self.perform(Cmd::Move(Direction::Right))
5655
}
57-
Event::Keyboard(KeyEvent {
58-
code: Key::Char('q'),
59-
..
60-
}) => return Some(Message::Quit),
6156
_ => CmdResult::None,
6257
};
6358
None

terminal-tui/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use tuirealm::application::PollStrategy;
99
use tuirealm::props::{AttrValue, Attribute};
1010
use tuirealm::tui::layout::Rect;
1111
use tuirealm::{Application, EventListenerCfg, NoUserEvent};
12-
use tuirealm::{Component, Frame};
12+
use tuirealm::{Component, Frame, Sub};
1313

1414
pub mod components;
15-
pub mod state;
1615
pub mod layout;
16+
pub mod state;
1717
pub mod theme;
1818

1919
/// A proxy that abstracts the tui-realm-specific application.
@@ -47,11 +47,16 @@ where
4747
Self { backend }
4848
}
4949

50-
pub fn mount<C>(&mut self, id: Id, component: C) -> Result<(), Error>
50+
pub fn mount<C>(
51+
&mut self,
52+
id: Id,
53+
component: C,
54+
subs: Vec<Sub<Id, NoUserEvent>>,
55+
) -> Result<(), Error>
5156
where
5257
C: Component<Message, NoUserEvent> + 'static,
5358
{
54-
self.backend.mount(id, Box::new(component), vec![])?;
59+
self.backend.mount(id, Box::new(component), subs)?;
5560
Ok(())
5661
}
5762

0 commit comments

Comments
 (0)