#gtk #store #state

gstore

Global and local state management in redux style for GTK applications written in Rust

26 releases

new 0.6.2 Jan 17, 2022
0.6.1 Jan 17, 2022
0.5.6 Jan 16, 2022
0.2.12 May 11, 2021
0.0.2 Dec 16, 2020

#53 in GUI

Download history 19/week @ 2021-10-09 17/week @ 2021-11-06 4/week @ 2021-11-20 22/week @ 2021-11-27 26/week @ 2021-12-04 25/week @ 2021-12-11 16/week @ 2021-12-18 26/week @ 2021-12-25 27/week @ 2022-01-01 87/week @ 2022-01-08 176/week @ 2022-01-15

319 downloads per month
Used in gtk-rust-app

GPL-3.0-or-later

32KB
621 lines

gstore

pipeline status API

Global and local state management for GTK apps which implements the flux pattern. This crate targets apps written with gtk-rust-app framework.

A State can be any kind of data structure an application is based on. For a 'counter' app it might be a struct with a single u32 field. Actions are enums which represent the possible features of the app affecting the state.

Global store usage

// src/store.rs

pub const INCREMENT: &str = "increment";

// Given you have a global state
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct State {
    pub count: u128
}

// Using the store macro a 'store' for this state
store!(State);
// The following public interface will be generated:
    pub type Store = gstore::Store<State>
    /// Initialize the store and global state
    /// 
    /// # Arguments
    /// - initializer: Initialize the state.
    /// - reducer: Handle actions and mutate the state
    /// - middlewares: Pre- and post-reduce handlers
    pub fn init_store(
        initializer: impl Fn(&mut State), 
        reducer: impl Fn(gstore::Action, &mut State), 
        middleware: Vec<Box<dyn gstore::Middleware<State>>>
    )
    /// Get a static reference to the store
    pub fn store() -> &'static Store
// main.rs

mod store;

// Initialize the store
pub fn main() {
    init_store(
        |state| {
            state.count = 0;
        },
        |action, state| {
            match action.name() {
                crate::store::INCREMENT => {
                    let amount: u128 = action.arg().unwrap();
                    state.count += amount;
                }
            }
        },
        vec![
            MyMiddleware::new()
        ]
    )
}

// Implement a middleware
struct MyMiddleware;
impl MyMiddleware {
    pub fn new() -> Box<Self> {
        Box::new(MyMiddleware)
    }
}
impl gstore::Middleware<State> for MyMiddleware {
    fn pre_reduce(action: &Action, state: &State) {
        //...
    }

    fn post_reduce(action: &Action, state: &State) {
        //...
    }
}

// Then in your GTK app you can dispatch actions to the store

let button: gtk::Button = gtk::Button::builder().build();
button.connect_clicked(|b| {
    b.dispatch(crate::store::INCREMENT, 2)
});

Performance data

gstore can print basic performance information via the environment variabl GSTORE_PERF=1;

License

gstore is distributed under the terms of the GPL-3.0 license. See LICENSE for details.

Dependencies

~0.8–5MB
~113K SLoC

a