[blam/gtk-builder] Make the clipboard request observable



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

    Make the clipboard request observable

 blam.csproj                |    1 +
 src/ClipboardExtensions.cs |   21 +++++++++++++++++++++
 src/Dialogs.cs             |   37 +++++++++++++++++++------------------
 3 files changed, 41 insertions(+), 18 deletions(-)
---
diff --git a/blam.csproj b/blam.csproj
index fe80bdc..757f431 100644
--- a/blam.csproj
+++ b/blam.csproj
@@ -123,6 +123,7 @@
     <Compile Include="src\TreeSelectionExtensions.cs" />
     <Compile Include="src\TreeViewExtensions.cs" />
     <Compile Include="src\EntryExtensions.cs" />
+    <Compile Include="src\ClipboardExtensions.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>
diff --git a/src/ClipboardExtensions.cs b/src/ClipboardExtensions.cs
new file mode 100644
index 0000000..213948d
--- /dev/null
+++ b/src/ClipboardExtensions.cs
@@ -0,0 +1,21 @@
+using System.Reactive.Subjects;
+using System;
+using System.Threading.Tasks;
+
+namespace Blam
+{
+       public static class ClipboardExtensions
+       {
+               public static IObservable<string> RequestTextAsync(this Gtk.Clipboard clip)
+               {
+                       var subj = new ReplaySubject<string>();
+                       clip.RequestText((_, text) => {
+                               subj.OnNext(text);
+                               subj.OnCompleted();
+                       });
+
+                       return subj;
+               }
+       }
+}
+
diff --git a/src/Dialogs.cs b/src/Dialogs.cs
index d0ccf3d..33aa394 100644
--- a/src/Dialogs.cs
+++ b/src/Dialogs.cs
@@ -73,7 +73,7 @@ namespace Blam
                Entry passwordEntry;
                Dialog dialog;
 
-               IDisposable urlChange;
+               IDisposable okSensitive;
                IDisposable activation;
 
                IObservable<EventPattern<EventArgs>> urlChanged {
@@ -84,6 +84,15 @@ namespace Blam
                        }
                }
 
+               void setInitialText(string text)
+               {
+                       if (!String.IsNullOrEmpty(text) && new[] {"http://";, 
"https://"}.Any(text.StartsWith)) {
+                               Url = text;
+                       } else {
+                               Url = String.Empty;
+                       }
+               }
+
                public AddChannelDialog(Gtk.Window parent)
                {
                        var bld = new Builder();
@@ -100,24 +109,16 @@ namespace Blam
                        image.Pixbuf = Pixbuf.LoadFromResource("blam-add-news.png");
 
                        var clipboard = Gtk.Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", true));
-                       clipboard.RequestText((c, text) => {
-                               if (!String.IsNullOrEmpty(text) && new[] {"http://";, 
"https://"}.Any(text.StartsWith)) {
-                                       Url = text;
-                               } else {
-                                       Url = String.Empty;
-                               }
-
-                               // seed a dummy event so we take the above set into account
-                               var initial = new EventPattern<EventArgs>(urlEntry, null);
-                               urlChange = urlChanged.StartWith(initial).Subscribe(obj => {
-                                       var t = ((Entry)obj.Sender).Text;
-                                       button.Sensitive = !String.IsNullOrEmpty(t);
-                               });
 
-                       });
+                       // 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))
+                               .Select(t => !String.IsNullOrEmpty(t));
+                       okSensitive = hasUrl.Subscribe(v => button.Sensitive = v);
 
                        activation = Observable.Merge(urlEntry.OnActivated(), usernameEntry.OnActivated(), 
passwordEntry.OnActivated())
-                               .Where(x => button.Sensitive)
+                               .Where(_ => button.Sensitive)
                                .Subscribe(obj => button.Click());
                }
 
@@ -131,7 +132,7 @@ namespace Blam
 
                public void Dispose()
                {
-                       urlChange.Dispose();
+                       okSensitive.Dispose();
                        activation.Dispose();
                }
        }


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