fractal/session/model/sidebar_data/
icon_item.rsuse std::fmt;
use gettextrs::gettext;
use gtk::{glib, prelude::*, subclass::prelude::*};
use crate::session::model::RoomCategory;
#[derive(Debug, Default, Hash, Eq, PartialEq, Clone, Copy, glib::Enum)]
#[repr(u32)]
#[enum_type(name = "SidebarIconItemType")]
pub enum SidebarIconItemType {
#[default]
Explore = 0,
Forget = 1,
}
impl SidebarIconItemType {
pub fn icon_name(self) -> &'static str {
match self {
Self::Explore => "explore-symbolic",
Self::Forget => "user-trash-symbolic",
}
}
}
impl fmt::Display for SidebarIconItemType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let label = match self {
Self::Explore => gettext("Explore"),
Self::Forget => gettext("Forget Room"),
};
f.write_str(&label)
}
}
mod imp {
use std::{cell::Cell, marker::PhantomData};
use super::*;
#[derive(Debug, Default, glib::Properties)]
#[properties(wrapper_type = super::SidebarIconItem)]
pub struct SidebarIconItem {
#[property(get, construct_only, builder(SidebarIconItemType::default()))]
pub item_type: Cell<SidebarIconItemType>,
#[property(get = Self::display_name)]
pub display_name: PhantomData<String>,
#[property(get = Self::icon_name)]
pub icon_name: PhantomData<String>,
}
#[glib::object_subclass]
impl ObjectSubclass for SidebarIconItem {
const NAME: &'static str = "SidebarIconItem";
type Type = super::SidebarIconItem;
}
#[glib::derived_properties]
impl ObjectImpl for SidebarIconItem {}
impl SidebarIconItem {
fn display_name(&self) -> String {
self.item_type.get().to_string()
}
fn icon_name(&self) -> String {
self.item_type.get().icon_name().to_owned()
}
}
}
glib::wrapper! {
pub struct SidebarIconItem(ObjectSubclass<imp::SidebarIconItem>);
}
impl SidebarIconItem {
pub fn new(item_type: SidebarIconItemType) -> Self {
glib::Object::builder()
.property("item-type", item_type)
.build()
}
pub fn visible_for_room_category(&self, source_category: Option<RoomCategory>) -> bool {
match self.item_type() {
SidebarIconItemType::Explore => true,
SidebarIconItemType::Forget => source_category == Some(RoomCategory::Left),
}
}
}