apprt/gtk: stylistic changes

This commit is contained in:
Mitchell Hashimoto
2024-01-01 08:34:19 -08:00
parent d235554606
commit 732063375a
3 changed files with 27 additions and 31 deletions

View File

@ -57,7 +57,7 @@ clipboard_confirmation_window: ?*ClipboardConfirmationWindow = null,
running: bool = true,
/// Xkb state (X11 only). Will be null on Wayland.
x11_xkb: ?x11.X11Xkb = null,
x11_xkb: ?x11.Xkb = null,
pub fn init(core_app: *CoreApp, opts: Options) !App {
_ = opts;
@ -173,9 +173,13 @@ pub fn init(core_app: *CoreApp, opts: Options) !App {
return error.GtkApplicationRegisterFailed;
}
var x11_xkb: ?x11.X11Xkb = null;
// Perform all X11 initialization. This ultimately returns the X11
// keyboard state but the block does more than that (i.e. setting up
// WM_CLASS).
const x11_xkb: ?x11.Xkb = x11_xkb: {
const display = c.gdk_display_get_default();
if (x11.is_display(display)) {
if (!x11.is_display(display)) break :x11_xkb null;
// Set the X11 window class property (WM_CLASS) if are are on an X11
// display.
//
@ -203,8 +207,8 @@ pub fn init(core_app: *CoreApp, opts: Options) !App {
c.gdk_x11_display_set_program_class(display, app_id);
// Set up Xkb
x11_xkb = try x11.X11Xkb.init(c.gdk_display_get_default());
}
break :x11_xkb try x11.Xkb.init(display);
};
// This just calls the "activate" signal but its part of the normal
// startup routine so we just call it:
@ -586,5 +590,6 @@ test "isValidAppId" {
/// Loads keyboard state from Xkb if there is an event pending and Xkb is
/// loaded (X11 only). Returns null otherwise.
pub fn modifier_state_from_xkb(self: *App, display_: ?*c.GdkDisplay) ?input.Mods {
return (self.x11_xkb orelse return null).modifier_state_from_notify(display_);
const x11_xkb = self.x11_xkb orelse return null;
return x11_xkb.modifier_state_from_notify(display_);
}

View File

@ -1333,22 +1333,15 @@ fn keyEvent(
// was presssed (i.e. left control)
const mods = mods: {
_ = gtk_mods;
const device = c.gdk_event_get_device(event);
var mods = init_mods: {
// Add any modifier state events from Xkb if we have them (X11 only).
if (self.app.modifier_state_from_xkb(display)) |xkb_mods| {
break :init_mods xkb_mods;
}
// Add any modifier state events from Xkb if we have them (X11 only).
// Null back from the Xkb call means there was no modifier
// event to read. This likely means that the key event did not
// result in a modifier change and we can safely rely on the
// GDK state.
break :init_mods translateMods(c.gdk_device_get_modifier_state(device));
};
var mods = self.app.modifier_state_from_xkb(display) orelse
translateMods(c.gdk_device_get_modifier_state(device));
mods.num_lock = c.gdk_device_get_num_lock_state(device) == 1;
switch (physical_key) {

View File

@ -13,13 +13,13 @@ pub fn is_display(display: ?*c.GdkDisplay) bool {
) != 0;
}
pub const X11Xkb = struct {
pub const Xkb = struct {
base_event_code: c_int,
funcs: Funcs,
/// Initialize an X11Xkb struct, for the given GDK display. If the display
/// Initialize an Xkb struct, for the given GDK display. If the display
/// isn't backed by X then this will return null.
pub fn init(display_: ?*c.GdkDisplay) !?X11Xkb {
pub fn init(display_: ?*c.GdkDisplay) !?Xkb {
// Display should never be null but we just treat that as a non-X11
// display so that the caller can just ignore it and not unwrap it.
const display = display_ orelse return null;
@ -27,14 +27,14 @@ pub const X11Xkb = struct {
// If the display isn't X11, then we don't need to do anything.
if (!is_display(display)) return null;
log.debug("X11Xkb.init: initializing Xkb", .{});
log.debug("Xkb.init: initializing Xkb", .{});
const xdisplay = c.gdk_x11_display_get_xdisplay(display);
var result: X11Xkb = .{
var result: Xkb = .{
.base_event_code = 0,
.funcs = try Funcs.init(),
};
log.debug("X11Xkb.init: running XkbQueryExtension", .{});
log.debug("Xkb.init: running XkbQueryExtension", .{});
var opcode: c_int = 0;
var base_error_code: c_int = 0;
var major = c.XkbMajorVersion;
@ -51,7 +51,7 @@ pub const X11Xkb = struct {
return error.XkbInitializationError;
}
log.debug("X11Xkb.init: running XkbSelectEventDetails", .{});
log.debug("Xkb.init: running XkbSelectEventDetails", .{});
if (result.funcs.XkbSelectEventDetails(
xdisplay,
c.XkbUseCoreKbd,
@ -76,7 +76,7 @@ pub const X11Xkb = struct {
/// Returns null if there is no event. In this case, the caller should fall
/// back to the standard GDK modifier state (this likely means the key
/// event did not result in a modifier change).
pub fn modifier_state_from_notify(self: X11Xkb, display_: ?*c.GdkDisplay) ?input.Mods {
pub fn modifier_state_from_notify(self: Xkb, display_: ?*c.GdkDisplay) ?input.Mods {
const display = display_ orelse return null;
// Shoutout to Mozilla for figuring out a clean way to do this, this is
@ -114,8 +114,6 @@ const Funcs = struct {
XEventsQueued: XEventsQueuedType,
XPeekEvent: XPeekEventType,
// X11 Function types. We load these dynamically at runtime to avoid having to
// link against X11.
const XkbQueryExtensionType = *const fn (?*c.struct__XDisplay, [*c]c_int, [*c]c_int, [*c]c_int, [*c]c_int, [*c]c_int) callconv(.C) c_int;
const XkbSelectEventDetailsType = *const fn (?*c.struct__XDisplay, c_uint, c_uint, c_ulong, c_ulong) callconv(.C) c_int;
const XEventsQueuedType = *const fn (?*c.struct__XDisplay, c_int) callconv(.C) c_int;