Skip to content

Commit

Permalink
feat(browser): implement device select dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Apr 18, 2018
1 parent a352dcc commit c2161da
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
3 changes: 3 additions & 0 deletions components/compositing/compositor_thread.rs
Expand Up @@ -141,6 +141,8 @@ pub enum EmbedderMsg {
LoadComplete(TopLevelBrowsingContextId),
/// A pipeline panicked. First string is the reason, second one is the backtrace.
Panic(TopLevelBrowsingContextId, String, Option<String>),
/// Open dialog to select bluetooth device.
GetSelectedBluetoothDevice(Vec<String>, IpcSender<Option<String>>),
/// Servo has shut down
Shutdown,
}
Expand Down Expand Up @@ -241,6 +243,7 @@ impl Debug for EmbedderMsg {
EmbedderMsg::LoadStart(..) => write!(f, "LoadStart"),
EmbedderMsg::LoadComplete(..) => write!(f, "LoadComplete"),
EmbedderMsg::Panic(..) => write!(f, "Panic"),
EmbedderMsg::GetSelectedBluetoothDevice(..) => write!(f, "GetSelectedBluetoothDevice"),
EmbedderMsg::Shutdown => write!(f, "Shutdown"),
}
}
Expand Down
39 changes: 39 additions & 0 deletions ports/servo/browser.rs
Expand Up @@ -7,6 +7,7 @@ use glutin_app::keyutils::{CMD_OR_CONTROL, CMD_OR_ALT};
use glutin_app::window::{Window, LINE_HEIGHT};
use servo::compositing::compositor_thread::EmbedderMsg;
use servo::compositing::windowing::{WebRenderDebugOption, WindowEvent};
use servo::ipc_channel::ipc::IpcSender;
use servo::msg::constellation_msg::{Key, TopLevelBrowsingContextId as BrowserId};
use servo::msg::constellation_msg::{KeyModifiers, KeyState, TraversalDirection};
use servo::net_traits::pub_domains::is_reg_domain;
Expand All @@ -16,6 +17,8 @@ use servo::servo_url::ServoUrl;
use servo::webrender_api::ScrollLocation;
use std::mem;
use std::rc::Rc;
#[cfg(target_os = "linux")]
use std::thread;
use tinyfiledialogs;

pub struct Browser {
Expand Down Expand Up @@ -288,13 +291,49 @@ impl Browser {
self.shutdown_requested = true;
},
EmbedderMsg::Panic(_browser_id, _reason, _backtrace) => {
},
EmbedderMsg::GetSelectedBluetoothDevice(devices, sender) => {
platform_get_selected_devices(devices, sender);
}
}
}
}

}

#[cfg(target_os = "linux")]
fn platform_get_selected_devices(devices: Vec<String>, sender: IpcSender<Option<String>>) {
let picker_name = "Choose a device";

thread::Builder::new().name(picker_name.to_owned()).spawn(move || {
let dialog_rows: Vec<&str> = devices.iter()
.map(|s| s.as_ref())
.collect();
let dialog_rows: Option<&[&str]> = Some(dialog_rows.as_slice());

match tinyfiledialogs::list_dialog("Choose a device", &["Id", "Name"], dialog_rows) {
Some(device) => {
// The device string format will be "Address|Name". We need the first part of it.
let address = device.split("|").next().map(|s| s.to_string());
let _ = sender.send(address);
},
None => {
let _ = sender.send(None);
}
}
}).expect("Thread spawning failed");
}

#[cfg(not(target_os = "linux"))]
fn platform_get_selected_devices(devices: Vec<String>, sender: IpcSender<Option<String>>) {
for device in devices {
if let Some(address) = device.split("|").next().map(|s| s.to_string()) {
let _ = sender.send(Some(address));
}
}
let _ = sender.send(None);
}

fn sanitize_url(request: &str) -> Option<ServoUrl> {
let request = request.trim();
ServoUrl::parse(&request).ok()
Expand Down

0 comments on commit c2161da

Please sign in to comment.