Files
sovereign_browser/src/shortcuts.h
T

139 lines
4.3 KiB
C

/*
* shortcuts.h — configurable keyboard shortcuts for sovereign_browser
*
* Browser-level keyboard shortcuts (new tab, close tab, focus URL, tab
* cycling, reload, back/forward, find, settings, fullscreen) are
* user-configurable. Bindings are stored in the SQLite key_value table
* as "shortcut.<action_id>" = GTK accelerator string (e.g. "<Control>t")
* and synced across devices via NIP-78 (kind 30078) by settings_sync.c.
*
* The on_key_press() handler in main.c calls shortcuts_lookup() on each
* key event and dispatches the returned action.
*/
#ifndef SHORTCUTS_H
#define SHORTCUTS_H
#include <glib.h>
#include <gtk/gtk.h>
/* Forward declaration for cJSON (used in serialize/merge functions). */
#include "../nostr_core_lib/cjson/cJSON.h"
#ifdef __cplusplus
extern "C" {
#endif
/* ── Action registry ────────────────────────────────────────────────── */
typedef enum {
SHORTCUT_NEW_TAB = 0,
SHORTCUT_NEW_WINDOW,
SHORTCUT_CLOSE_TAB,
SHORTCUT_FOCUS_URL,
SHORTCUT_NEXT_TAB,
SHORTCUT_PREV_TAB,
SHORTCUT_NEXT_TAB_PAGEDOWN,
SHORTCUT_PREV_TAB_PAGEUP,
SHORTCUT_RELOAD,
SHORTCUT_FORCE_RELOAD,
SHORTCUT_GO_BACK,
SHORTCUT_GO_FORWARD,
SHORTCUT_FIND,
SHORTCUT_OPEN_SETTINGS,
SHORTCUT_NEW_IDENTITY,
SHORTCUT_TOGGLE_FULLSCREEN,
SHORTCUT_TOGGLE_INSPECTOR,
SHORTCUT_TOGGLE_SIDEBAR,
SHORTCUT_ZOOM_IN,
SHORTCUT_ZOOM_OUT,
SHORTCUT_ZOOM_RESET,
SHORTCUT_COUNT
} shortcut_action_t;
/* Metadata for the settings UI. */
typedef struct {
const char *id; /* "new_tab" — used in db_kv key and JSON */
const char *label; /* "New tab" — shown in the settings page */
const char *desc; /* "Open a new tab" — short description */
const char *dflt; /* "<Control>t" — default GTK accelerator */
} shortcut_meta_t;
/* ── Public API ─────────────────────────────────────────────────────── */
/*
* Load all bindings from the SQLite key_value table into the in-memory
* array. Missing keys use the defaults from the registry.
* Call once at startup after db_init() and settings_load().
*/
void shortcuts_load(void);
/*
* Look up which action a key event matches, or -1 if none.
* Applies gtk_accelerator_get_default_mod_mask() to the event state
* and compares against the parsed bindings.
*
* If the master "shortcuts_enabled" setting is false, always returns -1.
*/
int shortcuts_lookup(GdkEventKey *event);
/*
* Get the current accelerator string for an action (e.g. for the
* settings UI or for Nostr sync serialization). Returns a pointer to
* an internal buffer valid until the next shortcuts_set() call.
*/
const char *shortcuts_get(shortcut_action_t action);
/*
* Set and persist a new binding for an action.
* accel_str — must be parseable by gtk_accelerator_parse()
* Returns 0 on success, -1 on parse failure.
* Updates the in-memory array immediately and writes to db_kv.
*/
int shortcuts_set(shortcut_action_t action, const char *accel_str);
/*
* Reset an action to its default binding (and persist to db_kv).
*/
void shortcuts_reset(shortcut_action_t action);
/*
* Reset all actions to their default bindings (and persist to db_kv).
*/
void shortcuts_reset_all(void);
/*
* Get the metadata (id, label, desc, default) for an action.
* Returns a pointer to a static struct.
*/
const shortcut_meta_t *shortcuts_meta(shortcut_action_t action);
/*
* Look up an action by its string id (e.g. "new_tab").
* Returns the action enum, or -1 if not found.
*/
int shortcuts_action_from_id(const char *id);
/*
* Serialize all current bindings to a JSON object suitable for NIP-78
* sync. Returns a newly allocated cJSON object (caller must delete).
* Format: {"new_tab":"<Control>t","close_tab":"<Control>w",...}
*/
cJSON *shortcuts_serialize(void);
/*
* Merge bindings from a parsed JSON object (as produced by
* shortcuts_serialize). For each key that matches a known action id,
* parse the accelerator and update the in-memory binding + db_kv.
* Unknown keys are silently ignored.
*
* json — a cJSON object mapping action id → accelerator string
*/
void shortcuts_merge_from_json(const cJSON *json);
#ifdef __cplusplus
}
#endif
#endif /* SHORTCUTS_H */