feat: editable hotkeys + fix console window on installed app

- Added HotkeyConfig with validation and dynamic registration
- Added set_hotkeys Tauri command for runtime hotkey updates
- Settings UI now has 4 editable hotkey inputs (Clip 60s, Clip 30s, Toggle Recording, Screenshot)
- Fixed console window issue: SUBSYSTEM:WINDOWS only for release builds
This commit is contained in:
Alvin-Zilverstand committed 2026-09-11 14:07:57 +02:00
1 parent fb4f19cea8
commit 40f622199e
5 files changed
+358 -68

No files matched your search

+5
View File
@@ -13,3 +13,8 @@ clipforge = { path = ".." }
serde = { version = "1", features = ["derive"] }
tauri = { version = "2.9.5", features = [] }
tauri-plugin-global-shortcut = "2"
[profile.release]
lto = "thin"
codegen-units = 1
strip = true
+6
View File
@@ -1,3 +1,9 @@
fn main() {
tauri_build::build();
#[cfg(all(target_os = "windows", not(debug_assertions)))]
{
// Set the subsystem to windows to avoid console window (release builds only)
println!("cargo:rustc-link-arg=/SUBSYSTEM:WINDOWS,5.01");
}
}
+198 -44
View File
@@ -47,6 +47,14 @@ struct AppRuntime {
capture: Mutex<Option<ActiveCapture>>,
league_poller: Mutex<LeagueLiveClientPoller>,
valve_events: Mutex<Vec<RawGameEvent>>,
hotkey_shortcuts: Mutex<Option<HotkeyShortcuts>>,
app_handle: Mutex<Option<tauri::AppHandle>>,
}
struct HotkeyShortcuts {
clip_60: tauri_plugin_global_shortcut::Shortcut,
clip_30: tauri_plugin_global_shortcut::Shortcut,
toggle_recording: tauri_plugin_global_shortcut::Shortcut,
}
struct ActiveCapture {
@@ -105,6 +113,10 @@ struct DesktopStatus {
system_audio_available: bool,
desktop_duplication_available: bool,
auto_clip_enabled_events: Vec<AutoClipEventDto>,
hotkey_clip_last_60s: String,
hotkey_clip_last_30s: String,
hotkey_toggle_session_recording: String,
hotkey_screenshot: String,
}
#[derive(Debug, Serialize)]
@@ -1102,6 +1114,166 @@ fn auto_clip_event_dtos(settings: &AppSettings) -> Vec<AutoClipEventDto> {
.collect()
}
#[cfg(desktop)]
fn parse_accelerator(accel: &str) -> Result<(Option<tauri_plugin_global_shortcut::Modifiers>, tauri_plugin_global_shortcut::Code), String> {
use tauri_plugin_global_shortcut::{Code, Modifiers};
let parts: Vec<&str> = accel.split('+').collect();
let key_str = parts.last().ok_or("Empty accelerator")?.trim();
let code = match key_str.to_uppercase().as_str() {
"F1" => Code::F1, "F2" => Code::F2, "F3" => Code::F3, "F4" => Code::F4,
"F5" => Code::F5, "F6" => Code::F6, "F7" => Code::F7, "F8" => Code::F8,
"F9" => Code::F9, "F10" => Code::F10, "F11" => Code::F11, "F12" => Code::F12,
"A" => Code::KeyA, "B" => Code::KeyB, "C" => Code::KeyC, "D" => Code::KeyD,
"E" => Code::KeyE, "F" => Code::KeyF, "G" => Code::KeyG, "H" => Code::KeyH,
"I" => Code::KeyI, "J" => Code::KeyJ, "K" => Code::KeyK, "L" => Code::KeyL,
"M" => Code::KeyM, "N" => Code::KeyN, "O" => Code::KeyO, "P" => Code::KeyP,
"Q" => Code::KeyQ, "R" => Code::KeyR, "S" => Code::KeyS, "T" => Code::KeyT,
"U" => Code::KeyU, "V" => Code::KeyV, "W" => Code::KeyW, "X" => Code::KeyX,
"Y" => Code::KeyY, "Z" => Code::KeyZ,
"0" => Code::Digit0, "1" => Code::Digit1, "2" => Code::Digit2, "3" => Code::Digit3,
"4" => Code::Digit4, "5" => Code::Digit5, "6" => Code::Digit6, "7" => Code::Digit7,
"8" => Code::Digit8, "9" => Code::Digit9,
"SPACE" => Code::Space, "ENTER" => Code::Enter, "ESCAPE" => Code::Escape,
"TAB" => Code::Tab, "BACKSPACE" => Code::Backspace, "DELETE" => Code::Delete,
"UP" => Code::ArrowUp, "DOWN" => Code::ArrowDown, "LEFT" => Code::ArrowLeft, "RIGHT" => Code::ArrowRight,
"HOME" => Code::Home, "END" => Code::End, "PAGEUP" => Code::PageUp, "PAGEDOWN" => Code::PageDown,
"INSERT" => Code::Insert, "NUMLOCK" => Code::NumLock, "SCROLLLOCK" => Code::ScrollLock, "PAUSE" => Code::Pause,
_ => return Err(format!("Unsupported key: {}", key_str)),
};
let mut modifiers = Modifiers::empty();
for modifier in &parts[..parts.len().saturating_sub(1)] {
match modifier.trim().to_uppercase().as_str() {
"CTRL" | "CONTROL" => modifiers |= Modifiers::CONTROL,
"SHIFT" => modifiers |= Modifiers::SHIFT,
"ALT" => modifiers |= Modifiers::ALT,
"META" | "SUPER" | "COMMAND" => modifiers |= Modifiers::SUPER,
_ => return Err(format!("Unsupported modifier: {}", modifier)),
}
}
Ok((if modifiers.is_empty() { None } else { Some(modifiers) }, code))
}
#[cfg(desktop)]
fn register_hotkeys(
app: &tauri::AppHandle,
runtime: &AppRuntime,
) -> Result<(), String> {
use tauri_plugin_global_shortcut::{GlobalShortcutExt, Shortcut, ShortcutState};
let settings = {
let recorder = runtime.recorder.lock().map_err(|e| e.to_string())?;
recorder.settings.hotkeys.clone()
};
// Unregister existing shortcuts
if let Ok(mut shortcuts_guard) = runtime.hotkey_shortcuts.lock() {
if let Some(existing) = shortcuts_guard.take() {
let _ = app.global_shortcut().unregister(existing.clip_60);
let _ = app.global_shortcut().unregister(existing.clip_30);
let _ = app.global_shortcut().unregister(existing.toggle_recording);
}
}
// Parse new shortcuts
let (clip_60_mods, clip_60_code) = parse_accelerator(&settings.clip_last_60s)?;
let (clip_30_mods, clip_30_code) = parse_accelerator(&settings.clip_last_30s)?;
let (toggle_mods, toggle_code) = parse_accelerator(&settings.toggle_session_recording)?;
let clip_60 = Shortcut::new(clip_60_mods, clip_60_code);
let clip_30 = Shortcut::new(clip_30_mods, clip_30_code);
let toggle_recording = Shortcut::new(toggle_mods, toggle_code);
let handler_clip_60 = clip_60.clone();
let handler_clip_30 = clip_30.clone();
let handler_toggle_recording = toggle_recording.clone();
app.plugin(
tauri_plugin_global_shortcut::Builder::new()
.with_handler(move |app, shortcut, event| {
if event.state() != ShortcutState::Pressed {
return;
}
let runtime = app.state::<AppRuntime>();
if shortcut == &handler_clip_60 {
let _ = save_manual_clip_inner(60, runtime.inner());
} else if shortcut == &handler_clip_30 {
let _ = save_manual_clip_inner(30, runtime.inner());
} else if shortcut == &handler_toggle_recording {
let capture_active = runtime
.capture
.lock()
.map(|capture| capture.is_some())
.unwrap_or(false);
if capture_active {
let _ = stop_capture_inner(runtime.inner());
} else {
let _ = start_capture_inner(runtime.inner());
}
}
})
.build(),
).map_err(|e| e.to_string())?;
app.global_shortcut().register(clip_60.clone()).map_err(|e| e.to_string())?;
app.global_shortcut().register(clip_30.clone()).map_err(|e| e.to_string())?;
app.global_shortcut().register(toggle_recording.clone()).map_err(|e| e.to_string())?;
if let Ok(mut shortcuts_guard) = runtime.hotkey_shortcuts.lock() {
*shortcuts_guard = Some(HotkeyShortcuts {
clip_60,
clip_30,
toggle_recording,
});
}
Ok(())
}
#[tauri::command]
fn set_hotkeys(
clip_last_60s: String,
clip_last_30s: String,
toggle_session_recording: String,
screenshot: String,
runtime: State<'_, AppRuntime>,
) -> Result<DesktopStatus, String> {
let mut recorder = runtime.recorder.lock().map_err(|error| error.to_string())?;
let capture = runtime.capture.lock().map_err(|error| error.to_string())?;
// Validate hotkeys
#[cfg(desktop)]
{
parse_accelerator(&clip_last_60s).map_err(|e| format!("Invalid clip_last_60s: {}", e))?;
parse_accelerator(&clip_last_30s).map_err(|e| format!("Invalid clip_last_30s: {}", e))?;
parse_accelerator(&toggle_session_recording).map_err(|e| format!("Invalid toggle_session_recording: {}", e))?;
parse_accelerator(&screenshot).map_err(|e| format!("Invalid screenshot: {}", e))?;
}
recorder.settings.hotkeys.clip_last_60s = clip_last_60s;
recorder.settings.hotkeys.clip_last_30s = clip_last_30s;
recorder.settings.hotkeys.toggle_session_recording = toggle_session_recording;
recorder.settings.hotkeys.screenshot = screenshot;
save_settings(&runtime.library_root, &recorder.settings)
.map_err(|error| format!("Could not save settings: {error}"))?;
// Re-register hotkeys
#[cfg(desktop)]
{
let app_handle = {
let runtime_guard = runtime.app_handle.lock().map_err(|e| e.to_string())?;
runtime_guard.clone().ok_or("App handle not available")?
};
register_hotkeys(&app_handle, runtime.inner())?;
}
Ok(status_from_recorder(&recorder, capture.as_ref()))
}
fn main() {
let (library_root, recorder, database) =
create_runtime().expect("could not initialize ClipForge runtime");
@@ -1114,53 +1286,30 @@ fn main() {
capture: Mutex::new(None),
league_poller: Mutex::new(LeagueLiveClientPoller::default()),
valve_events: Mutex::new(Vec::new()),
hotkey_shortcuts: Mutex::new(None),
app_handle: Mutex::new(None),
})
.setup(|app| {
start_valve_gsi_receiver(app.handle().clone());
start_backend_workers(app.handle().clone());
.setup(|app| {
let handle = app.handle().clone();
// Store app handle for hotkey re-registration
{
let runtime = app.state::<AppRuntime>();
let _ = runtime.app_handle.lock().map(|mut guard| {
*guard = Some(handle.clone());
});
}
start_valve_gsi_receiver(handle.clone());
start_backend_workers(handle.clone());
// Register hotkeys after storing the handle
#[cfg(desktop)]
{
use tauri_plugin_global_shortcut::{
Code, GlobalShortcutExt, Modifiers, Shortcut, ShortcutState,
};
let clip_60 = Shortcut::new(None, Code::F8);
let clip_30 = Shortcut::new(Some(Modifiers::SHIFT), Code::F8);
let toggle_recording = Shortcut::new(Some(Modifiers::ALT), Code::F7);
let handler_clip_60 = clip_60.clone();
let handler_clip_30 = clip_30.clone();
let handler_toggle_recording = toggle_recording.clone();
app.handle().plugin(
tauri_plugin_global_shortcut::Builder::new()
.with_handler(move |app, shortcut, event| {
if event.state() != ShortcutState::Pressed {
return;
}
let runtime = app.state::<AppRuntime>();
if shortcut == &handler_clip_60 {
let _ = save_manual_clip_inner(60, runtime.inner());
} else if shortcut == &handler_clip_30 {
let _ = save_manual_clip_inner(30, runtime.inner());
} else if shortcut == &handler_toggle_recording {
let capture_active = runtime
.capture
.lock()
.map(|capture| capture.is_some())
.unwrap_or(false);
if capture_active {
let _ = stop_capture_inner(runtime.inner());
} else {
let _ = start_capture_inner(runtime.inner());
}
}
})
.build(),
)?;
app.global_shortcut().register(clip_60)?;
app.global_shortcut().register(clip_30)?;
app.global_shortcut().register(toggle_recording)?;
let runtime = app.state::<AppRuntime>();
register_hotkeys(&handle, runtime.inner())?;
}
Ok(())
})
.invoke_handler(tauri::generate_handler![
@@ -1185,7 +1334,8 @@ fn main() {
trim_clip,
upload_clip,
update_clip_metadata,
export_clip_copy
export_clip_copy,
set_hotkeys
])
.run(tauri::generate_context!())
.expect("error while running ClipForge desktop shell");
@@ -1370,6 +1520,10 @@ fn status_from_recorder(recorder: &RecorderService, capture: Option<&ActiveCaptu
.map(|path| ffmpeg_supports_filter(&path, "ddagrab"))
.unwrap_or(false),
auto_clip_enabled_events: auto_clip_event_dtos(&recorder.settings),
hotkey_clip_last_60s: recorder.settings.hotkeys.clip_last_60s.clone(),
hotkey_clip_last_30s: recorder.settings.hotkeys.clip_last_30s.clone(),
hotkey_toggle_session_recording: recorder.settings.hotkeys.toggle_session_recording.clone(),
hotkey_screenshot: recorder.settings.hotkeys.screenshot.clone(),
}
}
+89 -22
View File
@@ -13,29 +13,50 @@ pub struct HotkeyBinding {
pub action: HotkeyAction,
}
pub fn default_bindings() -> Vec<HotkeyBinding> {
vec![
HotkeyBinding {
accelerator: "F8".to_string(),
action: HotkeyAction::SaveReplay {
length: Duration::from_secs(60),
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HotkeyConfig {
pub clip_last_60s: String,
pub clip_last_30s: String,
pub toggle_session_recording: String,
pub screenshot: String,
}
impl Default for HotkeyConfig {
fn default() -> Self {
Self {
clip_last_60s: "F8".to_string(),
clip_last_30s: "Shift+F8".to_string(),
toggle_session_recording: "Alt+F7".to_string(),
screenshot: "F9".to_string(),
}
}
}
impl HotkeyConfig {
pub fn into_bindings(self) -> Vec<HotkeyBinding> {
vec![
HotkeyBinding {
accelerator: self.clip_last_60s,
action: HotkeyAction::SaveReplay {
length: Duration::from_secs(60),
},
},
},
HotkeyBinding {
accelerator: "Shift+F8".to_string(),
action: HotkeyAction::SaveReplay {
length: Duration::from_secs(30),
HotkeyBinding {
accelerator: self.clip_last_30s,
action: HotkeyAction::SaveReplay {
length: Duration::from_secs(30),
},
},
},
HotkeyBinding {
accelerator: "Alt+F7".to_string(),
action: HotkeyAction::ToggleSessionRecording,
},
HotkeyBinding {
accelerator: "F9".to_string(),
action: HotkeyAction::Screenshot,
},
]
HotkeyBinding {
accelerator: self.toggle_session_recording,
action: HotkeyAction::ToggleSessionRecording,
},
HotkeyBinding {
accelerator: self.screenshot,
action: HotkeyAction::Screenshot,
},
]
}
}
pub fn parse_hotkey_action(accelerator: &str, bindings: &[HotkeyBinding]) -> Option<HotkeyAction> {
@@ -45,13 +66,42 @@ pub fn parse_hotkey_action(accelerator: &str, bindings: &[HotkeyBinding]) -> Opt
.map(|binding| binding.action.clone())
}
pub fn validate_hotkey(accelerator: &str) -> Result<(), String> {
let parts: Vec<&str> = accelerator.split('+').collect();
let key = parts.last().ok_or("Empty hotkey")?;
if key.is_empty() {
return Err("Empty hotkey".to_string());
}
let valid_keys = [
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"Space", "Enter", "Escape", "Tab", "Backspace", "Delete",
"Up", "Down", "Left", "Right",
"Home", "End", "PageUp", "PageDown",
"Insert", "NumLock", "ScrollLock", "Pause",
];
if !valid_keys.iter().any(|k| k.eq_ignore_ascii_case(key)) {
return Err(format!("Unsupported key: {key}"));
}
for modifier in &parts[..parts.len().saturating_sub(1)] {
if !["Ctrl", "Shift", "Alt", "Meta", "Super", "Command"].iter().any(|m| m.eq_ignore_ascii_case(modifier)) {
return Err(format!("Unsupported modifier: {modifier}"));
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_default_save_replay_hotkey() {
let action = parse_hotkey_action("f8", &default_bindings());
let config = HotkeyConfig::default();
let bindings = config.into_bindings();
let action = parse_hotkey_action("f8", &bindings);
assert_eq!(
action,
Some(HotkeyAction::SaveReplay {
@@ -59,4 +109,21 @@ mod tests {
})
);
}
#[test]
fn validates_supported_hotkeys() {
assert!(validate_hotkey("F8").is_ok());
assert!(validate_hotkey("Shift+F8").is_ok());
assert!(validate_hotkey("Alt+F7").is_ok());
assert!(validate_hotkey("Ctrl+Shift+A").is_ok());
assert!(validate_hotkey("Meta+Space").is_ok());
}
#[test]
fn rejects_invalid_hotkeys() {
assert!(validate_hotkey("").is_err());
assert!(validate_hotkey("InvalidKey").is_err());
assert!(validate_hotkey("Ctrl+Invalid").is_err());
assert!(validate_hotkey("+").is_err());
}
}
+60 -2
View File
@@ -70,6 +70,10 @@ type DesktopStatus = {
system_audio_available: boolean;
desktop_duplication_available: boolean;
auto_clip_enabled_events: AutoClipEventDto[];
hotkey_clip_last_60s: string;
hotkey_clip_last_30s: string;
hotkey_toggle_session_recording: string;
hotkey_screenshot: string;
};
type ClipDto = {
@@ -130,6 +134,10 @@ type AppState = {
selectedClipId: string;
clips: Clip[];
autoEvents: AutoEvent[];
hotkeyClipLast60s: string;
hotkeyClipLast30s: string;
hotkeyToggleRecording: string;
hotkeyScreenshot: string;
};
type TauriGlobal = {
@@ -192,6 +200,10 @@ const state: AppState = {
{ id: "dota-objective", gameId: "dota-2", eventType: "objective", game: "Dota 2", event: "Roshan/objective", enabled: true },
{ id: "dota-multi", gameId: "dota-2", eventType: "multi_kill", game: "Dota 2", event: "Multi-kill", enabled: true },
],
hotkeyClipLast60s: "F8",
hotkeyClipLast30s: "Shift+F8",
hotkeyToggleRecording: "Alt+F7",
hotkeyScreenshot: "F9",
};
const root = document.querySelector<HTMLDivElement>("#app");
@@ -519,8 +531,13 @@ function renderSettingsView() {
</article>
<article class="settings-panel">
<p class="eyebrow">Hotkeys</p>
<h2>F8 saves last 60s</h2>
<p class="muted">Shift+F8 saves 30s · Alt+F7 toggles session recording.</p>
<h2>Customizable</h2>
<label>Clip last 60s <input value="${escapeHtml(state.hotkeyClipLast60s)}" data-action="hotkey-clip-60s" placeholder="e.g. F8" /></label>
<label>Clip last 30s <input value="${escapeHtml(state.hotkeyClipLast30s)}" data-action="hotkey-clip-30s" placeholder="e.g. Shift+F8" /></label>
<label>Toggle recording <input value="${escapeHtml(state.hotkeyToggleRecording)}" data-action="hotkey-toggle-recording" placeholder="e.g. Alt+F7" /></label>
<label>Screenshot <input value="${escapeHtml(state.hotkeyScreenshot)}" data-action="hotkey-screenshot" placeholder="e.g. F9" /></label>
<p class="muted">Format: [Modifier+]Key (e.g. Ctrl+Shift+F1, Alt+F7, F8). Modifiers: Ctrl, Shift, Alt, Meta.</p>
<button data-action="save-hotkeys">Save Hotkeys</button>
</article>
</section>
`;
@@ -681,6 +698,22 @@ function bindEvents() {
}
});
});
appRoot.querySelector<HTMLInputElement>("[data-action='hotkey-clip-60s']")?.addEventListener("change", (event) => {
state.hotkeyClipLast60s = (event.target as HTMLInputElement).value;
});
appRoot.querySelector<HTMLInputElement>("[data-action='hotkey-clip-30s']")?.addEventListener("change", (event) => {
state.hotkeyClipLast30s = (event.target as HTMLInputElement).value;
});
appRoot.querySelector<HTMLInputElement>("[data-action='hotkey-toggle-recording']")?.addEventListener("change", (event) => {
state.hotkeyToggleRecording = (event.target as HTMLInputElement).value;
});
appRoot.querySelector<HTMLInputElement>("[data-action='hotkey-screenshot']")?.addEventListener("change", (event) => {
state.hotkeyScreenshot = (event.target as HTMLInputElement).value;
});
appRoot.querySelector<HTMLButtonElement>("[data-action='save-hotkeys']")?.addEventListener("click", () => {
void saveHotkeys();
});
}
async function saveClip() {
@@ -777,6 +810,10 @@ function applyDesktopStatus(status: DesktopStatus) {
event.enabled = enabled.enabled;
}
}
state.hotkeyClipLast60s = status.hotkey_clip_last_60s;
state.hotkeyClipLast30s = status.hotkey_clip_last_30s;
state.hotkeyToggleRecording = status.hotkey_toggle_session_recording;
state.hotkeyScreenshot = status.hotkey_screenshot;
}
async function trimSelectedClip() {
@@ -1098,6 +1135,27 @@ async function saveAutoClipEventSetting(eventRule: AutoEvent) {
}
}
async function saveHotkeys() {
if (!tauriInvoke) {
return;
}
try {
const status = await tauriInvoke<DesktopStatus>("set_hotkeys", {
clip_last_60s: state.hotkeyClipLast60s,
clip_last_30s: state.hotkeyClipLast30s,
toggle_session_recording: state.hotkeyToggleRecording,
screenshot: state.hotkeyScreenshot,
});
applyDesktopStatus(status);
showNotice("Hotkeys saved");
render();
} catch (error) {
console.warn("Could not save hotkeys", error);
showNotice(`Could not save hotkeys: ${String(error)}`);
}
}
function filteredClips() {
const query = state.libraryQuery.trim().toLowerCase();
return state.clips.filter((clip) => {