Skip to content

Commit

Permalink
Add Cursive::screen_size based on last layout phase
Browse files Browse the repository at this point in the history
  • Loading branch information
gyscos committed Oct 26, 2020
1 parent 58bbae5 commit fb23445
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
15 changes: 14 additions & 1 deletion cursive-core/src/cursive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use crate::{

static DEBUG_VIEW_NAME: &str = "_cursive_debug_view";

type RootView = views::OnEventView<views::ScreensView<views::StackView>>;

/// Central part of the cursive library.
///
/// It initializes ncurses on creation and cleans up on drop.
Expand All @@ -30,7 +32,7 @@ pub struct Cursive {
theme: theme::Theme,

// The main view
root: views::OnEventView<views::ScreensView<views::StackView>>,
root: RootView,

menubar: views::Menubar,

Expand All @@ -42,6 +44,8 @@ pub struct Cursive {
cb_source: Receiver<Box<dyn FnOnce(&mut Cursive) + Send>>,
cb_sink: Sender<Box<dyn FnOnce(&mut Cursive) + Send>>,

last_size: Vec2,

// User-provided data.
user_data: Box<dyn Any>,

Expand Down Expand Up @@ -84,6 +88,7 @@ impl Cursive {
views::StackView::new(),
)),
menubar: views::Menubar::new(),
last_size: Vec2::zero(),
needs_clear: true,
running: true,
cb_source,
Expand All @@ -96,7 +101,15 @@ impl Cursive {
cursive
}

/// Returns the screen size given in the last layout phase.
///
/// Note: this will return `(0, 0)` before the first layout phase.
pub fn screen_size(&self) -> Vec2 {
self.last_size
}

pub(crate) fn layout(&mut self, size: Vec2) {
self.last_size = size;
let offset = if self.menubar.autohide { 0 } else { 1 };
let size = size.saturating_sub((0, offset));
self.root.layout(size);
Expand Down
2 changes: 2 additions & 0 deletions cursive-core/src/cursive_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const INPUT_POLL_DELAY_MS: u64 = 30;
///
/// You can get one from `Cursive::runner`, then either call `.run()`, or
/// manually `.step()`.
///
/// The `C` type is usually either `Cursive` or `&mut Cursive`.
pub struct CursiveRunner<C> {
siv: C,
backend: Box<dyn backend::Backend>,
Expand Down
1 change: 1 addition & 0 deletions cursive-core/src/view/scroll/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ impl Core {
&& (self.offset.y + self.available_size().y
< self.inner_size.y) =>
{
// No `min` check here - we allow going over the edge.
self.offset.y += 5;
}
Event::Ctrl(Key::Down) | Event::Key(Key::Down)
Expand Down
16 changes: 5 additions & 11 deletions cursive-core/src/view/scroll/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,19 +210,13 @@ where
}

/// Implements `View::on_event` on the given model.
pub fn on_event<Model, GetScroller, OnEvent, ImportantArea>(
pub fn on_event<Model: ?Sized>(
event: Event,
model: &mut Model,
mut get_scroller: GetScroller,
mut on_event: OnEvent,
mut important_area: ImportantArea,
) -> EventResult
where
Model: ?Sized,
GetScroller: FnMut(&mut Model) -> &mut scroll::Core,
OnEvent: FnMut(&mut Model, Event) -> EventResult,
ImportantArea: FnMut(&Model, Vec2) -> Rect,
{
mut get_scroller: impl FnMut(&mut Model) -> &mut scroll::Core,
mut on_event: impl FnMut(&mut Model, Event) -> EventResult,
mut important_area: impl FnMut(&Model, Vec2) -> Rect,
) -> EventResult {
let mut relative_event = event.clone();
let inside = get_scroller(model).is_event_inside(&mut relative_event);
let result = if inside {
Expand Down

0 comments on commit fb23445

Please sign in to comment.