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; mod mcdu;
use gtk::glib::clone; use gtk::prelude::*;
use gtk::prelude::{BoxExt, ButtonExt, GtkWindowExt};
use mcdu::McduModel; use mcdu::McduModel;
use relm4::gtk::prelude::GridExt; use relm4::prelude::*;
use relm4::{
gtk, Component, ComponentController, ComponentParts, ComponentSender, RelmApp, RelmWidgetExt,
SimpleComponent,
};
struct AppModel { struct AppModel {
text: String, text: String,
mcdu: Controller<McduModel>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -18,56 +14,37 @@ enum AppInput {
McduInput(char), McduInput(char),
} }
struct AppWidgets { #[relm4::component]
label: gtk::Label,
}
impl SimpleComponent for AppModel { impl SimpleComponent for AppModel {
/// The type of the messages that this component can receive.
type Input = AppInput; type Input = AppInput;
/// The type of the messages that this component can send.
type Output = (); type Output = ();
/// The type of data with which this component will be initialized. type Init = ();
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;
fn init_root() -> Self::Root { view! {
gtk::Window::builder() #[root]
.title("Simple app") gtk::Window{
.default_width(300) set_default_width: 300,
.default_height(100) set_default_height: 300,
.build() 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( fn init(
counter: Self::Init, _init: Self::Init,
window: &Self::Root, window: &Self::Root,
sender: ComponentSender<Self>, sender: ComponentSender<Self>,
) -> relm4::ComponentParts<Self> { ) -> relm4::ComponentParts<Self> {
relm4_icons::initialize_icons(); 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() let mcdu = McduModel::builder()
.launch(()) .launch(())
@ -75,26 +52,12 @@ impl SimpleComponent for AppModel {
mcdu::McduOutput::ButtonPress(c) => AppInput::McduInput(c), mcdu::McduOutput::ButtonPress(c) => AppInput::McduInput(c),
}); });
let inc_button = gtk::Button::with_label("Increment3"); let model = AppModel {
let dec_button = gtk::Button::with_label("Decrement"); text: String::from("Text: NONE"),
mcdu,
};
let x = gtk::Button::default(); let widgets = view_output!();
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 };
ComponentParts { model, widgets } ComponentParts { model, widgets }
} }
@ -104,14 +67,9 @@ impl SimpleComponent for AppModel {
AppInput::McduInput(c) => self.text.push(c), 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() { fn main() {
let app = RelmApp::new("relm4.test.simple_manual"); let app = RelmApp::new("de.frajul.sheet-organizer");
app.run::<AppModel>(0); app.run::<AppModel>(());
} }