Skip to content

Commit

Permalink
impl warning when not saved + fix tooltips
Browse files Browse the repository at this point in the history
  • Loading branch information
wiiznokes committed Nov 7, 2024
1 parent 80d09b9 commit 6cc1e1b
Show file tree
Hide file tree
Showing 17 changed files with 123 additions and 49 deletions.
11 changes: 10 additions & 1 deletion data/src/config/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
update::UpdateError,
};

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[derive(Serialize, Deserialize, Debug, Clone, Default, Eq)]
pub struct Control {
// unique
pub name: String,
Expand All @@ -27,6 +27,15 @@ pub struct Control {
pub mode_set: Option<Mode>,
}

impl PartialEq for Control {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.hardware_id == other.hardware_id
&& self.input == other.input
&& self.active == other.active
}
}

impl Control {
pub fn new(
name: String,
Expand Down
2 changes: 1 addition & 1 deletion data/src/config/custom_temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
update::UpdateError,
};

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
pub struct CustomTemp {
pub name: String,
pub kind: CustomTempKind,
Expand Down
8 changes: 7 additions & 1 deletion data/src/config/fan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
use hardware::{HSensor, Hardware, HardwareBridge, Value};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[derive(Serialize, Deserialize, Debug, Clone, Default, Eq)]
pub struct Fan {
// unique
pub name: String,
Expand All @@ -21,6 +21,12 @@ pub struct Fan {
pub fan_h: Option<Rc<HSensor>>,
}

impl PartialEq for Fan {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.hardware_id == other.hardware_id
}
}

impl Fan {
pub fn get_value<H: HardwareBridge>(&self, bridge: &mut H) -> Result<Value, UpdateError> {
match &self.fan_h {
Expand Down
2 changes: 1 addition & 1 deletion data/src/config/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
app_graph::AppGraph,
node::{IsValid, Node, NodeType, ToNode},
};
#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Flat {
pub name: String,
pub value: u16,
Expand Down
67 changes: 37 additions & 30 deletions data/src/config/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
app_graph::AppGraph,
node::{IsValid, Node, NodeType, ToNode},
update::UpdateError,
utils::{has_duplicate, is_sorted, InsertSorted, RemoveElem},
};

use super::utils::affine::Affine;
Expand Down Expand Up @@ -48,18 +47,31 @@ impl Coord {
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Eq)]
pub struct Graph {
// unique
pub name: String,
// sorted
// temp unique
// 0 <= percent <= 100
#[serde(rename = "coord")]
pub coords: Vec<Coord>,
pub coords: BTreeSet<Coord>,
pub input: Option<String>, // Temp or CustomTemp
}

impl PartialEq for Graph {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.input == other.input
&& self.coords.len() == other.coords.len()
&& self
.coords
.iter()
.zip(&other.coords)
.all(|(a, b)| a.exact_same(b))
}
}

impl Default for Graph {
fn default() -> Self {
Self {
Expand All @@ -73,7 +85,9 @@ impl Default for Graph {
temp: 70,
percent: 100,
},
],
]
.into_iter()
.collect(),
input: Default::default(),
}
}
Expand All @@ -95,17 +109,12 @@ impl ToNode for Graph {

self.coords = deduplicator.into_iter().collect();

debug_assert!(!has_duplicate(&self.coords));
debug_assert!(is_sorted(&self.coords));

Node::new(NodeType::Graph(self), app_graph)
}
}

