kas_core/
classes.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//! Class-specific widget traits
7//!
8//! These traits provide generic ways to interact with common widget properties,
9//! e.g. to read the text of a `Label` or set the state of a `CheckBox`.
10
11use crate::Action;
12
13/// Read / write a boolean value
14///
15/// The value `true` means *checked*, *selected* or *toggled on*.
16pub trait HasBool {
17    /// Get the widget's state
18    fn get_bool(&self) -> bool;
19
20    /// Set the widget's state
21    fn set_bool(&mut self, state: bool) -> Action;
22}
23
24/// Read an unformatted `&str`
25///
26/// For write-support, see [`HasString`]. Alternatively, for e.g.
27/// `Label<&'static str>`, the `set_text` method which may be used, but in
28/// practice this is rarely sufficient.
29pub trait HasStr {
30    /// Get text by reference
31    fn get_str(&self) -> &str;
32
33    /// Get text as a `String`
34    #[inline]
35    fn get_string(&self) -> String {
36        self.get_str().to_string()
37    }
38}
39
40/// Read / write an unformatted `String`
41pub trait HasString: HasStr {
42    /// Set text from a `&str`
43    ///
44    /// This is a convenience method around `set_string(text.to_string())`.
45    #[inline]
46    fn set_str(&mut self, text: &str) -> Action {
47        self.set_string(text.to_string())
48    }
49
50    /// Set text from a string
51    fn set_string(&mut self, text: String) -> Action;
52}
53
54/*TODO: HasHtml with get and set?
55/// Read / write a formatted `String`
56pub trait HasFormatted {
57    /// Get text as a `String`
58    fn get_formatted(&self) -> FormattedString;
59
60    /// Set from a formatted string
61    fn set_formatted<S: Into<FormattedString>>(&mut self, text: S) -> Action
62    where
63        Self: Sized,
64    {
65        self.set_formatted_string(text.into())
66    }
67
68    /// Set from a formatted string
69    fn set_formatted_string(&mut self, text: FormattedString) -> Action;
70}
71*/