on master help sign in
Refactor Margin for fine grained control (#2727)
commit8e8367eea6ff146c7e1097af153398832691e078 authorGokul Soumya <gokulps15@gmail.com> dateTue 21 Jun 2022, 22:22 (4 years ago) parentce85b97
8 files changed+75−43
helix-term/src/commands/lsp.rs+2−4 helix-term/src/ui/info.rs+1−4 helix-term/src/ui/markdown.rs+1−4 helix-term/src/ui/picker.rs+1−4 helix-term/src/ui/popup.rs+3−6 helix-term/src/ui/prompt.rs+1−4 helix-tui/src/layout.rs+6−10 helix-view/src/graphics.rs+60−7
helix-term/src/commands/lsp.rs+2−4
@@ -287,10 +287,8 @@ pub fn code_action(cx: &mut Context) {
287287 });
288288 picker.move_down(); // pre-select the first item
289289
290− let popup = Popup::new("code-action", picker).margin(helix_view::graphics::Margin {
291− vertical: 1,
292− horizontal: 1,
293− });
290+ let popup =
291+ Popup::new("code-action", picker).margin(helix_view::graphics::Margin::all(1));
294292 compositor.replace_or_push("code-action", popup);
295293 },
296294 )
helix-term/src/ui/info.rs+1−4
@@ -27,10 +27,7 @@ impl Component for Info {
2727 .borders(Borders::ALL)
2828 .border_style(popup_style);
2929
30− let margin = Margin {
31− vertical: 0,
32− horizontal: 1,
33− };
30+ let margin = Margin::horizontal(1);
3431 let inner = block.inner(area).inner(&margin);
3532 block.render(area, surface);
3633
helix-term/src/ui/markdown.rs+1−4
@@ -323,10 +323,7 @@ impl Component for Markdown {
323323 .wrap(Wrap { trim: false })
324324 .scroll((cx.scroll.unwrap_or_default() as u16, 0));
325325
326− let margin = Margin {
327− vertical: 1,
328− horizontal: 1,
329− };
326+ let margin = Margin::all(1);
330327 par.render(area.inner(&margin), surface);
331328 }
332329
helix-term/src/ui/picker.rs+1−4
@@ -200,10 +200,7 @@ impl<T: 'static> Component for FilePicker<T> {
200200 // calculate the inner area inside the box
201201 let inner = block.inner(preview_area);
202202 // 1 column gap on either side
203− let margin = Margin {
204− vertical: 0,
205− horizontal: 1,
206− };
203+ let margin = Margin::horizontal(1);
207204 let inner = inner.inner(&margin);
208205 block.render(preview_area, surface);
209206
helix-term/src/ui/popup.rs+3−6
@@ -27,10 +27,7 @@ impl<T: Component> Popup<T> {
2727 Self {
2828 contents,
2929 position: None,
30− margin: Margin {
31− vertical: 0,
32− horizontal: 0,
33− },
30+ margin: Margin::none(),
3431 size: (0, 0),
3532 child_size: (0, 0),
3633 scroll: 0,
@@ -163,8 +160,8 @@ impl<T: Component> Component for Popup<T> {
163160
164161 self.child_size = (width, height);
165162 self.size = (
166− (width + self.margin.horizontal * 2).min(max_width),
167− (height + self.margin.vertical * 2).min(max_height),
163+ (width + self.margin.width()).min(max_width),
164+ (height + self.margin.height()).min(max_height),
168165 );
169166
170167 // re-clamp scroll offset
helix-term/src/ui/prompt.rs+1−4
@@ -430,10 +430,7 @@ impl Prompt {
430430 .borders(Borders::ALL)
431431 .border_style(background);
432432
433− let inner = block.inner(area).inner(&Margin {
434− vertical: 0,
435− horizontal: 1,
436− });
433+ let inner = block.inner(area).inner(&Margin::horizontal(1));
437434
438435 block.render(area, surface);
439436 text.render(inner, surface, cx);
helix-tui/src/layout.rs+6−10
@@ -68,10 +68,7 @@ impl Default for Layout {
6868 fn default() -> Layout {
6969 Layout {
7070 direction: Direction::Vertical,
71− margin: Margin {
72− horizontal: 0,
73− vertical: 0,
74− },
71+ margin: Margin::none(),
7572 constraints: Vec::new(),
7673 }
7774 }
@@ -87,20 +84,19 @@ impl Layout {
8784 }
8885
8986 pub fn margin(mut self, margin: u16) -> Layout {
90− self.margin = Margin {
91− horizontal: margin,
92− vertical: margin,
93− };
87+ self.margin = Margin::all(margin);
9488 self
9589 }
9690
9791 pub fn horizontal_margin(mut self, horizontal: u16) -> Layout {
98− self.margin.horizontal = horizontal;
92+ self.margin.left = horizontal;
93+ self.margin.right = horizontal;
9994 self
10095 }
10196
10297 pub fn vertical_margin(mut self, vertical: u16) -> Layout {
103− self.margin.vertical = vertical;
98+ self.margin.top = vertical;
99+ self.margin.bottom = vertical;
104100 self
105101 }
106102
helix-view/src/graphics.rs+60−7
@@ -27,8 +27,61 @@ impl Default for CursorKind {
2727
2828#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2929pub struct Margin {
30− pub vertical: u16,
31− pub horizontal: u16,
30+ pub left: u16,
31+ pub right: u16,
32+ pub top: u16,
33+ pub bottom: u16,
34+}
35+
36+impl Margin {
37+ pub fn none() -> Self {
38+ Self {
39+ left: 0,
40+ right: 0,
41+ top: 0,
42+ bottom: 0,
43+ }
44+ }
45+
46+ /// Set uniform margin for all sides.
47+ pub fn all(value: u16) -> Self {
48+ Self {
49+ left: value,
50+ right: value,
51+ top: value,
52+ bottom: value,
53+ }
54+ }
55+
56+ /// Set the margin of left and right sides to specified value.
57+ pub fn horizontal(value: u16) -> Self {
58+ Self {
59+ left: value,
60+ right: value,
61+ top: 0,
62+ bottom: 0,
63+ }
64+ }
65+
66+ /// Set the margin of top and bottom sides to specified value.
67+ pub fn vertical(value: u16) -> Self {
68+ Self {
69+ left: 0,
70+ right: 0,
71+ top: value,
72+ bottom: value,
73+ }
74+ }
75+
76+ /// Get the total width of the margin (left + right)
77+ pub fn width(&self) -> u16 {
78+ self.left + self.right
79+ }
80+
81+ /// Get the total height of the margin (top + bottom)
82+ pub fn height(&self) -> u16 {
83+ self.top + self.bottom
84+ }
3285}
3386
3487/// A simple rectangle used in the computation of the layout and to give widgets an hint about the
@@ -141,14 +194,14 @@ impl Rect {
141194 }
142195
143196 pub fn inner(self, margin: &Margin) -> Rect {
144− if self.width < 2 * margin.horizontal || self.height < 2 * margin.vertical {
197+ if self.width < margin.width() || self.height < margin.height() {
145198 Rect::default()
146199 } else {
147200 Rect {
148− x: self.x + margin.horizontal,
149− y: self.y + margin.vertical,
150− width: self.width - 2 * margin.horizontal,
151− height: self.height - 2 * margin.vertical,
201+ x: self.x + margin.left,
202+ y: self.y + margin.top,
203+ width: self.width - margin.width(),
204+ height: self.height - margin.height(),
152205 }
153206 }
154207 }