Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
[Mac] Let embedded NSTextViews handle certain accelerators if focused
Browse files Browse the repository at this point in the history
If an NSTextView has focus, always allow it to handle Cmd-[acxvz]
accelerators.

This depends upon bockbuild 2ef2dbdc7644a941d3665357bb760047dc05ff1d

Fixes https://bugzilla.xamarin.com/show_bug.cgi?id=20732
  • Loading branch information
bratsche committed Jan 22, 2015
1 parent d3edb28 commit edaf3fc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Expand Up @@ -75,6 +75,9 @@ public static class GtkWorkarounds
[DllImport (PangoUtil.LIBQUARTZ)]
static extern IntPtr gdk_quartz_window_get_nswindow (IntPtr window);

[DllImport (PangoUtil.LIBQUARTZ)]
static extern bool gdk_window_has_embedded_nsview_focus (IntPtr window);

struct CGRect32
{
public float X, Y, Width, Height;
Expand Down Expand Up @@ -815,6 +818,19 @@ public static void UpdateNativeShadow (Gtk.Window window)
objc_msgSend_IntPtr (ptr, sel_invalidateShadow);
}

public static bool HasNSTextFieldFocus (Gdk.Window window)
{
if (Platform.IsMac) {
try {
return gdk_window_has_embedded_nsview_focus (window.Handle);
} catch (Exception e) {
return false;
}
} else {
return false;
}
}

[DllImport (PangoUtil.LIBGTKGLUE, CallingConvention = CallingConvention.Cdecl)]
static extern void gtksharp_container_leak_fixed_marker ();

Expand Down
Expand Up @@ -303,18 +303,37 @@ bool CanUseBinding (KeyboardShortcut[] chords, KeyboardShortcut[] accels, out Ke
}

public event EventHandler<KeyBindingFailedEventArgs> KeyBindingFailed;


bool IsShortcutHandledByEmbeddedNSView (Gdk.Window window, KeyboardShortcut[] accels)
{
if (Mono.TextEditor.GtkWorkarounds.HasNSTextFieldFocus (window)) {
foreach (KeyboardShortcut ks in accels) {
if (ks.Modifier == Gdk.ModifierType.MetaMask && (
ks.Key == Gdk.Key.a || ks.Key == Gdk.Key.c ||
ks.Key == Gdk.Key.x || ks.Key == Gdk.Key.v ||
ks.Key == Gdk.Key.z)) {
return true;
}
}
}

return false;
}

[GLib.ConnectBefore]
void OnKeyPressed (object o, Gtk.KeyPressEventArgs e)
{
if (!IsEnabled)
return;

RegisterUserInteraction ();

bool complete;
KeyboardShortcut[] accels = KeyBindingManager.AccelsFromKey (e.Event, out complete);


if (IsShortcutHandledByEmbeddedNSView (e.Event.Window, accels))
return;

if (!complete) {
// incomplete accel
e.RetVal = true;
Expand Down

2 comments on commit edaf3fc

@Therzok
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bratsche I don't think this is needed anymore. The CommandManager handles NSViews now. Can we revert it? It breaks edit commands in mono 4.0+

@bratsche
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the original issue without this code and it seems to mostly be fine. The only thing that doesn't work then is Cmd-Z.

Please sign in to comment.