Gtk :: SpinButton get_value_as_int给出step_increment作为结果

时间:2017-06-07 20:44:37

标签: rust gtk gtk-rs

我正在使用Relm在Rust中使用SpinButton进行挣扎。 (免责声明,我是Rust GTK的菜鸟)

#![feature(proc_macro)]
extern crate gtk;

use chrono::{NaiveTime, Duration};

use gtk::prelude::*;
use gtk::{
    WidgetExt,
    ContainerExt,
    EntryExt,
    Adjustment
};

use relm::Widget;
use relm_attributes::widget;

#[derive(Msg, Debug)]
pub enum Msg {
    Changed,
    Display,
}

#[widget]
impl Widget for DurationSpin {
    fn model(m: Duration) -> Duration {
        m
    }

    fn update(&mut self, event: Msg) {
        match event {
            Msg::Display => self.format_display(),
            Msg::Changed => {
                println!("update, self.spin_btn.get_value_as_int() = {:?}", self.spin_btn.get_value_as_int());
                self.model = Duration::minutes(self.spin_btn.get_value_as_int() as i64)

            },
        };
    }

    fn format_display(&mut self) {
        println!("format_display, self.spin_btn.get_value_as_int() = {:?}", self.spin_btn.get_value_as_int());
        let minus = self.model.num_hours() * 60;
        self.spin_btn.set_text(&format!("{:02}:{:02}",
                                        self.model.num_hours(), self.model.num_minutes() - minus));
    }

    view! {
        #[name="spin_btn"]
        gtk::SpinButton {
            max_width_chars: 5,
            width_chars: 5,
            max_length: 5,
            numeric: false,
            output => (Msg::Display, Inhibit(false)),
            value_changed => Msg::Changed,
            adjustment: &Adjustment::new(self.model.num_minutes() as f64, 0.0, 720.0, 1.0, 60.0, 0.0),
        }
    }
}

(整个项目:https://github.com/Geobert/rusty_flexi

我的问题是点击" +"让get_value_as_int始终返回' 1'。

似乎我的output信号导致了这种情况,因为停用连接可以解决错误,但我无法看清它的错误。

1 个答案:

答案 0 :(得分:0)

似乎output信号处理程序不能异步。这意味着在这种情况下,您不应使用relm消息传递。

您应该执行以下操作:

fn init_view(&mut self) {
    let hours = self.model.num_hours();
    let minutes = self.model.num_minutes();
    self.spin_btn.connect_output(move |spin_btn| {
        let minus = hours * 60;
        spin_btn.set_text(&format!("{:02}:{:02}",
                                    hours, minutes - minus));
        Inhibit(false)
    });
}

并删除output => (Msg::Display, Inhibit(false)),