feat: very basic open and view

This commit is contained in:
electria 2026-08-25 20:10:39 -07:00
commit 86075c213e
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -33,7 +33,7 @@ struct State {
db_path: String,
db_path_exists: bool,
db_loaded: bool,
db: Option<keepass::Database>,
password: String,
@ -81,18 +81,27 @@ impl State {
Message::ChangePassword(password) => self.password = password,
Message::ChangeSecurity(bool) => self.is_secure = bool,
Message::OpenDb => todo!(),
Message::OpenDb => {
match self.open_db() {
Err(e) => eprintln!("failed to open db '{}': {e}", self.db_path),
Ok(()) => {
#[allow(unused_must_use)]
db::set_last_opened(&self.db_path)
.inspect_err(|e| eprintln!("failed to set last-opened db: {e}"));
}
};
}
Message::FocusNext => return widget::operation::focus_next(),
Message::FocusPrevious => return widget::operation::focus_previous(),
Message::Enter => {
if !self.db_loaded && self.db_path_exists {
if self.db.is_none() && self.db_path_exists {
return Task::done(Message::OpenDb);
}
}
Message::Pick => {
if !self.db_loaded {
if self.db.is_none() {
return Task::done(Message::PickDb);
}
}
@ -101,7 +110,23 @@ impl State {
Task::none()
}
fn view(&self) -> Element<'_> {
if !self.db_loaded {
if let Some(db) = self.db.as_ref() {
widget::scrollable(widget::row([
widget::column(db.iter_all_entries().map(|entry| {
widget::text(entry.get_title().unwrap_or_default().to_string()).into()
}))
.into(),
widget::column(db.iter_all_entries().map(|entry| {
widget::text(entry.get_username().unwrap_or_default().to_string()).into()
}))
.into(),
widget::column(db.iter_all_entries().map(|entry| {
widget::text(entry.get_url().unwrap_or_default().to_string()).into()
}))
.into(),
]))
.into()
} else {
widget::container(
widget::column([
widget::row([
@ -138,8 +163,6 @@ impl State {
)
.center(Length::Fill)
.into()
} else {
widget::space().into()
}
}
@ -165,6 +188,20 @@ impl State {
_ => None,
})
}
fn open_db(&mut self) -> Result<(), String> {
debug_assert!(self.db_path_exists);
self.db = Some(
keepass::Database::open(
&mut fs::File::open(&self.db_path).map_err(|e| e.to_string())?,
keepass::DatabaseKey::new().with_password(&self.password),
)
.map_err(|e| e.to_string())?,
);
Ok(())
}
}
pub fn run() -> Result<(), iced::Error> {