From 01a4445cfa9ac45b701cc751783a9422d4a239b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Langlois?= Date: Sun, 21 Aug 2022 22:36:51 +0200 Subject: [PATCH 1/3] Fix Printer class member 'content_offset' naming Replace "content_offset" by "drawing_area_offset". --- cursive-core/src/printer.rs | 49 ++++++++++++++--------------- cursive-core/src/view/mod.rs | 2 +- cursive-core/src/view/scroll/mod.rs | 2 +- cursive-core/src/views/text_view.rs | 2 +- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/cursive-core/src/printer.rs b/cursive-core/src/printer.rs index 1019bca2..42a6f9fb 100644 --- a/cursive-core/src/printer.rs +++ b/cursive-core/src/printer.rs @@ -20,7 +20,7 @@ use unicode_width::UnicodeWidthStr; /// /// The area it can print on is defined by `offset` and `size`. /// -/// The part of the content it will print is defined by `content_offset` +/// The part of the content it will print is defined by `drawing_area_offset` /// and `size`. #[derive(Clone)] pub struct Printer<'a, 'b> { @@ -48,8 +48,8 @@ pub struct Printer<'a, 'b> { /// The view being drawn can ignore this, but anything to the top-left of /// this will actually be ignored, so it can be used to skip this part. /// - /// A print request `x`, will really print at `x - content_offset`. - pub content_offset: Vec2, + /// A print request `x`, will really print at `x + drawing_area_offset`. + pub drawing_area_offset: Vec2, /// Whether the view to draw is currently focused or not. pub focused: bool, @@ -80,7 +80,7 @@ impl<'a, 'b> Printer<'a, 'b> { let size = size.into(); Printer { offset: Vec2::zero(), - content_offset: Vec2::zero(), + drawing_area_offset: Vec2::zero(), output_size: size, size, focused: true, @@ -134,19 +134,18 @@ impl<'a, 'b> Printer<'a, 'b> { S: Into, F: FnOnce(&str) -> usize, { - // Where we are asked to start printing. Oh boy. It's not that simple. let start = start.into(); - // We accept requests between `content_offset` and - // `content_offset + output_size`. - if !start.strictly_lt(self.output_size + self.content_offset) { + // Refuse to print if the starting offset is out of the higher limits of the drawing area. + // The drawing area is a rectangle area combined with an offset. + if !start.strictly_lt(self.output_size + self.drawing_area_offset) { return; } - // If start < content_offset, part of the text will not be visible. + // If start < drawing_area_offset, part of the text will not be visible. // This is the part of the text that's hidden: - // (It should always be smaller than the content offset) - let hidden_part = self.content_offset.saturating_sub(start); + // (It should always be smaller than the drawing area offset) + let hidden_part = self.drawing_area_offset.saturating_sub(start); if hidden_part.y > 0 { // Since we are printing a single line, there's nothing we can do. return; @@ -185,10 +184,10 @@ impl<'a, 'b> Printer<'a, 'b> { text_width -= skipped_width; } - assert!(start.fits(self.content_offset)); + assert!(start.fits(self.drawing_area_offset)); // What we did before should guarantee that this won't overflow. - start = start - self.content_offset; + start = start - self.drawing_area_offset; // Do we have enough room for the entire line? let room = self.output_size.x - start.x; @@ -217,12 +216,12 @@ impl<'a, 'b> Printer<'a, 'b> { // Here again, we can abort if we're trying to print too far right or // too low. - if !start.strictly_lt(self.output_size + self.content_offset) { + if !start.strictly_lt(self.output_size + self.drawing_area_offset) { return; } // hidden_part describes how far to the top left of the viewport we are. - let hidden_part = self.content_offset.saturating_sub(start); + let hidden_part = self.drawing_area_offset.saturating_sub(start); if hidden_part.x > 0 || hidden_part.y >= height { // We're printing a single column, so we can't do much here. return; @@ -230,12 +229,12 @@ impl<'a, 'b> Printer<'a, 'b> { // Skip `hidden_part` let start = start + hidden_part; - assert!(start.fits(self.content_offset)); + assert!(start.fits(self.drawing_area_offset)); let height = height - hidden_part.y; // What we did before ensures this won't overflow. - let start = start - self.content_offset; + let start = start - self.drawing_area_offset; // Don't go overboard let height = min(height, self.output_size.y - start.y); @@ -265,11 +264,11 @@ impl<'a, 'b> Printer<'a, 'b> { let start = start.into(); // Nothing to be done if the start if too far to the bottom/right - if !start.strictly_lt(self.output_size + self.content_offset) { + if !start.strictly_lt(self.output_size + self.drawing_area_offset) { return; } - let hidden_part = self.content_offset.saturating_sub(start); + let hidden_part = self.drawing_area_offset.saturating_sub(start); if hidden_part.y > 0 || hidden_part.x >= width { // We're printing a single line, so we can't do much here. return; @@ -277,12 +276,12 @@ impl<'a, 'b> Printer<'a, 'b> { // Skip `hidden_part` let start = start + hidden_part; - assert!(start.fits(self.content_offset)); + assert!(start.fits(self.drawing_area_offset)); let width = width - hidden_part.x; // Don't go too far - let start = start - self.content_offset; + let start = start - self.drawing_area_offset; // Don't write too much if we're close to the end let repetitions = min(width, self.output_size.x - start.x) / c.width(); @@ -521,10 +520,10 @@ impl<'a, 'b> Printer<'a, 'b> { self.clone().with(|s| { // If we are drawing a part of the content, // let's reduce this first. - let consumed = Vec2::min(s.content_offset, offset); + let consumed = Vec2::min(s.drawing_area_offset, offset); let offset = offset - consumed; - s.content_offset = s.content_offset - consumed; + s.drawing_area_offset = s.drawing_area_offset - consumed; s.offset = s.offset + offset; @@ -626,7 +625,7 @@ impl<'a, 'b> Printer<'a, 'b> { self.shrinked(borders - half_borders).offset(half_borders) } - /// Returns a new sub-printer with a content offset. + /// Returns a new sub-printer with a drawing area offset. /// /// This is useful for parent views that only show a subset of their /// child, like `ScrollView`. @@ -636,7 +635,7 @@ impl<'a, 'b> Printer<'a, 'b> { S: Into, { self.clone().with(|s| { - s.content_offset = s.content_offset + offset; + s.drawing_area_offset = s.drawing_area_offset + offset; }) } diff --git a/cursive-core/src/view/mod.rs b/cursive-core/src/view/mod.rs index 26a87206..1a11131e 100644 --- a/cursive-core/src/view/mod.rs +++ b/cursive-core/src/view/mod.rs @@ -65,7 +65,7 @@ //! //! In some cases however it may be interesting for the nested view to know //! about this, maybe to avoid computing parts of the view that are not -//! visible. `Printer::output_size` and `Printer::content_offset` can be used +//! visible. `Printer::output_size` and `Printer::drawing_area_offset` can be used //! to find out what part of the view should actually be printed. //! //! ## Important Area diff --git a/cursive-core/src/view/scroll/mod.rs b/cursive-core/src/view/scroll/mod.rs index 34d21a2c..78f311f8 100644 --- a/cursive-core/src/view/scroll/mod.rs +++ b/cursive-core/src/view/scroll/mod.rs @@ -185,7 +185,7 @@ pub fn draw_lines( LineDrawer: FnMut(&T, &Printer, usize), { draw(scroller, printer, |s, printer| { - let start = printer.content_offset.y; + let start = printer.drawing_area_offset.y; let end = start + printer.output_size.y; for y in start..end { let printer = printer.offset((0, y)).cropped((printer.size.x, 1)); diff --git a/cursive-core/src/views/text_view.rs b/cursive-core/src/views/text_view.rs index 20f1f417..020ccfb6 100644 --- a/cursive-core/src/views/text_view.rs +++ b/cursive-core/src/views/text_view.rs @@ -407,7 +407,7 @@ impl View for TextView { .rows .iter() .enumerate() - .skip(printer.content_offset.y) + .skip(printer.drawing_area_offset.y) .take(printer.output_size.y) { let l = row.width; From 82f239dba56ff5129d2c5b8718b60a4db064183e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Langlois?= Date: Mon, 22 Aug 2022 09:54:33 +0200 Subject: [PATCH 2/3] Improve a comment --- cursive-core/src/printer.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cursive-core/src/printer.rs b/cursive-core/src/printer.rs index 42a6f9fb..ff644a4f 100644 --- a/cursive-core/src/printer.rs +++ b/cursive-core/src/printer.rs @@ -136,7 +136,8 @@ impl<'a, 'b> Printer<'a, 'b> { { let start = start.into(); - // Refuse to print if the starting offset is out of the higher limits of the drawing area. + // Refuse to print if the starting offset is out of the higher limits + // of the drawing area (too far right, or too low on the screen). // The drawing area is a rectangle area combined with an offset. if !start.strictly_lt(self.output_size + self.drawing_area_offset) { return; From 3436d7a85847ffb841244ccfafc20a05701c8998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Langlois?= Date: Mon, 22 Aug 2022 10:01:22 +0200 Subject: [PATCH 3/3] Rename 'output_size' member to 'drawing_area_size' --- cursive-core/src/printer.rs | 20 ++++++++++---------- cursive-core/src/view/mod.rs | 2 +- cursive-core/src/view/scroll/mod.rs | 2 +- cursive-core/src/views/text_view.rs | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/cursive-core/src/printer.rs b/cursive-core/src/printer.rs index ff644a4f..145e9ba3 100644 --- a/cursive-core/src/printer.rs +++ b/cursive-core/src/printer.rs @@ -35,7 +35,7 @@ pub struct Printer<'a, 'b> { /// /// The view being drawn can ingore this, but anything further than that /// will be ignored. - pub output_size: Vec2, + pub drawing_area_size: Vec2, /// Size allocated to the view. /// @@ -81,7 +81,7 @@ impl<'a, 'b> Printer<'a, 'b> { Printer { offset: Vec2::zero(), drawing_area_offset: Vec2::zero(), - output_size: size, + drawing_area_size: size, size, focused: true, enabled: true, @@ -139,7 +139,7 @@ impl<'a, 'b> Printer<'a, 'b> { // Refuse to print if the starting offset is out of the higher limits // of the drawing area (too far right, or too low on the screen). // The drawing area is a rectangle area combined with an offset. - if !start.strictly_lt(self.output_size + self.drawing_area_offset) { + if !start.strictly_lt(self.drawing_area_size + self.drawing_area_offset) { return; } @@ -191,7 +191,7 @@ impl<'a, 'b> Printer<'a, 'b> { start = start - self.drawing_area_offset; // Do we have enough room for the entire line? - let room = self.output_size.x - start.x; + let room = self.drawing_area_size.x - start.x; if room < text_width { // Drop the end of the text if it's too long @@ -217,7 +217,7 @@ impl<'a, 'b> Printer<'a, 'b> { // Here again, we can abort if we're trying to print too far right or // too low. - if !start.strictly_lt(self.output_size + self.drawing_area_offset) { + if !start.strictly_lt(self.drawing_area_size + self.drawing_area_offset) { return; } @@ -238,7 +238,7 @@ impl<'a, 'b> Printer<'a, 'b> { let start = start - self.drawing_area_offset; // Don't go overboard - let height = min(height, self.output_size.y - start.y); + let height = min(height, self.drawing_area_size.y - start.y); let start = start + self.offset; for y in 0..height { @@ -265,7 +265,7 @@ impl<'a, 'b> Printer<'a, 'b> { let start = start.into(); // Nothing to be done if the start if too far to the bottom/right - if !start.strictly_lt(self.output_size + self.drawing_area_offset) { + if !start.strictly_lt(self.drawing_area_size + self.drawing_area_offset) { return; } @@ -285,7 +285,7 @@ impl<'a, 'b> Printer<'a, 'b> { let start = start - self.drawing_area_offset; // Don't write too much if we're close to the end - let repetitions = min(width, self.output_size.x - start.x) / c.width(); + let repetitions = min(width, self.drawing_area_size.x - start.x) / c.width(); let start = start + self.offset; self.backend.print_at_rep(start, repetitions, c); @@ -528,7 +528,7 @@ impl<'a, 'b> Printer<'a, 'b> { s.offset = s.offset + offset; - s.output_size = s.output_size.saturating_sub(offset); + s.drawing_area_size = s.drawing_area_size.saturating_sub(offset); s.size = s.size.saturating_sub(offset); }) } @@ -575,7 +575,7 @@ impl<'a, 'b> Printer<'a, 'b> { { self.clone().with(|s| { let size = size.into(); - s.output_size = Vec2::min(s.output_size, size); + s.drawing_area_size = Vec2::min(s.drawing_area_size, size); s.size = Vec2::min(s.size, size); }) } diff --git a/cursive-core/src/view/mod.rs b/cursive-core/src/view/mod.rs index 1a11131e..cd4e42ae 100644 --- a/cursive-core/src/view/mod.rs +++ b/cursive-core/src/view/mod.rs @@ -65,7 +65,7 @@ //! //! In some cases however it may be interesting for the nested view to know //! about this, maybe to avoid computing parts of the view that are not -//! visible. `Printer::output_size` and `Printer::drawing_area_offset` can be used +//! visible. `Printer::drawing_area_size` and `Printer::drawing_area_offset` can be used //! to find out what part of the view should actually be printed. //! //! ## Important Area diff --git a/cursive-core/src/view/scroll/mod.rs b/cursive-core/src/view/scroll/mod.rs index 78f311f8..fa00c4b5 100644 --- a/cursive-core/src/view/scroll/mod.rs +++ b/cursive-core/src/view/scroll/mod.rs @@ -186,7 +186,7 @@ pub fn draw_lines( { draw(scroller, printer, |s, printer| { let start = printer.drawing_area_offset.y; - let end = start + printer.output_size.y; + let end = start + printer.drawing_area_size.y; for y in start..end { let printer = printer.offset((0, y)).cropped((printer.size.x, 1)); line_drawer(s, &printer, y); diff --git a/cursive-core/src/views/text_view.rs b/cursive-core/src/views/text_view.rs index 020ccfb6..be6a40e6 100644 --- a/cursive-core/src/views/text_view.rs +++ b/cursive-core/src/views/text_view.rs @@ -408,7 +408,7 @@ impl View for TextView { .iter() .enumerate() .skip(printer.drawing_area_offset.y) - .take(printer.output_size.y) + .take(printer.drawing_area_size.y) { let l = row.width; let mut x = self.align.h.get_offset(l, printer.size.x);