From d1acdde89caa00e2fa3b48fe55e0f7c7d2093529 Mon Sep 17 00:00:00 2001 From: Ilya Zlobintsev Date: Thu, 29 Feb 2024 21:31:52 +0200 Subject: [PATCH] feat: improve fan curve point adjustment, add ability to manually set the speed value --- .../fan_curve_frame/point_adjustment.rs | 58 +++++++++++++++++-- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/lact-gui/src/app/root_stack/thermals_page/fan_curve_frame/point_adjustment.rs b/lact-gui/src/app/root_stack/thermals_page/fan_curve_frame/point_adjustment.rs index 9c92f0b..334ebdb 100644 --- a/lact-gui/src/app/root_stack/thermals_page/fan_curve_frame/point_adjustment.rs +++ b/lact-gui/src/app/root_stack/thermals_page/fan_curve_frame/point_adjustment.rs @@ -1,6 +1,7 @@ use glib::clone; use gtk::{ - glib, prelude::*, Adjustment, Box, Label, MenuButton, Orientation, Popover, Scale, SpinButton, + glib, prelude::*, Adjustment, Box, Grid, Label, MenuButton, Orientation, Popover, Scale, + SpinButton, }; #[derive(Clone)] @@ -12,8 +13,10 @@ pub struct PointAdjustment { impl PointAdjustment { pub fn new(parent: &Box, ratio: f32, temperature: i32) -> Self { let container = Box::new(Orientation::Vertical, 5); + container.set_margin_top(10); let ratio_adjustment = Adjustment::new(ratio.into(), 0.0, 1.0, 0.01, 0.05, 0.00); + let scale = Scale::builder() .orientation(Orientation::Vertical) .adjustment(&ratio_adjustment) @@ -24,19 +27,64 @@ impl PointAdjustment { container.append(&scale); let temperature_adjustment = Adjustment::new(temperature.into(), 0.0, 100.0, 1.0, 1.0, 0.0); + let temperature_selector = SpinButton::new(Some(&temperature_adjustment), 1.0, 0); + let ratio_selector = SpinButton::new(Some(&ratio_adjustment), 0.05, 2); + + temperature_selector.connect_input(|spin| { + let text = spin.text(); + let temp = text.trim_end_matches("°C"); + Some(Ok(temp.parse::().unwrap())) + }); + temperature_selector.connect_output(|spin| { + let text = format!("{}°C", spin.value_as_int()); + spin.set_text(&text); + glib::Propagation::Stop + }); + + ratio_selector.connect_input(|spin| { + let text = spin.text(); + let percentage = text.trim_end_matches('%'); + Some(Ok(percentage.parse::().unwrap() / 100.0)) + }); + ratio_selector.connect_output(|spin| { + let value = spin.value(); + let ratio = (value * 100.0).round(); + spin.set_text(&format!("{ratio}%")); + glib::Propagation::Stop + }); + + let popover_menu = Grid::builder() + .column_spacing(5) + .row_spacing(5) + .margin_start(5) + .margin_end(5) + .margin_top(5) + .margin_bottom(5) + .build(); + popover_menu.attach(&Label::new(Some("Speed:")), 0, 0, 1, 1); + popover_menu.attach(&ratio_selector, 1, 0, 1, 1); + popover_menu.attach(&Label::new(Some("Temperature:")), 0, 1, 1, 1); + popover_menu.attach(&temperature_selector, 1, 1, 1, 1); // Using the built-in MenuButton label function creates an empty icon - let temperature_label = Label::new(Some(&temperature.to_string())); + let text = format!("{}% at {temperature}°C", (ratio * 100.0).round()); + let temperature_label = Label::builder().label(text).use_markup(true).build(); temperature_adjustment.connect_value_changed( - clone!(@strong temperature_label => move |temperature_adjustment| { + clone!(@strong temperature_label, @strong ratio_adjustment => move |temperature_adjustment| { let temperature = temperature_adjustment.value(); - temperature_label.set_text(&temperature.to_string()); + let ratio = (ratio_adjustment.value() * 100.0).round(); + let text = format!("{ratio}% at {temperature}°C"); + temperature_label.set_markup(&text); }), ); - let popover = Popover::builder().child(&temperature_selector).build(); + ratio_adjustment.connect_value_changed(clone!(@strong temperature_adjustment => move |_| { + temperature_adjustment.emit_by_name::<()>("value-changed", &[]); + })); + + let popover = Popover::builder().child(&popover_menu).build(); let temperature_button = MenuButton::builder() .popover(&popover) .child(&temperature_label)