add app groups

This commit is contained in:
Ashley Wulber 2021-12-02 17:25:33 -05:00 committed by Jeremy Soller
parent 0bca6c94b6
commit b1cff2ab59
11 changed files with 373 additions and 76 deletions

View file

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="GridItem" parent="GtkBox">
<property name="orientation">vertical</property>
<property name="halign">center</property>
<property name="hexpand">true</property>
<property name="margin-top">4</property>
<property name="margin-end">4</property>
<property name="margin-bottom">4</property>
<child>
<object class="GtkImage" id="image">
<property name="margin-top">4</property>
<property name="margin-bottom">4</property>
<property name="pixel-size">80</property>
</object>
</child>
<child>
<object class="GtkLabel" id="name">
<property name="halign">center</property>
<property name="ellipsize">end</property>
<style>
<class name="title-5" />
</style>
</object>
</child>
</template>
</interface>

View file

@ -0,0 +1,34 @@
use gtk::glib;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk4 as gtk;
use gtk::CompositeTemplate;
#[derive(Debug, Default, CompositeTemplate)]
#[template(file = "grid_item.ui")]
pub struct GridItem {
#[template_child]
pub name: TemplateChild<gtk::Label>,
#[template_child]
pub image: TemplateChild<gtk::Image>,
}
#[glib::object_subclass]
impl ObjectSubclass for GridItem {
const NAME: &'static str = "GridItem";
type Type = super::GridItem;
type ParentType = gtk::Box;
fn class_init(klass: &mut Self::Class) {
Self::bind_template(klass);
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for GridItem {}
impl WidgetImpl for GridItem {}
impl BoxImpl for GridItem {}

View file

@ -0,0 +1,50 @@
use crate::app_group::AppGroup;
use gtk4 as gtk;
mod imp;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{gio, glib};
glib::wrapper! {
pub struct GridItem(ObjectSubclass<imp::GridItem>)
@extends gtk::Widget, gtk::Box;
}
impl Default for GridItem {
fn default() -> Self {
Self::new()
}
}
impl GridItem {
pub fn new() -> Self {
glib::Object::new(&[]).expect("Failed to create GridItem")
}
pub fn set_app_info(&self, app_info: &gio::DesktopAppInfo) {
let self_ = imp::GridItem::from_instance(self);
self_.name.set_text(&app_info.name());
if let Some(icon) = app_info.icon() {
self_.image.set_from_gicon(&icon);
}
}
pub fn set_group_info(&self, app_group: AppGroup) {
let self_ = imp::GridItem::from_instance(self);
if let Ok(name) = app_group.property("name") {
self_.name.set_text(
&name
.get::<String>()
.expect("property name needs to be a string."),
);
}
if let Ok(icon) = app_group.property("icon") {
&self_.image.set_from_icon_name(Some(
&icon
.get::<String>()
.expect("Property name needs to be a String."),
));
}
}
}