cursive/src/views/tracked_view.rs

46 lines
1.0 KiB
Rust
Raw Normal View History

use std::cell::Cell;
2016-07-28 23:36:01 +00:00
use view::{View, ViewWrapper};
use views::IdView;
2016-07-14 06:25:54 +00:00
use Printer;
2016-07-02 03:23:58 +00:00
use vec::Vec2;
/// Wrapper around a view that remembers its position.
2016-07-02 03:23:58 +00:00
pub struct TrackedView<T: View> {
/// Wrapped view.
2016-07-02 03:23:58 +00:00
pub view: T,
/// Last position the view was located.
offset: Cell<Vec2>,
}
impl<T: View> TrackedView<T> {
/// Return the last offset at which the view was drawn.
pub fn offset(&self) -> Vec2 {
self.offset.get()
}
2016-07-02 03:23:58 +00:00
}
2016-07-10 02:05:51 +00:00
impl<T: View> TrackedView<T> {
/// Creates a new `TrackedView` around `view`.
2016-07-02 03:23:58 +00:00
pub fn new(view: T) -> Self {
TrackedView {
view: view,
offset: Cell::new(Vec2::zero()),
2016-07-02 03:23:58 +00:00
}
}
/// Wraps itself in a `IdView` for easy retrieval.
2016-07-02 03:23:58 +00:00
pub fn with_id(self, id: &str) -> IdView<Self> {
IdView::new(id, self)
}
}
impl<T: View> ViewWrapper for TrackedView<T> {
wrap_impl!(&self.view);
fn wrap_draw(&self, printer: &Printer) {
self.offset.set(printer.offset);
2016-07-02 03:23:58 +00:00
self.view.draw(printer);
}
}