1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
#![doc = include_str!("../readme.md")]
use log::debug;
use rat_event::util::Outcome;
use rat_event::{ct_event, FocusKeys, HandleEvent, MouseOnly};
use ratatui::layout::{Position, Rect};
use std::cell::Cell;
use std::fmt::{Debug, Formatter};
use std::iter::Zip;
use std::{ptr, vec};
pub mod event {
//! Rexported eventhandling traits.
pub use rat_event::util::Outcome;
pub use rat_event::{FocusKeys, HandleEvent, MouseOnly, UsedEvent};
}
/// Contains flags for the focus.
/// This struct is embedded in the widget state.
///
/// See [HasFocusFlag], [on_gained!](crate::on_gained!) and
/// [on_lost!](crate::on_lost!).
///
#[derive(Clone, Default, PartialEq, Eq)]
pub struct FocusFlag {
/// Focus.
pub focus: Cell<bool>,
/// This widget just gained the focus. This flag is set by [Focus::handle]
/// if there is a focus transfer, and will be reset by the next
/// call to [Focus::handle].
///
/// See [on_gained!](crate::on_gained!)
pub gained: Cell<bool>,
/// This widget just lost the focus. This flag is set by [Focus::handle]
/// if there is a focus transfer, and will be reset by the next
/// call to [Focus::handle].
///
/// See [on_lost!](crate::on_lost!)
pub lost: Cell<bool>,
}
/// Trait for a widget that has a focus flag.
pub trait HasFocusFlag {
/// Access to the flag for the rest.
fn focus(&self) -> &FocusFlag;
/// Access the area for mouse focus.
fn area(&self) -> Rect;
/// Focused?
fn is_focused(&self) -> bool {
self.focus().get()
}
/// Just lost focus.
fn lost_focus(&self) -> bool {
self.focus().lost()
}
/// Just gained focus.
fn gained_focus(&self) -> bool {
self.focus().gained()
}
}
impl Debug for FocusFlag {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FocusFlag")
.field("focus", &self.focus.get())
.field("gained", &self.gained.get())
.field("lost", &self.lost.get())
.finish()
}
}
/// Keeps track of the focus.
///
/// It works by adding a [FocusFlag] to the State of a widget.
/// Focus is constructed with a list of references to these flags
/// and switches the focus that way. Each widget stays separate otherwise
/// and can pull its focus state from this struct.
///
/// ```rust ignore
/// use rat_focus::Focus;
///
/// let f = Focus::new(&[
/// &widget1,
/// &widget2
/// ]).handle(evt, FocusKeys);
/// ```
///
/// The result `f` indicates whether the focus has changed.
#[derive(Debug, Default)]
pub struct Focus<'a> {
/// Summarizes all the contained FocusFlags.
/// If any of them has the focus set, this will
/// be set too.
///
/// This can help if you build compound widgets.
accu: Option<&'a FocusFlag>,
/// Area for the whole compound. Only set if accu is some.
area: Rect,
/// Areas for each widget.
areas: Vec<Rect>,
/// List of flags.
focus: Vec<&'a FocusFlag>,
/// List of sub-accumulators and their dependencies.
/// Keeps track of all the Flags of a compound widget and its
/// accumulator.
///
/// This is filled if you call [Focus::append]. The accu of the
/// appended Focus and all its focus-flags are added. And
/// all the sub_accu of it are appended too.
sub_accu: Vec<(&'a FocusFlag, Rect, Vec<&'a FocusFlag>)>,
}
impl FocusFlag {
/// Has the focus.
#[inline]
pub fn get(&self) -> bool {
self.focus.get()
}
/// Set the focus.
#[inline]
pub fn set(&self) {
self.focus.set(true);
}
/// Just lost the focus.
#[inline]
pub fn lost(&self) -> bool {
self.lost.get()
}
/// Just gained the focus.
#[inline]
pub fn gained(&self) -> bool {
self.gained.get()
}
}
/// Does a match on the state struct of a widget. If `widget_state.lost_focus()` is true
/// the block is executed. This requires that `widget_state` implements [HasFocusFlag],
/// but that's the basic requirement for this whole crate.
///
/// ```rust ignore
/// use rat_focus::on_lost;
///
/// on_lost!(
/// state.field1 => {
/// // do checks
/// },
/// state.field2 => {
/// // do checks
/// }
/// );
/// ```
#[macro_export]
macro_rules! on_lost {
($($field:expr => $validate:expr),*) => {{
use $crate::HasFocusFlag;
$(if $field.lost_focus() { _ = $validate })*
}};
}
/// Does a match on the state struct of a widget. If `widget_state.gained_focus()` is true
/// the block is executed. This requires that `widget_state` implements [HasFocusFlag],
/// but that's the basic requirement for this whole crate.
///
/// ```rust ignore
/// use rat_focus::on_gained;
///
/// on_gained!(
/// state.field1 => {
/// // do prep
/// },
/// state.field2 => {
/// // do prep
/// }
/// );
/// ```
#[macro_export]
macro_rules! on_gained {
($($field:expr => $validate:expr),*) => {{
use $crate::HasFocusFlag;
$(if $field.gained_focus() { _ = $validate })*
}};
}
/// Does a match on the state struct of a widget. If
/// `widget_state.is_focused()` is true the block is executed.
/// There is a `_` branch too, that is evaluated if none of the
/// given widget-states has the focus.
///
/// This requires that `widget_state` implements [HasFocusFlag],
/// but that's the basic requirement for this whole crate.
///
/// ```rust ignore
/// use rat_focus::match_focus;
///
/// let res = match_focus!(
/// state.field1 => {
/// // do this
/// true
/// },
/// state.field2 => {
/// // do that
/// true
/// },
/// _ => {
/// false
/// }
/// );
///
/// if res {
/// // react
/// }
/// ```
///
#[macro_export]
macro_rules! match_focus {
($($field:expr => $block:expr),* $(, _ => $final:expr)?) => {{
use $crate::HasFocusFlag;
if false {
unreachable!();
}
$(else if $field.is_focused() { $block })*
$(else { $final })?
}};
}
impl<'a> IntoIterator for Focus<'a> {
type Item = (&'a FocusFlag, Rect);
type IntoIter = Zip<vec::IntoIter<&'a FocusFlag>, vec::IntoIter<Rect>>;
fn into_iter(self) -> Self::IntoIter {
self.focus.into_iter().zip(self.areas)
}
}
impl<'a> Focus<'a> {
/// Construct a new focus list.
pub fn new(list: &[&'a dyn HasFocusFlag]) -> Self {
let mut s = Focus::default();
for f in list {
s.focus.push(f.focus());
s.areas.push(f.area());
}
s
}
/// Construct a new focus list with an accumulator.
///
/// The accumulator might be useful if you create compound widgets.
/// Then you can have the focus state for each contained widget,
/// and an overall state for the compound.
///
/// If your compound widget contains compound widgets itself, you let
/// each of the components create their own Focus and add everything
/// together with [append](Focus::append).
///
pub fn new_accu(accu: &'a dyn HasFocusFlag, list: &[&'a dyn HasFocusFlag]) -> Self {
let mut s = Self {
accu: Some(accu.focus()),
area: accu.area(),
..Focus::default()
};
for f in list {
s.focus.push(f.focus());
s.areas.push(f.area());
}
s
}
/// Add a sub-focus cycle.
///
/// All its widgets are appended to this list. If the sub-cycle
/// has an accumulator it's added to the accumulators. All
/// sub-accumulators are appended too.
pub fn append(mut self, focus: Focus<'a>) -> Self {
for (focus, area, list) in focus.sub_accu {
self.sub_accu.push((focus, area, list));
}
if let Some(accu) = focus.accu {
self.sub_accu.push((accu, focus.area, focus.focus.clone()))
}
self.focus.extend(focus.focus);
self.areas.extend(focus.areas);
self
}
// reset flags for a new round.
fn start_focus_change(&self, set_lost: bool) {
for p in self.focus.iter() {
if set_lost {
p.lost.set(p.focus.get());
} else {
p.lost.set(false);
}
p.gained.set(false);
p.focus.set(false);
}
}
// accumulate everything
fn accumulate(&self) {
if let Some(accu) = self.accu {
accu.focus.set(false);
for p in self.focus.iter() {
if p.focus.get() {
accu.focus.set(true);
break;
}
}
}
for (f, _, list) in &self.sub_accu {
let mut any_gained = false;
let mut any_lost = false;
let mut any_focused = false;
for f in list {
any_gained |= f.gained.get();
any_lost |= f.lost.get();
any_focused |= f.focus.get();
}
f.focus.set(any_focused);
f.lost.set(any_lost && !any_gained);
f.gained.set(any_gained && !any_lost);
}
}
/// Sets the focus to the widget.
///
/// Sets focus and gained but not lost. This can be used to prevent validation of the field.
pub fn focus_no_lost(&self, flag: &FocusFlag) {
self.start_focus_change(false);
if let Some(f) = self.focus.iter().find(|f| ptr::eq(**f, flag)) {
f.focus.set(true);
f.gained.set(true);
}
self.accumulate();
}
/// Sets the focus to the widget with `tag`.
///
/// Sets the focus, gained and lost flags. If this ends up with the same widget as
/// before focus, gained and lost flag are all set.
pub fn focus(&self, flag: &FocusFlag) {
self.start_focus_change(true);
if let Some(f) = self.focus.iter().find(|f| ptr::eq(**f, flag)) {
f.focus.set(true);
f.gained.set(true);
}
self.accumulate();
}
/// Reset lost + gained flags.
/// This is done automatically in `HandleEvent::handle()` for every event.
pub fn reset_lost_gained(&self) {
for p in self.focus.iter() {
p.lost.set(false);
p.gained.set(false);
}
for (p, _, _) in self.sub_accu.iter() {
p.gained.set(false);
p.lost.set(false);
}
}
/// Change the focus.
///
/// Sets the focus, gained and lost flags.
///
/// If the field at idx has the focus all three are set.
pub fn focus_idx(&self, idx: usize) {
self.start_focus_change(true);
if let Some(f) = self.focus.get(idx) {
f.focus.set(true);
f.gained.set(true);
}
self.accumulate();
}
/// Change to focus to the given position.
///
pub fn focus_at(&self, col: u16, row: u16) -> bool {
debug!("focus {:#?}", self);
let pos = Position::new(col, row);
for (idx, area) in self.areas.iter().enumerate() {
if area.contains(pos) {
debug!("widget area {:?}", area);
self.focus_idx(idx);
return true;
}
}
for (_, area, list) in self.sub_accu.iter() {
if area.contains(pos) {
debug!("sub area {:?}", area);
if let Some(ff) = list.first() {
self.focus(ff);
return true;
}
}
}
if self.area.contains(pos) {
debug!("main area {:?}", self.area);
if let Some(ff) = self.focus.first() {
self.focus(ff);
return true;
}
}
false
}
/// Focus the next widget in the cycle.
///
/// Sets the focus, gained and lost flags. If this ends up with the same widget as
/// before focus, gained and lost flag are all set.
///
/// If no field has the focus the first one gets it.
pub fn next(&self) -> bool {
self.start_focus_change(true);
for (i, p) in self.focus.iter().enumerate() {
if p.lost.get() {
let n = next_circular(i, self.focus.len());
self.focus[n].focus.set(true);
self.focus[n].gained.set(true);
self.accumulate();
return true;
}
}
if !self.focus.is_empty() {
self.focus[0].focus.set(true);
self.focus[0].gained.set(true);
self.accumulate();
return true;
}
false
}
/// Focus the previous widget in the cycle.
///
/// Sets the focus and lost flags. If this ends up with the same widget as
/// before it returns *true* and sets the focus, gained and lost flag.
///
/// If no field has the focus the first one gets it.
pub fn prev(&self) -> bool {
self.start_focus_change(true);
for (i, p) in self.focus.iter().enumerate() {
if p.lost.get() {
let n = prev_circular(i, self.focus.len());
self.focus[n].focus.set(true);
self.focus[n].gained.set(true);
self.accumulate();
return true;
}
}
if !self.focus.is_empty() {
self.focus[0].focus.set(true);
self.focus[0].gained.set(true);
self.accumulate();
return true;
}
false
}
}
/// Next but circle around.
fn next_circular(select: usize, max: usize) -> usize {
if select + 1 < max {
select + 1
} else {
0
}
}
/// Prev but circle around.
fn prev_circular(select: usize, max: usize) -> usize {
if select > 0 {
select - 1
} else {
max - 1
}
}
impl<'a> HandleEvent<crossterm::event::Event, FocusKeys, Outcome> for Focus<'a> {
fn handle(&mut self, event: &crossterm::event::Event, _keymap: FocusKeys) -> Outcome {
match event {
ct_event!(keycode press Tab) => {
self.next();
Outcome::Changed
}
ct_event!(keycode press SHIFT-Tab) | ct_event!(keycode press SHIFT-BackTab) => {
self.prev();
Outcome::Changed
}
_ => self.handle(event, MouseOnly),
}
}
}
impl<'a> HandleEvent<crossterm::event::Event, MouseOnly, Outcome> for Focus<'a> {
fn handle(&mut self, event: &crossterm::event::Event, _keymap: MouseOnly) -> Outcome {
match event {
ct_event!(mouse down Left for column, row) => {
if self.focus_at(*column, *row) {
return Outcome::Changed;
} else {
self.reset_lost_gained();
Outcome::NotUsed
}
}
_ => {
self.reset_lost_gained();
Outcome::NotUsed
}
}
}
}