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
// 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
//! Toggle widgets
use super::AccelLabel;
use kas::event::UpdateId;
use kas::prelude::*;
use kas::theme::Feature;
use std::cell::RefCell;
use std::fmt::Debug;
use std::rc::Rc;
use std::time::Instant;
/// A group used by [`RadioButton`] and [`RadioBox`]
///
/// This type can (and likely should) be default constructed.
#[derive(Clone, Debug)]
pub struct RadioGroup(Rc<(UpdateId, RefCell<Option<WidgetId>>)>);
impl RadioGroup {
/// Construct a new, unique group
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self(Rc::new((UpdateId::new(), Default::default())))
}
/// Access update identifier
///
/// Data updates via this [`RadioGroup`] are triggered using this [`UpdateId`].
pub fn id(&self) -> UpdateId {
(self.0).0
}
/// Get the active [`RadioBox`], if any
///
/// Note: this is never equal to a [`RadioButton`]'s [`WidgetId`], but may
/// be a descendant (test with [`WidgetExt::is_ancestor_of`]).
pub fn get(&self) -> Option<WidgetId> {
(self.0).1.borrow().clone()
}
fn update(&self, mgr: &mut EventMgr, item: Option<WidgetId>) {
*(self.0).1.borrow_mut() = item;
mgr.update_with_id((self.0).0, 0);
}
}
impl_scope! {
/// A bare radio box (no label)
///
/// See also [`RadioButton`] which includes a label.
#[autoimpl(Debug ignore self.on_select)]
#[derive(Clone)]
#[widget {
navigable = true;
hover_highlight = true;
}]
pub struct RadioBox {
core: widget_core!(),
align: AlignPair,
state: bool,
last_change: Option<Instant>,
group: RadioGroup,
on_select: Option<Rc<dyn Fn(&mut EventMgr)>>,
}
impl Widget for Self {
fn handle_event(&mut self, mgr: &mut EventMgr, event: Event) -> Response {
match event {
Event::Update { id, .. } if id == self.group.id() => {
if self.state && !self.eq_id(self.group.get()) {
log::trace!("handle_event: unset {}", self.id());
self.state = false;
self.last_change = Some(Instant::now());
mgr.redraw(self.id());
}
Response::Used
}
event => event.on_activate(mgr, self.id(), |mgr| {
if self.select(mgr) {
if let Some(f) = self.on_select.as_ref() {
f(mgr);
}
}
Response::Used
}),
}
}
}
impl Layout for Self {
fn size_rules(&mut self, size_mgr: SizeMgr, axis: AxisInfo) -> SizeRules {
self.align.set_component(axis, axis.align_or_center());
size_mgr.feature(Feature::RadioBox, axis)
}
fn set_rect(&mut self, mgr: &mut ConfigMgr, rect: Rect) {
let rect = mgr.align_feature(Feature::RadioBox, rect, self.align);
self.core.rect = rect;
}
fn draw(&mut self, mut draw: DrawMgr) {
draw.radio_box(self.rect(), self.state, self.last_change);
}
}
impl Self {
/// Construct a radio box
///
/// All instances of [`RadioBox`] and [`RadioButton`] constructed over the
/// same `group` will be considered part of a single group.
#[inline]
pub fn new(group: RadioGroup) -> Self {
RadioBox {
core: Default::default(),
align: Default::default(),
state: false,
last_change: None,
group,
on_select: None,
}
}
/// Set event handler `f`
///
/// When the radio box is selected, the closure `f` is called.
///
/// No handler is called on deselection.
#[inline]
#[must_use]
pub fn on_select<F>(self, f: F) -> RadioBox
where
F: Fn(&mut EventMgr) + 'static,
{
RadioBox {
core: self.core,
align: self.align,
state: self.state,
last_change: self.last_change,
group: self.group,
on_select: Some(Rc::new(f)),
}
}
/// Construct a radio box with given `group` and event handler `f`
///
/// All instances of [`RadioBox`] and [`RadioButton`] constructed over the
/// same `group` will be considered part of a single group.
///
/// When the radio box is selected, the closure `f` is called.
///
/// No handler is called on deselection.
#[inline]
pub fn new_on<F>(group: RadioGroup, f: F) -> Self
where
F: Fn(&mut EventMgr) + 'static,
{
RadioBox::new(group).on_select(f)
}
/// Set the initial state of the radio box.
#[inline]
#[must_use]
pub fn with_state(mut self, state: bool) -> Self {
self.state = state;
self.last_change = None;
self
}
/// Select this radio box from the group
///
/// This radio box will be set true while all others from the group will
/// be set false. Returns true if newly selected, false if already selected.
///
/// This does not call the event handler set by [`Self::on_select`] or [`Self::new_on`].
pub fn select(&mut self, mgr: &mut EventMgr) -> bool {
if !self.state {
log::trace!("select: {}", self.id());
self.state = true;
self.last_change = Some(Instant::now());
mgr.redraw(self.id());
self.group.update(mgr, Some(self.id()));
true
} else {
false
}
}
/// Unset all radio boxes in the group
///
/// Note: state will not update until the next draw.
#[inline]
pub fn unset_all(&self, mgr: &mut EventMgr) {
self.group.update(mgr, None);
}
}
impl HasBool for Self {
fn get_bool(&self) -> bool {
self.state
}
fn set_bool(&mut self, state: bool) -> TkAction {
if state == self.state {
return TkAction::empty();
}
self.state = state;
self.last_change = None;
TkAction::REDRAW
}
}
}
impl_scope! {
/// A radio button with label
///
/// See also [`RadioBox`] which excludes the label.
#[autoimpl(Debug)]
#[autoimpl(HasBool using self.inner)]
#[derive(Clone)]
#[widget{
layout = list(self.direction()): [self.inner, non_navigable: self.label];
}]
pub struct RadioButton {
core: widget_core!(),
#[widget]
inner: RadioBox,
#[widget]
label: AccelLabel,
}
impl Layout for Self {
fn set_rect(&mut self, mgr: &mut ConfigMgr, rect: Rect) {
<Self as kas::layout::AutoLayout>::set_rect(self, mgr, rect);
let dir = self.direction();
crate::check_box::shrink_to_text(&mut self.core.rect, dir, &self.label);
}
fn find_id(&mut self, coord: Coord) -> Option<WidgetId> {
self.rect().contains(coord).then(|| self.inner.id())
}
}
impl Widget for Self {
fn configure(&mut self, mgr: &mut ConfigMgr) {
mgr.add_accel_keys(self.inner.id_ref(), self.label.keys());
}
}
impl Self {
/// Construct a radio button with a given `label` and `group`
///
/// RadioButton labels are optional; if no label is desired, use an empty
/// string or use [`RadioBox`] instead.
///
/// All instances of [`RadioBox`] and [`RadioButton`] constructed over the
/// same `group` will be considered part of a single group.
#[inline]
pub fn new<T: Into<AccelString>>(label: T, group: RadioGroup) -> Self {
RadioButton {
core: Default::default(),
inner: RadioBox::new(group),
label: AccelLabel::new(label.into()),
}
}
/// Set event handler `f`
///
/// When the radio button is selected, the closure `f` is called.
///
/// No handler is called on deselection.
#[inline]
#[must_use]
pub fn on_select<F>(self, f: F) -> RadioButton
where
F: Fn(&mut EventMgr) + 'static,
{
RadioButton {
core: self.core,
inner: self.inner.on_select(f),
label: self.label,
}
}
/// Construct a radio button with given `label`, `group` and event handler `f`
///
/// RadioButton labels are optional; if no label is desired, use an empty
/// string.
///
/// All instances of [`RadioBox`] and [`RadioButton`] constructed over the
/// same `group` will be considered part of a single group.
///
/// When the radio button is selected, the closure `f` is called.
///
/// No handler is called on deselection.
#[inline]
pub fn new_on<T: Into<AccelString>, F>(label: T, group: RadioGroup, f: F) -> Self
where
F: Fn(&mut EventMgr) + 'static,
{
RadioButton::new(label, group).on_select(f)
}
/// Construct a radio button with given `label`, `group` and payload `msg`
///
/// RadioButton labels are optional; if no label is desired, use an empty
/// string.
///
/// All instances of [`RadioBox`] and [`RadioButton`] constructed over the
/// same `group` will be considered part of a single group.
///
/// When the radio button is selected, a clone
/// of `msg` is returned to the parent widget via [`EventMgr::push_msg`].
///
/// No handler is called on deselection.
#[inline]
pub fn new_msg<S, M: Clone>(label: S, group: RadioGroup, msg: M) -> Self
where
S: Into<AccelString>,
M: Clone + Debug + 'static,
{
Self::new_on(label, group, move |mgr| mgr.push_msg(msg.clone()))
}
/// Set the initial state of the radio button.
#[inline]
#[must_use]
pub fn with_state(mut self, state: bool) -> Self {
self.inner = self.inner.with_state(state);
self
}
/// Select this radio box from the group
///
/// This radio box will be set true while all others from the group will
/// be set false. Returns true if newly selected, false if already selected.
///
/// This does not call the event handler set by [`Self::on_select`] or [`Self::new_on`].
#[inline]
pub fn select(&mut self, mgr: &mut EventMgr) -> bool {
self.inner.select(mgr)
}
/// Unset all radio boxes in the group
///
/// Note: state will not update until the next draw.
#[inline]
pub fn unset_all(&self, mgr: &mut EventMgr) {
self.inner.unset_all(mgr)
}
fn direction(&self) -> Direction {
match self.label.text().text_is_rtl() {
Ok(false) | Err(_) => Direction::Right,
Ok(true) => Direction::Left,
}
}
}
}