@@ -278,7 +278,6 @@ local function _TimMenu_GlobalDraw()
278278
279279 local mouseX , mouseY = table.unpack (input .GetMousePos ())
280280 TimMenuGlobal .mouseX , TimMenuGlobal .mouseY = mouseX , mouseY
281- local focusedWindowKey = nil
282281
283282 local currentFrame = globals .FrameCount ()
284283 local keysToRemove = {}
@@ -297,20 +296,39 @@ local function _TimMenu_GlobalDraw()
297296 end
298297 end
299298
299+ -- PRIMARY FOCUS LOOP: Top-to-bottom search for window under mouse
300+ local windowUnderMouse = nil
301+ local mousePressed = input .IsButtonPressed (MOUSE_LEFT )
302+
300303 for i = # TimMenuGlobal .order , 1 , - 1 do
301304 local key = TimMenuGlobal .order [i ]
302305 local win = TimMenuGlobal .windows [key ]
303306 if win and win .visible and win :_HitTest (mouseX , mouseY ) then
304- focusedWindowKey = key
305- break
307+ windowUnderMouse = key
308+ break -- FOUND THE TOPMOST OWNER. STOP SEARCHING.
309+ end
310+ end
311+
312+ -- Handle focus changes only when clicking
313+ if mousePressed and windowUnderMouse then
314+ -- Bring the owner window to front
315+ if TimMenuGlobal .order [# TimMenuGlobal .order ] ~= windowUnderMouse then
316+ for j , v_key in ipairs (TimMenuGlobal .order ) do
317+ if v_key == windowUnderMouse then
318+ table.remove (TimMenuGlobal .order , j )
319+ break
320+ end
321+ end
322+ table.insert (TimMenuGlobal .order , windowUnderMouse )
306323 end
307324 end
308325
326+ -- Update logic for all visible windows
309327 for i = 1 , # TimMenuGlobal .order do
310328 local key = TimMenuGlobal .order [i ]
311329 local win = TimMenuGlobal .windows [key ]
312330 if win and win .visible then
313- local isFocused = (key == focusedWindowKey )
331+ local isFocused = (key == windowUnderMouse )
314332 win :_UpdateLogic (
315333 mouseX ,
316334 mouseY ,
@@ -319,25 +337,6 @@ local function _TimMenu_GlobalDraw()
319337 input .IsButtonDown (MOUSE_LEFT ),
320338 input .IsButtonReleased (MOUSE_LEFT )
321339 )
322-
323- -- Only change focus if window allows it (prevents popup focus stealing)
324- -- AND no widget has consumed the click
325- if
326- isFocused
327- and input .IsButtonPressed (MOUSE_LEFT )
328- and win :ShouldChangeFocus (mouseX , mouseY )
329- and not Globals .isClickConsumed
330- then
331- if TimMenuGlobal .order [# TimMenuGlobal .order ] ~= key then
332- for j , v_key in ipairs (TimMenuGlobal .order ) do
333- if v_key == key then
334- table.remove (TimMenuGlobal .order , j )
335- break
336- end
337- end
338- table.insert (TimMenuGlobal .order , key )
339- end
340- end
341340 end
342341 end
343342
@@ -358,9 +357,6 @@ local function _TimMenu_GlobalDraw()
358357 end
359358 end
360359
361- -- Reset click consumption flag at end of frame
362- Globals .isClickConsumed = false
363-
364360 if not reRegistered then
365361 callbacks .Unregister (" Draw" , " zTimMenu_GlobalDraw" )
366362 callbacks .Register (" Draw" , " zTimMenu_GlobalDraw" , _TimMenu_GlobalDraw )
0 commit comments