impl IsValid for Graph {
fn is_valid(&self) -> bool {
debug_assert!(!has_duplicate(&self.coords));
debug_assert!(is_sorted(&self.coords));
self.input.is_some() && !self.coords.is_empty()
}
}
Expand All @@ -126,9 +135,9 @@ impl Graph {

let coord = Coord { temp, percent };

if self.coords.binary_search(&coord).is_ok() {
if self.coords.contains(&coord) {
return Err(format!(
"Can't add create this new coord {}, this temp is already present",
"Can't create this new coord {}, this temp is already present",
temp
)
.into());
Expand All @@ -138,32 +147,28 @@ impl Graph {
}

pub fn get_value(&self, value: Value) -> Result<Value, UpdateError> {
debug_assert!(!has_duplicate(&self.coords));
debug_assert!(is_sorted(&self.coords));

let dummy_coord = Coord {
temp: value as u8,
percent: 0,
};

let res = match self.coords.binary_search(&dummy_coord) {
Ok(index) => self.coords[index].percent as Value,
Err(index) => {
if index == 0 {
self.coords[index].percent as Value
} else if index == self.coords.len() {
self.coords[index - 1].percent as Value
} else {
let coord1 = &self.coords[index - 1];
let coord2 = &self.coords[index];

Affine {
let res = match self.coords.get(&dummy_coord) {
Some(c) => c.percent as Value,
None => {
let lower_bound = self.coords.range(..=dummy_coord).next_back();
let upper_bound = self.coords.range(dummy_coord..).next();

match (lower_bound, upper_bound) {
(Some(coord), None) | (None, Some(coord)) => coord.percent as Value,
(Some(coord1), Some(coord2)) => Affine {
xa: coord1.temp.into(),
ya: coord1.percent.into(),
xb: coord2.temp.into(),
yb: coord2.percent.into(),
}
.calcule(value) as Value
.calcule(value) as Value,

_ => panic!("internal error: no value for graph"),
}
}
};
Expand All @@ -172,10 +177,10 @@ impl Graph {
}

pub fn add_coord(&mut self, new: Coord) {
self.coords.insert_sorted(|c| c.cmp(&new), new);
self.coords.insert(new);
}
pub fn remove_coord(&mut self, coord: &Coord) {
self.coords.remove_elem(|c| c.exact_same(coord));
self.coords.remove(coord);
}
pub fn replace_coord(&mut self, prev: &Coord, new: Coord) {
self.remove_coord(prev);
Expand Down Expand Up @@ -214,7 +219,9 @@ mod test {
temp: 40,
percent: 5,
},
],
]
.into_iter()
.collect(),
input: None,
};

Expand Down
2 changes: 1 addition & 1 deletion data/src/config/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};

use super::utils::affine::Affine;

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct Linear {
pub name: String,
#[serde(rename = "minTemp", alias = "min_temp")]
Expand Down
2 changes: 1 addition & 1 deletion data/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
pub struct Config {
#[serde(default, rename = "Control")]
pub controls: Vec<Control>,
Expand Down
4 changes: 3 additions & 1 deletion data/src/config/serde_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ fn config1() -> Config {
temp: 50,
percent: 30,
},
],
]
.into_iter()
.collect(),
input: Some("max".into()),
}],
flats: vec![Flat {
Expand Down
13 changes: 12 additions & 1 deletion data/src/config/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
use hardware::{Hardware, Value};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Eq)]
pub struct Target {
pub name: String,
#[serde(rename = "idleTemp", alias = "idle_temp")]
Expand All @@ -23,6 +23,17 @@ pub struct Target {
pub idle_has_been_reatch: bool,
}

impl PartialEq for Target {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
&& self.idle_temp == other.idle_temp
&& self.idle_speed == other.idle_speed
&& self.load_temp == other.load_temp
&& self.load_speed == other.load_speed
&& self.input == other.input
}
}

impl Target {
pub fn get_value(&mut self, value: Value) -> Result<Value, UpdateError> {
if self.idle_has_been_reatch {
Expand Down
8 changes: 7 additions & 1 deletion data/src/config/temp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
update::UpdateError,
};

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
#[derive(Serialize, Deserialize, Debug, Clone, Default, Eq)]
pub struct Temp {
pub name: String,
#[serde(rename = "id")]
Expand All @@ -19,6 +19,12 @@ pub struct Temp {
pub temp_h: Option<Rc<HSensor>>,
}

impl PartialEq for Temp {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.hardware_id == other.hardware_id
}
}

impl Temp {
pub fn get_value<H: HardwareBridge>(&self, bridge: &mut H) -> Result<Value, UpdateError> {
match &self.temp_h {
Expand Down
3 changes: 3 additions & 0 deletions i18n/en/ui.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ create_config = Create configuration
already_used_error = This name is already being use
invalid_value_error = this value is invalid
# Warning
config_not_saved = Configuration not saved
# Dialogs
udev_rules_dialog_ok = I understand
udev_rules_dialog_remind_later = Remind me Later
Expand Down
1 change: 1 addition & 0 deletions res/icons/warning/20px.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions res/icons/warning/24px.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions res/icons/warning/40px.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions res/icons/warning/48px.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 6cc1e1b

Please sign in to comment.