audio: Volume scale partly working now
This commit is contained in:
parent
4e95d4aead
commit
6160512abe
3 changed files with 41 additions and 17 deletions
|
|
@ -12,6 +12,7 @@ use pa::PA;
|
||||||
mod task;
|
mod task;
|
||||||
mod volume;
|
mod volume;
|
||||||
mod volume_scale;
|
mod volume_scale;
|
||||||
|
use volume_scale::VolumeScale;
|
||||||
|
|
||||||
use futures::{channel::mpsc, stream::StreamExt};
|
use futures::{channel::mpsc, stream::StreamExt};
|
||||||
use gtk4::{
|
use gtk4::{
|
||||||
|
|
@ -94,7 +95,7 @@ fn app(application: &Application) {
|
||||||
append: output_icon = &Image {
|
append: output_icon = &Image {
|
||||||
set_icon_name: Some("audio-speakers-symbolic"),
|
set_icon_name: Some("audio-speakers-symbolic"),
|
||||||
},
|
},
|
||||||
append: output_volume = &Scale::with_range(Orientation::Horizontal, 0., 100., 1.) {
|
append: output_volume = &VolumeScale::new(pa.clone(), true) {
|
||||||
set_format_value_func: |_, value| {
|
set_format_value_func: |_, value| {
|
||||||
format!("{:.0}%", value)
|
format!("{:.0}%", value)
|
||||||
},
|
},
|
||||||
|
|
@ -108,7 +109,7 @@ fn app(application: &Application) {
|
||||||
append: input_icon = &Image {
|
append: input_icon = &Image {
|
||||||
set_icon_name: Some("audio-input-microphone-symbolic"),
|
set_icon_name: Some("audio-input-microphone-symbolic"),
|
||||||
},
|
},
|
||||||
append: input_volume = &Scale::with_range(Orientation::Horizontal, 0., 100., 1.) {
|
append: input_volume = &VolumeScale::new(pa.clone(), false) {
|
||||||
set_format_value_func: |_, value| {
|
set_format_value_func: |_, value| {
|
||||||
format!("{:.0}%", value)
|
format!("{:.0}%", value)
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
use gtk4::{prelude::*, Scale};
|
use gtk4::prelude::*;
|
||||||
use libpulse_binding::volume::Volume;
|
use libpulse_binding::volume::Volume;
|
||||||
|
|
||||||
use crate::pa::DeviceInfo;
|
use crate::{pa::DeviceInfo, volume_scale::VolumeScale};
|
||||||
|
|
||||||
pub fn update_volume(device: &DeviceInfo, scale: &Scale) {
|
pub fn update_volume(device: &DeviceInfo, scale: &VolumeScale) {
|
||||||
scale.set_value((device.volume.avg().0 as f64 / Volume::NORMAL.0 as f64) * 100.);
|
scale.set_name(device.name.clone());
|
||||||
|
scale.set_volume(&device.volume);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
use gtk4::{glib, prelude::*};
|
use gtk4::{glib, prelude::*, subclass::prelude::*};
|
||||||
use libpulse_binding::volume::{ChannelVolumes, Volume};
|
use libpulse_binding::volume::{ChannelVolumes, Volume};
|
||||||
use std::{
|
use std::{
|
||||||
cell::{Cell, RefCell},
|
cell::{Cell, RefCell},
|
||||||
|
|
@ -11,21 +11,41 @@ use crate::PA;
|
||||||
|
|
||||||
// Component
|
// Component
|
||||||
|
|
||||||
struct VolumeScale {
|
#[derive(Default)]
|
||||||
scale: gtk4::Scale,
|
pub struct VolumeScaleImp {
|
||||||
name: Rc<RefCell<Option<String>>>,
|
name: Rc<RefCell<Option<String>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[glib::object_subclass]
|
||||||
|
impl ObjectSubclass for VolumeScaleImp {
|
||||||
|
const NAME: &'static str = "VolumeScale";
|
||||||
|
type Type = VolumeScale;
|
||||||
|
type ParentType = gtk4::Scale;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ObjectImpl for VolumeScaleImp {}
|
||||||
|
impl WidgetImpl for VolumeScaleImp {}
|
||||||
|
impl RangeImpl for VolumeScaleImp {}
|
||||||
|
impl ScaleImpl for VolumeScaleImp {}
|
||||||
|
|
||||||
|
glib::wrapper! {
|
||||||
|
pub struct VolumeScale(ObjectSubclass<VolumeScaleImp>)
|
||||||
|
@extends gtk4::Scale, gtk4::Range, gtk4::Widget,
|
||||||
|
@implements gtk4::Accessible, gtk4::Orientable;
|
||||||
|
}
|
||||||
|
|
||||||
impl VolumeScale {
|
impl VolumeScale {
|
||||||
fn new(pa: PA, sink: bool) {
|
pub fn new(pa: PA, sink: bool) -> Self {
|
||||||
let name: Rc<RefCell<Option<String>>> = Rc::new(RefCell::new(None));
|
let scale: VolumeScale = glib::Object::new(&[]).unwrap();
|
||||||
let scale = gtk4::Scale::with_range(gtk4::Orientation::Horizontal, 0., 100., 1.);
|
scale.set_range(0., 100.);
|
||||||
|
let name = scale.imp().name.clone();
|
||||||
let updater = Updater::new(move |value: f64| {
|
let updater = Updater::new(move |value: f64| {
|
||||||
let name = name.clone();
|
let name = name.clone();
|
||||||
let pa = pa.clone();
|
let pa = pa.clone();
|
||||||
async move {
|
async move {
|
||||||
let mut volumes = ChannelVolumes::default();
|
let mut volumes = ChannelVolumes::default();
|
||||||
volumes.set(0, Volume((value * 100.) as _)); // XXX ?
|
let volume = value * (Volume::NORMAL.0 as f64) / 100.;
|
||||||
|
volumes.set(1, Volume(volume as _)); // XXX ?
|
||||||
|
|
||||||
let name_ref = name.borrow();
|
let name_ref = name.borrow();
|
||||||
if let Some(name) = name_ref.as_deref() {
|
if let Some(name) = name_ref.as_deref() {
|
||||||
|
|
@ -45,14 +65,16 @@ impl VolumeScale {
|
||||||
updater.update(value);
|
updater.update(value);
|
||||||
gtk4::Inhibit(false)
|
gtk4::Inhibit(false)
|
||||||
});
|
});
|
||||||
|
scale
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_value(&self, value: f64) {
|
pub fn set_volume(&self, volume: &ChannelVolumes) {
|
||||||
self.scale.set_value(value);
|
let value = volume.avg().0 as f64 / (Volume::NORMAL.0 as f64) * 100.;
|
||||||
|
self.set_value(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_name(&self, name: Option<String>) {
|
pub fn set_name(&self, name: Option<String>) {
|
||||||
*self.name.borrow_mut() = name;
|
*self.imp().name.borrow_mut() = name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue