libcosmic/widgets/src/labeled_item/mod.rs

91 lines
2 KiB
Rust
Raw Normal View History

2022-02-28 14:45:38 -05:00
mod imp;
2022-04-05 12:33:34 -04:00
use relm4::{
gtk::{glib::IsA, prelude::*, Align, Box as GtkBox, Orientation, Widget},
Component, ComponentController, ComponentParts, Controller,
};
use std::{cell::Ref, ops::Deref};
2022-02-28 14:45:38 -05:00
pub struct LabeledItem {
2022-04-05 12:33:34 -04:00
root: GtkBox,
controller: Controller<imp::LabeledItem>,
2022-02-28 14:45:38 -05:00
}
impl LabeledItem {
fn inner(&self) -> Ref<'_, ComponentParts<imp::LabeledItem>> {
2022-02-28 14:45:38 -05:00
self.controller.state().get()
}
pub fn new() -> Self {
Self::default()
}
2022-04-05 12:33:34 -04:00
pub fn widget(&self) -> GtkBox {
2022-02-28 14:45:38 -05:00
self.root.clone()
}
pub fn title(&self) -> String {
self.inner().model.title().to_owned()
}
pub fn description(&self) -> Option<String> {
self.inner().model.description().cloned()
}
pub fn alignment(&self) -> Align {
self.inner().model.alignment()
}
pub fn child(&self) -> Option<Widget> {
self.inner().model.child().cloned()
}
pub fn set_title<S>(&self, title: S)
where
S: ToString,
{
self.inner().model.set_title(title)
}
2022-03-02 10:18:29 -05:00
pub fn set_description<'a, O>(&self, description: O)
2022-02-28 14:45:38 -05:00
where
2022-03-02 10:18:29 -05:00
O: Into<Option<&'a str>>,
2022-02-28 14:45:38 -05:00
{
self.inner().model.set_description(description)
}
pub fn set_alignment(&self, align: Align) {
self.inner().model.set_alignment(align)
}
pub fn set_child(&self, child: &impl IsA<Widget>) {
let widget = child.upcast_ref();
self.inner().model.set_child(widget.clone());
}
}
impl Default for LabeledItem {
fn default() -> Self {
2022-04-05 12:33:34 -04:00
let root = GtkBox::new(Orientation::Horizontal, 0);
2022-02-28 14:45:38 -05:00
let controller = imp::LabeledItem::init()
.attach_to(&root)
.launch(())
.detach();
Self { root, controller }
}
}
impl AsRef<Widget> for LabeledItem {
fn as_ref(&self) -> &Widget {
self.root.upcast_ref()
}
}
impl Deref for LabeledItem {
2022-04-05 12:33:34 -04:00
type Target = GtkBox;
fn deref(&self) -> &Self::Target {
&self.root
}
}