Refactor Margin for fine grained control (#2727)
This commit is contained in:
parent
ce85b9716d
commit
8e8367eea6
8 changed files with 75 additions and 43 deletions
|
@ -287,10 +287,8 @@ pub fn code_action(cx: &mut Context) {
|
|||
});
|
||||
picker.move_down(); // pre-select the first item
|
||||
|
||||
let popup = Popup::new("code-action", picker).margin(helix_view::graphics::Margin {
|
||||
vertical: 1,
|
||||
horizontal: 1,
|
||||
});
|
||||
let popup =
|
||||
Popup::new("code-action", picker).margin(helix_view::graphics::Margin::all(1));
|
||||
compositor.replace_or_push("code-action", popup);
|
||||
},
|
||||
)
|
||||
|
|
|
@ -27,10 +27,7 @@ impl Component for Info {
|
|||
.borders(Borders::ALL)
|
||||
.border_style(popup_style);
|
||||
|
||||
let margin = Margin {
|
||||
vertical: 0,
|
||||
horizontal: 1,
|
||||
};
|
||||
let margin = Margin::horizontal(1);
|
||||
let inner = block.inner(area).inner(&margin);
|
||||
block.render(area, surface);
|
||||
|
||||
|
|
|
@ -323,10 +323,7 @@ impl Component for Markdown {
|
|||
.wrap(Wrap { trim: false })
|
||||
.scroll((cx.scroll.unwrap_or_default() as u16, 0));
|
||||
|
||||
let margin = Margin {
|
||||
vertical: 1,
|
||||
horizontal: 1,
|
||||
};
|
||||
let margin = Margin::all(1);
|
||||
par.render(area.inner(&margin), surface);
|
||||
}
|
||||
|
||||
|
|
|
@ -200,10 +200,7 @@ impl<T: 'static> Component for FilePicker<T> {
|
|||
// calculate the inner area inside the box
|
||||
let inner = block.inner(preview_area);
|
||||
// 1 column gap on either side
|
||||
let margin = Margin {
|
||||
vertical: 0,
|
||||
horizontal: 1,
|
||||
};
|
||||
let margin = Margin::horizontal(1);
|
||||
let inner = inner.inner(&margin);
|
||||
block.render(preview_area, surface);
|
||||
|
||||
|
|
|
@ -27,10 +27,7 @@ impl<T: Component> Popup<T> {
|
|||
Self {
|
||||
contents,
|
||||
position: None,
|
||||
margin: Margin {
|
||||
vertical: 0,
|
||||
horizontal: 0,
|
||||
},
|
||||
margin: Margin::none(),
|
||||
size: (0, 0),
|
||||
child_size: (0, 0),
|
||||
scroll: 0,
|
||||
|
@ -163,8 +160,8 @@ impl<T: Component> Component for Popup<T> {
|
|||
|
||||
self.child_size = (width, height);
|
||||
self.size = (
|
||||
(width + self.margin.horizontal * 2).min(max_width),
|
||||
(height + self.margin.vertical * 2).min(max_height),
|
||||
(width + self.margin.width()).min(max_width),
|
||||
(height + self.margin.height()).min(max_height),
|
||||
);
|
||||
|
||||
// re-clamp scroll offset
|
||||
|
|
|
@ -430,10 +430,7 @@ impl Prompt {
|
|||
.borders(Borders::ALL)
|
||||
.border_style(background);
|
||||
|
||||
let inner = block.inner(area).inner(&Margin {
|
||||
vertical: 0,
|
||||
horizontal: 1,
|
||||
});
|
||||
let inner = block.inner(area).inner(&Margin::horizontal(1));
|
||||
|
||||
block.render(area, surface);
|
||||
text.render(inner, surface, cx);
|
||||
|
|
|
@ -68,10 +68,7 @@ impl Default for Layout {
|
|||
fn default() -> Layout {
|
||||
Layout {
|
||||
direction: Direction::Vertical,
|
||||
margin: Margin {
|
||||
horizontal: 0,
|
||||
vertical: 0,
|
||||
},
|
||||
margin: Margin::none(),
|
||||
constraints: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
@ -87,20 +84,19 @@ impl Layout {
|
|||
}
|
||||
|
||||
pub fn margin(mut self, margin: u16) -> Layout {
|
||||
self.margin = Margin {
|
||||
horizontal: margin,
|
||||
vertical: margin,
|
||||
};
|
||||
self.margin = Margin::all(margin);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn horizontal_margin(mut self, horizontal: u16) -> Layout {
|
||||
self.margin.horizontal = horizontal;
|
||||
self.margin.left = horizontal;
|
||||
self.margin.right = horizontal;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn vertical_margin(mut self, vertical: u16) -> Layout {
|
||||
self.margin.vertical = vertical;
|
||||
self.margin.top = vertical;
|
||||
self.margin.bottom = vertical;
|
||||
self
|
||||
}
|
||||
|
||||
|
|
|
@ -27,8 +27,61 @@ impl Default for CursorKind {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Margin {
|
||||
pub vertical: u16,
|
||||
pub horizontal: u16,
|
||||
pub left: u16,
|
||||
pub right: u16,
|
||||
pub top: u16,
|
||||
pub bottom: u16,
|
||||
}
|
||||
|
||||
impl Margin {
|
||||
pub fn none() -> Self {
|
||||
Self {
|
||||
left: 0,
|
||||
right: 0,
|
||||
top: 0,
|
||||
bottom: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set uniform margin for all sides.
|
||||
pub fn all(value: u16) -> Self {
|
||||
Self {
|
||||
left: value,
|
||||
right: value,
|
||||
top: value,
|
||||
bottom: value,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the margin of left and right sides to specified value.
|
||||
pub fn horizontal(value: u16) -> Self {
|
||||
Self {
|
||||
left: value,
|
||||
right: value,
|
||||
top: 0,
|
||||
bottom: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Set the margin of top and bottom sides to specified value.
|
||||
pub fn vertical(value: u16) -> Self {
|
||||
Self {
|
||||
left: 0,
|
||||
right: 0,
|
||||
top: value,
|
||||
bottom: value,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the total width of the margin (left + right)
|
||||
pub fn width(&self) -> u16 {
|
||||
self.left + self.right
|
||||
}
|
||||
|
||||
/// Get the total height of the margin (top + bottom)
|
||||
pub fn height(&self) -> u16 {
|
||||
self.top + self.bottom
|
||||
}
|
||||
}
|
||||
|
||||
/// A simple rectangle used in the computation of the layout and to give widgets an hint about the
|
||||
|
@ -141,14 +194,14 @@ impl Rect {
|
|||
}
|
||||
|
||||
pub fn inner(self, margin: &Margin) -> Rect {
|
||||
if self.width < 2 * margin.horizontal || self.height < 2 * margin.vertical {
|
||||
if self.width < margin.width() || self.height < margin.height() {
|
||||
Rect::default()
|
||||
} else {
|
||||
Rect {
|
||||
x: self.x + margin.horizontal,
|
||||
y: self.y + margin.vertical,
|
||||
width: self.width - 2 * margin.horizontal,
|
||||
height: self.height - 2 * margin.vertical,
|
||||
x: self.x + margin.left,
|
||||
y: self.y + margin.top,
|
||||
width: self.width - margin.width(),
|
||||
height: self.height - margin.height(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue