Skip to content

Commit

Permalink
added error for password enter in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikwilkowski committed Jan 16, 2024
1 parent 88c8583 commit bded597
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ mod arc_rwlock_serde {
#[derive(Debug, Deserialize, Serialize)]
pub struct ConfigGeneral {
pub something: bool,
pub db_timeout: f64,
}

impl Default for Config {
fn default() -> Self {
Config {
general: Arc::new(RwLock::new(ConfigGeneral { something: true })),
general: Arc::new(RwLock::new(ConfigGeneral {
something: true,
db_timeout: 900.0,
})),
db: Arc::new(RwLock::new(Db::default())),
}
}
Expand All @@ -87,6 +91,7 @@ impl From<ConfigFile> for Config {
Config {
general: Arc::new(RwLock::new(ConfigGeneral {
something: config_file.general.something,
db_timeout: config_file.general.db_timeout,
})),
db: Arc::new(RwLock::new(Db {
timeout: config_file.db.timeout,
Expand Down
25 changes: 23 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// #![windows_subsystem = "windows"]

use std::{
sync::{Arc, RwLock},
time::Duration,
};

use floem::{
action::exec_after,
event::EventListener,
kurbo::Size,
menu::{Menu, MenuItem},
Expand Down Expand Up @@ -43,15 +49,30 @@ use crate::ui::password_view::password_view;

fn main() {
let password = create_rw_signal(String::from(""));
let error = create_rw_signal(String::from(""));
let config = Arc::new(RwLock::new(config::Config::new()));

let view = container(
dyn_container(
move || password.get(),
move |pass_value| {
if pass_value.is_empty() {
Box::new(password_view(password))
Box::new(password_view(password, error))
} else {
let timeout = config.read().unwrap().general.read().unwrap().db_timeout;
exec_after(Duration::from_secs_f64(timeout), move |_| {
password.set(String::from(""));
error.set(String::from(""));
});

// TODO: run encrypt and pass password to error RwSignal if there are any
if &password.get() == "fail" {
// TODO: remove this... just here to show how to pass errors to the UI
error.set(String::from("That's not the password silly!"));
password.set(String::from(""));
}
Box::new(
app_view(config::Config::new())
app_view(config.write().unwrap().clone())
.window_title(|| String::from("Vault"))
.window_menu(|| {
Menu::new("").entry(MenuItem::new("Menu item")).entry(
Expand Down
2 changes: 2 additions & 0 deletions src/ui/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ pub const C_SHADOW_3: Color = Color::rgb8(180, 180, 180);
pub const C_BG_TOOLTIP: Color = Color::rgb8(247, 248, 250);
pub const C_TEXT_TOOLTIP: Color = Color::rgb8(40, 44, 49);
pub const C_BORDER_TOOLTIP: Color = Color::rgb8(215, 216, 219);

pub const C_ERROR: Color = Color::rgb8(255, 0, 0);
20 changes: 12 additions & 8 deletions src/ui/password_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ use floem::{
reactive::{create_rw_signal, RwSignal},
style::Position,
view::View,
views::{container, Decorators},
views::{label, v_stack, Decorators},
EventPropagation,
};

use crate::ui::{colors::*, primitives::input_field::input_field};

pub fn password_view(password: RwSignal<String>) -> impl View {
pub fn password_view(
password: RwSignal<String>,
error: RwSignal<String>,
) -> impl View {
let value = create_rw_signal(String::from(""));

let input = input_field(value);
let input_id = input.id();

container(
v_stack((
input
.style(|s| s.width(250))
.placeholder("Enter password")
.on_event(EventListener::WindowGotFocus, move |_| {
input_id.request_focus();
EventPropagation::Continue
})
.request_focus(move || password.track())
.on_event(EventListener::KeyDown, move |event| {
let key = match event {
Event::KeyDown(k) => k.key.physical_key,
Expand All @@ -33,9 +33,12 @@ pub fn password_view(password: RwSignal<String>) -> impl View {
if key == PhysicalKey::Code(KeyCode::Enter) {
password.set(value.get());
}

input_id.request_focus();
EventPropagation::Continue
}),
)
label(move || error.get()).style(|s| s.color(C_ERROR)),
))
.style(|s| {
s.position(Position::Absolute)
.inset(0)
Expand All @@ -45,6 +48,7 @@ pub fn password_view(password: RwSignal<String>) -> impl View {
.justify_center()
.width_full()
.height_full()
.gap(0, 6)
.background(C_BG_MAIN.with_alpha_factor(0.8))
})
.on_event(EventListener::Click, move |_| EventPropagation::Stop)
Expand Down
1 change: 1 addition & 0 deletions vault_config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[general]
something = true
db_timeout = 900.0

[db]
timeout = 60
Expand Down

0 comments on commit bded597

Please sign in to comment.