[blam/gtk-builder] Move the remaining FromEventPattern to an extension method



commit efa31f19a4389a11884fe9b50808a8510904143e
Author: Carlos Martín Nieto <cmn dwim me>
Date:   Mon Oct 7 21:34:04 2013 +0200

    Move the remaining FromEventPattern to an extension method

 blam.csproj               |    1 +
 src/Blam.cs               |   10 ++++------
 src/Dialogs.cs            |   11 +----------
 src/EntryExtensions.cs    |   10 ++++++++++
 src/MenuItemExtensions.cs |   17 +++++++++++++++++
 5 files changed, 33 insertions(+), 16 deletions(-)
---
diff --git a/blam.csproj b/blam.csproj
index 757f431..589ea60 100644
--- a/blam.csproj
+++ b/blam.csproj
@@ -124,6 +124,7 @@
     <Compile Include="src\TreeViewExtensions.cs" />
     <Compile Include="src\EntryExtensions.cs" />
     <Compile Include="src\ClipboardExtensions.cs" />
+    <Compile Include="src\MenuItemExtensions.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>
diff --git a/src/Blam.cs b/src/Blam.cs
index 528ecd7..eebedb8 100644
--- a/src/Blam.cs
+++ b/src/Blam.cs
@@ -71,12 +71,10 @@ namespace Blam
                void wireMenuItems(Builder bld)
                {
                        var item = bld.GetObject<MenuItem>("menu-add-channel");
-                       Observable.FromEventPattern<EventHandler, EventArgs>(
-                               x => item.Activated += x, x => item.Activated -= x)
-                               .Subscribe(obj => {
-                                       using (var dialog = new AddChannelDialog(MainWindow))
-                                               addChannel(dialog);
-                               });
+                       item.ObserveActivated().Subscribe(obj => {
+                               using (var dialog = new AddChannelDialog(MainWindow))
+                                       addChannel(dialog);
+                       });
                }
 
                void addChannel(AddChannelDialog dialog)
diff --git a/src/Dialogs.cs b/src/Dialogs.cs
index 13ee5fd..c88275b 100644
--- a/src/Dialogs.cs
+++ b/src/Dialogs.cs
@@ -76,14 +76,6 @@ namespace Blam
                IDisposable okSensitive;
                IDisposable activation;
 
-               IObservable<EventPattern<EventArgs>> urlChanged {
-                       get {
-                               return Observable.FromEventPattern<EventHandler, EventArgs>(
-                                       x => urlEntry.Changed += x,
-                                       x => urlEntry.Changed -= x);
-                       }
-               }
-
                void setInitialText(string text)
                {
                        if (!String.IsNullOrEmpty(text) && new[] {"http://";, 
"https://"}.Any(text.StartsWith)) {
@@ -112,8 +104,7 @@ namespace Blam
 
                        // RequestTextAsync() will provide the initial text (if we find it's acceptable) and 
will cause the
                        // check for the button's senstivity to run that first time
-                       var urlText = urlChanged.Select(obj => ((Entry)obj.Sender).Text);
-                       var hasUrl = Observable.Merge(urlText, 
clipboard.RequestTextAsync().Do(setInitialText).Select(_ => Url))
+                       var hasUrl = Observable.Merge(urlEntry.ObserveTextChanged(), 
clipboard.RequestTextAsync().Do(setInitialText).Select(_ => Url))
                                .Select(t => !String.IsNullOrEmpty(t));
                        okSensitive = hasUrl.Subscribe(v => button.Sensitive = v);
 
diff --git a/src/EntryExtensions.cs b/src/EntryExtensions.cs
index d3f31f9..5a144b4 100644
--- a/src/EntryExtensions.cs
+++ b/src/EntryExtensions.cs
@@ -1,6 +1,7 @@
 using System;
 using System.Reactive;
 using System.Reactive.Linq;
+using Gtk;
 
 namespace Blam
 {
@@ -20,6 +21,15 @@ namespace Blam
                                x => entry.Changed -= x);
                }
 
+               /// <summary>
+               /// Get a stream of the text in an entry whenever it changes.
+               /// </summary>
+               /// <returns>An Observable with the text any time it changes</returns>
+               /// <param name="entry">Entry.</param>
+               public static IObservable<string> ObserveTextChanged(this Entry entry)
+               {
+                       return entry.OnChanged().Select(obj => ((Entry)obj.Sender).Text);
+               }
        }
 }
 
diff --git a/src/MenuItemExtensions.cs b/src/MenuItemExtensions.cs
new file mode 100644
index 0000000..25cf0d7
--- /dev/null
+++ b/src/MenuItemExtensions.cs
@@ -0,0 +1,17 @@
+using System;
+using System.Reactive;
+using System.Reactive.Linq;
+using Gtk;
+
+namespace Blam
+{
+       public static class MenuItemExtensions
+       {
+               public static IObservable<EventPattern<EventArgs>> ObserveActivated(this MenuItem item)
+               {
+                       return Observable.FromEventPattern<EventHandler, EventArgs>(
+                               x => item.Activated += x, x => item.Activated -= x);
+               }
+       }
+}
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]