Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions core/widgets/SelectableText.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ hold: *TouchHold,
style: SelectionControls.Style,

/// Starts the layout and reads this frame's touch events. Call `deinit` on the result, as for `dvui.textLayout`.
pub fn init(src: std.builtin.SourceLocation, init_opts: dvui.TextLayoutWidget.InitOptions, opts: dvui.Options) SelectableText {
pub fn init(src: std.lang.SourceLocation, init_opts: dvui.TextLayoutWidget.InitOptions, opts: dvui.Options) SelectableText {
return initStyled(src, init_opts, opts, .{});
}

/// `init`, with the selection menu drawn in `style`.
pub fn initStyled(src: std.builtin.SourceLocation, init_opts: dvui.TextLayoutWidget.InitOptions, opts: dvui.Options, style: SelectionControls.Style) SelectableText {
pub fn initStyled(src: std.lang.SourceLocation, init_opts: dvui.TextLayoutWidget.InitOptions, opts: dvui.Options, style: SelectionControls.Style) SelectableText {
var tl_opts = init_opts;
tl_opts.touch_edit_just_focused = false;
const unhandled = touch_press.unhandledPresses();
Expand Down Expand Up @@ -116,13 +116,7 @@ fn processTouch(self: SelectableText, unhandled: touch_press.Presses) void {
else => {},
}
}
// A press the layout did not take, or a drag it let go of, is not a hold.
if (hold.armed and !hold.fired and !dvui.captured(tl.data().id)) hold.cancel();

if (hold.due(dvui.frameTimeNS())) selectWordAt(tl, tl.data().id, hold.x, hold.y);
if (hold.remaining(dvui.frameTimeNS())) |ns| {
dvui.timer(tl.data().id, @intCast(@divTrunc(ns, std.time.ns_per_us) + 1));
}
_ = endHold(hold, tl, tl.data().id);
}

/// Selects the word under the physical point (`x`, `y`) of `tl` and shows its handles and menu.
Expand All @@ -137,6 +131,19 @@ pub fn selectWordAt(tl: *dvui.TextLayoutWidget, focus_id: dvui.Id, x: f32, y: f3
dvui.refresh(null, @src(), tl.data().id);
}

/// Call after a frame's touch events. Drops a hold whose press `tl` does not capture (a press it
/// did not take, or a drag it let go of); when the hold is due, selects the word under it with
/// `focus_id` focused and returns true. Asks dvui for a frame when the hold will be due.
pub fn endHold(hold: *TouchHold, tl: *dvui.TextLayoutWidget, focus_id: dvui.Id) bool {
if (hold.armed and !hold.fired and !dvui.captured(tl.data().id)) hold.cancel();
const fired = hold.due(dvui.frameTimeNS());
if (fired) selectWordAt(tl, focus_id, hold.x, hold.y);
if (hold.remaining(dvui.frameTimeNS())) |ns| {
dvui.timer(tl.data().id, @intCast(@divTrunc(ns, std.time.ns_per_us) + 1));
}
return fired;
}

fn clear(tl: *dvui.TextLayoutWidget) void {
tl.selection.start = 0;
tl.selection.end = 0;
Expand Down
9 changes: 1 addition & 8 deletions core/widgets/TextEntry.zig
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,7 @@ fn processTouch(self: TextEntry, unhandled: touch.Presses, touch_editing: bool)
else => {},
}
}
if (hold.armed and !hold.fired and !dvui.captured(tl.data().id)) hold.cancel();
if (hold.due(dvui.frameTimeNS())) {
SelectableText.selectWordAt(tl, self.entry.data().id, hold.x, hold.y);
placed = true;
}
if (hold.remaining(dvui.frameTimeNS())) |ns| {
dvui.timer(tl.data().id, @intCast(@divTrunc(ns, std.time.ns_per_us) + 1));
}
if (SelectableText.endHold(hold, tl, self.entry.data().id)) placed = true;
tl.state.te_first = false;
tl.state.te_focus_on_touchdown = false;
return placed;
Expand Down
Loading