mirror of
https://github.com/servo/servo.git
synced 2025-08-07 14:35:33 +01:00
Add list dialog
This commit is contained in:
parent
89f26f4653
commit
6048f0f2cc
4 changed files with 44 additions and 5 deletions
|
@ -19,6 +19,8 @@ use std::collections::HashMap;
|
||||||
use std::string::String;
|
use std::string::String;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
#[cfg(target_os = "linux")]
|
||||||
|
use tinyfiledialogs;
|
||||||
use util::thread::spawn_named;
|
use util::thread::spawn_named;
|
||||||
|
|
||||||
const ADAPTER_ERROR: &'static str = "No adapter found";
|
const ADAPTER_ERROR: &'static str = "No adapter found";
|
||||||
|
@ -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";
|
const VALUE_ERROR: &'static str = "No characteristic or descriptor found with that id";
|
||||||
// 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! {
|
||||||
flags Flags: u32 {
|
flags Flags: u32 {
|
||||||
|
@ -208,6 +216,37 @@ impl BluetoothManager {
|
||||||
None
|
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
|
// Service
|
||||||
|
|
||||||
fn get_and_cache_gatt_services(&mut self,
|
fn get_and_cache_gatt_services(&mut self,
|
||||||
|
@ -370,8 +409,8 @@ impl BluetoothManager {
|
||||||
let matched_devices: Vec<BluetoothDevice> = devices.into_iter()
|
let matched_devices: Vec<BluetoothDevice> = devices.into_iter()
|
||||||
.filter(|d| matches_filters(d, options.get_filters()))
|
.filter(|d| matches_filters(d, options.get_filters()))
|
||||||
.collect();
|
.collect();
|
||||||
for device in matched_devices {
|
if let Some(address) = self.select_device(matched_devices) {
|
||||||
if let Ok(address) = device.get_address() {
|
if let Some(device) = self.get_device(&mut adapter, address.as_str()) {
|
||||||
let message = Ok(BluetoothDeviceMsg {
|
let message = Ok(BluetoothDeviceMsg {
|
||||||
id: address,
|
id: address,
|
||||||
name: device.get_name().ok(),
|
name: device.get_name().ok(),
|
||||||
|
|
2
components/servo/Cargo.lock
generated
2
components/servo/Cargo.lock
generated
|
@ -2260,7 +2260,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tinyfiledialogs"
|
name = "tinyfiledialogs"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/jdm/tinyfiledialogs#686abf781f30b360a4c265964fd3744e2f61cf2d"
|
source = "git+https://github.com/jdm/tinyfiledialogs#3a30f8f95686195cb3bcecfc77ff77277a624a53"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
2
ports/cef/Cargo.lock
generated
2
ports/cef/Cargo.lock
generated
|
@ -2128,7 +2128,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tinyfiledialogs"
|
name = "tinyfiledialogs"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/jdm/tinyfiledialogs#686abf781f30b360a4c265964fd3744e2f61cf2d"
|
source = "git+https://github.com/jdm/tinyfiledialogs#3a30f8f95686195cb3bcecfc77ff77277a624a53"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
2
ports/gonk/Cargo.lock
generated
2
ports/gonk/Cargo.lock
generated
|
@ -2118,7 +2118,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tinyfiledialogs"
|
name = "tinyfiledialogs"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/jdm/tinyfiledialogs#686abf781f30b360a4c265964fd3744e2f61cf2d"
|
source = "git+https://github.com/jdm/tinyfiledialogs#3a30f8f95686195cb3bcecfc77ff77277a624a53"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue