srdusr
aboutsummaryrefslogtreecommitdiffstats
path: root/crates/config/src/engine/layout.rs
blob: c0d923be85bee380492b4aaa2deef637fad1e60d (plain) (blame)
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
use super::*;
use super::support::flatten_table_into;

impl Engine {
    // ---- srd.layout.* ------------------------------------------------------

    pub(super) fn fn_layout_set(&self) -> Result<mlua::Function<'_>> {
        let state = self.state.clone();
        Ok(self.lua.create_function(move |_, name: String| {
            let wm = state.borrow().wm.clone();
            let mut wm = wm.borrow_mut();
            let ws = wm.current_workspace();
            wm.set_layout(ws, name);
            wm.arrange_workspace(ws);
            Ok(())
        })?)
    }

    /// `srd.layout.get()` - the name of the layout the *current workspace*
    /// is using right now.
    ///
    /// Deliberately not `srd.get("workspace.layout")`, which reads the
    /// config values table: that holds the configured default layout for
    /// new workspaces, not what the workspace in front of you switched to
    /// since. A toggle written against that key works exactly once and
    /// then sticks, because the value it reads never changes.
    pub(super) fn fn_layout_get(&self) -> Result<mlua::Function<'_>> {
        let state = self.state.clone();
        Ok(self.lua.create_function(move |lua, ()| {
            let wm = state.borrow().wm.clone();
            let wm = wm.borrow();
            let ws = wm.current_workspace();
            let name = wm.workspace(ws).map(|w| w.layout.clone()).unwrap_or_default();
            Ok(Value::String(lua.create_string(&name)?))
        })?)
    }

    pub(super) fn fn_layout_configure(&self) -> Result<mlua::Function<'_>> {
        let state = self.state.clone();
        Ok(self.lua.create_function(move |_, (name, table): (String, Table)| {
            let mut s = state.borrow_mut();
            flatten_table_into(&format!("layout.{name}"), &table, &mut s.values)?;
            let master_ratio = s.values.get(&format!("layout.{name}.master_ratio")).and_then(|v| v.as_f64());
            drop(s);
            if let Some(ratio) = master_ratio {
                state.borrow().wm.borrow_mut().tiling.master_ratio = ratio as f32;
            }
            Ok(())
        })?)
    }
}