// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-

const Lang = imports.lang;
const St = imports.gi.St;
const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Slider = imports.ui.slider;

let _button = null;

const MIN_VALUE = 0.50;
const MAX_VALUE = 3.00;

const TextScalerButton = new Lang.Class({
    Name: 'TextScalerButton',
    Extends: PanelMenu.Button,

    _init: function() {
        this.parent(0.0, "Text Scaler Button");
        this.setSensitive(true);

        // The actual text scaling factor, as a float
        this._currentValue = 1.00;

        // Panel menu icon
        this._icon = new St.Icon({ icon_name: 'zoom-in',
                                   style_class: 'system-status-icon' });
        this.actor.add_child(this._icon);

        // Popup Menu
        this._menu = new PopupMenu.PopupMenu(this.actor, 0.0, St.Side.BOTTOM);
        this.setMenu(this._menu);

        // Create the text entry and the slider
        this._menuItem = new PopupMenu.PopupBaseMenuItem({ activate: true });
        this._menu.addMenuItem(this._menuItem);

        this._entry = new St.Entry();
        this._entry.clutter_text.set_editable(false);
        this._menuItem.actor.add_child(this._entry);

        this._slider = new Slider.Slider(0.0);
        this._slider.connect('value-changed', Lang.bind(this, this._onSliderValueChanged));
        this._slider.connect('drag-end', Lang.bind(this, this._onSliderDragEnded));
        this._slider.actor.x_expand = true;
        this._menuItem.actor.add_actor(this._slider.actor);

        this._updateValue(this._currentValue);
        this._updateEntry();
        this._updateSlider();
    },

    _onSliderValueChanged: function(slider, value) {
        let newScalingFactor = value * (MAX_VALUE - MIN_VALUE) + MIN_VALUE;
        this._updateValue(newScalingFactor);
        this._updateEntry();
    },

    _onSliderDragEnded: function(slider) {
        // TODO: Actually change the scaling factor here
    },

    _updateValue: function(value) {
        // Need to keep the value between the valid limits
        this._currentValue = Math.max(MIN_VALUE, Math.min(value, MAX_VALUE));
    },

    _updateEntry: function() {
        // We only show 2 decimals on the text entry widget
        this._entry.set_text(this._currentValue.toFixed(2));
    },

    _updateSlider: function() {
        // Need to normalize the current value to the [0.0, 1.0] range
        let newSliderValue = (this._currentValue - MIN_VALUE) / (MAX_VALUE - MIN_VALUE);
        this._slider.setValue(newSliderValue);
    }
});

function init() {
}

function enable() {
    _button = new TextScalerButton();
    Main.panel.addToStatusArea('text-scaler-button', _button);
}

function disable() {
    _button.destroy();
}

@@ -1634,90 +1634,10 @@
 date
-Value(this._currentValue);%0A        this._updateEntry();%0A        this._updateSlider
+UI
 ();%0A
@@ -1819,37 +1819,16 @@
 ctor
-);%0A        this._updateEntry(
+, slider
 );%0A 
@@ -1970,27 +1970,102 @@
 nction(value
-) %7B
+, source=null) %7B%0A        if (this._currentValue == value)%0A            return;%0A
 %0A        // 
@@ -2189,16 +2189,259 @@
 VALUE));
+%0A%0A        this._updateUI(source);%0A    %7D,%0A%0A    _updateUI: function(source=null) %7B%0A        if (source != this._entry) %7B%0A            this._updateEntry();%0A        %7D%0A%0A        if (source != this._slider) %7B%0A            this._updateSlider();%0A        %7D
 %0A    %7D,%0A

Split _updateValue() into a _updateValue() and _updateUI()