Skip to content

Commit

Permalink
Make Downloader more stable on Mac.
Browse files Browse the repository at this point in the history
Fixes crash when using Show/HideWithEffect and keeps focus on the
downloader window even when a popup is being shown.

Fixes mrichards42#54
  • Loading branch information
jpd236 committed Dec 6, 2019
1 parent 819382d commit d64088a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
11 changes: 7 additions & 4 deletions scripts/download/gui/popup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ local function PopupWindow(parent)
-- A borderless dialog
local self = wx.wxDialog(
parent, wx.wxID_ANY, '', wx.wxDefaultPosition, wx.wxDefaultSize,
wx.wxFRAME_NO_TASKBAR + wx.wxFRAME_FLOAT_ON_PARENT + wx.wxWS_EX_TRANSIENT
wx.wxFRAME_NO_TASKBAR + wx.wxFRAME_FLOAT_ON_PARENT + wx.wxWS_EX_TRANSIENT
)

-- wxWindow:Fit is simply SetSize(GetBestSize()), but we need to adjust
-- both position and size
function self:Fit()
Expand Down Expand Up @@ -48,10 +48,13 @@ local function PopupWindow(parent)
function self:Popup(effect, timeout)
-- Move to the best position based on the current mouse pointer
self:Fit()
if effect then
if effect and effect ~= wx.wxSHOW_EFFECT_NONE then
self:ShowWithEffect(effect, timeout or 0)
else
self:Show()
-- On Mac, Show() causes focus to move to the popup.
-- Unclear why this is inconsistent between platforms, but since the
-- effect isn't supported anyway, this works fine.
self:ShowWithoutActivating()
end

-- Destroy the popup when the mouse leaves the parent window.
Expand Down
20 changes: 14 additions & 6 deletions scripts/download/gui/puzzle_grid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local function TextGrid(parent, x_gap, y_gap)
-- @param col the column (0-indexed)
-- @param row the row (0-indexed)
-- @param[opt] data Custom item data for hyperlinked text.
-- @param[opt] is_span If true, don't factor into grid size.
-- @param[opt] is_span If true, don't factor into grid size.
-- This can be used for headers, etc. that cross multiple cells.
function self:Add(text, col, row, data, is_span)
local item = {col, row, text, data}
Expand Down Expand Up @@ -49,7 +49,7 @@ local function TextGrid(parent, x_gap, y_gap)
self.item_width,
self.item_height
end

--- Override wxWindow::Fit to set a better min size
function self:Fit()
-- Find the rectangle of the bottom-right most item.
Expand Down Expand Up @@ -276,6 +276,14 @@ local function PuzzleGrid(parent, x_gap, y_gap)
local popup
--- Show a popup and underline on hover
function self:OnHover(puzzle)
-- Effects cause crashes on Mac when rapidly showing/hiding popups.
local effect
if wx.__WXMAC__ then
effect = wx.wxSHOW_EFFECT_NONE
else
effect = wx.wxSHOW_EFFECT_BLEND
end

-- Destroy popup
if puzzle then
if not popup then
Expand All @@ -288,9 +296,9 @@ local function PuzzleGrid(parent, x_gap, y_gap)
end
-- Show it
popup:set_puzzle(puzzle)
popup:Popup(wx.wxSHOW_EFFECT_BLEND, 100)
popup:Popup(effect, 100)
elseif popup then
popup:HideWithEffect(wx.wxSHOW_EFFECT_BLEND, 100)
popup:HideWithEffect(effect, 100)
popup:Destroy()
end
end
Expand Down Expand Up @@ -331,7 +339,7 @@ local function PuzzleGrid(parent, x_gap, y_gap)
self:PopupMenu(menu)
end
end)

-- Cleanup when we are destroyed
self:Connect(self:GetId(), wx.wxEVT_DESTROY, function(evt)
for _, filename in ipairs(filenames) do
Expand Down Expand Up @@ -421,4 +429,4 @@ return setmetatable({
Month = Month,
List = List,
TextGrid = TextGrid,
}, { __call = PuzzleGrid })
}, { __call = PuzzleGrid })

0 comments on commit d64088a

Please sign in to comment.