Clean up main using view! macro

This commit is contained in:
Julian Mutter 2024-01-16 18:39:38 +01:00
parent 02a41a50c0
commit f6a4de51e7

View File

@ -1,16 +1,12 @@
mod mcdu;
use gtk::glib::clone;
use gtk::prelude::{BoxExt, ButtonExt, GtkWindowExt};
use gtk::prelude::*;
use mcdu::McduModel;
use relm4::gtk::prelude::GridExt;
use relm4::{
gtk, Component, ComponentController, ComponentParts, ComponentSender, RelmApp, RelmWidgetExt,
SimpleComponent,
};
use relm4::prelude::*;
struct AppModel {
text: String,
mcdu: Controller<McduModel>,
}
#[derive(Debug)]
@ -18,56 +14,37 @@ enum AppInput {
McduInput(char),
}
struct AppWidgets {
label: gtk::Label,
}
#[relm4::component]
impl SimpleComponent for AppModel {
/// The type of the messages that this component can receive.
type Input = AppInput;
/// The type of the messages that this component can send.
type Output = ();
/// The type of data with which this component will be initialized.
type Init = u8;
/// The root GTK widget that this component will create.
type Root = gtk::Window;
/// A data structure that contains the widgets that you will need to update.
type Widgets = AppWidgets;
type Init = ();
fn init_root() -> Self::Root {
gtk::Window::builder()
.title("Simple app")
.default_width(300)
.default_height(100)
.build()
view! {
#[root]
gtk::Window{
set_default_width: 300,
set_default_height: 300,
set_title: Some("Play music!!!"),
gtk::Box {
set_orientation: gtk::Orientation::Horizontal,
model.mcdu.widget(),
#[name = "label"]
gtk::Label {
#[watch]
set_label: &model.text,
set_margin_all: 5,
}
}
}
}
/// Initialize the UI and model.
fn init(
counter: Self::Init,
_init: Self::Init,
window: &Self::Root,
sender: ComponentSender<Self>,
) -> relm4::ComponentParts<Self> {
relm4_icons::initialize_icons();
let model = AppModel {
text: String::new(),
};
let layout = gtk::Box::builder()
.orientation(gtk::Orientation::Horizontal)
// .spacing(5)
.build();
let tab_chooser = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.build();
let main_view = gtk::Box::builder()
.orientation(gtk::Orientation::Vertical)
.build();
let ll = gtk::GridLayout::builder()
.column_homogeneous(true)
.row_homogeneous(true)
.build();
let mcdu = McduModel::builder()
.launch(())
@ -75,26 +52,12 @@ impl SimpleComponent for AppModel {
mcdu::McduOutput::ButtonPress(c) => AppInput::McduInput(c),
});
let inc_button = gtk::Button::with_label("Increment3");
let dec_button = gtk::Button::with_label("Decrement");
let model = AppModel {
text: String::from("Text: NONE"),
mcdu,
};
let x = gtk::Button::default();
x.set_label("Tab 1");
// x.set_icon_name("plus");
tab_chooser.append(&x);
let label = gtk::Label::new(Some(&format!("Text: NONE")));
label.set_margin_all(5);
window.set_child(Some(&layout));
layout.set_margin_all(5);
layout.append(&tab_chooser);
layout.append(&main_view);
layout.append(mcdu.widget());
layout.append(&label);
let widgets = AppWidgets { label };
let widgets = view_output!();
ComponentParts { model, widgets }
}
@ -104,14 +67,9 @@ impl SimpleComponent for AppModel {
AppInput::McduInput(c) => self.text.push(c),
}
}
/// Update the view to represent the updated model.
fn update_view(&self, widgets: &mut Self::Widgets, _sender: ComponentSender<Self>) {
widgets.label.set_label(&format!("Text: {}", self.text));
}
}
fn main() {
let app = RelmApp::new("relm4.test.simple_manual");
app.run::<AppModel>(0);
let app = RelmApp::new("de.frajul.sheet-organizer");
app.run::<AppModel>(());
}