calendar: Slow down scroll, add click navigation
This commit is contained in:
@@ -12,7 +12,7 @@ In nice, big letters!
|
|||||||
|
|
||||||
### 3 month calendar
|
### 3 month calendar
|
||||||
|
|
||||||
Scroll on it to navigate.
|
Scroll or click on the months to navigate.
|
||||||
|
|
||||||
### Battery level
|
### Battery level
|
||||||
|
|
||||||
|
|||||||
+19
-16
@@ -7,8 +7,8 @@ use chrono::{Date, DateTime, Datelike, Local};
|
|||||||
|
|
||||||
pub struct Calendar {
|
pub struct Calendar {
|
||||||
cur_date: Date<Local>,
|
cur_date: Date<Local>,
|
||||||
first_draw: bool,
|
dirty: bool,
|
||||||
offset: i32,
|
offset: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_month(
|
fn draw_month(
|
||||||
@@ -152,8 +152,8 @@ impl Calendar {
|
|||||||
pub fn new() -> Calendar {
|
pub fn new() -> Calendar {
|
||||||
Calendar {
|
Calendar {
|
||||||
cur_date: Local::now().date(),
|
cur_date: Local::now().date(),
|
||||||
first_draw: true,
|
dirty: true,
|
||||||
offset: 0,
|
offset: 0.0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -167,8 +167,9 @@ impl ModuleImpl for Calendar {
|
|||||||
) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> {
|
) -> Result<Vec<(i32, i32, i32, i32)>, ::std::io::Error> {
|
||||||
let time = time.date();
|
let time = time.date();
|
||||||
let mut t = time.with_day(1).unwrap();
|
let mut t = time.with_day(1).unwrap();
|
||||||
if self.offset != 0 {
|
let o = (self.offset / 100.0) as i32;
|
||||||
let mut month = (t.month() - 1) as i32 + self.offset;
|
if o != 0 {
|
||||||
|
let mut month = (t.month() - 1) as i32 + o;
|
||||||
let mut year = t.year();
|
let mut year = t.year();
|
||||||
while month > 11 {
|
while month > 11 {
|
||||||
year += 1;
|
year += 1;
|
||||||
@@ -212,8 +213,8 @@ impl ModuleImpl for Calendar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
|
fn update(&mut self, time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
|
||||||
if self.first_draw {
|
if self.dirty {
|
||||||
self.first_draw = false;
|
self.dirty = false;
|
||||||
Ok(true)
|
Ok(true)
|
||||||
} else if time.date() != self.cur_date || force {
|
} else if time.date() != self.cur_date || force {
|
||||||
self.cur_date = time.date();
|
self.cur_date = time.date();
|
||||||
@@ -226,16 +227,18 @@ impl ModuleImpl for Calendar {
|
|||||||
fn input(&mut self, input: Input) {
|
fn input(&mut self, input: Input) {
|
||||||
match input {
|
match input {
|
||||||
Input::Scroll { pos: _, x: _, y } => {
|
Input::Scroll { pos: _, x: _, y } => {
|
||||||
if y > 0.0 {
|
self.offset += y;
|
||||||
self.offset += 1;
|
self.dirty = true;
|
||||||
|
}
|
||||||
|
Input::Click { pos: (x, _), button: _ } => {
|
||||||
|
if x < 448 {
|
||||||
|
self.offset -= 100.0;
|
||||||
|
} else if x >= 896 {
|
||||||
|
self.offset += 100.0;
|
||||||
} else {
|
} else {
|
||||||
self.offset -= 1;
|
self.offset = 0.0;
|
||||||
}
|
}
|
||||||
self.first_draw = true;
|
self.dirty = true;
|
||||||
}
|
|
||||||
Input::Click { pos: _, button: _ } => {
|
|
||||||
self.offset = 0;
|
|
||||||
self.first_draw = true;
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user