Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Configuration

Topics: themes and UI configuration

We won't build anything new this chapter. Instead, we'll take a moment to discuss configuration.

Themes

Kas supports theme abstraction: widgets, for the most part, don't precisely determine their sizes or handle the minutae of drawing.

Theming is abstracted and exposed to widgets through two interfaces:

  • SizeCx supplies widgets with size information
  • DrawCx is used to draw widget elements

Kas currently provides three theme implementations (along with one meta-implementation):

  • kas::theme::SimpleTheme prioritises simplicity without loss of functionality.
  • kas::theme::FlatTheme extends SimpleTheme, putting more effort into styling while using no complex drawing techniques (well, if one doesn't count fonts).
  • 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).
  • kas::theme::MultiTheme supports run-time switching between pre-loaded themes. It is used by the Gallery example.

Configuration

Previously we adjusted the font size before the UI was started:

extern crate kas;
use kas::prelude::*;
fn main() -> kas::runner::Result<()> {
    let theme = kas::theme::SimpleTheme::new();
    let mut app = kas::runner::Runner::with_theme(theme).build(())?;
    let _ = app.config_mut().font.set_size(24.0);
    Ok(())
}

Various aspects of fonts, themes, event handling and shortcuts may be adjusted here; see the Config struct.

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.

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.

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.