Fix resize to smaller size.

This commit is contained in:
daa 2016-05-03 13:30:36 +03:00
parent 3b5e26f95c
commit f2911beee3

View File

@ -41,6 +41,7 @@ pub struct Ui {
pub model: UiModel, pub model: UiModel,
nvim: Option<Neovim>, nvim: Option<Neovim>,
drawing_area: DrawingArea, drawing_area: DrawingArea,
window: Window,
cur_attrs: Option<Attrs>, cur_attrs: Option<Attrs>,
bg_color: Color, bg_color: Color,
fg_color: Color, fg_color: Color,
@ -54,6 +55,7 @@ impl Ui {
Ui { Ui {
model: UiModel::empty(), model: UiModel::empty(),
drawing_area: DrawingArea::new(), drawing_area: DrawingArea::new(),
window: Window::new(WindowType::Toplevel),
nvim: None, nvim: None,
cur_attrs: None, cur_attrs: None,
bg_color: COLOR_BLACK, bg_color: COLOR_BLACK,
@ -73,9 +75,6 @@ impl Ui {
} }
pub fn init(&mut self) { pub fn init(&mut self) {
let window = Window::new(WindowType::Toplevel);
let grid = Grid::new(); let grid = Grid::new();
let button_bar = ButtonBox::new(Orientation::Horizontal); let button_bar = ButtonBox::new(Orientation::Horizontal);
@ -103,10 +102,10 @@ impl Ui {
grid.attach(&self.drawing_area, 0, 1, 1, 1); grid.attach(&self.drawing_area, 0, 1, 1, 1);
self.drawing_area.connect_draw(gtk_draw); self.drawing_area.connect_draw(gtk_draw);
window.add(&grid); self.window.add(&grid);
window.show_all(); self.window.show_all();
window.connect_key_press_event(gtk_key_press); self.window.connect_key_press_event(gtk_key_press);
window.connect_delete_event(|_, _| { self.window.connect_delete_event(|_, _| {
gtk::main_quit(); gtk::main_quit();
Inhibit(false) Inhibit(false)
}); });
@ -131,7 +130,7 @@ fn calc_char_bounds(ctx: &cairo::Context) -> TextExtents {
ctx.text_extents("A") ctx.text_extents("A")
} }
fn gtk_draw(drawing_area: &DrawingArea, ctx: &cairo::Context) -> Inhibit { fn gtk_draw(_: &DrawingArea, ctx: &cairo::Context) -> Inhibit {
UI.with(|ui_cell| { UI.with(|ui_cell| {
let mut ui = ui_cell.borrow_mut(); let mut ui = ui_cell.borrow_mut();
@ -141,8 +140,7 @@ fn gtk_draw(drawing_area: &DrawingArea, ctx: &cairo::Context) -> Inhibit {
ui.char_width = Some(char_bounds.width.round()); ui.char_width = Some(char_bounds.width.round());
draw(&*ui, ctx); draw(&*ui, ctx);
request_width(&drawing_area, &*ui); request_width(&*ui);
}); });
Inhibit(false) Inhibit(false)
@ -235,14 +233,20 @@ fn draw(ui: &Ui, ctx: &cairo::Context) {
} }
fn request_width(drawing_area: &DrawingArea, ui: &Ui) { fn request_width(ui: &Ui) {
let width = drawing_area.get_allocated_width(); if ui.resize_timer.is_some() {
let height = drawing_area.get_allocated_height(); return;
}
let width = ui.drawing_area.get_allocated_width();
let height = ui.drawing_area.get_allocated_height();
let request_height = (ui.model.rows as f64 * ui.line_height.unwrap()) as i32; let request_height = (ui.model.rows as f64 * ui.line_height.unwrap()) as i32;
let request_width = (ui.model.columns as f64 * ui.char_width.unwrap()) as i32; let request_width = (ui.model.columns as f64 * ui.char_width.unwrap()) as i32;
if width != request_width || height != request_height { if width != request_width || height != request_height {
drawing_area.set_size_request(request_width, request_height); let h_border = ui.window.get_allocated_width() - width;
let v_border = ui.window.get_allocated_height() - height;
ui.window.resize(request_width + h_border, request_height + v_border);
} }
} }