From be0d0f6bb175650eaf668c707b7562199f9c1647 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Tue, 27 May 2025 10:27:49 +0300 Subject: [PATCH] increase x axis mouse tooltip accuracy --- src/components/view_ruler.zig | 4 ++-- src/screens/main_screen.zig | 8 ++++---- src/utils.zig | 8 ++++++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/components/view_ruler.zig b/src/components/view_ruler.zig index 6dce824..643834a 100644 --- a/src/components/view_ruler.zig +++ b/src/components/view_ruler.zig @@ -176,7 +176,7 @@ fn formatAxisLabel(allocator: std.mem.Allocator, project: *App.Project, axis: UI const sample_rate = project.sample_rate; if (axis == .X and sample_rate != null) { const seconds = position / sample_rate.?; - return try utils.formatDuration(allocator, seconds); + return try utils.formatDuration(allocator, seconds, false); } else { return try std.fmt.allocPrint(allocator, "{d:.3}", .{ position }); } @@ -240,7 +240,7 @@ fn showMouseTooltip(ctx: Context, axis: UI.Axis, view_id: Id, position: f64) voi } else if (axis == .X and sample_rate != null) { const seconds = position / sample_rate.?; const frame_allocator = ui.frameAllocator(); - _ = ui.label("{s}", .{ utils.formatDuration(frame_allocator, seconds) catch "-" }); + _ = ui.label("{s}", .{ utils.formatDuration(frame_allocator, seconds, true) catch "-" }); } else { _ = ui.label("{d:.3}", .{position}); diff --git a/src/screens/main_screen.zig b/src/screens/main_screen.zig index f4b9c94..a659b92 100644 --- a/src/screens/main_screen.zig +++ b/src/screens/main_screen.zig @@ -754,7 +754,7 @@ fn showViewSettings(self: *MainScreen, view_id: Id) !void { var duration_str: ?[]const u8 = null; if (sample_rate != null) { const duration = @as(f64, @floatFromInt(sample_count)) / sample_rate.?; - if (utils.formatDuration(ui.frameAllocator(), duration)) |str| { + if (utils.formatDuration(ui.frameAllocator(), duration, false)) |str| { duration_str = str; } else |_| {} } @@ -1186,7 +1186,7 @@ fn showSolutions(self: *MainScreen) !void { if (sample_rate != null) { const seconds = @as(f64, @floatFromInt(solution.sample)) / sample_rate.?; - _ = ui.label("{s}", .{ try utils.formatDuration(ui.frameAllocator(), seconds) }); + _ = ui.label("{s}", .{ try utils.formatDuration(ui.frameAllocator(), seconds, false) }); } else { _ = ui.label("{d}", .{ solution.sample }); } @@ -1235,7 +1235,7 @@ fn showStatisticsPoints(self: *MainScreen) !void { if (sample_rate != null) { const seconds = @as(f64, @floatFromInt(sample_timestamp)) / sample_rate.?; - _ = ui.label("{s}", .{ try utils.formatDuration(ui.frameAllocator(), seconds) }); + _ = ui.label("{s}", .{ try utils.formatDuration(ui.frameAllocator(), seconds, false) }); } else { _ = ui.label("{d}", .{ sample_timestamp }); } @@ -1284,7 +1284,7 @@ fn showGainChanges(self: *MainScreen) !void { if (sample_rate != null) { const seconds = gain_change.sample / sample_rate.?; - _ = ui.label("{s}", .{ try utils.formatDuration(ui.frameAllocator(), seconds) }); + _ = ui.label("{s}", .{ try utils.formatDuration(ui.frameAllocator(), seconds, false) }); } else { _ = ui.label("{d}", .{ gain_change.sample }); } diff --git a/src/utils.zig b/src/utils.zig index a5b2352..7ca97cb 100644 --- a/src/utils.zig +++ b/src/utils.zig @@ -86,8 +86,12 @@ pub inline fn dumpErrorTrace() void { } } -pub fn formatDuration(allocator: std.mem.Allocator, total_seconds: f64) ![]u8 { +pub fn formatDuration(allocator: std.mem.Allocator, total_seconds: f64, high_accuracy: bool) ![]u8 { const seconds = @mod(total_seconds, @as(f64, @floatFromInt(std.time.s_per_min))); const minutes = @divFloor(total_seconds, std.time.s_per_min); - return try std.fmt.allocPrint(allocator, "{d:.0}m {d:.3}s", .{ minutes, seconds }); + if (high_accuracy) { + return try std.fmt.allocPrint(allocator, "{d:.0}m {d:.5}s", .{ minutes, seconds }); + } else { + return try std.fmt.allocPrint(allocator, "{d:.0}m {d:.3}s", .{ minutes, seconds }); + } } \ No newline at end of file