ResizablePanel.hx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package hide.comp;
  2. enum LayoutDirection {
  3. Horizontal;
  4. Vertical;
  5. }
  6. class ResizablePanel extends hide.comp.Component {
  7. var layoutDirection : LayoutDirection;
  8. public function new(layoutDirection : LayoutDirection, element : Element) {
  9. super(null, element);
  10. this.layoutDirection = layoutDirection;
  11. var splitter = new Element('<div class="splitter"><div class="drag-handle"></div></div>');
  12. switch (layoutDirection) {
  13. case Horizontal:
  14. splitter.addClass("horizontal");
  15. case Vertical:
  16. splitter.addClass("vertical");
  17. }
  18. splitter.insertBefore(element);
  19. var handle = splitter.find(".drag-handle").first();
  20. var drag = false;
  21. var startSize = 0;
  22. var startPos = 0;
  23. handle.mousedown((e) -> {
  24. drag = true;
  25. startSize = Std.int(layoutDirection == Horizontal? element.width() : element.height());
  26. startPos = layoutDirection == Horizontal? e.clientX : e.clientY;
  27. });
  28. handle.mouseup((e) -> drag = false);
  29. handle.dblclick((e) -> {
  30. setSize(Std.parseInt(element.css("min-width")));
  31. });
  32. var scenePartition = element.parent();
  33. scenePartition.mousemove((e) -> {
  34. if (drag){
  35. setSize(startSize - ((layoutDirection == Horizontal? e.clientX : e.clientY) - startPos));
  36. }
  37. });
  38. scenePartition.mouseup((e) -> {
  39. drag = false;
  40. });
  41. scenePartition.mouseleave((e) -> {
  42. drag = false;
  43. });
  44. }
  45. public function setSize(?newSize : Int) {
  46. var minSize = (layoutDirection == Horizontal? Std.parseInt(element.css("min-width")) : Std.parseInt(element.css("min-height")));
  47. var maxSize = (layoutDirection == Horizontal? Std.parseInt(element.css("max-width")) : Std.parseInt(element.css("max-height")));
  48. var clampedSize = 0;
  49. if (newSize != null) clampedSize = hxd.Math.iclamp(newSize, minSize, maxSize);
  50. else clampedSize = hxd.Math.iclamp(getDisplayState("size"), minSize, maxSize);
  51. switch (layoutDirection) {
  52. case Horizontal :
  53. element.width(clampedSize);
  54. case Vertical :
  55. element.height(clampedSize);
  56. }
  57. if (newSize != null) saveDisplayState("size", clampedSize);
  58. onResize(); //@:privateAccess if( scene.window != null) scene.window.checkResize();
  59. }
  60. public dynamic function onResize() {}
  61. }