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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0
//! A tabbed stack
use crate::{Row, Stack, TextButton};
use kas::prelude::*;
use std::fmt::Debug;
#[derive(Clone, Debug)]
struct MsgSelect;
#[derive(Clone, Debug)]
struct MsgSelectIndex(usize);
/// A tab
///
/// TODO: a tab is not a button! Support directional graphics, icon and close button.
pub type Tab = TextButton;
/// A tabbed stack of boxed widgets
///
/// This is a parametrisation of [`TabStack`].
pub type BoxTabStack = TabStack<Box<dyn Widget>>;
impl_scope! {
/// A tabbed stack of widgets
///
/// A stack consists a set of child widgets, "pages", all of equal size.
/// Only a single page is visible at a time. The page is "turned" via tab
/// handles or calling [`Self::set_active`].
///
/// Type parameter `D` controls the position of tabs relative to the stack;
/// default value is [`Direction::Up`]: tabs are positioned above the stack.
/// Within the bar, items are always left-to-right
/// (TODO: support for vertical bars).
///
/// This may only be parametrised with a single widget type, thus usually
/// it will be necessary to box children (this is what [`BoxTabStack`] is).
///
/// See also the main implementing widget: [`Stack`].
#[impl_default]
#[derive(Clone, Debug)]
#[widget {
layout = list(self.direction): [
self.stack,
self.tabs,
];
}]
pub struct TabStack<W: Widget> {
core: widget_core!(),
direction: Direction = Direction::Up,
#[widget]
tabs: Row<Tab>, // TODO: want a TabBar widget for scrolling support?
#[widget]
stack: Stack<W>,
}
impl Self {
/// Construct a new, empty instance
pub fn new() -> Self {
Self {
core: Default::default(),
direction: Direction::Up,
stack: Stack::new(),
tabs: Row::new().on_message(|mgr, index| {
if let Some(MsgSelect) = mgr.try_pop_msg() {
mgr.push_msg(MsgSelectIndex(index));
}
}),
}
}
/// Set the position of tabs relative to content
///
/// Default value: [`Direction::Up`]
pub fn set_direction(&mut self, direction: Direction) -> TkAction {
self.direction = direction;
// Note: most of the time SET_SIZE would be enough, but margins can be different
TkAction::RESIZE
}
}
impl Widget for Self {
fn nav_next(&mut self,
_: &mut ConfigMgr,
reverse: bool,
from: Option<usize>,
) -> Option<usize> {
let reverse = reverse ^ !self.direction.is_reversed();
kas::util::nav_next(reverse, from, self.num_children())
}
fn handle_message(&mut self, mgr: &mut EventMgr, _: usize) {
if let Some(MsgSelectIndex(index)) = mgr.try_pop_msg() {
mgr.config_mgr(|mgr| self.set_active(mgr, index));
}
}
}
}
impl<W: Widget> TabStack<W> {
/// Limit the number of pages considered by [`Layout::size_rules`]
///
/// By default, this is `usize::MAX`: all pages affect the result. If
/// this is set to 1 then only the active page will affect the result. If
/// this is `n > 1`, then `min(n, num_pages)` pages (including active)
/// will be used. (If this is set to 0 it is silently replaced with 1.)
///
/// Using a limit lower than the number of pages has two effects:
/// (1) resizing is faster and (2) calling [`Self::set_active`] may cause a
/// full-window resize.
pub fn set_size_limit(&mut self, limit: usize) {
self.stack.set_size_limit(limit);
}
/// Get the index of the active page
#[inline]
pub fn active(&self) -> usize {
self.stack.active()
}
/// Set the active page (inline)
///
/// Unlike [`Self::set_active`], this does not update anything; it is
/// assumed that sizing happens afterwards.
#[inline]
pub fn with_active(mut self, active: usize) -> Self {
self.stack = self.stack.with_active(active);
self
}
/// Set the active page
///
/// Behaviour depends on whether [`SizeRules`] were already solved for
/// `index` (see [`Self::set_size_limit`] and note that methods like
/// [`Self::push`] do not solve rules for new pages). Case:
///
/// - `index >= num_pages`: no page displayed
/// - `index == active` and `SizeRules` were solved: nothing happens
/// - `SizeRules` were solved: set layout ([`Layout::set_rect`]) and
/// update mouse-cursor target ([`TkAction::REGION_MOVED`])
/// - Otherwise: resize the whole window ([`TkAction::RESIZE`])
pub fn set_active(&mut self, mgr: &mut ConfigMgr, index: usize) {
self.stack.set_active(mgr, index);
}
/// Get a direct reference to the active child widget, if any
pub fn get_active(&self) -> Option<&W> {
self.stack.get_active()
}
/// True if there are no pages
pub fn is_empty(&self) -> bool {
self.stack.is_empty()
}
/// Returns the number of pages
pub fn len(&self) -> usize {
self.stack.len()
}
/// Remove all pages
///
/// This does not change the active page index.
pub fn clear(&mut self) {
self.stack.clear();
self.tabs.clear();
}
/// Get a page
pub fn get(&self, index: usize) -> Option<&W> {
self.stack.get(index)
}
/// Get a page
pub fn get_mut(&mut self, index: usize) -> Option<&mut W> {
self.stack.get_mut(index)
}
/// Get a tab
pub fn get_tab(&self, index: usize) -> Option<&Tab> {
self.tabs.get(index)
}
/// Get a tab
pub fn get_tab_mut(&mut self, index: usize) -> Option<&mut Tab> {
self.tabs.get_mut(index)
}
/// Append a page (inline)
///
/// Does not configure or size child.
pub fn with_tab(mut self, tab: Tab, widget: W) -> Self {
let _ = self.stack.edit(|widgets| widgets.push(widget));
let _ = self.tabs.edit(|tabs| tabs.push(tab));
self
}
/// Append a page (inline)
///
/// Does not configure or size child.
pub fn with_title(self, title: impl Into<AccelString>, widget: W) -> Self {
self.with_tab(Tab::new_on(title, |mgr| mgr.push_msg(MsgSelect)), widget)
}
/// Append a page
///
/// The new page is configured immediately. If it becomes the active page
/// and then [`TkAction::RESIZE`] will be triggered.
///
/// Returns the new page's index.
pub fn push(&mut self, mgr: &mut ConfigMgr, tab: Tab, widget: W) -> usize {
let ti = self.tabs.push(mgr, tab);
let si = self.stack.push(mgr, widget);
debug_assert_eq!(ti, si);
si
}
/// Remove the last child widget (if any) and return
///
/// If this page was active then the previous page becomes active.
pub fn pop(&mut self, mgr: &mut ConfigMgr) -> Option<(Tab, W)> {
let tab = self.tabs.pop(mgr);
let w = self.stack.pop(mgr);
debug_assert_eq!(tab.is_some(), w.is_some());
tab.zip(w)
}
/// Inserts a child widget position `index`
///
/// Panics if `index > len`.
///
/// The new child is configured immediately. The active page does not
/// change.
pub fn insert(&mut self, mgr: &mut ConfigMgr, index: usize, tab: Tab, widget: W) {
self.tabs.insert(mgr, index, tab);
self.stack.insert(mgr, index, widget);
}
/// Removes the child widget at position `index`
///
/// Panics if `index` is out of bounds.
///
/// If the active page is removed then the previous page (if any) becomes
/// active.
pub fn remove(&mut self, mgr: &mut ConfigMgr, index: usize) -> (Tab, W) {
let tab = self.tabs.remove(mgr, index);
let stack = self.stack.remove(mgr, index);
(tab, stack)
}
/// Replace the child at `index`
///
/// Panics if `index` is out of bounds.
///
/// The new child is configured immediately. If it replaces the active page,
/// then [`TkAction::RESIZE`] is triggered.
pub fn replace(&mut self, mgr: &mut ConfigMgr, index: usize, w: W) -> W {
self.stack.replace(mgr, index, w)
}
/// Append child widgets from an iterator
///
/// New children are configured immediately. If a new page becomes active,
/// then [`TkAction::RESIZE`] is triggered.
pub fn extend<T: IntoIterator<Item = (Tab, W)>>(&mut self, mgr: &mut ConfigMgr, iter: T) {
let iter = iter.into_iter();
// let min_len = iter.size_hint().0;
// self.tabs.reserve(min_len);
// self.stack.reserve(min_len);
for (tab, w) in iter {
self.tabs.push(mgr, tab);
self.stack.push(mgr, w);
}
}
}
impl<W: Widget, T: IntoIterator<Item = (Tab, W)>> From<T> for TabStack<W> {
#[inline]
fn from(iter: T) -> Self {
let iter = iter.into_iter();
let min_len = iter.size_hint().0;
let mut stack = Vec::with_capacity(min_len);
let mut tabs = Vec::with_capacity(min_len);
for (tab, w) in iter {
stack.push(w);
tabs.push(tab);
}
Self {
stack: Stack::new_vec(stack),
tabs: Row::new_vec(tabs),
..Default::default()
}
}
}