From a19c28473e6e45d1b40363b1f2398011691c70f9 Mon Sep 17 00:00:00 2001 From: daa Date: Mon, 4 Apr 2016 01:34:44 +0300 Subject: [PATCH] Resize from nvim settings --- src/ui.rs | 21 +++++++++++++++++---- src/ui_model.rs | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index ba9cef3..2b94305 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -82,7 +82,7 @@ impl Ui { grid.attach(&button_bar, 0, 0, 1, 1); - self.drawing_area.set_size_request(500, 500); + self.drawing_area.set_size_request(500, 300); self.drawing_area.set_hexpand(true); self.drawing_area.set_vexpand(true); grid.attach(&self.drawing_area, 0, 1, 1, 1); @@ -164,9 +164,6 @@ fn calc_char_bounds(ctx: &cairo::Context) -> TextExtents { } fn gtk_draw(drawing_area: &DrawingArea, ctx: &cairo::Context) -> Inhibit { - let width = drawing_area.get_allocated_width() as f64; - let height = drawing_area.get_allocated_height() as f64; - ctx.set_source_rgb(0.0, 0.0, 0.0); ctx.paint(); ctx.set_source_rgb(1.0, 1.0, 1.0); @@ -214,11 +211,27 @@ fn gtk_draw(drawing_area: &DrawingArea, ctx: &cairo::Context) -> Inhibit { } line_y += font_extents.height; } + + request_width(&drawing_area, &ui, font_extents.height, char_bounds.width); + }); + + Inhibit(true) } +fn request_width(drawing_area: &DrawingArea, ui: &Ui, line_height: f64, char_width: f64) { + let width = drawing_area.get_allocated_width(); + let height = drawing_area.get_allocated_height(); + let request_height = (ui.model.rows as f64 * line_height) as i32; + let request_width = (ui.model.columns as f64 * char_width) as i32; + + if width != request_width || height != request_height { + drawing_area.set_size_request(request_width, request_height); + } +} + impl RedrawEvents for Ui { fn on_cursor_goto(&mut self, row: u64, col: u64) { self.model.set_cursor(row, col); diff --git a/src/ui_model.rs b/src/ui_model.rs index 87a343c..cad536c 100644 --- a/src/ui_model.rs +++ b/src/ui_model.rs @@ -52,8 +52,8 @@ impl Cell { } pub struct UiModel { - columns: usize, - rows: usize, + pub columns: usize, + pub rows: usize, cur_row: usize, cur_col: usize, model: Vec>,