From e3af509358dd57e24e3073de8a32640d6a3832d5 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 8 Mar 2025 19:10:29 +0100 Subject: [PATCH] Add bell widget --- CHANGELOG.md | 1 + src/widgets.rs | 2 ++ src/widgets/bell.rs | 55 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 src/widgets/bell.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index c43f3dc..c30abdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ Procedure when bumping the version number: ### Added - `Frame::set_bell` to print a bell character when the frame is displayed +- `widgets::bell` ## v0.3.3 - 2025-02-28 diff --git a/src/widgets.rs b/src/widgets.rs index 28b44d8..cbbff7c 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -1,4 +1,5 @@ pub mod background; +pub mod bell; pub mod border; pub mod boxed; pub mod cursor; @@ -16,6 +17,7 @@ pub mod text; pub mod title; pub use background::*; +pub use bell::*; pub use border::*; pub use boxed::*; pub use cursor::*; diff --git a/src/widgets/bell.rs b/src/widgets/bell.rs new file mode 100644 index 0000000..b37fb67 --- /dev/null +++ b/src/widgets/bell.rs @@ -0,0 +1,55 @@ +use crate::{Frame, Size, Widget, WidthDb}; + +/////////// +// State // +/////////// + +#[derive(Debug, Default, Clone)] +pub struct BellState { + // Whether the bell should be rung the next time the widget is displayed. + pub ring: bool, +} + +impl BellState { + pub fn new() -> Self { + Self::default() + } + + pub fn widget(&mut self) -> Bell<'_> { + Bell { state: self } + } +} + +//////////// +// Widget // +//////////// + +#[derive(Debug)] +pub struct Bell<'a> { + state: &'a mut BellState, +} + +impl Bell<'_> { + pub fn state(&mut self) -> &mut BellState { + self.state + } +} + +impl Widget for Bell<'_> { + fn size( + &self, + _widthdb: &mut WidthDb, + _max_width: Option, + _max_height: Option, + ) -> Result { + Ok(Size::ZERO) + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + if self.state.ring { + frame.set_bell(true); + self.state.ring = false + } + Ok(()) + } +}