aboutsummaryrefslogtreecommitdiff
path: root/src/components/Scrollbar.cpp
diff options
context:
space:
mode:
authorLoic Guegan <manzerbredes@mailbox.org>2023-06-05 13:48:29 +0200
committerLoic Guegan <manzerbredes@mailbox.org>2023-06-05 13:48:29 +0200
commit2a4a20beefa533730d85a75bbc2dc6676fa247b5 (patch)
tree987bea897030e3f5a6114f30acccaef56bb01768 /src/components/Scrollbar.cpp
parent298714449a7c19c7a0f85d2f06305432f8a5251c (diff)
Introduce move based focusing
Diffstat (limited to 'src/components/Scrollbar.cpp')
-rw-r--r--src/components/Scrollbar.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/components/Scrollbar.cpp b/src/components/Scrollbar.cpp
index 76c5c1a..ac713e9 100644
--- a/src/components/Scrollbar.cpp
+++ b/src/components/Scrollbar.cpp
@@ -109,4 +109,40 @@ void Scrollbar::Refresh() {
elements.push_back(bar);
}
+void Scrollbar::Focus(double XorY){
+ double MTCanvasHeight = status->CanvasHeight - status->ScrollbarWidth;
+ double MTCanvasWidth = status->CanvasWidth - status->ScrollbarWidth;
+ // Check horizontal or vertical and if should scroll
+ if(IsHorizontal && status->MoveTableMaxX > MTCanvasWidth){
+ // First normalize:
+ XorY-=MTCanvasWidth; // Do not scroll on first page
+ XorY=std::max(0.0,XorY); // Ensure focus is greater than 0
+ XorY=std::min(XorY,(status->MoveTableMaxX-MTCanvasWidth)); // Ensure focus is than scroll max
+ // Then compute percent and apply scroll
+ double percent = XorY / (status->MoveTableMaxX-MTCanvasWidth);
+ status->ScrollX =
+ -(status->MoveTableMaxX - MTCanvasWidth) * percent;
+ // Do not forget to update scrollbar:
+ double maxScroll = bg.width - bar.width;
+ bar.x = maxScroll * percent;
+ bar.x = std::max(bg.x, bar.x);
+ bar.x = std::min(bg.x + maxScroll, bar.x);
+ }
+ else if(status->MoveTableMaxY > MTCanvasHeight){
+ // First normalize:
+ XorY-=MTCanvasHeight; // Do not scroll on first page
+ XorY=std::max(0.0,XorY); // Ensure focus is greater than 0
+ XorY=std::min(XorY,(status->MoveTableMaxY-MTCanvasHeight)); // Ensure focus is than scroll max
+ // Then compute percent and apply scroll
+ double percent = XorY / (status->MoveTableMaxY-MTCanvasHeight);
+ status->ScrollY =
+ -(status->MoveTableMaxY - MTCanvasHeight) * percent;
+ // Do not forget to update scrollbar:
+ double maxScroll = bg.height - bar.height;
+ bar.y = maxScroll * percent;
+ bar.y = std::max(bg.y, bar.y);
+ bar.y = std::min(bg.y + maxScroll, bar.y);
+ }
+}
+
} // namespace cgeditor \ No newline at end of file