Giter Club home page Giter Club logo

Comments (9)

tronta avatar tronta commented on May 16, 2024 1

Ah, sorry. I haven't read through all of it yet.
Maybe it makes sense to add a sentences that other cases than the default implementation will be described later.

from relm4.

AaronErhardt avatar AaronErhardt commented on May 16, 2024

I think this is what you're looking for: https://aaronerhardt.github.io/relm4-book/book/widget_macro_reference.html#functions

from relm4.

tronta avatar tronta commented on May 16, 2024

Maybe can you elaborate a little how I can do this in detail? The explanation in the documentation did not help me.
Maybe a concrete example could help. That would be great.

from relm4.

AaronErhardt avatar AaronErhardt commented on May 16, 2024

If you want to use a function to initialize a widget you just need to use

property_name = gtk::Scale::with_range(gtk::Orientation::Vertical, 0.0, 100.0, 1.0)  { ... }

instead of

property_name = gtk::Scale { ... }

I think gtk::Scale has no proper setter and getter methods for min and max values so you need to use the constructor function.

from relm4.

AaronErhardt avatar AaronErhardt commented on May 16, 2024

Was that explanation helpful or is it still unclear?

from relm4.

tronta avatar tronta commented on May 16, 2024

Actually it was very easy, after trying it. Probably, I was just a little be confused as it is described in the back with some little text.
For me at least, it would have helped, even more, if there would be something in the running code.
The whole thing is simple enough to be added in the first example.

What about changing the creation of one of the buttons in the first example like this:

use gtk::prelude::{BoxExt, ButtonExt, GtkWindowExt, OrientableExt};
use relm4::{send, AppUpdate, Model, RelmApp, Sender, WidgetPlus, Widgets};

#[derive(Default)]
struct AppModel {
    counter: u8,
}

enum AppMsg {
    Increment,
    Decrement,
}

impl Model for AppModel {
    type Msg = AppMsg;
    type Widgets = AppWidgets;
    type Components = ();
}

impl AppUpdate for AppModel {
    fn update(&mut self, msg: AppMsg, _components: &(), _sender: Sender<AppMsg>) -> bool {
        match msg {
            AppMsg::Increment => {
                self.counter = self.counter.wrapping_add(1);
            }
            AppMsg::Decrement => {
                self.counter = self.counter.wrapping_sub(1);
            }
        }
        true
    }
}

#[relm4_macros::widget]
impl Widgets<AppModel, ()> for AppWidgets {
    view! {
        gtk::ApplicationWindow {
            set_title: Some("Simple app"),
            set_default_width: 300,
            set_default_height: 100,
            set_child = Some(&gtk::Box) {
                set_orientation: gtk::Orientation::Vertical,
                set_margin_all: 5,
                set_spacing: 5,

                append = &gtk::Button {
                    set_label: "Increment",
                    connect_clicked(sender) => move |_| {
                        send!(sender, AppMsg::Increment);
                    },
                },
                append = &gtk::Button::with_label("Decrement") {
                    connect_clicked(sender) => move |_| {
                        send!(sender, AppMsg::Decrement);
                    },
                },
                append = &gtk::Label {
                    set_margin_all: 5,
                    set_label: watch! { &format!("Counter: {}", model.counter) },
                }
            },
        }
    }
}

fn main() {
    let model = AppModel::default();
    let app = RelmApp::new(model);
    app.run();
}

And then explain it directly there?

from relm4.

AaronErhardt avatar AaronErhardt commented on May 16, 2024

Sounds like a good idea. I will add this to the book. Thanks a lot for your feedback!

from relm4.

AaronErhardt avatar AaronErhardt commented on May 16, 2024

I added your example to the book and added some headings to make it more clear: https://aaronerhardt.github.io/relm4-book/book/widget_macro.html
What do you think?

from relm4.

tronta avatar tronta commented on May 16, 2024

Looks good! Thanks.

from relm4.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.