feat: add FinalToken v1 for Claude and Codex usage (#2)
FinalToken v1: a NativeAOT tray app for Windows 11 and CachyOS that tracks Claude Code and Codex usage and limits through their CLIs only, with no provider sign-in and no tokens (R1-R7, D1-D11). - Core: incremental collectors for Claude transcripts and Codex rollouts, limit probes through `claude -p /usage` and `codex app-server`, a journaled history store, the refresh engine, pace and popup decisions, and the snapshot.json contract. - App (Avalonia): stats panel with limit bars, pace, projections, token totals, charts, limit history and breakdowns; tray, settings, non-activating pace popups, single-instance activation, start with the OS, and the Off/Ask/Automatic Stream Deck plugin install. - Stream Deck plugin (SDK v2, NativeAOT) for Elgato and OpenDeck. - finaltoken-cli, tools/publish.py, the documentation check and the agent review workflow. Closes #1
This commit was merged in pull request #2.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Runtime.Versioning;
|
||||
using Avalonia.Controls;
|
||||
|
||||
namespace FinalToken.App;
|
||||
|
||||
[SupportedOSPlatform("windows")]
|
||||
internal static partial class WindowsActivation
|
||||
{
|
||||
public static void Activate(Window window)
|
||||
{
|
||||
if (window.TryGetPlatformHandle() is not { } platform) return;
|
||||
var handle = platform.Handle;
|
||||
if (SetForegroundWindow(handle) != 0) return;
|
||||
var foreground = GetForegroundWindow();
|
||||
if (foreground == 0) return;
|
||||
var foregroundThread = GetWindowThreadProcessId(foreground, out _);
|
||||
var ownThread = GetCurrentThreadId();
|
||||
if (foregroundThread == ownThread || AttachThreadInput(ownThread, foregroundThread, 1) == 0) return;
|
||||
try { SetForegroundWindow(handle); }
|
||||
finally { AttachThreadInput(ownThread, foregroundThread, 0); }
|
||||
}
|
||||
// D8: a launcher transfers its foreground permission before the running app activates.
|
||||
public static void AllowRunningInstance()
|
||||
{
|
||||
using var current = Process.GetCurrentProcess();
|
||||
foreach (var candidate in Process.GetProcessesByName(current.ProcessName))
|
||||
{
|
||||
using (candidate)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (candidate.Id != current.Id && candidate.SessionId == current.SessionId &&
|
||||
string.Equals(candidate.MainModule?.FileName, Environment.ProcessPath, StringComparison.OrdinalIgnoreCase))
|
||||
AllowSetForegroundWindow(candidate.Id);
|
||||
}
|
||||
catch (Exception e) when (e is Win32Exception or InvalidOperationException) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
[LibraryImport("user32.dll")]
|
||||
private static partial int AllowSetForegroundWindow(int processId);
|
||||
[LibraryImport("user32.dll")]
|
||||
private static partial int SetForegroundWindow(nint window);
|
||||
[LibraryImport("user32.dll")]
|
||||
private static partial nint GetForegroundWindow();
|
||||
[LibraryImport("user32.dll")]
|
||||
private static partial uint GetWindowThreadProcessId(nint window, out uint processId);
|
||||
[LibraryImport("kernel32.dll")]
|
||||
private static partial uint GetCurrentThreadId();
|
||||
[LibraryImport("user32.dll")]
|
||||
private static partial int AttachThreadInput(uint first, uint second, int attach);
|
||||
}
|
||||
Reference in New Issue
Block a user