Skip to content

Commit

Permalink
Add function to set metrics and size simultaneously
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Nov 1, 2023
1 parent 423fc22 commit d53932b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
36 changes: 29 additions & 7 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 1 addition & 7 deletions src/edit/vi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit d53932b

Please sign in to comment.