|
| 1 | +# Configuration |
| 2 | + |
| 3 | +*Topics: themes and UI configuration* |
| 4 | + |
| 5 | +We won't build anything new this chapter. Instead, we'll take a moment to discuss configuration. |
| 6 | + |
| 7 | + |
| 8 | +## Themes |
| 9 | + |
| 10 | +Kas supports theme abstraction: widgets, for the most part, don't precisely determine their sizes or handle the minutae of drawing. |
| 11 | + |
| 12 | +Theming is abstracted and exposed to widgets through two interfaces: |
| 13 | + |
| 14 | +- [`SizeCx`] supplies widgets with size information |
| 15 | +- [`DrawCx`] is used to draw widget elements |
| 16 | + |
| 17 | +Kas currently provides three theme implementations (along with one meta-implementation): |
| 18 | + |
| 19 | +- [`kas::theme::SimpleTheme`] prioritises simplicity without loss of functionality. |
| 20 | +- [`kas::theme::FlatTheme`] extends `SimpleTheme`, putting more effort into styling while using no complex drawing techniques (well, if one doesn't count fonts). |
| 21 | +- [`kas_wgpu::ShadedTheme`] extends `FlatTheme` using shaded drawing for bevelled widget borders. The resulting styling is rather opinionated, bordering on a tech demo (it could further be adapted to e.g. use the mouse pointer as a light source instead of assuming a fixed light position, though it would quickly become apparent that the theme lacks true shadows). |
| 22 | +- [`kas::theme::MultiTheme`] supports run-time switching between pre-loaded themes. It is used by the [Gallery example]. |
| 23 | + |
| 24 | + |
| 25 | +## Configuration |
| 26 | + |
| 27 | +Previously we adjusted the font size before the UI was started: |
| 28 | +```rust |
| 29 | +# extern crate kas; |
| 30 | +# use kas::prelude::*; |
| 31 | +# fn main() -> kas::runner::Result<()> { |
| 32 | + let theme = kas::theme::SimpleTheme::new(); |
| 33 | + let mut app = kas::runner::Runner::with_theme(theme).build(())?; |
| 34 | + let _ = app.config_mut().font.set_size(24.0); |
| 35 | +# } |
| 36 | +``` |
| 37 | + |
| 38 | +Various aspects of fonts, themes, event handling and shortcuts may be adjusted here; see the [`Config`] struct. |
| 39 | + |
| 40 | +The above snippet adjusts the default configuration before the UI is started using [`Runner::config_mut`]. The returned [`Action`] is discarded (`let _ =`) since the UI has not yet been started. |
| 41 | + |
| 42 | +Configuration may also be accessed at run-time ([`EventState::config`]) and adjusted using [`WindowConfig::update_base`], though this has some limitations; in particular fonts are not re-selected and new widget sizes are not fully realized without manual resizing of the window. |
| 43 | + |
| 44 | +Pre-launch, one may supply a configuration factory through [`Builder::with_config`]. More specifically, this allows using a [`ReadWriteFactory`] to persist configuration to/from local storage. |
| 45 | + |
| 46 | + |
| 47 | +[`Runner::config_mut`]: https://docs.rs/kas/latest/kas/runner/struct.Runner.html#method.config_mut |
| 48 | +[`Action`]: https://docs.rs/kas/latest/kas/struct.Action.html |
| 49 | +[`EventState::config`]: https://docs.rs/kas/latest/kas/event/struct.EventState.html#method.config |
| 50 | +[`WindowConfig::update_base`]: https://docs.rs/kas/latest/kas/config/struct.WindowConfig.html#method.update_base |
| 51 | +[`ReadWriteFactory`]: https://docs.rs/kas/latest/kas/config/struct.ReadWriteFactory.html |
| 52 | +[`Builder::with_config`]: https://docs.rs/kas/latest/kas/runner/struct.Builder.html#method.with_config |
| 53 | +[Gallery example]: https://github.com/kas-gui/kas/tree/master/examples#gallery |
| 54 | +[`Config`]: https://docs.rs/kas/latest/kas/config/struct.Config.html |
| 55 | +[`SizeCx`]: https://docs.rs/kas/latest/kas/theme/struct.SizeCx.html |
| 56 | +[`DrawCx`]: https://docs.rs/kas/latest/kas/theme/struct.DrawCx.html |
| 57 | +[`kas::theme::SimpleTheme`]: https://docs.rs/kas/latest/kas/theme/struct.SimpleTheme.html |
| 58 | +[`kas::theme::FlatTheme`]: https://docs.rs/kas/latest/kas/theme/struct.FlatTheme.html |
| 59 | +[`kas::theme::MultiTheme`]: https://docs.rs/kas/latest/kas/theme/struct.MultiTheme.html |
| 60 | +[`kas_wgpu::ShadedTheme`]: https://docs.rs/kas-wgpu/latest/kas_wgpu/struct.ShadedTheme.html |
0 commit comments