1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::cell::RefCell;
use adw::prelude::*;
use glib::{clone, subclass, ParamSpec, Properties};
use gtk::subclass::prelude::*;
use gtk::{glib, CompositeTemplate};
use crate::main::context::{
SkContext, SkContextDetail, SkContextDetailGroup, SkContextDetailKind, SkContextDetailLevel,
};
use crate::main::ui::context::SkContextDetailRow;
use crate::main::ui::utils;
mod imp {
use super::*;
#[derive(Debug, CompositeTemplate, Default, Properties)]
#[properties(wrapper_type = super::SkContextBox)]
#[template(resource = "/de/haeckerfelix/Souk/gtk/context_box.ui")]
pub struct SkContextBox {
#[property(get, set = Self::set_context)]
context: RefCell<Option<SkContext>>,
#[template_child]
kind_stack: TemplateChild<gtk::Stack>,
#[template_child]
icon_image: TemplateChild<gtk::Image>,
#[template_child]
text_label: TemplateChild<gtk::Label>,
#[template_child]
title_label: TemplateChild<gtk::Label>,
#[template_child]
description_label: TemplateChild<gtk::Label>,
#[template_child]
details_listbox: TemplateChild<gtk::ListBox>,
}
#[glib::object_subclass]
impl ObjectSubclass for SkContextBox {
const NAME: &'static str = "SkContextBox";
type ParentType = gtk::Box;
type Type = super::SkContextBox;
fn class_init(klass: &mut Self::Class) {
Self::bind_template(klass);
}
fn instance_init(obj: &subclass::InitializingObject<Self>) {
SkContext::static_type();
obj.init_template();
}
}
impl ObjectImpl for SkContextBox {
fn properties() -> &'static [ParamSpec] {
Self::derived_properties()
}
fn property(&self, id: usize, pspec: &ParamSpec) -> glib::Value {
Self::derived_property(self, id, pspec)
}
fn set_property(&self, id: usize, value: &glib::Value, pspec: &ParamSpec) {
Self::derived_set_property(self, id, value, pspec)
}
}
impl WidgetImpl for SkContextBox {}
impl BoxImpl for SkContextBox {}
impl SkContextBox {
fn set_context(&self, value: &SkContext) {
*self.context.borrow_mut() = Some(value.clone());
self.obj().notify("context");
self.update_widgets();
}
fn update_widgets(&self) {
let context = self.obj().context().unwrap();
let summary = context.summary();
if summary.kind() == SkContextDetailKind::Icon {
self.kind_stack.set_visible_child(&self.icon_image.get());
self.icon_image.set_icon_name(Some(&summary.kind_value()));
} else if summary.kind() == SkContextDetailKind::Size {
let markup = utils::size_to_markup(&summary.kind_value());
self.text_label.set_markup(&markup);
} else {
self.kind_stack.set_visible_child(&self.text_label.get());
self.text_label.set_markup(&summary.kind_value());
}
utils::remove_css_colors(&self.icon_image.get());
utils::remove_css_colors(&self.text_label.get());
let css = match summary.level() {
SkContextDetailLevel::Neutral => "color-neutral",
SkContextDetailLevel::Good => "color-good",
SkContextDetailLevel::Minor => "color-minor",
SkContextDetailLevel::Moderate => "color-moderate",
SkContextDetailLevel::Warning => "color-warning",
SkContextDetailLevel::Bad => "color-bad",
};
self.icon_image.add_css_class(css);
self.text_label.add_css_class(css);
self.title_label.set_text(&context.summary().title());
self.description_label
.set_markup(&context.summary().description());
self.details_listbox.bind_model(
Some(&context.details()),
clone!(@weak self as this => @default-panic, move |detail_group| {
let detail_group = detail_group.downcast_ref::<SkContextDetailGroup>().unwrap();
let group_box = adw::PreferencesGroup::new();
group_box.set_margin_bottom(12);
if let Some(title) = detail_group.title(){
group_box.set_title(&title);
}
if let Some(description) = detail_group.description(){
group_box.set_description(Some(&description));
}
for detail in detail_group.snapshot().iter(){
let detail = detail.downcast_ref::<SkContextDetail>().unwrap();
group_box.add(&SkContextDetailRow::new(detail, false));
}
let row = gtk::ListBoxRow::new();
row.set_child(Some(&group_box));
row.set_activatable(false);
row.upcast()
}),
);
}
}
}
glib::wrapper! {
pub struct SkContextBox(
ObjectSubclass<imp::SkContextBox>)
@extends gtk::Widget, gtk::Box;
}
impl SkContextBox {
pub fn new(context: &SkContext) -> Self {
glib::Object::builder().property("context", context).build()
}
}