Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Printer class member 'content_offset' naming #672

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions cursive-core/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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.
///
Expand All @@ -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,
Expand Down Expand Up @@ -80,8 +80,8 @@ impl<'a, 'b> Printer<'a, 'b> {
let size = size.into();
Printer {
offset: Vec2::zero(),
content_offset: Vec2::zero(),
output_size: size,
drawing_area_offset: Vec2::zero(),
drawing_area_size: size,
size,
focused: true,
enabled: true,
Expand Down Expand Up @@ -134,19 +134,19 @@ impl<'a, 'b> Printer<'a, 'b> {
S: Into<Vec2>,
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 (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.drawing_area_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;
Expand Down Expand Up @@ -185,13 +185,13 @@ 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;
let room = self.drawing_area_size.x - start.x;

if room < text_width {
// Drop the end of the text if it's too long
Expand All @@ -217,28 +217,28 @@ 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.drawing_area_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;
}

// 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);
let height = min(height, self.drawing_area_size.y - start.y);

let start = start + self.offset;
for y in 0..height {
Expand All @@ -265,27 +265,27 @@ 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.drawing_area_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;
}

// 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();
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);
Expand Down Expand Up @@ -521,14 +521,14 @@ 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;

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);
})
}
Expand Down Expand Up @@ -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);
})
}
Expand Down Expand Up @@ -626,7 +626,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`.
Expand All @@ -636,7 +636,7 @@ impl<'a, 'b> Printer<'a, 'b> {
S: Into<Vec2>,
{
self.clone().with(|s| {
s.content_offset = s.content_offset + offset;
s.drawing_area_offset = s.drawing_area_offset + offset;
})
}

Expand Down
2 changes: 1 addition & 1 deletion cursive-core/src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::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
Expand Down
4 changes: 2 additions & 2 deletions cursive-core/src/view/scroll/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ pub fn draw_lines<T, LineDrawer>(
LineDrawer: FnMut(&T, &Printer, usize),
{
draw(scroller, printer, |s, printer| {
let start = printer.content_offset.y;
let end = start + printer.output_size.y;
let start = printer.drawing_area_offset.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);
Expand Down
4 changes: 2 additions & 2 deletions cursive-core/src/views/text_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ impl View for TextView {
.rows
.iter()
.enumerate()
.skip(printer.content_offset.y)
.take(printer.output_size.y)
.skip(printer.drawing_area_offset.y)
.take(printer.drawing_area_size.y)
{
let l = row.width;
let mut x = self.align.h.get_offset(l, printer.size.x);
Expand Down