From d53932bd7c7e835cb93e9969c5de302f6fa04cf0 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Tue, 31 Oct 2023 20:40:46 -0600 Subject: [PATCH] Add function to set metrics and size simultaneously --- src/buffer.rs | 36 +++++++++++++++++++++++++++++------- src/edit/vi.rs | 8 +------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index 3a32373928..2f60395986 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -552,12 +552,7 @@ impl Buffer { /// /// Will panic if `metrics.font_size` is zero. pub fn set_metrics(&mut self, font_system: &mut FontSystem, metrics: Metrics) { - if metrics != self.metrics { - assert_ne!(metrics.font_size, 0.0, "font size cannot be 0"); - self.metrics = metrics; - self.relayout(font_system); - self.shape_until_scroll(font_system); - } + self.set_metrics_and_size(font_system, metrics, self.width, self.height); } /// Get the current [`Wrap`] @@ -581,10 +576,27 @@ impl Buffer { /// Set the current buffer dimensions pub fn set_size(&mut self, font_system: &mut FontSystem, width: f32, height: f32) { + self.set_metrics_and_size(font_system, self.metrics, width, height); + } + + /// Set the current [`Metrics`] and buffer dimensions at the same time + /// + /// # Panics + /// + /// Will panic if `metrics.font_size` is zero. + pub fn set_metrics_and_size( + &mut self, + font_system: &mut FontSystem, + metrics: Metrics, + width: f32, + height: f32, + ) { let clamped_width = width.max(0.0); let clamped_height = height.max(0.0); - if clamped_width != self.width || clamped_height != self.height { + if metrics != self.metrics || clamped_width != self.width || clamped_height != self.height { + assert_ne!(metrics.font_size, 0.0, "font size cannot be 0"); + self.metrics = metrics; self.width = clamped_width; self.height = clamped_height; self.relayout(font_system); @@ -925,6 +937,16 @@ impl<'a> BorrowedWithFontSystem<'a, Buffer> { self.inner.set_size(self.font_system, width, height); } + /// Set the current [`Metrics`] and buffer dimensions at the same time + /// + /// # Panics + /// + /// Will panic if `metrics.font_size` is zero. + pub fn set_metrics_and_size(&mut self, metrics: Metrics, width: f32, height: f32) { + self.inner + .set_metrics_and_size(self.font_system, metrics, width, height); + } + /// Set text of buffer, using provided attributes for each line by default pub fn set_text(&mut self, text: &str, attrs: Attrs, shaping: Shaping) { self.inner.set_text(self.font_system, text, attrs, shaping); diff --git a/src/edit/vi.rs b/src/edit/vi.rs index 483a88cf24..f11ec1c80b 100644 --- a/src/edit/vi.rs +++ b/src/edit/vi.rs @@ -590,13 +590,7 @@ impl<'a> Edit for ViEditor<'a> { Color::rgba(color.r(), color.g(), color.b(), 0x33), ); } else { - f( - start_x, - line_top as i32, - 1, - line_height as u32, - color, - ); + f(start_x, line_top as i32, 1, line_height as u32, color); } }