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 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
#![doc = include_str!("../readme.md")]
#[allow(unused_imports)]
use log::debug;
use rat_event::{ct_event, FocusKeys, HandleEvent, MouseOnly, Outcome};
use ratatui::layout::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::{
crossterm, ct_event, flow, flow_ok, util, ConsumedEvent, FocusKeys, HandleEvent, MouseOnly,
Outcome,
};
}
/// 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 {
/// Field name for debugging purposes.
pub name: Cell<&'static str>,
/// 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()
}
}
/// Is this a container widget of sorts.
pub trait HasFocus {
/// Returns a Focus struct.
fn focus(&self) -> Focus<'_>;
}
impl Debug for FocusFlag {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FocusFlag")
.field("name", &self.name.get())
.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> {
/// Name for the cycle.
name: String,
/// Focus logging
log: Cell<bool>,
/// 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::add_focus]. 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);
}
/// Set the field-name.
#[inline]
pub fn set_name(&self, name: &'static str) {
self.name.set(name);
}
/// Get the field-name.
#[inline]
pub fn name(&self) -> &'static str {
self.name.get()
}
/// 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()
}
/// Reset all flags to false.
#[inline]
pub fn clear(&self) {
self.focus.set(false);
self.lost.set(false);
self.gained.set(false);
}
}
/// 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 for a container widget.
///
/// The focus-flag for the container accumulates all the flags.
/// If any has focus, the container has the focus too.
/// Focus-gained and focus-lost are only set if the focus
/// leaves the container.
///
/// The container widget itself interacts with the mouse too.
/// If no single widget is hit with the mouse, but the area of
/// the container is, the first widget gets the focus.
///
/// See `examples/focus_recursive` and `examples/focus_recursive2`
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
}
/// Construct a new focus list with group accumulator.
///
/// This is meant for some loose grouping of widgets, for which
/// you want an overview.
///
/// The same rules apply as for new_accu(), but with this one
/// there is no overall area for mouse interaction.
pub fn new_grp(accu: &'a FocusFlag, list: &[&'a dyn HasFocusFlag]) -> Self {
let mut s = Self {
accu: Some(accu),
area: Rect::default(),
..Focus::default()
};
for f in list {
s.focus.push(f.focus());
s.areas.push(f.area());
}
s
}
/// Add a single widget.
pub fn add_flag(&mut self, flag: &'a FocusFlag, area: Rect) -> &mut Self {
self.focus.push(flag);
self.areas.push(area);
self
}
/// 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 sub-accumulators. All
/// sub-sub-accumulators are appended too.
pub fn add_focus(&mut self, focus: Focus<'a>) -> &mut 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
}
/// Add a container widget.
pub fn add_container(&mut self, container: &'a dyn HasFocus) -> &mut Self {
self.add_focus(container.focus());
self
}
/// Add a single widget.
pub fn add(&mut self, f: &'a dyn HasFocusFlag) -> &mut Self {
self.focus.push(f.focus());
self.areas.push(f.area());
self
}
/// Add a list of widgets.
pub fn add_all(&mut self, list: &[&'a dyn HasFocusFlag]) -> &mut Self {
for f in list {
self.focus.push(f.focus());
self.areas.push(f.area());
}
self
}
/// Writes a log for each operation.
pub fn enable_log(&self, log: bool) {
self.log.set(log)
}
/// Set a name for debugging.
pub fn set_name(&mut self, name: impl Into<String>) {
self.name = name.into();
}
/// Name for debugging.
pub fn name(&self) -> &str {
self.name.as_str()
}
/// Set the initial state for all widgets.
/// This ensures that there is only one focused widget.
/// The first widget in the list gets the focus.
pub fn init(&self) {
if self.log.get() {
debug!("init focus");
}
self.core_init();
}
/// Clears the focus state for all widgets.
/// This is useful, if part of your widgets are temporarily
/// exempt from focus handling, and should therefore not
/// have any focus-flags set to avoid problems with
/// event-handling.
pub fn clear(&self) {
if self.log.get() {
debug!("clear focus");
}
self.core_clear();
}
/// 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_widget_no_lost(&self, state: &'a dyn HasFocusFlag) {
let flag = state.focus();
self.focus_no_lost(flag);
}
/// 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_widget(&self, state: &'a dyn HasFocusFlag) {
let flag = state.focus();
self.focus(flag);
}
/// 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) {
if self.log.get() {
debug!("focus_no_lost {:?}", flag);
}
self.core_focus_no_lost(flag);
}
/// 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 not set.
pub fn focus(&self, flag: &FocusFlag) {
if self.log.get() {
debug!("focus {:?}", flag);
}
self.core_focus(flag);
}
/// Returns the focused widget as FocusFlag.
///
/// This is mainly for debugging purposes.
/// For control-flow [match_focus] or [on_gained] or [on_lost]
/// will be nicer.
pub fn focused(&self) -> Option<&'a FocusFlag> {
self.core_focused()
}
/// Returns the widget that lost the focus as FocusFlag.
///
/// This is mainly for debugging purposes.
/// For control-flow [match_focus] or [on_gained] or [on_lost]
/// will be nicer.
pub fn lost_focus(&self) -> Option<&'a FocusFlag> {
self.core_lost_focus()
}
/// Returns the widget that gained the focus as FocusFlag.
///
/// This is mainly for debugging purposes.
/// For control-flow [match_focus] or [on_gained] or [on_lost]
/// will be nicer.
pub fn gained_focus(&self) -> Option<&'a FocusFlag> {
self.core_gained_focus()
}
/// Reset lost + gained flags.
/// This is done automatically in `HandleEvent::handle()` for every event.
pub fn reset_lost_gained(&self) {
if self.log.get() {
debug!("reset_lost_gained");
}
self.core_reset_lost_gained();
}
/// 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) {
if self.log.get() {
debug!("focus_idx {}", idx);
}
self.core_focus_idx(idx);
}
/// Change to focus to the given position.
///
pub fn focus_at(&self, col: u16, row: u16) -> bool {
if self.log.get() {
debug!("focus_at {},{}", col, row);
}
self.core_focus_at(col, row)
}
/// 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 {
if self.log.get() {
debug!("next {:?}", self.core_focused());
}
self.core_next()
}
/// 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 {
if self.log.get() {
debug!("prev {:?}", self.core_focused());
}
self.core_prev()
}
}
impl<'a> Focus<'a> {
// reset flags for a new round.
fn core_start_focus_change(&self, set_lost: bool) {
if self.log.get() {
debug!("start_focus_change {}", set_lost);
}
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 core_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);
}
}
fn core_init(&self) {
self.core_start_focus_change(false);
if let Some(first) = self.focus.first() {
first.focus.set(true);
}
}
fn core_clear(&self) {
if let Some(v) = self.accu {
v.clear()
}
for f in &self.focus {
f.clear();
}
for (f, _, _) in &self.sub_accu {
f.clear();
}
}
fn core_focus_no_lost(&self, flag: &FocusFlag) {
self.core_start_focus_change(false);
if let Some(f) = self.focus.iter().find(|f| ptr::eq(**f, flag)) {
f.focus.set(true);
f.gained.set(false);
}
self.core_accumulate();
}
fn core_focus(&self, flag: &FocusFlag) {
self.core_start_focus_change(true);
if let Some(f) = self.focus.iter().find(|f| ptr::eq(**f, flag)) {
f.focus.set(true);
if f.lost.get() {
// lost == gained -> noop
f.lost.set(false);
} else {
f.gained.set(true);
}
}
self.core_accumulate();
}
fn core_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);
}
}
fn core_focus_idx(&self, idx: usize) {
self.core_start_focus_change(true);
if let Some(f) = self.focus.get(idx) {
f.focus.set(true);
if f.lost.get() {
// lost == gained -> noop
f.lost.set(false);
} else {
f.gained.set(true);
}
}
self.core_accumulate();
}
fn core_focus_at(&self, col: u16, row: u16) -> bool {
let pos = (col, row);
for (idx, area) in self.areas.iter().enumerate() {
if area.contains(pos.into()) {
if self.log.get() {
debug!("found area for {:?}", self.focus[idx]);
}
self.focus_idx(idx);
return true;
}
}
for (accu, area, list) in self.sub_accu.iter() {
if area.contains(pos.into()) {
if let Some(ff) = list.first() {
if self.log.get() {
debug!("focus container {:?}", accu);
}
self.core_focus(ff);
return true;
}
}
}
if self.area.contains(pos.into()) {
if self.log.get() {
debug!("focus container {:?}", self.accu);
}
if let Some(ff) = self.focus.first() {
self.core_focus(ff);
return true;
}
}
false
}
fn core_next(&self) -> bool {
self.core_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.core_accumulate();
return true;
}
}
if !self.focus.is_empty() {
self.focus[0].focus.set(true);
self.focus[0].gained.set(true);
self.core_accumulate();
return true;
}
false
}
fn core_prev(&self) -> bool {
self.core_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.core_accumulate();
return true;
}
}
if !self.focus.is_empty() {
self.focus[0].focus.set(true);
self.focus[0].gained.set(true);
self.core_accumulate();
return true;
}
false
}
fn core_focused(&self) -> Option<&'a FocusFlag> {
self.focus.iter().find(|v| v.get()).copied()
}
fn core_lost_focus(&self) -> Option<&'a FocusFlag> {
self.focus.iter().find(|v| v.lost()).copied()
}
fn core_gained_focus(&self) -> Option<&'a FocusFlag> {
self.focus.iter().find(|v| v.gained()).copied()
}
}
/// 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.saturating_sub(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) => {
if self.log.get() {
debug!("Tab {:?}", self.core_focused());
}
self.core_next();
if self.log.get() {
debug!("=> {:?}", self.core_focused());
}
Outcome::Changed
}
ct_event!(keycode press SHIFT-Tab) | ct_event!(keycode press SHIFT-BackTab) => {
if self.log.get() {
debug!("BackTab {:?}", self.core_focused());
}
self.core_prev();
if self.log.get() {
debug!("=> {:?}", self.core_focused());
}
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.log.get() {
debug!("mouse down {},{}", column, row);
}
if self.core_focus_at(*column, *row) {
if self.log.get() {
debug!("=> {:?}", self.core_focused());
}
Outcome::Changed
} else {
if self.log.get() {
debug!("=> None");
}
self.core_reset_lost_gained();
Outcome::NotUsed
}
}
_ => {
self.core_reset_lost_gained();
Outcome::NotUsed
}
}
}
}