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

@ -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);
// }
// }
// }
}