Skip to content

Instantly share code, notes, and snippets.

@mantognini
Created April 23, 2014 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mantognini/11208620 to your computer and use it in GitHub Desktop.
Save mantognini/11208620 to your computer and use it in GitHub Desktop.
From 37d455a746983975ae17ee12215dd1f381d6096f Mon Sep 17 00:00:00 2001
From: Marco Antognini <antognini.marco@gmail.com>
Date: Wed, 23 Apr 2014 11:26:44 +0200
Subject: [PATCH] Added OS X impl of requestFocus and hasFocus
---
src/SFML/Window/OSX/SFViewController.mm | 18 ++++++++++++++++++
src/SFML/Window/OSX/SFWindowController.mm | 15 +++++++++++++++
src/SFML/Window/OSX/WindowImplCocoa.hpp | 17 +++++++++++++++++
src/SFML/Window/OSX/WindowImplCocoa.mm | 14 ++++++++++++++
src/SFML/Window/OSX/WindowImplDelegateProtocol.h | 17 +++++++++++++++++
5 files changed, 81 insertions(+)
diff --git a/src/SFML/Window/OSX/SFViewController.mm b/src/SFML/Window/OSX/SFViewController.mm
index 05e9dc9..7633e63 100644
--- a/src/SFML/Window/OSX/SFViewController.mm
+++ b/src/SFML/Window/OSX/SFViewController.mm
@@ -184,6 +184,24 @@
////////////////////////////////////////////////////////
+-(BOOL)requestFocus
+{
+ // Note: this doesn't imply that the view will get any event.
+ // The user has to make sure events are forwarded to the view
+ // with the usual responder chain.
+ [[m_view window] makeKeyAndOrderFront:nil];
+ return [self hasFocus];
+}
+
+
+////////////////////////////////////////////////////////////
+-(BOOL)hasFocus
+{
+ return [NSApp keyWindow] == [m_view window];
+}
+
+
+////////////////////////////////////////////////////////
-(void)enableKeyRepeat
{
[m_oglView enableKeyRepeat];
diff --git a/src/SFML/Window/OSX/SFWindowController.mm b/src/SFML/Window/OSX/SFWindowController.mm
index e045d08..591b93b 100644
--- a/src/SFML/Window/OSX/SFWindowController.mm
+++ b/src/SFML/Window/OSX/SFWindowController.mm
@@ -434,6 +434,21 @@
////////////////////////////////////////////////////////
+-(BOOL)requestFocus
+{
+ [m_window makeKeyAndOrderFront:nil];
+ return [self hasFocus];
+}
+
+
+////////////////////////////////////////////////////////////
+-(BOOL)hasFocus
+{
+ return [NSApp keyWindow] == m_window;
+}
+
+
+////////////////////////////////////////////////////////
-(void)enableKeyRepeat
{
[m_oglView enableKeyRepeat];
diff --git a/src/SFML/Window/OSX/WindowImplCocoa.hpp b/src/SFML/Window/OSX/WindowImplCocoa.hpp
index f8b209a..71dc51d 100644
--- a/src/SFML/Window/OSX/WindowImplCocoa.hpp
+++ b/src/SFML/Window/OSX/WindowImplCocoa.hpp
@@ -320,6 +320,23 @@ public :
////////////////////////////////////////////////////////////
virtual void setKeyRepeatEnabled(bool enabled);
+ ////////////////////////////////////////////////////////////
+ /// \brief Request the current window to be made the active
+ /// foreground window
+ ///
+ /// \return True if operation was successful, false otherwise
+ ///
+ ////////////////////////////////////////////////////////////
+ virtual bool requestFocus() const;
+
+ ////////////////////////////////////////////////////////////
+ /// \brief Determine whether the window has the input focus
+ ///
+ /// \return True if window has focus, false otherwise
+ ///
+ ////////////////////////////////////////////////////////////
+ virtual bool hasFocus() const;
+
protected :
////////////////////////////////////////////////////////////
diff --git a/src/SFML/Window/OSX/WindowImplCocoa.mm b/src/SFML/Window/OSX/WindowImplCocoa.mm
index 6c77280..8d6a7c3 100644
--- a/src/SFML/Window/OSX/WindowImplCocoa.mm
+++ b/src/SFML/Window/OSX/WindowImplCocoa.mm
@@ -499,6 +499,20 @@ void WindowImplCocoa::setKeyRepeatEnabled(bool enabled)
}
+////////////////////////////////////////////////////////////
+bool WindowImplCocoa::requestFocus() const
+{
+ return [m_delegate requestFocus];
+}
+
+
+////////////////////////////////////////////////////////////
+bool WindowImplCocoa::hasFocus() const
+{
+ return [m_delegate hasFocus];
+}
+
+
} // namespace priv
} // namespace sf
diff --git a/src/SFML/Window/OSX/WindowImplDelegateProtocol.h b/src/SFML/Window/OSX/WindowImplDelegateProtocol.h
index 2f3a63c..e37a7c9 100644
--- a/src/SFML/Window/OSX/WindowImplDelegateProtocol.h
+++ b/src/SFML/Window/OSX/WindowImplDelegateProtocol.h
@@ -163,6 +163,23 @@ namespace sf {
-(void)closeWindow;
////////////////////////////////////////////////////////////
+/// \brief Request the current window to be made the active
+/// foreground window
+///
+/// \return True if operation was successful, false otherwise
+///
+////////////////////////////////////////////////////////////
+-(BOOL)requestFocus;
+
+////////////////////////////////////////////////////////////
+/// \brief Determine whether the window has the input focus
+///
+/// \return True if window has focus, false otherwise
+///
+////////////////////////////////////////////////////////////
+-(BOOL)hasFocus;
+
+////////////////////////////////////////////////////////////
/// \brief Enable key repeat
///
////////////////////////////////////////////////////////////
--
1.8.5.2 (Apple Git-48)
@eXpl0it3r
Copy link

Feel free to remove this Gist or edit the title to mirror the repository URL change. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment