Skip to content

Commit

Permalink
Merge pull request #75 from wiiznokes/dev
Browse files Browse the repository at this point in the history
feat: remove bridge.update call from update
  • Loading branch information
wiiznokes committed Jan 5, 2024
2 parents 1985c49 + b4fa6e7 commit 83c93ad
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 47 deletions.
5 changes: 2 additions & 3 deletions DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

## Todo/upgradable parts
- add cargo-packager a way to localize ressource: done but not merged
- add app icon on windows (nsis)
- add app icon on windows, with cargo packager (nsis)
- add resource folder only for nsis
- execute as admin by default: nsis
- return error from the windows server
- stop windows server with a command: implemented but could be improved with taking app state back
- get app_state back when we close the ui
Expand All @@ -42,7 +41,7 @@
- find out how to do blocking operation on the command pool of Iced, without blocking the thread
- support const value in trait that use enum_dispatch
- fix the time_stamp of env_logger

- pop when leaving the app, to save the current config or auto create temp config

## run specific test:
```
Expand Down
3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::{env, io};
// https://github.com/mxre/winres/

fn main() -> io::Result<()> {
if env::var_os("CARGO_CFG_WINDOWS").is_some() && std::env::var("PROFILE").unwrap() == "release" {
if env::var_os("CARGO_CFG_WINDOWS").is_some() && std::env::var("PROFILE").unwrap() == "release"
{
winres::WindowsResource::new()
.set_icon("resource/app_icon/app_icon150.ico")
.set_manifest_file("resource/windows/manifest.xml")
Expand Down
9 changes: 5 additions & 4 deletions data/src/update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use hardware::{HardwareBridge, HardwareBridgeT, Mode, Value};
use hardware::{HardwareBridge, Mode, Value};

use thiserror::Error;

Expand Down Expand Up @@ -42,14 +42,15 @@ impl Update {
}

// todo: remember what nodes are valid
/// Update graph in an optimal way. This shouln't be use
/// with a graphical interface.
/// Warning: doesn't call update from the bridge, it's the role of the caller.
pub fn optimized(
&mut self,
nodes: &mut Nodes,
root_nodes: &RootNodes,
bridge: &mut HardwareBridge,
) -> Result<()> {
bridge.update()?;

let mut updated: HashSet<Id> = HashSet::new();
for node_id in root_nodes {
if let Err(e) = Self::update_rec(nodes, node_id, &mut updated, bridge) {
Expand All @@ -60,7 +61,7 @@ impl Update {
}

/// Doesn't update root nodes and doesn't re update nodes that could have been updated (fans).
/// Also, don't call update function of the bridge, it must have been called by the caller
/// Warning: doesn't call update from the bridge, it's the role of the caller.
pub fn all(&mut self, nodes: &mut Nodes, bridge: &mut HardwareBridge) -> Result<()> {
let ids_to_update_sorted: Vec<Id>;
{
Expand Down
9 changes: 2 additions & 7 deletions hardware/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,8 @@ mod test {
fn update(bridge: &mut WindowsBridge, hardware: &Hardware) {
info!("");

bench(
|| {
bridge.update().unwrap();
"lhm".to_string()
},
"update",
);
bridge.update().unwrap();
std::thread::sleep(crate::TIME_TO_UPDATE);

for h in &hardware.controls {
get_value(bridge, &h.internal_index, &h.name);
Expand Down
13 changes: 11 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub fn run_cli(mut app_state: AppState) {
display_info(app_state.dir_manager.settings(), current_config);

loop {
if let Err(e) = app_state.bridge.update() {
error!("{}", e);
break;
}
std::thread::sleep(hardware::TIME_TO_UPDATE);

if let Err(e) = app_state.update.optimized(
&mut app_state.app_graph.nodes,
&app_state.app_graph.root_nodes,
Expand All @@ -38,9 +44,12 @@ pub fn run_cli(mut app_state: AppState) {
error!("{}", e);
}

let duration = Duration::from_millis(app_state.dir_manager.settings().update_delay);
let settings_update_delay =
Duration::from_millis(app_state.dir_manager.settings().update_delay)
- hardware::TIME_TO_UPDATE;
let final_delay = std::cmp::max(settings_update_delay, Duration::from_millis(50));

match rx.recv_timeout(duration) {
match rx.recv_timeout(final_delay) {
Ok(action) => match action {
UserAction::Quit => {
println!("quit requested");
Expand Down
6 changes: 6 additions & 0 deletions src/fake_integrated_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ fn test_config() {
};

for _ in 0..20 {
if let Err(e) = app_state.bridge.update() {
error!("{}", e);
break;
}
std::thread::sleep(hardware::TIME_TO_UPDATE);

app_state
.update
.optimized(
Expand Down
71 changes: 41 additions & 30 deletions ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,7 @@ impl cosmic::Application for Ui {
.current_config_text()
.to_owned();

let commands = Command::batch([
command::set_theme(to_cosmic_theme(&flags.dir_manager.settings().theme)),
command::message(cosmic::app::Message::App(AppMsg::Tick)),
]);

let ui_state = Ui {
let mut ui_state = Ui {
nodes_c: NodesC::new(flags.app_graph.nodes.values()),
app_state: flags,
core,
Expand All @@ -99,35 +94,24 @@ impl cosmic::Application for Ui {
current_config_cached,
is_updating: false,
};

let update_graph_command = ui_state.maybe_update_hardware_to_update_graph();

let commands = Command::batch([
command::set_theme(to_cosmic_theme(
&ui_state.app_state.dir_manager.settings().theme,
)),
update_graph_command,
]);

(ui_state, commands)
}

fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
let dir_manager = &mut self.app_state.dir_manager;

fn wait_update_to_finish(msg_to_send: AppMsg) -> Command<AppMsg> {
Command::perform(
async {
tokio::time::sleep(hardware::TIME_TO_UPDATE).await;
},
|_| cosmic::app::Message::App(msg_to_send),
)
}

match message {
AppMsg::Tick => {
if !self.is_updating {
self.is_updating = true;
if let Err(e) = self.app_state.bridge.update() {
error!("{}", e);
self.is_updating = false;
} else {
return wait_update_to_finish(AppMsg::UpdateGraph);
}
} else {
debug!("An update is already processing: skipping that one.");
}
}
AppMsg::Tick => return self.maybe_update_hardware_to_update_graph(),
AppMsg::UpdateGraph => {
if let Err(e) = self.app_state.update.all(
&mut self.app_state.app_graph.nodes,
Expand All @@ -139,7 +123,7 @@ impl cosmic::Application for Ui {
error!("{}", e);
self.is_updating = false;
} else {
return wait_update_to_finish(AppMsg::UpdateRootNodes);
return wait_hardware_update_to_finish(AppMsg::UpdateRootNodes);
}
}
AppMsg::UpdateRootNodes => {
Expand Down Expand Up @@ -393,7 +377,7 @@ impl cosmic::Application for Ui {
AppGraph::from_config(config, self.app_state.bridge.hardware());
self.nodes_c = NodesC::new(self.app_state.app_graph.nodes.values());

return command::message(cosmic::app::Message::App(AppMsg::Tick));
return self.maybe_update_hardware_to_update_graph();
}
None => {
self.current_config_cached.clear();
Expand Down Expand Up @@ -536,3 +520,30 @@ fn to_cosmic_theme(theme: &AppTheme) -> theme::Theme {
AppTheme::System => theme::system_preference(),
}
}

fn wait_hardware_update_to_finish(msg_to_send: AppMsg) -> Command<AppMsg> {
Command::perform(
async {
tokio::time::sleep(hardware::TIME_TO_UPDATE).await;
},
|_| cosmic::app::Message::App(msg_to_send),
)
}

impl Ui {
fn maybe_update_hardware_to_update_graph(&mut self) -> Command<AppMsg> {
if !self.is_updating {
self.is_updating = true;
if let Err(e) = self.app_state.bridge.update() {
error!("{}", e);
self.is_updating = false;
} else {
return wait_hardware_update_to_finish(AppMsg::UpdateGraph);
}
} else {
warn!("An update is already processing: skipping that one.");
}

Command::none()
}
}

0 comments on commit 83c93ad

Please sign in to comment.