shuffle around routines to better match original vault code

This commit is contained in:
bunnie 2025-06-03 00:14:49 +08:00
parent 1e680727a8
commit 10957971bc

View file

@ -8,8 +8,7 @@ use ux_api::service::gfx::Gfx;
use ux_api::widgets::ScrollableList;
use xous::CID;
use crate::ItemLists;
use crate::VaultMode;
use crate::*;
pub enum NavDir {
Up,
@ -45,6 +44,7 @@ pub struct VaultUi {
gfx: Gfx,
totp_list: ScrollableList,
item_lists: Arc<Mutex<ItemLists>>,
mode: Arc<Mutex<VaultMode>>,
/// totp redraw state
totp_code: Option<String>,
@ -52,7 +52,12 @@ pub struct VaultUi {
}
impl VaultUi {
pub fn new(xns: &xous_names::XousNames, cid: xous::CID, item_lists: Arc<Mutex<ItemLists>>) -> Self {
pub fn new(
xns: &xous_names::XousNames,
cid: xous::CID,
item_lists: Arc<Mutex<ItemLists>>,
mode: Arc<Mutex<VaultMode>>,
) -> Self {
let mut totp_list = ScrollableList::default()
.set_margin(TotpLayout::totp_margin())
.pane_size(TotpLayout::list_box())
@ -65,6 +70,7 @@ impl VaultUi {
gfx: Gfx::new(&xns).unwrap(),
totp_list,
item_lists,
mode,
totp_code: None,
last_epoch: crate::totp::get_current_unix_time().expect("couldn't get current time") / 30,
}
@ -74,72 +80,94 @@ impl VaultUi {
pub fn clear_area(&self) { self.gfx.clear().ok(); }
/// Redraw the text view onto the screen.
pub fn redraw_totp(&mut self) {
pub fn redraw(&mut self) {
// to reduce locking thrash, we cache a copy of the current mode at the top of redraw.
let mode_at_entry = (*self.mode.lock().unwrap()).clone();
self.gfx.clear().ok();
// decorative box around code
let mut totp_box = TotpLayout::totp_box();
totp_box.border.style = DrawStyle::new(PixelColor::Dark, PixelColor::Light, 1);
self.gfx.draw_rounded_rectangle(totp_box).ok();
match mode_at_entry {
VaultMode::Totp => {
// decorative box around code
let mut totp_box = TotpLayout::totp_box();
totp_box.border.style = DrawStyle::new(PixelColor::Dark, PixelColor::Light, 1);
self.gfx.draw_rounded_rectangle(totp_box).ok();
// the TOTP code
let mut tv = TextView::new(
Gid::dummy(),
TextBounds::CenteredTop(
TotpLayout::totp_box().border.translate_chain(TotpLayout::totp_font_vmargin()),
),
);
tv.invert = true;
tv.margin = Point::new(0, 0);
tv.style = TotpLayout::totp_font();
tv.draw_border = false;
// the TOTP code
let mut tv = TextView::new(
Gid::dummy(),
TextBounds::CenteredTop(
TotpLayout::totp_box().border.translate_chain(TotpLayout::totp_font_vmargin()),
),
);
tv.invert = true;
tv.margin = Point::new(0, 0);
tv.style = TotpLayout::totp_font();
tv.draw_border = false;
match &self.totp_code {
Some(code) => {
write!(tv, "{}", code).ok();
}
_ => {
write!(tv, "******").ok();
}
}
self.gfx.draw_textview(&mut tv).expect("couldn't draw text");
// list of codes to pick from
self.totp_list.draw(TotpLayout::timer_box().br().y);
// draw the timer element
let mut timer_box = TotpLayout::timer_box();
timer_box.style = DrawStyle::new(PixelColor::Dark, PixelColor::Light, 1);
self.gfx.draw_rectangle(timer_box).ok();
// draw the duration bar
let current_time = std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.map(|duration| duration.as_millis())
.expect("couldn't get time as millis");
// manage the epoch as well
let epoch = (current_time / (30 * 1000)) as u64;
if self.last_epoch != epoch {
self.last_epoch = epoch;
if !self.item_lists.lock().unwrap().is_db_empty(VaultMode::Totp) {
match crate::totp::db_str_to_code(
&self.item_lists.lock().unwrap().selected_extra(VaultMode::Totp),
) {
Ok(s) => self.totp_code = Some(s),
_ => self.totp_code = None,
match &self.totp_code {
Some(code) => {
write!(tv, "{}", code).ok();
}
_ => {
write!(tv, "******").ok();
}
}
self.gfx.draw_textview(&mut tv).expect("couldn't draw text");
// list of codes to pick from
self.totp_list.draw(TotpLayout::timer_box().br().y);
// draw the timer element
let mut timer_box = TotpLayout::timer_box();
timer_box.style = DrawStyle::new(PixelColor::Dark, PixelColor::Light, 1);
self.gfx.draw_rectangle(timer_box).ok();
// draw the duration bar
let current_time = std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.map(|duration| duration.as_millis())
.expect("couldn't get time as millis");
// manage the epoch as well
let epoch = (current_time / (30 * 1000)) as u64;
if self.last_epoch != epoch {
self.last_epoch = epoch;
if !self.item_lists.lock().unwrap().is_db_empty(VaultMode::Totp) {
match crate::totp::db_str_to_code(
&self.item_lists.lock().unwrap().selected_extra(VaultMode::Totp),
) {
Ok(s) => self.totp_code = Some(s),
_ => self.totp_code = None,
}
}
}
let mut timer_remaining = TotpLayout::timer_box();
let delta = (current_time - (self.last_epoch as u128 * 30 * 1000)) as isize;
let width = timer_remaining.width() as isize;
let delta_width = (delta * width * 128) / (30 * 128 * 1000);
timer_remaining.br = Point::new(width - delta_width, timer_remaining.br().y);
timer_remaining.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 1);
self.gfx.draw_rectangle(timer_remaining).ok();
}
VaultMode::Password => {
todo!()
}
}
let mut timer_remaining = TotpLayout::timer_box();
let delta = (current_time - (self.last_epoch as u128 * 30 * 1000)) as isize;
let width = timer_remaining.width() as isize;
let delta_width = (delta * width * 128) / (30 * 128 * 1000);
timer_remaining.br = Point::new(width - delta_width, timer_remaining.br().y);
timer_remaining.style = DrawStyle::new(PixelColor::Light, PixelColor::Light, 1);
self.gfx.draw_rectangle(timer_remaining).ok();
self.gfx.flush().ok();
}
pub(crate) fn nav(&mut self, dir: NavDir) {
self.item_lists.lock().unwrap().nav((*self.mode.lock().unwrap()).clone(), dir);
}
pub(crate) fn filter(&mut self, criteria: &String) {
self.item_lists.lock().unwrap().filter(self.mode.lock().unwrap().clone(), criteria);
}
pub(crate) fn selected_entry(&self) -> Option<SelectedEntry> {
let mode = (*self.mode.lock().unwrap()).clone();
self.item_lists.lock().unwrap().selected_entry(mode)
}
}