mirror of
https://github.com/Xetibo/ReSet.git
synced 2025-04-17 10:18:32 +02:00
chore: move bluetooth handlers to separate file
This commit is contained in:
parent
0fe99fa3a6
commit
e62559a966
|
@ -22,6 +22,10 @@ use crate::components::bluetooth::bluetooth_box_impl;
|
||||||
use crate::components::bluetooth::bluetooth_entry::BluetoothEntry;
|
use crate::components::bluetooth::bluetooth_entry::BluetoothEntry;
|
||||||
use crate::components::utils::{BASE, BLUETOOTH, DBUS_PATH};
|
use crate::components::utils::{BASE, BLUETOOTH, DBUS_PATH};
|
||||||
|
|
||||||
|
use super::bluetooth_event_handlers::{
|
||||||
|
device_added_handler, device_changed_handler, device_removed_handler,
|
||||||
|
};
|
||||||
|
|
||||||
glib::wrapper! {
|
glib::wrapper! {
|
||||||
pub struct BluetoothBox(ObjectSubclass<bluetooth_box_impl::BluetoothBox>)
|
pub struct BluetoothBox(ObjectSubclass<bluetooth_box_impl::BluetoothBox>)
|
||||||
@extends gtk::Box, gtk::Widget,
|
@extends gtk::Box, gtk::Widget,
|
||||||
|
@ -405,90 +409,6 @@ fn bluetooth_listener_loop(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn device_changed_handler(
|
|
||||||
device_changed_box: Arc<BluetoothBox>,
|
|
||||||
ir: BluetoothDeviceChanged,
|
|
||||||
) -> bool {
|
|
||||||
let bluetooth_box = device_changed_box.clone();
|
|
||||||
glib::spawn_future(async move {
|
|
||||||
glib::idle_add_once(move || {
|
|
||||||
let imp = bluetooth_box.imp();
|
|
||||||
let mut map = imp.available_devices.borrow_mut();
|
|
||||||
if let Some(list_entry) = map.get_mut(&ir.bluetooth_device.path) {
|
|
||||||
let mut existing_bluetooth_device = list_entry.imp().bluetooth_device.borrow_mut();
|
|
||||||
if existing_bluetooth_device.connected != ir.bluetooth_device.connected {
|
|
||||||
if ir.bluetooth_device.connected {
|
|
||||||
imp.reset_bluetooth_available_devices.remove(&**list_entry);
|
|
||||||
imp.reset_bluetooth_connected_devices.add(&**list_entry);
|
|
||||||
} else {
|
|
||||||
imp.reset_bluetooth_connected_devices.remove(&**list_entry);
|
|
||||||
imp.reset_bluetooth_available_devices.add(&**list_entry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if existing_bluetooth_device.bonded != ir.bluetooth_device.bonded {
|
|
||||||
if ir.bluetooth_device.bonded {
|
|
||||||
list_entry
|
|
||||||
.imp()
|
|
||||||
.remove_device_button
|
|
||||||
.borrow()
|
|
||||||
.set_sensitive(true);
|
|
||||||
} else {
|
|
||||||
list_entry
|
|
||||||
.imp()
|
|
||||||
.remove_device_button
|
|
||||||
.borrow()
|
|
||||||
.set_sensitive(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*existing_bluetooth_device = ir.bluetooth_device;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
fn device_removed_handler(
|
|
||||||
device_removed_box: Arc<BluetoothBox>,
|
|
||||||
ir: BluetoothDeviceRemoved,
|
|
||||||
) -> bool {
|
|
||||||
let bluetooth_box = device_removed_box.clone();
|
|
||||||
glib::spawn_future(async move {
|
|
||||||
glib::idle_add_once(move || {
|
|
||||||
let imp = bluetooth_box.imp();
|
|
||||||
let mut map = imp.available_devices.borrow_mut();
|
|
||||||
if let Some(list_entry) = map.remove(&ir.bluetooth_device) {
|
|
||||||
if list_entry.imp().bluetooth_device.borrow().connected {
|
|
||||||
imp.reset_bluetooth_connected_devices.remove(&*list_entry);
|
|
||||||
} else {
|
|
||||||
imp.reset_bluetooth_available_devices.remove(&*list_entry);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
fn device_added_handler(device_added_box: Arc<BluetoothBox>, ir: BluetoothDeviceAdded) -> bool {
|
|
||||||
let bluetooth_box = device_added_box.clone();
|
|
||||||
glib::spawn_future(async move {
|
|
||||||
glib::idle_add_once(move || {
|
|
||||||
let imp = bluetooth_box.imp();
|
|
||||||
let path = ir.bluetooth_device.path.clone();
|
|
||||||
let connected = ir.bluetooth_device.connected;
|
|
||||||
let bluetooth_entry = BluetoothEntry::new(ir.bluetooth_device, bluetooth_box.clone());
|
|
||||||
imp.available_devices
|
|
||||||
.borrow_mut()
|
|
||||||
.insert(path, bluetooth_entry.clone());
|
|
||||||
if connected {
|
|
||||||
imp.reset_bluetooth_connected_devices.add(&*bluetooth_entry);
|
|
||||||
} else {
|
|
||||||
imp.reset_bluetooth_available_devices.add(&*bluetooth_entry);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
true
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_connected_devices(bluetooth_box: Arc<BluetoothBox>) -> Vec<BluetoothDevice> {
|
fn get_connected_devices(bluetooth_box: Arc<BluetoothBox>) -> Vec<BluetoothDevice> {
|
||||||
let conn = Connection::new_session().unwrap();
|
let conn = Connection::new_session().unwrap();
|
||||||
let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000));
|
let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000));
|
||||||
|
|
92
src/components/bluetooth/bluetooth_event_handlers.rs
Normal file
92
src/components/bluetooth/bluetooth_event_handlers.rs
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use adw::prelude::PreferencesGroupExt;
|
||||||
|
use glib::subclass::types::ObjectSubclassIsExt;
|
||||||
|
use gtk::prelude::WidgetExt;
|
||||||
|
use re_set_lib::signals::{BluetoothDeviceAdded, BluetoothDeviceChanged, BluetoothDeviceRemoved};
|
||||||
|
|
||||||
|
use super::{bluetooth_box::BluetoothBox, bluetooth_entry::BluetoothEntry};
|
||||||
|
|
||||||
|
pub fn device_changed_handler(
|
||||||
|
device_changed_box: Arc<BluetoothBox>,
|
||||||
|
ir: BluetoothDeviceChanged,
|
||||||
|
) -> bool {
|
||||||
|
let bluetooth_box = device_changed_box.clone();
|
||||||
|
glib::spawn_future(async move {
|
||||||
|
glib::idle_add_once(move || {
|
||||||
|
let imp = bluetooth_box.imp();
|
||||||
|
let mut map = imp.available_devices.borrow_mut();
|
||||||
|
if let Some(list_entry) = map.get_mut(&ir.bluetooth_device.path) {
|
||||||
|
let mut existing_bluetooth_device = list_entry.imp().bluetooth_device.borrow_mut();
|
||||||
|
if existing_bluetooth_device.connected != ir.bluetooth_device.connected {
|
||||||
|
if ir.bluetooth_device.connected {
|
||||||
|
imp.reset_bluetooth_available_devices.remove(&**list_entry);
|
||||||
|
imp.reset_bluetooth_connected_devices.add(&**list_entry);
|
||||||
|
} else {
|
||||||
|
imp.reset_bluetooth_connected_devices.remove(&**list_entry);
|
||||||
|
imp.reset_bluetooth_available_devices.add(&**list_entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if existing_bluetooth_device.bonded != ir.bluetooth_device.bonded {
|
||||||
|
if ir.bluetooth_device.bonded {
|
||||||
|
list_entry
|
||||||
|
.imp()
|
||||||
|
.remove_device_button
|
||||||
|
.borrow()
|
||||||
|
.set_sensitive(true);
|
||||||
|
} else {
|
||||||
|
list_entry
|
||||||
|
.imp()
|
||||||
|
.remove_device_button
|
||||||
|
.borrow()
|
||||||
|
.set_sensitive(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*existing_bluetooth_device = ir.bluetooth_device;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn device_removed_handler(
|
||||||
|
device_removed_box: Arc<BluetoothBox>,
|
||||||
|
ir: BluetoothDeviceRemoved,
|
||||||
|
) -> bool {
|
||||||
|
let bluetooth_box = device_removed_box.clone();
|
||||||
|
glib::spawn_future(async move {
|
||||||
|
glib::idle_add_once(move || {
|
||||||
|
let imp = bluetooth_box.imp();
|
||||||
|
let mut map = imp.available_devices.borrow_mut();
|
||||||
|
if let Some(list_entry) = map.remove(&ir.bluetooth_device) {
|
||||||
|
if list_entry.imp().bluetooth_device.borrow().connected {
|
||||||
|
imp.reset_bluetooth_connected_devices.remove(&*list_entry);
|
||||||
|
} else {
|
||||||
|
imp.reset_bluetooth_available_devices.remove(&*list_entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
true
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn device_added_handler(device_added_box: Arc<BluetoothBox>, ir: BluetoothDeviceAdded) -> bool {
|
||||||
|
let bluetooth_box = device_added_box.clone();
|
||||||
|
glib::spawn_future(async move {
|
||||||
|
glib::idle_add_once(move || {
|
||||||
|
let imp = bluetooth_box.imp();
|
||||||
|
let path = ir.bluetooth_device.path.clone();
|
||||||
|
let connected = ir.bluetooth_device.connected;
|
||||||
|
let bluetooth_entry = BluetoothEntry::new(ir.bluetooth_device, bluetooth_box.clone());
|
||||||
|
imp.available_devices
|
||||||
|
.borrow_mut()
|
||||||
|
.insert(path, bluetooth_entry.clone());
|
||||||
|
if connected {
|
||||||
|
imp.reset_bluetooth_connected_devices.add(&*bluetooth_entry);
|
||||||
|
} else {
|
||||||
|
imp.reset_bluetooth_available_devices.add(&*bluetooth_entry);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
true
|
||||||
|
}
|
|
@ -2,3 +2,4 @@ pub mod bluetooth_box;
|
||||||
pub mod bluetooth_box_impl;
|
pub mod bluetooth_box_impl;
|
||||||
pub mod bluetooth_entry;
|
pub mod bluetooth_entry;
|
||||||
pub mod bluetooth_entry_impl;
|
pub mod bluetooth_entry_impl;
|
||||||
|
mod bluetooth_event_handlers;
|
||||||
|
|
Loading…
Reference in a new issue