Skip to content

Commit e8b853d

Browse files
committed
mark all on double click
1 parent 23a5297 commit e8b853d

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/main/java/com/cleanroommc/modularui/widgets/textfield/BaseTextFieldWidget.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cleanroommc.modularui.widgets.textfield;
22

3+
import com.cleanroommc.modularui.ModularUI;
34
import com.cleanroommc.modularui.ModularUIConfig;
45
import com.cleanroommc.modularui.api.ITheme;
56
import com.cleanroommc.modularui.api.widget.IFocusedWidget;
@@ -14,6 +15,7 @@
1415
import com.cleanroommc.modularui.widget.scroll.ScrollData;
1516
import com.cleanroommc.modularui.widgets.VoidWidget;
1617

18+
import net.minecraft.client.Minecraft;
1719
import net.minecraft.client.gui.GuiScreen;
1820

1921
import org.jetbrains.annotations.NotNull;
@@ -42,6 +44,7 @@ public class BaseTextFieldWidget<W extends BaseTextFieldWidget<W>> extends Abstr
4244
private static final Pattern BASE_PATTERN = Pattern.compile("[^§]");
4345

4446
private static final int CURSOR_BLINK_RATE = 10;
47+
private static final int DOUBLE_CLICK_THRESHOLD = 300; // max time between clicks to count as double-click in ms
4548

4649
protected TextFieldHandler handler = new TextFieldHandler(this);
4750
protected TextFieldRenderer renderer = new TextFieldRenderer(this.handler);
@@ -51,6 +54,7 @@ public class BaseTextFieldWidget<W extends BaseTextFieldWidget<W>> extends Abstr
5154
protected float scale = 1f;
5255
protected boolean focusOnGuiOpen;
5356
private int cursorTimer;
57+
protected long lastClickTime = 0;
5458

5559
protected Integer textColor;
5660
protected Integer markedColor;
@@ -171,7 +175,27 @@ public void onRemoveFocus(ModularGuiContext context) {
171175
// the current transformation does not include the transformation of the children (the scroll) so we need to manually transform here
172176
int x = getContext().getMouseX() + getScrollX();
173177
int y = getContext().getMouseY() + getScrollY();
178+
long now = Minecraft.getSystemTime();
179+
if (this.lastClickTime < 0) {
180+
// triple click
181+
if (now + this.lastClickTime < DOUBLE_CLICK_THRESHOLD) {
182+
this.handler.markAll();
183+
this.lastClickTime = 0;
184+
return Result.SUCCESS;
185+
}
186+
this.lastClickTime = 0;
187+
} else if (this.lastClickTime > 0) {
188+
// double click
189+
if (now - this.lastClickTime < DOUBLE_CLICK_THRESHOLD) {
190+
this.handler.markCurrentLine();
191+
this.lastClickTime = -Minecraft.getSystemTime();
192+
return Result.SUCCESS;
193+
}
194+
this.lastClickTime = 0;
195+
}
196+
// single click
174197
this.handler.setCursor(this.renderer.getCursorPos(this.handler.getText(), x, y), true);
198+
this.lastClickTime = Minecraft.getSystemTime();
175199
}
176200
return Result.SUCCESS;
177201
}

src/main/java/com/cleanroommc/modularui/widgets/textfield/TextFieldHandler.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ public void markAll() {
226226
setMainCursor(this.text.size() - 1, this.text.get(this.text.size() - 1).length(), true);
227227
}
228228

229+
public void markCurrentLine() {
230+
setOffsetCursor(getMainCursor().y, 0);
231+
setMainCursor(getMainCursor().y, this.text.get(getMainCursor().y).length(), true);
232+
}
233+
229234
public String getTextAsString() {
230235
return JOINER.join(this.text);
231236
}

0 commit comments

Comments
 (0)