/*******************************************************************************
This file is part of the Shellfish UI toolkit.
Copyright (c) 2020 - 2023 Martin Grimme <martin.grimme@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*******************************************************************************/
require "shellfish/ui";
/**
* Element representing a UI value slider.
*
* `value` is a decimal number within the range of `minValue` and `maxValue`.
* Remember to round or truncate it if you need integer values.
*
* @memberof ui
* @name Slider
* @class
* @extends html.MouseBox
*
* @property {number} maxValue - (default: `100`) The maximum allowed value.
* @property {number} minValue - (default: `0`) The minimum allowed value.
* @property {bool} seeking - [readonly] `true` while the user is seeking.
* @property {number} seekValue - [readonly] The actual value while `seeking` is `true`. Otherwise the same as `value`.
* @property {number} stepSize - (default: `5`) The step size between value changes. The step size only applies to value changes with the PgUp/PgDown keyboard keys or the mouse wheel.
* @property {template} styleOfBackground - The background style template.
* @property {template} styleOfHandle - The handle style template.
* @property {number} value - (default: `0`) The current value. This value is not updated while `seeking` is `true`.
*/
MouseBox {
id: slider
property minValue: 0
property maxValue: 100
property value: 0
property seekValue: 0
property stepSize: 5
property seeking: false
property styleOfBackground: template Box {
fillWidth: true
fillHeight: true
layout: "center"
Box {
fillWidth: true
height: theme.paddingSmall / 2
borderWidth: 1
borderColor: theme.borderColor
borderRadius: 3
color: theme.secondaryColor
}
}
property styleOfHandle: template Box {
width: bboxHeight * 1.5
fillHeight: true
borderWidth: 1
borderColor: theme.borderColor
borderRadius: theme.borderRadius
color: pressed ? theme.highlightBackgroundColor
: containsMouse ? theme.hoverBackgroundColor
: theme.contentBackgroundColor
layout: "center"
Label {
color: parent.containsMouse || parent.pressed ? theme.highlightColor
: theme.secondaryColor;
text: "[icon:core-drag_indicator]"
}
}
/**
* Sets the current value.
*
* @memberof ui.Slider.prototype
* @name setValue
* @method
*
* @param {number} v - The new value.
*/
function setValue(v)
{
const nv = Math.max(minValue, Math.min(maxValue, v));
if (! isNaN(nv) && nv !== value)
{
value = nv;
}
seekValue = value;
}
canFocus: true
width: theme.itemWidthLarge
height: theme.itemHeightSmall
opacity: enabled && ancestorsEnabled ? 1 : 0.6
onMinValueChanged: () => { setValue(value); }
onMaxValueChanged: () => { setValue(value); }
onValueChanged: () => { setValue(value); }
onSeekingChanged: () =>
{
if (! seeking)
{
value = seekValue;
}
}
onWheel: ev =>
{
ev.accepted = true;
if (ev.deltaY > 0)
{
value = Math.max(minValue, seekValue - stepSize);
}
else if (ev.deltaY < 0)
{
value = Math.min(maxValue, seekValue + stepSize);
}
}
Loader {
fillWidth: true
fillHeight: true
sourceTemplate: styleOfBackground
}
MouseBox {
id: handle
property percents: slider.maxValue > slider.minValue ? Math.max(0, Math.min(1, (slider.seekValue - slider.minValue) / (slider.maxValue - slider.minValue)))
: 0
property animated: false
position: "free"
fillHeight: true
x: (slider.bboxWidth - bboxWidth) * percents
xTransition: NumberAnimation { enabled: handle.animated; duration: 300; easing: "InOutQuad" }
onPointerDown: (ev) =>
{
if (ev.buttons === 1)
{
slider.focus = true;
slider.seeking = true;
ev.accepted = true;
}
}
onDrag: (ev) =>
{
const sx = Math.max(0, Math.min(slider.bboxWidth - bboxWidth, x + ev.deltaX));
const p = sx / (slider.bboxWidth - bboxWidth);
const newValue = slider.minValue + (slider.maxValue - slider.minValue) * p;
slider.seekValue = newValue;
slider.focus = true;
ev.accepted = true;
}
onDragEnd: () =>
{
slider.seeking = false;
}
Loader {
fillHeight: true
sourceTemplate: slider.styleOfHandle
}
}
onPointerDown: (ev) =>
{
if (ev.buttons === 1)
{
ev.accepted = true;
handle.animated = true;
const x = Math.max(0, Math.min(bboxWidth, ev.x));
value = minValue + (maxValue - minValue) * (x / bboxWidth);
handle.animated = false;
focus = true;
}
}
onKeyDown: (ev) =>
{
if (ev.key === "ArrowLeft")
{
seeking = true;
seekValue = Math.max(minValue, seekValue - 1);
ev.accepted = true;
}
else if (ev.key === "ArrowRight")
{
seeking = true;
seekValue = Math.min(maxValue, seekValue + 1);
ev.accepted = true;
}
else if (ev.key === "PageUp")
{
seeking = true;
seekValue = Math.max(minValue, seekValue - stepSize);
ev.accepted = true;
}
else if (ev.key === "PageDown")
{
seeking = true;
seekValue = Math.min(maxValue, seekValue + stepSize);
ev.accepted = true;
}
else if (ev.key === "Home")
{
handle.animated = true;
value = minValue;
handle.animated = false;
ev.accepted = true;
}
else if (ev.key === "End")
{
handle.animated = true;
value = maxValue;
handle.animated = false;
ev.accepted = true;
}
}
onKeyUp: (ev) =>
{
if (seeking)
{
seeking = false;
}
}
FocusIndicator { visible: slider.focus }
}