<commit_before>package ru.stablex.ui.widgets;

import nme.events.Event;
import nme.events.MouseEvent;
import nme.Lib;
import ru.stablex.ui.events.WidgetEvent;



/**
* Progress bar
*
*/
class Progress extends Widget{
    //Maximum value.
    public var max (default,set_max) : Float = 100;
    //current value
    @:isVar public var value (get_value,set_value) : Float = 0;
    private var _value : Float = 0;
    //bar
    public var bar : Widget;
    //Whether user can click/tap/slide progress bar to change value
    public var interactive (default,set_interactive) : Bool = false;
    //Visualize progress bar changes smoothly
    public var smoothChange : Bool;


    /**
    * Constructor
    *
    */
    public function new () : Void {
        super();
        this.bar = cast this.addChild(UIBuilder.create(Widget));
        this.bar.heightPt = 100;
    }//function new()


    /**
    * Set initial bar size on creation is complete.
    *
    */
    override public function onCreate () : Void {
        super.onCreate();
        this._setBarWidth(this.value, this.max);
    }//function onCreate()


    /**
    * Setter for `.max`
    *
    */
    private function set_max (m:Float) : Float {
        if( this.created ){
            this._setBarWidth(this.value, m);
        }
        return this.max = m;
    }//function set_max()


    /**
    * Setter for `.value`
    *
    */
    private function set_value (v:Float) : Float {
        this._setBarWidth(v, this.max);
        return this._value = v;
    }//function set_value()


    /**
    * Getter for `.value`
    *
    */
    private function get_value () : Float {
        return this._value;
    }//function set_value()


    /**
    * Set bar width based on provided values
    *
    */
    private inline function _setBarWidth (value:Float, max:Float) : Void {
        if( !this.smoothChange ){
            this.bar.widthPt = 100 * (max <= 0 || value <= 0 ? 0 : value / max);
        }else{
            this.bar.tween(0.1, {widthPt: 100 * (max <= 0 || value <= 0 ? 0 : value / max)}, "Quad.easeIn");
        }
    }//function _setBarWidth()


    /**
    * Setter `interactive`.
    *
    */
    private function set_interactive (interactive:Bool) : Bool {
        if( interactive ){
            this.addUniqueListener(MouseEvent.MOUSE_DOWN, this._slide);
        }else{
            this.removeEventListener(MouseEvent.MOUSE_DOWN, this._slide);
        }
        return this.interactive = interactive;
    }//function set_interactive



    /**
    * Change value on click/tap/slide
    *
    */
    private function _slide (e:MouseEvent) : Void {
        var dx : Float = this.mouseX;

        var fn : Event->Void = function(e:Event) : Void {
            var newValue:Float = Math.min((this.mouseX / this._width) * this.max, this.max);
            if( newValue != this.value ){
                if( newValue < 0 ){
                    newValue = 0;
                }else if( newValue > this.max ){
                    newValue = max;
                }
                this.value = newValue;
                this.dispatchEvent(new WidgetEvent(WidgetEvent.CHANGE));
            }
        };

        var fnRelease : MouseEvent->Void = null;<commit_msg>Progress widget tweaks<commit_after>package ru.stablex.ui.widgets;

import nme.events.Event;
import nme.events.MouseEvent;
import nme.Lib;
import ru.stablex.ui.events.WidgetEvent;



/**
* Progress bar
*
*/
class Progress extends Widget{
    //Maximum value.
    public var max (default,set_max) : Float = 100;
    //current value
    @:isVar public var value (get_value,set_value) : Float = 0;
    private var _value : Float = 0;
    //bar
    public var bar : Widget;
    //Whether user can click/tap/slide progress bar to change value
    public var interactive (default,set_interactive) : Bool = false;
    //Visualize progress bar changes smoothly
    public var smoothChange : Bool;


    /**
    * Constructor
    *
    */
    public function new () : Void {
        super();
        this.bar = cast this.addChild(UIBuilder.create(Widget));
        this.bar.heightPt = 100;
    }//function new()


    /**
    * Set initial bar size on creation is complete.
    *
    */
    override public function onCreate () : Void {
        super.onCreate();
        this._setBarWidth(this.value, this.max);
    }//function onCreate()


    /**
    * Setter for `.max`
    *
    */
    private function set_max (m:Float) : Float {
        if( this.created ){
            this._setBarWidth(this.value, m);
        }
        return this.max = m;
    }//function set_max()


    /**
    * Setter for `.value`
    *
    */
    private function set_value (v:Float) : Float {
        this._setBarWidth(v, this.max);
        this._value = v;
        if( this.created ){
            this.dispatchEvent(new WidgetEvent(WidgetEvent.CHANGE));
        }
        return v;
    }//function set_value()


    /**
    * Getter for `.value`
    *
    */
    private function get_value () : Float {
        return this._value;
    }//function set_value()


    /**
    * Set bar width based on provided values
    *
    */
    private inline function _setBarWidth (value:Float, max:Float) : Void {
        if( !this.smoothChange ){
            this.bar.widthPt = 100 * (max <= 0 || value <= 0 ? 0 : value / max);
        }else{
            this.bar.tween(0.1, {widthPt: 100 * (max <= 0 || value <= 0 ? 0 : value / max)}, "Quad.easeIn");
        }
    }//function _setBarWidth()


    /**
    * Setter `interactive`.
    *
    */
    private function set_interactive (interactive:Bool) : Bool {
        if( interactive ){
            this.addUniqueListener(MouseEvent.MOUSE_DOWN, this._slide);
        }else{
            this.removeEventListener(MouseEvent.MOUSE_DOWN, this._slide);
        }
        return this.interactive = interactive;
    }//function set_interactive



    /**
    * Change value on click/tap/slide
    *
    */
    private function _slide (e:MouseEvent) : Void {
        var dx : Float = this.mouseX;

        var fn : Event->Void = function(e:Event) : Void {
            var newValue:Float = Math.min((this.mouseX / this._width) * this.max, this.max);
            if( newValue != this.value ){
                if( newValue < 0 ){
                    newValue = 0;
                }else if( newValue > this.max ){
                    newValue = max;
                }
                this.value = newValue;
            }
        };

        var fnRelease : MouseEvent->Void = null;
{'fraction_non_alphanumeric': 0.12480786965877651, 'fraction_numerical': 0.007377805102981863, 'mean_word_length': 1.8779301194161875, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7573452', 'n_tokens_mistral': 2046, 'n_tokens_neox': 1933, 'n_words': 494}