feat: MultiSelect support for segmented buttons
This commit is contained in:
parent
2c309a67a2
commit
2fcfb84484
9 changed files with 263 additions and 112 deletions
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
use derive_setters::Setters;
|
||||
use slotmap::{SecondaryMap, SlotMap};
|
||||
use std::borrow::Cow;
|
||||
use std::{borrow::Cow, collections::HashSet};
|
||||
|
||||
use crate::widget::IconSource;
|
||||
|
||||
|
|
@ -13,15 +13,15 @@ slotmap::new_key_type! {
|
|||
}
|
||||
|
||||
/// Contains all state for interacting with a segmented button.
|
||||
pub struct State<Data> {
|
||||
pub struct State<Selection, Data> {
|
||||
/// State that is shared with widget drawing.
|
||||
pub inner: SharedWidgetState,
|
||||
pub inner: SharedWidgetState<Selection>,
|
||||
|
||||
/// State unique to the application.
|
||||
pub data: SecondaryState<Data>,
|
||||
}
|
||||
|
||||
impl<Data> Default for State<Data> {
|
||||
impl<Selection: Default, Data> Default for State<Selection, Data> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
inner: SharedWidgetState::default(),
|
||||
|
|
@ -32,43 +32,97 @@ impl<Data> Default for State<Data> {
|
|||
|
||||
/// State which is most useful to the widget.
|
||||
#[derive(Default)]
|
||||
pub struct SharedWidgetState {
|
||||
pub struct SharedWidgetState<Variant> {
|
||||
/// The content used for drawing segmented buttons.
|
||||
pub buttons: SlotMap<Key, Content>,
|
||||
|
||||
/// The actively-selected segmented button.
|
||||
pub active: Key,
|
||||
/// Manages selections
|
||||
pub selection: Variant,
|
||||
}
|
||||
|
||||
/// State which is most useful to the application.
|
||||
pub type SecondaryState<Data> = SecondaryMap<Key, Data>;
|
||||
|
||||
impl<Data> State<Data> {
|
||||
#[must_use]
|
||||
pub fn builder() -> Builder<Data> {
|
||||
Builder(Self::default())
|
||||
impl<Data> State<SingleSelect, Data> {
|
||||
pub fn activate(&mut self, key: Key) {
|
||||
self.inner.selection.activate(key);
|
||||
}
|
||||
|
||||
/// Activates this button.
|
||||
pub fn activate(&mut self, key: Key) {
|
||||
self.inner.active = key;
|
||||
pub fn deactivate(&mut self, key: Key) {
|
||||
self.inner.selection.deactivate(key);
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_active(&self, key: Key) -> bool {
|
||||
self.inner.selection.is_active(key)
|
||||
}
|
||||
|
||||
/// The ID of the active button.
|
||||
#[must_use]
|
||||
pub fn active(&self) -> Key {
|
||||
self.inner.active
|
||||
self.inner.selection.active
|
||||
}
|
||||
|
||||
/// Get the application data for the active button.
|
||||
#[must_use]
|
||||
pub fn active_data(&self) -> Option<&Data> {
|
||||
self.data(self.active())
|
||||
self.data.get(self.inner.selection.active)
|
||||
}
|
||||
|
||||
/// Mutable application data for the active button.
|
||||
#[must_use]
|
||||
pub fn active_data_mut(&mut self) -> Option<&mut Data> {
|
||||
self.data.get_mut(self.inner.selection.active)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Data> State<MultiSelect, Data> {
|
||||
pub fn activate(&mut self, key: Key) {
|
||||
if self.inner.selection.is_active(key) {
|
||||
self.inner.selection.deactivate(key);
|
||||
} else {
|
||||
self.inner.selection.activate(key);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn deactivate(&mut self, key: Key) {
|
||||
self.inner.selection.deactivate(key);
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn is_active(&self, key: Key) -> bool {
|
||||
self.inner.selection.is_active(key)
|
||||
}
|
||||
|
||||
/// The IDs of the active buttons.
|
||||
pub fn active(&self) -> impl Iterator<Item = Key> + '_ {
|
||||
self.inner.selection.active.iter().copied()
|
||||
}
|
||||
|
||||
/// Get the application data for the active buttons.
|
||||
pub fn active_data(&self) -> impl Iterator<Item = (Key, &Data)> {
|
||||
self.inner.buttons.keys().filter_map(|key| {
|
||||
if self.inner.selection.is_active(key) {
|
||||
self.data.get(key).map(|data| (key, data))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// State which is most useful to the application.
|
||||
pub type SecondaryState<Data> = SecondaryMap<Key, Data>;
|
||||
|
||||
impl<Selection, Data> State<Selection, Data>
|
||||
where
|
||||
Selection: Selectable,
|
||||
{
|
||||
#[must_use]
|
||||
pub fn builder() -> Builder<Selection, Data> {
|
||||
Builder(Self::default())
|
||||
}
|
||||
|
||||
/// Convenience method for batching multiple operations
|
||||
#[must_use]
|
||||
pub fn batch(&mut self) -> Batch<Data> {
|
||||
pub fn batch(&mut self) -> Batch<Selection, Data> {
|
||||
Batch(self)
|
||||
}
|
||||
|
||||
|
|
@ -106,17 +160,104 @@ impl<Data> State<Data> {
|
|||
/// Inserts and activates a button.
|
||||
pub fn insert_active(&mut self, content: impl Into<Content>, data: Data) -> Key {
|
||||
let key = self.insert(content, data);
|
||||
self.activate(key);
|
||||
self.inner.selection.activate(key);
|
||||
key
|
||||
}
|
||||
|
||||
/// Removes a button.
|
||||
pub fn remove(&mut self, key: Key) -> Option<Data> {
|
||||
self.inner.buttons.remove(key);
|
||||
self.inner.selection.deactivate(key);
|
||||
self.data.remove(key)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Selectable: Default {
|
||||
fn activate(&mut self, key: Key);
|
||||
|
||||
fn deactivate(&mut self, key: Key);
|
||||
|
||||
fn is_active(&self, key: Key) -> bool;
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct SingleSelect {
|
||||
pub active: Key,
|
||||
}
|
||||
|
||||
impl Selectable for SingleSelect {
|
||||
fn activate(&mut self, key: Key) {
|
||||
self.active = key;
|
||||
}
|
||||
|
||||
fn deactivate(&mut self, _key: Key) {
|
||||
self.active = Key::default();
|
||||
}
|
||||
|
||||
fn is_active(&self, key: Key) -> bool {
|
||||
self.active == key
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MultiSelect {
|
||||
pub active: HashSet<Key>,
|
||||
}
|
||||
|
||||
impl Selectable for MultiSelect {
|
||||
fn activate(&mut self, key: Key) {
|
||||
self.active.insert(key);
|
||||
}
|
||||
|
||||
fn deactivate(&mut self, key: Key) {
|
||||
self.active.remove(&key);
|
||||
}
|
||||
|
||||
fn is_active(&self, key: Key) -> bool {
|
||||
self.active.contains(&key)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Builder<Selection, Data>(State<Selection, Data>);
|
||||
|
||||
impl<Selection: Selectable, Data> Builder<Selection, Data> {
|
||||
pub fn insert(mut self, content: impl Into<Content>, data: Data) -> Self {
|
||||
self.0.insert(content, data);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn insert_active(mut self, content: impl Into<Content>, data: Data) -> Self {
|
||||
self.0.insert_active(content, data);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> State<Selection, Data> {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience type for batching multiple operations
|
||||
pub struct Batch<'a, Selection, Data>(&'a mut State<Selection, Data>);
|
||||
|
||||
impl<'a, Selection: Selectable, Data> Batch<'a, Selection, Data> {
|
||||
/// Insert a new button.
|
||||
pub fn insert(self, content: impl Into<Content>, data: Data) -> Self {
|
||||
self.0.insert(content, data);
|
||||
self
|
||||
}
|
||||
|
||||
/// Inserts and activates a button.
|
||||
pub fn insert_active(self, content: impl Into<Content>, data: Data) -> Self {
|
||||
self.0.insert_active(content, data);
|
||||
self
|
||||
}
|
||||
|
||||
/// Removes a button.
|
||||
pub fn remove(&mut self, key: Key) {
|
||||
self.0.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
/// Data to be drawn in a segmented button.
|
||||
#[derive(Default, Setters)]
|
||||
pub struct Content {
|
||||
|
|
@ -149,43 +290,3 @@ impl From<Cow<'static, str>> for Content {
|
|||
Content::default().text(text)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Builder<Data>(State<Data>);
|
||||
|
||||
impl<Data> Builder<Data> {
|
||||
pub fn insert(mut self, content: impl Into<Content>, data: Data) -> Self {
|
||||
self.0.insert(content, data);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn insert_active(mut self, content: impl Into<Content>, data: Data) -> Self {
|
||||
self.0.insert_active(content, data);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> State<Data> {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
/// Convenience type for batching multiple operations
|
||||
pub struct Batch<'a, Data>(&'a mut State<Data>);
|
||||
|
||||
impl<'a, Data> Batch<'a, Data> {
|
||||
/// Insert a new button.
|
||||
pub fn insert(self, content: impl Into<Content>, data: Data) -> Self {
|
||||
self.0.insert(content, data);
|
||||
self
|
||||
}
|
||||
|
||||
/// Inserts and activates a button.
|
||||
pub fn insert_active(self, content: impl Into<Content>, data: Data) -> Self {
|
||||
self.0.insert_active(content, data);
|
||||
self
|
||||
}
|
||||
|
||||
/// Removes a button.
|
||||
pub fn remove(&mut self, key: Key) {
|
||||
self.0.remove(key);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue