detect window focus

This commit is contained in:
jacob 2025-10-27 01:05:09 -05:00
parent 9297e40b0c
commit e730ad7b5e
4 changed files with 32 additions and 190 deletions

View File

@ -106,13 +106,16 @@ void PushGameUiStyle(void)
// UI_Push(FontSize, 24);
UI_Push(FontSize, 12);
UI_Push(Rounding, 0.5f);
UI_Push(Rounding, 0);
UI_Push(TextPadding, 4);
UI_Push(Border, 3);
UI_Push(BackgroundColor, Rgba32F(0.3, 0.5, 0.3, 0.3));
UI_Push(Border, 1);
// UI_Push(BackgroundColor, Rgba32F(0.3, 0.5, 0.3, 0.3));
UI_Push(BackgroundColor, Rgba32F(0.3, 0.3, 0.3, 1));
UI_Push(BorderColor, Rgba32F(0.1, 0.4, 0.3, 0.4));
UI_Push(BorderColor, Rgba32F(0.6, 0.6, 0.6, 1));
UI_Push(LayoutAxis, Axis_Y);
}
//- Draw xform
@ -363,8 +366,7 @@ void DrawDebugConsole(b32 minimized)
UI_Push(Width, UI_TextSize(0));
UI_Push(Height, UI_TextSize(0));
UI_Push(BorderColor, Rgba32F(0.25, 0.25, 0.25, 1));
UI_Push(Border, 2);
UI_Push(Rounding, 0.5);
UI_Push(Rounding, 0);
UI_Push(TextPadding, 6);
Lock lock = LockE(&g->console_logs_mutex);
{
@ -513,6 +515,7 @@ void UpdateUser(void)
WND_PushCmd(window_frame, .kind = WND_CmdKind_Restore, .restore = restore);
}
}
else
{
String src = window_frame.restore;
if (src.len > g->window_restore.len)
@ -533,9 +536,10 @@ void UpdateUser(void)
//- Begin UI
UI_BeginBuild();
UI_Push(LayoutAxis, Axis_Y);
UI_Box *pp_root_box = UI_BuildBox(0, UI_NilKey);
UI_Push(Parent, pp_root_box);
PushGameUiStyle();
UI_Push(Parent, pp_root_box);
//- Init render data buffers
if (!g->material_instances_arena)
@ -2401,13 +2405,13 @@ JobDef(UpdateUserOrSleep, UNUSED sig, UNUSED id)
i64 time_ns = TimeNs();
while (!Atomic32Fetch(&g->shutdown))
{
#if FPS_LIMIT > 0
u32 fps_limit = FPS_LIMIT;
if (fps_limit > 0)
{
__profn("User frame limit");
P_SleepFrame(time_ns, 1000000000 / FPS_LIMIT);
P_SleepFrame(time_ns, 1000000000 / fps_limit);
time_ns = TimeNs();
}
#endif
UpdateUser();
}
}

View File

@ -44,6 +44,7 @@ Struct(WND_Frame)
b32 maximized;
b32 fullscreen;
b32 forced_top;
b32 has_focus;
};
////////////////////////////////////////////////////////////

View File

@ -378,182 +378,6 @@ LRESULT CALLBACK WND_W32_WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM l
}
EndScratch(scratch);
} break;
#if 0
//- Process command
case WND_CmdMsgId:
{
P_LogWarningF("RAAAAH");
WND_Event user_event = ZI;
WND_Cmd user_cmd = ZI;
{
LockTicketMutex(&window->u2w_tm);
{
user_event = window->u2w_update_end_event;
user_cmd = window->u2w_update_end_cmd;
}
UnlockTicketMutex(&window->u2w_tm);
}
/* Determine old settings */
Vec2I32 old_draw_p0 = ZI;
Vec2I32 old_draw_p1 = ZI;
Vec2I32 old_restore_p0 = ZI;
Vec2I32 old_restore_p1 = ZI;
b32 old_is_visible = 0;
b32 old_has_border = 0;
{
{
RECT screen_rect = ZI;
GetClientRect(hwnd, (LPRECT)&screen_rect);
ClientToScreen(hwnd, (LPPOINT)&screen_rect.left);
ClientToScreen(hwnd, (LPPOINT)&screen_rect.right);
old_draw_p0 = VEC2I32(screen_rect.left, screen_rect.top);
old_draw_p1 = VEC2I32(screen_rect.right, screen_rect.bottom);
}
{
WINDOWPLACEMENT placement = { .length = sizeof(placement) };
GetWindowPlacement(hwnd, &placement);
RECT placement_rect = placement.rcNormalPosition;
old_restore_p0 = VEC2I32(placement_rect.left, placement_rect.top);
old_restore_p1 = VEC2I32(placement_rect.right, placement_rect.bottom);
}
{
DWORD style = (DWORD)GetWindowLongPtr(hwnd, GWL_STYLE);
old_is_visible = !!(style & WS_VISIBLE);
old_has_border = !(style & WS_POPUP);
}
}
/* Determine new settings */
Vec2I32 new_draw_p0 = old_draw_p0;
Vec2I32 new_draw_p1 = old_draw_p1;
Vec2I32 new_restore_p0 = old_restore_p0;
Vec2I32 new_restore_p1 = old_restore_p1;
b32 new_is_visible = old_is_visible;
b32 new_has_border = old_has_border;
{
WND_Settings desired_settings = user_cmd.desired_settings;
if (desired_settings.flags & WND_Flag_Fullscreen)
{
new_has_border = 0;
new_draw_p0 = user_event.monitor_p0;
new_draw_p1 = user_event.monitor_p1;
}
else if (desired_settings.flags & WND_Flag_Maximized)
{
// show_cmd = SW_MAXIMIZE;
}
else if (desired_settings.flags & WND_Flag_Minimized)
{
// show_cmd = SW_MINIMIZE;
}
else
{
new_has_border = 1;
new_restore_p0 = desired_settings.restore_p0;
new_restore_p1 = desired_settings.restore_p1;
}
new_is_visible = 1;
}
/* Determine new style & placement */
b32 has_user_seen_os_settings = user_event.os_gen == (u64)Atomic64Fetch(&window->os_gen);
if (has_user_seen_os_settings)
{
/* Calculate style */
b32 dirty_style = 0;
DWORD new_style = (DWORD)GetWindowLongPtr(hwnd, GWL_STYLE);
{
if (new_is_visible != old_is_visible)
{
dirty_style = 1;
if (new_is_visible)
{
new_style |= WS_VISIBLE;
}
else
{
new_style &= ~WS_VISIBLE;
}
}
if (new_has_border != old_has_border)
{
dirty_style = 1;
if (new_has_border)
{
new_style &= ~WS_POPUP;
new_style |= WS_OVERLAPPEDWINDOW;
}
else
{
new_style &= ~WS_OVERLAPPEDWINDOW;
new_style |= WS_POPUP;
}
}
}
/* Calculate placement */
b32 dirty_placement = 0;
WINDOWPLACEMENT new_placement = { .length = sizeof(new_placement) };
GetWindowPlacement(hwnd, &new_placement);
{
if (!EqVec2I32(new_restore_p0, old_restore_p0) || !EqVec2I32(new_restore_p1, old_restore_p1))
{
dirty_placement = 1;
new_placement.rcNormalPosition.left = new_restore_p0.x;
new_placement.rcNormalPosition.top = new_restore_p0.y;
new_placement.rcNormalPosition.right = new_restore_p1.x;
new_placement.rcNormalPosition.bottom = new_restore_p1.y;
}
}
/* Calculate draw position */
b32 dirty_draw_rect = 0;
RECT new_draw_rect = ZI;
if (!EqVec2I32(new_draw_p0, old_draw_p0) || !EqVec2I32(new_draw_p1, old_draw_p1))
{
dirty_draw_rect = 1;
new_draw_rect.left = new_draw_p0.x;
new_draw_rect.top = new_draw_p0.y;
new_draw_rect.right = new_draw_p1.x;
new_draw_rect.bottom = new_draw_p1.y;
AdjustWindowRect(&new_draw_rect, new_style, 0);
}
/* Apply changes */
if (dirty_style || dirty_placement || dirty_draw_rect)
{
if (dirty_style)
{
++window->cmd_depth;
SetWindowLongPtrW(hwnd, GWL_STYLE, new_style);
}
if (dirty_placement)
{
++window->cmd_depth;
SetWindowPlacement(hwnd, &new_placement);
}
if (dirty_draw_rect)
{
u32 pflags = 0;
Vec2I32 size = VEC2I32(new_draw_rect.right - new_draw_rect.left, new_draw_rect.bottom - new_draw_rect.top);
++window->cmd_depth;
SetWindowPos(hwnd, 0, new_draw_rect.left, new_draw_rect.top, size.x, size.y, pflags);
}
}
}
/* Bring window to front on first show */
if (!window->first_shown)
{
SetForegroundWindow(hwnd);
BringWindowToTop(hwnd);
window->first_shown = 1;
}
} break;
#endif
}
}
@ -656,6 +480,7 @@ WND_Frame WND_BeginFrame(Arena *arena)
result.forced_top = window->is_forced_top;
result.fullscreen = window->is_fullscreen;
result.has_focus = GetForegroundWindow() == hwnd;
/* Generate restore data */
{
@ -668,6 +493,7 @@ WND_Frame WND_BeginFrame(Arena *arena)
restore.is_forced_top = window->is_forced_top;
restore.is_fullscreen = window->is_fullscreen;
restore.fullscreen_restore_rect = window->fullscreen_restore_rect;
restore.has_focus = result.has_focus;
if (IsWindowArranged(hwnd))
{
restore.is_snapped = 1;
@ -688,6 +514,7 @@ void WND_EndFrame(WND_Frame frame)
HWND hwnd = window->hwnd;
/* Process cmds */
b32 was_restored = 0;
for (WND_W32_CmdNode *n = window->first_cmd; n; n = n->next)
{
WND_Cmd cmd = n->cmd;
@ -767,6 +594,7 @@ void WND_EndFrame(WND_Frame frame)
if (restore->magic == WND_W32_RestoreMagic)
{
WINDOWPLACEMENT placement = restore->placement;
was_restored = 1;
SetWindowPlacement(hwnd, &placement);
SetWindowLongPtr(hwnd, GWL_STYLE, restore->style);
SetWindowLongPtr(hwnd, GWL_EXSTYLE, restore->ex_style);
@ -793,20 +621,27 @@ void WND_EndFrame(WND_Frame frame)
HWND pos_hwnd = window->is_forced_top ? HWND_TOPMOST : HWND_NOTOPMOST;
SetWindowPos(hwnd, pos_hwnd, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, 0);
}
if (restore->has_focus)
{
ShowWindow(hwnd, SW_SHOW);
SetForegroundWindow(hwnd);
BringWindowToTop(hwnd);
}
}
}
} break;
}
}
/* Bring window to front on first show */
if (!window->first_shown)
/* Bring window to front on first frame */
if (!was_restored && window->frame_gen == 0)
{
ShowWindow(hwnd, SW_SHOW);
SetForegroundWindow(hwnd);
BringWindowToTop(hwnd);
window->first_shown = 1;
}
++window->frame_gen;
EndScratch(scratch);
}

View File

@ -13,7 +13,7 @@ Struct(WND_W32_Window)
Arena *cmds_arena;
struct WND_W32_CmdNode *first_cmd;
struct WND_W32_CmdNode *last_cmd;
b32 first_shown;
u64 frame_gen;
b32 is_forced_top;
b32 is_fullscreen;
RECT fullscreen_restore_rect;
@ -42,6 +42,8 @@ Struct(WND_W32_RestorableData)
b32 is_fullscreen;
RECT fullscreen_restore_rect;
b32 has_focus;
b32 is_snapped;
RECT snapped_screen_rect;
};