Skip to content

Commit 70dc34f

Browse files
committed
Rewrite tooltip manager to avoid leaking connections
1 parent c94c749 commit 70dc34f

7 files changed

Lines changed: 41 additions & 32 deletions

File tree

src/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ aegisub_src = files(
135135
'timeedit_ctrl.cpp',
136136
'toggle_bitmap.cpp',
137137
'toolbar.cpp',
138-
'tooltip_manager.cpp',
138+
'tooltip_binding.cpp',
139139
'utils.cpp',
140140
'validators.cpp',
141141
'vector2d.cpp',

src/subs_edit_box.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
#include "subs_edit_ctrl.h"
5252
#include "text_selection_controller.h"
5353
#include "timeedit_ctrl.h"
54-
#include "tooltip_manager.h"
5554
#include "validators.h"
5655

5756
#include <libaegisub/character_count.h>
@@ -282,7 +281,7 @@ TimeEdit *SubsEditBox::MakeTimeCtrl(wxString const& tooltip, TimeField field) {
282281
void SubsEditBox::MakeButton(const char *cmd_name) {
283282
cmd::Command *command = cmd::get(cmd_name);
284283
wxBitmapButton *btn = new wxBitmapButton(this, -1, command->Icon());
285-
ToolTipManager::Bind(btn, command->StrHelp(), "Subtitle Edit Box", cmd_name);
284+
tool_tip_bindings.emplace_back(btn, command->StrHelp(), "Subtitle Edit Box", cmd_name);
286285

287286
middle_right_sizer->Add(btn, wxSizerFlags().Expand());
288287
btn->Bind(wxEVT_BUTTON, std::bind(&SubsEditBox::CallCommand, this, cmd_name));
@@ -291,7 +290,7 @@ void SubsEditBox::MakeButton(const char *cmd_name) {
291290
wxButton *SubsEditBox::MakeBottomButton(const char *cmd_name) {
292291
cmd::Command *command = cmd::get(cmd_name);
293292
wxButton *btn = new wxButton(this, -1, command->StrDisplay(c));
294-
ToolTipManager::Bind(btn, command->StrHelp(), "Subtitle Edit Box", cmd_name);
293+
tool_tip_bindings.emplace_back(btn, command->StrHelp(), "Subtitle Edit Box", cmd_name);
295294

296295
btn->Bind(wxEVT_BUTTON, std::bind(&SubsEditBox::CallCommand, this, cmd_name));
297296
return btn;

src/subs_edit_box.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
//
2828
// Aegisub Project http://www.aegisub.org/
2929

30+
#include "tooltip_binding.h"
31+
3032
#include <array>
3133
#include <boost/container/map.hpp>
3234
#include <boost/flyweight/flyweight_fwd.hpp>
35+
#include <list>
3336
#include <vector>
3437

3538
#include <wx/combobox.h>
@@ -68,6 +71,7 @@ class SubsEditBox final : public wxPanel {
6871
};
6972

7073
std::vector<agi::signal::Connection> connections;
74+
std::list<ToolTipBinding> tool_tip_bindings;
7175

7276
/// Currently active dialogue line
7377
AssDialogue *line = nullptr;

src/toggle_bitmap.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "toggle_bitmap.h"
3636

3737
#include "command/command.h"
38-
#include "tooltip_manager.h"
3938

4039
#include <wx/dcbuffer.h>
4140
#include <wx/settings.h>
@@ -46,6 +45,7 @@ ToggleBitmap::ToggleBitmap(wxWindow *parent, agi::Context *context, const char *
4645
, context(context)
4746
, command(*cmd::get(cmd_name))
4847
, imgs(command.Icon(icon_size))
48+
, tool_tip_binding(this, command.StrHelp(), ht_ctx, cmd_name)
4949
{
5050
wxBitmap img = imgs.GetBitmapFor(this);
5151
int w = size.GetWidth() != -1 ? size.GetWidth() : img.GetLogicalWidth();
@@ -55,7 +55,6 @@ ToggleBitmap::ToggleBitmap(wxWindow *parent, agi::Context *context, const char *
5555
SetSizeHints(w, h, w, h);
5656
SetBackgroundStyle(wxBG_STYLE_PAINT);
5757

58-
ToolTipManager::Bind(this, command.StrHelp(), ht_ctx, cmd_name);
5958
Bind(wxEVT_PAINT, &ToggleBitmap::OnPaint, this);
6059
Bind(wxEVT_LEFT_DOWN, &ToggleBitmap::OnMouseEvent, this);
6160
}

src/toggle_bitmap.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
/// @ingroup custom_control
3333
///
3434

35+
#include "tooltip_binding.h"
36+
3537
#include <wx/bmpbndl.h>
3638
#include <wx/control.h>
3739

@@ -42,6 +44,7 @@ class ToggleBitmap final : public wxControl {
4244
agi::Context *context;
4345
cmd::Command &command;
4446
wxBitmapBundle imgs;
47+
ToolTipBinding tool_tip_binding;
4548

4649
void OnMouseEvent(wxMouseEvent &evt);
4750
void OnPaint(wxPaintEvent &evt);
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,23 @@
2727
//
2828
// Aegisub Project http://www.aegisub.org/
2929

30-
/// @file tooltip_manager.cpp
30+
/// @file tooltip_binding.cpp
3131
/// @brief Generate tooltips for controls by combining a base text and any hotkeys found for the function
3232
/// @ingroup custom_control
3333
///
3434

35-
#include "tooltip_manager.h"
35+
#include "tooltip_binding.h"
3636

3737
#include "compat.h"
3838
#include "include/aegisub/hotkey.h"
3939

4040
#include <libaegisub/hotkey.h>
4141

42-
#include <list>
43-
#include <wx/weakref.h>
44-
#include <wx/window.h>
45-
46-
struct ToolTipBinding {
47-
wxWeakRef<wxWindow> window;
48-
wxString toolTip;
49-
const char *command;
50-
const char *context;
51-
void Update();
52-
};
53-
54-
void ToolTipManager::Bind(wxWindow *window, wxString tooltip, const char *context, const char *command) {
55-
ToolTipBinding tip{window, tooltip, command, context};
56-
tip.Update();
57-
58-
static std::list<ToolTipBinding> tips;
59-
tips.push_back(tip);
60-
// FIXME: lifetime of the connection?
61-
hotkey::inst->AddHotkeyChangeListener(&ToolTipBinding::Update, &tips.back());
42+
ToolTipBinding::ToolTipBinding(wxWindow *window, wxString tooltip, const char *context, const char *command)
43+
: window(window), toolTip(tooltip), context(context), command(command)
44+
, connection(hotkey::inst->AddHotkeyChangeListener(&ToolTipBinding::Update, this))
45+
{
46+
Update();
6247
}
6348

6449
void ToolTipBinding::Update() {
Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,29 @@
2727
//
2828
// Aegisub Project http://www.aegisub.org/
2929

30-
class wxString;
31-
class wxWindow;
30+
#pragma once
31+
32+
#include <libaegisub/signal.h>
33+
34+
#include <wx/weakref.h>
35+
#include <wx/window.h>
36+
37+
class ToolTipBinding {
38+
private:
39+
wxWeakRef<wxWindow> window;
40+
wxString toolTip;
41+
const char *context;
42+
const char *command;
43+
agi::signal::Connection connection;
44+
45+
void Update();
3246

33-
class ToolTipManager {
3447
public:
35-
static void Bind(wxWindow *window, wxString tooltip, const char *context, const char *command);
48+
ToolTipBinding(wxWindow *window, wxString tooltip, const char *context, const char *command);
49+
50+
ToolTipBinding(const ToolTipBinding&) = delete;
51+
ToolTipBinding& operator=(const ToolTipBinding&) = delete;
52+
53+
ToolTipBinding(ToolTipBinding&&) = delete;
54+
ToolTipBinding& operator=(ToolTipBinding&&) = delete;
3655
};

0 commit comments

Comments
 (0)