From 9735e9580ad6b2f600143df43d6c6e5fe889534c Mon Sep 17 00:00:00 2001 From: dmitry-erin Date: Mon, 2 Sep 2024 11:00:26 +0300 Subject: [PATCH] Extend Audio settings widget with footer and custom property Signed-off-by: dmitry-erin --- src/audio_settings.rs | 42 ++++++++++++++++++++++++++++++++++++--- src/ui/audio_settings.ui | 43 ++++++++++++++++++++++++++++++++++++++++ src/ui/settings.ui | 4 +++- src/ui/vm_settings.ui | 1 + src/vm_settings.rs | 2 +- 5 files changed, 87 insertions(+), 5 deletions(-) diff --git a/src/audio_settings.rs b/src/audio_settings.rs index 4db0512..eb40684 100755 --- a/src/audio_settings.rs +++ b/src/audio_settings.rs @@ -2,14 +2,15 @@ use std::cell::RefCell; use std::sync::OnceLock; use gtk::prelude::*; use gtk::subclass::prelude::*; -use gtk::{glib, CompositeTemplate, DropDown, Scale}; +use gtk::{glib, CompositeTemplate, Box, DropDown, Scale}; use glib::{Binding, Properties}; use glib::subclass::Signal; mod imp { use super::*; - #[derive(Default, CompositeTemplate)] + #[derive(Default, CompositeTemplate, Properties)] + #[properties(wrapper_type = super::AudioSettings)] #[template(resource = "/org/gnome/controlpanelgui/ui/audio_settings.ui")] pub struct AudioSettings { pub name: String, @@ -22,6 +23,11 @@ mod imp { pub speaker_switch: TemplateChild, #[template_child] pub speaker_volume: TemplateChild, + #[template_child] + pub footer: TemplateChild, + + #[property(name = "footer-visible", get, set, type = bool)] + footer_visible: RefCell, // Vector holding the bindings to properties of `Object` pub bindings: RefCell>, @@ -68,9 +74,34 @@ mod imp { let value = scale.value(); self.obj().emit_by_name::<()>("speaker-volume-changed", &[&value]); } + #[template_callback] + fn on_reset_clicked(&self) { + println!("Reset to defaults!"); + self.obj().emit_by_name::<()>("set-defaults", &[]); + } + #[template_callback] + fn on_save_clicked(&self) { + println!("Apply new!"); + let mic = self.mic_switch.selected(); + let speaker = self.speaker_switch.selected(); + let mic_volume = self.mic_volume.value(); + let speaker_volume = self.speaker_volume.value(); + self.obj().emit_by_name::<()>("apply-new", &[&mic, &speaker, &mic_volume, &speaker_volume]); + } }//end #[gtk::template_callbacks] + #[glib::derived_properties] impl ObjectImpl for AudioSettings { + fn constructed(&self) { + self.parent_constructed(); + + // After the object is constructed, bind the footer visibilty property + let obj = self.obj(); + obj.bind_property("footer-visible", &self.footer.get(), "visible") + .flags(glib::BindingFlags::DEFAULT) + .build(); + } + fn signals() -> &'static [Signal] { static SIGNALS: OnceLock> = OnceLock::new(); SIGNALS.get_or_init(|| { @@ -86,7 +117,12 @@ mod imp { .build(), Signal::builder("speaker-volume-changed") .param_types([f64::static_type()]) - .build() + .build(), + Signal::builder("set-defaults") + .build(), + Signal::builder("apply-new") + .param_types([u32::static_type(), u32::static_type(), f64::static_type(), f64::static_type()]) + .build(), ] }) } diff --git a/src/ui/audio_settings.ui b/src/ui/audio_settings.ui index cec452d..3541382 100755 --- a/src/ui/audio_settings.ui +++ b/src/ui/audio_settings.ui @@ -115,5 +115,48 @@ true + + + true + + + + + vertical + end + + + + + + + horizontal + + + Reset to defaults + + + 140 + 30 + + + + + true + + + + + Save + + + 60 + 30 + + + + + + \ No newline at end of file diff --git a/src/ui/settings.ui b/src/ui/settings.ui index 2d43c97..7cc0f0a 100644 --- a/src/ui/settings.ui +++ b/src/ui/settings.ui @@ -282,7 +282,9 @@ audio - + + true + diff --git a/src/ui/vm_settings.ui b/src/ui/vm_settings.ui index edd64d8..cc24a35 100644 --- a/src/ui/vm_settings.ui +++ b/src/ui/vm_settings.ui @@ -122,6 +122,7 @@ + false diff --git a/src/vm_settings.rs b/src/vm_settings.rs index c9f4fba..6ab27f6 100644 --- a/src/vm_settings.rs +++ b/src/vm_settings.rs @@ -3,7 +3,7 @@ use std::sync::OnceLock; use gtk::prelude::*; use gtk::subclass::prelude::*; use gtk::{glib, CompositeTemplate, Label, Image, MenuButton, Popover}; -use glib::{Binding, ToValue}; +use glib::Binding; use glib::subclass::Signal; use crate::vm_gobject::VMGObject;