将 webOS Slider 小部件对齐到整数

发布于 2024-11-02 00:19:08 字数 1325 浏览 3 评论 0原文

在 webOS 应用程序中,我有一个 Mojo.Widget.Slider,用于将整数设置为 0、1 或 2。我自己已经负责舍入 Slider 值了(尽管有一个功能可以为您执行此操作,但我停止使用它,以防这是问题的根源,但事实并非如此),但我希望滑块在用户完成滑动后捕捉到舍入整数位置。据我所知,没有内置任何东西可以做到这一点,所以我必须自己做。我认为像下面这样的东西会起作用。

在我的场景助手的设置功能中:

this.mySliderModel = {value: 1};
this.controller.setupWidget('mySlider', {minValue: 0, maxValue: 2}, this.mySliderModel

并侦听滑块的更改:

this.mySliderChangedEventListener = this.mySliderChanged.bindAsEventListener(this);
Mojo.Event.listen(this.controller.get("mySlider"), Mojo.Event.propertyChange, this.mySliderChangedEventListener);

然后侦听器:

MainAssistant.prototype.mySliderChanged = function(event) {
    var sliderValue = Math.round(event.value);
    switch (sliderValue) {
        case 0:
        case 1:
        case 2:
            //it's a good value
            //do some stuff

            this.mySliderModel.value = sliderValue;
            this.controller.modelChanged(this.mySliderModel);
            break;
        default:
            Mojo.Log.info("mySlider error value: ", event.value);
            break;
    }
};

但是更改模型并调用 modelChanged 似乎没有执行任何操作,尽管文档和示例代码显示这可以工作。有什么想法为什么我的代码不起作用以及我可以做什么来达到预期的效果吗?

注意:我知道我可以使用不同的小部件来实现此类操作,但我想使用滑块来实现。如果我找不到办法让它工作,使用其他东西是我的备份。

In a webOS app, I have a Mojo.Widget.Slider that I use to set an integer to 0, 1, or 2. I take care of rounding the Slider value myself already (though there is a feature to do this for you, but I stopped using it in case that was the source of my problem, but it wasn't), but I would like the Slider to snap to the rounded integer position after the user is done sliding. There's nothing built in to do this as far as I can tell, so I have to do it myself. I thought something like the following would work.

In my scene assistant's setup function:

this.mySliderModel = {value: 1};
this.controller.setupWidget('mySlider', {minValue: 0, maxValue: 2}, this.mySliderModel

And to listen for changes to the slider:

this.mySliderChangedEventListener = this.mySliderChanged.bindAsEventListener(this);
Mojo.Event.listen(this.controller.get("mySlider"), Mojo.Event.propertyChange, this.mySliderChangedEventListener);

And then the listener:

MainAssistant.prototype.mySliderChanged = function(event) {
    var sliderValue = Math.round(event.value);
    switch (sliderValue) {
        case 0:
        case 1:
        case 2:
            //it's a good value
            //do some stuff

            this.mySliderModel.value = sliderValue;
            this.controller.modelChanged(this.mySliderModel);
            break;
        default:
            Mojo.Log.info("mySlider error value: ", event.value);
            break;
    }
};

But changing the model and calling modelChanged doesn't seem to do anything, although the documentation and example code shows that this can work. Any ideas why my code doesn't work and what I can do to achieve the desired effect?

Note: I'm aware I could achieve this sort of thing using different widgets, but I'd like to do it with a Slider. Using something else is my backup if I can't find a way to get this working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人│生佛魔见 2024-11-09 00:19:08

问题似乎是小部件的handleModelUpdate 中的检查。有一个标志(this.seeking)可以防止小部件在拖动过程中处理模型更改。当小部件上发生“放置”时,该标志将被清除,但 propertyChange 事件会在该标志被清除之前 2 行被触发。

解决方案可以很简单,只需添加一个非常短的超时,以便当前脚本执行可以在您再次更新模型之前完成。

MainAssistant.prototype.mySliderChanged = function(event) {
    var sliderValue = Math.round(event.value);
    switch (sliderValue) {
        case 0:
        case 1:
        case 2:
            //it's a good value
            //do some stuff
            setTimeout(function() {
                this.mySliderModel.value = sliderValue;
                this.controller.modelChanged(this.mySliderModel);
            }.bind(this), 100);
            break;
        default:
            Mojo.Log.info("mySlider error value: ", event.value);
            break;
    }
}; 

The issue appears to be a check in the widget's handleModelUpdate. There's a flag (this.seeking) that prevents the widget from handling model changes while dragging is in progress. That flag is cleared when the "drop" happens on the widget but the propertyChange event is triggered 2 lines before the flag is cleared.

The solution could be as simple as adding a very brief timeout so the current script execution can finish before you update the model again.

MainAssistant.prototype.mySliderChanged = function(event) {
    var sliderValue = Math.round(event.value);
    switch (sliderValue) {
        case 0:
        case 1:
        case 2:
            //it's a good value
            //do some stuff
            setTimeout(function() {
                this.mySliderModel.value = sliderValue;
                this.controller.modelChanged(this.mySliderModel);
            }.bind(this), 100);
            break;
        default:
            Mojo.Log.info("mySlider error value: ", event.value);
            break;
    }
}; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文