libnotcurses_sys/plane/options/mod.rs
1//!
2
3use crate::{c_api::ffi, NcAlign, NcResizeCb};
4use core::ptr::{null, null_mut};
5
6mod builder;
7pub(crate) mod flags;
8
9pub use builder::NcPlaneOptionsBuilder;
10pub use flags::NcPlaneFlag;
11
12/// Options struct for [`NcPlane`][crate::NcPlane].
13///
14/// It is recommended to construct it via [`NcPlaneOptionsBuilder`]
15/// by calling [`NcPlaneOptions::builder()`].
16///
17/// # Fields
18/// - [`y`]: vertical placement relative to parent plane.
19/// - [`x`]: horizontal placement relative to parent plane.
20/// - [`rows`]: vertical length in rows.
21/// - [`cols`]: horizontal length in columns.
22/// - [`userptr`]: optional user curry.
23/// - [`name`]: optional string identifier for debugging.
24/// - [`resizecb`]: callback when parent is resized.
25/// - [`flags`]: [`NcPlaneFlag`].
26/// - [`margin_b`]: bottom margin (requires the [`Marginalized`] flag).
27/// - [`margin_r`]: right margin (requires the [`Marginalized`]).
28///
29/// [`y`]: ffi::ncplane_options#structfield.y
30/// [`x`]: ffi::ncplane_options#structfield.x
31/// [`rows`]: ffi::ncplane_options#structfield.rows
32/// [`cols`]: ffi::ncplane_options#structfield.cols
33/// [`userptr`]: ffi::ncplane_options#structfield.userptr
34/// [`name`]: ffi::ncplane_options#structfield.name
35/// [`resizecb`]: ffi::ncplane_options#structfield.resizecb
36/// [`flags`]: ffi::ncplane_options#structfield.flags
37/// [`margin_b`]: ffi::ncplane_options#structfield.margin_b
38/// [`margin_r`]: ffi::ncplane_options#structfield.margin_r
39/// [`Marginalized`]: NcPlaneFlag#associatedconstant.Marginalized
40pub type NcPlaneOptions = ffi::ncplane_options;
41
42/// # Constructors
43impl NcPlaneOptions {
44 /// New `NcPlaneOptions` using the horizontal x.
45 pub fn new(y: i32, x: i32, rows: u32, cols: u32) -> Self {
46 Self::with_flags(y, x, rows, cols, None, NcPlaneFlag::None, 0, 0)
47 }
48
49 /// Returns a default `NcPlane` options builder.
50 pub fn builder() -> NcPlaneOptionsBuilder {
51 NcPlaneOptionsBuilder::default()
52 }
53
54 /// Returns a builder object from the current `NcPlane` options.
55 pub fn to_builder(&self) -> NcPlaneOptionsBuilder {
56 NcPlaneOptionsBuilder::from_options(self)
57 }
58
59 /// New `NcPlaneOptions` with horizontal alignment.
60 pub fn new_aligned(y: i32, align: impl Into<NcAlign>, rows: u32, cols: u32) -> Self {
61 Self::with_flags_aligned(y, align.into(), rows, cols, None, NcPlaneFlag::HorAligned)
62 }
63
64 /// New `NcPlaneOptions`, with flags.
65 pub fn with_flags(
66 y: i32,
67 x: i32,
68 rows: u32,
69 cols: u32,
70 resizecb: Option<NcResizeCb>,
71 flags: impl Into<NcPlaneFlag>,
72 margin_b: u32,
73 margin_r: u32,
74 ) -> Self {
75 NcPlaneOptions {
76 y,
77 x,
78 rows,
79 cols,
80 userptr: null_mut(),
81 name: null(),
82 resizecb: crate::c_api::ncresizecb_to_c(resizecb),
83 flags: flags.into().into(),
84 margin_b,
85 margin_r,
86 }
87 }
88
89 /// New `NcPlaneOptions`, with flags and horizontal alignment.
90 ///
91 /// Note: Already includes the
92 /// [`NcPlaneOptions::HORALIGNED`][NcPlaneOptions#associatedconstant.HORALIGNED]
93 /// flag.
94 pub fn with_flags_aligned(
95 y: i32,
96 align: impl Into<NcAlign>,
97 rows: u32,
98 cols: u32,
99 resizecb: Option<NcResizeCb>,
100 flags: impl Into<NcPlaneFlag>,
101 ) -> Self {
102 let flags = NcPlaneFlag::HorAligned | flags.into();
103 NcPlaneOptions {
104 y,
105 x: align.into().into(),
106 rows,
107 cols,
108 userptr: null_mut(),
109 name: null(),
110 resizecb: crate::c_api::ncresizecb_to_c(resizecb),
111 flags: flags.into(),
112 margin_b: 0,
113 margin_r: 0,
114 }
115 }
116}
117
118/// # Methods
119impl NcPlaneOptions {
120 /// Returns `true` if it has the [`VerAligned`] flag set.
121 ///
122 /// [`VerAligned`]: NcPlaneFlag#associatedconstant.VerAligned
123 pub fn is_veraligned(&self) -> bool {
124 self.flags & NcPlaneFlag::VerAligned != NcPlaneFlag::None
125 }
126
127 /// Returns `true` if it has the [`HorAligned`] flag set.
128 ///
129 /// [`HorAligned`]: NcPlaneFlag#associatedconstant.HorAligned
130 pub fn is_horaligned(&self) -> bool {
131 self.flags & NcPlaneFlag::HorAligned != NcPlaneFlag::None
132 }
133
134 /// Returns `true` if it has the [`Marginalized`] flag set.
135 ///
136 /// [`Marginalized`]: NcPlaneFlag#associatedconstant.Marginalized
137 pub fn is_marginalized(&self) -> bool {
138 self.flags & NcPlaneFlag::Marginalized != NcPlaneFlag::None
139 }
140
141 /// Returns `true` if it has the [`Fixed`] flag set.
142 ///
143 /// [`Fixed`]: NcPlaneFlag#associatedconstant.Fixed
144 pub fn is_fixed(&self) -> bool {
145 self.flags & NcPlaneFlag::Fixed != NcPlaneFlag::None
146 }
147
148 /// Returns `true` if it has the [`AutoGrow`] flag set.
149 ///
150 /// [`AutoGrow`]: NcPlaneFlag#associatedconstant.AutoGrow
151 pub fn is_autogrow(&self) -> bool {
152 self.flags & NcPlaneFlag::AutoGrow != NcPlaneFlag::None
153 }
154
155 /// Returns `true` if it has the [`VScroll`] flag set.
156 ///
157 /// [`VScroll`]: NcPlaneFlag#associatedconstant.VScroll
158 pub fn is_vscroll(&self) -> bool {
159 self.flags & NcPlaneFlag::VScroll != NcPlaneFlag::None
160 }
161}