Improvments to launcher.
Moved UI building to a function. Found a way to clear GtkListBox without a loop. Ran rustfmt.
This commit is contained in:
parent
f4d8d778fe
commit
91087608b2
3 changed files with 134 additions and 133 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,2 +1,3 @@
|
||||||
/target
|
/target
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
.idea
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use pop_launcher::{Request, Response};
|
use pop_launcher::{Request, Response};
|
||||||
use std::process;
|
|
||||||
use std::io::{self, BufRead, Write};
|
use std::io::{self, BufRead, Write};
|
||||||
|
use std::process;
|
||||||
|
|
||||||
pub struct LauncherIpc {
|
pub struct LauncherIpc {
|
||||||
child: process::Child,
|
child: process::Child,
|
||||||
|
|
@ -16,13 +16,15 @@ impl LauncherIpc {
|
||||||
.stdout(process::Stdio::piped())
|
.stdout(process::Stdio::piped())
|
||||||
.spawn()?;
|
.spawn()?;
|
||||||
|
|
||||||
let stdin = child.stdin.take().ok_or(
|
let stdin = child.stdin.take().ok_or(io::Error::new(
|
||||||
io::Error::new(io::ErrorKind::Other, "failed to find child stdin")
|
io::ErrorKind::Other,
|
||||||
)?;
|
"failed to find child stdin",
|
||||||
|
))?;
|
||||||
|
|
||||||
let stdout = io::BufReader::new(child.stdout.take().ok_or(
|
let stdout = io::BufReader::new(child.stdout.take().ok_or(io::Error::new(
|
||||||
io::Error::new(io::ErrorKind::Other, "failed to find child stdout")
|
io::ErrorKind::Other,
|
||||||
)?);
|
"failed to find child stdout",
|
||||||
|
))?);
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
child,
|
child,
|
||||||
|
|
@ -33,9 +35,8 @@ impl LauncherIpc {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send_request(&mut self, request: Request) -> io::Result<()> {
|
fn send_request(&mut self, request: Request) -> io::Result<()> {
|
||||||
let mut request_json = serde_json::to_string(&request).map_err(|err| {
|
let mut request_json = serde_json::to_string(&request)
|
||||||
io::Error::new(io::ErrorKind::InvalidInput, err)
|
.map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;
|
||||||
})?;
|
|
||||||
request_json.push('\n');
|
request_json.push('\n');
|
||||||
self.stdin.write_all(request_json.as_bytes())
|
self.stdin.write_all(request_json.as_bytes())
|
||||||
}
|
}
|
||||||
|
|
@ -43,9 +44,8 @@ impl LauncherIpc {
|
||||||
fn recv_response(&mut self) -> io::Result<Response> {
|
fn recv_response(&mut self) -> io::Result<Response> {
|
||||||
let mut response_json = String::new();
|
let mut response_json = String::new();
|
||||||
self.stdout.read_line(&mut response_json)?;
|
self.stdout.read_line(&mut response_json)?;
|
||||||
serde_json::from_str(&response_json).map_err(|err| {
|
serde_json::from_str(&response_json)
|
||||||
io::Error::new(io::ErrorKind::InvalidData, err)
|
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn request(&mut self, request: Request) -> io::Result<Response> {
|
pub fn request(&mut self, request: Request) -> io::Result<Response> {
|
||||||
|
|
@ -55,7 +55,7 @@ impl LauncherIpc {
|
||||||
|
|
||||||
//TODO: better exit implementation
|
//TODO: better exit implementation
|
||||||
pub fn exit(&mut self) -> io::Result<Option<process::ExitStatus>> {
|
pub fn exit(&mut self) -> io::Result<Option<process::ExitStatus>> {
|
||||||
if ! self.exited {
|
if !self.exited {
|
||||||
self.send_request(Request::Exit)?;
|
self.send_request(Request::Exit)?;
|
||||||
let status = self.child.wait()?;
|
let status = self.child.wait()?;
|
||||||
self.exited = true;
|
self.exited = true;
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
use gtk4 as gtk;
|
use gtk4 as gtk;
|
||||||
|
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
|
use gtk4::Application;
|
||||||
use libcosmic::x;
|
use libcosmic::x;
|
||||||
use std::{
|
use std::{cell::RefCell, rc::Rc};
|
||||||
cell::RefCell,
|
use gtk4::glib::Type;
|
||||||
rc::Rc,
|
|
||||||
};
|
|
||||||
|
|
||||||
use self::ipc::LauncherIpc;
|
use self::ipc::LauncherIpc;
|
||||||
mod ipc;
|
mod ipc;
|
||||||
|
|
@ -14,12 +13,10 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
@ -27,120 +24,123 @@ fn icon_source(icon: >k::Image, source: &Option<pop_launcher::IconSource>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let launcher = Rc::new(RefCell::new(
|
|
||||||
LauncherIpc::new().expect("failed to connect to launcher service")
|
|
||||||
));
|
|
||||||
|
|
||||||
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| build_ui(app));
|
||||||
let window = gtk::ApplicationWindow::builder()
|
|
||||||
.application(app)
|
|
||||||
.decorated(false)
|
|
||||||
.default_width(480)
|
|
||||||
.default_height(440)
|
|
||||||
.title("Launcher")
|
|
||||||
.build();
|
|
||||||
|
|
||||||
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 16);
|
|
||||||
vbox.set_margin_start(16);
|
|
||||||
vbox.set_margin_end(16);
|
|
||||||
vbox.set_margin_top(16);
|
|
||||||
vbox.set_margin_bottom(16);
|
|
||||||
window.set_child(Some(&vbox));
|
|
||||||
|
|
||||||
let search = gtk::SearchEntry::new();
|
|
||||||
search.set_placeholder_text(Some(" Type to search apps, or type '?' for more options."));
|
|
||||||
vbox.append(&search);
|
|
||||||
|
|
||||||
let listbox = gtk::ListBox::new();
|
|
||||||
vbox.append(&listbox);
|
|
||||||
|
|
||||||
{
|
|
||||||
let launcher = launcher.clone();
|
|
||||||
let search_changed = move |search: >k::SearchEntry| {
|
|
||||||
//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(
|
|
||||||
pop_launcher::Request::Search(search.text().to_string())
|
|
||||||
);
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setting the window to dialog type must happen between realize and show. Dialog windows
|
|
||||||
// show up centered on the display with the cursor, so we do not have to set position
|
|
||||||
window.connect_realize(move |window| {
|
|
||||||
if let Some((display, surface)) = x::get_window_x11(window) {
|
|
||||||
unsafe {
|
|
||||||
x::change_property(
|
|
||||||
&display,
|
|
||||||
&surface,
|
|
||||||
"_NET_WM_WINDOW_TYPE",
|
|
||||||
x::PropMode::Replace,
|
|
||||||
&[x::Atom::new(&display, "_NET_WM_WINDOW_TYPE_DIALOG").unwrap()],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
println!("failed to get X11 window");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
window.show();
|
|
||||||
});
|
|
||||||
|
|
||||||
app.run();
|
app.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn build_ui(app: &Application) {
|
||||||
|
let launcher = Rc::new(RefCell::new(
|
||||||
|
LauncherIpc::new().expect("failed to connect to launcher service"),
|
||||||
|
));
|
||||||
|
|
||||||
|
let window = gtk::ApplicationWindow::builder()
|
||||||
|
.application(app)
|
||||||
|
.decorated(false)
|
||||||
|
.default_width(590)
|
||||||
|
.default_height(150)
|
||||||
|
.title("Launcher")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let vbox = gtk::Box::new(gtk::Orientation::Vertical, 16);
|
||||||
|
vbox.set_margin_start(16);
|
||||||
|
vbox.set_margin_end(16);
|
||||||
|
vbox.set_margin_top(16);
|
||||||
|
vbox.set_margin_bottom(16);
|
||||||
|
window.set_child(Some(&vbox));
|
||||||
|
|
||||||
|
let search = gtk::SearchEntry::new();
|
||||||
|
search.set_placeholder_text(Some(" Type to search apps, or type '?' for more options."));
|
||||||
|
vbox.append(&search);
|
||||||
|
{
|
||||||
|
let launcher = launcher.clone();
|
||||||
|
let search_changed = move |search: >k::SearchEntry| {
|
||||||
|
let listbox_widget = vbox.last_child().unwrap();
|
||||||
|
let gtk_list_box = Type::from_name("GtkListBox");
|
||||||
|
if gtk_list_box.is_some() && listbox_widget.type_() == gtk_list_box.unwrap() {
|
||||||
|
vbox.remove(&listbox_widget);
|
||||||
|
}
|
||||||
|
let listbox = gtk::ListBox::new();
|
||||||
|
vbox.append(&listbox);
|
||||||
|
|
||||||
|
let response_res = launcher
|
||||||
|
.borrow_mut()
|
||||||
|
.request(pop_launcher::Request::Search(search.text().to_string()));
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setting the window to dialog type must happen between realize and show. Dialog windows
|
||||||
|
// show up centered on the display with the cursor, so we do not have to set position
|
||||||
|
window.connect_realize(move |window| {
|
||||||
|
if let Some((display, surface)) = x::get_window_x11(window) {
|
||||||
|
unsafe {
|
||||||
|
x::change_property(
|
||||||
|
&display,
|
||||||
|
&surface,
|
||||||
|
"_NET_WM_WINDOW_TYPE",
|
||||||
|
x::PropMode::Replace,
|
||||||
|
&[x::Atom::new(&display, "_NET_WM_WINDOW_TYPE_DIALOG").unwrap()],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("failed to get X11 window");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
window.show();
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue