nvim_api/types/
window_config.rs

1use derive_builder::Builder;
2use nvim_types::{
3    conversion::{self, FromObject},
4    serde::Deserializer,
5    Array,
6    Float,
7    Integer,
8    Object,
9};
10use serde::Deserialize;
11
12use super::{WindowAnchor, WindowBorder, WindowRelativeTo, WindowStyle};
13
14#[non_exhaustive]
15#[derive(Clone, Debug, Default, PartialEq, Builder, Deserialize)]
16#[builder(default, build_fn(private, name = "fallible_build"))]
17pub struct WindowConfig {
18    /// Decides which corner of the window to place at `(row, col)`.
19    #[builder(setter(strip_option))]
20    pub anchor: Option<WindowAnchor>,
21
22    /// Style of the optional window border.
23    #[builder(setter(strip_option))]
24    pub border: Option<WindowBorder>,
25
26    /// Places window relative to buffer text (only when
27    /// [`relative`](WindowConfigBuilder::relative) is set to
28    /// [`WindowRelativeTo::Window(win)`](WindowRelativeTo)). Takes a zero
29    /// indexed `(line, column)` tuple, with `row` and `col` being placed
30    /// relative to this position if specified.
31    #[builder(setter(custom))]
32    pub bufpos: Option<(usize, usize)>,
33
34    /// Column position in units of screen cell width. May be fractional
35    #[builder(setter(into, strip_option))]
36    pub col: Option<Float>,
37
38    /// Whether an attached GUI should display the window as an external
39    /// top-level window.
40    #[builder(setter(strip_option))]
41    pub external: Option<bool>,
42
43    /// Enable focus by user actions like mouse events. Non-focusable windows
44    /// can be entered by [`set_current_win`](crate::set_current_win).
45    #[builder(setter(strip_option))]
46    pub focusable: Option<bool>,
47
48    /// Window height in character cells. Minimum of 1.
49    #[builder(setter(strip_option))]
50    pub height: Option<u32>,
51
52    /// If `true` then no buffer-related autocommand events such as `BufEnter`
53    /// or `BufLeave` are fired when calling [`open_win`](crate::open_win).
54    #[builder(setter(strip_option))]
55    pub noautocmd: Option<bool>,
56
57    /// What the window is positioned relative to.
58    #[builder(setter(strip_option))]
59    pub relative: Option<WindowRelativeTo>,
60
61    /// Row position in units of screen cell height. May be fractional.
62    #[builder(setter(into, strip_option))]
63    pub row: Option<Float>,
64
65    /// Configures the appearance of the window.
66    #[builder(setter(strip_option))]
67    pub style: Option<WindowStyle>,
68
69    /// Window width in character cells. Minimum of 1.
70    #[builder(setter(strip_option))]
71    pub width: Option<u32>,
72
73    /// Stacking order. Windows with higher `zindex` go in front of windows
74    /// with lower indices.
75    #[builder(setter(strip_option))]
76    pub zindex: Option<u32>,
77}
78
79impl WindowConfig {
80    #[inline(always)]
81    /// Creates a new `WinConfigBuilder`.
82    pub fn builder() -> WindowConfigBuilder {
83        WindowConfigBuilder::default()
84    }
85}
86
87impl WindowConfigBuilder {
88    /// Places window relative to buffer text (only when
89    /// [`relative`](WindowConfigBuilder::relative) is set to
90    /// [`WindowRelativeTo::Window(win)`](WindowRelativeTo)). Takes a zero
91    /// indexed `(line, column)` tuple, with `row` and `col` being placed
92    /// relative to this position if specified.
93    pub fn bufpos(&mut self, line: usize, column: usize) -> &mut Self {
94        self.bufpos = Some(Some((line, column)));
95        self
96    }
97
98    pub fn build(&mut self) -> WindowConfig {
99        self.fallible_build().expect("never fails, all fields have defaults")
100    }
101}
102
103impl FromObject for WindowConfig {
104    fn from_object(obj: Object) -> Result<Self, conversion::Error> {
105        Self::deserialize(Deserializer::new(obj)).map_err(Into::into)
106    }
107}
108
109#[derive(Default)]
110#[allow(non_camel_case_types)]
111#[repr(C)]
112pub(crate) struct KeyDict_float_config {
113    col: Object,
114    row: Object,
115    win: Object,
116    style: Object,
117    width: Object,
118    height: Object,
119    zindex: Object,
120    anchor: Object,
121    border: Object,
122    bufpos: Object,
123    external: Object,
124    relative: Object,
125    focusable: Object,
126    noautocmd: Object,
127}
128
129impl From<&WindowConfig> for KeyDict_float_config {
130    fn from(config: &WindowConfig) -> Self {
131        let win = match &config.relative {
132            Some(WindowRelativeTo::Window(win)) => win.0.into(),
133            _ => Object::nil(),
134        };
135
136        let bufpos = match config.bufpos {
137            Some((line, column)) => {
138                Array::from_iter([line as Integer, column as Integer]).into()
139            },
140            _ => Object::nil(),
141        };
142
143        Self {
144            col: config.col.into(),
145            row: config.row.into(),
146            win,
147            style: config.style.into(),
148            width: config.width.into(),
149            height: config.height.into(),
150            zindex: config.zindex.into(),
151            anchor: config.anchor.into(),
152            border: config.border.clone().into(),
153            bufpos,
154            external: config.external.into(),
155            relative: config.relative.as_ref().into(),
156            focusable: config.focusable.into(),
157            noautocmd: config.noautocmd.into(),
158        }
159    }
160}