Easily manage windows in OS X

I’ve been using OS X for many years already, and as my primary computer OS for almost a year. Before that, I used to spend time with Windows (like 90% of it), Linux (for server) and OS X.

There are two things that I missed after switching from Windows (last version I regularly used was 8.1) to OS X (I started to regularly use it on Tiger), being a heavy keyboard user: the way window cycling works on Windows, and Windows snap (available since Windows 7, including shortcuts).

Window switch

Window switch shortcut on Windows (Alt+Tab) cycles through all your windows, in usage order, but on OS X (Cmd+Tab) cycles through applications. There is a second shortcut, which usually is assigned to Cmd+`. This shortcut is quite cumbersome, at least on Spanish keyboard layout (maybe also on English layout too): as soon you enter in a text field of any kind, if you press this shortcut you get the ` character, waiting for you to complete an accented letter (yes, in Catalan and Spanish languages we have such a thing). Additionally you cycle through your windows immediately, without preview, and without remembering your usage order.

There are two solutions here:

  • change the shortcut in System Preferences (preview and usage order still missing here),
  • use Witch (thanks to Jaume Sánchez for pointing me this out).

I prefer using Witch, as it has lots of options, allowing you to use a switcher more like Windows one, making no distinction between windows and apps, or use it the way I do, which is to use it as an improvement to OS X default way of switching between windows inside an application. BTW I use Alt+Cmd+Tab as this shortcut, and doesn’t conflict with any other shortcut, working all the time.

Windows Snap

When working with many windows, sometimes is useful to be able to “snap” windows to one side of the screen or the other. This feature is really useful on Windows (starting from 7), but I’ve been missing it on OS X since the beginning. Also, maximizing not always maximizes in both directions, but only in vertical (depends on the application). Finally, on OS X “El Capitan”, maximize button has changed to fullscreen as the default option, having to press Alt to maximize. This version of OS X also includes a feature like Snap, but only works for fullscreen apps.

So, recently I found a way of overcoming this and having nice shortcuts “a-la-Windows”: Spectacle. This nice app sits in your menu bar and helps you to quickly resize your windows (snap, fullscreen, even quarter of screen snap), move them between screens, even undo and redo this actions!

Disabling right click and swipe on Windows Store Apps without using code-behind

A month ago I was working on a Windows Store project and I needed to disable right click and swipe for some grid controls. Of course, the property to disable/enable swipe is well known: IsSwipeEnabled. Just put a false there and you’re ready to go! But I could not find an equivalent property to disable right-click, and IsSwipeEnabled does exactly what it says: disable swipe touch gesture, but has no effect on right-click (which you may expect it will, because right-click is the equivalent manipulation using mouse).

Anyway, I found that the only way to disable right-click was to handle the right-click event, using code. So, my first thought was using code behind, and add an event handler and tell the remaining handlers that I handled the event already. So an event handler like this one should do it:

private static void RightClickEnabledProperty_RightTapped(object sender, Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e)
{
    e.Handled = true;
}

But then, I thought I rather had a property implemented to be able to use it whenever I wanted without having to add more code behind to each XAML. So, here is the implementation for this property:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;

namespace App.Properties
{
    public class RightClickEnabledProperty
    {
        public static readonly DependencyProperty IsRightClickEnabledProperty =
            DependencyProperty.RegisterAttached("IsRightClickEnabled", typeof(bool), typeof(RightClickEnabledProperty),
                                                new PropertyMetadata(true, IsRightClickEnabledPropertyChanged));


        public static void SetIsRightClickEnabled(DependencyObject attached, bool value)
        {
            attached.SetValue(IsRightClickEnabledProperty, value);
        }


        public static bool GetIsRightClickEnabled(DependencyObject attached)
        {
            return (bool)attached.GetValue(IsRightClickEnabledProperty);
        }


        private static void IsRightClickEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            bool? value = e.NewValue as bool?;
            if (!value.HasValue || value.Value)
                (d as UIElement).RightTapped -= RightClickEnabledProperty_RightTapped;
            else
                (d as UIElement).RightTapped += RightClickEnabledProperty_RightTapped;
        }

        private static void RightClickEnabledProperty_RightTapped(object sender, Windows.UI.Xaml.Input.RightTappedRoutedEventArgs e)
        {
            UIElement control = sender as UIElement;
            if (control == null)
                return;

            if (!GetIsRightClickEnabled(control))
                e.Handled = true;
        }

    }
}

The downside of this is that the property must be defined in the item itself (usually in the top-level control or container inside the ItemTemplate), while the IsSwipeEnabled property is defined in the Grid or ListView itself.

See an example (remember to define the namespace in the root node of the XAML, like: xmlns:properties="using:App.Properties"):

<ListView
    ItemsSource="{Binding ElementList}"
    SelectedItem="{Binding SelectedElement, Mode=TwoWay}"
    IsSwipeEnabled="False"
    SelectionMode="Single">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock
                Text="{Binding ElementName}"
                properties:RightClickEnabledProperty.IsRightClickEnabled="False"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Hope this helps to keep your XAML a little more clean, with less code-behind.