1 module rpui.widgets.text_input.select_component;
2 
3 import std.algorithm.comparison;
4 import rpui.math;
5 import rpui.theme;
6 import rpui.widgets.text_input.widget;
7 import rpui.widgets.text_input.edit_component;
8 import rpui.primitives;
9 
10 struct SelectRegion {
11     int start = 0;
12     int end = 0;
13     vec2 size;
14     vec2 absolutePosition;
15 
16     bool startedSelection = false;
17     int startSelectionPos = 0;
18     EditComponent* editComponent;
19 
20     void attach(EditComponent* editComponent) {
21         this.editComponent = editComponent;
22     }
23 
24     void updateSelect(in int pos) {
25         if (!startedSelection)
26             return;
27 
28         start = startSelectionPos;
29         end = pos;
30 
31         clampSelectRegion();
32     }
33 
34     void clampSelectRegion() {
35         const regionMin = min(start, end);
36         const regionMax = max(start, end);
37 
38         start = regionMin;
39         end = regionMax;
40 
41         start = max(0, start);
42         end = min(end, cast(int) editComponent.text.length);
43     }
44 
45     void startSelection(in int pos) {
46         startedSelection = true;
47         startSelectionPos = pos;
48         start = startSelectionPos;
49         end = startSelectionPos;
50     }
51 
52     void stopSelection() {
53         startedSelection = false;
54         start = 0;
55         end = 0;
56         startSelectionPos = 0;
57     }
58 
59     bool textIsSelected() {
60         return start != end;
61     }
62 }