kas_view/lib.rs
1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License in the LICENSE-APACHE file or at:
4// https://www.apache.org/licenses/LICENSE-2.0
5
6//! # Views
7//!
8//! Views allow virtual scrolling and query views over a data set, supporting
9//! both sync and async access.
10//!
11//! Each visible data `Item` is assigned a **view widget**, with dynamic
12//! re-assignment as the view changes.
13//!
14//! ## Data sets and clerks
15//!
16//! The full data set might be available in local memory, on disk, or on a
17//! remote server.
18//!
19//! A [`DataClerk`] manages all interactions between the view and the data as
20//! well as providing a local cache of (at least) the currently visible data.
21//!
22//! ### Data generators
23//!
24//! A higher-level interface is provided for data generators: [`DataGenerator`]
25//! using clerk [`GeneratorClerk`].
26//!
27//! ## View controller
28//!
29//! This crate provides the following **view controllers**:
30//!
31//! - [`ListView`] constructs a row or column view over items indexed by type `usize`
32//! - [`GridView`] constructs a table over items indexed by type `(u32, u32)`
33//!
34//! ## Driver
35//!
36//! A view controller uses a **driver** to construct and re-assign view widgets.
37//! Simple types (strings and numbers) may use a pre-defined [`driver`],
38//! otherwise a custom implementation of [`Driver`] is required.
39
40#![cfg_attr(docsrs, feature(doc_auto_cfg))]
41
42use kas::Id;
43use std::borrow::Borrow;
44
45mod data_clerk;
46mod generator;
47
48pub use data_clerk::*;
49pub use generator::*;
50
51pub mod filter;
52
53pub mod driver;
54pub use driver::Driver;
55
56mod list_view;
57pub use list_view::ListView;
58
59mod grid_view;
60pub use grid_view::{GridIndex, GridView};
61
62/// A pair which may be borrowed over the first item
63#[derive(Debug, Default)]
64pub struct Token<K, I> {
65 pub key: K,
66 pub item: I,
67}
68
69impl<K, I> Token<K, I> {
70 /// Construct
71 #[inline]
72 pub fn new(key: K, item: I) -> Self {
73 Token { key, item }
74 }
75}
76
77impl<K, I> Borrow<K> for Token<K, I> {
78 fn borrow(&self) -> &K {
79 &self.key
80 }
81}
82
83/// Used to notify selection and deselection of [`ListView`] and [`GridView`] children
84#[derive(Clone, Debug)]
85pub enum SelectionMsg<K> {
86 /// Selection of item
87 Select(K),
88 /// Deselection of item
89 ///
90 /// Note: not emitted due to selection of another item in single-item selection mode.
91 Deselect(K),
92}
93
94/// Selection mode used by [`ListView`]
95#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
96pub enum SelectionMode {
97 /// Disable selection
98 #[default]
99 None,
100 /// Support single-item selection. Selecting another item automatically
101 /// clears the prior selection (without sending [`SelectionMsg::Deselect`]).
102 Single,
103 /// Support multi-item selection.
104 Multiple,
105}
106
107#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
108enum Update {
109 None,
110 Token,
111 Configure,
112}