styling tweaks & add separator

This commit is contained in:
Ashley Wulber 2021-12-15 11:37:28 -05:00
parent 7f65293179
commit b59d8a434c
9 changed files with 199 additions and 212 deletions

View file

@ -55,9 +55,6 @@ impl Window {
let app_model = gio::ListStore::new(gio::DesktopAppInfo::static_type()); let app_model = gio::ListStore::new(gio::DesktopAppInfo::static_type());
// Get state and set model // Get state and set model
let imp = imp::Window::from_instance(self); let imp = imp::Window::from_instance(self);
imp.app_model
.set(app_model.clone())
.expect("Could not set model");
// A sorter used to sort AppInfo in the model by their name // A sorter used to sort AppInfo in the model by their name
xdg::BaseDirectories::new() xdg::BaseDirectories::new()
@ -87,6 +84,9 @@ impl Window {
}) })
} }
}); });
imp.app_model
.set(app_model.clone())
.expect("Could not set model");
let sorter = gtk::CustomSorter::new(move |obj1, obj2| { let sorter = gtk::CustomSorter::new(move |obj1, obj2| {
let app_info1 = obj1.downcast_ref::<gio::DesktopAppInfo>().unwrap(); let app_info1 = obj1.downcast_ref::<gio::DesktopAppInfo>().unwrap();

View file

@ -1,59 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<interface> <interface>
<template class="ApplicationRow" parent="GtkBox"> <template class="DockItem" parent="GtkBox">
<property name="orientation">vertical</property> <property name="orientation">vertical</property>
<property name="spacing">4</property> <property name="spacing">4</property>
<property name="margin-start">4</property> <child>
<property name="margin-end">4</property>
<child>
<object class="GtkImage" id="categoryimage">
<property name="pixel-size">64</property>
</object>
</child>
<child>
<object class="GtkImage" id="image"> <object class="GtkImage" id="image">
<property name="margin-top">4</property> <property name="margin-start">4</property>
<property name="margin-bottom">4</property>
<property name="pixel-size">36</property>
</object>
</child>
<child>
<object class="GtkBox">
<property name="orientation">vertical</property>
<property name="halign">fill</property>
<property name="hexpand">true</property>
<property name="margin-top">4</property>
<property name="margin-end">4</property> <property name="margin-end">4</property>
<property name="margin-top">4</property>
<property name="margin-bottom">4</property> <property name="margin-bottom">4</property>
<child> <property name="pixel-size">48</property>
<object class="GtkLabel" id="name">
<property name="halign">start</property>
<property name="ellipsize">end</property>
<property name="max-width-chars">40</property>
<style>
<class name="title-4" />
</style>
</object>
</child>
<child>
<object class="GtkLabel" id="description">
<property name="halign">start</property>
<property name="ellipsize">end</property>
<property name="max-width-chars">50</property>
<style>
<class name="body" />
</style>
</object>
</child>
</object>
</child>
<child type="end">
<object class="GtkLabel" id="shortcut">
<property name="halign">end</property>
<property name="wrap">false</property>
<style>
<class name="body" />
</style>
</object> </object>
</child> </child>
</template> </template>

View file

@ -6,24 +6,16 @@ use gtk4 as gtk;
use gtk::CompositeTemplate; use gtk::CompositeTemplate;
#[derive(Debug, Default, CompositeTemplate)] #[derive(Debug, Default, CompositeTemplate)]
#[template(file = "application_row.ui")] #[template(file = "dock_item.ui")]
pub struct ApplicationRow { pub struct DockItem {
#[template_child]
pub name: TemplateChild<gtk::Label>,
#[template_child]
pub description: TemplateChild<gtk::Label>,
#[template_child]
pub shortcut: TemplateChild<gtk::Label>,
#[template_child] #[template_child]
pub image: TemplateChild<gtk::Image>, pub image: TemplateChild<gtk::Image>,
#[template_child]
pub categoryimage: TemplateChild<gtk::Image>,
} }
#[glib::object_subclass] #[glib::object_subclass]
impl ObjectSubclass for ApplicationRow { impl ObjectSubclass for DockItem {
const NAME: &'static str = "ApplicationRow"; const NAME: &'static str = "DockItem";
type Type = super::ApplicationRow; type Type = super::DockItem;
type ParentType = gtk::Box; type ParentType = gtk::Box;
fn class_init(klass: &mut Self::Class) { fn class_init(klass: &mut Self::Class) {
@ -35,6 +27,6 @@ impl ObjectSubclass for ApplicationRow {
} }
} }
impl ObjectImpl for ApplicationRow {} impl ObjectImpl for DockItem {}
impl WidgetImpl for ApplicationRow {} impl WidgetImpl for DockItem {}
impl BoxImpl for ApplicationRow {} impl BoxImpl for DockItem {}

View file

@ -1,7 +1,12 @@
use crate::icon_source; use crate::icon_source;
use gdk4::ContentProvider;
use gdk4::Display;
use gio::File;
use glib::FromVariant; use glib::FromVariant;
use glib::Variant; use glib::Variant;
use gtk4 as gtk; use gtk4 as gtk;
use gtk4::DragSource;
use gtk4::IconTheme;
mod imp; mod imp;
use crate::ApplicationObject; use crate::ApplicationObject;
@ -10,70 +15,81 @@ use gtk::prelude::*;
use gtk::subclass::prelude::*; use gtk::subclass::prelude::*;
glib::wrapper! { glib::wrapper! {
pub struct ApplicationRow(ObjectSubclass<imp::ApplicationRow>) pub struct DockItem(ObjectSubclass<imp::DockItem>)
@extends gtk::Widget, gtk::Box; @extends gtk::Widget, gtk::Box;
} }
impl Default for ApplicationRow { impl Default for DockItem {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()
} }
} }
impl ApplicationRow { impl DockItem {
pub fn new() -> Self { pub fn new() -> Self {
glib::Object::new(&[]).expect("Failed to create ApplicationRow") glib::Object::new(&[]).expect("Failed to create DockItem")
} }
pub fn set_app_info(&self, app_obj: ApplicationObject) { pub fn set_app_info(&self, app_info: &gio::DesktopAppInfo) {
let self_ = imp::ApplicationRow::from_instance(self); dbg!("setting app info");
let self_ = imp::DockItem::from_instance(self);
self_.image.set_tooltip_text(Some(&app_info.name()));
if let Ok(name) = app_obj.property("name") { let drag = DragSource::builder()
self_.name.set_text( .name("application library drag source")
&name .actions(gdk4::DragAction::COPY)
.get::<String>() // .content()
.expect("Property name needs to be a String."), .build();
); self.add_controller(&drag);
if let Some(file) = app_info.filename() {
let file = File::for_path(file);
let provider = ContentProvider::for_value(&file.to_value());
drag.set_content(Some(&provider));
} }
if let Ok(desc) = app_obj.property("description") { if let Some(icon) = app_info.icon() {
self_.description.set_text( dbg!("setting icon {}", app_info.name());
&desc self_.image.set_from_gicon(&icon);
.get::<String>() // set drag source icon if possible...
.expect("Property description needs to be a String."), // gio Icon is not easily converted to a Paintable, but this seems to be the correct method
); if let Some(default_display) = &Display::default() {
} if let Some(icon_theme) = IconTheme::for_display(default_display) {
if let Ok(icon) = app_obj.property("icon") { if let Some(paintable_icon) = icon_theme.lookup_by_gicon(
if let Ok(icon) = icon.get::<Variant>() { &icon,
let icon = match <(i32, String)>::from_variant(&icon) { 64,
Some((i_type, name)) if i_type == pop_launcher::IconSource::Name as i32 => { 1,
Some(pop_launcher::IconSource::Name(name.into())) gtk4::TextDirection::None,
gtk4::IconLookupFlags::empty(),
) {
drag.set_icon(Some(&paintable_icon), 32, 32);
} }
Some((i_type, name)) if i_type == pop_launcher::IconSource::Mime as i32 => { }
Some(pop_launcher::IconSource::Mime(name.into()))
}
_ => None,
};
icon_source(&self_.image, &icon);
}
}
if let Ok(icon) = app_obj.property("categoryicon") {
if let Ok(icon) = icon.get::<Variant>() {
let icon = match <(i32, String)>::from_variant(&icon) {
Some((i_type, name)) if i_type == pop_launcher::IconSource::Name as i32 => {
Some(pop_launcher::IconSource::Name(name.into()))
}
Some((i_type, name)) if i_type == pop_launcher::IconSource::Mime as i32 => {
Some(pop_launcher::IconSource::Mime(name.into()))
}
_ => None,
};
icon_source(&self_.categoryimage, &icon);
} }
} }
} }
pub fn set_shortcut(&self, indx: u32) { // pub fn set_app_info(&self, app_obj: ApplicationObject) {
let self_ = imp::ApplicationRow::from_instance(self); // let self_ = imp::DockItem::from_instance(self);
self_.shortcut.set_text(&format!("Ctrl + {}", indx));
} // if let Ok(name) = app_obj.property("name") {
// self_.image.set_tooltip_text(Some(
// &name
// .get::<String>()
// .expect("Property name needs to be a String."),
// ));
// }
// if let Ok(icon) = app_obj.property("icon") {
// if let Ok(icon) = icon.get::<Variant>() {
// let icon = match <(i32, String)>::from_variant(&icon) {
// Some((i_type, name)) if i_type == pop_launcher::IconSource::Name as i32 => {
// Some(pop_launcher::IconSource::Name(name.into()))
// }
// Some((i_type, name)) if i_type == pop_launcher::IconSource::Mime as i32 => {
// Some(pop_launcher::IconSource::Mime(name.into()))
// }
// _ => None,
// };
// icon_source(&self_.image, &icon);
// }
// }
// }
} }

View file

@ -1,4 +1,5 @@
mod application_object; mod application_object;
mod dock_item;
mod window; mod window;
use gdk4::Display; use gdk4::Display;

View file

@ -1,13 +1,21 @@
row:hover { row:hover {
transition: 100ms; transition: 100ms;
background: #888888; background: #888888;
border-radius: 10px;
} }
row { row {
background: #333333; background: #333333;
border-radius: 10px;
} }
listview { listview {
border-radius: 10px;
background: #333333; background: #333333;
} }
window { window {
background: rgba(50,50,50,0.0); background: rgba(50,50,50,0.0);
} }
box#dock-container {
padding-right: 10px;
border-radius: 10px;
background: #333333;
}

View file

@ -1,5 +1,4 @@
use gtk4 as gtk; use gtk4 as gtk;
use gtk4::EventController;
use gtk4::EventControllerMotion; use gtk4::EventControllerMotion;
use gtk4::Revealer; use gtk4::Revealer;
@ -7,15 +6,15 @@ use glib::subclass::InitializingObject;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::subclass::prelude::*; use gtk::subclass::prelude::*;
use gtk::{gio, glib}; use gtk::{gio, glib};
use gtk::{CompositeTemplate, Entry, ListView}; use gtk::{CompositeTemplate, ListView};
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
// Object holding the state // Object holding the state
#[derive(CompositeTemplate, Default)] #[derive(CompositeTemplate, Default)]
#[template(file = "window.ui")] #[template(file = "window.ui")]
pub struct Window { pub struct Window {
// #[template_child] #[template_child]
// pub list_view: TemplateChild<ListView>, pub list_view: TemplateChild<ListView>,
#[template_child] #[template_child]
pub revealer: TemplateChild<Revealer>, pub revealer: TemplateChild<Revealer>,
pub model: OnceCell<gio::ListStore>, pub model: OnceCell<gio::ListStore>,

View file

@ -1,14 +1,13 @@
mod imp; mod imp;
// use crate::ApplicationObject; // use crate::ApplicationObject;
use crate::TX; use crate::dock_item::DockItem;
use crate::X11_CONN; use crate::X11_CONN;
use gdk4::Rectangle; use gdk4::Rectangle;
use gdk4::Surface; use gdk4::Surface;
use gdk4_x11::X11Surface; use gdk4_x11::X11Surface;
use gio::DesktopAppInfo;
use gtk4 as gtk; use gtk4 as gtk;
use gtk4::Allocation;
use gtk4::EventControllerMotion; use gtk4::EventControllerMotion;
use postage::prelude::Sink;
use x11rb::connection::Connection; use x11rb::connection::Connection;
use x11rb::protocol::xproto::ConnectionExt; use x11rb::protocol::xproto::ConnectionExt;
@ -28,8 +27,6 @@ glib::wrapper! {
gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager; gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
} }
const NUM_LAUNCHER_ITEMS: u8 = 9;
impl Window { impl Window {
pub fn new(app: &Application) -> Self { pub fn new(app: &Application) -> Self {
let self_: Self = Object::new(&[("application", app)]).expect("Failed to create `Window`."); let self_: Self = Object::new(&[("application", app)]).expect("Failed to create `Window`.");
@ -44,60 +41,84 @@ impl Window {
fn setup_model(&self) { fn setup_model(&self) {
// Get state and set model // Get state and set model
let imp = imp::Window::from_instance(self); let imp = imp::Window::from_instance(self);
// let model = gio::ListStore::new(ApplicationObject::static_type()); let model = gio::ListStore::new(DesktopAppInfo::static_type());
// let selection_model = gtk::SingleSelection::builder() let selection_model = gtk::SingleSelection::builder()
// .model(&slice_model) .autoselect(false)
// .autoselect(false) .can_unselect(true)
// .can_unselect(true) .selected(gtk4::INVALID_LIST_POSITION)
// .selected(gtk4::INVALID_LIST_POSITION) .model(&model)
// .build(); .build();
xdg::BaseDirectories::new()
.expect("could not access XDG Base directory")
.get_data_dirs()
.iter_mut()
.for_each(|xdg_data_path| {
let defaults = ["Firefox Web Browser", "Files", "Terminal", "Pop!_Shop"];
xdg_data_path.push("applications");
dbg!(&xdg_data_path);
if let Ok(dir_iter) = std::fs::read_dir(xdg_data_path) {
dir_iter.for_each(|dir_entry| {
if let Ok(dir_entry) = dir_entry {
if let Some(path) = dir_entry.path().file_name() {
if let Some(path) = path.to_str() {
if let Some(app_info) = gio::DesktopAppInfo::new(path) {
if app_info.should_show()
&& defaults.contains(&app_info.name().as_str())
{
dbg!(app_info.name());
model.append(&app_info)
} else {
// println!("Ignoring {}", path);
}
} else {
// println!("error loading {}", path);
}
}
}
}
})
}
});
// imp.model.set(model).expect("Could not set model"); imp.model.set(model).expect("Could not set model");
// // Wrap model with selection and pass it to the list view // Wrap model with selection and pass it to the list view
// imp.list_view.set_model(Some(&selection_model)); imp.list_view.set_model(Some(&selection_model));
} }
fn setup_callbacks(&self) { fn setup_callbacks(&self) {
// Get state // Get state
let imp = imp::Window::from_instance(self); let imp = imp::Window::from_instance(self);
let window = self.clone().upcast::<gtk::Window>(); let window = self.clone().upcast::<gtk::Window>();
// let list_view = &imp.list_view; let list_view = &imp.list_view;
// let lv = list_view.get();
// let revealer = Revealer::builder() let app_selection_model = list_view
// .child(&window) .model()
// .reveal_child(false) .expect("List view missing selection model")
// .transition_duration(200) .downcast::<gtk::SingleSelection>()
// .transition_type(gtk4::RevealerTransitionType::SlideUp) .expect("could not downcast listview model to single selection model");
// .build();
// let app_selection_model = list_view app_selection_model.connect_selected_notify(glib::clone!(@weak window => move |model| {
// .model() let position = model.selected();
// .expect("List view missing selection model") println!("selected app {}", position);
// .downcast::<gtk::SingleSelection>() // Launch the application when an item of the list is activated
// .expect("could not downcast listview model to single selection model"); if let Some(item) = model.item(position) {
let app_info = item.downcast::<gio::DesktopAppInfo>().unwrap();
// app_selection_model.connect_selected_notify(glib::clone!(@weak window => move |model| { let context = window.display().app_launch_context();
// let i = model.selected(); if let Err(err) = app_info.launch(&[], Some(&context)) {
// println!("acitvating... {}", i + 1); gtk::MessageDialog::builder()
// let app_info = model.item(i); .text(&format!("Failed to start {}", app_info.name()))
// if app_info.is_none() { .secondary_text(&err.to_string())
// println!("oops no app for this row..."); .message_type(gtk::MessageType::Error)
// return; .modal(true)
// } .transient_for(&window)
// if let Ok(id) = app_info.unwrap().property("id") { .build()
// let id = id.get::<u32>().expect("App ID must be u32"); .show();
}
// glib::MainContext::default().spawn_local(async move { }
// if let Some(tx) = TX.get() { }));
// let mut tx = tx.clone();
// let _ = tx.send(crate::Event::Activate(id)).await;
// }
// });
// }
// }));
let event_controller = &imp.event_controller.get().unwrap(); let event_controller = &imp.event_controller.get().unwrap();
let revealer = &imp.revealer.get(); let revealer = &imp.revealer.get();
@ -161,9 +182,6 @@ impl Window {
s.connect_height_notify(surface_resize_handler.clone()); s.connect_height_notify(surface_resize_handler.clone());
s.connect_width_notify(surface_resize_handler.clone()); s.connect_width_notify(surface_resize_handler.clone());
s.connect_scale_factor_notify(surface_resize_handler); s.connect_scale_factor_notify(surface_resize_handler);
// s.connect_enter_monitor(glib::clone!(@weak rv => move |s, monitor| {
// monitor.connect_connector_notify
// }));
} else { } else {
println!("failed to get X11 window"); println!("failed to get X11 window");
} }
@ -193,29 +211,26 @@ impl Window {
fn setup_factory(&self) { fn setup_factory(&self) {
let factory = SignalListItemFactory::new(); let factory = SignalListItemFactory::new();
// factory.connect_setup(move |_, list_item| { factory.connect_setup(move |_, list_item| {
// let row = ApplicationRow::new():q ; let dock_item = DockItem::new();
// list_item.set_child(Some(&row)) list_item.set_child(Some(&dock_item));
// }); });
// factory.connect_bind(move |_, list_item| { factory.connect_bind(move |_, list_item| {
// let application_object = list_item let application_object = list_item
// .item() .item()
// .expect("The item has to exist.") .expect("The item has to exist.")
// .downcast::<ApplicationObject>() .downcast::<DesktopAppInfo>()
// .expect("The item has to be an `ApplicationObject`"); .expect("The item has to be a `DesktopAppInfo`");
// let row = list_item let dock_item = list_item
// .child() .child()
// .expect("The list item child needs to exist.") .expect("The list item child needs to exist.")
// .downcast::<ApplicationRow>() .downcast::<DockItem>()
// .expect("The list item type needs to be `ApplicationRow`"); .expect("The list item type needs to be `DockItem`");
// if list_item.position() < 9 {
// row.set_shortcut(list_item.position() + 1);
// }
// row.set_app_info(application_object); dock_item.set_app_info(&application_object);
// }); });
// Set the factory of the list view // Set the factory of the list view
let imp = imp::Window::from_instance(self); let imp = imp::Window::from_instance(self);
// imp.list_view.set_factory(Some(&factory)); imp.list_view.set_factory(Some(&factory));
} }
} }

View file

@ -1,38 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<interface> <interface>
<template class="LauncherWindow" parent="GtkApplicationWindow"> <template class="LauncherWindow" parent="GtkApplicationWindow">
<property name="height-request">64</property> <property name="height-request">20</property>
<property name="width-request">256</property>
<property name="title">Gtk Pop Dock</property> <property name="title">Gtk Pop Dock</property>
<property name="decorated">false</property> <property name="decorated">false</property>
<property name="resizable">false</property> <property name="resizable">false</property>
<child> <child>
<object class="GtkRevealer" id="revealer"> <object class="GtkRevealer" id="revealer">
<property name="hexpand">true</property>
<property name="vexpand">true</property>
<property name="reveal-child">true</property> <property name="reveal-child">true</property>
<property name="transition-duration">200</property> <property name="transition-duration">200</property>
<property name="transition-type">swing-up</property> <property name="transition-type">swing-up</property>
<!--
<child> <child>
<object class="GtkListView" id="list_view"> <object class="GtkBox">
<property name="orientation">horizontal</property> <property name="orientation">horizontal</property>
<property name="hexpand">true</property> <property name="margin-start">4</property>
<property name="margin-end">4</property>
<property name="margin-top">4</property> <property name="margin-top">4</property>
<property name="margin-bottom">4</property> <property name="margin-bottom">4</property>
<property name="margin-start">4</property> <property name="spacing">4</property>
<property name="margin-end">4</property> <property name="name">dock-container</property>
</object> <child>
</child> <object class="GtkListView" id="list_view">
--> <property name="orientation">horizontal</property>
<child> <property name="hexpand">true</property>
<object class="GtkLabel"> </object>
<property name="width-request">256</property> </child>
<property name="height-request">64</property> <child>
<property name="label">hello world</property> <object class="GtkSeparator">
<property name="hexpand">true</property> <property name="margin-top">4</property>
<property name="margin-start">4</property> <property name="margin-bottom">4</property>
<property name="margin-end">4</property> <property name="orientation">vertical</property>
<property name="vexpand">true</property>
</object>
</child>
</object> </object>
</child> </child>
</object> </object>