mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-10 07:04:17 +00:00
Change layout algorithm
This commit is contained in:
parent
d11dedd864
commit
275b210fd4
4 changed files with 31 additions and 20 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
|||
target
|
||||
Cargo.lock
|
||||
*.log
|
||||
.gdb_history
|
||||
|
|
|
@ -130,7 +130,7 @@ fn draw(terminal: &mut Terminal, app: &App) {
|
|||
let ui = Group::default()
|
||||
.direction(Direction::Vertical)
|
||||
.alignment(Alignment::Left)
|
||||
.chunks(&[Size::Fixed(5), Size::Percent(80), Size::Fixed(10)])
|
||||
.chunks(&[Size::Fixed(5), Size::Min(5), Size::Fixed(3)])
|
||||
.render(&terminal.area(), |chunks, tree| {
|
||||
info!("{:?}", terminal.area());
|
||||
tree.add(Block::default().borders(border::ALL).title("Gauges").render(&chunks[0]));
|
||||
|
@ -148,9 +148,9 @@ fn draw(terminal: &mut Terminal, app: &App) {
|
|||
.render(&chunks[2]));
|
||||
}));
|
||||
let sizes = if app.show_episodes {
|
||||
vec![Size::Percent(50), Size::Percent(50)]
|
||||
vec![Size::Min(20), Size::Min(20)]
|
||||
} else {
|
||||
vec![Size::Percent(100)]
|
||||
vec![Size::Min(20)]
|
||||
};
|
||||
tree.add(Group::default()
|
||||
.direction(Direction::Horizontal)
|
||||
|
|
|
@ -104,7 +104,8 @@ impl Rect {
|
|||
#[derive(Debug, Clone, Hash)]
|
||||
pub enum Size {
|
||||
Fixed(u16),
|
||||
Percent(u16),
|
||||
Max(u16),
|
||||
Min(u16),
|
||||
}
|
||||
|
||||
/// # Examples
|
||||
|
@ -149,50 +150,47 @@ pub fn split(area: &Rect,
|
|||
if let Some(last) = elements.last() {
|
||||
constraints.push(match *dir {
|
||||
Direction::Horizontal => {
|
||||
last.x + last.width | EQ(REQUIRED) | (dest_area.x + dest_area.width) as f64
|
||||
last.x + last.width | EQ(WEAK) | (dest_area.x + dest_area.width) as f64
|
||||
}
|
||||
Direction::Vertical => {
|
||||
last.y + last.height | EQ(REQUIRED) | (dest_area.y + dest_area.height) as f64
|
||||
last.y + last.height | EQ(WEAK) | (dest_area.y + dest_area.height) as f64
|
||||
}
|
||||
})
|
||||
}
|
||||
match *dir {
|
||||
Direction::Horizontal => {
|
||||
for pair in elements.windows(2) {
|
||||
constraints.push(pair[0].x + pair[0].width | LE(REQUIRED) | pair[1].x);
|
||||
constraints.push(pair[0].x + pair[0].width | EQ(REQUIRED) | pair[1].x);
|
||||
}
|
||||
for (i, size) in sizes.iter().enumerate() {
|
||||
let cs = [elements[i].y | EQ(REQUIRED) | dest_area.y as f64,
|
||||
elements[i].height | EQ(REQUIRED) | dest_area.height as f64,
|
||||
match *size {
|
||||
Size::Fixed(f) => elements[i].width | EQ(MEDIUM) | f as f64,
|
||||
Size::Percent(p) => {
|
||||
elements[i].width | EQ(WEAK) |
|
||||
(dest_area.width * p) as f64 / 100.0
|
||||
}
|
||||
Size::Fixed(v) => elements[i].width | EQ(REQUIRED) | v as f64,
|
||||
Size::Min(v) => elements[i].width | GE(REQUIRED) | v as f64,
|
||||
Size::Max(v) => elements[i].width | LE(REQUIRED) | v as f64,
|
||||
}];
|
||||
constraints.extend_from_slice(&cs);
|
||||
}
|
||||
}
|
||||
Direction::Vertical => {
|
||||
for pair in elements.windows(2) {
|
||||
constraints.push(pair[0].y + pair[0].height | LE(REQUIRED) | pair[1].y);
|
||||
constraints.push(pair[0].y + pair[0].height | EQ(REQUIRED) | pair[1].y);
|
||||
}
|
||||
for (i, size) in sizes.iter().enumerate() {
|
||||
let cs = [elements[i].x | EQ(REQUIRED) | dest_area.x as f64,
|
||||
elements[i].width | EQ(REQUIRED) | dest_area.width as f64,
|
||||
match *size {
|
||||
Size::Fixed(f) => elements[i].height | EQ(REQUIRED) | f as f64,
|
||||
Size::Percent(p) => {
|
||||
elements[i].height | EQ(WEAK) |
|
||||
(dest_area.height * p) as f64 / 100.0
|
||||
}
|
||||
Size::Fixed(v) => elements[i].height | EQ(REQUIRED) | v as f64,
|
||||
Size::Min(v) => elements[i].height | GE(REQUIRED) | v as f64,
|
||||
Size::Max(v) => elements[i].height | LE(REQUIRED) | v as f64,
|
||||
}];
|
||||
constraints.extend_from_slice(&cs);
|
||||
}
|
||||
}
|
||||
}
|
||||
solver.add_constraints(&constraints).unwrap();
|
||||
// TODO: Find a better way to handle overflow error
|
||||
for &(var, value) in solver.fetch_changes() {
|
||||
let (index, attr) = vars[&var];
|
||||
match attr {
|
||||
|
@ -203,10 +201,18 @@ pub fn split(area: &Rect,
|
|||
results[index].y = value as u16;
|
||||
}
|
||||
2 => {
|
||||
results[index].width = value as u16;
|
||||
let mut v = value as u16;
|
||||
if v > area.width {
|
||||
v = 0;
|
||||
}
|
||||
results[index].width = v;
|
||||
}
|
||||
3 => {
|
||||
results[index].height = value as u16;
|
||||
let mut v = value as u16;
|
||||
if v > area.height {
|
||||
v = 0;
|
||||
}
|
||||
results[index].height = v;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
|
@ -44,6 +44,10 @@ impl<'a> Widget for Block<'a> {
|
|||
return buf;
|
||||
}
|
||||
|
||||
if area.width < 2 || area.height < 2 {
|
||||
return buf;
|
||||
}
|
||||
|
||||
// Sides
|
||||
if self.borders.intersects(border::LEFT) {
|
||||
let line = vline(area.x, area.y, area.height, self.border_fg, self.border_bg);
|
||||
|
|
Loading…
Reference in a new issue