mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-10 03:10:41 +00:00
Can now change dialog button alignment
This commit is contained in:
parent
7545d7170c
commit
781d9c1a03
@ -1,6 +1,7 @@
|
||||
extern crate cursive;
|
||||
|
||||
use cursive::Cursive;
|
||||
use cursive::align::HAlign;
|
||||
use cursive::view::{TextView,Dialog};
|
||||
|
||||
use std::fs::File;
|
||||
@ -20,6 +21,7 @@ fn main() {
|
||||
// The text is too long to fit on a line, so the view will wrap lines,
|
||||
// and will adapt to the terminal size.
|
||||
siv.add_layer(Dialog::new(TextView::new(&content))
|
||||
.h_align(HAlign::Center)
|
||||
.button("Quit", |s| s.quit()));
|
||||
// Show a popup on top of the view.
|
||||
siv.add_layer(Dialog::new(TextView::new("Try resizing the terminal!\n(Press 'q' to quit when you're done.)"))
|
||||
|
12
src/align.rs
12
src/align.rs
@ -17,6 +17,18 @@ impl Align {
|
||||
Align::new(HAlign::Left, VAlign::Top)
|
||||
}
|
||||
|
||||
pub fn top_right() -> Self {
|
||||
Align::new(HAlign::Right, VAlign::Top)
|
||||
}
|
||||
|
||||
pub fn bot_left() -> Self {
|
||||
Align::new(HAlign::Left, VAlign::Bottom)
|
||||
}
|
||||
|
||||
pub fn bot_right() -> Self {
|
||||
Align::new(HAlign::Right, VAlign::Top)
|
||||
}
|
||||
|
||||
pub fn center() -> Self {
|
||||
Align::new(HAlign::Center, VAlign::Center)
|
||||
}
|
||||
|
59
src/vec.rs
59
src/vec.rs
@ -124,6 +124,7 @@ impl Mul<usize> for Vec2 {
|
||||
}
|
||||
|
||||
/// Four values representing each direction.
|
||||
#[derive(Clone,Copy)]
|
||||
pub struct Vec4 {
|
||||
/// Left margin
|
||||
pub left: usize,
|
||||
@ -193,3 +194,61 @@ impl ToVec4 for (i32,i32,i32,i32) {
|
||||
Vec4::new(self.0 as usize, self.1 as usize, self.2 as usize, self.3 as usize)
|
||||
}
|
||||
}
|
||||
|
||||
impl <T: ToVec4> Add<T> for Vec4 {
|
||||
type Output = Vec4;
|
||||
|
||||
fn add(self, other: T) -> Vec4 {
|
||||
let ov = other.to_vec4();
|
||||
|
||||
Vec4 {
|
||||
left: self.left + ov.left,
|
||||
right: self.right + ov.right,
|
||||
top: self.top + ov.top,
|
||||
bottom: self.bottom + ov.bottom,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl <T: ToVec4> Sub<T> for Vec4 {
|
||||
type Output = Vec4;
|
||||
|
||||
fn sub(self, other: T) -> Vec4 {
|
||||
let ov = other.to_vec4();
|
||||
|
||||
Vec4 {
|
||||
left: self.left - ov.left,
|
||||
right: self.right - ov.right,
|
||||
top: self.top - ov.top,
|
||||
bottom: self.bottom - ov.bottom,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Div<usize> for Vec4 {
|
||||
type Output = Vec4;
|
||||
|
||||
fn div(self, other: usize) -> Vec4 {
|
||||
Vec4 {
|
||||
left: self.left / other,
|
||||
right: self.right / other,
|
||||
top: self.top / other,
|
||||
bottom: self.bottom / other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul<usize> for Vec4 {
|
||||
type Output = Vec4;
|
||||
|
||||
fn mul(self, other: usize) -> Vec4 {
|
||||
Vec4 {
|
||||
left: self.left * other,
|
||||
right: self.right * other,
|
||||
top: self.top * other,
|
||||
bottom: self.bottom * other,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ use std::any::Any;
|
||||
|
||||
use color;
|
||||
use ::{Cursive};
|
||||
use align::*;
|
||||
use event::*;
|
||||
use view::{View,SizeRequest,DimensionRequest,Selector};
|
||||
use view::{Button,SizedView};
|
||||
@ -32,6 +33,8 @@ pub struct Dialog {
|
||||
borders: Vec4,
|
||||
|
||||
focus: Focus,
|
||||
|
||||
align: Align,
|
||||
}
|
||||
|
||||
impl Dialog {
|
||||
@ -44,6 +47,7 @@ impl Dialog {
|
||||
focus: Focus::Content,
|
||||
padding: Vec4::new(1,1,0,0),
|
||||
borders: Vec4::new(1,1,1,1),
|
||||
align: Align::top_right(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,6 +62,18 @@ impl Dialog {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn h_align(mut self, h: HAlign) -> Self {
|
||||
self.align.h = h;
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
pub fn v_align(mut self, v: VAlign) -> Self {
|
||||
self.align.v = v;
|
||||
|
||||
self
|
||||
}
|
||||
|
||||
/// Shortcut method to add a button that will dismiss the dialog.
|
||||
pub fn dismiss_button<'a>(self, label: &'a str) -> Self {
|
||||
self.button(label, |s| s.screen_mut().pop_layer())
|
||||
@ -85,14 +101,19 @@ impl View for Dialog {
|
||||
// This will be the height used by the buttons.
|
||||
let mut height = 0;
|
||||
// Current horizontal position of the next button we'll draw.
|
||||
let mut x = 0;
|
||||
for (i,button) in self.buttons.iter_mut().enumerate().rev() {
|
||||
|
||||
let width = self.buttons.len() + self.buttons.iter().map(|button| button.size.x).fold(0, |a,b| a+b);
|
||||
let overhead = self.padding + self.borders;
|
||||
let mut offset = overhead.left + self.align.h.get_offset(width, printer.size.x - overhead.horizontal());
|
||||
let y = printer.size.y - self.padding.bottom - self.borders.bottom - 1;
|
||||
|
||||
for (i,button) in self.buttons.iter_mut().enumerate() {
|
||||
let size = button.size;
|
||||
let offset = printer.size - self.borders.bot_right() - self.padding.bot_right() - size - Vec2::new(x, 0);
|
||||
// let offset = printer.size - self.borders.bot_right() - self.padding.bot_right() - size - Vec2::new(x, 0);
|
||||
// Add some special effect to the focused button
|
||||
button.draw(&printer.sub_printer(offset, size, self.focus == Focus::Button(i)));
|
||||
button.draw(&printer.sub_printer(Vec2::new(offset, y), size, self.focus == Focus::Button(i)));
|
||||
// Keep 1 blank between two buttons
|
||||
x += size.x + 1;
|
||||
offset += size.x + 1;
|
||||
// Also keep 1 blank above the buttons
|
||||
height = max(height, size.y+1);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user