remove template for dock_item

This commit is contained in:
Ashley Wulber 2021-12-30 17:53:06 -05:00
parent e48f501b0d
commit 71273ec430
16 changed files with 82 additions and 87 deletions

View file

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="DockItem" parent="GtkBox">
<property name="orientation">vertical</property>
<child>
<object class="GtkImage" id="image">
<property name="margin-start">4</property>
<property name="margin-end">4</property>
<property name="margin-top">4</property>
<property name="pixel-size">48</property>
<property name="icon-size">large</property>
</object>
</child>
<child>
<object class="GtkLabel" id="dots">
<property name="hexpand">true</property>
<property name="halign">center</property>
</object>
</child>
</template>
</interface>

View file

@ -1,15 +1,12 @@
use gtk4::glib;
use gtk4::prelude::*;
use gtk4::subclass::prelude::*;
use gtk4::CompositeTemplate;
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug, Default, CompositeTemplate)]
#[template(file = "dock_item.ui")]
#[derive(Debug, Default)]
pub struct DockItem {
#[template_child]
pub image: TemplateChild<gtk4::Image>,
#[template_child]
pub dots: TemplateChild<gtk4::Label>,
pub image: Rc<RefCell<gtk4::Image>>,
pub dots: Rc<RefCell<gtk4::Label>>,
}
#[glib::object_subclass]
@ -17,14 +14,6 @@ impl ObjectSubclass for DockItem {
const NAME: &'static str = "DockItem";
type Type = super::DockItem;
type ParentType = gtk4::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 DockItem {}

View file

@ -1,8 +1,12 @@
use cascade::cascade;
use gio::DesktopAppInfo;
use gio::Icon;
use gtk4::glib;
use gtk4::prelude::*;
use gtk4::subclass::prelude::*;
use gtk4::Align;
use gtk4::Image;
use gtk4::Label;
use crate::dock_object::DockObject;
use crate::utils::BoxedWindowList;
@ -11,7 +15,9 @@ mod imp;
glib::wrapper! {
pub struct DockItem(ObjectSubclass<imp::DockItem>)
@extends gtk4::Widget, gtk4::Box;
@extends gtk4::Widget, gtk4::Box,
@implements gtk4::Accessible, gtk4::Buildable, gtk4::ConstraintTarget, gtk4::Orientable;
}
impl Default for DockItem {
@ -23,6 +29,25 @@ impl Default for DockItem {
impl DockItem {
pub fn new() -> Self {
let self_: DockItem = glib::Object::new(&[]).expect("Failed to create DockItem");
self_.set_orientation(gtk4::Orientation::Vertical);
let image = cascade! {
Image::new();
..set_margin_start(4);
..set_margin_end(4);
..set_margin_top(4);
..set_pixel_size(48);
};
let dots = cascade! {
Label::new(Some(""));
..set_hexpand(true);
..set_halign(Align::Center);
};
self_.append(&image);
self_.append(&dots);
let imp = imp::DockItem::from_instance(&self_);
imp.image.replace(image);
imp.dots.replace(dots);
self_.show();
self_
}
@ -31,24 +56,26 @@ impl DockItem {
let self_ = imp::DockItem::from_instance(self);
if let Ok(app_info_value) = dock_object.property("appinfo") {
if let Ok(Some(app_info)) = app_info_value.get::<Option<DesktopAppInfo>>() {
self_.image.set_tooltip_text(Some(&app_info.name()));
self_
.image
.borrow()
.set_tooltip_text(Some(&app_info.name()));
let icon = app_info.icon().unwrap_or(
Icon::for_string("image-missing").expect("Failed to set default icon"),
);
self_.image.set_from_gicon(&icon);
self_.image.borrow().set_from_gicon(&icon);
}
} else {
println!("initializing dock item failed...");
}
if let Ok(active_value) = dock_object.property("active") {
if let Ok(active) = active_value.get::<BoxedWindowList>() {
self_.dots.set_text("");
let dots = self_.dots.borrow();
dots.set_text("");
for _ in active.0 {
self_
.dots
.set_text(format!("{}{}", self_.dots.text(), " · ").as_str());
dots.set_text(format!("{}{}", dots.text(), " · ").as_str());
}
}
}