Merge remote-tracking branch 'upstream/client' into launcher/searchentry
This commit is contained in:
commit
04fea498b0
4 changed files with 108 additions and 153 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
||||||
/target
|
/target
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
.idea
|
||||||
12
Cargo.toml
12
Cargo.toml
|
|
@ -10,9 +10,13 @@ gdk4-x11 = "0.3.0"
|
||||||
gio = "0.14.8"
|
gio = "0.14.8"
|
||||||
gtk4 = "0.3.1"
|
gtk4 = "0.3.1"
|
||||||
x11 = { version = "2", features = ["xlib"] }
|
x11 = { version = "2", features = ["xlib"] }
|
||||||
# examples/launcher
|
|
||||||
pop-launcher = "1.0.3"
|
[dev-dependencies]
|
||||||
serde_json = "1.0.70"
|
pop-launcher = { git = "https://github.com/pop-os/launcher", branch = "client" }
|
||||||
|
pop-launcher-service = { git = "https://github.com/pop-os/launcher", branch = "client" }
|
||||||
|
postage = "0.4.1"
|
||||||
|
futures = "0.3.18"
|
||||||
|
glib = "0.14.8"
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "launcher" # examples/launcher/main.rs
|
name = "launcher" # examples/launcher/main.rs
|
||||||
|
|
|
||||||
|
|
@ -1,76 +0,0 @@
|
||||||
use pop_launcher::{Request, Response};
|
|
||||||
use std::process;
|
|
||||||
use std::io::{self, BufRead, Write};
|
|
||||||
|
|
||||||
pub struct LauncherIpc {
|
|
||||||
child: process::Child,
|
|
||||||
stdin: process::ChildStdin,
|
|
||||||
stdout: io::BufReader<process::ChildStdout>,
|
|
||||||
exited: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LauncherIpc {
|
|
||||||
pub fn new() -> io::Result<Self> {
|
|
||||||
let mut child = process::Command::new("pop-launcher")
|
|
||||||
.stdin(process::Stdio::piped())
|
|
||||||
.stdout(process::Stdio::piped())
|
|
||||||
.spawn()?;
|
|
||||||
|
|
||||||
let stdin = child.stdin.take().ok_or(
|
|
||||||
io::Error::new(io::ErrorKind::Other, "failed to find child stdin")
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let stdout = io::BufReader::new(child.stdout.take().ok_or(
|
|
||||||
io::Error::new(io::ErrorKind::Other, "failed to find child stdout")
|
|
||||||
)?);
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
child,
|
|
||||||
stdin,
|
|
||||||
stdout,
|
|
||||||
exited: false,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn send_request(&mut self, request: Request) -> io::Result<()> {
|
|
||||||
let mut request_json = serde_json::to_string(&request).map_err(|err| {
|
|
||||||
io::Error::new(io::ErrorKind::InvalidInput, err)
|
|
||||||
})?;
|
|
||||||
request_json.push('\n');
|
|
||||||
self.stdin.write_all(request_json.as_bytes())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn recv_response(&mut self) -> io::Result<Response> {
|
|
||||||
let mut response_json = String::new();
|
|
||||||
self.stdout.read_line(&mut response_json)?;
|
|
||||||
serde_json::from_str(&response_json).map_err(|err| {
|
|
||||||
io::Error::new(io::ErrorKind::InvalidData, err)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn request(&mut self, request: Request) -> io::Result<Response> {
|
|
||||||
self.send_request(request)?;
|
|
||||||
self.recv_response()
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: better exit implementation
|
|
||||||
pub fn exit(&mut self) -> io::Result<Option<process::ExitStatus>> {
|
|
||||||
if ! self.exited {
|
|
||||||
self.send_request(Request::Exit)?;
|
|
||||||
let status = self.child.wait()?;
|
|
||||||
self.exited = true;
|
|
||||||
Ok(Some(status))
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Drop for LauncherIpc {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
match self.exit() {
|
|
||||||
Ok(_) => (),
|
|
||||||
Err(err) => eprintln!("LauncherIpc::drop failed: {}", err),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -2,40 +2,53 @@ use gtk4 as gtk;
|
||||||
|
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use libcosmic::x;
|
use libcosmic::x;
|
||||||
use std::{
|
use pop_launcher_service::IpcClient;
|
||||||
cell::RefCell,
|
use postage::mpsc::Sender;
|
||||||
rc::Rc,
|
use postage::prelude::*;
|
||||||
};
|
|
||||||
|
|
||||||
use self::ipc::LauncherIpc;
|
|
||||||
mod ipc;
|
|
||||||
|
|
||||||
fn icon_source(icon: >k::Image, source: &Option<pop_launcher::IconSource>) {
|
fn icon_source(icon: >k::Image, source: &Option<pop_launcher::IconSource>) {
|
||||||
match source {
|
match source {
|
||||||
Some(pop_launcher::IconSource::Name(name)) => {
|
Some(pop_launcher::IconSource::Name(name)) => {
|
||||||
icon.set_from_icon_name(Some(name));
|
icon.set_from_icon_name(Some(name));
|
||||||
},
|
}
|
||||||
Some(pop_launcher::IconSource::Mime(content_type)) => {
|
Some(pop_launcher::IconSource::Mime(content_type)) => {
|
||||||
icon.set_from_gicon(
|
icon.set_from_gicon(&gio::content_type_get_icon(content_type));
|
||||||
&gio::content_type_get_icon(content_type)
|
}
|
||||||
);
|
|
||||||
},
|
|
||||||
_ => {
|
_ => {
|
||||||
icon.set_from_icon_name(None);
|
icon.set_from_icon_name(None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
enum Event {
|
||||||
let launcher = Rc::new(RefCell::new(
|
Response(pop_launcher::Response),
|
||||||
LauncherIpc::new().expect("failed to connect to launcher service")
|
Search(String),
|
||||||
));
|
}
|
||||||
|
|
||||||
|
fn spawn_launcher(mut tx: Sender<Event>) -> IpcClient {
|
||||||
|
let (launcher, responses) =
|
||||||
|
pop_launcher_service::IpcClient::new().expect("failed to connect to launcher service");
|
||||||
|
|
||||||
|
glib::MainContext::default().spawn_local(async move {
|
||||||
|
use futures::StreamExt;
|
||||||
|
futures::pin_mut!(responses);
|
||||||
|
while let Some(event) = responses.next().await {
|
||||||
|
let _ = tx.send(Event::Response(event)).await;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
launcher
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
let app = gtk::Application::builder()
|
let app = gtk::Application::builder()
|
||||||
.application_id("com.system76.Launcher")
|
.application_id("com.system76.Launcher")
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
app.connect_activate(move |app| {
|
app.connect_activate(move |app| {
|
||||||
|
let (tx, mut rx) = postage::mpsc::channel(1);
|
||||||
|
let mut launcher = spawn_launcher(tx.clone());
|
||||||
|
|
||||||
let window = gtk::ApplicationWindow::builder()
|
let window = gtk::ApplicationWindow::builder()
|
||||||
.application(app)
|
.application(app)
|
||||||
.decorated(false)
|
.decorated(false)
|
||||||
|
|
@ -59,66 +72,17 @@ fn main() {
|
||||||
vbox.append(&listbox);
|
vbox.append(&listbox);
|
||||||
|
|
||||||
{
|
{
|
||||||
let launcher = launcher.clone();
|
let search_changed = glib::clone!(@strong tx => move |search: >k::SearchEntry| {
|
||||||
let search_changed = move |search: >k::SearchEntry| {
|
let search = search.text().to_string();
|
||||||
//TODO: is this the best way to clear a listbox?
|
|
||||||
while let Some(child) = listbox.last_child() {
|
|
||||||
listbox.remove(&child);
|
|
||||||
}
|
|
||||||
|
|
||||||
let response_res = launcher.borrow_mut().request(
|
let mut tx = tx.clone();
|
||||||
pop_launcher::Request::Search(search.text().to_string())
|
glib::MainContext::default().spawn_local(async move {
|
||||||
);
|
let _ = tx.send(Event::Search(search)).await;
|
||||||
|
});
|
||||||
println!("{:#?}", response_res);
|
});
|
||||||
|
|
||||||
if let Ok(pop_launcher::Response::Update(results)) = response_res {
|
|
||||||
for (i, result) in results.iter().enumerate() {
|
|
||||||
// Limit to 9 results
|
|
||||||
if i >= 9 { continue; }
|
|
||||||
|
|
||||||
let row = gtk::ListBoxRow::new();
|
|
||||||
listbox.append(&row);
|
|
||||||
|
|
||||||
// Select first row
|
|
||||||
if i == 0 {
|
|
||||||
listbox.select_row(Some(&row));
|
|
||||||
}
|
|
||||||
|
|
||||||
let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 8);
|
|
||||||
hbox.set_margin_start(8);
|
|
||||||
hbox.set_margin_end(8);
|
|
||||||
hbox.set_margin_top(8);
|
|
||||||
hbox.set_margin_bottom(8);
|
|
||||||
row.set_child(Some(&hbox));
|
|
||||||
|
|
||||||
let category_icon = gtk::Image::new();
|
|
||||||
category_icon.set_pixel_size(16);
|
|
||||||
icon_source(&category_icon, &result.category_icon);
|
|
||||||
hbox.append(&category_icon);
|
|
||||||
|
|
||||||
let icon = gtk::Image::new();
|
|
||||||
icon.set_pixel_size(32);
|
|
||||||
icon_source(&icon, &result.icon);
|
|
||||||
hbox.append(&icon);
|
|
||||||
|
|
||||||
let labels = gtk::Box::new(gtk::Orientation::Vertical, 4);
|
|
||||||
hbox.append(&labels);
|
|
||||||
|
|
||||||
let name = gtk::Label::new(Some(&result.name));
|
|
||||||
name.set_halign(gtk::Align::Start);
|
|
||||||
labels.append(&name);
|
|
||||||
|
|
||||||
let description = gtk::Label::new(Some(&result.description));
|
|
||||||
description.set_halign(gtk::Align::Start);
|
|
||||||
labels.append(&description);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
search.connect_changed(search_changed.clone());
|
|
||||||
|
|
||||||
search_changed(&search);
|
search_changed(&search);
|
||||||
|
search.connect_changed(search_changed);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setting the window to dialog type must happen between realize and show. Dialog windows
|
// Setting the window to dialog type must happen between realize and show. Dialog windows
|
||||||
|
|
@ -140,6 +104,68 @@ fn main() {
|
||||||
});
|
});
|
||||||
|
|
||||||
window.show();
|
window.show();
|
||||||
|
|
||||||
|
glib::MainContext::default().spawn_local(async move {
|
||||||
|
while let Some(event) = rx.recv().await {
|
||||||
|
match event {
|
||||||
|
Event::Search(search) => {
|
||||||
|
//TODO: is this the best way to clear a listbox?
|
||||||
|
while let Some(child) = listbox.last_child() {
|
||||||
|
listbox.remove(&child);
|
||||||
|
}
|
||||||
|
|
||||||
|
let _ = launcher.send(pop_launcher::Request::Search(search)).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
Event::Response(event) => {
|
||||||
|
if let pop_launcher::Response::Update(results) = event {
|
||||||
|
for (i, result) in results.iter().enumerate() {
|
||||||
|
// Limit to 9 results
|
||||||
|
if i >= 9 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let row = gtk::ListBoxRow::new();
|
||||||
|
listbox.append(&row);
|
||||||
|
|
||||||
|
// Select first row
|
||||||
|
if i == 0 {
|
||||||
|
listbox.select_row(Some(&row));
|
||||||
|
}
|
||||||
|
|
||||||
|
let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 8);
|
||||||
|
hbox.set_margin_start(8);
|
||||||
|
hbox.set_margin_end(8);
|
||||||
|
hbox.set_margin_top(8);
|
||||||
|
hbox.set_margin_bottom(8);
|
||||||
|
row.set_child(Some(&hbox));
|
||||||
|
|
||||||
|
let category_icon = gtk::Image::new();
|
||||||
|
category_icon.set_pixel_size(16);
|
||||||
|
icon_source(&category_icon, &result.category_icon);
|
||||||
|
hbox.append(&category_icon);
|
||||||
|
|
||||||
|
let icon = gtk::Image::new();
|
||||||
|
icon.set_pixel_size(32);
|
||||||
|
icon_source(&icon, &result.icon);
|
||||||
|
hbox.append(&icon);
|
||||||
|
|
||||||
|
let labels = gtk::Box::new(gtk::Orientation::Vertical, 4);
|
||||||
|
hbox.append(&labels);
|
||||||
|
|
||||||
|
let name = gtk::Label::new(Some(&result.name));
|
||||||
|
name.set_halign(gtk::Align::Start);
|
||||||
|
labels.append(&name);
|
||||||
|
|
||||||
|
let description = gtk::Label::new(Some(&result.description));
|
||||||
|
description.set_halign(gtk::Align::Start);
|
||||||
|
labels.append(&description);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.run();
|
app.run();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue