Skip to content

Commit

Permalink
Add list dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
dati91 committed May 26, 2016
1 parent 89f26f4 commit 6048f0f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
43 changes: 41 additions & 2 deletions components/net/bluetooth_thread.rs
Expand Up @@ -19,6 +19,8 @@ use std::collections::HashMap;
use std::string::String;
use std::thread;
use std::time::Duration;
#[cfg(target_os = "linux")]
use tinyfiledialogs;
use util::thread::spawn_named;

const ADAPTER_ERROR: &'static str = "No adapter found";
Expand All @@ -30,6 +32,12 @@ const DESCRIPTOR_ERROR: &'static str = "No descriptor found";
const VALUE_ERROR: &'static str = "No characteristic or descriptor found with that id";
// The discovery session needs some time to find any nearby devices
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! {
flags Flags: u32 {
Expand Down Expand Up @@ -208,6 +216,37 @@ impl BluetoothManager {
None
}

#[cfg(target_os = "linux")]
fn select_device(&mut self, devices: Vec<BluetoothDevice>) -> Option<String> {
let mut dialog_rows: Vec<String> = vec!();
for device in devices {
dialog_rows.extend_from_slice(&[device.get_address().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,
&[DIALOG_COLUMN_ID, DIALOG_COLUMN_NAME],
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"))]
fn select_device(&mut self, devices: Vec<BluetoothDevice>) -> Option<String> {
for device in devices {
if let Ok(address) = device.get_address() {
return Some(address);
}
}
None
}

// Service

fn get_and_cache_gatt_services(&mut self,
Expand Down Expand Up @@ -370,8 +409,8 @@ impl BluetoothManager {
let matched_devices: Vec<BluetoothDevice> = devices.into_iter()
.filter(|d| matches_filters(d, options.get_filters()))
.collect();
for device in matched_devices {
if let Ok(address) = device.get_address() {
if let Some(address) = self.select_device(matched_devices) {
if let Some(device) = self.get_device(&mut adapter, address.as_str()) {
let message = Ok(BluetoothDeviceMsg {
id: address,
name: device.get_name().ok(),
Expand Down
2 changes: 1 addition & 1 deletion components/servo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ports/cef/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ports/gonk/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6048f0f

Please sign in to comment.