Wrap LabeledItem in a newtype
This commit is contained in:
parent
6f7d0fad80
commit
724f7f5a9f
2 changed files with 79 additions and 9 deletions
|
|
@ -3,7 +3,7 @@ use relm4::{ComponentBuilder, ComponentParts, Sender, SimpleComponent};
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum LabeledItemMessage {
|
pub(crate) enum LabeledItemMessage {
|
||||||
Title(String),
|
Title(String),
|
||||||
Desc(Option<String>),
|
Desc(Option<String>),
|
||||||
Align(Align),
|
Align(Align),
|
||||||
|
|
@ -11,7 +11,7 @@ pub enum LabeledItemMessage {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[track]
|
#[track]
|
||||||
pub struct LabeledItem {
|
pub(crate) struct LabeledItem {
|
||||||
_title: String,
|
_title: String,
|
||||||
_desc: Option<String>,
|
_desc: Option<String>,
|
||||||
_align: Align,
|
_align: Align,
|
||||||
|
|
@ -23,16 +23,12 @@ pub struct LabeledItem {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LabeledItem {
|
impl LabeledItem {
|
||||||
pub fn new() -> ComponentBuilder<Self, gtk4::Box> {
|
|
||||||
Self::init()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn title(&self) -> &str {
|
pub fn title(&self) -> &str {
|
||||||
&self._title
|
&self._title
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn description(&self) -> &str {
|
pub fn description(&self) -> Option<&String> {
|
||||||
&self._title
|
self._desc.as_ref()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn alignment(&self) -> Align {
|
pub fn alignment(&self) -> Align {
|
||||||
|
|
@ -68,7 +64,7 @@ impl LabeledItem {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[component(pub)]
|
#[component(pub(crate))]
|
||||||
impl SimpleComponent for LabeledItem {
|
impl SimpleComponent for LabeledItem {
|
||||||
type Widgets = AppWidgets;
|
type Widgets = AppWidgets;
|
||||||
type InitParams = ();
|
type InitParams = ();
|
||||||
74
widgets/src/labeled_item/mod.rs
Normal file
74
widgets/src/labeled_item/mod.rs
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
mod imp;
|
||||||
|
|
||||||
|
use gtk4::{glib::IsA, prelude::*, Align, Orientation, Widget};
|
||||||
|
use relm4::{Component, ComponentController, ComponentParts, Controller};
|
||||||
|
use std::cell::Ref;
|
||||||
|
|
||||||
|
pub struct LabeledItem {
|
||||||
|
root: gtk4::Box,
|
||||||
|
controller: Controller<imp::LabeledItem, gtk4::Box, imp::AppWidgets, imp::LabeledItemMessage>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LabeledItem {
|
||||||
|
fn inner(&self) -> Ref<'_, ComponentParts<imp::LabeledItem, imp::AppWidgets>> {
|
||||||
|
self.controller.state().get()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn widget(&self) -> gtk4::Box {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_description<S>(&self, description: Option<S>)
|
||||||
|
where
|
||||||
|
S: ToString,
|
||||||
|
{
|
||||||
|
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 {
|
||||||
|
let root = gtk4::Box::new(Orientation::Horizontal, 0);
|
||||||
|
let controller = imp::LabeledItem::init()
|
||||||
|
.attach_to(&root)
|
||||||
|
.launch(())
|
||||||
|
.detach();
|
||||||
|
Self { root, controller }
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue