Dynamically switch to the retina toolbar icons as appropriate

This commit is contained in:
Thomas Goyne 2013-12-26 19:23:59 -08:00
parent f4a6d90db4
commit c8368a500b
7 changed files with 115 additions and 10 deletions

View File

@ -16,7 +16,7 @@ LIBS += $(LIBS_FONTCONFIG) $(LIBS_FFTW3) $(LIBS_UCHARDET) $(LIBS_BOOST)
LIBS += $(LIBS_ICU) $(LIBS_LUA)
ifeq (yes, $(BUILD_DARWIN))
SRC += osx_utils.mm
SRC += osx_utils.mm retina_helper.mm
endif
###############

View File

@ -62,7 +62,3 @@ void SetPlaceholderText(wxWindow *window, wxString const& placeholder) {
}
}
double GetScaleFactor(wxWindow *window) {
NSWindow *nsWindow = [window->GetHandle() window];
return [nsWindow respondsToSelector:@selector(backingScaleFactor)] ? nsWindow.backingScaleFactor : 1.0;
}

View File

@ -0,0 +1,32 @@
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Aegisub Project http://www.aegisub.org/
#include <libaegisub/signal.h>
class wxWindow;
class RetinaHelper {
wxWindow *window;
void *observer;
agi::signal::Signal<int> ScaleFactorChanged;
public:
RetinaHelper(wxWindow *window);
~RetinaHelper();
int GetScaleFactor() const;
DEFINE_SIGNAL_ADDERS(ScaleFactorChanged, AddScaleFactorListener)
};

View File

@ -0,0 +1,64 @@
// Copyright (c) 2013, Thomas Goyne <plorkyeran@aegisub.org>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Aegisub Project http://www.aegisub.org/
#include "config.h"
#include "retina_helper.h"
#include <Cocoa/Cocoa.h>
#include <wx/window.h>
@interface RetinaObserver : NSObject
@property (nonatomic, assign) NSWindow *window;
@property (nonatomic, copy) void (^block)();
@end
@implementation RetinaObserver
- (void)backingPropertiesDidChange:(NSNotification *)notification {
self.block();
}
- (void)dealloc {
[_block release];
[super dealloc];
}
@end
RetinaHelper::RetinaHelper(wxWindow *window)
: window(window)
, observer([RetinaObserver new])
{
RetinaObserver *obs = (id)observer;
obs.window = window->GetHandle().window;
obs.block = ^{ ScaleFactorChanged(GetScaleFactor()); };
NSNotificationCenter *nc = NSNotificationCenter.defaultCenter;
[nc addObserver:(id)observer
selector:@selector(backingPropertiesDidChange:)
name:NSWindowDidChangeBackingPropertiesNotification
object:window->GetHandle().window];
}
RetinaHelper::~RetinaHelper() {
[NSNotificationCenter.defaultCenter removeObserver:(id)observer];
[(id)observer release];
}
int RetinaHelper::GetScaleFactor() const {
return static_cast<int>(window->GetHandle().window.backingScaleFactor);
}

View File

@ -26,6 +26,7 @@
#include "include/aegisub/hotkey.h"
#include "libresrc/libresrc.h"
#include "options.h"
#include "retina_helper.h"
#include "utils.h"
#include <libaegisub/hotkey.h>
@ -61,6 +62,9 @@ namespace {
std::vector<cmd::Command *> commands;
/// Hotkey context
std::string ht_context;
RetinaHelper retina_helper;
/// Current icon size
int icon_size;
@ -170,6 +174,7 @@ namespace {
, name(std::move(name))
, context(c)
, ht_context(std::move(ht_context))
, retina_helper(parent)
, icon_size(OPT_GET("App/Toolbar Icon Size")->GetInt())
, icon_size_slot(OPT_SUB("App/Toolbar Icon Size", &Toolbar::OnIconSizeChange, this))
, hotkeys_changed_slot(hotkey::inst->AddHotkeyChangeListener(&Toolbar::RegenerateToolbar, this))
@ -183,11 +188,16 @@ namespace {
, name(std::move(name))
, context(c)
, ht_context(std::move(ht_context))
, retina_helper(parent)
#ifndef __WXMAC__
, icon_size(OPT_GET("App/Toolbar Icon Size")->GetInt())
, icon_size_slot(OPT_SUB("App/Toolbar Icon Size", &Toolbar::OnIconSizeChange, this))
#else
, icon_size(32 * GetScaleFactor(parent))
, icon_size(32 * retina_helper.GetScaleFactor())
, icon_size_slot(retina_helper.AddScaleFactorListener([=](double scale) {
icon_size = 32 * retina_helper.GetScaleFactor();
RegenerateToolbar();
}))
#endif
, hotkeys_changed_slot(hotkey::inst->AddHotkeyChangeListener(&Toolbar::RegenerateToolbar, this))
{

View File

@ -40,6 +40,7 @@
#include "frame_main.h"
#include "main.h"
#include "options.h"
#include "retina_helper.h"
#include <libaegisub/ass/dialogue_parser.h>
#include <libaegisub/dispatch.h>
@ -267,11 +268,15 @@ size_t MaxLineLength(std::string const& text, bool ignore_whitespace) {
}
// OS X implementation in osx_utils.mm
#ifndef __WXOSX_COCOA__
// OS X implementation in osx_utils.mm
void AddFullScreenButton(wxWindow *) { }
void SetFloatOnParent(wxWindow *) { }
double GetScaleFactor(wxWindow *) { return 1; }
// OS X implementation in retina_helper.mm
RetinaHelper::RetinaHelper(wxWindow *) { }
RetinaHelper::~RetinaHelper() { }
int RetinaHelper::GetScaleFactor() const { return 1; }
#endif
wxString FontFace(std::string opt_prefix) {

View File

@ -78,8 +78,6 @@ void SetFloatOnParent(wxWindow *window);
void SetPlaceholderText(wxWindow *window, wxString const& placeholder);
double GetScaleFactor(wxWindow *window);
/// Forward a mouse wheel event to the window under the mouse if needed
/// @param source The initial target of the wheel event
/// @param evt The event