diff --git a/src/log.rs b/src/log.rs index 5b98a67..695f1a4 100644 --- a/src/log.rs +++ b/src/log.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; use toml::value::{Date, Time}; use crate::config::Config; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Debug)] pub struct Log { pub date: Date, pub events: Vec @@ -64,7 +64,7 @@ impl Log { } } -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Clone, Debug)] pub struct Event { pub name: String, pub start: Time, diff --git a/src/main.rs b/src/main.rs index eeed74f..7da69fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ // Prevent console window in addition to Slint window in Windows release builds when, e.g., starting the app via file manager. Ignored on other platforms. #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] -use std::{error::Error, sync::{Arc, Mutex}}; +use std::{error::Error, rc::Rc, sync::{Arc, Mutex}}; use aliveline::{config::Config, load_config, log::{Event, Log}}; use chrono::{Datelike, Timelike}; -use slint::{Model, SharedString, ToSharedString, VecModel}; +use slint::{Model, ModelRc, SharedString, ToSharedString, VecModel}; use toml::value::{Date as TomlDate, Time}; slint::include_modules!(); @@ -26,10 +26,11 @@ impl From for Event { second: (event.start % 60) as u8, nanosecond: 0 }; + let endsecs = event.start + event.duration; let end = Time { - hour: start.hour + (event.duration / 3600) as u8, - minute: start.minute + ((event.duration % 3600) / 60) as u8, - second: start.second + (event.duration % 60) as u8, + hour: (endsecs / 3600) as u8, + minute: ((endsecs % 3600) / 60) as u8, + second: (endsecs % 60) as u8, nanosecond: 0 }; Event { start, end, name: event.label.to_string(), finished: event.finished } @@ -47,7 +48,26 @@ fn main() -> Result<(), Box> { let config: Arc = Arc::new(load_config()); let writing_log: Arc> = Arc::new(Mutex::new(Log::load_from(&config, date))); + { + println!("Log: {:?}", writing_log.lock().unwrap().events); + let ui_weak = ui.as_weak(); + let log = writing_log.clone(); + (move || { + println!("c"); + let ui = ui_weak.unwrap(); + let log_guard = log.lock().expect("Log shouldn't be used twice"); + let events: Vec = (*log_guard).events.iter().map(|event| TimelineEvent::from((*event).clone())).collect(); + let in_progress = events.iter().any(|event| !event.finished); + let model: ModelRc = Rc::new(VecModel::from(events)).into(); + println!("get: {:?}", model); + ui.set_record_events(model); + ui.set_in_progress(in_progress); + })() + } + ui.invoke_update_record_offset(offset as i32); + ui.invoke_load_log(); + ui.invoke_another_call(); ui.on_save_log({ let config = config.clone(); @@ -60,6 +80,14 @@ fn main() -> Result<(), Box> { } }); + ui.on_another_call({ + println!("outside move"); + move || { + println!("inside move"); + } + }); + + ui.on_update_record_visible_time({ let ui_weak = ui.as_weak(); move |hours_string: SharedString| { diff --git a/ui/app-window.slint b/ui/app-window.slint index ccd2ad2..f91b143 100644 --- a/ui/app-window.slint +++ b/ui/app-window.slint @@ -10,6 +10,8 @@ export component AppWindow inherits Window { callback chain-event <=> record.chain-event; callback update-record-offset(int); callback save-log; + callback another-call; + callback load-log; update-record-offset(new-offset) => { record.offset = new-offset; @@ -18,6 +20,7 @@ export component AppWindow inherits Window { in-out property record-events <=> record.events; in-out property record-offset <=> record.offset; in-out property record-visible-time <=> record.visible-time; + in-out property in-progress <=> record.in-progress; property<[string]> combo-spans: ["1 Hour", "4 Hours", "8 Hours", "24 Hours"]; title: "Aliveline"; diff --git a/ui/record.slint b/ui/record.slint index 0556e37..cf195af 100644 --- a/ui/record.slint +++ b/ui/record.slint @@ -11,7 +11,7 @@ export component RecordWidget inherits VerticalBox { in-out property offset <=> tl.offset; in-out property events <=> tl.events; in property<[string]> combo-spans: []; - property in-progress: false; + in-out property in-progress: false; property event-name <=> le.text; tl := Timeline {