/*******************************************************************************
This file is part of the Shellfish UI toolkit.
Copyright (c) 2021 - 2024 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 decoration for overflowing boxes, which allows to
* scroll the box. This is useful for toolbars or side panels.
*
* @example
* // a toolbar
* Box {
* fillWidth: true
* layout: "center-row"
*
* Button {
* icon: "ui-cut"
* }
*
* Button {
* icon: "ui-copy"
* }
*
* Button {
* icon: "ui-paste"
* }
*
* OverflowScroller { }
* }
*
* @memberof ui
* @name OverflowScroller
* @class
* @extends html.Box
*
* @property {number} stepSize - (default: `theme.paddingMedium`) The step size for scrolling.
* @property {template} styleOfHandle - The handle style template. It is expected to have a property `edge` that will be set to the particular edge (one of `top|bottom|left|right`).
* @property {html.Box} target - (default: `parent`) The target box.
*/
Box {
id: box
property stepSize: theme.paddingMedium
property target: parent
property styleOfHandle: template MouseBox {
property edge: ""
width: fillWidth ? -1 : theme.paddingLarge
height: fillHeight ? -1 : theme.paddingLarge
fillWidth: edge === "top" || edge === "bottom"
fillHeight: edge === "left" || edge === "right"
layout: "center"
color: pressed ? theme.highlightBackgroundColor
: containsMouse ? theme.hoverBackgroundColor
: "transparent"
// what a hack, but well...
//onDrag: (ev) => { ev.accepted = true; }
Label {
visible: parent.containsMouse
text: (() =>
{
switch (parent.edge)
{
case "top":
return "[icon:core-arrow_up]";
case "bottom":
return "[icon:core-arrow_down]";
case "left":
return "[icon:core-arrow_left]";
case "right":
return "[icon:core-arrow_right]";
}
})()
}
Timer {
running: parent.containsMouse
repeat: true
interval: 50
onTimeout: () =>
{
box.scrollEdge(parent.edge);
}
}
}
function scroll(axis, amount)
{
if (axis === "x")
{
if (amount < 0)
{
target.contentX = Math.max(0, target.contentX + amount);
}
else
{
const maxX = target.contentWidth - target.bboxWidth;
target.contentX = Math.min(maxX, target.contentX + amount);
}
}
else
{
if (amount < 0)
{
target.contentY = Math.max(0, target.contentY + amount);
}
else
{
const maxY = target.contentHeight - target.bboxHeight;
target.contentY = Math.min(maxY, target.contentY + amount);
}
}
}
function scrollEdge(edge)
{
switch (edge)
{
case "top":
scroll("y", -box.stepSize);
break;
case "bottom":
scroll("y", box.stepSize);
break;
case "left":
scroll("x", -box.stepSize);
break;
case "right":
scroll("x", box.stepSize);
break;
}
}
visible: thisDocument.inputDevice !== "touch" &&
(target.contentWidth > target.bboxWidth ||
target.contentHeight > target.bboxHeight)
position: "global"
x: parent.bboxX
y: parent.bboxY
width: parent.bboxWidth
height: parent.bboxHeight
onInitialization: () =>
{
css("pointer-events", "none");
}
onVisibleChanged: () =>
{
if (visible)
{
parent.enablePositionTracking();
}
}
Repeater {
model: ListModel {
data: [
"top", "bottom", "left", "right"
]
}
delegate: template Box {
property edge: modelData.value
visible: (() =>
{
switch (edge)
{
case "top":
return target.contentY > 0;
case "bottom":
return target.bboxHeight < target.contentHeight &&
target.contentY + 0.5 < target.contentHeight - target.bboxHeight;
case "left":
return target.contentX > 0;
case "right":
return target.bboxWidth < target.contentWidth &&
target.contentX + 0.5 < target.contentWidth - target.bboxWidth;
}
})()
position: "free"
origin: (() =>
{
switch (edge)
{
case "top":
return "top-left";
case "bottom":
return "bottom-left";
case "left":
return "top-left";
case "right":
return "top-right";
}
})()
fillWidth: edge === "top" || edge === "bottom"
fillHeight: edge === "left" || edge === "right"
Loader {
fillWidth: parent.fillWidth
fillHeight: parent.fillHeight
sourceTemplate: box.styleOfHandle
layout: "center"
onItemChanged: () =>
{
if (item)
{
item.edge = parent.edge;
item.css("pointer-events", "all");
}
}
}
}
}
//Label { text: target.contentX + ", " + target.contentY + " @ " + target.contentWidth + " x " + target.contentHeight }
}