styling tweaks & add separator

This commit is contained in:
Ashley Wulber 2021-12-15 11:37:28 -05:00 committed by Jeremy Soller
parent 5927e29fc6
commit a06a821bbe
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());
// Get state and set model
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
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 app_info1 = obj1.downcast_ref::<gio::DesktopAppInfo>().unwrap();

View file

@ -1,59 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="ApplicationRow" parent="GtkBox">
<template class="DockItem" parent="GtkBox">
<property name="orientation">vertical</property>
<property name="spacing">4</property>
<property name="margin-start">4</property>
<property name="margin-end">4</property>
<child>
<object class="GtkImage" id="categoryimage">
<property name="pixel-size">64</property>
</object>
</child>
<child>
<child>
<object class="GtkImage" id="image">
<property name="margin-top">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-start">4</property>
<property name="margin-end">4</property>
<property name="margin-top">4</property>
<property name="margin-bottom">4</property>
<child>
<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>
<property name="pixel-size">48</property>
</object>
</child>
</template>

View file

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

View file

@ -1,7 +1,12 @@
use crate::icon_source;
use gdk4::ContentProvider;
use gdk4::Display;
use gio::File;
use glib::FromVariant;
use glib::Variant;
use gtk4 as gtk;
use gtk4::DragSource;
use gtk4::IconTheme;
mod imp;
use crate::ApplicationObject;
@ -10,70 +15,81 @@ use gtk::prelude::*;
use gtk::subclass::prelude::*;
glib::wrapper! {
pub struct ApplicationRow(ObjectSubclass<imp::ApplicationRow>)
pub struct DockItem(ObjectSubclass<imp::DockItem>)
@extends gtk::Widget, gtk::Box;
}
impl Default for ApplicationRow {
impl Default for DockItem {
fn default() -> Self {
Self::new()
}
}
impl ApplicationRow {
impl DockItem {
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) {
let self_ = imp::ApplicationRow::from_instance(self);
pub fn set_app_info(&self, app_info: &gio::DesktopAppInfo) {
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") {
self_.name.set_text(
&name
.get::<String>()
.expect("Property name needs to be a String."),
);
let drag = DragSource::builder()
.name("application library drag source")
.actions(gdk4::DragAction::COPY)
// .content()
.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") {
self_.description.set_text(
&desc
.get::<String>()
.expect("Property description 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()))
if let Some(icon) = app_info.icon() {
dbg!("setting icon {}", app_info.name());
self_.image.set_from_gicon(&icon);
// set drag source icon if possible...
// 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 Some(paintable_icon) = icon_theme.lookup_by_gicon(
&icon,
64,
1,
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) {
let self_ = imp::ApplicationRow::from_instance(self);
self_.shortcut.set_text(&format!("Ctrl + {}", indx));
}
// pub fn set_app_info(&self, app_obj: ApplicationObject) {
// let self_ = imp::DockItem::from_instance(self);
// 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 dock_item;
mod window;
use gdk4::Display;

View file

@ -1,13 +1,21 @@
row:hover {
transition: 100ms;
background: #888888;
border-radius: 10px;
}
row {
background: #333333;
border-radius: 10px;
}
listview {
border-radius: 10px;
background: #333333;
}
window {
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::EventController;
use gtk4::EventControllerMotion;
use gtk4::Revealer;
@ -7,15 +6,15 @@ use glib::subclass::InitializingObject;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{gio, glib};
use gtk::{CompositeTemplate, Entry, ListView};
use gtk::{CompositeTemplate, ListView};
use once_cell::sync::OnceCell;
// Object holding the state
#[derive(CompositeTemplate, Default)]
#[template(file = "window.ui")]
pub struct Window {
// #[template_child]
// pub list_view: TemplateChild<ListView>,
#[template_child]
pub list_view: TemplateChild<ListView>,
#[template_child]
pub revealer: TemplateChild<Revealer>,
pub model: OnceCell<gio::ListStore>,

View file

@ -1,14 +1,13 @@
mod imp;
// use crate::ApplicationObject;
use crate::TX;
use crate::dock_item::DockItem;
use crate::X11_CONN;
use gdk4::Rectangle;
use gdk4::Surface;
use gdk4_x11::X11Surface;
use gio::DesktopAppInfo;
use gtk4 as gtk;
use gtk4::Allocation;
use gtk4::EventControllerMotion;
use postage::prelude::Sink;
use x11rb::connection::Connection;
use x11rb::protocol::xproto::ConnectionExt;
@ -28,8 +27,6 @@ glib::wrapper! {
gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
}
const NUM_LAUNCHER_ITEMS: u8 = 9;
impl Window {
pub fn new(app: &Application) -> Self {
let self_: Self = Object::new(&[("application", app)]).expect("Failed to create `Window`.");
@ -44,60 +41,84 @@ impl Window {
fn setup_model(&self) {
// Get state and set model
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()
// .model(&slice_model)
// .autoselect(false)
// .can_unselect(true)
// .selected(gtk4::INVALID_LIST_POSITION)
// .build();
let selection_model = gtk::SingleSelection::builder()
.autoselect(false)
.can_unselect(true)
.selected(gtk4::INVALID_LIST_POSITION)
.model(&model)
.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");
// // Wrap model with selection and pass it to the list view
// imp.list_view.set_model(Some(&selection_model));
imp.model.set(model).expect("Could not set model");
// Wrap model with selection and pass it to the list view
imp.list_view.set_model(Some(&selection_model));
}
fn setup_callbacks(&self) {
// Get state
let imp = imp::Window::from_instance(self);
let window = self.clone().upcast::<gtk::Window>();
// let list_view = &imp.list_view;
// let lv = list_view.get();
let list_view = &imp.list_view;
// let revealer = Revealer::builder()
// .child(&window)
// .reveal_child(false)
// .transition_duration(200)
// .transition_type(gtk4::RevealerTransitionType::SlideUp)
// .build();
let app_selection_model = list_view
.model()
.expect("List view missing selection model")
.downcast::<gtk::SingleSelection>()
.expect("could not downcast listview model to single selection model");
// let app_selection_model = list_view
// .model()
// .expect("List view missing selection model")
// .downcast::<gtk::SingleSelection>()
// .expect("could not downcast listview model to single selection model");
// app_selection_model.connect_selected_notify(glib::clone!(@weak window => move |model| {
// let i = model.selected();
// println!("acitvating... {}", i + 1);
// let app_info = model.item(i);
// if app_info.is_none() {
// println!("oops no app for this row...");
// return;
// }
// if let Ok(id) = app_info.unwrap().property("id") {
// let id = id.get::<u32>().expect("App ID must be u32");
// 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;
// }
// });
// }
// }));
app_selection_model.connect_selected_notify(glib::clone!(@weak window => move |model| {
let position = model.selected();
println!("selected app {}", position);
// Launch the application when an item of the list is activated
if let Some(item) = model.item(position) {
let app_info = item.downcast::<gio::DesktopAppInfo>().unwrap();
let context = window.display().app_launch_context();
if let Err(err) = app_info.launch(&[], Some(&context)) {
gtk::MessageDialog::builder()
.text(&format!("Failed to start {}", app_info.name()))
.secondary_text(&err.to_string())
.message_type(gtk::MessageType::Error)
.modal(true)
.transient_for(&window)
.build()
.show();
}
}
}));
let event_controller = &imp.event_controller.get().unwrap();
let revealer = &imp.revealer.get();
@ -161,9 +182,6 @@ impl Window {
s.connect_height_notify(surface_resize_handler.clone());
s.connect_width_notify(surface_resize_handler.clone());
s.connect_scale_factor_notify(surface_resize_handler);
// s.connect_enter_monitor(glib::clone!(@weak rv => move |s, monitor| {
// monitor.connect_connector_notify
// }));
} else {
println!("failed to get X11 window");
}
@ -193,29 +211,26 @@ impl Window {
fn setup_factory(&self) {
let factory = SignalListItemFactory::new();
// factory.connect_setup(move |_, list_item| {
// let row = ApplicationRow::new():q ;
// list_item.set_child(Some(&row))
// });
// factory.connect_bind(move |_, list_item| {
// let application_object = list_item
// .item()
// .expect("The item has to exist.")
// .downcast::<ApplicationObject>()
// .expect("The item has to be an `ApplicationObject`");
// let row = list_item
// .child()
// .expect("The list item child needs to exist.")
// .downcast::<ApplicationRow>()
// .expect("The list item type needs to be `ApplicationRow`");
// if list_item.position() < 9 {
// row.set_shortcut(list_item.position() + 1);
// }
factory.connect_setup(move |_, list_item| {
let dock_item = DockItem::new();
list_item.set_child(Some(&dock_item));
});
factory.connect_bind(move |_, list_item| {
let application_object = list_item
.item()
.expect("The item has to exist.")
.downcast::<DesktopAppInfo>()
.expect("The item has to be a `DesktopAppInfo`");
let dock_item = list_item
.child()
.expect("The list item child needs to exist.")
.downcast::<DockItem>()
.expect("The list item type needs to be `DockItem`");
// row.set_app_info(application_object);
// });
dock_item.set_app_info(&application_object);
});
// Set the factory of the list view
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"?>
<interface>
<template class="LauncherWindow" parent="GtkApplicationWindow">
<property name="height-request">64</property>
<property name="width-request">256</property>
<property name="height-request">20</property>
<property name="title">Gtk Pop Dock</property>
<property name="decorated">false</property>
<property name="resizable">false</property>
<child>
<object class="GtkRevealer" id="revealer">
<property name="hexpand">true</property>
<property name="vexpand">true</property>
<property name="reveal-child">true</property>
<property name="transition-duration">200</property>
<property name="transition-type">swing-up</property>
<!--
<child>
<object class="GtkListView" id="list_view">
<object class="GtkBox">
<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-bottom">4</property>
<property name="margin-start">4</property>
<property name="margin-end">4</property>
</object>
</child>
-->
<child>
<object class="GtkLabel">
<property name="width-request">256</property>
<property name="height-request">64</property>
<property name="label">hello world</property>
<property name="hexpand">true</property>
<property name="margin-start">4</property>
<property name="margin-end">4</property>
<property name="spacing">4</property>
<property name="name">dock-container</property>
<child>
<object class="GtkListView" id="list_view">
<property name="orientation">horizontal</property>
<property name="hexpand">true</property>
</object>
</child>
<child>
<object class="GtkSeparator">
<property name="margin-top">4</property>
<property name="margin-bottom">4</property>
<property name="orientation">vertical</property>
<property name="vexpand">true</property>
</object>
</child>
</object>
</child>
</object>