refactor(bluetooth): send message to open select dialog

This commit is contained in:
OJ Kwon 2018-04-06 12:35:12 -07:00
parent 410cf63a8e
commit 6fa74133dd
No known key found for this signature in database
GPG key ID: FFCFEF3460FD1901
3 changed files with 4 additions and 35 deletions

1
Cargo.lock generated
View file

@ -184,7 +184,6 @@ dependencies = [
"script_traits 0.0.1", "script_traits 0.0.1",
"servo_config 0.0.1", "servo_config 0.0.1",
"servo_rand 0.0.1", "servo_rand 0.0.1",
"tinyfiledialogs 3.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"uuid 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
] ]

View file

@ -18,6 +18,3 @@ script_traits = {path = "../script_traits"}
servo_config = {path = "../config"} servo_config = {path = "../config"}
servo_rand = {path = "../rand"} servo_rand = {path = "../rand"}
uuid = {version = "0.6", features = ["v4"]} uuid = {version = "0.6", features = ["v4"]}
[target.'cfg(target_os = "linux")'.dependencies]
tinyfiledialogs = "3.0"

View file

@ -10,8 +10,6 @@ extern crate ipc_channel;
extern crate script_traits; extern crate script_traits;
extern crate servo_config; extern crate servo_config;
extern crate servo_rand; extern crate servo_rand;
#[cfg(target_os = "linux")]
extern crate tinyfiledialogs;
extern crate uuid; extern crate uuid;
pub mod test; pub mod test;
@ -25,7 +23,6 @@ use device::bluetooth::{BluetoothAdapter, BluetoothDevice, BluetoothGATTCharacte
use device::bluetooth::{BluetoothGATTDescriptor, BluetoothGATTService}; use device::bluetooth::{BluetoothGATTDescriptor, BluetoothGATTService};
use ipc_channel::ipc::{self, IpcReceiver, IpcSender}; use ipc_channel::ipc::{self, IpcReceiver, IpcSender};
use script_traits::BluetoothManagerMsg; use script_traits::BluetoothManagerMsg;
#[cfg(target_os = "linux")]
use servo_config::opts; use servo_config::opts;
use servo_config::prefs::PREFS; use servo_config::prefs::PREFS;
use servo_rand::Rng; use servo_rand::Rng;
@ -41,12 +38,6 @@ const MAXIMUM_TRANSACTION_TIME: u8 = 30;
const CONNECTION_TIMEOUT_MS: u64 = 1000; const CONNECTION_TIMEOUT_MS: u64 = 1000;
// The discovery session needs some time to find any nearby devices // The discovery session needs some time to find any nearby devices
const DISCOVERY_TIMEOUT_MS: u64 = 1500; const DISCOVERY_TIMEOUT_MS: u64 = 1500;
#[cfg(target_os = "linux")]
const DIALOG_TITLE: &'static str = "Choose a device";
#[cfg(target_os = "linux")]
const DIALOG_COLUMN_ID: &'static str = "Id";
#[cfg(target_os = "linux")]
const DIALOG_COLUMN_NAME: &'static str = "Name";
bitflags! { bitflags! {
struct Flags: u32 { struct Flags: u32 {
@ -368,10 +359,9 @@ impl BluetoothManager {
None None
} }
#[cfg(target_os = "linux")]
fn select_device(&mut self, devices: Vec<BluetoothDevice>, adapter: &BluetoothAdapter) -> Option<String> { fn select_device(&mut self, devices: Vec<BluetoothDevice>, adapter: &BluetoothAdapter) -> Option<String> {
if is_mock_adapter(adapter) || opts::get().headless { if is_mock_adapter(adapter) || opts::get().headless {
for device in devices { for device in &devices {
if let Ok(address) = device.get_address() { if let Ok(address) = device.get_address() {
return Some(address); return Some(address);
} }
@ -384,28 +374,11 @@ impl BluetoothManager {
dialog_rows.extend_from_slice(&[device.get_address().unwrap_or("".to_string()), dialog_rows.extend_from_slice(&[device.get_address().unwrap_or("".to_string()),
device.get_name().unwrap_or("".to_string())]); device.get_name().unwrap_or("".to_string())]);
} }
let dialog_rows: Vec<&str> = dialog_rows.iter()
.map(|s| s.as_ref())
.collect();
let dialog_rows: &[&str] = dialog_rows.as_slice();
if let Some(device) = tinyfiledialogs::list_dialog(DIALOG_TITLE, let (ipc_sender, ipc_receiver) = ipc::channel().expect("Failed to create IPC channel!");
&[DIALOG_COLUMN_ID, DIALOG_COLUMN_NAME], let msg = BluetoothManagerMsg::OpenDeviceSelectDialog(dialog_rows, ipc_sender);
Some(dialog_rows)) {
// The device string format will be "Address|Name". We need the first part of it.
return device.split("|").next().map(|s| s.to_string());
}
None
}
#[cfg(not(target_os = "linux"))] self.constellation_chan.send(msg).map(|_| ipc_receiver.recv().unwrap()).unwrap_or_default()
fn select_device(&mut self, devices: Vec<BluetoothDevice>, _adapter: &BluetoothAdapter) -> Option<String> {
for device in devices {
if let Ok(address) = device.get_address() {
return Some(address);
}
}
None
} }
fn generate_device_id(&mut self) -> String { fn generate_device_id(&mut self) -> String {