Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove time to update #121

Merged
merged 3 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
345 changes: 146 additions & 199 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions hardware/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use derive_more::Display;
use serde::Serialize;
use std::{fmt::Debug, rc::Rc, time::Duration};
use std::{fmt::Debug, rc::Rc};
use thiserror::Error;

#[macro_use]
Expand Down Expand Up @@ -131,9 +131,6 @@ pub fn new() -> Result<impl HardwareBridge> {
}

pub trait HardwareBridge {
/// Approximative time to update sensors on my pc
const TIME_TO_UPDATE: Duration = Duration::from_millis(0);

fn new() -> Result<Self>
where
Self: Sized;
Expand Down
2 changes: 0 additions & 2 deletions hardware/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,6 @@ impl WindowsBridge {
}

impl HardwareBridge for WindowsBridge {
const TIME_TO_UPDATE: Duration = Duration::from_millis(250);

fn new() -> crate::Result<Self> {
let process_handle = spawn_windows_server()?;
let stream = try_connect()?;
Expand Down
4 changes: 1 addition & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub fn run_cli<H: HardwareBridge>(mut app_state: AppState<H>) {
error!("{}", e);
break;
}
std::thread::sleep(H::TIME_TO_UPDATE);

if let Err(e) = app_state.update.optimized(
&mut app_state.app_graph.nodes,
Expand All @@ -45,8 +44,7 @@ pub fn run_cli<H: HardwareBridge>(mut app_state: AppState<H>) {
}

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

match rx.recv_timeout(final_delay) {
Expand Down
2 changes: 0 additions & 2 deletions src/fake_integrated_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ fn run<H: HardwareBridge>(mut app_state: AppState<H>) {
break;
}

std::thread::sleep(H::TIME_TO_UPDATE);

app_state
.update
.optimized(
Expand Down
4 changes: 2 additions & 2 deletions ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ git = "https://github.com/pop-os/libcosmic"
branch = "master"
default-features = false
features = [
"smol",
# disable because cause seg fault when quitting the app on flatpak
# cause seg fault when quitting the app on flatpak
"wgpu",
"winit",
"tokio",
"multi-window"
# "smol",
#"a11y",
#"debug",
#"serde-keycode",
Expand Down
81 changes: 33 additions & 48 deletions ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<H: HardwareBridge + 'static> cosmic::Application for Ui<H> {
.current_config_text()
.to_owned();

let mut ui_state = Ui {
let ui_state = Ui {
nodes_c: NodesC::new(flags.app_graph.nodes.values()),
app_state: flags,
core,
Expand All @@ -100,13 +100,11 @@ impl<H: HardwareBridge + 'static> cosmic::Application for Ui<H> {
graph_window: None,
};

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,
cosmic::app::command::message(cosmic::app::message::app(AppMsg::Tick)),
]);

(ui_state, commands)
Expand All @@ -116,29 +114,8 @@ impl<H: HardwareBridge + 'static> cosmic::Application for Ui<H> {
let dir_manager = &mut self.app_state.dir_manager;

match message {
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,
&mut self.app_state.bridge,
) {
error!("{}", e);
self.is_updating = false;
} else if let Err(e) = self.app_state.bridge.update() {
error!("{}", e);
self.is_updating = false;
} else {
return wait_hardware_update_to_finish::<H>(AppMsg::UpdateRootNodes);
}
}
AppMsg::UpdateRootNodes => {
if let Err(e) = self.app_state.update.nodes_which_update_can_change(
&mut self.app_state.app_graph.nodes,
&mut self.app_state.bridge,
) {
error!("{}", e);
}
self.is_updating = false;
AppMsg::Tick => {
self.update_hardware();
}

AppMsg::ModifNode(id, modif_node_msg) => {
Expand Down Expand Up @@ -403,7 +380,7 @@ impl<H: HardwareBridge + 'static> cosmic::Application for Ui<H> {
AppGraph::from_config(config, self.app_state.bridge.hardware());
self.nodes_c = NodesC::new(self.app_state.app_graph.nodes.values());

return self.maybe_update_hardware_to_update_graph();
self.update_hardware();
}
None => {
self.current_config_cached.clear();
Expand Down Expand Up @@ -606,29 +583,37 @@ fn to_cosmic_theme(theme: &AppTheme) -> theme::Theme {
}
}

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

impl<H: HardwareBridge> Ui<H> {
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::<H>(AppMsg::UpdateGraph);
}
} else {
fn update_hardware(&mut self) {
if self.is_updating {
warn!("An update is already processing: skipping that one.");
return;
}

Command::none()
self.is_updating = true;

if let Err(e) = self.app_state.bridge.update() {
error!("{}", e);
self.is_updating = false;
return;
}
if let Err(e) = self.app_state.update.all(
&mut self.app_state.app_graph.nodes,
&mut self.app_state.bridge,
) {
error!("{}", e);
self.is_updating = false;
return;
}

if let Err(e) = self.app_state.update.nodes_which_update_can_change(
&mut self.app_state.app_graph.nodes,
&mut self.app_state.bridge,
) {
error!("{}", e);
self.is_updating = false;
return;
}
self.is_updating = false;
}
}
2 changes: 0 additions & 2 deletions ui/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ use crate::graph::GraphWindowMsg;
#[derive(Debug, Clone)]
pub enum AppMsg {
Tick,
UpdateGraph,
UpdateRootNodes,

Config(ConfigMsg),
Settings(SettingsMsg),
Expand Down
Loading