custom shader animation can be set to "always" to always remain active

Fixes #1225

The `custom-shader-animation` configuration can now be set to "always"
which keeps animation active even if the terminal is unfocused.
This commit is contained in:
Mitchell Hashimoto
2024-01-09 09:21:15 -08:00
parent 92697bad12
commit 96d33fef20
7 changed files with 88 additions and 27 deletions

View File

@@ -155,7 +155,6 @@ pub const DerivedConfig = struct {
invert_selection_fg_bg: bool,
min_contrast: f32,
custom_shaders: std.ArrayListUnmanaged([:0]const u8),
custom_shader_animation: bool,
links: link.Set,
pub fn init(
@@ -218,7 +217,6 @@ pub const DerivedConfig = struct {
null,
.custom_shaders = custom_shaders,
.custom_shader_animation = config.@"custom-shader-animation",
.links = links,
.arena = arena,
@@ -488,8 +486,7 @@ pub fn threadExit(self: *const Metal) void {
/// True if our renderer has animations so that a higher frequency
/// timer is used.
pub fn hasAnimations(self: *const Metal) bool {
return self.custom_shader_state != null and
self.config.custom_shader_animation;
return self.custom_shader_state != null;
}
/// Returns the grid size for a given screen size. This is safe to call

View File

@@ -252,7 +252,6 @@ pub const DerivedConfig = struct {
invert_selection_fg_bg: bool,
min_contrast: f32,
custom_shaders: std.ArrayListUnmanaged([:0]const u8),
custom_shader_animation: bool,
links: link.Set,
pub fn init(
@@ -315,7 +314,6 @@ pub const DerivedConfig = struct {
null,
.custom_shaders = custom_shaders,
.custom_shader_animation = config.@"custom-shader-animation",
.links = links,
.arena = arena,
@@ -558,7 +556,7 @@ pub fn threadExit(self: *const OpenGL) void {
/// timer is used.
pub fn hasAnimations(self: *const OpenGL) bool {
const state = self.gl_state orelse return false;
return state.custom != null and self.config.custom_shader_animation;
return state.custom != null;
}
/// Callback when the focus changes for the terminal this is rendering.

View File

@@ -7,6 +7,7 @@ const builtin = @import("builtin");
const xev = @import("xev");
const renderer = @import("../renderer.zig");
const apprt = @import("../apprt.zig");
const configpkg = @import("../config.zig");
const BlockingQueue = @import("../blocking_queue.zig").BlockingQueue;
const tracy = @import("tracy");
const trace = tracy.trace;
@@ -72,6 +73,9 @@ mailbox: *Mailbox,
/// Mailbox to send messages to the app thread
app_mailbox: App.Mailbox,
/// Configuration we need derived from the main config.
config: DerivedConfig,
flags: packed struct {
/// This is true when a blinking cursor should be visible and false
/// when it should not be visible. This is toggled on a timer by the
@@ -82,11 +86,22 @@ flags: packed struct {
has_inspector: bool = false,
} = .{},
pub const DerivedConfig = struct {
custom_shader_animation: configpkg.CustomShaderAnimation,
pub fn init(config: *const configpkg.Config) DerivedConfig {
return .{
.custom_shader_animation = config.@"custom-shader-animation",
};
}
};
/// Initialize the thread. This does not START the thread. This only sets
/// up all the internal state necessary prior to starting the thread. It
/// is up to the caller to start the thread with the threadMain entrypoint.
pub fn init(
alloc: Allocator,
config: *const configpkg.Config,
surface: *apprt.Surface,
renderer_impl: *renderer.Renderer,
state: *renderer.State,
@@ -122,6 +137,7 @@ pub fn init(
return Thread{
.alloc = alloc,
.config = DerivedConfig.init(config),
.loop = loop,
.wakeup = wakeup_h,
.stop = stop_h,
@@ -199,6 +215,7 @@ fn startDrawTimer(self: *Thread) void {
// If our renderer doesn't suppoort animations then we never run this.
if (!@hasDecl(renderer.Renderer, "hasAnimations")) return;
if (!self.renderer.hasAnimations()) return;
if (self.config.custom_shader_animation == .false) return;
// Set our active state so it knows we're running. We set this before
// even checking the active state in case we have a pending shutdown.
@@ -236,8 +253,10 @@ fn drainMailbox(self: *Thread) !void {
try self.renderer.setFocus(v);
if (!v) {
// Stop the draw timer
self.stopDrawTimer();
if (self.config.custom_shader_animation != .always) {
// Stop the draw timer
self.stopDrawTimer();
}
// If we're not focused, then we stop the cursor blink
if (self.cursor_c.state() == .active and
@@ -308,8 +327,9 @@ fn drainMailbox(self: *Thread) !void {
},
.change_config => |config| {
defer config.alloc.destroy(config.ptr);
try self.renderer.changeConfig(config.ptr);
defer message.deinit();
try self.changeConfig(config.thread);
try self.renderer.changeConfig(config.impl);
// Stop and start the draw timer to capture the new
// hasAnimations value.
@@ -322,6 +342,10 @@ fn drainMailbox(self: *Thread) !void {
}
}
fn changeConfig(self: *Thread, config: *const DerivedConfig) !void {
self.config = config.*;
}
fn wakeupCallback(
self_: ?*Thread,
_: *xev.Loop,

View File

@@ -1,6 +1,7 @@
const std = @import("std");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const configpkg = @import("../config.zig");
const font = @import("../font/main.zig");
const renderer = @import("../renderer.zig");
const terminal = @import("../terminal/main.zig");
@@ -45,9 +46,42 @@ pub const Message = union(enum) {
/// The derived configuration to update the renderer with.
change_config: struct {
alloc: Allocator,
ptr: *renderer.Renderer.DerivedConfig,
thread: *renderer.Thread.DerivedConfig,
impl: *renderer.Renderer.DerivedConfig,
},
/// Activate or deactivate the inspector.
inspector: bool,
/// Initialize a change_config message.
pub fn initChangeConfig(alloc: Allocator, config: *const configpkg.Config) !Message {
const thread_ptr = try alloc.create(renderer.Thread.DerivedConfig);
errdefer alloc.destroy(thread_ptr);
const config_ptr = try alloc.create(renderer.Renderer.DerivedConfig);
errdefer alloc.destroy(config_ptr);
thread_ptr.* = renderer.Thread.DerivedConfig.init(config);
config_ptr.* = try renderer.Renderer.DerivedConfig.init(alloc, config);
errdefer config_ptr.deinit();
return .{
.change_config = .{
.alloc = alloc,
.thread = thread_ptr,
.impl = config_ptr,
},
};
}
pub fn deinit(self: *const Message) void {
switch (self.*) {
.change_config => |v| {
v.impl.deinit();
v.alloc.destroy(v.impl);
v.alloc.destroy(v.thread);
},
else => {},
}
}
};