Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

132
LINES

< > BotCompany Repo | #1033471 // WindowResizeDragger - resize undecorated JFrame with mouse

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5239L/29K).

1  
// This class uses the border of the component that caught
2  
// the mouse event to determine the drag direction.
3  
4  
sclass WindowResizeDragger extends MouseAdapter {
5  
  int cornerSize = 10; // how big do we assume the corners are
6  
  bool debug;
7  
  Pt minWindowSize = pt(20, 20);
8  
  
9  
  MouseEvent mouseDownEvent; // if != null, we're engaged
10  
  JComponent src;
11  
  Window window;
12  
  Rect originalPosition, srcBounds;
13  
  int dragX, dragY; // -1, 0 or 1 depending on what we are dragging
14  
  Cursor originalCursor, ourCursor;
15  
  
16  
  *(JComponent *src) {
17  
    addMouseAndMotionListener(src, this);
18  
  }
19  
  
20  
  bool engaged() { ret mouseDownEvent != null; } 
21  
  
22  
  void start(MouseEvent e) {
23  
    this.mouseDownEvent = e;
24  
    window = getWindow(src);
25  
    if (window == null) ret;
26  
    originalPosition = toRect(getBounds(window));
27  
    cursorCalc(e, true);
28  
  }
29  
  
30  
  void cursorCalc(MouseEvent e, bool inside) {
31  
    srcBounds = toRect(boundsOnScreen(src));
32  
    Pt mouse = pt(e.getXOnScreen(), e.getYOnScreen());
33  
    
34  
    if (distance(mouse.y, srcBounds.y2()) <= cornerSize)
35  
      dragY = 1;
36  
    else if (distance(mouse.y, srcBounds.y1()) <= cornerSize)
37  
      dragY = -1;
38  
    else
39  
      dragY = 0;
40  
    
41  
    if (distance(mouse.x, srcBounds.x2()) <= cornerSize)
42  
      dragX = 1;
43  
    else if (distance(mouse.x, srcBounds.x1()) <= cornerSize)
44  
      dragX = -1;
45  
    else
46  
      dragX = 0;
47  
      
48  
    originalCursor = src.getCursor();
49  
    int iCursor = dragY*3+dragX+4;
50  
    int cursor = new int[] {
51  
      Cursor.NW_RESIZE_CURSOR,
52  
      Cursor.N_RESIZE_CURSOR,
53  
      Cursor.NE_RESIZE_CURSOR,
54  
      Cursor.W_RESIZE_CURSOR,
55  
      -1,
56  
      Cursor.E_RESIZE_CURSOR,
57  
      Cursor.SW_RESIZE_CURSOR,
58  
      Cursor.S_RESIZE_CURSOR,
59  
      Cursor.SE_RESIZE_CURSOR,
60  
    }[iCursor];
61  
    Cursor newCursor = cursor >= 0 && inside ? Cursor.getPredefinedCursor(cursor) : null;
62  
    if (debug) printVars(iCursor, +ourCursor, +newCursor);
63  
    if (newCursor != ourCursor) {
64  
      ourCursor = newCursor;
65  
      src.setCursor(newCursor);
66  
    }
67  
      
68  
    /*if (debug)
69  
      printVars("WindowResizeDragger", +dragX, +dragY, +mouse, +srcBounds, +cornerSize);*/
70  
  }
71  
  
72  
  @Override public void mouseEntered(MouseEvent e) {
73  
    if (debug) print("mouseEntered");
74  
    if (!engaged())
75  
      cursorCalc(e, true);
76  
  }
77  
78  
  @Override public void mouseExited(MouseEvent e) {
79  
    if (debug) print("mouseExited");
80  
    if (!engaged())
81  
      cursorCalc(e, false);
82  
  }
83  
84  
  public void mousePressed(MouseEvent e) {
85  
    if (e.getButton() != MouseEvent.BUTTON1) ret;
86  
    //if (window cast JFrame && isMaximized(window)) ret;
87  
    if (engaged()) ret; // what?
88  
    start(e);
89  
  }
90  
  
91  
  public void mouseDragged(MouseEvent e) {
92  
    if (!engaged()) ret;
93  
    updatePosition(e);
94  
  }
95  
  
96  
  public void mouseMoved(MouseEvent e) {
97  
    cursorCalc(e, true);
98  
  }
99  
  
100  
  public void mouseReleased(MouseEvent e) {
101  
    if (e.getButton() != mouseDownEvent.getButton()) ret;
102  
    updatePosition(e);
103  
    if (debug) print("drag done");
104  
    if (src.getCursor() == ourCursor)
105  
      src.setCursor(originalCursor);
106  
    mouseDownEvent = null;
107  
    dragDone();
108  
  }
109  
  
110  
  void updatePosition(MouseEvent e) {
111  
    //if (debug) printVars("updatePosition", +e);
112  
    Rect w = toRect(window.getBounds());
113  
    Pt a = topLeftCorner(w), b = bottomRightCorner(w);
114  
    Pt mouse = pt(e.getXOnScreen(), e.getYOnScreen());
115  
    
116  
    if (dragX < 0)
117  
      a.x = min(b.x-minWindowSize.x, mouse.x + originalPosition.x - mouseDownEvent.getXOnScreen());
118  
    else if (dragX > 0)
119  
      b.x = max(a.x+minWindowSize.x, mouse.x + originalPosition.x2() - mouseDownEvent.getXOnScreen());
120  
      
121  
    if (dragY < 0)
122  
      a.y = min(b.y-minWindowSize.y, mouse.y + originalPosition.y - mouseDownEvent.getYOnScreen());
123  
    else if (dragY > 0)
124  
      b.y = max(a.y+minWindowSize.y, mouse.y + originalPosition.y2() - mouseDownEvent.getYOnScreen());
125  
      
126  
    Rect r = rectFromPoints(a, b);
127  
    if (debug) printVars("updatePosition", +w, +mouse, +a, +b, +dragX, +dragY);
128  
    window.setBounds(toRectangle(r));
129  
  }
130  
  
131  
  void dragDone {}
132  
}

Author comment

Began life as a copy of #1033463

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1033471
Snippet name: WindowResizeDragger - resize undecorated JFrame with mouse
Eternal ID of this version: #1033471/40
Text MD5: 825aa2b2c71568086841e736b46b814f
Transpilation MD5: 0367df3596c3ec64fe5fc769c2ef6ac8
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-05 07:03:14
Source code size: 4109 bytes / 132 lines
Pitched / IR pitched: No / No
Views / Downloads: 226 / 357
Version history: 39 change(s)
Referenced in: [show references]